Updated If and WhileStatement
This commit is contained in:
parent
f2260c7a73
commit
0fcea195de
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user