-
Notifications
You must be signed in to change notification settings - Fork 17
Adds the For Loop #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
886ccad
feat: add ASTForLoopStmtNode implementation and corresponding parser …
6212d04
fix: remove merge conflict markers from StmtParser.java
2c5c2ec
Update ci.yml
Sebastian-Heinzenburger 3883dfd
Revert "Update ci.yml"
8d88b09
fix: update interpreter tests to use regex for expected output
963ba08
fix: update regex in interpreter tests for expected output
1900868
Merge remote-tracking branch 'upstream/main'
b6ab006
fix: revert InterpreterMain
3f69bb6
fix: revert the changes to InterpreterDumpTestBase and the ConstFoldU…
73427d8
fix: revert the changes to InterpreterDumpTestBase and the ConstFoldU…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,4 +23,4 @@ public static void main(String[] args) throws Exception { | |
| outStream.flush(); | ||
| } | ||
|
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package com.compiler; | ||
|
|
||
| import com.compiler.ast.ASTExprNode; | ||
| import com.compiler.ast.ASTStmtNode; | ||
| import com.compiler.instr.InstrCondJump; | ||
| import com.compiler.instr.InstrJump; | ||
|
|
||
| import java.io.OutputStreamWriter; | ||
|
|
||
| public class ASTForLoopStmtNode extends ASTStmtNode { | ||
| private ASTStmtNode m_initStmt; | ||
| private ASTExprNode m_predicate; | ||
| private ASTStmtNode m_updateStmt; | ||
| private ASTStmtNode m_body; | ||
|
|
||
| public ASTForLoopStmtNode(ASTStmtNode initStmt, ASTExprNode predicate, ASTStmtNode updateStmt, ASTStmtNode body) { | ||
| m_initStmt = initStmt; | ||
| m_predicate = predicate; | ||
| m_updateStmt = updateStmt; | ||
| m_body = body; | ||
| } | ||
|
|
||
| @Override | ||
| public void execute(OutputStreamWriter out) { | ||
| m_initStmt.execute(out); | ||
| while (m_predicate.eval() != 0) { | ||
| m_body.execute(out); | ||
| m_updateStmt.execute(out); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void codegen(CompileEnvIntf env) { | ||
| InstrBlock initBlock = env.createBlock("for_init"); | ||
| InstrBlock predicateBlock = env.createBlock("for_predicate"); | ||
| InstrBlock bodyBlock = env.createBlock("for_body"); | ||
| InstrBlock updateBlock = env.createBlock("for_update"); | ||
| InstrBlock exitBlock = env.createBlock("for_exit"); | ||
|
|
||
| env.addInstr(new InstrJump(initBlock)); | ||
| env.setCurrentBlock(initBlock); | ||
| m_initStmt.codegen(env); | ||
| env.addInstr(new InstrJump(predicateBlock)); | ||
|
|
||
| env.setCurrentBlock(predicateBlock); | ||
| InstrIntf predicateInstr = m_predicate.codegen(env); | ||
| env.addInstr(predicateInstr); | ||
| env.addInstr(new InstrCondJump(predicateInstr, bodyBlock, exitBlock)); | ||
|
|
||
| env.setCurrentBlock(bodyBlock); | ||
| m_body.codegen(env); | ||
| env.addInstr(new InstrJump(updateBlock)); | ||
|
|
||
| env.setCurrentBlock(updateBlock); | ||
| m_updateStmt.codegen(env); | ||
| env.addInstr(new InstrJump(predicateBlock)); | ||
|
|
||
| env.setCurrentBlock(exitBlock); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void print(OutputStreamWriter outStream, String indent) throws Exception { | ||
| outStream.write(indent + "FOR:\n" ); | ||
|
|
||
| outStream.write(indent + "\t" + "initStmt: \n"); | ||
| m_initStmt.print(outStream, indent + "\t\t"); | ||
| outStream.write("\n"); | ||
|
|
||
| outStream.write(indent + "\t" + "predicate: \n"); | ||
| m_predicate.print(outStream, indent + "\t\t"); | ||
| outStream.write("\n"); | ||
|
|
||
| outStream.write(indent + "\t" + "updateStmt: \n"); | ||
| m_updateStmt.print(outStream, indent + "\t\t"); | ||
| outStream.write("\n"); | ||
|
|
||
| outStream.write(indent + "\t" + "body: \n"); | ||
| m_body.print(outStream, indent + "\t\t"); | ||
| outStream.write("\n"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,6 @@ public void testNotZero() throws Exception { | |
|
|
||
| """; | ||
| testInterpreter(input, expected); | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package com.compiler; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| public class TestForLoop extends InterpreterTestBase { | ||
|
|
||
| @Test | ||
| public void testForLoop1() throws Exception { | ||
| String code = """ | ||
| { | ||
| DECLARE index; | ||
| DECLARE sum; | ||
| index = 0; | ||
| sum = 0; | ||
| FOR(index = 10; index; index = index - 1;) { | ||
| sum = sum + index; | ||
| }; | ||
| PRINT sum; | ||
| } | ||
| """; | ||
| testInterpreter(code, "55\n"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testForLoop2() throws Exception { | ||
| String code = """ | ||
| { | ||
| DECLARE index; | ||
| DECLARE sum; | ||
| index = 64; | ||
| sum = 0; | ||
| FOR(index = index * 2; index; index = index / 2;) { | ||
| sum = sum + index; | ||
| }; | ||
| PRINT sum; | ||
| } | ||
| """; | ||
| testInterpreter(code, "255\n"); | ||
| } | ||
|
|
||
| @Test | ||
| public void testForLoop3() throws Exception { | ||
| String code = """ | ||
| { | ||
| DECLARE index; | ||
| DECLARE sum; | ||
| index = 10 - 20; | ||
| sum = 0; | ||
| FOR(PRINT index; index; index = index + 2;) { | ||
| sum = sum + index; | ||
| }; | ||
| PRINT sum; | ||
| } | ||
| """; | ||
| testInterpreter(code, "-10\n-30\n"); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sieht gut aus
der for_init block und der for_update block sind nicht notwendig, der Content kann auch in den Vorgängerblock geschrieben werden (kann man aber auch so lassen, wenn es euch besser gefällt)