Skip to content

Commit

Permalink
add redo functions, few more javaisms
Browse files Browse the repository at this point in the history
git-svn-id: https://pyjamas.svn.sourceforge.net/svnroot/pyjamas/trunk@529 7a2bd370-bda8-463c-979e-2900ccfb811e
  • Loading branch information
lkcl committed Apr 10, 2009
1 parent 37c4846 commit 7790a8c
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions contrib/java2py.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@
""" """


import sys import sys
import string


def countspaces(txt): def countspaces(txt):
count = 0 count = 0
while count < len(txt) and txt[count] == ' ': while count < len(txt) and txt[count] == ' ':
count += 1 count += 1
return count return count

def java2pythonlinebyline(txt): def java2pythonlinebyline(txt):
if txt.find('if (') >= 0: if txt.find('if (') >= 0:
txt = txt.replace('if (', 'if ') txt = txt.replace('if (', 'if ')
txt = txt.replace('!', 'not ')
txt = txt.replace('not =', '!=') # whoops
txt = txt.replace(') {', ':') txt = txt.replace(') {', ':')
elif txt.find('class ') >= 0 and txt.endswith("{"): elif txt.find('class ') >= 0 and txt.endswith("{"):
txt = txt.replace('{', ':') txt = txt.replace('{', ':')
Expand All @@ -36,8 +40,49 @@ def java2pythonlinebyline(txt):
txt = txt.replace("static ", "") txt = txt.replace("static ", "")
if txt[count:].startswith("final ") >= 0: if txt[count:].startswith("final ") >= 0:
txt = txt.replace("final ", "") txt = txt.replace("final ", "")
if txt.endswith(";"):
txt = txt[:-1]
if txt.endswith(" :"):
txt = txt[:-2] + ":"

return txt 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): def reindent(txt):
""" reindents according to { and } braces. strips all whitespace, """ reindents according to { and } braces. strips all whitespace,
possibly not smartest thing to do. oh well. possibly not smartest thing to do. oh well.
Expand All @@ -59,6 +104,9 @@ def java2python(txt):
txt = txt.replace("*/", '"""') txt = txt.replace("*/", '"""')
txt = txt.replace("<>", '!=') txt = txt.replace("<>", '!=')
txt = txt.replace("null", 'None') 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("true", 'True')
txt = txt.replace("false", 'False') txt = txt.replace("false", 'False')
txt = txt.replace("//", '#') txt = txt.replace("//", '#')
Expand All @@ -67,6 +115,7 @@ def java2python(txt):
txt = txt.replace("new ", '') txt = txt.replace("new ", '')
l = txt.split("\n") l = txt.split("\n")
l = map(java2pythonlinebyline, l) l = map(java2pythonlinebyline, l)
l = map(redofunctions, l)
txt = txt.replace("}", '') txt = txt.replace("}", '')
return '\n'.join(l) return '\n'.join(l)


Expand Down

0 comments on commit 7790a8c

Please sign in to comment.