1010 */
1111
1212public 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 += "\t iand\n " ;
30+ stackHeight --;
2731 type = "boolean" ;
2832 }
2933 @ Override
3034 public void outAOrExpr (AOrExpr node ) {
3135 code += "\t ior\n " ;
36+ stackHeight --;
3237 type = "boolean" ;
3338 }
3439 @ Override
3540 public void outAXorExpr (AXorExpr node ) {
3641 code += "\t ixor\n " ;
42+ stackHeight --;
3743 type = "boolean" ;
3844 }
3945 @ Override
4046 public void outANotExpr (ANotExpr node ) {
4147 code += "\t ineg\n " ;
4248 type = "boolean" ;
4349 }
44- @ Override
45- public void outATrueExpr (ATrueExpr node ) {
46- code += "\t bipush 1\n " ;
47- type = "boolean" ;
48- }
49- @ Override
50- public void outAFalseExpr (AFalseExpr node ) {
51- code += "\t bipush 0\n " ;
52- type = "boolean" ;
53- }
54-
5550 // Arithmetic operations
5651 @ Override
5752 public void outAPlusExpr (APlusExpr node ) {
5853 code += "\t iadd\n " ;
54+ stackHeight --;
5955 type = "integer" ;
6056 }
6157 @ Override
6258 public void outAMinusExpr (AMinusExpr node ) {
6359 code += "\t isub\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 += "\t ineg\n " ;
@@ -71,11 +70,13 @@ public void outAUnaryMinusExpr(AUnaryMinusExpr node) {
7170 @ Override
7271 public void outAMultExpr (AMultExpr node ) {
7372 code += "\t imul\n " ;
73+ stackHeight --;
7474 type = "integer" ;
7575 }
7676 @ Override
7777 public void outADivExpr (ADivExpr node ) {
7878 code += "\t idiv\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 += "\t iload " +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 += "\t istore " +symbolTable .get (node .getIdentifier ().toString ().toLowerCase ().replaceAll (" " ,"" ))+"\n " ;
114+ stackHeight --;
114115 }
115116 @ Override
116117 public void caseANumberExpr (ANumberExpr node ) {
117- code += "\t ldc " +node .getNumber ().toString ().replaceAll (" " ,"" )+"\n " ;
118+ code += "\t bipush " +node .getNumber ().toString ().replaceAll (" " ,"" )+"\n " ;
119+ stackHeight ++;
118120 type = "integer" ;
119121 }
120122 @ Override
121123 public void caseATrueExpr (ATrueExpr node ) {
122- code += "\t ldc 1\n " ;
124+ code += "\t bipush 1\n " ;
125+ stackHeight ++;
123126 type = "boolean" ;
124127 }
125128 @ Override
126129 public void caseAFalseExpr (AFalseExpr node ) {
127- code += "\t ldc 0\n " ;
130+ code += "\t bipush 0\n " ;
131+ stackHeight ++;
128132 type = "boolean" ;
129133 }
130134 @ Override
131135 public void caseAPrintExpr (APrintExpr node ) {
132136 String intOrBoolean = "I" ;
133-
134137 code += "\t getstatic java/lang/System/out Ljava/io/PrintStream;\n " ;
135138 node .getExpr ().apply (this );
136139 if (type .equals ("boolean" )) intOrBoolean = "Z" ;
137140 code += "\t invokevirtual java/io/PrintStream/println(" +intOrBoolean +")V\n " ;
141+ stackHeight ++;
138142 }
139143
140144 // While loop
141145 @ Override
142146 public void caseAWhileExpr (AWhileExpr node ) {
143- code += "\t Label" +countLabels +":\n " ;
147+ breakCounter = countLabels ;
148+ int temp = countLabels ++;
149+ code += "LabelWhileUp" +temp +":\n " ;
144150 node .getLeft ().apply (this );
145- code += "\n done\n " ;
151+ code += "\t ifeq LabelWhileDown" +temp +"\n " ;
152+ stackHeight --;
153+ node .getRight ().apply (this );
154+ code += "\t goto LabelWhileUp" +temp +"\n " ;
155+ code += "LabelWhileDown" +temp +":\n " ;
156+ }
157+
158+ // Break
159+ @ Override
160+ public void caseABreakExpr (ABreakExpr node ) {
161+ code += "\t goto 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 += "\t ifeq 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 += "\t ifeq 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 += "\t isub\n " ; // To check with zero
194+ stackHeight --;
195+ temp = compareCounter ;
196+ node .getComparison ().apply (this ); // Add line matching to corresponding symbol
197+ code += "\t bipush 0\n " ;
198+ stackHeight ++;
199+ code += "\t goto LabelCompEnd" +temp +"\n " ;
200+ code += "LabelTrue" +temp +":\n " ;
201+ code += "\t bipush 1\n " ;
202+ stackHeight ++;
203+ code += "LabelCompEnd" +temp +":\n " ;
204+ }
205+ @ Override
206+ public void outAGtExpr (AGtExpr node ) {
207+ code += "\t ifgt LabelTrue" +(compareCounter ++)+"\n " ;
208+ stackHeight --;
209+ }
210+ @ Override
211+ public void outAGeExpr (AGeExpr node ) {
212+ code += "\t ifge LabelTrue" +(compareCounter ++)+"\n " ;
213+ stackHeight --;
214+ }
215+ @ Override
216+ public void outALtExpr (ALtExpr node ) {
217+ code += "\t iflt LabelTrue" +(compareCounter ++)+"\n " ;
218+ stackHeight --;
219+ }
220+ @ Override
221+ public void outALeExpr (ALeExpr node ) {
222+ code += "\t ifle LabelTrue" +(compareCounter ++)+"\n " ;
223+ stackHeight --;
224+ }
225+ @ Override
226+ public void outANeExpr (ANeExpr node ) {
227+ code += "\t ifne LabelTrue" +(compareCounter ++)+"\n " ;
228+ stackHeight --;
229+ }
230+ @ Override
231+ public void outAEqExpr (AEqExpr node ) {
232+ code += "\t ifeq 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}
0 commit comments