Permalink
Browse files

more java2py

git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@528 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information...
1 parent bdf2920 commit 37c4846a655e8b53bc50cc4514e443d135b99155 lkcl committed Apr 10, 2009
Showing with 37 additions and 0 deletions.
  1. +37 −0 contrib/java2py.py
View
37 contrib/java2py.py 100644 → 100755
@@ -5,13 +5,43 @@
import sys
+def countspaces(txt):
+ count = 0
+ while count < len(txt) and txt[count] == ' ':
+ count += 1
+ return count
def java2pythonlinebyline(txt):
if txt.find('if (') >= 0:
txt = txt.replace('if (', 'if ')
txt = txt.replace(') {', ':')
+ elif txt.find('class ') >= 0 and txt.endswith("{"):
+ txt = txt.replace('{', ':')
+ elif txt.find('while (') >= 0:
+ txt = txt.replace('while (', 'while ')
+ txt = txt.replace(') {', ':')
+ elif txt.find('for (') >= 0:
+ txt = txt.replace('for (', 'for ')
+ txt = txt.replace(') {', ':')
+ count = countspaces(txt)
+ if txt[count:].startswith("}"):
+ txt = count * ' ' + txt[count+1:]
+
+ if txt[count:].startswith("protected ") >= 0:
+ txt = txt.replace("protected ", "")
+ if txt[count:].startswith("public ") >= 0:
+ txt = txt.replace("public ", "")
+ if txt[count:].startswith("private ") >= 0:
+ txt = txt.replace("private ", "")
+ if txt[count:].startswith("static ") >= 0:
+ txt = txt.replace("static ", "")
+ if txt[count:].startswith("final ") >= 0:
+ txt = txt.replace("final ", "")
return txt
def reindent(txt):
+ """ reindents according to { and } braces. strips all whitespace,
+ possibly not smartest thing to do. oh well.
+ """
res = ''
indent = 0
for l in txt.split("\n"):
@@ -27,10 +57,17 @@ def java2python(txt):
txt = reindent(txt)
txt = txt.replace("/*", '"""')
txt = txt.replace("*/", '"""')
+ txt = txt.replace("<>", '!=')
+ txt = txt.replace("null", 'None')
+ txt = txt.replace("true", 'True')
+ txt = txt.replace("false", 'False')
txt = txt.replace("//", '#')
txt = txt.replace("this.", 'self.')
+ txt = txt.replace("else if", 'elif')
+ txt = txt.replace("new ", '')
l = txt.split("\n")
l = map(java2pythonlinebyline, l)
+ txt = txt.replace("}", '')
return '\n'.join(l)
if __name__ == "__main__":

0 comments on commit 37c4846

Please sign in to comment.