Skip to content

Commit b396816

Browse files
committed
Implemented Visual Error Messages, optimiezed code, calculate the right stack height and corrected the right calculation of the label indices
1 parent 1b405eb commit b396816

File tree

128 files changed

+1608
-374
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+1608
-374
lines changed

ASTPrinter.class

3.26 KB
Binary file not shown.

CodeGenerator.java

Lines changed: 117 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@
1010
*/
1111

1212
public class CodeGenerator extends DepthFirstAdapter {
13-
public String code = "";
14-
public HashMap<String, Integer> symbolTable = new HashMap<String, Integer>();
13+
private String code = "";
14+
private HashMap<String, Integer> symbolTable = new HashMap<String, Integer>();
1515
private HashMap<String, String> typeTable;
1616
private String type = "";
1717
private int countLabels = 0;
18+
private int compareCounter = 0;
19+
private int breakCounter = 0;
20+
private int stackHeight = 1;
1821

1922
public CodeGenerator(HashMap<String, String> symbolTable) {
2023
typeTable = symbolTable;
@@ -24,45 +27,41 @@ public CodeGenerator(HashMap<String, String> symbolTable) {
2427
@Override
2528
public void outAAndExpr(AAndExpr node) {
2629
code += "\tiand\n";
30+
stackHeight--;
2731
type = "boolean";
2832
}
2933
@Override
3034
public void outAOrExpr(AOrExpr node) {
3135
code += "\tior\n";
36+
stackHeight--;
3237
type = "boolean";
3338
}
3439
@Override
3540
public void outAXorExpr(AXorExpr node) {
3641
code += "\tixor\n";
42+
stackHeight--;
3743
type = "boolean";
3844
}
3945
@Override
4046
public void outANotExpr(ANotExpr node) {
4147
code += "\tineg\n";
4248
type = "boolean";
4349
}
44-
@Override
45-
public void outATrueExpr(ATrueExpr node) {
46-
code += "\tbipush 1\n";
47-
type = "boolean";
48-
}
49-
@Override
50-
public void outAFalseExpr(AFalseExpr node) {
51-
code += "\tbipush 0\n";
52-
type = "boolean";
53-
}
54-
5550
// Arithmetic operations
5651
@Override
5752
public void outAPlusExpr(APlusExpr node) {
5853
code += "\tiadd\n";
54+
stackHeight--;
5955
type = "integer";
6056
}
6157
@Override
6258
public void outAMinusExpr(AMinusExpr node) {
6359
code += "\tisub\n";
60+
stackHeight--;
6461
type = "integer";
6562
}
63+
64+
// Arithmetic expressions
6665
@Override // NEED TO TEST THIS!
6766
public void outAUnaryMinusExpr(AUnaryMinusExpr node) {
6867
code += "\tineg\n";
@@ -71,11 +70,13 @@ public void outAUnaryMinusExpr(AUnaryMinusExpr node) {
7170
@Override
7271
public void outAMultExpr(AMultExpr node) {
7372
code += "\timul\n";
73+
stackHeight--;
7474
type = "integer";
7575
}
7676
@Override
7777
public void outADivExpr(ADivExpr node) {
7878
code += "\tidiv\n";
79+
stackHeight--;
7980
type = "integer";
8081
}
8182
@Override
@@ -84,16 +85,14 @@ public void outAModExpr(AModExpr node) {
8485
type = "integer";
8586
}
8687

87-
// Comparisons
88-
89-
// Identifier
88+
// Check identifier
9089
@Override
9190
public void outAIdentifierExpr(AIdentifierExpr node) {
9291
boolean check = true;
9392
String identifier = node.getIdentifier().toString().toLowerCase().replaceAll(" ","");
9493
Node parent = node;
9594
String parentName;
96-
do {
95+
do { // Needed to check, if we are in a declaration-context
9796
parent = parent.parent();
9897
parentName = parent.getClass().getSimpleName().replaceAll(" ","");
9998
if (parentName.equals("ADeclarationExpr")) check = false;
@@ -102,6 +101,7 @@ public void outAIdentifierExpr(AIdentifierExpr node) {
102101

103102
if (check) {
104103
code += "\tiload "+symbolTable.get(identifier)+"\n";
104+
stackHeight++;
105105
type = typeTable.get(identifier);
106106
}
107107
}
@@ -111,47 +111,137 @@ public void outAIdentifierExpr(AIdentifierExpr node) {
111111
public void caseAAssignmentExpr(AAssignmentExpr node) {
112112
node.getExpr().apply(this);
113113
code += "\tistore "+symbolTable.get(node.getIdentifier().toString().toLowerCase().replaceAll(" ",""))+"\n";
114+
stackHeight--;
114115
}
115116
@Override
116117
public void caseANumberExpr(ANumberExpr node) {
117-
code += "\tldc "+node.getNumber().toString().replaceAll(" ","")+"\n";
118+
code += "\tbipush "+node.getNumber().toString().replaceAll(" ","")+"\n";
119+
stackHeight++;
118120
type = "integer";
119121
}
120122
@Override
121123
public void caseATrueExpr(ATrueExpr node) {
122-
code += "\tldc 1\n";
124+
code += "\tbipush 1\n";
125+
stackHeight++;
123126
type = "boolean";
124127
}
125128
@Override
126129
public void caseAFalseExpr(AFalseExpr node) {
127-
code += "\tldc 0\n";
130+
code += "\tbipush 0\n";
131+
stackHeight++;
128132
type = "boolean";
129133
}
130134
@Override
131135
public void caseAPrintExpr(APrintExpr node) {
132136
String intOrBoolean = "I";
133-
134137
code += "\tgetstatic java/lang/System/out Ljava/io/PrintStream;\n";
135138
node.getExpr().apply(this);
136139
if (type.equals("boolean")) intOrBoolean = "Z";
137140
code += "\tinvokevirtual java/io/PrintStream/println("+intOrBoolean+")V\n";
141+
stackHeight++;
138142
}
139143

140144
// While loop
141145
@Override
142146
public void caseAWhileExpr(AWhileExpr node) {
143-
code += "\tLabel"+countLabels+":\n";
147+
breakCounter = countLabels;
148+
int temp = countLabels++;
149+
code += "LabelWhileUp"+temp+":\n";
144150
node.getLeft().apply(this);
145-
code += "\ndone\n";
151+
code += "\tifeq LabelWhileDown"+temp+"\n";
152+
stackHeight--;
153+
node.getRight().apply(this);
154+
code += "\tgoto LabelWhileUp"+temp+"\n";
155+
code += "LabelWhileDown"+temp+":\n";
156+
}
157+
158+
// Break
159+
@Override
160+
public void caseABreakExpr(ABreakExpr node) {
161+
code += "\tgoto LabelWhileDown"+breakCounter+"\n";
162+
}
163+
164+
// If-then Part
165+
@Override
166+
public void caseAIfThenExpr(AIfThenExpr node) {
167+
int temp = countLabels++;
168+
node.getLeft().apply(this);
169+
code += "\tifeq LabelIfDown"+temp+"\n";
170+
stackHeight--;
171+
node.getRight().apply(this);
172+
code += "LabelIfDown"+temp+":\n";
173+
}
174+
175+
// If-then-else Part
176+
@Override
177+
public void caseAIfThenElseExpr(AIfThenElseExpr node) {
178+
int temp = countLabels++;
179+
node.getIf().apply(this);
180+
code += "\tifeq LabelIfElse"+temp+"\n";
181+
stackHeight--;
182+
node.getThen().apply(this);
183+
code += "LabelIfElse"+temp+":\n";
184+
node.getElse().apply(this);
146185
}
147186

148187
// Comparisons
149188
@Override
150189
public void caseAComparisonExpr(AComparisonExpr node) {
151-
String compare = node.getComparison().toString().toLowerCase().replaceAll(" ","");
152-
if (compare.equals("<")) {
153-
System.out.println("bombe");
154-
}
190+
int temp = 0;
191+
node.getLeft().apply(this);
192+
node.getRight().apply(this);
193+
code += "\tisub\n"; // To check with zero
194+
stackHeight--;
195+
temp = compareCounter;
196+
node.getComparison().apply(this); // Add line matching to corresponding symbol
197+
code += "\tbipush 0\n";
198+
stackHeight++;
199+
code += "\tgoto LabelCompEnd"+temp+"\n";
200+
code += "LabelTrue"+temp+":\n";
201+
code += "\tbipush 1\n";
202+
stackHeight++;
203+
code += "LabelCompEnd"+temp+":\n";
204+
}
205+
@Override
206+
public void outAGtExpr(AGtExpr node) {
207+
code += "\tifgt LabelTrue"+(compareCounter++)+"\n";
208+
stackHeight--;
209+
}
210+
@Override
211+
public void outAGeExpr(AGeExpr node) {
212+
code += "\tifge LabelTrue"+(compareCounter++)+"\n";
213+
stackHeight--;
214+
}
215+
@Override
216+
public void outALtExpr(ALtExpr node) {
217+
code += "\tiflt LabelTrue"+(compareCounter++)+"\n";
218+
stackHeight--;
219+
}
220+
@Override
221+
public void outALeExpr(ALeExpr node) {
222+
code += "\tifle LabelTrue"+(compareCounter++)+"\n";
223+
stackHeight--;
224+
}
225+
@Override
226+
public void outANeExpr(ANeExpr node) {
227+
code += "\tifne LabelTrue"+(compareCounter++)+"\n";
228+
stackHeight--;
229+
}
230+
@Override
231+
public void outAEqExpr(AEqExpr node) {
232+
code += "\tifeq LabelTrue"+(compareCounter++)+"\n";
233+
stackHeight--;
234+
}
235+
236+
/********************************* Getter and Setter **************************************/
155237

238+
public HashMap<String, Integer> getSymbolTable() {
239+
return symbolTable;
240+
}
241+
public String getCode() {
242+
return code;
243+
}
244+
public int getStackHeight() {
245+
return stackHeight;
156246
}
157247
}

CodeGeneratorException.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* This file was generated by SableCC (http://www.sablecc.org/). */
2+
3+
import node.*;
4+
5+
@SuppressWarnings("serial")
6+
public class CodeGeneratorException extends Exception {
7+
private Token token;
8+
9+
public CodeGeneratorException(@SuppressWarnings("hiding") Token token, String message) {
10+
super(message);
11+
this.token = token;
12+
}
13+
14+
public Token getToken() {
15+
return this.token;
16+
}
17+
}

Pascal.class

411 Bytes
Binary file not shown.

Pascal.j

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
.bytecode 50.0
2+
.class public Pascal
3+
.super java/lang/Object
4+
.method public <init>()V
5+
.limit stack 1
6+
.limit locals 1
7+
aload_0
8+
invokespecial java/lang/Object/<init>()V
9+
return
10+
.end method
11+
.method public static main([Ljava/lang/String;)V
12+
.limit stack 9
13+
.limit locals 5
14+
bipush 1
15+
istore 2
16+
bipush 1
17+
istore 3
18+
iload 3
19+
istore 2
20+
LabelWhileUp0:
21+
iload 2
22+
bipush 100
23+
isub
24+
iflt LabelTrue0
25+
bipush 0
26+
goto LabelCompEnd0
27+
LabelTrue0:
28+
bipush 1
29+
LabelCompEnd0:
30+
ifeq LabelWhileDown0
31+
getstatic java/lang/System/out Ljava/io/PrintStream;
32+
iload 2
33+
invokevirtual java/io/PrintStream/println(I)V
34+
iload 3
35+
istore 1
36+
iload 2
37+
iload 3
38+
iadd
39+
istore 3
40+
iload 1
41+
istore 2
42+
iload 2
43+
bipush 34
44+
isub
45+
ifgt LabelTrue1
46+
bipush 0
47+
goto LabelCompEnd1
48+
LabelTrue1:
49+
bipush 1
50+
LabelCompEnd1:
51+
ifeq LabelIfElse1
52+
goto LabelWhileDown0
53+
LabelIfElse1:
54+
getstatic java/lang/System/out Ljava/io/PrintStream;
55+
bipush 0
56+
invokevirtual java/io/PrintStream/println(I)V
57+
goto LabelWhileUp0
58+
LabelWhileDown0:
59+
getstatic java/lang/System/out Ljava/io/PrintStream;
60+
bipush 10
61+
invokevirtual java/io/PrintStream/println(I)V
62+
return
63+
.end method

Pascal.pas

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
program fibonacci_test;
2-
var a,c : integer;
3-
var b : boolean;
1+
program fibonacci;;
2+
var a : integer;
3+
var b,D: integer;
4+
var temp : integer;
45
begin
5-
a := 1+4;
6-
c := (42 div 4);
7-
8-
while a < c do
9-
a := a+1;
10-
11-
writeln(a);
12-
end.
13-
14-
15-
{
16-
1 < 2
17-
-1 < 0
18-
}
6+
a := 1;
7+
b := 1;
8+
a := b;
9+
while a < 100 do
10+
begin
11+
writeln(a);
12+
temp := b;
13+
b := a + b;
14+
a := temp ;
15+
if a > 34 then break else writeln(0);
16+
end;
17+
writeln(10)
18+
end.

StupsCompiler.class

2.32 KB
Binary file not shown.

0 commit comments

Comments
 (0)