Updated If and WhileStatement

This commit is contained in:
Jochen Seyfried 2024-05-14 14:54:33 +02:00
parent f2260c7a73
commit 0fcea195de
2 changed files with 7 additions and 2 deletions

View File

@ -38,12 +38,14 @@ public class IfStatement extends AbstractType implements IStatement{
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
Label conditionFalse = new Label();
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.codeGen(mv);
ifStatement.codeGen(mv, blockLocalVars);
mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed
}

View File

@ -36,6 +36,9 @@ public class WhileStatement extends AbstractType implements IStatement {
@Override
public void codeGen(MethodVisitor mv, HashMap<String, String> localVars) throws Exception {
HashMap<String, String> blockLocalVars = new HashMap<>(localVars);
Label conditionFalse = new Label();
Label LoopStart = new Label();
@ -44,7 +47,7 @@ public class WhileStatement extends AbstractType implements IStatement {
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0)
statement.codeGen(mv);
statement.codeGen(mv, blockLocalVars);
//TODO: If the block ends with a return statement, we might have to pop it from the stack
// So the next iteration starts with a clean stack
mv.visitJumpInsn(Opcodes.GOTO, LoopStart); // Jump to the start of the while loop