diff --git a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java index fc8c22d..cf57577 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfStatement.java @@ -38,12 +38,14 @@ public class IfStatement extends AbstractType implements IStatement{ @Override public void codeGen(MethodVisitor mv, HashMap localVars) throws Exception { + HashMap 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 } diff --git a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java index 02d19e3..e3648f9 100644 --- a/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/WhileStatement.java @@ -36,6 +36,9 @@ public class WhileStatement extends AbstractType implements IStatement { @Override public void codeGen(MethodVisitor mv, HashMap localVars) throws Exception { + + HashMap 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