GeezerVison is showing github.com — new option `j2py --rusthon` outputs Rusthon syntax. · rusthon/java2python@04d02c1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bin/j2py +10-9Lines changed: 10 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,8 +17,7 @@ from java2python.compiler import Module, buildAST, transformAST | 17 | 17 | from java2python.config import Config | 18 | 18 | from java2python.lib import escapes | 19 | 19 |
| 20 | | - | 21 | | -version = '0.5.1' | | 20 | +version = '0.5.2' | 22 | 21 |
| 23 | 22 |
| 24 | 23 | def logLevel(value): | @@ -87,7 +86,6 @@ def runTransform(options): | 87 | 86 | """ Compile the indicated java source with the given options. """ | 88 | 87 | timed = defaultdict(time) | 89 | 88 | timed['overall'] | 90 | | - | 91 | 89 | filein = fileout = filedefault = '-' | 92 | 90 | if options.inputfile and not isinstance(options.inputfile, file): | 93 | 91 | filein = options.inputfile | @@ -116,11 +114,12 @@ def runTransform(options): | 116 | 114 | return code | 117 | 115 |
| 118 | 116 | timed['comp'] | 119 | | - try: | 120 | | - tree = buildAST(source) | 121 | | - except (Exception, ), exc: | 122 | | - exception('exception while parsing') | 123 | | - return 1 | | 117 | + tree = buildAST(source) | | 118 | + #try: | | 119 | + # tree = buildAST(source) | | 120 | + #except (Exception, ), exc: | | 121 | + # exception('exception while parsing') | | 122 | + # return 1 | 124 | 123 | timed['comp_finish'] | 125 | 124 |
| 126 | 125 | config = Config(configs) | @@ -130,6 +129,7 @@ def runTransform(options): | 130 | 129 |
| 131 | 130 | timed['visit'] | 132 | 131 | module = Module(config) | | 132 | + module.set_global_options(options) | 133 | 133 | module.sourceFilename = path.abspath(filein) if filein != '-' else None | 134 | 134 | module.name = path.splitext(path.basename(filein))[0] if filein != '-' else '<stdin>' | 135 | 135 | module.walk(tree) | @@ -161,7 +161,7 @@ def runTransform(options): | 161 | 161 | module.name = path.splitext(filein)[0] if filein != '-' else '<stdin>' | 162 | 162 | print >> output, source | 163 | 163 |
| 164 | | - if not options.skipcompile: | | 164 | + if not options.skipcompile and not options.rusthon: | 165 | 165 | try: | 166 | 166 | compile(source, '<string>', 'exec') | 167 | 167 | except (SyntaxError, ), ex: | @@ -243,6 +243,7 @@ def configScript(argv): | 243 | 243 | help='Print lexer tokens to stderr.', | 244 | 244 | default=False, action='store_true') | 245 | 245 | add('-v', '--version', action='version', version='%(prog)s ' + version) | | 246 | + add('--rusthon', help='output rusthon code', action='store_true') | 246 | 247 |
| 247 | 248 | ns = parser.parse_args(argv) | 248 | 249 | if ns.inputfile == '-': | |
Collapse file Copy file name to clipboardExpand all lines: java2python/compiler/template.py +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | from java2python.lang import tokens | 21 | 21 | from java2python.lib import FS, colors | 22 | 22 |
| | 23 | +OPTIONS = None | 23 | 24 |
| 24 | 25 | class Factory(object): | 25 | 26 | """ Factory -> creates pre-configured callables for new block instances. | @@ -162,6 +163,8 @@ def altIdent(self, name): | 162 | 163 | return name | 163 | 164 | if name in method.variables: | 164 | 165 | return name | | 166 | + if OPTIONS and OPTIONS.rusthon: | | 167 | + raise RuntimeError('ooookkk') | 165 | 168 | return ('cls' if method.isStatic else 'self') + '.' + name | 166 | 169 | return name | 167 | 170 |
| @@ -480,7 +483,10 @@ def iterDecl(self): | 480 | 483 | def formatParam(p): | 481 | 484 | if 'default' in p: | 482 | 485 | return '{0}={1}'.format(p['name'], p['default']) | 483 | | - return p['name'] | | 486 | + if OPTIONS and OPTIONS.rusthon: | | 487 | + return '%s:%s' %(p['name'],p['type']) | | 488 | + else: | | 489 | + return p['name'] | 484 | 490 | params = ', '.join(formatParam(param) for param in self.iterParams()) | 485 | 491 | yield 'def {0}({1}):'.format(self.name, params) | 486 | 492 |
| |
Collapse file Copy file name to clipboardExpand all lines: java2python/compiler/visitor.py +12-2Lines changed: 12 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 20 | 20 | from java2python.lang import tokens | 21 | 21 | from java2python.lib import FS | 22 | 22 |
| | 23 | +OPTIONS = None | 23 | 24 |
| 24 | 25 | class Memo(object): | 25 | 26 | """ Memo -> AST walking luggage. """ | @@ -33,6 +34,12 @@ class Base(object): | 33 | 34 |
| 34 | 35 | commentSubs = map(recompile, ['^\s*/(\*)+', '(\*)+/\s*$', '^\s*//']) | 35 | 36 |
| | 37 | + def set_global_options(self, cfg): | | 38 | + global OPTIONS | | 39 | + OPTIONS = cfg | | 40 | + import java2python.compiler.template | | 41 | + java2python.compiler.template.OPTIONS = cfg | | 42 | + | 36 | 43 | def accept(self, node, memo): | 37 | 44 | """ Accept a node, possibly creating a child visitor. """ | 38 | 45 | tokType = tokens.map.get(node.token.type) | @@ -139,7 +146,6 @@ def acceptInterface(self, node, memo): | 139 | 146 |
| 140 | 147 | class Module(TypeAcceptor, Base): | 141 | 148 | """ Module -> accepts AST branches for module-level objects. """ | 142 | | - | 143 | 149 | def makeAcceptHandledDecl(part): | 144 | 150 | """ Creates an accept function for a decl to be processed by a handler. """ | 145 | 151 | def acceptDecl(self, node, memo): | @@ -169,6 +175,7 @@ def acceptModifierList(self, node, memo): | 169 | 175 |
| 170 | 176 | def nodesToAnnos(self, branch, memo): | 171 | 177 | """ Convert the annotations in the given branch to a decorator. """ | | 178 | + raise RuntimeError('nodesToAnnos') | 172 | 179 | name = branch.firstChildOfType(tokens.IDENT).text | 173 | 180 | init = branch.firstChildOfType(tokens.ANNOTATION_INIT_BLOCK) | 174 | 181 | if not init: | @@ -196,7 +203,10 @@ def nodesToModifiers(self, branch, root): | 196 | 203 | if root.parentType in tokens.methodTypes: | 197 | 204 | self.modifiers.extend(n.text for n in root.children) | 198 | 205 | if self.isStatic and self.parameters: | 199 | | - self.parameters[0]['name'] = 'cls' | | 206 | + name = 'cls' | | 207 | + #if OPTIONS and OPTIONS.rusthon: | | 208 | + # name = 'cls:self' | | 209 | + self.parameters[0]['name'] = name | 200 | 210 | self.modifiers.append(branch.text) | 201 | 211 |
| 202 | 212 |
| |
Collapse file Copy file name to clipboardExpand all lines: readme.md +12-4Lines changed: 12 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | ## java2python | 2 | 2 |
| 3 | | -Simple but effective tool to translate Java source code into Python. | | 3 | +Simple but effective tool to translate Java source code into Python and Rusthon. | 4 | 4 |
| 5 | 5 |
| 6 | 6 | The java2python package can translate any syntactically valid Java source code | @@ -30,12 +30,14 @@ class HelloWorld { | 30 | 30 | } | 31 | 31 | ``` | 32 | 32 |
| 33 | | -Next we run our program: | 34 | | - | 35 | | - | | 33 | +Convert Java Program To Python | | 34 | +----------------------------- | 36 | 35 | ```bash | 37 | 36 | $ j2py HelloWorld.java | 38 | 37 | ``` | | 38 | + | | 39 | +Python Output | | 40 | +------------- | 39 | 41 | ```python | 40 | 42 | #!/usr/bin/env python | 41 | 43 | """ generated source for module HelloWorld """ | @@ -54,6 +56,12 @@ if __name__ == '__main__': | 54 | 56 | HelloWorld.main(sys.argv) | 55 | 57 | ``` | 56 | 58 |
| | 59 | +Convert to Rusthon | | 60 | +-------------- | | 61 | +```bash | | 62 | +$ j2py --rusthon HelloWorld.java | | 63 | +``` | | 64 | + | 57 | 65 |
| 58 | 66 | [downloads]: https://github.com/natural/java2python/downloads | 59 | 67 | [installation]: https://github.com/natural/java2python/tree/master/doc/install.md | |
|