Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python |
| 2 | """Generate C code from an ASDL description.""" |
| 3 | |
| 4 | # TO DO |
| 5 | # handle fields that have a type but no name |
| 6 | |
| 7 | import os, sys, traceback |
| 8 | |
| 9 | import asdl |
| 10 | |
| 11 | TABSIZE = 8 |
| 12 | MAX_COL = 80 |
| 13 | |
| 14 | def get_c_type(name): |
| 15 | """Return a string for the C name of the type. |
| 16 | |
| 17 | This function special cases the default types provided by asdl: |
| 18 | identifier, string, int, bool. |
| 19 | """ |
| 20 | # XXX ack! need to figure out where Id is useful and where string |
| 21 | if isinstance(name, asdl.Id): |
| 22 | name = name.value |
| 23 | if name in asdl.builtin_types: |
| 24 | return name |
| 25 | else: |
| 26 | return "%s_ty" % name |
| 27 | |
| 28 | def reflow_lines(s, depth): |
| 29 | """Reflow the line s indented depth tabs. |
| 30 | |
| 31 | Return a sequence of lines where no line extends beyond MAX_COL |
| 32 | when properly indented. The first line is properly indented based |
| 33 | exclusively on depth * TABSIZE. All following lines -- these are |
| 34 | the reflowed lines generated by this function -- start at the same |
| 35 | column as the first character beyond the opening { in the first |
| 36 | line. |
| 37 | """ |
| 38 | size = MAX_COL - depth * TABSIZE |
| 39 | if len(s) < size: |
| 40 | return [s] |
| 41 | |
| 42 | lines = [] |
| 43 | cur = s |
| 44 | padding = "" |
| 45 | while len(cur) > size: |
| 46 | i = cur.rfind(' ', 0, size) |
| 47 | # XXX this should be fixed for real |
| 48 | if i == -1 and 'GeneratorExp' in cur: |
| 49 | i = size + 3 |
| 50 | assert i != -1, "Impossible line %d to reflow: %s" % (size, `s`) |
| 51 | lines.append(padding + cur[:i]) |
| 52 | if len(lines) == 1: |
| 53 | # find new size based on brace |
| 54 | j = cur.find('{', 0, i) |
| 55 | if j >= 0: |
| 56 | j += 2 # account for the brace and the space after it |
| 57 | size -= j |
| 58 | padding = " " * j |
| 59 | else: |
| 60 | j = cur.find('(', 0, i) |
| 61 | if j >= 0: |
| 62 | j += 1 # account for the paren (no space after it) |
| 63 | size -= j |
| 64 | padding = " " * j |
| 65 | cur = cur[i+1:] |
| 66 | else: |
| 67 | lines.append(padding + cur) |
| 68 | return lines |
| 69 | |
| 70 | def is_simple(sum): |
| 71 | """Return True if a sum is a simple. |
| 72 | |
| 73 | A sum is simple if its types have no fields, e.g. |
| 74 | unaryop = Invert | Not | UAdd | USub |
| 75 | """ |
| 76 | |
| 77 | for t in sum.types: |
| 78 | if t.fields: |
| 79 | return False |
| 80 | return True |
| 81 | |
| 82 | class EmitVisitor(asdl.VisitorBase): |
| 83 | """Visit that emits lines""" |
| 84 | |
| 85 | def __init__(self, file): |
| 86 | self.file = file |
| 87 | super(EmitVisitor, self).__init__() |
| 88 | |
| 89 | def emit(self, s, depth, reflow=1): |
| 90 | # XXX reflow long lines? |
| 91 | if reflow: |
| 92 | lines = reflow_lines(s, depth) |
| 93 | else: |
| 94 | lines = [s] |
| 95 | for line in lines: |
| 96 | line = (" " * TABSIZE * depth) + line + "\n" |
| 97 | self.file.write(line) |
| 98 | |
| 99 | class TypeDefVisitor(EmitVisitor): |
| 100 | def visitModule(self, mod): |
| 101 | for dfn in mod.dfns: |
| 102 | self.visit(dfn) |
| 103 | |
| 104 | def visitType(self, type, depth=0): |
| 105 | self.visit(type.value, type.name, depth) |
| 106 | |
| 107 | def visitSum(self, sum, name, depth): |
| 108 | if is_simple(sum): |
| 109 | self.simple_sum(sum, name, depth) |
| 110 | else: |
| 111 | self.sum_with_constructors(sum, name, depth) |
| 112 | |
| 113 | def simple_sum(self, sum, name, depth): |
| 114 | enum = [] |
| 115 | for i in range(len(sum.types)): |
| 116 | type = sum.types[i] |
| 117 | enum.append("%s=%d" % (type.name, i + 1)) |
| 118 | enums = ", ".join(enum) |
| 119 | ctype = get_c_type(name) |
| 120 | s = "typedef enum _%s { %s } %s;" % (name, enums, ctype) |
| 121 | self.emit(s, depth) |
| 122 | self.emit("", depth) |
| 123 | |
| 124 | def sum_with_constructors(self, sum, name, depth): |
| 125 | ctype = get_c_type(name) |
| 126 | s = "typedef struct _%(name)s *%(ctype)s;" % locals() |
| 127 | self.emit(s, depth) |
| 128 | self.emit("", depth) |
| 129 | |
| 130 | def visitProduct(self, product, name, depth): |
| 131 | ctype = get_c_type(name) |
| 132 | s = "typedef struct _%(name)s *%(ctype)s;" % locals() |
| 133 | self.emit(s, depth) |
| 134 | self.emit("", depth) |
| 135 | |
| 136 | class StructVisitor(EmitVisitor): |
| 137 | """Visitor to generate typdefs for AST.""" |
| 138 | |
| 139 | def visitModule(self, mod): |
| 140 | for dfn in mod.dfns: |
| 141 | self.visit(dfn) |
| 142 | |
| 143 | def visitType(self, type, depth=0): |
| 144 | self.visit(type.value, type.name, depth) |
| 145 | |
| 146 | def visitSum(self, sum, name, depth): |
| 147 | if not is_simple(sum): |
| 148 | self.sum_with_constructors(sum, name, depth) |
| 149 | |
| 150 | def sum_with_constructors(self, sum, name, depth): |
| 151 | def emit(s, depth=depth): |
| 152 | self.emit(s % sys._getframe(1).f_locals, depth) |
| 153 | enum = [] |
| 154 | for i in range(len(sum.types)): |
| 155 | type = sum.types[i] |
| 156 | enum.append("%s_kind=%d" % (type.name, i + 1)) |
| 157 | |
Martin v. Löwis | 01b8101 | 2006-04-11 08:06:50 +0000 | [diff] [blame] | 158 | emit("enum _%(name)s_kind {" + ", ".join(enum) + "};") |
| 159 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 160 | emit("struct _%(name)s {") |
Martin v. Löwis | 01b8101 | 2006-04-11 08:06:50 +0000 | [diff] [blame] | 161 | emit("enum _%(name)s_kind kind;", depth + 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 162 | emit("union {", depth + 1) |
| 163 | for t in sum.types: |
| 164 | self.visit(t, depth + 2) |
| 165 | emit("} v;", depth + 1) |
| 166 | for field in sum.attributes: |
| 167 | # rudimentary attribute handling |
| 168 | type = str(field.type) |
| 169 | assert type in asdl.builtin_types, type |
| 170 | emit("%s %s;" % (type, field.name), depth + 1); |
| 171 | emit("};") |
| 172 | emit("") |
| 173 | |
| 174 | def visitConstructor(self, cons, depth): |
| 175 | if cons.fields: |
| 176 | self.emit("struct {", depth) |
| 177 | for f in cons.fields: |
| 178 | self.visit(f, depth + 1) |
| 179 | self.emit("} %s;" % cons.name, depth) |
| 180 | self.emit("", depth) |
| 181 | else: |
| 182 | # XXX not sure what I want here, nothing is probably fine |
| 183 | pass |
| 184 | |
| 185 | def visitField(self, field, depth): |
| 186 | # XXX need to lookup field.type, because it might be something |
| 187 | # like a builtin... |
| 188 | ctype = get_c_type(field.type) |
| 189 | name = field.name |
| 190 | if field.seq: |
Martin v. Löwis | 0cc56e5 | 2006-04-13 12:29:43 +0000 | [diff] [blame] | 191 | if field.type.value in ('cmpop',): |
| 192 | self.emit("asdl_int_seq *%(name)s;" % locals(), depth) |
| 193 | else: |
| 194 | self.emit("asdl_seq *%(name)s;" % locals(), depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 195 | else: |
| 196 | self.emit("%(ctype)s %(name)s;" % locals(), depth) |
| 197 | |
| 198 | def visitProduct(self, product, name, depth): |
| 199 | self.emit("struct _%(name)s {" % locals(), depth) |
| 200 | for f in product.fields: |
| 201 | self.visit(f, depth + 1) |
| 202 | self.emit("};", depth) |
| 203 | self.emit("", depth) |
| 204 | |
| 205 | class PrototypeVisitor(EmitVisitor): |
| 206 | """Generate function prototypes for the .h file""" |
| 207 | |
| 208 | def visitModule(self, mod): |
| 209 | for dfn in mod.dfns: |
| 210 | self.visit(dfn) |
| 211 | |
| 212 | def visitType(self, type): |
| 213 | self.visit(type.value, type.name) |
| 214 | |
| 215 | def visitSum(self, sum, name): |
| 216 | if is_simple(sum): |
| 217 | pass # XXX |
| 218 | else: |
| 219 | for t in sum.types: |
| 220 | self.visit(t, name, sum.attributes) |
| 221 | |
| 222 | def get_args(self, fields): |
| 223 | """Return list of C argument into, one for each field. |
| 224 | |
| 225 | Argument info is 3-tuple of a C type, variable name, and flag |
| 226 | that is true if type can be NULL. |
| 227 | """ |
| 228 | args = [] |
| 229 | unnamed = {} |
| 230 | for f in fields: |
| 231 | if f.name is None: |
| 232 | name = f.type |
| 233 | c = unnamed[name] = unnamed.get(name, 0) + 1 |
| 234 | if c > 1: |
| 235 | name = "name%d" % (c - 1) |
| 236 | else: |
| 237 | name = f.name |
| 238 | # XXX should extend get_c_type() to handle this |
| 239 | if f.seq: |
Martin v. Löwis | 0cc56e5 | 2006-04-13 12:29:43 +0000 | [diff] [blame] | 240 | if f.type.value in ('cmpop',): |
| 241 | ctype = "asdl_int_seq *" |
| 242 | else: |
| 243 | ctype = "asdl_seq *" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 244 | else: |
| 245 | ctype = get_c_type(f.type) |
| 246 | args.append((ctype, name, f.opt or f.seq)) |
| 247 | return args |
| 248 | |
| 249 | def visitConstructor(self, cons, type, attrs): |
| 250 | args = self.get_args(cons.fields) |
| 251 | attrs = self.get_args(attrs) |
| 252 | ctype = get_c_type(type) |
| 253 | self.emit_function(cons.name, ctype, args, attrs) |
| 254 | |
| 255 | def emit_function(self, name, ctype, args, attrs, union=1): |
| 256 | args = args + attrs |
| 257 | if args: |
| 258 | argstr = ", ".join(["%s %s" % (atype, aname) |
| 259 | for atype, aname, opt in args]) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 260 | argstr += ", PyArena *arena" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 261 | else: |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 262 | argstr = "PyArena *arena" |
Martin v. Löwis | 9d179ce | 2007-01-19 06:42:33 +0000 | [diff] [blame^] | 263 | margs = "a0" |
| 264 | for i in range(1, len(args)+1): |
| 265 | margs += ", a%d" % i |
| 266 | self.emit("#define %s(%s) _Py_%s(%s)" % (name, margs, name, margs), 0, |
| 267 | reflow = 0) |
| 268 | self.emit("%s _Py_%s(%s);" % (ctype, name, argstr), 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 269 | |
| 270 | def visitProduct(self, prod, name): |
| 271 | self.emit_function(name, get_c_type(name), |
| 272 | self.get_args(prod.fields), [], union=0) |
| 273 | |
| 274 | class FunctionVisitor(PrototypeVisitor): |
| 275 | """Visitor to generate constructor functions for AST.""" |
| 276 | |
| 277 | def emit_function(self, name, ctype, args, attrs, union=1): |
| 278 | def emit(s, depth=0, reflow=1): |
| 279 | self.emit(s, depth, reflow) |
| 280 | argstr = ", ".join(["%s %s" % (atype, aname) |
| 281 | for atype, aname, opt in args + attrs]) |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 282 | if argstr: |
| 283 | argstr += ", PyArena *arena" |
| 284 | else: |
| 285 | argstr = "PyArena *arena" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 286 | self.emit("%s" % ctype, 0) |
| 287 | emit("%s(%s)" % (name, argstr)) |
| 288 | emit("{") |
| 289 | emit("%s p;" % ctype, 1) |
| 290 | for argtype, argname, opt in args: |
| 291 | # XXX hack alert: false is allowed for a bool |
Jeremy Hylton | 2f327c1 | 2006-04-04 04:00:23 +0000 | [diff] [blame] | 292 | if not opt and not (argtype == "bool" or argtype == "int"): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 293 | emit("if (!%s) {" % argname, 1) |
| 294 | emit("PyErr_SetString(PyExc_ValueError,", 2) |
| 295 | msg = "field %s is required for %s" % (argname, name) |
| 296 | emit(' "%s");' % msg, |
| 297 | 2, reflow=0) |
| 298 | emit('return NULL;', 2) |
| 299 | emit('}', 1) |
| 300 | |
Neal Norwitz | adb69fc | 2005-12-17 20:54:49 +0000 | [diff] [blame] | 301 | emit("p = (%s)PyArena_Malloc(arena, sizeof(*p));" % ctype, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 302 | emit("if (!p) {", 1) |
Neal Norwitz | a34584b | 2005-10-23 18:59:17 +0000 | [diff] [blame] | 303 | emit("PyErr_NoMemory();", 2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 304 | emit("return NULL;", 2) |
| 305 | emit("}", 1) |
| 306 | if union: |
| 307 | self.emit_body_union(name, args, attrs) |
| 308 | else: |
| 309 | self.emit_body_struct(name, args, attrs) |
| 310 | emit("return p;", 1) |
| 311 | emit("}") |
| 312 | emit("") |
| 313 | |
| 314 | def emit_body_union(self, name, args, attrs): |
| 315 | def emit(s, depth=0, reflow=1): |
| 316 | self.emit(s, depth, reflow) |
| 317 | emit("p->kind = %s_kind;" % name, 1) |
| 318 | for argtype, argname, opt in args: |
| 319 | emit("p->v.%s.%s = %s;" % (name, argname, argname), 1) |
| 320 | for argtype, argname, opt in attrs: |
| 321 | emit("p->%s = %s;" % (argname, argname), 1) |
| 322 | |
| 323 | def emit_body_struct(self, name, args, attrs): |
| 324 | def emit(s, depth=0, reflow=1): |
| 325 | self.emit(s, depth, reflow) |
| 326 | for argtype, argname, opt in args: |
| 327 | emit("p->%s = %s;" % (argname, argname), 1) |
| 328 | assert not attrs |
| 329 | |
| 330 | class PickleVisitor(EmitVisitor): |
| 331 | |
| 332 | def visitModule(self, mod): |
| 333 | for dfn in mod.dfns: |
| 334 | self.visit(dfn) |
| 335 | |
| 336 | def visitType(self, type): |
| 337 | self.visit(type.value, type.name) |
| 338 | |
| 339 | def visitSum(self, sum, name): |
| 340 | pass |
| 341 | |
| 342 | def visitProduct(self, sum, name): |
| 343 | pass |
| 344 | |
| 345 | def visitConstructor(self, cons, name): |
| 346 | pass |
| 347 | |
| 348 | def visitField(self, sum): |
| 349 | pass |
| 350 | |
| 351 | class MarshalPrototypeVisitor(PickleVisitor): |
| 352 | |
| 353 | def prototype(self, sum, name): |
| 354 | ctype = get_c_type(name) |
Neal Norwitz | 6576bd8 | 2005-11-13 18:41:28 +0000 | [diff] [blame] | 355 | self.emit("static int marshal_write_%s(PyObject **, int *, %s);" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 356 | % (name, ctype), 0) |
| 357 | |
| 358 | visitProduct = visitSum = prototype |
| 359 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 360 | class PyTypesDeclareVisitor(PickleVisitor): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 361 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 362 | def visitProduct(self, prod, name): |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 363 | self.emit("static PyTypeObject *%s_type;" % name, 0) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 364 | self.emit("static PyObject* ast2obj_%s(void*);" % name, 0) |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 365 | if prod.fields: |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 366 | self.emit("static char *%s_fields[]={" % name,0) |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 367 | for f in prod.fields: |
| 368 | self.emit('"%s",' % f.name, 1) |
| 369 | self.emit("};", 0) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 370 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 371 | def visitSum(self, sum, name): |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 372 | self.emit("static PyTypeObject *%s_type;" % name, 0) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 373 | if sum.attributes: |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 374 | self.emit("static char *%s_attributes[] = {" % name, 0) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 375 | for a in sum.attributes: |
| 376 | self.emit('"%s",' % a.name, 1) |
| 377 | self.emit("};", 0) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 378 | ptype = "void*" |
| 379 | if is_simple(sum): |
| 380 | ptype = get_c_type(name) |
| 381 | tnames = [] |
| 382 | for t in sum.types: |
| 383 | tnames.append(str(t.name)+"_singleton") |
| 384 | tnames = ", *".join(tnames) |
| 385 | self.emit("static PyObject *%s;" % tnames, 0) |
| 386 | self.emit("static PyObject* ast2obj_%s(%s);" % (name, ptype), 0) |
| 387 | for t in sum.types: |
| 388 | self.visitConstructor(t, name) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 389 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 390 | def visitConstructor(self, cons, name): |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 391 | self.emit("static PyTypeObject *%s_type;" % cons.name, 0) |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 392 | if cons.fields: |
Neal Norwitz | 53d960c | 2006-02-28 22:47:29 +0000 | [diff] [blame] | 393 | self.emit("static char *%s_fields[]={" % cons.name, 0) |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 394 | for t in cons.fields: |
| 395 | self.emit('"%s",' % t.name, 1) |
| 396 | self.emit("};",0) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 397 | |
| 398 | class PyTypesVisitor(PickleVisitor): |
| 399 | |
| 400 | def visitModule(self, mod): |
| 401 | self.emit(""" |
| 402 | static PyTypeObject* make_type(char *type, PyTypeObject* base, char**fields, int num_fields) |
| 403 | { |
| 404 | PyObject *fnames, *result; |
| 405 | int i; |
| 406 | if (num_fields) { |
| 407 | fnames = PyTuple_New(num_fields); |
| 408 | if (!fnames) return NULL; |
| 409 | } else { |
| 410 | fnames = Py_None; |
| 411 | Py_INCREF(Py_None); |
| 412 | } |
| 413 | for(i=0; i < num_fields; i++) { |
| 414 | PyObject *field = PyString_FromString(fields[i]); |
| 415 | if (!field) { |
| 416 | Py_DECREF(fnames); |
| 417 | return NULL; |
| 418 | } |
| 419 | PyTuple_SET_ITEM(fnames, i, field); |
| 420 | } |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 421 | result = PyObject_CallFunction((PyObject*)&PyType_Type, "s(O){sOss}", |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 422 | type, base, "_fields", fnames, "__module__", "_ast"); |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 423 | Py_DECREF(fnames); |
| 424 | return (PyTypeObject*)result; |
| 425 | } |
| 426 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 427 | static int add_attributes(PyTypeObject* type, char**attrs, int num_fields) |
| 428 | { |
Neal Norwitz | 19379f1 | 2006-04-03 04:50:58 +0000 | [diff] [blame] | 429 | int i, result; |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 430 | PyObject *s, *l = PyList_New(num_fields); |
| 431 | if (!l) return 0; |
Neal Norwitz | 19379f1 | 2006-04-03 04:50:58 +0000 | [diff] [blame] | 432 | for(i = 0; i < num_fields; i++) { |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 433 | s = PyString_FromString(attrs[i]); |
| 434 | if (!s) { |
| 435 | Py_DECREF(l); |
| 436 | return 0; |
| 437 | } |
| 438 | PyList_SET_ITEM(l, i, s); |
| 439 | } |
Neal Norwitz | 19379f1 | 2006-04-03 04:50:58 +0000 | [diff] [blame] | 440 | result = PyObject_SetAttrString((PyObject*)type, "_attributes", l) >= 0; |
| 441 | Py_DECREF(l); |
| 442 | return result; |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 445 | static PyObject* ast2obj_list(asdl_seq *seq, PyObject* (*func)(void*)) |
| 446 | { |
| 447 | int i, n = asdl_seq_LEN(seq); |
| 448 | PyObject *result = PyList_New(n); |
| 449 | PyObject *value; |
| 450 | if (!result) |
| 451 | return NULL; |
| 452 | for (i = 0; i < n; i++) { |
| 453 | value = func(asdl_seq_GET(seq, i)); |
| 454 | if (!value) { |
| 455 | Py_DECREF(result); |
| 456 | return NULL; |
| 457 | } |
| 458 | PyList_SET_ITEM(result, i, value); |
| 459 | } |
| 460 | return result; |
| 461 | } |
| 462 | |
| 463 | static PyObject* ast2obj_object(void *o) |
| 464 | { |
| 465 | if (!o) |
| 466 | o = Py_None; |
| 467 | Py_INCREF((PyObject*)o); |
| 468 | return (PyObject*)o; |
| 469 | } |
| 470 | #define ast2obj_identifier ast2obj_object |
| 471 | #define ast2obj_string ast2obj_object |
| 472 | static PyObject* ast2obj_bool(bool b) |
| 473 | { |
| 474 | return PyBool_FromLong(b); |
| 475 | } |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 476 | |
| 477 | static PyObject* ast2obj_int(bool b) |
| 478 | { |
| 479 | return PyInt_FromLong(b); |
| 480 | } |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 481 | """, 0, reflow=False) |
| 482 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 483 | self.emit("static int init_types(void)",0) |
| 484 | self.emit("{", 0) |
Neal Norwitz | 19379f1 | 2006-04-03 04:50:58 +0000 | [diff] [blame] | 485 | self.emit("static int initialized;", 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 486 | self.emit("if (initialized) return 1;", 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 487 | self.emit('AST_type = make_type("AST", &PyBaseObject_Type, NULL, 0);', 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 488 | for dfn in mod.dfns: |
| 489 | self.visit(dfn) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 490 | self.emit("initialized = 1;", 1) |
| 491 | self.emit("return 1;", 1); |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 492 | self.emit("}", 0) |
| 493 | |
| 494 | def visitProduct(self, prod, name): |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 495 | if prod.fields: |
| 496 | fields = name.value+"_fields" |
| 497 | else: |
| 498 | fields = "NULL" |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 499 | self.emit('%s_type = make_type("%s", AST_type, %s, %d);' % |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 500 | (name, name, fields, len(prod.fields)), 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 501 | self.emit("if (!%s_type) return 0;" % name, 1) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 502 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 503 | def visitSum(self, sum, name): |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 504 | self.emit('%s_type = make_type("%s", AST_type, NULL, 0);' % (name, name), 1) |
| 505 | self.emit("if (!%s_type) return 0;" % name, 1) |
| 506 | if sum.attributes: |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 507 | self.emit("if (!add_attributes(%s_type, %s_attributes, %d)) return 0;" % |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 508 | (name, name, len(sum.attributes)), 1) |
| 509 | else: |
| 510 | self.emit("if (!add_attributes(%s_type, NULL, 0)) return 0;" % name, 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 511 | simple = is_simple(sum) |
| 512 | for t in sum.types: |
| 513 | self.visitConstructor(t, name, simple) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 514 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 515 | def visitConstructor(self, cons, name, simple): |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 516 | if cons.fields: |
| 517 | fields = cons.name.value+"_fields" |
| 518 | else: |
| 519 | fields = "NULL" |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 520 | self.emit('%s_type = make_type("%s", %s_type, %s, %d);' % |
Martin v. Löwis | 8d0701d | 2006-02-26 23:40:20 +0000 | [diff] [blame] | 521 | (cons.name, cons.name, name, fields, len(cons.fields)), 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 522 | self.emit("if (!%s_type) return 0;" % cons.name, 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 523 | if simple: |
| 524 | self.emit("%s_singleton = PyType_GenericNew(%s_type, NULL, NULL);" % |
| 525 | (cons.name, cons.name), 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 526 | self.emit("if (!%s_singleton) return 0;" % cons.name, 1) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 527 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 528 | class ASTModuleVisitor(PickleVisitor): |
| 529 | |
| 530 | def visitModule(self, mod): |
| 531 | self.emit("PyMODINIT_FUNC", 0) |
| 532 | self.emit("init_ast(void)", 0) |
| 533 | self.emit("{", 0) |
| 534 | self.emit("PyObject *m, *d;", 1) |
| 535 | self.emit("if (!init_types()) return;", 1) |
| 536 | self.emit('m = Py_InitModule3("_ast", NULL, NULL);', 1) |
| 537 | self.emit("if (!m) return;", 1) |
| 538 | self.emit("d = PyModule_GetDict(m);", 1) |
| 539 | self.emit('if (PyDict_SetItemString(d, "AST", (PyObject*)AST_type) < 0) return;', 1) |
| 540 | self.emit('if (PyModule_AddIntConstant(m, "PyCF_ONLY_AST", PyCF_ONLY_AST) < 0)', 1) |
| 541 | self.emit("return;", 2) |
Martin v. Löwis | 40d8459 | 2006-02-28 00:30:54 +0000 | [diff] [blame] | 542 | # Value of version: "$Revision$" |
| 543 | self.emit('if (PyModule_AddStringConstant(m, "__version__", "%s") < 0)' % mod.version.value[12:-3], 1) |
Martin v. Löwis | b003f04 | 2006-02-28 00:37:04 +0000 | [diff] [blame] | 544 | self.emit("return;", 2) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 545 | for dfn in mod.dfns: |
| 546 | self.visit(dfn) |
| 547 | self.emit("}", 0) |
| 548 | |
| 549 | def visitProduct(self, prod, name): |
| 550 | self.addObj(name) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 551 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 552 | def visitSum(self, sum, name): |
| 553 | self.addObj(name) |
| 554 | for t in sum.types: |
| 555 | self.visitConstructor(t, name) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 556 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 557 | def visitConstructor(self, cons, name): |
| 558 | self.addObj(cons.name) |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 559 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 560 | def addObj(self, name): |
Neal Norwitz | 19379f1 | 2006-04-03 04:50:58 +0000 | [diff] [blame] | 561 | self.emit('if (PyDict_SetItemString(d, "%s", (PyObject*)%s_type) < 0) return;' % (name, name), 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 562 | |
| 563 | _SPECIALIZED_SEQUENCES = ('stmt', 'expr') |
| 564 | |
| 565 | def find_sequence(fields, doing_specialization): |
| 566 | """Return True if any field uses a sequence.""" |
| 567 | for f in fields: |
| 568 | if f.seq: |
| 569 | if not doing_specialization: |
| 570 | return True |
| 571 | if str(f.type) not in _SPECIALIZED_SEQUENCES: |
| 572 | return True |
| 573 | return False |
| 574 | |
| 575 | def has_sequence(types, doing_specialization): |
| 576 | for t in types: |
| 577 | if find_sequence(t.fields, doing_specialization): |
| 578 | return True |
| 579 | return False |
| 580 | |
| 581 | |
| 582 | class StaticVisitor(PickleVisitor): |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 583 | CODE = '''Very simple, always emit this static code. Overide CODE''' |
| 584 | |
| 585 | def visit(self, object): |
| 586 | self.emit(self.CODE, 0, reflow=False) |
| 587 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 588 | class ObjVisitor(PickleVisitor): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 589 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 590 | def func_begin(self, name): |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 591 | ctype = get_c_type(name) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 592 | self.emit("PyObject*", 0) |
| 593 | self.emit("ast2obj_%s(void* _o)" % (name), 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 594 | self.emit("{", 0) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 595 | self.emit("%s o = (%s)_o;" % (ctype, ctype), 1) |
| 596 | self.emit("PyObject *result = NULL, *value = NULL;", 1) |
| 597 | self.emit('if (!o) {', 1) |
| 598 | self.emit("Py_INCREF(Py_None);", 2) |
| 599 | self.emit('return Py_None;', 2) |
| 600 | self.emit("}", 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 601 | self.emit('', 0) |
| 602 | |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 603 | def func_end(self): |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 604 | self.emit("return result;", 1) |
| 605 | self.emit("failed:", 0) |
| 606 | self.emit("Py_XDECREF(value);", 1) |
| 607 | self.emit("Py_XDECREF(result);", 1) |
| 608 | self.emit("return NULL;", 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 609 | self.emit("}", 0) |
| 610 | self.emit("", 0) |
| 611 | |
| 612 | def visitSum(self, sum, name): |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 613 | if is_simple(sum): |
| 614 | self.simpleSum(sum, name) |
| 615 | return |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 616 | self.func_begin(name) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 617 | self.emit("switch (o->kind) {", 1) |
| 618 | for i in range(len(sum.types)): |
| 619 | t = sum.types[i] |
| 620 | self.visitConstructor(t, i + 1, name) |
| 621 | self.emit("}", 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 622 | for a in sum.attributes: |
| 623 | self.emit("value = ast2obj_%s(o->%s);" % (a.type, a.name), 1) |
| 624 | self.emit("if (!value) goto failed;", 1) |
Martin v. Löwis | 03e5bc0 | 2006-03-02 00:31:27 +0000 | [diff] [blame] | 625 | self.emit('if (PyObject_SetAttrString(result, "%s", value) < 0)' % a.name, 1) |
| 626 | self.emit('goto failed;', 2) |
| 627 | self.emit('Py_DECREF(value);', 1) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 628 | self.func_end() |
Tim Peters | 710ab3b | 2006-02-28 18:30:36 +0000 | [diff] [blame] | 629 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 630 | def simpleSum(self, sum, name): |
| 631 | self.emit("PyObject* ast2obj_%s(%s_ty o)" % (name, name), 0) |
| 632 | self.emit("{", 0) |
| 633 | self.emit("switch(o) {", 1) |
| 634 | for t in sum.types: |
| 635 | self.emit("case %s:" % t.name, 2) |
| 636 | self.emit("Py_INCREF(%s_singleton);" % t.name, 3) |
| 637 | self.emit("return %s_singleton;" % t.name, 3) |
| 638 | self.emit("}", 1) |
| 639 | self.emit("return NULL; /* cannot happen */", 1) |
| 640 | self.emit("}", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 641 | |
| 642 | def visitProduct(self, prod, name): |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 643 | self.func_begin(name) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 644 | self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % name, 1); |
| 645 | self.emit("if (!result) return NULL;", 1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 646 | for field in prod.fields: |
| 647 | self.visitField(field, name, 1, True) |
Martin v. Löwis | 577b5b9 | 2006-02-27 15:23:19 +0000 | [diff] [blame] | 648 | self.func_end() |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 649 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 650 | def visitConstructor(self, cons, enum, name): |
| 651 | self.emit("case %s_kind:" % cons.name, 1) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 652 | self.emit("result = PyType_GenericNew(%s_type, NULL, NULL);" % cons.name, 2); |
| 653 | self.emit("if (!result) goto failed;", 2) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 654 | for f in cons.fields: |
| 655 | self.visitField(f, cons.name, 2, False) |
| 656 | self.emit("break;", 2) |
| 657 | |
| 658 | def visitField(self, field, name, depth, product): |
| 659 | def emit(s, d): |
| 660 | self.emit(s, depth + d) |
| 661 | if product: |
| 662 | value = "o->%s" % field.name |
| 663 | else: |
| 664 | value = "o->v.%s.%s" % (name, field.name) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 665 | self.set(field, value, depth) |
| 666 | emit("if (!value) goto failed;", 0) |
| 667 | emit('if (PyObject_SetAttrString(result, "%s", value) == -1)' % field.name, 0) |
| 668 | emit("goto failed;", 1) |
| 669 | emit("Py_DECREF(value);", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 670 | |
| 671 | def emitSeq(self, field, value, depth, emit): |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 672 | emit("seq = %s;" % value, 0) |
| 673 | emit("n = asdl_seq_LEN(seq);", 0) |
| 674 | emit("value = PyList_New(n);", 0) |
| 675 | emit("if (!value) goto failed;", 0) |
| 676 | emit("for (i = 0; i < n; i++) {", 0) |
| 677 | self.set("value", field, "asdl_seq_GET(seq, i)", depth + 1) |
| 678 | emit("if (!value1) goto failed;", 1) |
| 679 | emit("PyList_SET_ITEM(value, i, value1);", 1) |
| 680 | emit("value1 = NULL;", 1) |
| 681 | emit("}", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 682 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 683 | def set(self, field, value, depth): |
| 684 | if field.seq: |
Martin v. Löwis | ce1d5d2 | 2006-02-26 20:51:25 +0000 | [diff] [blame] | 685 | # XXX should really check for is_simple, but that requires a symbol table |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 686 | if field.type.value == "cmpop": |
Martin v. Löwis | ce1d5d2 | 2006-02-26 20:51:25 +0000 | [diff] [blame] | 687 | # While the sequence elements are stored as void*, |
| 688 | # ast2obj_cmpop expects an enum |
| 689 | self.emit("{", depth) |
| 690 | self.emit("int i, n = asdl_seq_LEN(%s);" % value, depth+1) |
| 691 | self.emit("value = PyList_New(n);", depth+1) |
| 692 | self.emit("if (!value) goto failed;", depth+1) |
| 693 | self.emit("for(i = 0; i < n; i++)", depth+1) |
| 694 | # This cannot fail, so no need for error handling |
Martin v. Löwis | 0cc56e5 | 2006-04-13 12:29:43 +0000 | [diff] [blame] | 695 | self.emit("PyList_SET_ITEM(value, i, ast2obj_cmpop((cmpop_ty)asdl_seq_GET(%s, i)));" % value, |
Martin v. Löwis | 01b8101 | 2006-04-11 08:06:50 +0000 | [diff] [blame] | 696 | depth+2, reflow=False) |
Martin v. Löwis | ce1d5d2 | 2006-02-26 20:51:25 +0000 | [diff] [blame] | 697 | self.emit("}", depth) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 698 | else: |
Martin v. Löwis | ce1d5d2 | 2006-02-26 20:51:25 +0000 | [diff] [blame] | 699 | self.emit("value = ast2obj_list(%s, ast2obj_%s);" % (value, field.type), depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 700 | else: |
| 701 | ctype = get_c_type(field.type) |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 702 | self.emit("value = ast2obj_%s(%s);" % (field.type, value), depth, reflow=False) |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 703 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 704 | |
Martin v. Löwis | bd260da | 2006-02-26 19:42:26 +0000 | [diff] [blame] | 705 | class PartingShots(StaticVisitor): |
| 706 | |
| 707 | CODE = """ |
| 708 | PyObject* PyAST_mod2obj(mod_ty t) |
| 709 | { |
| 710 | init_types(); |
| 711 | return ast2obj_mod(t); |
| 712 | } |
| 713 | """ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 714 | |
| 715 | class ChainOfVisitors: |
| 716 | def __init__(self, *visitors): |
| 717 | self.visitors = visitors |
| 718 | |
| 719 | def visit(self, object): |
| 720 | for v in self.visitors: |
| 721 | v.visit(object) |
Neal Norwitz | 7b5a604 | 2005-11-13 19:14:20 +0000 | [diff] [blame] | 722 | v.emit("", 0) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 723 | |
| 724 | def main(srcfile): |
Neal Norwitz | 897ff81 | 2005-12-11 21:18:22 +0000 | [diff] [blame] | 725 | argv0 = sys.argv[0] |
Armin Rigo | 6b1793f | 2005-12-14 18:05:14 +0000 | [diff] [blame] | 726 | components = argv0.split(os.sep) |
| 727 | argv0 = os.sep.join(components[-2:]) |
Neal Norwitz | 897ff81 | 2005-12-11 21:18:22 +0000 | [diff] [blame] | 728 | auto_gen_msg = '/* File automatically generated by %s */\n' % argv0 |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 729 | mod = asdl.parse(srcfile) |
| 730 | if not asdl.check(mod): |
| 731 | sys.exit(1) |
| 732 | if INC_DIR: |
| 733 | p = "%s/%s-ast.h" % (INC_DIR, mod.name) |
Martin v. Löwis | 7580149 | 2006-04-14 15:02:32 +0000 | [diff] [blame] | 734 | f = open(p, "wb") |
| 735 | print >> f, auto_gen_msg |
| 736 | print >> f, '#include "asdl.h"\n' |
| 737 | c = ChainOfVisitors(TypeDefVisitor(f), |
| 738 | StructVisitor(f), |
| 739 | PrototypeVisitor(f), |
| 740 | ) |
| 741 | c.visit(mod) |
| 742 | print >>f, "PyObject* PyAST_mod2obj(mod_ty t);" |
| 743 | f.close() |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 744 | |
| 745 | if SRC_DIR: |
Jeremy Hylton | 76c21bd | 2006-04-04 12:11:12 +0000 | [diff] [blame] | 746 | p = os.path.join(SRC_DIR, str(mod.name) + "-ast.c") |
Martin v. Löwis | 7580149 | 2006-04-14 15:02:32 +0000 | [diff] [blame] | 747 | f = open(p, "wb") |
| 748 | print >> f, auto_gen_msg |
| 749 | print >> f, '#include "Python.h"' |
| 750 | print >> f, '#include "%s-ast.h"' % mod.name |
| 751 | print >> f |
| 752 | print >>f, "static PyTypeObject* AST_type;" |
| 753 | v = ChainOfVisitors( |
| 754 | PyTypesDeclareVisitor(f), |
| 755 | PyTypesVisitor(f), |
| 756 | FunctionVisitor(f), |
| 757 | ObjVisitor(f), |
| 758 | ASTModuleVisitor(f), |
| 759 | PartingShots(f), |
| 760 | ) |
| 761 | v.visit(mod) |
| 762 | f.close() |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 763 | |
| 764 | if __name__ == "__main__": |
| 765 | import sys |
| 766 | import getopt |
| 767 | |
| 768 | INC_DIR = '' |
| 769 | SRC_DIR = '' |
| 770 | opts, args = getopt.getopt(sys.argv[1:], "h:c:") |
Martin v. Löwis | 7580149 | 2006-04-14 15:02:32 +0000 | [diff] [blame] | 771 | if len(opts) != 1: |
| 772 | print "Must specify exactly one output file" |
| 773 | sys.exit(1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 774 | for o, v in opts: |
| 775 | if o == '-h': |
| 776 | INC_DIR = v |
| 777 | if o == '-c': |
| 778 | SRC_DIR = v |
| 779 | if len(args) != 1: |
| 780 | print "Must specify single input file" |
Martin v. Löwis | 7580149 | 2006-04-14 15:02:32 +0000 | [diff] [blame] | 781 | sys.exit(1) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 782 | main(args[0]) |