Skip to content

Commit 20474f7

Browse files
committed
simple1 compiled correctly
1 parent b93d4e3 commit 20474f7

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

.gitignore

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,8 @@ dmypy.json
115115
.pyre/
116116

117117
# ide files
118-
.idea
118+
.idea
119+
120+
# antlr automate created files
121+
grammer/*
122+
!grammer/*.g4

MinijavaPrintListener.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22
from grammer.MinijavaParser import MinijavaParser
33

44

5+
def keyboard(banner=None):
6+
import code, sys
7+
8+
# use exception trick to pick up the current frame
9+
try:
10+
raise None
11+
except:
12+
frame = sys.exc_info()[2].tb_frame.f_back
13+
14+
# evaluate commands in current namespace
15+
namespace = frame.f_globals.copy()
16+
namespace.update(frame.f_locals)
17+
18+
code.interact(banner=banner, local=namespace)
19+
520
class MiniJavaPrintListener(MinijavaListener):
621
def __init__(self, name):
722
super(MinijavaListener, self).__init__()
@@ -43,6 +58,12 @@ def exitSubExpression(self, ctx:MinijavaParser.SubExpressionContext):
4358
print("enterSubExpression")
4459
self.code += "isub"
4560

61+
def enterIntLitExpression(self, ctx:MinijavaParser.IntLitExpressionContext):
62+
print()
63+
print("enterIntLitExpression")
64+
literal = ctx.getChild(0).getText()
65+
self.code += "ldc %s" % literal + '\n'
66+
4667

4768
def enterVarDeclaration(self, ctx:MinijavaParser.VarDeclarationContext):
4869
print()
@@ -61,3 +82,10 @@ def enterPowExpression(self, ctx:MinijavaParser.PowExpressionContext):
6182
print()
6283
print("enterPowExpression")
6384
self.code += ""
85+
86+
87+
def enterPrintStatement(self, ctx: MinijavaParser.PrintStatementContext):
88+
self.code += "getstatic java/lang/System/out Ljava/io/PrintStream;" + '\n'
89+
90+
def exitPrintStatement(self, ctx:MinijavaParser.PrintStatementContext):
91+
self.code += "invokevirtual java/io/PrintStream/println(I)V" + '\n'

0 commit comments

Comments
 (0)