[Python-checkins] python/nondist/sandbox/ast asdl_c.py,1.13,1.14

jhylton@sourceforge.net jhylton@sourceforge.net
Fri, 19 Apr 2002 15:12:45 -0700


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

Modified Files:
	asdl_c.py 
Log Message:
Create "constructor" functions for product types like alias.

Also, don't generate NULL tests for * types, since 0 is valid for *
and it would be a pain to pass an empty asdl_seq *.


Index: asdl_c.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/asdl_c.py,v
retrieving revision 1.13
retrieving revision 1.14
diff -C2 -d -r1.13 -r1.14
*** asdl_c.py	19 Apr 2002 12:56:08 -0000	1.13
--- asdl_c.py	19 Apr 2002 22:12:43 -0000	1.14
***************
*** 207,232 ****
                  self.visit(t, name)
  
!     def visitConstructor(self, cons, type):
          args = []
!         if cons.fields:
!             unnamed = {}
!             for f in cons.fields:
!                 if f.name is None:
!                     name = f.type
!                     c = unnamed[name] = unnamed.get(name, 0) + 1
!                     if c > 1:
!                         name = "name%d" % (c - 1)
!                 else:
!                     name = f.name
!                 # XXX should extend get_c_type() to handle this
!                 if f.seq:
!                     ctype = "asdl_seq *"
!                 else:
!                     ctype = get_c_type(f.type)
!                 args.append((ctype, name, f.opt))
          ctype = get_c_type(type)
          self.emit_function(cons.name, ctype, args)
  
!     def emit_function(self, name, ctype, args):
          if args:
              argstr = ", ".join(["%s %s" % (atype, aname)
--- 207,235 ----
                  self.visit(t, name)
  
!     def get_args(self, fields):
          args = []
!         unnamed = {}
!         for f in fields:
!             if f.name is None:
!                 name = f.type
!                 c = unnamed[name] = unnamed.get(name, 0) + 1
!                 if c > 1:
!                     name = "name%d" % (c - 1)
!             else:
!                 name = f.name
!             # XXX should extend get_c_type() to handle this
!             if f.seq:
!                 ctype = "asdl_seq *"
!             else:
!                 ctype = get_c_type(f.type)
!             args.append((ctype, name, f.opt or f.seq))
!         return args
! 
!     def visitConstructor(self, cons, type):
!         args = self.get_args(cons.fields)
          ctype = get_c_type(type)
          self.emit_function(cons.name, ctype, args)
  
!     def emit_function(self, name, ctype, args, union=1):
          if args:
              argstr = ", ".join(["%s %s" % (atype, aname)
***************
*** 236,247 ****
          self.emit("%s %s(%s);" % (ctype, name, argstr), 0)
  
-     # XXX or just set skip=1 in constructor?
      def visitProduct(self, prod, name):
!         pass
  
  class FunctionVisitor(PrototypeVisitor):
      """Visitor to generate constructor functions for AST."""
  
!     def emit_function(self, name, ctype, args):
          def emit(s, depth=0, reflow=1):
              self.emit(s, depth, reflow)
--- 239,250 ----
          self.emit("%s %s(%s);" % (ctype, name, argstr), 0)
  
      def visitProduct(self, prod, name):
!         self.emit_function(name, get_c_type(name),
!                            self.get_args(prod.fields), union=0)
  
  class FunctionVisitor(PrototypeVisitor):
      """Visitor to generate constructor functions for AST."""
  
!     def emit_function(self, name, ctype, args, union=1):
          def emit(s, depth=0, reflow=1):
              self.emit(s, depth, reflow)
***************
*** 268,278 ****
          emit("return NULL;", 2)
          emit("}", 1)
          emit("p->kind = %s_kind;" % name, 1)
          for argtype, argname, opt in args:
              emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
  
!         emit("return p;", 1)
!         emit("}")
!         emit("")
  
  class PickleVisitor(EmitVisitor):
--- 271,294 ----
          emit("return NULL;", 2)
          emit("}", 1)
+         if union:
+             self.emit_body_union(name, args)
+         else:
+             self.emit_body_struct(name, args)
+         emit("return p;", 1)
+         emit("}")
+         emit("")
+ 
+     def emit_body_union(self, name, args):
+         def emit(s, depth=0, reflow=1):
+             self.emit(s, depth, reflow)
          emit("p->kind = %s_kind;" % name, 1)
          for argtype, argname, opt in args:
              emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
  
!     def emit_body_struct(self, name, args):
!         def emit(s, depth=0, reflow=1):
!             self.emit(s, depth, reflow)
!         for argtype, argname, opt in args:
!             emit("p->%s = %s;" % (argname, argname), 1)
  
  class PickleVisitor(EmitVisitor):