[pypy-svn] r20832 - pypy/dist/pypy/interpreter/astcompiler

adim at codespeak.net adim at codespeak.net
Wed Dec 7 16:05:52 CET 2005


Author: adim
Date: Wed Dec  7 16:05:51 2005
New Revision: 20832

Modified:
   pypy/dist/pypy/interpreter/astcompiler/astgen.py
Log:
don't use load_boilerplate() anymore to find prologue and epilogue



Modified: pypy/dist/pypy/interpreter/astcompiler/astgen.py
==============================================================================
--- pypy/dist/pypy/interpreter/astcompiler/astgen.py	(original)
+++ pypy/dist/pypy/interpreter/astcompiler/astgen.py	Wed Dec  7 16:05:51 2005
@@ -18,16 +18,6 @@
 SPEC = "ast.txt"
 COMMA = ", "
 
-def load_boilerplate(file):
-    f = open(file)
-    buf = f.read()
-    f.close()
-    i = buf.find('### ''PROLOGUE')
-    j = buf.find('### ''EPILOGUE')
-    pro = buf[i+12:j].strip()
-    epi = buf[j+12:].strip()
-    return pro, epi
-
 def strip_default(arg):
     """Return the argname from an 'arg = default' string"""
     i = arg.find('=')
@@ -115,6 +105,8 @@
         self._gen_repr(buf)
         print >> buf
         self._gen_visit(buf)
+        print >> buf
+        self._gen_descr_visit(buf)
         buf.seek(0, 0)
         return buf.read()
 
@@ -226,6 +218,12 @@
         print >> buf, "    def accept(self, visitor):"
         print >> buf, "        return visitor.visit%s(self)" % self.name
 
+    def _gen_descr_visit(self, buf):
+        print >> buf, "    def descr_accept(self, space, w_visitor):"
+        print >> buf, "        w_callable = space.getattr(w_visitor, space.wrap('visit%s'))" % self.name
+        print >> buf, "        args = Arguments(space, [ self ])"
+        print >> buf, "        return space.call_args(w_callable, args)"
+
     def _gen_additional_methods(self, buf):
         for key, value in self.additional_methods.iteritems():
             if key not in '_cur_':
@@ -333,7 +331,6 @@
     print buf.getvalue()
 
 def main():
-    prologue, epilogue = load_boilerplate(sys.argv[-1])
     print prologue
     print
     classes = parse_spec(SPEC)
@@ -348,18 +345,17 @@
         emit(info)
     gen_ast_visitor(classes)
     print epilogue
-
-if __name__ == "__main__":
-    main()
-    sys.exit(0)
-
-### PROLOGUE
+    
+prologue = '''
 """Python abstract syntax node definitions
 
 This file is automatically generated by Tools/compiler/astgen.py
 """
 from consts import CO_VARARGS, CO_VARKEYWORDS, OP_ASSIGN
 from pypy.interpreter.baseobjspace import Wrappable
+from pypy.interpreter.typedef import TypeDef
+from pypy.interpreter.gateway import interp2app
+from pypy.interpreter.argument import Arguments
 
 def flatten(list):
     l = []
@@ -405,6 +401,28 @@
             res.append( self )
         return res
 
+    def __repr__(self):
+	return "Node()"
+
+    def descr_repr( self, space ):
+	return space.wrap( self.__repr__() )
+
+def descr_node_repr( space, w_obj ):
+    return w_obj.descr_repr( space )
+    
+def descr_getChildNodes( space, w_obj ):
+    lst = w_obj.getChildNodes()
+    return space.newlist( lst )
+
+def descr_accept( space, w_obj, w_visitor ):
+    return w_obj.descr_accept( space, w_visitor )
+
+Node.typedef = TypeDef('ASTNode',
+		       __repr__ = interp2app(descr_node_repr),
+		       getChildNodes = interp2app(descr_getChildNodes),
+		       accept = interp2app(descr_accept),
+		       )
+
         
 class EmptyNode(Node):
     def accept(self, visitor):
@@ -429,7 +447,15 @@
     def accept(self, visitor):
         return visitor.visitExpression(self)
 
-### EPILOGUE
+'''
+
+epilogue = '''
 for name, obj in globals().items():
     if isinstance(obj, type) and issubclass(obj, Node):
         nodes[name.lower()] = obj
+'''
+
+if __name__ == "__main__":
+    main()
+    sys.exit(0)
+



More information about the Pypy-commit mailing list