# HG changeset patch # User Paul Boddie # Date 1491171226 -7200 # Node ID 6487964362bacf51058af435dd17f899da424287 # Parent ea84f496b3f687c77aaf9667469d17a97e7688d6 Convert operators with lists of operands to pairwise operator invocations. diff -r ea84f496b3f6 -r 6487964362ba common.py --- a/common.py Mon Apr 03 00:12:47 2017 +0200 +++ b/common.py Mon Apr 03 00:13:46 2017 +0200 @@ -620,12 +620,44 @@ Process the given operator node 'n' as an operator function invocation. """ - op = operator_functions[n.__class__.__name__] - invocation = compiler.ast.CallFunc( + opname = n.__class__.__name__ + operands = n.getChildNodes() + + # Convert a unary operation to an invocation. + + op = unary_operator_functions.get(opname) + + if op: + invocation = compiler.ast.CallFunc( + compiler.ast.Name("$op%s" % op), + [operands[0]] + ) + + # Convert a single operator with a list of operands to a combination of + # pairwise operations. + + else: + op = operator_functions[opname] + invocation = self._process_operator_node(op, operands) + + return self.process_structure_node(invocation) + + def _process_operator_node(self, op, operands): + + """ + Process the given 'op', being an operator function, together with the + supplied 'operands', returning either a single remaining operand or an + invocation combining the operands. + """ + + remaining = operands[1:] + if not remaining: + return operands[0] + + return compiler.ast.CallFunc( compiler.ast.Name("$op%s" % op), - list(n.getChildNodes()) + [operands[0], self._process_operator_node(op, remaining)] ) - return self.process_structure_node(invocation) def process_print_node(self, n): @@ -1548,6 +1580,15 @@ predefined_constants = "False", "None", "NotImplemented", "True" +unary_operator_functions = { + + # Unary operations. + + "Invert" : "invert", + "UnaryAdd" : "pos", + "UnarySub" : "neg", + } + operator_functions = { # Fundamental operations. @@ -1572,12 +1613,6 @@ "RightShift" : "rshift", "Sub" : "sub", - # Unary operations. - - "Invert" : "invert", - "UnaryAdd" : "pos", - "UnarySub" : "neg", - # Augmented assignment. "+=" : "iadd", @@ -1603,4 +1638,6 @@ ">" : "gt", } +operator_functions.update(unary_operator_functions) + # vim: tabstop=4 expandtab shiftwidth=4