1.1 --- a/micropython/ast.py Mon Nov 10 21:38:58 2008 +0100
1.2 +++ b/micropython/ast.py Mon Nov 10 23:57:05 2008 +0100
1.3 @@ -1014,7 +1014,7 @@
1.4 else:
1.5 self.new_op(AddressInstruction(self.get_builtin(name, node)))
1.6
1.7 - def _visitUnary(self, node, method):
1.8 + def _visitUnary(self, node):
1.9
1.10 """
1.11 _t = node.expr
1.12 @@ -1024,6 +1024,8 @@
1.13 raise TypeError
1.14 """
1.15
1.16 + method = unary_methods[node.__class__.__name__]
1.17 +
1.18 type_error_block = self.new_block()
1.19 end_block = self.new_block()
1.20
1.21 @@ -1074,7 +1076,7 @@
1.22 self.discard_temp(temp)
1.23 self.discard_temp(temp_out)
1.24
1.25 - def _visitBinary(self, node, left_method, right_method):
1.26 + def _visitBinary(self, node):
1.27
1.28 """
1.29 _t1 = node.left
1.30 @@ -1092,6 +1094,8 @@
1.31 raise TypeError
1.32 """
1.33
1.34 + left_method, right_method = binary_methods[node.__class__.__name__]
1.35 +
1.36 # Evaluate and store the left operand in temporary storage.
1.37
1.38 self.dispatch(node.left)
1.39 @@ -1278,52 +1282,24 @@
1.40
1.41 # Binary operators.
1.42
1.43 - def visitAdd(self, node):
1.44 - self._visitBinary(node, "__add__", "__radd__")
1.45 -
1.46 - def visitBitand(self, node):
1.47 - self._visitBinary(node, "__and__", "__rand__")
1.48 -
1.49 - def visitBitor(self, node):
1.50 - self._visitBinary(node, "__or__", "__ror__")
1.51 -
1.52 - def visitBitxor(self, node):
1.53 - self._visitBinary(node, "__xor__", "__rxor__")
1.54 -
1.55 - def visitDiv(self, node):
1.56 - self._visitBinary(node, "__div__", "__rdiv__")
1.57 -
1.58 - def visitFloorDiv(self, node):
1.59 - self._visitBinary(node, "__floordiv__", "__rfloordiv__")
1.60 -
1.61 - def visitLeftShift(self, node):
1.62 - self._visitBinary(node, "__lshift__", "__rlshift__")
1.63 -
1.64 - def visitMod(self, node):
1.65 - self._visitBinary(node, "__mod__", "__rmod__")
1.66 -
1.67 - def visitMul(self, node):
1.68 - self._visitBinary(node, "__mul__", "__rmul__")
1.69 -
1.70 - def visitPower(self, node):
1.71 - self._visitBinary(node, "__pow__", "__rpow__")
1.72 -
1.73 - def visitRightShift(self, node):
1.74 - self._visitBinary(node, "__rshift__", "__rrshift__")
1.75 -
1.76 - def visitSub(self, node):
1.77 - self._visitBinary(node, "__sub__", "__rsub__")
1.78 + visitAdd = _visitBinary
1.79 + visitBitand = _visitBinary
1.80 + visitBitor = _visitBinary
1.81 + visitBitxor = _visitBinary
1.82 + visitDiv = _visitBinary
1.83 + visitFloorDiv = _visitBinary
1.84 + visitLeftShift = _visitBinary
1.85 + visitMod = _visitBinary
1.86 + visitMul = _visitBinary
1.87 + visitPower = _visitBinary
1.88 + visitRightShift = _visitBinary
1.89 + visitSub = _visitBinary
1.90
1.91 # Unary operators.
1.92
1.93 - def visitInvert(self, node):
1.94 - self._visitUnary(node, "__invert__")
1.95 -
1.96 - def visitUnaryAdd(self, node):
1.97 - self._visitUnary(node, "__pos__")
1.98 -
1.99 - def visitUnarySub(self, node):
1.100 - self._visitUnary(node, "__neg__")
1.101 + visitInvert = _visitUnary
1.102 + visitUnaryAdd = _visitUnary
1.103 + visitUnarySub = _visitUnary
1.104
1.105 # Logical operators.
1.106
1.107 @@ -1391,11 +1367,13 @@
1.108 self.dispatch(node.expr)
1.109 temp2 = self.optimiser.optimise_temp_storage()
1.110
1.111 + # NOTE: Replicated by some code in micropython.inspect.visitCompare.
1.112 +
1.113 last_op = node.ops[-1]
1.114
1.115 for op in node.ops:
1.116 op_name, next_node = op
1.117 - methods = self.comparison_methods[op_name]
1.118 + methods = comparison_methods[op_name]
1.119
1.120 # Propagate the arguments as we traverse the construct.
1.121
1.122 @@ -1614,7 +1592,7 @@
1.123
1.124 # Find the augmented assignment method and attempt to use it.
1.125
1.126 - aug_method, (left_method, right_method) = self.augassign_methods[node.op]
1.127 + aug_method, (left_method, right_method) = augassign_methods[node.op]
1.128 temp_out = self._generateOpMethod(node, temp1, temp2, aug_method, use_binary_block, end_block)
1.129 self.discard_temp(temp_out) # NOTE: Will re-use the same storage.
1.130
1.131 @@ -2042,33 +2020,4 @@
1.132
1.133 def visitYield(self, node): raise TranslationNotImplementedError(self.module.full_name(), node, "Yield")
1.134
1.135 - # Useful data.
1.136 -
1.137 - comparison_methods = {
1.138 - "==" : ("__eq__", "__ne__"),
1.139 - "!=" : ("__ne__", "__eq__"),
1.140 - "<" : ("__lt__", "__gt__"),
1.141 - "<=" : ("__le__", "__ge__"),
1.142 - ">=" : ("__ge__", "__le__"),
1.143 - ">" : ("__gt__", "__lt__"),
1.144 - "is" : None,
1.145 - "is not" : None,
1.146 - "in" : None,
1.147 - "not in" : None
1.148 - }
1.149 -
1.150 - augassign_methods = {
1.151 - "+=" : ("__iadd__", ("__add__", "__radd__")),
1.152 - "-=" : ("__isub__", ("__sub__", "__rsub__")),
1.153 - "*=" : ("__imul__", ("__mul__", "__rmul__")),
1.154 - "/=" : ("__idiv__", ("__div__", "__rdiv__")),
1.155 - "%=" : ("__imod__", ("__mod__", "__rmod__")),
1.156 - "**=" : ("__ipow__", ("__pow__", "__rpow__")),
1.157 - "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")),
1.158 - ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")),
1.159 - "&=" : ("__iand__", ("__and__", "__rand__")),
1.160 - "^=" : ("__ixor__", ("__xor__", "__rxor__")),
1.161 - "|=" : ("__ior__", ("__or__", "__ror__"))
1.162 - }
1.163 -
1.164 # vim: tabstop=4 expandtab shiftwidth=4