Skip to content

Commit fec483a

Browse files
committed
add labels
1 parent 95471ad commit fec483a

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

java_class.class

-259 Bytes
Binary file not shown.

java_parser.y

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ while:
146146
backpatch(*($4.trueList), $7.nextInstructionIndex);
147147
$$.nextList = new unordered_set<int>();
148148
*($$.nextList) = *($4.falseList);
149-
appendToCode("goto _" + $2.nextInstructionIndex);
149+
appendToCode("goto Label_" + to_string($2.nextInstructionIndex));
150150
}
151151
;
152152

@@ -241,7 +241,7 @@ boolean_expression:
241241
($$.trueList)->insert(static_cast<int>(outputCode.size()));
242242
$$.falseList = new unordered_set<int>();
243243
($$.falseList)->insert(static_cast<int>(outputCode.size()+1));
244-
appendToCode(getOperationCode($2)+ " ");
244+
appendToCode(getOperationCode($2)+ " _");
245245
appendToCode("goto _");
246246
}
247247
;

semantic_actions_utils.h

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,44 +54,46 @@ namespace semantic_actions_util {
5454
}
5555

5656
void appendToCode(string code) {
57-
outputCode.push_back(code);
57+
outputCode.push_back("Label_" + std::to_string(nextInstructionIndex) + ":\n" + code);
5858
nextInstructionIndex++;
5959
}
6060

6161
void backpatch(unordered_set<int> list, int instruction_index) {
6262
for (auto index : list) {
6363
outputCode[index] = outputCode[index].substr(0, outputCode[index].size()-1);
64-
outputCode[index] += to_string(instruction_index);
64+
outputCode[index] += "Label_" + to_string(instruction_index);
6565
}
6666
}
6767

6868
void defineVariable(string name, int varType) {
6969
declareVariable(name, varType);
7070
if (varType == INT_TYPE) {
71-
appendToCode("iconst_0\nistore_" + to_string(currentVariableIndex -1));
71+
appendToCode("iconst_0");
72+
appendToCode("istore_" + to_string(currentVariableIndex -1));
7273
} else if (varType == FLOAT_TYPE) {
73-
appendToCode("fconst_0\nfstore_" + to_string(currentVariableIndex -1));
74+
appendToCode("fconst_0");
75+
appendToCode("fstore_" + to_string(currentVariableIndex -1));
7476
}
7577

7678
}
7779

7880
void generateHeader() {
7981
//TO-DO get file name
8082
//appendToCode(".source " + outfileName);
81-
appendToCode(".class public java_class\n.super java/lang/Object\n");
82-
appendToCode(".method public <init>()V");
83-
appendToCode("aload_0");
84-
appendToCode("invokenonvirtual java/lang/Object/<init>()V");
85-
appendToCode("return");
86-
appendToCode(".end method\n");
87-
appendToCode(".method public static main([Ljava/lang/String;)V");
88-
appendToCode(".limit locals 100\n.limit stack 100");
89-
appendToCode(".line 1");
83+
outputCode.push_back(".class public java_class\n.super java/lang/Object\n");
84+
outputCode.push_back(".method public <init>()V");
85+
outputCode.push_back("aload_0");
86+
outputCode.push_back("invokenonvirtual java/lang/Object/<init>()V");
87+
outputCode.push_back("return");
88+
outputCode.push_back(".end method\n");
89+
outputCode.push_back(".method public static main([Ljava/lang/String;)V");
90+
outputCode.push_back(".limit locals 100\n.limit stack 100");
91+
outputCode.push_back(".line 1");
9092
}
9193

9294
void generateFooter() {
93-
appendToCode("return");
94-
appendToCode(".end method");
95+
outputCode.push_back("return");
96+
outputCode.push_back(".end method");
9597
}
9698

9799
unordered_set<int> makeList(int instruction_index) {

0 commit comments

Comments
 (0)