Skip to content

Commit 6c1d292

Browse files
committed
Start to generate VM code in let simple expression, example: let x = 1;
1 parent 0ce0904 commit 6c1d292

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed
Binary file not shown.
Binary file not shown.
-162 Bytes
Binary file not shown.
Binary file not shown.

src/codeGeneration/CodeGenerator.java

+40-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package codeGeneration;
22

3-
import backend.IDLE;
43
import generated.MonkeyParser;
54
import generated.MonkeyParserBaseVisitor;
6-
import utils.TYPE;
5+
76

87
import java.util.ArrayList;
98

@@ -13,11 +12,21 @@ public class CodeGenerator extends MonkeyParserBaseVisitor<Object> {
1312
private ArrayList<String> code;
1413
public int level;
1514

15+
// Aux
16+
private MonkeyParser.LetStatementASTContext ctxLet;
17+
private boolean isLet;
18+
private boolean isReturn;
19+
private boolean isCall;
20+
21+
1622
//Constructor
1723
public CodeGenerator() {
1824
this.index=0;
1925
this.code= new ArrayList<>();
2026
this.level = -1;
27+
this.isLet = false;
28+
this.isReturn = false;
29+
this.isCall = false;
2130
}
2231

2332
//Method to generate Monkey Virtual Machine code
@@ -45,7 +54,9 @@ public Object visitProgramAST(MonkeyParser.ProgramASTContext ctx) {
4554
@Override
4655
public Object visitStatement_LetAST(MonkeyParser.Statement_LetASTContext ctx) {
4756
System.out.println("***********************************************************************************");
48-
System.out.println("LET : ");
57+
System.out.println("LET : " + ctx.getText());
58+
makeAllFalse();
59+
isLet = true;
4960
visit(ctx.letStatement());
5061
return null;
5162
}
@@ -54,6 +65,8 @@ public Object visitStatement_LetAST(MonkeyParser.Statement_LetASTContext ctx) {
5465
public Object visitStatement_returnAST(MonkeyParser.Statement_returnASTContext ctx) {
5566
System.out.println("***********************************************************************************");
5667
System.out.println("RETURN : ");
68+
makeAllFalse();
69+
isReturn = true;
5770
visit(ctx.returnStatement());
5871
return null;
5972
}
@@ -62,6 +75,8 @@ public Object visitStatement_returnAST(MonkeyParser.Statement_returnASTContext c
6275
public Object visitCallExpressionStatementAST(MonkeyParser.CallExpressionStatementASTContext ctx) {
6376
System.out.println("***********************************************************************************");
6477
System.out.println("CALL : " + ctx.getText());
78+
makeAllFalse();
79+
isCall = true;
6580
visit(ctx.expressionStatement());
6681
return null;
6782
}
@@ -70,9 +85,11 @@ public Object visitCallExpressionStatementAST(MonkeyParser.CallExpressionStateme
7085
public Object visitLetStatementAST(MonkeyParser.LetStatementASTContext ctx) {
7186
level ++;
7287

88+
//save the ctx as aux
89+
ctxLet = ctx;
7390
//TODO: get best order if this visit at the end, but the problem is Virtual Machine
7491
//visit(ctx.expression());
75-
//System.out.println("***********************************************************************************");
92+
7693
// If is function
7794
if(ctx.getText().split("\\=")[1].startsWith("fn(")){
7895
if(ctx.IDENT().getText().toLowerCase().equals("main") && level == 1){
@@ -94,6 +111,12 @@ else if(ctx.getText().split("\\=")[1].startsWith("{")){
94111
// if is variable
95112
else{
96113
System.out.println("IDENT: " +ctx.IDENT() +" Level: " + level +" => var");
114+
if(level == 1){
115+
this.generate(this.index,"PUSH_GLOBAL_I",ctx.IDENT().getText());
116+
}else{
117+
this.generate(this.index,"PUSH_LOCAL_I",ctx.IDENT().getText());
118+
}
119+
97120
}
98121

99122
visit(ctx.expression());
@@ -136,7 +159,6 @@ public Object visitAdditionExpressionAST(MonkeyParser.AdditionExpressionASTConte
136159
if(ctx.multiplicationExpression() != null){
137160
visit(ctx.multiplicationExpression());
138161
}
139-
140162
return null;
141163
}
142164

@@ -197,6 +219,12 @@ public Object visitCallExpressionAST(MonkeyParser.CallExpressionASTContext ctx)
197219

198220
@Override
199221
public Object visitPrimitiveExpression_numberAST(MonkeyParser.PrimitiveExpression_numberASTContext ctx) {
222+
223+
if(isLet){
224+
this.generate(this.index,"LOAD_CONST", ctx.INTEGER());
225+
this.generate(this.index,"STORE_FAST", ctxLet.IDENT().getText());
226+
}
227+
200228
System.out.println("INT: " + ctx.INTEGER());
201229
return null;
202230
}
@@ -396,7 +424,6 @@ public Object visitBlockStatementAST(MonkeyParser.BlockStatementASTContext ctx)
396424
return null;
397425
}
398426

399-
400427
//Method To String
401428
@Override
402429
public String toString() {
@@ -406,4 +433,11 @@ public String toString() {
406433
}
407434
return data;
408435
}
436+
437+
public void makeAllFalse(){
438+
isLet = false;
439+
isCall = false;
440+
isReturn = false;
441+
}
442+
409443
}

0 commit comments

Comments
 (0)