Skip to content

Commit

Permalink
Try a more flexible approach to autogenerating Tree wrapper subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmalcolm committed Apr 20, 2011
1 parent 9b51459 commit a12a44a
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,10 @@ a.out
# Generated by Cython:
optpass.c
tree.c

# Generated elsewhere in build:
tree-types.txt
tree-types.pyx.in
tree.pyx


8 changes: 7 additions & 1 deletion Makefile
Expand Up @@ -30,9 +30,15 @@ optpass.c: optpass.pyx
tree.c: tree.pyx
cython $^ -o $@

tree.pyx: tree.pyx.in
tree.pyx: tree.pyx.in tree-types.pyx.in
cpp $(CFLAGS) tree.pyx.in -o $@

tree-types.txt: tree-types.txt.in
cpp $(CFLAGS) $^ -o $@

tree-types.pyx.in: tree-types.txt maketreetypes.py
python maketreetypes.py > tree-types.pyx.in

# Hint for debugging: add -v to the gcc options
# to get a command line for invoking individual subprocesses
# Doing so seems to require that paths be absolute, rather than relative
Expand Down
34 changes: 34 additions & 0 deletions maketreetypes.py
@@ -0,0 +1,34 @@
from collections import namedtuple

class TreeType(namedtuple('TreeType', 'SYM, STRING, TYPE, NARGS')):
def camel_cased_string(self):
return ''.join([word.title()
for word in self.STRING.split('_')])

def iter_tree_types():
import re
f = open('tree-types.txt')
for line in f:
# e.g.
# ERROR_MARK, "error_mark", tcc_exceptional, 0
m = re.match('(.+), (.+), (.+), (.+)', line)
if m:
yield TreeType(SYM=m.group(1),
STRING=m.group(2)[1:-1],
TYPE=m.group(3),
NARGS=int(m.group(4)))
else:
# print 'UNMATCHED: ', line
assert(line.startswith('#') or line.strip() == '')
f.close()

for t in iter_tree_types():
print (
"""
cdef class %s(Tree):
_STRING = %r
_SYM = %r
_TYPE = %r
_NARGS = %i
""" % (t.camel_cased_string(),
t.STRING, t.SYM, t.TYPE, t.NARGS))
5 changes: 5 additions & 0 deletions tree-types.txt.in
@@ -0,0 +1,5 @@
#define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
SYM, STRING, TYPE, NARGS
#define END_OF_BASE_TREE_CODES
#include "all-tree.def"
#undef DEFTREECODE
8 changes: 1 addition & 7 deletions tree.pyx.in
Expand Up @@ -32,13 +32,7 @@ cdef class Tree:
def __repr__(self):
return 'tree.Tree(%r)' % 'foo'


#define DEFTREECODE(SYM, STRING, TYPE, NARGS) \
cdef class SYM(Tree): \
pass
#define END_OF_BASE_TREE_CODES
#include "all-tree.def"
#undef DEFTREECODE
#include "tree-types.pyx.in"

cdef extern gcc_python_make_wrapper_tree(tree t):
obj = Tree()
Expand Down

0 comments on commit a12a44a

Please sign in to comment.