add while and do while

This commit is contained in:
404Simon 2024-05-20 20:18:23 +02:00
parent 99ed3a322b
commit de4082eff3
3 changed files with 31 additions and 3 deletions

View File

@ -3,6 +3,8 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import lombok.Data;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@ -33,6 +35,18 @@ public class TypedDoWhile implements TypedStatement {
@Override
public void codeGen(MethodContext ctx) {
Label loopStart = new Label();
Label loopEnd = new Label();
ctx.getMv().visitLabel(loopStart);
typedBlock.codeGen(ctx);
cond.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IFNE, loopStart);
ctx.popStack();
ctx.getMv().visitLabel(loopEnd);
}
}

View File

@ -3,6 +3,9 @@ package de.maishai.typedast.typedclass;
import de.maishai.ast.records.*;
import de.maishai.typedast.*;
import lombok.Data;
import org.objectweb.asm.Label;
import org.objectweb.asm.Opcodes;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@ -33,6 +36,16 @@ public class TypedWhile implements TypedStatement {
@Override
public void codeGen(MethodContext ctx) {
Label loopStart = new Label();
Label loopEnd = new Label();
ctx.getMv().visitLabel(loopStart);
cond.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.IFEQ, loopEnd);
ctx.popStack();
typedBlock.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, loopStart);
ctx.getMv().visitLabel(loopEnd);
}
}

View File

@ -9,8 +9,9 @@ public class ClassCanBeBytecoded {
}
public int test(int var1) {
int i;
i = this.c.c.callable();
return i;
do {
var1 = var1 + 1;
} while (var1 < 10);
return var1;
}
}