Skip to content

Commit 81f93b8

Browse files
committed
that's enough, i dont work on this shit anymore
1 parent 972ef4c commit 81f93b8

File tree

5 files changed

+135
-21
lines changed

5 files changed

+135
-21
lines changed

MinijavaPrintListener.py

Lines changed: 82 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ class MiniJavaPrintListener(MinijavaListener):
2121
def __init__(self, symbolTable):
2222
super().__init__()
2323
self.currentClass = ""
24-
self.currentMethodType = ""
24+
self.currentMethod = ""
2525
self.lastExpressionType = ""
2626
self.codeStore = {}
2727
self.code = ""
2828
self.isInitialized = False
2929

3030
self.symbolTable = symbolTable
31+
self.lables = []
32+
self.lableCounter = 0
3133

3234
self.block_number = 0
3335
self.variables = {}
@@ -40,17 +42,49 @@ def get_bytecode(self):
4042
def get_representation(s_type):
4143
if s_type == 'int':
4244
return 'I'
43-
if s_type == 'boolean':
45+
elif s_type == 'boolean':
4446
return 'Z'
47+
else:
48+
print("type UNKNOWN", s_type)
49+
50+
def get_new_lable(self):
51+
lable = 'LABLE_' + str(self.lableCounter)
52+
self.lableCounter = self.lableCounter + 1
53+
self.lables.append(lable)
54+
return lable
55+
56+
def get_last_lable(self):
57+
return self.lables[-1]
58+
59+
def remove_last_lable(self):
60+
self.lables = self.lables[:-1]
4561

4662
@staticmethod
4763
def get_return_by_type(s_type):
4864
#todo implement array
4965
if s_type == 'int':
5066
return 'ireturn'
51-
if s_type == 'boolean':
67+
elif s_type == 'boolean':
5268
return 'ireturn'
5369

70+
71+
def get_method_type(self, method_name):
72+
prop = self.symbolTable[self.currentClass][method_name]
73+
return_type = prop['return_type']
74+
return return_type
75+
76+
def get_method_inp_type(self, method_name, lookup_class=None):
77+
if lookup_class is None:
78+
lookup_class = self.currentClass
79+
print(lookup_class, "PPPPPPPPP")
80+
print(self.symbolTable[lookup_class])
81+
prop = self.symbolTable[lookup_class][method_name]
82+
inputs = prop["inputs"]
83+
str_types = ""
84+
for inp in inputs:
85+
str_types += self.get_representation(inp[0]) + ','
86+
return str_types[:-1]
87+
5488
def enterMainClass(self, ctx:MinijavaParser.MainClassContext):
5589
self.currentClass = ctx.getChild(1).getText()
5690
self.code += '.class public %s' % self.currentClass + '\n'
@@ -96,32 +130,39 @@ def enterMethodDeclaration(self, ctx:MinijavaParser.MethodDeclarationContext):
96130
self.code += 'invokenonvirtual java/lang/Object/<init>()V' + '\n'
97131
self.code += 'return' + '\n'
98132
self.code += '.end method' + '\n'
99-
self.currentMethodType = ctx.getChild(1).getText()
100-
method_name = ctx.getChild(2).getText()
101-
102-
self.code += '.method public %s' % method_name
133+
method_type = ctx.getChild(1).getText()
134+
self.currentMethod = ctx.getChild(2).getText()
135+
self.code += '.method public %s' % self.currentMethod
103136

104137
if ctx.getChild(4).getText() == ')':
105-
return_type_representation = self.get_representation(self.currentMethodType)
138+
return_type_representation = self.get_representation(method_type)
106139
self.code += '()%s' % return_type_representation + '\n'
107140

108141
def exitMethodDeclaration(self, ctx: MinijavaParser.MethodDeclarationContext):
109142
self.code += '.end method' + '\n'
110143

111144
def enterParameterList(self, ctx:MinijavaParser.ParameterListContext):
112145
self.code += '('
146+
self.code += self.get_method_inp_type(self.currentMethod)
113147

114148
def exitParameterList(self, ctx: MinijavaParser.ParameterListContext):
115-
return_type_representation = self.get_representation(self.currentMethodType)
149+
method_type = self.get_method_type(self.currentMethod)
150+
return_type_representation = self.get_representation(method_type)
116151
self.code += ')%s' % return_type_representation + '\n'
117152

118153
def enterMethodBody(self, ctx:MinijavaParser.MethodBodyContext):
119154
self.code += '.limit stack 10000' + '\n'
155+
self.code += '.limit locals 1000' + '\n'
120156
#todo implement .limit locals
121157
self.code += ''
122158

123159
def exitMethodBody(self, ctx:MinijavaParser.MethodBodyContext):
124-
self.code += self.get_return_by_type(self.currentMethodType) + '\n'
160+
method_type = self.get_method_type(self.currentMethod)
161+
self.code += self.get_return_by_type(method_type) + '\n'
162+
163+
def exitNotExpression(self, ctx:MinijavaParser.NotExpressionContext):
164+
self.code += 'ldc 1' + '\n'
165+
self.code += 'ixor' + '\n'
125166

126167
def enterObjectInstantiationExpression(self, ctx:MinijavaParser.ObjectInstantiationExpressionContext):
127168
object_class_name = ctx.getChild(1).getText()
@@ -136,20 +177,19 @@ def exitObjectInstantiationExpression(self, ctx:MinijavaParser.ObjectInstantiati
136177
def exitMethodCallExpression(self, ctx: MinijavaParser.MethodCallExpressionContext):
137178
#todo implement method inputs
138179
method_name = ctx.getChild(2).getText()
139-
print("gi")
140-
print(self.lastExpressionType)
141-
print(method_name)
142180
class_properties = self.symbolTable[self.lastExpressionType][method_name]
181+
input_types = self.get_method_inp_type(method_name, self.lastExpressionType)
143182
return_type_representation = self.get_representation(class_properties["return_type"])
144-
self.code += 'invokevirtual %s/%s(%s)%s' % (self.lastExpressionType, method_name, '', return_type_representation) + '\n'
183+
self.code += 'invokevirtual %s/%s(%s)%s' % (self.lastExpressionType, method_name, input_types, return_type_representation) + '\n'
145184

146185
def enterLocalDeclaration(self, ctx:MinijavaParser.LocalDeclarationContext):
147186
#todo
148-
var_name = ctx.getChild(1).getText()
187+
var_name = ctx.getChild(0).getChild(1).getText()
149188
var_properties= self.symbolTable[self.currentClass][var_name]
189+
var_id = var_properties['id']
150190
if var_properties['return_type'] == 'int':
151191
self.code += 'bipush 0' + '\n'
152-
self.code += 'istore' + '\n'
192+
self.code += 'istore %s' % var_id + '\n'
153193

154194
def enterVariableAssignmentStatement(self, ctx:MinijavaParser.VariableAssignmentStatementContext):
155195
name = ctx.getChild(0).getText()
@@ -158,6 +198,10 @@ def enterVariableAssignmentStatement(self, ctx:MinijavaParser.VariableAssignment
158198
if type == 'field':
159199
self.code += 'aload 0 ; push this' + '\n'
160200

201+
def enterIfElseStatement(self, ctx:MinijavaParser.IfElseStatementContext):
202+
lable = self.get_new_lable()
203+
self.code += ''
204+
161205
#todo var type
162206
def exitVariableAssignmentStatement(self, ctx:MinijavaParser.VariableAssignmentStatementContext):
163207
name = ctx.getChild(0).getText()
@@ -187,6 +231,23 @@ def exitBasicMathExpression(self, ctx:MinijavaParser.BasicMathExpressionContext)
187231
elif type == '-':
188232
self.code += "isub" + '\n'
189233

234+
def exitLtExpression(self, ctx:MinijavaParser.LtExpressionContext):
235+
l1 = self.get_new_lable()
236+
l2 = self.get_new_lable()
237+
print()
238+
print("enterLtExpression")
239+
self.code += 'isub' + '\n'
240+
self.code += 'ifle %s' % l1 + '\n'
241+
self.code += 'ldc 0' + '\n'
242+
self.code += 'goto %s' % l2 + '\n'
243+
self.code += '%s:' % l1 + '\n'
244+
self.code += 'ldc 1' + '\n'
245+
self.code += '%s:' % l2 + '\n'
246+
247+
self.remove_last_lable()
248+
self.remove_last_lable()
249+
250+
190251
def enterIntLitExpression(self, ctx:MinijavaParser.IntLitExpressionContext):
191252
literal = ctx.getChild(0).getText()
192253
print("enterIntLitExpression %s" % literal)
@@ -201,6 +262,11 @@ def enterIdentifierExpression(self, ctx:MinijavaParser.IdentifierExpressionConte
201262
representation = self.get_representation(return_type)
202263
self.code += 'aload 0 ; push this' + '\n'
203264
self.code += 'getfield %s/%s %s' % (self.currentClass, name, representation) + '\n'
265+
elif type == 'var':
266+
var_id = properties['id'] - 1
267+
return_type = properties['return_type']
268+
upcode = self.get_representation(return_type).lower() + 'load'
269+
self.code += '%s %d' % (upcode, var_id) + '\n'
204270

205271
def enterPrintStatement(self, ctx: MinijavaParser.PrintStatementContext):
206272
self.code += "getstatic java/lang/System/out Ljava/io/PrintStream;" + '\n'
@@ -209,10 +275,6 @@ def exitPrintStatement(self, ctx:MinijavaParser.PrintStatementContext):
209275
self.code += "invokevirtual java/io/PrintStream/println(I)V" + '\n'
210276

211277

212-
def enterLtExpression(self, ctx:MinijavaParser.LtExpressionContext):
213-
print()
214-
print("enterLtExpression")
215-
self.code += ""
216278

217279
def enterAndExpression(self, ctx:MinijavaParser.AndExpressionContext):
218280
print()

MinijavaSymbolVisitor.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ class MinijavaSymbolVisitor(MinijavaVisitor):
55
def __init__(self):
66
super().__init__()
77
self.currentClass = ""
8+
self.currentMethod = ""
89
self.symbolTable = {}
910
self.variables = {}
1011

@@ -40,10 +41,24 @@ def visitVarDeclaration(self, ctx: MinijavaParser.VarDeclarationContext):
4041
def visitMethodDeclaration(self, ctx:MinijavaParser.MethodDeclarationContext):
4142
method_type = ctx.getChild(1).getText()
4243
method_name = ctx.getChild(2).getText()
43-
self.symbolTable[self.currentClass][method_name] = {type: "method", "return_type": method_type, "inputes": []}
44+
self.currentMethod = method_name
45+
self.symbolTable[self.currentClass][method_name] = {"type": "method", "return_type": method_type, "inputs": []}
4446

4547
return self.visitChildren(ctx)
4648

49+
def visitParameterList(self, ctx:MinijavaParser.ParameterListContext):
50+
for child in ctx.getChildren():
51+
if child.getText() != ',':
52+
param_type = child.getChild(0).getText()
53+
param_name = child.getChild(1).getText()
54+
param_prop = (param_type, param_name)
55+
print(param_prop, "ZZZZZZ")
56+
self.symbolTable[self.currentClass][self.currentMethod]["inputs"].append(param_prop)
57+
var_type = param_type
58+
var_name = param_name
59+
var_id = self.new_variable(var_name)
60+
self.symbolTable[self.currentClass][var_name] = {"type": "var", "return_type": var_type, "id": var_id}
61+
4762
# todo implement input param
4863
def visitParameter(self, ctx: MinijavaParser.ParameterContext):
4964
return self.visitChildren(ctx)

sample/class4.minijava

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class class4{
2+
public static void main(String[] a) {
3+
System.out.println(new Dynamic().twice(7));
4+
}
5+
}
6+
7+
class Dynamic{
8+
9+
int fixula;
10+
11+
public int twice(int b){
12+
fixula = 'string';
13+
return fixula * b;
14+
}
15+
}

sample/factorial.minijava

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class factorial{
2+
public static void main(String[] a) {
3+
System.out.println(new Fac().ComputeFac(10));
4+
}
5+
}
6+
7+
class Fac{
8+
9+
public int ComputeFac(int num){
10+
int num_aux;
11+
if (!num<1)
12+
num_aux = 1;
13+
else
14+
num_aux = num*(this.ComputeFac(num-1));
15+
return num_aux;
16+
}
17+
}

sample/math.minijava

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Math{
2+
public static void main(String[] a){
3+
System.out.println(3+4*5*6+7-2*7);
4+
}
5+
}

0 commit comments

Comments
 (0)