Commit 75a35b4b authored by Dylan Trotter's avatar Dylan Trotter

Implement unary negation slot.

parent 93461c68
......@@ -371,6 +371,7 @@ class ExprVisitor(ast.NodeVisitor):
_UNARY_OP_TEMPLATES = {
ast.Invert: 'πg.Invert(πF, {operand})',
ast.USub: 'πg.Neg(πF, {operand})',
}
def visit_function_inline(self, node):
......
......@@ -759,6 +759,16 @@ func ToStr(f *Frame, o *Object) (*Str, *BaseException) {
return toStrUnsafe(result), nil
}
// Neg returns the result of o.__neg__ and is equivalent to the Python
// expression "-o".
func Neg(f *Frame, o *Object) (*Object, *BaseException) {
neg := o.typ.slots.Neg
if neg == nil {
return nil, f.RaiseType(TypeErrorType, fmt.Sprintf("bad operand type for unary ~: '%s'", o.typ.Name()))
}
return neg.Fn(f, o)
}
// Xor returns the result of the bitwise xor operator v ^ w according to
// __xor/rxor__.
func Xor(f *Frame, v, w *Object) (*Object, *BaseException) {
......
......@@ -408,6 +408,7 @@ type typeSlots struct {
Mul *binaryOpSlot
Native *nativeSlot
NE *binaryOpSlot
Neg *unaryOpSlot
New *newSlot
Next *unaryOpSlot
NonZero *unaryOpSlot
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment