X Tutup
Skip to content

Commit 963c7e6

Browse files
author
Troy Melhase
committed
Updates to tree grammar to support more all of test file. Closes natural#5.
1 parent 3012a5f commit 963c7e6

File tree

3 files changed

+78
-45
lines changed

3 files changed

+78
-45
lines changed

bin/print_java_ast

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
3+
import sys
34

45
from antlr import ASTVisitor
5-
from lib import lexer
6-
from lib import parser
6+
7+
from java2python.lib import lexer
8+
from java2python.lib import parser
9+
10+
try:
11+
import psyco
12+
except (ImportError, ):
13+
pass
14+
else:
15+
psyco.full()
716

817

918
class Printer(ASTVisitor):
@@ -48,10 +57,6 @@ def show(filename):
4857

4958

5059
if __name__ == '__main__':
51-
import sys
52-
import psyco
53-
psyco.full()
54-
5560
try:
5661
name = sys.argv[1]
5762
except (IndexError, ):

lib/sourcetypes.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,12 @@ def fixDecl(self, *args):
231231
@varparam args names to fix
232232
@return fixed name or names
233233
"""
234-
fixed = list(args)
235-
allvars = list(self.allVars)
236-
for i, arg in enumerate(args):
237-
if arg in allvars:
238-
fixed[i] = 'self.%s' % (arg, )
234+
fixed = list(args)
235+
if self.isMethod:
236+
allvars = list(self.allVars)
237+
for i, arg in enumerate(args):
238+
if arg in allvars:
239+
fixed[i] = 'self.%s' % (arg, )
239240
if len(fixed) == 1:
240241
return fixed[0]
241242
else:

lib/walker.g

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ returns [spec]
7575

7676

7777
type_spec_array [block]
78-
: #(ARRAY_DECLARATOR type_spec_array[block])
78+
: #(ad0:ARRAY_DECLARATOR type_spec_array[block])
7979
| spec = type[block]
8080
;
8181

@@ -89,15 +89,15 @@ returns [typ]
8989

9090
builtin_type [block]
9191
returns [typ]
92-
: "void" {typ = "None" }
93-
| "boolean" {typ = "bool" }
94-
| "byte" {typ = "str" }
95-
| "char" {typ = "str" }
96-
| "short" {typ = "int" }
97-
| "int" {typ = "int" }
98-
| "float" {typ = "float" }
99-
| "long" {typ = "long" }
100-
| "double" {typ = "float" }
92+
: "void" {typ = "None" }
93+
| "boolean" {typ = "bool" }
94+
| "byte" {typ = "str" }
95+
| "char" {typ = "str" }
96+
| "short" {typ = "int" }
97+
| "int" {typ = "int" }
98+
| "float" {typ = "float" }
99+
| "long" {typ = "long" }
100+
| "double" {typ = "float" }
101101
;
102102

103103

@@ -186,7 +186,10 @@ method_decl [block]
186186
modifiers[meth]
187187
typ = type_spec[meth]
188188
method_head[meth]
189-
) {meth.type = typ}
189+
)
190+
{
191+
meth.type = typ
192+
}
190193
;
191194

192195

@@ -231,7 +234,7 @@ variable_def [block]
231234

232235

233236
parameter_def [meth, add=True]
234-
: #(PARAMETER_DEF
237+
: #(pd0:PARAMETER_DEF
235238
modifiers[meth]
236239
ptype = type_spec[meth]
237240
ident = identifier[meth]
@@ -321,9 +324,10 @@ returns [ident]
321324

322325

323326
statement_list [block]
324-
: #(SLIST (statement[block])*)
327+
: #(SLIST (s1:statement[block])*)
325328
;
326329

330+
// MARKER
327331

328332
statement [block]
329333
: typ = type_def[block]
@@ -429,6 +433,7 @@ statement [block]
429433
}
430434

431435
| try_block[block]
436+
| ctor_call[block]
432437
| statement_list[block]
433438
| EMPTY_STAT
434439
;
@@ -637,39 +642,60 @@ primary_expr [block]
637642
returns [exp = block.missingValue]
638643
: i0:IDENT {exp = ("%s", i0.getText())}
639644

640-
| {x = ""}
645+
| {
646+
dotexpr = this0 = class0 = class1 = class2 = id0 = id1 = ar0 = typ = None
647+
index = None
648+
}
641649
#(DOT
642-
(x=expr[block]
643-
(a:IDENT
650+
(dotexpr=expr[block]
651+
(id0:IDENT
644652
| index = array_index[block]
645-
| "this"
646-
| a0:"class"
647-
| #("new" k:IDENT el0=expr_list[block] )
653+
| this0:"this"
654+
| class0:"class"
655+
| #("new" id1:IDENT el0=expr_list[block] )
648656
| "super"
649657
)
650-
| #(ARRAY_DECLARATOR type_spec_array[block])
651-
| t = builtin_type[block]("class")?
658+
| #(ar0:ARRAY_DECLARATOR type_spec_array[block])
659+
(class1:"class")?
660+
| typ = builtin_type[block](class2:"class")?
652661
)
653662
)
654663
{
655-
if a:
656-
exp = ("%s.%s", (x, ("%s", a.getText())))
664+
if id0:
665+
exp = ("%s.%s", (dotexpr, ("%s", id0.getText())))
666+
elif ar0:
667+
exp = ("%s", "[]")
668+
if class1:
669+
exp = ("%s.__class__", exp)
670+
elif typ:
671+
exp = ("%s", typ)
672+
elif id1:
673+
el0 = el0 or ""
674+
exp = ("%s(%s)", (("%s", id1.getText()), ("%s", el0)))
675+
}
676+
677+
| index = array_index[block]
678+
{
679+
exp = index
657680
}
658681

659-
| index = array_index[block] {exp = index}
660-
661-
| #(METHOD_CALL pex = primary_expr[block] el44=expr_list[block])
682+
| #(METHOD_CALL pex = primary_expr[block] exl = expr_list[block])
662683
{
663-
if el44:
664-
exp = ("%s(%s)", (pex, el44))
684+
if exl:
685+
exp = ("%s(%s)", (pex, exl))
665686
else:
666687
exp = ("%s()", pex)
667688
}
668689

669-
| call = ctor_call[block] {exp = ("%s", call)}
690+
| ctor_call[block]
670691

671-
| #(TYPECAST type_cast = type_spec[block] cast_exp = expr[block])
672-
{exp = ("%s", cast_exp)}
692+
| #(TYPECAST
693+
type_cast = type_spec[block]
694+
cast_exp = expr[block]
695+
)
696+
{
697+
exp = ("%s", cast_exp)
698+
}
673699

674700
| other_exp = new_expression[block] {exp = ("%s", other_exp)}
675701
| con = constant[block] {exp = ("%s", con)}
@@ -684,10 +710,11 @@ returns [exp = block.missingValue]
684710

685711

686712
ctor_call [block]
687-
returns [seq=()]
688-
: #(cn:CTOR_CALL seq=expr_list[block] )
713+
: #(cn:CTOR_CALL seq=expr_list[block, False] )
689714
{
690-
print "#@#@@@", cn
715+
name = block.parent.name
716+
call = ("super(%s, self).__init__(%s)", (("%s", name), ("%s", seq)))
717+
block.addSource(call)
691718
}
692719

693720
| #(SUPER_CTOR_CALL

0 commit comments

Comments
 (0)
X Tutup