# HG changeset patch # User Paul Boddie # Date 1372974369 -7200 # Node ID 76bdd4381b66eb6eb225676e2070b97277c80465 # Parent 5c759716f9f2a8bbefe4b4c0f21780f7e434a048 Fixed operator and built-in function invocations to use the "apply" function. Added a proper translation of print statements. diff -r 5c759716f9f2 -r 76bdd4381b66 micropython/syspython.py --- a/micropython/syspython.py Thu Jul 04 22:53:22 2013 +0200 +++ b/micropython/syspython.py Thu Jul 04 23:46:09 2013 +0200 @@ -132,8 +132,9 @@ op_name = operator_functions[node.op] return self.dispatch(node.node, compiler.ast.CallFunc( - module_attribute("operator", op_name), - [self.dispatch(node.node), self.dispatch(node.expr)] + special_name("apply"), + [module_attribute("operator", op_name), + self.dispatch(node.node), self.dispatch(node.expr)] )) visitBreak = NOP @@ -251,8 +252,8 @@ compiler.ast.CallFunc(special_name("storetemp"), [ temp, compiler.ast.CallFunc( - module_attribute("__builtins__", "iter"), - [self.dispatch(node.list)] + special_name("apply"), + [module_attribute("__builtins__", "iter"), self.dispatch(node.list)] ) ]), # while True: ... @@ -403,15 +404,19 @@ return compiler.ast.Stmt([]) def visitPrint(self, node): - return compiler.ast.Print( - [self.dispatch(n) for n in node.nodes], - node.dest and self.dispatch(node.dest) + return compiler.ast.CallFunc( + special_name("apply"), + [module_attribute("__builtins__", "_print"), + node.dest and self.dispatch(node.dest) or special_name("None")] + + [self.dispatch(n) for n in node.nodes] ) def visitPrintnl(self, node): - return compiler.ast.Print( - [self.dispatch(n) for n in node.nodes], - node.dest and self.dispatch(node.dest) + return compiler.ast.CallFunc( + special_name("apply"), + [module_attribute("__builtins__", "_println"), + node.dest and self.dispatch(node.dest) or special_name("None")] + + [self.dispatch(n) for n in node.nodes] ) def visitRaise(self, node): @@ -460,8 +465,8 @@ for n in node.nodes[1:]: last = compiler.ast.CallFunc( - module_attribute("operator", op_name), - [last, self.dispatch(n)] + special_name("apply"), + [module_attribute("operator", op_name), last, self.dispatch(n)] ) return last @@ -470,16 +475,17 @@ op_name = operator_functions[node.__class__.__name__] return compiler.ast.CallFunc( - module_attribute("operator", op_name), - [self.dispatch(node.left), self.dispatch(node.right)] + special_name("apply"), + [module_attribute("operator", op_name), + self.dispatch(node.left), self.dispatch(node.right)] ) def _visitUnary(self, node): op_name = operator_functions[node.__class__.__name__] return compiler.ast.CallFunc( - module_attribute("operator", op_name), - [self.dispatch(node.expr)] + special_name("apply"), + [module_attribute("operator", op_name), self.dispatch(node.expr)] ) def _generateValue(self, value): @@ -614,8 +620,8 @@ return compiler.ast.Stmt([ self.dispatch(n, compiler.ast.CallFunc( - module_attribute("operator", "getitem"), - [expr, i] + special_name("apply"), + [module_attribute("operator", "getitem"), expr, i] )) for (i, n) in enumerate(node.nodes) ]) @@ -731,8 +737,9 @@ else: nodes.append( compiler.ast.CallFunc( - module_attribute("operator", operator_functions.get(op_name)), - [self.dispatch(left), self.dispatch(right)] + special_name("apply"), + [module_attribute("operator", operator_functions.get(op_name)), + self.dispatch(left), self.dispatch(right)] ) ) left = right @@ -831,8 +838,8 @@ compiler.ast.CallFunc(special_name("storetemp"), [ temp, compiler.ast.CallFunc( - module_attribute("__builtins__", "list"), - [] + special_name("apply"), + [module_attribute("__builtins__", "list")] ) ]), # ... @@ -881,8 +888,8 @@ compiler.ast.CallFunc(special_name("storetemp"), [ temp, compiler.ast.CallFunc( - module_attribute("__builtins__", "iter"), - [self.dispatch(node.list)] + special_name("apply"), + [module_attribute("__builtins__", "iter"), self.dispatch(node.list)] ) ]), # while True: ... @@ -1010,9 +1017,12 @@ def visitSlice(self, node, expr=None): return compiler.ast.CallFunc( - module_attribute("operator", expr and "setslice" or "getslice"), - [self.dispatch(node.expr), node.lower and self.dispatch(node.lower), node.upper and self.dispatch(node.upper)] - + (expr and [expr] or []) + special_name("apply"), + [module_attribute("operator", expr and "setslice" or "getslice"), + self.dispatch(node.expr), + node.lower and self.dispatch(node.lower), + node.upper and self.dispatch(node.upper)] + + (expr and [expr] or []) ) def visitSliceobj(self, node): @@ -1023,9 +1033,11 @@ def visitSubscript(self, node, expr=None): return compiler.ast.CallFunc( - module_attribute("operator", expr and "setitem" or "getitem"), - [self.dispatch(node.expr), compiler.ast.Tuple([self.dispatch(sub) for sub in node.subs])] - + (expr and [expr] or []) + special_name("apply"), + [module_attribute("operator", expr and "setitem" or "getitem"), + self.dispatch(node.expr), + compiler.ast.Tuple([self.dispatch(sub) for sub in node.subs])] + + (expr and [expr] or []) ) def visitTuple(self, node, expr=None):