Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 04d02c1

Browse files
author
hartsantler
committed
new option j2py --rusthon outputs Rusthon syntax.
1 parent ee997c7 commit 04d02c1

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

bin/j2py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ from java2python.compiler import Module, buildAST, transformAST
1717
from java2python.config import Config
1818
from java2python.lib import escapes
1919

20-
21-
version = '0.5.1'
20+
version = '0.5.2'
2221

2322

2423
def logLevel(value):
@@ -87,7 +86,6 @@ def runTransform(options):
8786
""" Compile the indicated java source with the given options. """
8887
timed = defaultdict(time)
8988
timed['overall']
90-
9189
filein = fileout = filedefault = '-'
9290
if options.inputfile and not isinstance(options.inputfile, file):
9391
filein = options.inputfile
@@ -116,11 +114,12 @@ def runTransform(options):
116114
return code
117115

118116
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
124123
timed['comp_finish']
125124

126125
config = Config(configs)
@@ -130,6 +129,7 @@ def runTransform(options):
130129

131130
timed['visit']
132131
module = Module(config)
132+
module.set_global_options(options)
133133
module.sourceFilename = path.abspath(filein) if filein != '-' else None
134134
module.name = path.splitext(path.basename(filein))[0] if filein != '-' else '<stdin>'
135135
module.walk(tree)
@@ -161,7 +161,7 @@ def runTransform(options):
161161
module.name = path.splitext(filein)[0] if filein != '-' else '<stdin>'
162162
print >> output, source
163163

164-
if not options.skipcompile:
164+
if not options.skipcompile and not options.rusthon:
165165
try:
166166
compile(source, '<string>', 'exec')
167167
except (SyntaxError, ), ex:
@@ -243,6 +243,7 @@ def configScript(argv):
243243
help='Print lexer tokens to stderr.',
244244
default=False, action='store_true')
245245
add('-v', '--version', action='version', version='%(prog)s ' + version)
246+
add('--rusthon', help='output rusthon code', action='store_true')
246247

247248
ns = parser.parse_args(argv)
248249
if ns.inputfile == '-':

java2python/compiler/template.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from java2python.lang import tokens
2121
from java2python.lib import FS, colors
2222

23+
OPTIONS = None
2324

2425
class Factory(object):
2526
""" Factory -> creates pre-configured callables for new block instances.
@@ -162,6 +163,8 @@ def altIdent(self, name):
162163
return name
163164
if name in method.variables:
164165
return name
166+
if OPTIONS and OPTIONS.rusthon:
167+
raise RuntimeError('ooookkk')
165168
return ('cls' if method.isStatic else 'self') + '.' + name
166169
return name
167170

@@ -480,7 +483,10 @@ def iterDecl(self):
480483
def formatParam(p):
481484
if 'default' in p:
482485
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']
484490
params = ', '.join(formatParam(param) for param in self.iterParams())
485491
yield 'def {0}({1}):'.format(self.name, params)
486492

java2python/compiler/visitor.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from java2python.lang import tokens
2121
from java2python.lib import FS
2222

23+
OPTIONS = None
2324

2425
class Memo(object):
2526
""" Memo -> AST walking luggage. """
@@ -33,6 +34,12 @@ class Base(object):
3334

3435
commentSubs = map(recompile, ['^\s*/(\*)+', '(\*)+/\s*$', '^\s*//'])
3536

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+
3643
def accept(self, node, memo):
3744
""" Accept a node, possibly creating a child visitor. """
3845
tokType = tokens.map.get(node.token.type)
@@ -139,7 +146,6 @@ def acceptInterface(self, node, memo):
139146

140147
class Module(TypeAcceptor, Base):
141148
""" Module -> accepts AST branches for module-level objects. """
142-
143149
def makeAcceptHandledDecl(part):
144150
""" Creates an accept function for a decl to be processed by a handler. """
145151
def acceptDecl(self, node, memo):
@@ -169,6 +175,7 @@ def acceptModifierList(self, node, memo):
169175

170176
def nodesToAnnos(self, branch, memo):
171177
""" Convert the annotations in the given branch to a decorator. """
178+
raise RuntimeError('nodesToAnnos')
172179
name = branch.firstChildOfType(tokens.IDENT).text
173180
init = branch.firstChildOfType(tokens.ANNOTATION_INIT_BLOCK)
174181
if not init:
@@ -196,7 +203,10 @@ def nodesToModifiers(self, branch, root):
196203
if root.parentType in tokens.methodTypes:
197204
self.modifiers.extend(n.text for n in root.children)
198205
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
200210
self.modifiers.append(branch.text)
201211

202212

readme.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## java2python
22

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.
44

55

66
The java2python package can translate any syntactically valid Java source code
@@ -30,12 +30,14 @@ class HelloWorld {
3030
}
3131
```
3232

33-
Next we run our program:
34-
35-
33+
Convert Java Program To Python
34+
-----------------------------
3635
```bash
3736
$ j2py HelloWorld.java
3837
```
38+
39+
Python Output
40+
-------------
3941
```python
4042
#!/usr/bin/env python
4143
""" generated source for module HelloWorld """
@@ -54,6 +56,12 @@ if __name__ == '__main__':
5456
HelloWorld.main(sys.argv)
5557
```
5658

59+
Convert to Rusthon
60+
--------------
61+
```bash
62+
$ j2py --rusthon HelloWorld.java
63+
```
64+
5765

5866
[downloads]: https://github.com/natural/java2python/downloads
5967
[installation]: https://github.com/natural/java2python/tree/master/doc/install.md

0 commit comments

Comments
 (0)