# HG changeset patch # User paulb@jeremy # Date 1153861391 -7200 # Node ID f064d7c3a32bc996e4f3bc526b0f54fec73e32ea # Parent b9f526fa9a0fa1279d3548a01da0bb6cd33f5293 Introduced None as a name for anonymous subprograms. Made Class, Constant and Instance subclasses of the new non-program Structure node class. diff -r b9f526fa9a0f -r f064d7c3a32b simplified.py --- a/simplified.py Mon Jul 24 22:34:30 2006 +0200 +++ b/simplified.py Tue Jul 25 23:03:11 2006 +0200 @@ -92,7 +92,7 @@ elif hasattr(self, "value"): return "%s %s (at %x)" % (self.__class__, repr(self.value), id(self)) elif hasattr(self, "ref"): - return "%s '%s' (at %x)" % (self.__class__, self.ref.name, id(self)) + return "%s '%x' (at %x)" % (self.__class__, id(self.ref), id(self)) else: return "%s (at %x)" % (self.__class__, id(self)) @@ -147,7 +147,6 @@ class Module(Node): "A Python module." class Subprogram(Node): "A subprogram: functions, methods and loops." -class Constant(Node): "A constant." class Pass(Node): "A placeholder node corresponding to pass." class Invoke(Node): "A function, method or loop invocation." class Return(Node): "Return an evaluated expression." @@ -170,6 +169,25 @@ class Try(Node): "A try...except...else...finally grouping node." class Raise(Node): "An exception raising node." class Not(Node): "A negation of an expression." -class Class(Node): "A Python class." + +# Special non-program nodes. + +class Structure: + + "A non-program node containing some kind of namespace." + + def __init__(self, **kw): + for name, value in kw.items(): + setattr(self, name, value) + + def __repr__(self): + if hasattr(self, "name"): + return "%s '%s' (at %x)" % (self.__class__, self.name, id(self)) + else: + return "%s (at %x)" % (self.__class__, id(self)) + +class Class(Structure): "A Python class." +class Instance(Structure): "An instance." +class Constant(Instance): "A constant." # vim: tabstop=4 expandtab shiftwidth=4 diff -r b9f526fa9a0f -r f064d7c3a32b simplify.py --- a/simplify.py Mon Jul 24 22:34:30 2006 +0200 +++ b/simplify.py Tue Jul 25 23:03:11 2006 +0200 @@ -183,7 +183,7 @@ # Make a subprogram for the statement and record it outside the main tree. - subprogram = Subprogram(name=hex(id(if_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) + subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) # In the subprogram, make conditionals for each test plus statement, @@ -197,7 +197,7 @@ expr=LoadAttr(expr=self.dispatch(compare), name="__true__"), args=[], star=None, dstar=None)) - body_subprogram = Subprogram(name=hex(id(stmt)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) + body_subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) self.current_subprograms.append(body_subprogram) body_subprogram.code = self.dispatch(stmt) + [Return()] @@ -211,7 +211,7 @@ # Add the compound statement from any else clause to the end. if if_.else_ is not None: - else_subprogram = Subprogram(name=hex(id(if_.else_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) + else_subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) self.current_subprograms.append(else_subprogram) else_subprogram.code = self.dispatch(if_.else_) + [Return()] @@ -236,7 +236,7 @@ # Make a subprogram for the statement and record it outside the main tree. - subprogram = Subprogram(name=hex(id(tryexcept)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) + subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) # In the subprogram, make conditionals for each test plus statement, @@ -293,7 +293,7 @@ # Make a subprogram for the expression and record it outside the main tree. - subprogram = Subprogram(name=hex(id(compare)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) + subprogram = Subprogram(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 @@ -337,7 +337,7 @@ # Make a subprogram for the expression and record it outside the main tree. - subprogram = Subprogram(name=hex(id(and_)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) + subprogram = Subprogram(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 @@ -370,7 +370,7 @@ # Make a subprogram for the expression and record it outside the main tree. - subprogram = Subprogram(name=hex(id(or_)), acquire_locals=1, returns_value=1, params=[], star=None, dstar=None) + subprogram = Subprogram(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 @@ -592,14 +592,16 @@ # Invocation and subprogram transformations. def visitClass(self, class_): - structure = Class(name=hex(id(class_)), bases=class_.bases) + structure = Class(name=class_.name, bases=self.dispatches(class_.bases)) self.structures.append(structure) # Make a subprogram which initialises the class structure. - subprogram = Subprogram(name=hex(id(class_)), structure=structure, params=[], star=None, dstar=None) + subprogram = Subprogram(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) self.current_subprograms.pop() @@ -655,7 +657,7 @@ # Make a subprogram for the function and record it outside the main # tree. - subprogram = Subprogram(name=hex(id(function)), returns_value=1, star=None, dstar=None) + subprogram = Subprogram(name=function.name, returns_value=1, star=None, dstar=None) self.current_subprograms.append(subprogram) subprogram.code = self.dispatch(function.code) self.current_subprograms.pop() @@ -671,7 +673,7 @@ # Make a subprogram for the function and record it outside the main # tree. - subprogram = Subprogram(name=hex(id(lambda_)), returns_value=1, star=None, dstar=None) + subprogram = Subprogram(name=None, returns_value=1, star=None, dstar=None) self.current_subprograms.append(subprogram) subprogram.code = [Return(expr=self.dispatch(lambda_.code))] self.current_subprograms.pop() @@ -695,7 +697,7 @@ # Make a subprogram for the block and record it outside the main tree. - subprogram = Subprogram(name=hex(id(while_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) + subprogram = Subprogram(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. @@ -727,7 +729,7 @@ # Make a subprogram for the block and record it outside the main tree. - subprogram = Subprogram(name=hex(id(for_)), acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) + subprogram = Subprogram(name=None, acquire_locals=1, returns_value=0, params=[], star=None, dstar=None) self.current_subprograms.append(subprogram) if for_.else_ is not None: