# HG changeset patch # User Paul Boddie # Date 1226357825 -3600 # Node ID a25117656161dc5300f8e879867fe08211b1fa8f # Parent 13861cd5d24547327bea64ba2e2add95c0202a20 Moved method name information into the common module. Tidied the handler methods for operators in the Translation class, making use of the new binary and unary method name dictionaries which encode previously inline information. Introduced anticipated operator method name accounting to the inspect module, ensuring that required methods are not removed before code generation occurs. diff -r 13861cd5d245 -r a25117656161 micropython/ast.py --- a/micropython/ast.py Mon Nov 10 21:38:58 2008 +0100 +++ b/micropython/ast.py Mon Nov 10 23:57:05 2008 +0100 @@ -1014,7 +1014,7 @@ else: self.new_op(AddressInstruction(self.get_builtin(name, node))) - def _visitUnary(self, node, method): + def _visitUnary(self, node): """ _t = node.expr @@ -1024,6 +1024,8 @@ raise TypeError """ + method = unary_methods[node.__class__.__name__] + type_error_block = self.new_block() end_block = self.new_block() @@ -1074,7 +1076,7 @@ self.discard_temp(temp) self.discard_temp(temp_out) - def _visitBinary(self, node, left_method, right_method): + def _visitBinary(self, node): """ _t1 = node.left @@ -1092,6 +1094,8 @@ raise TypeError """ + left_method, right_method = binary_methods[node.__class__.__name__] + # Evaluate and store the left operand in temporary storage. self.dispatch(node.left) @@ -1278,52 +1282,24 @@ # Binary operators. - def visitAdd(self, node): - self._visitBinary(node, "__add__", "__radd__") - - def visitBitand(self, node): - self._visitBinary(node, "__and__", "__rand__") - - def visitBitor(self, node): - self._visitBinary(node, "__or__", "__ror__") - - def visitBitxor(self, node): - self._visitBinary(node, "__xor__", "__rxor__") - - def visitDiv(self, node): - self._visitBinary(node, "__div__", "__rdiv__") - - def visitFloorDiv(self, node): - self._visitBinary(node, "__floordiv__", "__rfloordiv__") - - def visitLeftShift(self, node): - self._visitBinary(node, "__lshift__", "__rlshift__") - - def visitMod(self, node): - self._visitBinary(node, "__mod__", "__rmod__") - - def visitMul(self, node): - self._visitBinary(node, "__mul__", "__rmul__") - - def visitPower(self, node): - self._visitBinary(node, "__pow__", "__rpow__") - - def visitRightShift(self, node): - self._visitBinary(node, "__rshift__", "__rrshift__") - - def visitSub(self, node): - self._visitBinary(node, "__sub__", "__rsub__") + visitAdd = _visitBinary + visitBitand = _visitBinary + visitBitor = _visitBinary + visitBitxor = _visitBinary + visitDiv = _visitBinary + visitFloorDiv = _visitBinary + visitLeftShift = _visitBinary + visitMod = _visitBinary + visitMul = _visitBinary + visitPower = _visitBinary + visitRightShift = _visitBinary + visitSub = _visitBinary # Unary operators. - def visitInvert(self, node): - self._visitUnary(node, "__invert__") - - def visitUnaryAdd(self, node): - self._visitUnary(node, "__pos__") - - def visitUnarySub(self, node): - self._visitUnary(node, "__neg__") + visitInvert = _visitUnary + visitUnaryAdd = _visitUnary + visitUnarySub = _visitUnary # Logical operators. @@ -1391,11 +1367,13 @@ self.dispatch(node.expr) temp2 = self.optimiser.optimise_temp_storage() + # NOTE: Replicated by some code in micropython.inspect.visitCompare. + last_op = node.ops[-1] for op in node.ops: op_name, next_node = op - methods = self.comparison_methods[op_name] + methods = comparison_methods[op_name] # Propagate the arguments as we traverse the construct. @@ -1614,7 +1592,7 @@ # Find the augmented assignment method and attempt to use it. - aug_method, (left_method, right_method) = self.augassign_methods[node.op] + aug_method, (left_method, right_method) = augassign_methods[node.op] temp_out = self._generateOpMethod(node, temp1, temp2, aug_method, use_binary_block, end_block) self.discard_temp(temp_out) # NOTE: Will re-use the same storage. @@ -2042,33 +2020,4 @@ def visitYield(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Yield") - # Useful data. - - comparison_methods = { - "==" : ("__eq__", "__ne__"), - "!=" : ("__ne__", "__eq__"), - "<" : ("__lt__", "__gt__"), - "<=" : ("__le__", "__ge__"), - ">=" : ("__ge__", "__le__"), - ">" : ("__gt__", "__lt__"), - "is" : None, - "is not" : None, - "in" : None, - "not in" : None - } - - augassign_methods = { - "+=" : ("__iadd__", ("__add__", "__radd__")), - "-=" : ("__isub__", ("__sub__", "__rsub__")), - "*=" : ("__imul__", ("__mul__", "__rmul__")), - "/=" : ("__idiv__", ("__div__", "__rdiv__")), - "%=" : ("__imod__", ("__mod__", "__rmod__")), - "**=" : ("__ipow__", ("__pow__", "__rpow__")), - "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), - ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), - "&=" : ("__iand__", ("__and__", "__rand__")), - "^=" : ("__ixor__", ("__xor__", "__rxor__")), - "|=" : ("__ior__", ("__or__", "__ror__")) - } - # vim: tabstop=4 expandtab shiftwidth=4 diff -r 13861cd5d245 -r a25117656161 micropython/common.py --- a/micropython/common.py Mon Nov 10 21:38:58 2008 +0100 +++ b/micropython/common.py Mon Nov 10 23:57:05 2008 +0100 @@ -145,4 +145,54 @@ pass +# Useful data. + +comparison_methods = { + "==" : ("__eq__", "__ne__"), + "!=" : ("__ne__", "__eq__"), + "<" : ("__lt__", "__gt__"), + "<=" : ("__le__", "__ge__"), + ">=" : ("__ge__", "__le__"), + ">" : ("__gt__", "__lt__"), + "is" : None, + "is not" : None, + "in" : None, + "not in" : None + } + +augassign_methods = { + "+=" : ("__iadd__", ("__add__", "__radd__")), + "-=" : ("__isub__", ("__sub__", "__rsub__")), + "*=" : ("__imul__", ("__mul__", "__rmul__")), + "/=" : ("__idiv__", ("__div__", "__rdiv__")), + "%=" : ("__imod__", ("__mod__", "__rmod__")), + "**=" : ("__ipow__", ("__pow__", "__rpow__")), + "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")), + ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")), + "&=" : ("__iand__", ("__and__", "__rand__")), + "^=" : ("__ixor__", ("__xor__", "__rxor__")), + "|=" : ("__ior__", ("__or__", "__ror__")) + } + +binary_methods = { + "Add" : ("__add__", "__radd__"), + "Bitand" : ("__and__", "__rand__"), + "Bitor" : ("__or__", "__ror__"), + "Bitxor" : ("__xor__", "__rxor__"), + "Div" : ("__div__", "__rdiv__"), + "FloorDiv" : ("__floordiv__", "__rfloordiv__"), + "LeftShift" : ("__lshift__", "__rlshift__"), + "Mod" : ("__mod__", "__rmod__"), + "Mul" : ("__mul__", "__rmul__"), + "Power" : ("__pow__", "__rpow__"), + "RightShift" : ("__rshift__", "__rrshift__"), + "Sub" : ("__sub__", "__rsub__") + } + +unary_methods = { + "Invert" : "__invert__", + "UnaryAdd" : "__pos__", + "UnarySub" : "__neg__" + } + # vim: tabstop=4 expandtab shiftwidth=4 diff -r 13861cd5d245 -r a25117656161 micropython/inspect.py --- a/micropython/inspect.py Mon Nov 10 21:38:58 2008 +0100 +++ b/micropython/inspect.py Mon Nov 10 23:57:05 2008 +0100 @@ -314,6 +314,23 @@ self.dispatch(n) return Instance() + def _visitUnary(self, node): + + "Accounting method for the unary operator 'node'." + + method = unary_methods[node.__class__.__name__] + self.importer.use_name(method) + return self.OP(node) + + def _visitBinary(self, node): + + "Accounting method for the binary operator 'node'." + + left_method, right_method = binary_methods[node.__class__.__name__] + self.importer.use_name(left_method) + self.importer.use_name(right_method) + return self.OP(node) + def _visitFunction(self, node, name): """ @@ -384,7 +401,9 @@ self.add_object(function, any_scope=1) return function - visitAdd = OP + # Specific handler methods. + + visitAdd = _visitBinary visitAnd = OP @@ -419,15 +438,23 @@ visitAssTuple = visitAssList - visitAugAssign = OP + def visitAugAssign(self, node): + + # Accounting. + + aug_method, (left_method, right_method) = augassign_methods[node.op] + self.importer.use_name(aug_method) + self.importer.use_name(left_method) + self.importer.use_name(right_method) + return self.OP(node) visitBackquote = OP - visitBitand = OP + visitBitand = _visitBinary - visitBitor = OP + visitBitor = _visitBinary - visitBitxor = OP + visitBitxor = _visitBinary visitBreak = NOP @@ -485,7 +512,21 @@ return cls - visitCompare = OP + def visitCompare(self, node): + + # Accounting. + # NOTE: Replicates some code in micropython.ast.visitCompare. + + for op in node.ops: + op_name, next_node = op + methods = comparison_methods[op_name] + if methods is not None: + self.importer.use_name(methods[0]) + self.importer.use_name(methods[1]) + elif op_name.endswith("in"): + self.importer.use_name("__contains__") + + return self.OP(node) def visitConst(self, node): return self.importer.make_constant(node.value) @@ -498,7 +539,7 @@ visitDiscard = NOP - visitDiv = OP + visitDiv = _visitBinary visitEllipsis = NOP @@ -506,7 +547,7 @@ visitExpression = OP - visitFloorDiv = OP + visitFloorDiv = _visitBinary def visitFor(self, node): self.in_loop = 1 @@ -613,7 +654,7 @@ return None - visitInvert = OP + visitInvert = _visitUnary def visitKeyword(self, node): self.dispatch(node.expr) @@ -624,7 +665,7 @@ def visitLambda(self, node): return self._visitFunction(node, None) - visitLeftShift = OP + visitLeftShift = _visitBinary visitList = OP @@ -634,7 +675,7 @@ visitListCompIf = NOP - visitMod = OP + visitMod = _visitBinary def visitModule(self, node): @@ -643,7 +684,7 @@ node.unit = self return self.dispatch(node.node) - visitMul = OP + visitMul = _visitBinary def visitName(self, node): name = node.name @@ -674,7 +715,7 @@ visitPass = NOP - visitPower = OP + visitPower = _visitBinary visitPrint = NOP @@ -684,7 +725,7 @@ visitReturn = NOP - visitRightShift = OP + visitRightShift = _visitBinary visitSlice = OP @@ -695,7 +736,7 @@ self.dispatch(n) return None - visitSub = OP + visitSub = _visitBinary visitSubscript = OP @@ -717,9 +758,9 @@ visitTuple = OP - visitUnaryAdd = OP + visitUnaryAdd = _visitUnary - visitUnarySub = OP + visitUnarySub = _visitUnary def visitWhile(self, node): self.in_loop = 1