1.1 --- a/common.py Mon Apr 03 00:12:47 2017 +0200
1.2 +++ b/common.py Mon Apr 03 00:13:46 2017 +0200
1.3 @@ -620,12 +620,44 @@
1.4 Process the given operator node 'n' as an operator function invocation.
1.5 """
1.6
1.7 - op = operator_functions[n.__class__.__name__]
1.8 - invocation = compiler.ast.CallFunc(
1.9 + opname = n.__class__.__name__
1.10 + operands = n.getChildNodes()
1.11 +
1.12 + # Convert a unary operation to an invocation.
1.13 +
1.14 + op = unary_operator_functions.get(opname)
1.15 +
1.16 + if op:
1.17 + invocation = compiler.ast.CallFunc(
1.18 + compiler.ast.Name("$op%s" % op),
1.19 + [operands[0]]
1.20 + )
1.21 +
1.22 + # Convert a single operator with a list of operands to a combination of
1.23 + # pairwise operations.
1.24 +
1.25 + else:
1.26 + op = operator_functions[opname]
1.27 + invocation = self._process_operator_node(op, operands)
1.28 +
1.29 + return self.process_structure_node(invocation)
1.30 +
1.31 + def _process_operator_node(self, op, operands):
1.32 +
1.33 + """
1.34 + Process the given 'op', being an operator function, together with the
1.35 + supplied 'operands', returning either a single remaining operand or an
1.36 + invocation combining the operands.
1.37 + """
1.38 +
1.39 + remaining = operands[1:]
1.40 + if not remaining:
1.41 + return operands[0]
1.42 +
1.43 + return compiler.ast.CallFunc(
1.44 compiler.ast.Name("$op%s" % op),
1.45 - list(n.getChildNodes())
1.46 + [operands[0], self._process_operator_node(op, remaining)]
1.47 )
1.48 - return self.process_structure_node(invocation)
1.49
1.50 def process_print_node(self, n):
1.51
1.52 @@ -1548,6 +1580,15 @@
1.53
1.54 predefined_constants = "False", "None", "NotImplemented", "True"
1.55
1.56 +unary_operator_functions = {
1.57 +
1.58 + # Unary operations.
1.59 +
1.60 + "Invert" : "invert",
1.61 + "UnaryAdd" : "pos",
1.62 + "UnarySub" : "neg",
1.63 + }
1.64 +
1.65 operator_functions = {
1.66
1.67 # Fundamental operations.
1.68 @@ -1572,12 +1613,6 @@
1.69 "RightShift" : "rshift",
1.70 "Sub" : "sub",
1.71
1.72 - # Unary operations.
1.73 -
1.74 - "Invert" : "invert",
1.75 - "UnaryAdd" : "pos",
1.76 - "UnarySub" : "neg",
1.77 -
1.78 # Augmented assignment.
1.79
1.80 "+=" : "iadd",
1.81 @@ -1603,4 +1638,6 @@
1.82 ">" : "gt",
1.83 }
1.84
1.85 +operator_functions.update(unary_operator_functions)
1.86 +
1.87 # vim: tabstop=4 expandtab shiftwidth=4