Skip to content

Commit af1d6be

Browse files
committed
Trying to implement LOOP
1 parent cbe3e86 commit af1d6be

4 files changed

Lines changed: 91 additions & 1 deletion

File tree

src/main/java/com/InterpreterMain.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ public class InterpreterMain {
77
public static void main(String[] args) throws Exception {
88
String input = new String("""
99
{
10-
PRINT 4 + 3 - 2;
10+
DECLARE a;
11+
a = 1;
12+
LOOP{
13+
PRINT(a);
14+
a = a + 1;
15+
IF(a > 10){
16+
BREAK
17+
}
18+
}ENDLOOP
1119
}
1220
""");
1321
com.compiler.CompileEnv compileEnv = new com.compiler.CompileEnv(input, false);

src/main/java/com/compiler/StmtParser.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ public ASTStmtNode parseStmt() throws Exception {
8787
return parseIfElseStmt();
8888
}
8989

90+
if (type == Type.LOOP) {
91+
return parseForLoop();
92+
}
93+
94+
if(type == Type.BREAK){
95+
return parseBreakNode();
96+
}
97+
9098
m_lexer.throwCompilerException("Invalid begin of statement", "DECLARE or IDENTIFIER or PRINT or NUMERIC_IF");
9199
return null; // unreachable
92100
}
@@ -303,6 +311,22 @@ ASTDoWhileLoopStmtNode parseDoWhileLoopStmt() throws Exception {
303311
m_lexer.expect(Type.SEMICOLON);
304312
return new ASTDoWhileLoopStmtNode(predicate, body);
305313
}
314+
315+
public ASTStmtNode parseForLoop() throws Exception {
316+
//loopstmt : loopstart [stmtlist] loopend
317+
m_lexer.expect(Type.LOOP);
318+
m_lexer.expect(Type.LBRACE);
319+
ASTStmtListNode body = parseStmtlist();
320+
m_lexer.expect(Type.RBRACE);
321+
m_lexer.expect(Type.ENDLOOP);
322+
return new ASTLoopStmtNode(body);
323+
324+
}
325+
326+
private ASTStmtNode parseBreakNode() throws Exception {
327+
m_lexer.expect(Type.BREAK);
328+
return new ASTBreakExprNode();
329+
}
306330

307331
ASTStmtNode parseExecuteNTimesStmt() throws Exception {
308332
// EXECUTE integer|identifier TIMES LBRACE stmtList RBRACE SEMICOLON
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.compiler.ast;
2+
3+
import java.io.OutputStreamWriter;
4+
5+
public class ASTBreakExprNode extends ASTStmtNode{
6+
@Override
7+
public void print(OutputStreamWriter outStream, String indent) throws Exception {
8+
9+
}
10+
11+
@Override
12+
public void execute(OutputStreamWriter out) {
13+
14+
}
15+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.compiler.ast;
2+
3+
import com.compiler.CompileEnvIntf;
4+
import com.compiler.InstrBlock;
5+
import com.compiler.instr.InstrJump;
6+
7+
import java.io.OutputStreamWriter;
8+
9+
public class ASTLoopStmtNode extends ASTStmtNode{
10+
11+
//loopstmt : loopstart stmtlist loopend
12+
//
13+
//loopstart : LOOP LBRACE
14+
//
15+
//loopend : RBRACE ENDLOOP
16+
17+
private ASTStmtListNode m_body;
18+
19+
public ASTLoopStmtNode(ASTStmtListNode body) {
20+
m_body = body;
21+
}
22+
23+
@Override
24+
public void execute(OutputStreamWriter out) {
25+
for(ASTStmtNode node : m_body.stmts){
26+
if((node instanceof ASTBreakExprNode)){
27+
node.execute(out);
28+
}
29+
}
30+
}
31+
32+
@Override
33+
public void codegen(CompileEnvIntf env){
34+
35+
36+
37+
}
38+
39+
@Override
40+
public void print(OutputStreamWriter outStream, String indent) throws Exception {
41+
42+
}
43+
}

0 commit comments

Comments
 (0)