Skip to content

Commit 1b405eb

Browse files
committed
CodeGen
1 parent 197f006 commit 1b405eb

File tree

13 files changed

+295
-101
lines changed

13 files changed

+295
-101
lines changed

CodeGenerator.java

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
import analysis.DepthFirstAdapter;
2+
import node.*;
3+
4+
import java.util.HashMap;
5+
6+
/**
7+
* Need to test:
8+
* - UnaryMinus
9+
* - UnaryPlus not necessary?
10+
*/
11+
12+
public class CodeGenerator extends DepthFirstAdapter {
13+
public String code = "";
14+
public HashMap<String, Integer> symbolTable = new HashMap<String, Integer>();
15+
private HashMap<String, String> typeTable;
16+
private String type = "";
17+
private int countLabels = 0;
18+
19+
public CodeGenerator(HashMap<String, String> symbolTable) {
20+
typeTable = symbolTable;
21+
}
22+
23+
// Boolean expressions
24+
@Override
25+
public void outAAndExpr(AAndExpr node) {
26+
code += "\tiand\n";
27+
type = "boolean";
28+
}
29+
@Override
30+
public void outAOrExpr(AOrExpr node) {
31+
code += "\tior\n";
32+
type = "boolean";
33+
}
34+
@Override
35+
public void outAXorExpr(AXorExpr node) {
36+
code += "\tixor\n";
37+
type = "boolean";
38+
}
39+
@Override
40+
public void outANotExpr(ANotExpr node) {
41+
code += "\tineg\n";
42+
type = "boolean";
43+
}
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+
55+
// Arithmetic operations
56+
@Override
57+
public void outAPlusExpr(APlusExpr node) {
58+
code += "\tiadd\n";
59+
type = "integer";
60+
}
61+
@Override
62+
public void outAMinusExpr(AMinusExpr node) {
63+
code += "\tisub\n";
64+
type = "integer";
65+
}
66+
@Override // NEED TO TEST THIS!
67+
public void outAUnaryMinusExpr(AUnaryMinusExpr node) {
68+
code += "\tineg\n";
69+
type = "integer";
70+
}
71+
@Override
72+
public void outAMultExpr(AMultExpr node) {
73+
code += "\timul\n";
74+
type = "integer";
75+
}
76+
@Override
77+
public void outADivExpr(ADivExpr node) {
78+
code += "\tidiv\n";
79+
type = "integer";
80+
}
81+
@Override
82+
public void outAModExpr(AModExpr node) {
83+
code += "\tirem\n";
84+
type = "integer";
85+
}
86+
87+
// Comparisons
88+
89+
// Identifier
90+
@Override
91+
public void outAIdentifierExpr(AIdentifierExpr node) {
92+
boolean check = true;
93+
String identifier = node.getIdentifier().toString().toLowerCase().replaceAll(" ","");
94+
Node parent = node;
95+
String parentName;
96+
do {
97+
parent = parent.parent();
98+
parentName = parent.getClass().getSimpleName().replaceAll(" ","");
99+
if (parentName.equals("ADeclarationExpr")) check = false;
100+
101+
} while (!parentName.equals("AStartExpr"));
102+
103+
if (check) {
104+
code += "\tiload "+symbolTable.get(identifier)+"\n";
105+
type = typeTable.get(identifier);
106+
}
107+
}
108+
109+
// Check assignment and save expression in the variable
110+
@Override
111+
public void caseAAssignmentExpr(AAssignmentExpr node) {
112+
node.getExpr().apply(this);
113+
code += "\tistore "+symbolTable.get(node.getIdentifier().toString().toLowerCase().replaceAll(" ",""))+"\n";
114+
}
115+
@Override
116+
public void caseANumberExpr(ANumberExpr node) {
117+
code += "\tldc "+node.getNumber().toString().replaceAll(" ","")+"\n";
118+
type = "integer";
119+
}
120+
@Override
121+
public void caseATrueExpr(ATrueExpr node) {
122+
code += "\tldc 1\n";
123+
type = "boolean";
124+
}
125+
@Override
126+
public void caseAFalseExpr(AFalseExpr node) {
127+
code += "\tldc 0\n";
128+
type = "boolean";
129+
}
130+
@Override
131+
public void caseAPrintExpr(APrintExpr node) {
132+
String intOrBoolean = "I";
133+
134+
code += "\tgetstatic java/lang/System/out Ljava/io/PrintStream;\n";
135+
node.getExpr().apply(this);
136+
if (type.equals("boolean")) intOrBoolean = "Z";
137+
code += "\tinvokevirtual java/io/PrintStream/println("+intOrBoolean+")V\n";
138+
}
139+
140+
// While loop
141+
@Override
142+
public void caseAWhileExpr(AWhileExpr node) {
143+
code += "\tLabel"+countLabels+":\n";
144+
node.getLeft().apply(this);
145+
code += "\ndone\n";
146+
}
147+
148+
// Comparisons
149+
@Override
150+
public void caseAComparisonExpr(AComparisonExpr node) {
151+
String compare = node.getComparison().toString().toLowerCase().replaceAll(" ","");
152+
if (compare.equals("<")) {
153+
System.out.println("bombe");
154+
}
155+
156+
}
157+
}

Pascal.pas

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
program fibonacci_test;
2-
var a : integer;
3-
var b : integer;
4-
var d : integer;
5-
var c : boolean;
2+
var a,c : integer;
3+
var b : boolean;
64
begin
7-
a := 1;
8-
b := 1;
9-
c := true;
10-
a := a div b;
11-
c := 1 < 2;
5+
a := 1+4;
6+
c := (42 div 4);
127

13-
end.
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+
}

StupsCompiler.java

Lines changed: 46 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,8 @@ public static void main(String[] args) throws LexerException, IOException, Parse
3838
zeile = br.readLine();
3939
}
4040
br.close();
41+
parse(input, args[1].substring(0,args[1].lastIndexOf('.')));
4142

42-
// System.out.println("# Input: \n"+input);
43-
44-
parse(input);
4543
} else if (args[0].equals("-liveness")) {
4644
// Write something about liveness...
4745
} else {
@@ -56,24 +54,56 @@ public static void main(String[] args) throws LexerException, IOException, Parse
5654
}
5755
}
5856

59-
private static void parse(String input) throws ParserException, LexerException, IOException {
60-
StringReader reader = new StringReader(input);
57+
private static void parse(String input, String fileName) throws ParserException, LexerException, IOException {
58+
String output;
59+
StringReader reader = new StringReader(input);
6160
PushbackReader r = new PushbackReader(reader, 100);
62-
// System.out.println("# Start Lexing and Parsing...");
6361
Lexer l = new Lexer(r);
6462
Parser parser = new Parser(l);
6563
Start start = parser.parse();
66-
// System.out.println("# Lexer and Parser: Finished with no errors.\n");
67-
68-
// System.out.println("# Start printing the AST...");
69-
// ASTPrinter printer = new ASTPrinter();
70-
// start.apply(printer);
71-
// System.out.println("# AST: Finished with no errors.\n");
64+
ASTPrinter printer = new ASTPrinter();
65+
start.apply(printer);
7266

73-
// System.out.println("# Start TypeChecker...");
7467
TypeChecker interpreter = new TypeChecker();
7568
start.apply(interpreter);
76-
// System.out.println("# TypeChecker: Finished with no errors.\n");
77-
// System.out.println("################################# Success! #################################\n");
78-
}
69+
70+
CodeGenerator visitor = new CodeGenerator(interpreter.symbolTable);
71+
copySymbolTable(interpreter, visitor); // To get all the identifiers copied with an index to CodeGen
72+
start.apply(visitor);
73+
74+
output = createOutput(visitor);
75+
System.out.println(output);
76+
77+
Writer wout = new BufferedWriter(
78+
new OutputStreamWriter(
79+
new FileOutputStream(fileName+".j"),"UTF8"));
80+
wout.append(output);
81+
wout.close();
82+
}
83+
84+
private static String createOutput(CodeGenerator visitor) {
85+
int size = visitor.symbolTable.size()+1;
86+
return ".bytecode 50.0\n"+
87+
".class public Out\n"+
88+
".super java/lang/Object\n"+
89+
".method public <init>()V\n"+
90+
"\t.limit stack 1\n"+
91+
"\t.limit locals 1\n"+
92+
"\taload_0\n"+
93+
"\tinvokespecial java/lang/Object/<init>()V\n"+
94+
"\treturn\n"+
95+
".end method\n"+
96+
"\t.method public static main([Ljava/lang/String;)V\n"+
97+
"\t.limit stack 100\n"+ // NEEDS TO BE UPGRADED
98+
"\t.limit locals "+size+"\n"+
99+
visitor.code+
100+
"\treturn\n"+
101+
".end method\n";
102+
}
103+
104+
private static void copySymbolTable(TypeChecker interpreter, CodeGenerator visitor) {
105+
int i = interpreter.symbolTable.size();
106+
for (String id : interpreter.symbolTable.keySet())
107+
visitor.symbolTable.put(id, i--);
108+
}
79109
}

TypeChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
public class TypeChecker extends DepthFirstAdapter {
1515

16-
private HashMap<String, String> symbolTable = new HashMap<String, String>();
16+
public HashMap<String, String> symbolTable = new HashMap<String, String>();
1717
private String result;
1818

1919
/**

0 commit comments

Comments
 (0)