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
2.1 --- a/micropython/common.py Mon Nov 10 21:38:58 2008 +0100
2.2 +++ b/micropython/common.py Mon Nov 10 23:57:05 2008 +0100
2.3 @@ -145,4 +145,54 @@
2.4
2.5 pass
2.6
2.7 +# Useful data.
2.8 +
2.9 +comparison_methods = {
2.10 + "==" : ("__eq__", "__ne__"),
2.11 + "!=" : ("__ne__", "__eq__"),
2.12 + "<" : ("__lt__", "__gt__"),
2.13 + "<=" : ("__le__", "__ge__"),
2.14 + ">=" : ("__ge__", "__le__"),
2.15 + ">" : ("__gt__", "__lt__"),
2.16 + "is" : None,
2.17 + "is not" : None,
2.18 + "in" : None,
2.19 + "not in" : None
2.20 + }
2.21 +
2.22 +augassign_methods = {
2.23 + "+=" : ("__iadd__", ("__add__", "__radd__")),
2.24 + "-=" : ("__isub__", ("__sub__", "__rsub__")),
2.25 + "*=" : ("__imul__", ("__mul__", "__rmul__")),
2.26 + "/=" : ("__idiv__", ("__div__", "__rdiv__")),
2.27 + "%=" : ("__imod__", ("__mod__", "__rmod__")),
2.28 + "**=" : ("__ipow__", ("__pow__", "__rpow__")),
2.29 + "<<=" : ("__ilshift__", ("__lshift__", "__rlshift__")),
2.30 + ">>=" : ("__irshift__", ("__rshift__", "__rrshift__")),
2.31 + "&=" : ("__iand__", ("__and__", "__rand__")),
2.32 + "^=" : ("__ixor__", ("__xor__", "__rxor__")),
2.33 + "|=" : ("__ior__", ("__or__", "__ror__"))
2.34 + }
2.35 +
2.36 +binary_methods = {
2.37 + "Add" : ("__add__", "__radd__"),
2.38 + "Bitand" : ("__and__", "__rand__"),
2.39 + "Bitor" : ("__or__", "__ror__"),
2.40 + "Bitxor" : ("__xor__", "__rxor__"),
2.41 + "Div" : ("__div__", "__rdiv__"),
2.42 + "FloorDiv" : ("__floordiv__", "__rfloordiv__"),
2.43 + "LeftShift" : ("__lshift__", "__rlshift__"),
2.44 + "Mod" : ("__mod__", "__rmod__"),
2.45 + "Mul" : ("__mul__", "__rmul__"),
2.46 + "Power" : ("__pow__", "__rpow__"),
2.47 + "RightShift" : ("__rshift__", "__rrshift__"),
2.48 + "Sub" : ("__sub__", "__rsub__")
2.49 + }
2.50 +
2.51 +unary_methods = {
2.52 + "Invert" : "__invert__",
2.53 + "UnaryAdd" : "__pos__",
2.54 + "UnarySub" : "__neg__"
2.55 + }
2.56 +
2.57 # vim: tabstop=4 expandtab shiftwidth=4
3.1 --- a/micropython/inspect.py Mon Nov 10 21:38:58 2008 +0100
3.2 +++ b/micropython/inspect.py Mon Nov 10 23:57:05 2008 +0100
3.3 @@ -314,6 +314,23 @@
3.4 self.dispatch(n)
3.5 return Instance()
3.6
3.7 + def _visitUnary(self, node):
3.8 +
3.9 + "Accounting method for the unary operator 'node'."
3.10 +
3.11 + method = unary_methods[node.__class__.__name__]
3.12 + self.importer.use_name(method)
3.13 + return self.OP(node)
3.14 +
3.15 + def _visitBinary(self, node):
3.16 +
3.17 + "Accounting method for the binary operator 'node'."
3.18 +
3.19 + left_method, right_method = binary_methods[node.__class__.__name__]
3.20 + self.importer.use_name(left_method)
3.21 + self.importer.use_name(right_method)
3.22 + return self.OP(node)
3.23 +
3.24 def _visitFunction(self, node, name):
3.25
3.26 """
3.27 @@ -384,7 +401,9 @@
3.28 self.add_object(function, any_scope=1)
3.29 return function
3.30
3.31 - visitAdd = OP
3.32 + # Specific handler methods.
3.33 +
3.34 + visitAdd = _visitBinary
3.35
3.36 visitAnd = OP
3.37
3.38 @@ -419,15 +438,23 @@
3.39
3.40 visitAssTuple = visitAssList
3.41
3.42 - visitAugAssign = OP
3.43 + def visitAugAssign(self, node):
3.44 +
3.45 + # Accounting.
3.46 +
3.47 + aug_method, (left_method, right_method) = augassign_methods[node.op]
3.48 + self.importer.use_name(aug_method)
3.49 + self.importer.use_name(left_method)
3.50 + self.importer.use_name(right_method)
3.51 + return self.OP(node)
3.52
3.53 visitBackquote = OP
3.54
3.55 - visitBitand = OP
3.56 + visitBitand = _visitBinary
3.57
3.58 - visitBitor = OP
3.59 + visitBitor = _visitBinary
3.60
3.61 - visitBitxor = OP
3.62 + visitBitxor = _visitBinary
3.63
3.64 visitBreak = NOP
3.65
3.66 @@ -485,7 +512,21 @@
3.67
3.68 return cls
3.69
3.70 - visitCompare = OP
3.71 + def visitCompare(self, node):
3.72 +
3.73 + # Accounting.
3.74 + # NOTE: Replicates some code in micropython.ast.visitCompare.
3.75 +
3.76 + for op in node.ops:
3.77 + op_name, next_node = op
3.78 + methods = comparison_methods[op_name]
3.79 + if methods is not None:
3.80 + self.importer.use_name(methods[0])
3.81 + self.importer.use_name(methods[1])
3.82 + elif op_name.endswith("in"):
3.83 + self.importer.use_name("__contains__")
3.84 +
3.85 + return self.OP(node)
3.86
3.87 def visitConst(self, node):
3.88 return self.importer.make_constant(node.value)
3.89 @@ -498,7 +539,7 @@
3.90
3.91 visitDiscard = NOP
3.92
3.93 - visitDiv = OP
3.94 + visitDiv = _visitBinary
3.95
3.96 visitEllipsis = NOP
3.97
3.98 @@ -506,7 +547,7 @@
3.99
3.100 visitExpression = OP
3.101
3.102 - visitFloorDiv = OP
3.103 + visitFloorDiv = _visitBinary
3.104
3.105 def visitFor(self, node):
3.106 self.in_loop = 1
3.107 @@ -613,7 +654,7 @@
3.108
3.109 return None
3.110
3.111 - visitInvert = OP
3.112 + visitInvert = _visitUnary
3.113
3.114 def visitKeyword(self, node):
3.115 self.dispatch(node.expr)
3.116 @@ -624,7 +665,7 @@
3.117 def visitLambda(self, node):
3.118 return self._visitFunction(node, None)
3.119
3.120 - visitLeftShift = OP
3.121 + visitLeftShift = _visitBinary
3.122
3.123 visitList = OP
3.124
3.125 @@ -634,7 +675,7 @@
3.126
3.127 visitListCompIf = NOP
3.128
3.129 - visitMod = OP
3.130 + visitMod = _visitBinary
3.131
3.132 def visitModule(self, node):
3.133
3.134 @@ -643,7 +684,7 @@
3.135 node.unit = self
3.136 return self.dispatch(node.node)
3.137
3.138 - visitMul = OP
3.139 + visitMul = _visitBinary
3.140
3.141 def visitName(self, node):
3.142 name = node.name
3.143 @@ -674,7 +715,7 @@
3.144
3.145 visitPass = NOP
3.146
3.147 - visitPower = OP
3.148 + visitPower = _visitBinary
3.149
3.150 visitPrint = NOP
3.151
3.152 @@ -684,7 +725,7 @@
3.153
3.154 visitReturn = NOP
3.155
3.156 - visitRightShift = OP
3.157 + visitRightShift = _visitBinary
3.158
3.159 visitSlice = OP
3.160
3.161 @@ -695,7 +736,7 @@
3.162 self.dispatch(n)
3.163 return None
3.164
3.165 - visitSub = OP
3.166 + visitSub = _visitBinary
3.167
3.168 visitSubscript = OP
3.169
3.170 @@ -717,9 +758,9 @@
3.171
3.172 visitTuple = OP
3.173
3.174 - visitUnaryAdd = OP
3.175 + visitUnaryAdd = _visitUnary
3.176
3.177 - visitUnarySub = OP
3.178 + visitUnarySub = _visitUnary
3.179
3.180 def visitWhile(self, node):
3.181 self.in_loop = 1