| 279 | self.sum_with_constructors(sum, name, depth) |
| 280 | |
| 281 | def sum_with_constructors(self, sum, name, depth): |
| 282 | def emit(s, depth=depth): |
| 283 | self.emit(s % sys._getframe(1).f_locals, depth) |
| 284 | enum = [] |
| 285 | for i in range(len(sum.types)): |
| 286 | type = sum.types[i] |
| 287 | enum.append("%s_kind=%d" % (type.name, i + 1)) |
| 288 | |
| 289 | emit("enum _%(name)s_kind {" + ", ".join(enum) + "};") |
| 290 | |
| 291 | emit("struct _%(name)s {") |
| 292 | emit("enum _%(name)s_kind kind;", depth + 1) |
| 293 | emit("union {", depth + 1) |
| 294 | for t in sum.types: |
| 295 | self.visit(t, depth + 2) |
| 296 | emit("} v;", depth + 1) |
| 297 | for field in sum.attributes: |
| 298 | # rudimentary attribute handling |
| 299 | type = str(field.type) |
| 300 | assert type in asdl.builtin_types, type |
| 301 | emit("%s %s;" % (type, field.name), depth + 1); |
| 302 | emit("};") |
| 303 | emit("") |
| 304 | |
| 305 | def visitConstructor(self, cons, depth): |
| 306 | if cons.fields: |