Permalink
Browse files

add redo functions, few more javaisms

git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@529 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information...
lkcl
lkcl committed Apr 10, 2009
1 parent 37c4846 commit 7790a8c15a875bcda8c81b07a30eed75ba9ea6d0
Showing with 49 additions and 0 deletions.
  1. +49 −0 contrib/java2py.py
View
@@ -4,15 +4,19 @@
"""
import sys
+import string
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('!', 'not ')
+ txt = txt.replace('not =', '!=') # whoops
txt = txt.replace(') {', ':')
elif txt.find('class ') >= 0 and txt.endswith("{"):
txt = txt.replace('{', ':')
@@ -36,8 +40,49 @@ def java2pythonlinebyline(txt):
txt = txt.replace("static ", "")
if txt[count:].startswith("final ") >= 0:
txt = txt.replace("final ", "")
+ if txt.endswith(";"):
+ txt = txt[:-1]
+ if txt.endswith(" :"):
+ txt = txt[:-2] + ":"
+
return txt
+def redofunctions(txt):
+ if not txt.endswith("{"):
+ return txt
+ lbr = txt.find("(")
+ rbr = txt.find(") {")
+ if lbr == -1 or rbr == -1 or lbr > rbr:
+ return txt
+ count = countspaces(txt)
+ pre = txt[count:lbr]
+ args = txt[lbr+1:rbr]
+
+ pre = map(string.strip, pre.split(' '))
+ if len(pre) == 1: # assume it's a constructor
+ pre = '__init__'
+ elif len(pre) == 2:
+ pre = pre[-1] # drop the first word (return type)
+ else:
+ error # deliberately cause error - investigate 3-word thingies!
+
+ args = map(string.strip, args.split(','))
+ newargs = []
+ for arg in args:
+ if arg == '':
+ continue
+ arg = map(string.strip, arg.split(' '))
+ if len(arg) == 2:
+ newargs.append(arg[1]) # drop first word (arg type)
+ else:
+ print pre, args, arg
+ error # deliberately cause error - find out why arg no type
+ if count != 0:
+ # assume class not global function - add self
+ newargs = ['self'] + newargs
+ newargs = ', '.join(newargs)
+ return "%sdef %s(%s):" % (count*' ', pre, newargs)
+
def reindent(txt):
""" reindents according to { and } braces. strips all whitespace,
possibly not smartest thing to do. oh well.
@@ -59,6 +104,9 @@ def java2python(txt):
txt = txt.replace("*/", '"""')
txt = txt.replace("<>", '!=')
txt = txt.replace("null", 'None')
+ txt = txt.replace("!= None", 'is not None')
+ txt = txt.replace("== None", 'is None')
+ txt = txt.replace("throw ", 'raise ')
txt = txt.replace("true", 'True')
txt = txt.replace("false", 'False')
txt = txt.replace("//", '#')
@@ -67,6 +115,7 @@ def java2python(txt):
txt = txt.replace("new ", '')
l = txt.split("\n")
l = map(java2pythonlinebyline, l)
+ l = map(redofunctions, l)
txt = txt.replace("}", '')
return '\n'.join(l)

0 comments on commit 7790a8c

Please sign in to comment.