|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +# java2python.compiler.block -> Visitors combined with templates. |
| 4 | +# |
| 5 | +# This module defines classes which combine AST walking with source |
| 6 | +# generation. We've put these two behaviors into separate modules, |
| 7 | +# java2python.compiler.template for creating source code, and |
| 8 | +# java2python.compiler.visitor for walking ANTLR trees. |
| 9 | +# |
| 10 | +# Each of the classes depends on the behavior of its counterpart. |
| 11 | +# This means they're very tightly coupled and that the classes are not |
| 12 | +# very reusable. The module split does allow for grouping of related |
| 13 | +# methods and does hide the cluttered code. |
| 14 | + |
| 15 | +from sys import modules |
| 16 | +from java2python.compiler import template, visitor |
| 17 | + |
| 18 | + |
| 19 | +def addTypeToModule((className, factoryName)): |
| 20 | + """ Constructs and adds a new type to this module. """ |
| 21 | + bases = (getattr(template, className), getattr(visitor, className)) |
| 22 | + newType = type(className, bases, dict(factoryName=factoryName)) |
| 23 | + setattr(modules[__name__], className, newType) |
| 24 | + |
| 25 | + |
| 26 | +map(addTypeToModule, ( |
| 27 | + ('Annotation', 'at'), |
| 28 | + ('Class', 'klass'), |
| 29 | + ('Comment', 'comment'), |
| 30 | + ('Enum', 'enum'), |
| 31 | + ('Expression', 'expr'), |
| 32 | + ('Interface', 'interface'), |
| 33 | + ('Method', 'method'), |
| 34 | + ('MethodContent', 'methodContent'), |
| 35 | + ('Module', 'module'), |
| 36 | + ('Statement', 'statement'), |
| 37 | + ) |
| 38 | + ) |
0 commit comments