[Python-checkins] python/nondist/sandbox/ast asdl_c.py,1.9,1.10

jhylton@sourceforge.net jhylton@sourceforge.net
Mon, 15 Apr 2002 20:59:12 -0700


Update of /cvsroot/python/python/nondist/sandbox/ast
In directory usw-pr-cvs1:/tmp/cvs-serv23805

Modified Files:
	asdl_c.py 
Log Message:
Factor out the simple test into a method is_simple().

Add some preliminary code (not hooked up) to generate C code to write
pickles.


Index: asdl_c.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/asdl_c.py,v
retrieving revision 1.9
retrieving revision 1.10
diff -C2 -d -r1.9 -r1.10
*** asdl_c.py	16 Apr 2002 03:22:54 -0000	1.9
--- asdl_c.py	16 Apr 2002 03:59:10 -0000	1.10
***************
*** 81,84 ****
--- 81,97 ----
              self.file.write(line)
  
+     def is_simple(self, sum):
+         """Return true if a sum is a simple.
+ 
+         A sum is simple if its types have no fields, e.g.
+         unaryop = Invert | Not | UAdd | USub
+         """
+         simple = 1
+         for t in sum.types:
+             if t.fields:
+                 simple = 0
+                 break
+         return simple
+ 
  class TypeDefVisitor(EmitVisitor):
      def visitModule(self, mod):
***************
*** 90,99 ****
  
      def visitSum(self, sum, name, depth):
!         simple = 1
!         for t in sum.types:
!             if t.fields:
!                 simple = 0
!                 break
!         if simple:
              self.simple_sum(sum, name, depth)
          else:
--- 103,107 ----
  
      def visitSum(self, sum, name, depth):
!         if self.is_simple(sum):
              self.simple_sum(sum, name, depth)
          else:
***************
*** 134,143 ****
  
      def visitSum(self, sum, name, depth):
!         simple = 1
!         for t in sum.types:
!             if t.fields:
!                 simple = 0
!                 break
!         if not simple:
              self.sum_with_constructors(sum, name, depth)
  
--- 142,146 ----
  
      def visitSum(self, sum, name, depth):
!         if not self.is_simple(sum):
              self.sum_with_constructors(sum, name, depth)
  
***************
*** 198,208 ****
  
      def visitSum(self, sum, name):
!         simple = 1
!         for t in sum.types:
!             if t.fields:
!                 simple = 0
!                 break
! 
!         if simple:
              pass # XXX
          else:
--- 201,205 ----
  
      def visitSum(self, sum, name):
!         if self.is_simple(sum):
              pass # XXX
          else:
***************
*** 273,277 ****
          emit("}")
          emit("")
!         
  class ChainOfVisitors:
      def __init__(self, *visitors):
--- 270,334 ----
          emit("}")
          emit("")
! 
! class PickleVisitor(EmitVisitor):
! 
!     def visitModule(self, mod):
!         for dfn in mod.dfns:
!             self.visit(dfn)
! 
!     def visitType(self, type):
!         self.visit(type.value, type.name)
! 
!     def visitSum(self, sum, name):
!         pass
! 
!     def visitProduct(self, sum, name):
!         pass
! 
!     def visitConstructor(self, cons, name):
!         pass
! 
!     def visitField(self, sum):
!         pass
! 
! class PicklePrototypeVisitor(PickleVisitor):
! 
!     def visitSum(self, sum, name):
!         ctype = get_c_type(name)
!         self.emit("int pkl_write_%s(PyObject *write, %s o);" % (name, ctype),
!                   0)
! 
! class PickleFunctionVisitor(PickleVisitor):
!     
!     def visitSum(self, sum, name):
!         ctype = get_c_type(name)
!         self.emit("int", 0)
!         self.emit("pkl_write_%s(PyObject *write, %s o)" % (name, ctype), 0)
!         self.emit("{", 0)
!         self.emit("switch (o->kind) {", 1)
!         simple = self.is_simple(sum)
!         for i in range(len(sum.types)):
!             t = sum.types[i]
!             self.visit(t, i + 1, name, simple)
!         self.emit("}", 1)
!         self.emit("return 0;", 1)
!         self.emit("}", 0)
!         self.emit("", 0)
!             
!     def visitConstructor(self, cons, enum, name, simple):
!         if simple:
!             pass
!         else:
!             self.emit("case %s_kind:" % cons.name, 1)
!             self.emit("pkl_write_int(write, %d);" % enum, 2)
!             for f in cons.fields:
!                 self.visit(f, cons.name)
!             self.emit("break;", 2)
! 
!     def visitField(self, field, name):
!         # handle seq and opt
!         self.emit("pkl_write_%s(write, o->v.%s.%s);" % (
!             field.type, name, field.name), 2)
! 
  class ChainOfVisitors:
      def __init__(self, *visitors):
***************
*** 292,296 ****
      c = ChainOfVisitors(TypeDefVisitor(f),
                          StructVisitor(f),
!                         PrototypeVisitor(f)
                          )
      c.visit(mod)
--- 349,354 ----
      c = ChainOfVisitors(TypeDefVisitor(f),
                          StructVisitor(f),
!                         PrototypeVisitor(f),
! ##                        PicklePrototypeVisitor(f),
                          )
      c.visit(mod)
***************
*** 300,304 ****
      print >> f, '#include "Python.h"'
      print >> f, '#include "%s-ast.h"' % mod.name
!     v = FunctionVisitor(f)
      v.visit(mod)
      f.close()
--- 358,364 ----
      print >> f, '#include "Python.h"'
      print >> f, '#include "%s-ast.h"' % mod.name
!     v = ChainOfVisitors(FunctionVisitor(f),
! ##                        PickleFunctionVisitor(f),
!                         )
      v.visit(mod)
      f.close()