# HG changeset patch # User paulb@jeremy # Date 1160173279 -7200 # Node ID 91656d3aa52bb58d6150d7b6d55decd73ea1ee5e # Parent 50cfa7dd7f31538cca11298d60eb01a9182bf54f Introduced original node references into all simplified nodes. diff -r 50cfa7dd7f31 -r 91656d3aa52b simplify.py --- a/simplify.py Wed Oct 04 01:05:41 2006 +0200 +++ b/simplify.py Sat Oct 07 00:21:19 2006 +0200 @@ -117,7 +117,13 @@ init_code = [] for value, constant in self.constants.items(): - init_code.append(StoreAttr(lvalue=LoadRef(ref=constant), name="__class__", expr=LoadName(name=constant.typename))) + init_code.append( + StoreAttr(module, + lvalue=LoadRef(module, ref=constant), + name="__class__", + expr=LoadName(module, name=constant.typename) + ) + ) # NOTE: Hack to ensure correct initialisation of constants. @@ -151,18 +157,25 @@ result = Assign(import_) code = [] for path, alias in import_.names: - importer = Import(name=path) + importer = Import(import_, name=path) top = alias or path.split(".")[0] - code.append(StoreName(expr=importer, name=top)) + code.append(StoreName(import_, expr=importer, name=top)) result.code = code return result def visitFrom(self, from_): result = Assign(from_) code = [] - code.append(StoreTemp(expr=Import(name=from_.modname))) + code.append(StoreTemp(from_, expr=Import(from_, name=from_.modname))) for name, alias in from_.names: - code.append(StoreName(expr=LoadAttr(expr=LoadTemp(), name=name), name=(alias or name))) + code.append( + StoreName(from_, + expr=LoadAttr(from_, + expr=LoadTemp(from_), + name=name), + name=(alias or name) + ) + ) result.code = code return result @@ -172,8 +185,8 @@ def visitConst(self, const): if not self.constants.has_key(const.value): - self.constants[const.value] = Constant(name=repr(const.value), value=const.value) - result = LoadRef(ref=self.constants[const.value]) + self.constants[const.value] = Constant(const, name=repr(const.value), value=const.value) + result = LoadRef(const, ref=self.constants[const.value]) return result def visitReturn(self, return_): @@ -188,7 +201,7 @@ def visitContinue(self, continue_): result = InvokeBlock(continue_, - expr=LoadRef(ref=self.current_subprograms[-1]) + expr=LoadRef(continue_, ref=self.current_subprograms[-1]) ) return result @@ -201,7 +214,7 @@ return result def _visitBuiltin(self, builtin, name): - result = Invoke(builtin, expr=LoadName(name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) + result = Invoke(builtin, expr=LoadName(builtin, name=name), args=self.dispatches(builtin.nodes), star=None, dstar=None) return result def visitTuple(self, tuple): @@ -211,10 +224,10 @@ return self._visitBuiltin(list, "list") def visitDict(self, dict): - result = Invoke(dict, expr=LoadName(name="dict"), star=None, dstar=None) + result = Invoke(dict, expr=LoadName(dict, name="dict"), star=None, dstar=None) args = [] for key, value in dict.items: - tuple = Invoke(expr=LoadName(name="tuple"), star=None, dstar=None) + tuple = Invoke(dict, expr=LoadName(dict, name="tuple"), star=None, dstar=None) tuple.args = [self.dispatch(key), self.dispatch(value)] args.append(tuple) result.args = args @@ -246,10 +259,20 @@ results = nodes = [] + # Produce something like... + # expr.__true__() ? body + for compare, stmt in if_.tests: - # Produce something like... - # expr.__true__() ? body - test = Conditional(test=Invoke(expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), args=[], star=None, dstar=None)) + test = Conditional(if_, + test=Invoke(if_, + expr=LoadAttr(if_, + expr=self.dispatch(compare), + name="__true__" + ), + args=[], + star=None, + dstar=None) + ) test.body = self.dispatch(stmt) nodes.append(test) nodes = test.else_ = [] @@ -305,21 +328,33 @@ else: new_spec = self.dispatch(spec) - test = Conditional(test=Invoke(expr=LoadName(name="isinstance"), args=[LoadExc(), new_spec], star=None, dstar=None)) + test = Conditional(tryexcept, + test=Invoke(tryexcept, + expr=LoadName(tryexcept, name="isinstance"), + args=[LoadExc(tryexcept), new_spec], + star=None, + dstar=None) + ) test.body = [] if assign is not None: - test.body.append(Assign(code=[StoreTemp(expr=LoadExc()), self.dispatch(assign), ReleaseTemp()])) + test.body.append( + Assign(tryexcept, + code=[StoreTemp(expr=LoadExc(tryexcept)), self.dispatch(assign), ReleaseTemp(tryexcept)]) + ) # Always return from conditional sections. - test.body += self.dispatch(stmt) + [Return()] + test.body += self.dispatch(stmt) + [Return(tryexcept)] nodes.append(test) nodes = test.else_ = [] # Add a raise operation to deal with unhandled exceptions. - nodes.append(Raise(expr=LoadExc())) + nodes.append( + Raise(tryexcept, + expr=LoadExc(tryexcept)) + ) result.handler = results return result @@ -346,7 +381,7 @@ (else) -> ... """ - subprogram = Subprogram(name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) + subprogram = Subprogram(compare, name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) # In the subprogram, make instructions which invoke a method on the @@ -365,29 +400,29 @@ method_name = self.comparison_methods[op_name] if method_name: - invocation = Invoke(expr=LoadAttr(expr=previous, name=method_name), args=[expr], star=None, dstar=None) + invocation = Invoke(compare, expr=LoadAttr(compare, expr=previous, name=method_name), args=[expr], star=None, dstar=None) elif op_name == "is": - invocation = Invoke(expr=LoadName(name="__is__"), args=[previous, expr], star=None, dstar=None) + invocation = Invoke(compare, expr=LoadName(compare, name="__is__"), args=[previous, expr], star=None, dstar=None) elif op_name == "is not": - invocation = Not(expr=Invoke(expr=LoadName(name="__is__"), args=[previous, expr], star=None, dstar=None)) + invocation = Not(compare, expr=Invoke(compare, expr=LoadName(compare, name="__is__"), args=[previous, expr], star=None, dstar=None)) else: raise NotImplementedError, op_name - nodes.append(StoreTemp(expr=invocation)) + nodes.append(StoreTemp(compare, expr=invocation)) # Return from the subprogram where the test is not satisfied. if op is not last: - test = Conditional(test=Not(expr=LoadTemp()), body=[Return(expr=LoadTemp())]) + test = Conditional(compare, test=Not(compare, expr=LoadTemp(compare)), body=[Return(compare, expr=LoadTemp(compare))]) nodes.append(test) # Put subsequent operations in the else section of this conditional. - nodes = test.else_ = [ReleaseTemp()] + nodes = test.else_ = [ReleaseTemp(compare)] # For the last operation, return the result. else: - nodes.append(Return(expr=LoadTemp())) + nodes.append(Return(compare, expr=LoadTemp(compare))) previous = expr @@ -401,7 +436,7 @@ # Make an invocation of the subprogram. result = InvokeBlock(compare, produces_result=1) - result.expr = LoadRef(ref=subprogram) + result.expr = LoadRef(compare, ref=subprogram) return result def visitAnd(self, and_): @@ -421,7 +456,7 @@ (else) -> ... """ - subprogram = Subprogram(name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) + subprogram = Subprogram(and_, name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) # In the subprogram, make instructions which store each operand, test @@ -437,19 +472,19 @@ # Return from the subprogram where the test is not satisfied. if node is not last: - nodes.append(StoreTemp(expr=expr)) - invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None) - test = Conditional(test=Not(expr=invocation), body=[Return(expr=LoadTemp())]) + nodes.append(StoreTemp(and_, expr=expr)) + invocation = Invoke(and_, expr=LoadAttr(and_, expr=LoadTemp(and_), name="__true__"), args=[], star=None, dstar=None) + test = Conditional(and_, test=Not(and_, expr=invocation), body=[Return(and_, expr=LoadTemp(and_))]) nodes.append(test) # Put subsequent operations in the else section of this conditional. - nodes = test.else_ = [ReleaseTemp()] + nodes = test.else_ = [ReleaseTemp(and_)] # For the last operation, return the result. else: - nodes.append(Return(expr=expr)) + nodes.append(Return(and_, expr=expr)) # Finish the subprogram definition. @@ -461,7 +496,7 @@ # Make an invocation of the subprogram. result = InvokeBlock(and_, produces_result=1) - result.expr = LoadRef(ref=subprogram) + result.expr = LoadRef(and_, ref=subprogram) return result def visitOr(self, or_): @@ -481,7 +516,7 @@ (else) -> ... """ - subprogram = Subprogram(name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) + subprogram = Subprogram(or_, name=None, acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) # In the subprogram, make instructions which store each operand, test @@ -497,19 +532,21 @@ # Return from the subprogram where the test is satisfied. if node is not last: - nodes.append(StoreTemp(expr=expr)) - invocation = Invoke(expr=LoadAttr(expr=LoadTemp(), name="__true__"), args=[], star=None, dstar=None) - test = Conditional(test=invocation, body=[Return(expr=LoadTemp())]) + nodes.append(StoreTemp(or_, expr=expr)) + invocation = Invoke(or_, expr=LoadAttr(or_, expr=LoadTemp(or_), name="__true__"), args=[], star=None, dstar=None) + test = Conditional(or_, test=invocation, body=[Return(or_, expr=LoadTemp(or_))]) nodes.append(test) # Put subsequent operations in the else section of this conditional. - nodes = test.else_ = [ReleaseTemp()] + nodes = test.else_ = [ReleaseTemp(or_)] # For the last operation, return the result. else: - nodes.append(Return(expr=expr)) + nodes.append( + Return(or_, expr=expr) + ) # Finish the subprogram definition. @@ -521,23 +558,23 @@ # Make an invocation of the subprogram. result = InvokeBlock(or_, produces_result=1) - result.expr = LoadRef(ref=subprogram) + result.expr = LoadRef(or_, ref=subprogram) return result def visitNot(self, not_): - result = Not(not_, expr=Invoke(expr=LoadAttr(expr=self.dispatch(not_.expr), name="__true__"), args=[], star=None, dstar=None)) + result = Not(not_, expr=Invoke(not_, expr=LoadAttr(not_, expr=self.dispatch(not_.expr), name="__true__"), args=[], star=None, dstar=None)) return result # Operators. def visitUnaryAdd(self, unaryadd): - return Invoke(unaryadd, expr=LoadAttr(expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[], star=None, dstar=None) + return Invoke(unaryadd, expr=LoadAttr(unaryadd, expr=self.dispatch(unaryadd.expr), name="__pos__"), args=[], star=None, dstar=None) def visitUnarySub(self, unarysub): - return Invoke(unarysub, expr=LoadAttr(expr=self.dispatch(unarysub.expr), name="__neg__"), args=[], star=None, dstar=None) + return Invoke(unarysub, expr=LoadAttr(unarysub, expr=self.dispatch(unarysub.expr), name="__neg__"), args=[], star=None, dstar=None) def visitInvert(self, invert): - return Invoke(invert, expr=LoadAttr(expr=self.dispatch(invert.expr), name="__invert__"), args=[], star=None, dstar=None) + return Invoke(invert, expr=LoadAttr(invert, expr=self.dispatch(invert.expr), name="__invert__"), args=[], star=None, dstar=None) def visitAdd(self, add): @@ -553,15 +590,27 @@ result = Try(add, body=[ - Invoke(expr=LoadAttr(expr=self.dispatch(add.left), name="__add__"), args=[self.dispatch(add.right)], star=None, dstar=None) + Invoke(add, + expr=LoadAttr(add, expr=self.dispatch(add.left), name="__add__"), + args=[self.dispatch(add.right)], + star=None, + dstar=None) ], else_=[], finally_=[], handler=[ - Conditional( - test=Invoke(expr=LoadName(name="isinstance"), args=[LoadExc(), LoadName(name="TypeError")], star=None, dstar=None), + Conditional(add, + test=Invoke(add, + expr=LoadName(add, name="isinstance"), + args=[LoadExc(add), LoadName(add, name="TypeError")], + star=None, + dstar=None), body=[ - Invoke(expr=LoadAttr(expr=self.dispatch(add.right), name="__radd__"), args=[self.dispatch(add.left)], star=None, dstar=None) + Invoke(add, + expr=LoadAttr(add, expr=self.dispatch(add.right), name="__radd__"), + args=[self.dispatch(add.left)], + star=None, + dstar=None) ], else_=[] ) @@ -586,19 +635,21 @@ if isinstance(augassign.node, compiler.ast.Name): result.code = [ - StoreTemp( - expr=Invoke( - args=[expr], star=None, dstar=None, - expr=LoadAttr( + StoreTemp(augassign, + expr=Invoke(augassign, + args=[expr], + star=None, + dstar=None, + expr=LoadAttr(augassign, expr=self.dispatch(augassign.node), name=self.augassign_methods[augassign.op] ) ) ), - StoreName( - expr=LoadTemp(), + StoreName(augassign, + expr=LoadTemp(augassign), name=augassign.node.name), - ReleaseTemp() + ReleaseTemp(augassign) ] # Complicated augmented assignment: lvalue.attr += expr @@ -608,29 +659,29 @@ # -> setattr(, getattr(, "attr").__xxx__(expr)) result.code = [ - StoreTemp( + StoreTemp(augassign, index="expr", expr=self.dispatch(augassign.node.expr) ), - StoreTemp( - expr=Invoke( + StoreTemp(augassign, + expr=Invoke(augassign, args=[expr], star=None, dstar=None, - expr=LoadAttr( - expr=LoadAttr( - expr=LoadTemp(index="expr"), + expr=LoadAttr(augassign, + expr=LoadAttr(augassign, + expr=LoadTemp(augassign, index="expr"), name=augassign.node.attrname ), name=self.augassign_methods[augassign.op] ) ) ), - StoreAttr( - expr=LoadTemp(), - lvalue=LoadTemp(index="expr"), + StoreAttr(augassign, + expr=LoadTemp(augassign), + lvalue=LoadTemp(augassign, index="expr"), name=augassign.node.attrname ), - ReleaseTemp(index="expr"), - ReleaseTemp() + ReleaseTemp(augassign, index="expr"), + ReleaseTemp(augassign) ] # Complicated augassign using slices: lvalue[lower:upper] += expr @@ -640,32 +691,44 @@ # , , -> .__setslice__(, , .__getslice__(, ).__xxx__(expr)) result.code = [ - StoreTemp( + StoreTemp(augassign, index="expr", expr=self.dispatch(augassign.node.expr) ), - StoreTemp( + StoreTemp(augassign, index="lower", expr=self.dispatch_or_none(augassign.node.lower) ), - StoreTemp( + StoreTemp(augassign, index="upper", expr=self.dispatch_or_none(augassign.node.upper) ), - StoreTemp( - expr=Invoke( + StoreTemp(augassign, + expr=Invoke(augassign, args=[expr], star=None, dstar=None, - expr=LoadAttr( - expr=self._visitSlice(augassign.node, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_APPLY"), + expr=LoadAttr(augassign, + expr=self._visitSlice( + augassign.node, + LoadTemp(augassign, index="expr"), + LoadTemp(augassign, index="lower"), + LoadTemp(augassign, index="upper"), + "OP_APPLY"), name=self.augassign_methods[augassign.op] ) ) ), - self._visitSlice(augassign.node, LoadTemp(index="expr"), LoadTemp(index="lower"), LoadTemp(index="upper"), "OP_ASSIGN", LoadTemp()), - ReleaseTemp(index="expr"), - ReleaseTemp(index="lower"), - ReleaseTemp(index="upper"), - ReleaseTemp() + self._visitSlice( + augassign.node, + LoadTemp(augassign, index="expr"), + LoadTemp(augassign, index="lower"), + LoadTemp(augassign, index="upper"), + "OP_ASSIGN", + LoadTemp(augassign) + ), + ReleaseTemp(augassign, index="expr"), + ReleaseTemp(augassign, index="lower"), + ReleaseTemp(augassign, index="upper"), + ReleaseTemp(augassign) ] # Complicated augassign using subscripts: lvalue[subs] += expr @@ -675,21 +738,32 @@ # , -> .__setitem__(, .__getitem__().__xxx__(expr)) result.code = [ - StoreTemp(index="expr", expr=self.dispatch(augassign.node.expr)), - StoreTemp(index="subs", expr=self._visitSubscriptSubs(augassign.node.subs)), - StoreTemp( - expr=Invoke( + StoreTemp(augassign, index="expr", expr=self.dispatch(augassign.node.expr)), + StoreTemp(augassign, index="subs", expr=self._visitSubscriptSubs(augassign.node.subs)), + StoreTemp(augassign, + expr=Invoke(augassign, args=[expr], star=None, dstar=None, - expr=LoadAttr( - expr=self._visitSubscript(augassign.node, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_APPLY"), + expr=LoadAttr(augassign, + expr=self._visitSubscript( + augassign.node, + LoadTemp(augassign, index="expr"), + LoadTemp(augassign, index="subs"), + "OP_APPLY" + ), name=self.augassign_methods[augassign.op] ) ) ), - self._visitSubscript(augassign.node, LoadTemp(index="expr"), LoadTemp(index="subs"), "OP_ASSIGN", LoadTemp()), - ReleaseTemp(index="expr"), - ReleaseTemp(index="subs"), - ReleaseTemp() + self._visitSubscript( + augassign.node, + LoadTemp(augassign, index="expr"), + LoadTemp(augassign, index="subs"), + "OP_ASSIGN", + LoadTemp(augassign) + ), + ReleaseTemp(augassign, index="expr"), + ReleaseTemp(augassign, index="subs"), + ReleaseTemp(augassign) ] else: @@ -699,19 +773,19 @@ def visitAssign(self, assign): result = Assign(assign) - store = StoreTemp(expr=self.dispatch(assign.expr)) - release = ReleaseTemp() + store = StoreTemp(assign, expr=self.dispatch(assign.expr)) + release = ReleaseTemp(assign) result.code = [store] + self.dispatches(assign.nodes, 0) + [release] return result def visitAssList(self, asslist, in_sequence=0): if not in_sequence: - expr = LoadTemp() + expr = LoadTemp(asslist) else: - expr = Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) + expr = Invoke(asslist, expr=LoadAttr(asslist, expr=LoadTemp(asslist), name="next"), star=None, dstar=None, args=[]) result = Assign(asslist) - store = StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=expr), star=None, dstar=None, args=[])) - release = ReleaseTemp() + store = StoreTemp(asslist, expr=Invoke(asslist, expr=LoadAttr(asslist, name="__iter__", expr=expr), star=None, dstar=None, args=[])) + release = ReleaseTemp(asslist) result.code = [store] + self.dispatches(asslist.nodes, 1) + [release] return result @@ -719,9 +793,9 @@ def _visitAssNameOrAttr(self, node, in_sequence): if not in_sequence: - return LoadTemp() + return LoadTemp(node) else: - return Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), star=None, dstar=None, args=[]) + return Invoke(node, expr=LoadAttr(node, expr=LoadTemp(node), name="next"), star=None, dstar=None, args=[]) def visitAssName(self, assname, in_sequence=0): expr = self._visitAssNameOrAttr(assname, in_sequence) @@ -737,13 +811,13 @@ def _visitSlice(self, slice, expr, lower, upper, flags, value=None): if flags == "OP_ASSIGN": args = [value] - result = Invoke(expr=LoadAttr(expr=expr, name="__setslice__"), star=None, dstar=None, args=[]) + result = Invoke(slice, expr=LoadAttr(slice, expr=expr, name="__setslice__"), star=None, dstar=None, args=[]) elif flags == "OP_APPLY": args = [] - result = Invoke(expr=LoadAttr(expr=expr, name="__getslice__"), star=None, dstar=None, args=[]) + result = Invoke(slice, expr=LoadAttr(slice, expr=expr, name="__getslice__"), star=None, dstar=None, args=[]) elif flags == "OP_DELETE": args = [] - result = Invoke(expr=LoadAttr(expr=expr, name="__delslice__"), star=None, dstar=None, args=[]) + result = Invoke(slice, expr=LoadAttr(slice, expr=expr, name="__delslice__"), star=None, dstar=None, args=[]) else: raise NotImplementedError, flags @@ -763,13 +837,13 @@ def _visitSubscript(self, subscript, expr, subs, flags, value=None): if flags == "OP_ASSIGN": args = [value] - result = Invoke(expr=LoadAttr(expr=expr, name="__setitem__"), star=None, dstar=None, args=[]) + result = Invoke(subscript, expr=LoadAttr(subscript, expr=expr, name="__setitem__"), star=None, dstar=None, args=[]) elif flags == "OP_APPLY": args = [] - result = Invoke(expr=LoadAttr(expr=expr, name="__getitem__"), star=None, dstar=None, args=[]) + result = Invoke(subscript, expr=LoadAttr(subscript, expr=expr, name="__getitem__"), star=None, dstar=None, args=[]) elif flags == "OP_DELETE": args = [] - result = Invoke(expr=LoadAttr(expr=expr, name="__delitem__"), star=None, dstar=None, args=[]) + result = Invoke(subscript, expr=LoadAttr(subscript, expr=expr, name="__delitem__"), star=None, dstar=None, args=[]) else: raise NotImplementedError, flags @@ -785,7 +859,7 @@ if len(subs) == 1: return self.dispatch(subs[0]) else: - return Invoke(expr=LoadName(name="tuple"), args=self.dispatches(subs), star=None, dstar=None) + return Invoke(subs, expr=LoadName(subs, name="tuple"), args=self.dispatches(subs), star=None, dstar=None) def visitSubscript(self, subscript, in_sequence=0): return self._visitSubscript( @@ -796,17 +870,17 @@ # Invocation and subprogram transformations. def visitClass(self, class_): - structure = Class(name=class_.name, bases=self.dispatches(class_.bases)) + structure = Class(class_, name=class_.name, bases=self.dispatches(class_.bases)) self.structures.append(structure) # Make a subprogram which initialises the class structure. - subprogram = Subprogram(name=None, structure=structure, params=[], star=None, dstar=None) + subprogram = Subprogram(class_, name=None, structure=structure, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) # The class is initialised using the code found inside. - subprogram.code = self.dispatch(class_.code) + [Return()] + subprogram.code = self.dispatch(class_.code) + [Return(class_)] self.current_subprograms.pop() self.subprograms.append(subprogram) @@ -815,13 +889,13 @@ result = Assign(class_, code=[ - StoreName( + StoreName(class_, name=class_.name, - expr=LoadRef(ref=structure) + expr=LoadRef(class_, ref=structure) ), - Invoke( + Invoke(class_, args=[], star=None, dstar=None, - expr=LoadRef(ref=subprogram) + expr=LoadRef(class_, ref=subprogram) ) ] ) @@ -853,11 +927,11 @@ # Produce star and dstar parameters with appropriate defaults. if has_star: - star = (function.argnames[npositional], Invoke(expr=LoadName(name="list"), args=[], star=None, dstar=None)) + star = (function.argnames[npositional], Invoke(function, expr=LoadName(function, name="list"), args=[], star=None, dstar=None)) else: star = None if has_dstar: - dstar = (function.argnames[npositional + has_star], Invoke(expr=LoadName(name="dict"), args=[], star=None, dstar=None)) + dstar = (function.argnames[npositional + has_star], Invoke(function, expr=LoadName(function, name="dict"), args=[], star=None, dstar=None)) else: dstar = None @@ -886,13 +960,13 @@ subprogram = Subprogram(function, name=function.name, acquire_locals=0, returns_value=1, star=None, dstar=None) self.current_subprograms.append(subprogram) - subprogram.code = self.dispatch(function.code) + [Return()] + subprogram.code = self.dispatch(function.code) + [Return(function)] self.current_subprograms.pop() self._visitFunction(function, subprogram) # Make a definition of the function associating it with a name. - result = StoreName(name=function.name, expr=LoadRef(ref=subprogram)) + result = StoreName(function, name=function.name, expr=LoadRef(function, ref=subprogram)) return result def visitLambda(self, lambda_): @@ -900,9 +974,9 @@ # Make a subprogram for the function and record it outside the main # tree. - subprogram = Subprogram(name=None, acquire_locals=0, returns_value=1, star=None, dstar=None) + subprogram = Subprogram(lambda_, name=None, acquire_locals=0, returns_value=1, star=None, dstar=None) self.current_subprograms.append(subprogram) - subprogram.code = [Return(expr=self.dispatch(lambda_.code))] + subprogram.code = [Return(lambda_, expr=self.dispatch(lambda_.code))] self.current_subprograms.pop() self._visitFunction(lambda_, subprogram) @@ -936,28 +1010,28 @@ (else) -> ... """ - subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) + subprogram = Subprogram(while_, name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) # Include a conditional statement in the subprogram. - test = Conditional(else_=[]) - test.test = Invoke(expr=LoadAttr(expr=self.dispatch(while_.test), name="__true__"), args=[], star=None, dstar=None) + test = Conditional(while_, else_=[]) + test.test = Invoke(while_, expr=LoadAttr(while_, expr=self.dispatch(while_.test), name="__true__"), args=[], star=None, dstar=None) # Inside the conditional, add a recursive invocation to the subprogram # if the test condition was satisfied. - continuation = InvokeBlock() - continuation.expr = LoadRef(ref=subprogram) + continuation = InvokeBlock(while_) + continuation.expr = LoadRef(while_, ref=subprogram) # Return within the main section of the loop. - test.body = self.dispatch(while_.body) + [continuation, Return()] + test.body = self.dispatch(while_.body) + [continuation, Return(while_)] # Provide the else section, if present, along with an explicit return. if while_.else_ is not None: - test.else_ = self.dispatch(while_.else_) + [Return()] + test.else_ = self.dispatch(while_.else_) + [Return(while_)] # Finish the subprogram definition. @@ -969,7 +1043,7 @@ # Make an invocation of the subprogram. result = InvokeBlock(while_) - result.expr = LoadRef(ref=subprogram) + result.expr = LoadRef(while_, ref=subprogram) return result def visitFor(self, for_): @@ -992,39 +1066,39 @@ (else) -> ... """ - subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) + subprogram = Subprogram(for_, name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) # Always return from conditional sections/subprograms. if for_.else_ is not None: - else_stmt = self.dispatch(for_.else_) + [Return()] + else_stmt = self.dispatch(for_.else_) + [Return(for_)] else: - else_stmt = [Return()] + else_stmt = [Return(for_)] # Wrap the assignment in a try...except statement. - try_except = Try(body=[], else_=[], finally_=[]) - test = Conditional( - test=Invoke(expr=LoadName(name="isinstance"), args=[LoadExc(), LoadName(name="StopIteration")], star=None, dstar=None), + try_except = Try(for_, body=[], else_=[], finally_=[]) + test = Conditional(for_, + test=Invoke(for_, expr=LoadName(for_, name="isinstance"), args=[LoadExc(for_), LoadName(for_, name="StopIteration")], star=None, dstar=None), body=else_stmt, - else_=[Raise(expr=LoadExc())]) + else_=[Raise(for_, expr=LoadExc(for_))]) try_except.handler = [test] - assign = Assign() - assign.code = [ - StoreTemp(expr=Invoke(expr=LoadAttr(expr=LoadTemp(), name="next"), args=[], star=None, dstar=None)), - self.dispatch(for_.assign), - ReleaseTemp() - ] + assign = Assign(for_, + code=[ + StoreTemp(for_, expr=Invoke(for_, expr=LoadAttr(for_, expr=LoadTemp(for_), name="next"), args=[], star=None, dstar=None)), + self.dispatch(for_.assign), + ReleaseTemp(for_) + ]) # Inside the conditional, add a recursive invocation to the subprogram # if the test condition was satisfied. - continuation = InvokeBlock() - continuation.expr = LoadRef(ref=subprogram) + continuation = InvokeBlock(for_) + continuation.expr = LoadRef(for_, ref=subprogram) try_except.body = [assign] + self.dispatch(for_.body) + [continuation] - subprogram.code = [try_except, Return()] + subprogram.code = [try_except, Return(for_)] # Finish the subprogram definition. @@ -1036,9 +1110,9 @@ result = Assign(for_) result.code = [ - StoreTemp(expr=Invoke(expr=LoadAttr(name="__iter__", expr=self.dispatch(for_.list)), args=[], star=None, dstar=None)), - InvokeBlock(expr=LoadRef(ref=subprogram)), - ReleaseTemp() + StoreTemp(for_, expr=Invoke(for_, expr=LoadAttr(for_, name="__iter__", expr=self.dispatch(for_.list)), args=[], star=None, dstar=None)), + InvokeBlock(for_, expr=LoadRef(for_, ref=subprogram)), + ReleaseTemp(for_) ] return result