Skip to content

Commit a3d1db4

Browse files
authored
Merge pull request #65 from GilbertBckr/while
added codegen for (do) while
2 parents 89eada4 + 9e7ea4d commit a3d1db4

3 files changed

Lines changed: 129 additions & 59 deletions

File tree

src/main/java/com/compiler/ast/ASTDoWhileLoopStmtNode.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import java.io.OutputStreamWriter;
44

5+
import com.compiler.CompileEnvIntf;
6+
import com.compiler.InstrBlock;
7+
import com.compiler.instr.InstrCondJump;
8+
import com.compiler.instr.InstrJump;
9+
510
public class ASTDoWhileLoopStmtNode extends ASTStmtNode {
611

712
ASTExprNode m_predicate;
@@ -26,4 +31,20 @@ public void print(OutputStreamWriter outStream, String indent) throws Exception
2631
m_predicate.print(outStream, " " + indent);
2732
m_loopBody.print(outStream, " " + indent);
2833
}
34+
35+
@Override
36+
public void codegen(CompileEnvIntf env) {
37+
InstrBlock doWhile = env.createBlock("DoWhileBlock");
38+
InstrBlock exit = env.createBlock("DoWhileExitBlock");
39+
40+
InstrJump jumpToLoop = new InstrJump(doWhile);
41+
env.addInstr(jumpToLoop);
42+
43+
env.setCurrentBlock(doWhile);
44+
m_loopBody.codegen(env);
45+
InstrCondJump conditionalJump = new InstrCondJump(m_predicate.codegen(env), doWhile, exit);
46+
env.addInstr(conditionalJump);
47+
48+
env.setCurrentBlock(exit);
49+
}
2950
}

src/main/java/com/compiler/ast/ASTWhileLoopStmtNode.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
import java.io.OutputStreamWriter;
44

5+
import com.compiler.CompileEnvIntf;
6+
import com.compiler.InstrBlock;
7+
import com.compiler.instr.InstrCondJump;
8+
import com.compiler.instr.InstrJump;
9+
510
public class ASTWhileLoopStmtNode extends ASTStmtNode {
611

712
ASTExprNode m_predicate;
@@ -26,4 +31,26 @@ public void print(OutputStreamWriter outStream, String indent) throws Exception
2631
m_predicate.print(outStream, " " + indent);
2732
m_loopBody.print(outStream, " " + indent);
2833
}
34+
35+
@Override
36+
public void codegen(CompileEnvIntf env) {
37+
InstrBlock whileHead = env.createBlock("WhileLoopHead");
38+
InstrBlock whileBody = env.createBlock("WhileLoopBody");
39+
InstrBlock exit = env.createBlock("WhileExitBlock");
40+
InstrJump jumpToHead = new InstrJump(whileHead);
41+
env.addInstr(jumpToHead);
42+
43+
env.setCurrentBlock(whileBody);
44+
45+
m_loopBody.codegen(env);
46+
47+
env.addInstr(new InstrJump(whileHead));
48+
49+
env.setCurrentBlock(whileHead);
50+
InstrCondJump conditionalJump = new InstrCondJump(m_predicate.codegen(env), whileBody, exit);
51+
52+
whileHead.addInstr(conditionalJump);
53+
54+
env.setCurrentBlock(exit);
55+
}
2956
}

src/test/java/com/compiler/StmtDoWhileStmtParserTest.java

Lines changed: 81 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,65 +2,87 @@
22

33
import org.junit.Test;
44

5-
public class StmtDoWhileStmtParserTest extends StmtParserTestBase
6-
{
7-
@Test
8-
public void testWhileProgram1() throws Exception {
9-
String program = """
10-
{
11-
DECLARE index;
12-
DECLARE sum;
13-
index = 10;
14-
sum = 0;
15-
WHILE(index) {
16-
sum = sum + index;
17-
index = index - 1;
18-
};
19-
PRINT sum;
20-
}
21-
""";
22-
// laut kleinem Gauß: 55
23-
testParser(program, "55\n");
24-
}
5+
public class StmtDoWhileStmtParserTest extends InterpreterTestBase {
6+
@Test
7+
public void testWhileProgram1() throws Exception {
8+
String program = """
9+
{
10+
DECLARE index;
11+
DECLARE sum;
12+
index = 10;
13+
sum = 0;
14+
WHILE(index) {
15+
sum = sum + index;
16+
index = index - 1;
17+
};
18+
PRINT sum;
19+
}
20+
""";
21+
// laut kleinem Gauß: 55
22+
testInterpreter(program, "55\n");
23+
}
2524

26-
@Test
27-
public void testDoWhileProgram2() throws Exception {
28-
String program = """
29-
{
30-
DECLARE index;
31-
DECLARE sum;
32-
index = 10;
33-
sum = 0;
34-
DO {
35-
sum = sum + index;
36-
index = index - 1;
37-
} WHILE(index);
38-
PRINT sum;
39-
}
40-
""";
41-
testParser(program, "55\n");
42-
}
25+
@Test
26+
public void testDoWhileProgram2() throws Exception {
27+
String program = """
28+
{
29+
DECLARE index;
30+
DECLARE sum;
31+
index = 10;
32+
sum = 0;
33+
DO {
34+
sum = sum + index;
35+
index = index - 1;
36+
} WHILE(index);
37+
PRINT sum;
38+
}
39+
""";
40+
testInterpreter(program, "55\n");
41+
}
4342

44-
@Test
45-
public void testNestedWhileProgram3() throws Exception {
46-
String program = """
47-
{
48-
DECLARE index0;
49-
DECLARE sum;
50-
DECLARE index1;
51-
index0 = 10;
52-
sum = 0;
53-
WHILE(index0) {
54-
index1 = 5;
55-
WHILE(index1) {
56-
sum = sum + index1;
57-
index1 = index1 - 1;
58-
};
59-
index0 = index0 - 1;
60-
};
61-
PRINT sum;
62-
}
63-
""";
64-
testParser(program, "150\n");
65-
}
43+
@Test
44+
public void testNestedWhileProgram3() throws Exception {
45+
String program = """
46+
{
47+
DECLARE index0;
48+
DECLARE sum;
49+
DECLARE index1;
50+
index0 = 10;
51+
sum = 0;
52+
WHILE(index0) {
53+
index1 = 5;
54+
WHILE(index1) {
55+
sum = sum + index1;
56+
index1 = index1 - 1;
57+
};
58+
index0 = index0 - 1;
59+
};
60+
PRINT sum;
61+
}
62+
""";
63+
testInterpreter(program, "150\n");
64+
}
65+
66+
@Test
67+
public void testNestedDoWhileProgram4() throws Exception {
68+
String program = """
69+
{
70+
DECLARE index0;
71+
DECLARE sum;
72+
DECLARE index1;
73+
index0 = 10;
74+
sum = 0;
75+
DO {
76+
index1 = 5;
77+
DO {
78+
sum = sum + index1;
79+
index1 = index1 - 1;
80+
} WHILE(index1);
81+
index0 = index0 - 1;
82+
} WHILE(index0) ;
83+
PRINT sum;
84+
}
85+
""";
86+
testInterpreter(program, "150\n");
87+
}
6688
}

0 commit comments

Comments
 (0)