File tree Expand file tree Collapse file tree
main/java/com/compiler/ast Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ public void codegen(CompileEnvIntf env) {
4343 env .setCurrentBlock (doWhile );
4444 m_loopBody .codegen (env );
4545 InstrCondJump conditionalJump = new InstrCondJump (m_predicate .codegen (env ), doWhile , exit );
46- doWhile .addInstr (conditionalJump );
46+ env .addInstr (conditionalJump );
4747
4848 env .setCurrentBlock (exit );
4949 }
Original file line number Diff line number Diff line change @@ -41,8 +41,10 @@ public void codegen(CompileEnvIntf env) {
4141 env .addInstr (jumpToHead );
4242
4343 env .setCurrentBlock (whileBody );
44+
4445 m_loopBody .codegen (env );
45- whileBody .addInstr (new InstrJump (whileHead ));
46+
47+ env .addInstr (new InstrJump (whileHead ));
4648
4749 env .setCurrentBlock (whileHead );
4850 InstrCondJump conditionalJump = new InstrCondJump (m_predicate .codegen (env ), whileBody , exit );
Original file line number Diff line number Diff line change 22
33import 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}
You can’t perform that action at this time.
0 commit comments