Mailing List Archive

python/nondist/sandbox/ast asdl_c.py,1.7,1.8
Update of /cvsroot/python/python/nondist/sandbox/ast
In directory usw-pr-cvs1:/tmp/cvs-serv27699

Modified Files:
asdl_c.py
Log Message:
Implement the PrototypeVisitor, which generates C function
prototypes. Implement it and FunctionVisitor using the same basic
infrastructure, where all the code is generated by emit_function().
FunctionVisitor extends PrototypeVisitor and overrides
emit_function().

Extend reflow_lines() to indent to first paren if there is no brace.
This change gets function prototypes formatted nicely.


Index: asdl_c.py
===================================================================
RCS file: /cvsroot/python/python/nondist/sandbox/ast/asdl_c.py,v
retrieving revision 1.7
retrieving revision 1.8
diff -C2 -d -r1.7 -r1.8
*** asdl_c.py 12 Apr 2002 02:35:15 -0000 1.7
--- asdl_c.py 12 Apr 2002 15:50:36 -0000 1.8
***************
*** 53,56 ****
--- 53,62 ----
size -= j
padding = " " * j
+ else:
+ j = cur.find('(', 0, i)
+ if j >= 0:
+ j += 1 # account for the paren (no space after it)
+ size -= j
+ padding = " " * j
cur = cur[i+1:]
else:
***************
*** 178,184 ****
"""Generate function prototypes for the .h file"""

- class FunctionVisitor(EmitVisitor):
- """Visitor to generate constructor functions for AST."""
-
def visitModule(self, mod):
for dfn in mod.dfns:
--- 184,187 ----
***************
*** 200,208 ****
for t in sum.types:
self.visit(t, name)
- self.emit("", 0)

def visitConstructor(self, cons, type):
- def emit(s, depth=0):
- self.emit(s, depth)
args = []
if cons.fields:
--- 203,208 ----
***************
*** 217,224 ****
name = f.name
args.append((get_c_type(f.type), name))
- argstr = ", ".join(["%s %s" % (atype, aname) for atype, aname in args])
ctype = get_c_type(type)
self.emit("%s" % ctype, 0)
! emit("%s(%s)" % (cons.name, argstr))
emit("{")
emit("%s p = (%s)malloc(sizeof(*p));" % (ctype, ctype), 1)
--- 217,244 ----
name = f.name
args.append((get_c_type(f.type), name))
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)
+ for atype, aname in args])
+ else:
+ argstr = "void"
+ 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):
+ self.emit(s, depth)
+ argstr = ", ".join(["%s %s" % (atype, aname) for atype, aname in args])
self.emit("%s" % ctype, 0)
! emit("%s(%s)" % (name, argstr))
emit("{")
emit("%s p = (%s)malloc(sizeof(*p));" % (ctype, ctype), 1)
***************
*** 227,239 ****
emit("return NULL;", 2)
emit("}", 1)
! emit("p->kind = %s_kind;" % cons.name, 1)
for argtype, argname in args:
! emit("p->v.%s.%s = %s;" % (cons.name, argname, argname), 1)
emit("return p;", 1)
emit("}")
!
! def visitProduct(self, prod, name):
! pass
!
class ChainOfVisitors:
def __init__(self, *visitors):
--- 247,257 ----
emit("return NULL;", 2)
emit("}", 1)
! emit("p->kind = %s_kind;" % name, 1)
for argtype, argname in args:
! emit("p->v.%s.%s = %s;" % (name, argname, argname), 1)
emit("return p;", 1)
emit("}")
! emit("")
!
class ChainOfVisitors:
def __init__(self, *visitors):
***************
*** 254,258 ****
c = ChainOfVisitors(TypeDefVisitor(f),
StructVisitor(f),
! ## PrototypeVisitor(f)
)
c.visit(mod)
--- 272,276 ----
c = ChainOfVisitors(TypeDefVisitor(f),
StructVisitor(f),
! PrototypeVisitor(f)
)
c.visit(mod)