add continue, mod and div

This commit is contained in:
404Simon 2024-05-20 22:16:52 +02:00
parent 0eba601eff
commit 4eb90f72ba
8 changed files with 47 additions and 1 deletions

View File

@ -23,6 +23,8 @@ public class MethodContext {
private int maxStack = 0;
//used to jump out of loops with break
private final Stack<Label> breakLabels = new Stack<>();
//used to jump to the start of the loop with continue
private final Stack<Label> continueLabels = new Stack<>();
public MethodContext(ClassContext classContext, MethodVisitor mv) {
this.mv = mv;

View File

@ -203,6 +203,10 @@ public class TypedBinary implements TypedExpression {
ctx.getMv().visitInsn(Opcodes.ISUB);
} else if (op == Operator.MUL) {
ctx.getMv().visitInsn(Opcodes.IMUL);
} else if (op == Operator.DIV) {
ctx.getMv().visitInsn(Opcodes.IDIV);
} else if (op == Operator.MOD) {
ctx.getMv().visitInsn(Opcodes.IREM);
}
}
ctx.popStack();

View File

@ -88,6 +88,11 @@ public class TypedBlock implements TypedNode {
continue;
}
if (stmt instanceof Continue) {
stmts.add(new TypedContinue());
continue;
}
if (stmt instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(typedProgram, methodCall);
typedMethodCall.typeCheck(typedProgram);

View File

@ -0,0 +1,26 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Break;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.Type;
import lombok.Data;
import org.objectweb.asm.Opcodes;
@Data
public class TypedContinue implements TypedStatement {
private Type type = Type.VOID;
@Override
public Type typeCheck(TypedProgram typedProgram) {
return type;
}
@Override
public void codeGen(MethodContext ctx) {
if (ctx.getBreakLabels().isEmpty()) {
throw new RuntimeException("Break statement outside of loop");
}
ctx.getMv().visitJumpInsn(Opcodes.GOTO, ctx.getContinueLabels().peek());
}
}

View File

@ -38,6 +38,7 @@ public class TypedDoWhile implements TypedStatement {
Label loopStart = new Label();
Label loopEnd = new Label();
ctx.getBreakLabels().push(loopEnd);
ctx.getContinueLabels().push(loopStart);
ctx.getMv().visitLabel(loopStart);
@ -49,5 +50,6 @@ public class TypedDoWhile implements TypedStatement {
ctx.popStack();
ctx.getMv().visitLabel(loopEnd);
ctx.getContinueLabels().pop();
}
}

View File

@ -48,6 +48,7 @@ public class TypedFor implements TypedStatement {
Label loopUpdate = new Label();
Label loopEnd = new Label();
ctx.getBreakLabels().push(loopEnd);
ctx.getContinueLabels().push(loopUpdate);
assign.codeGen(ctx);
ctx.getMv().visitLabel(loopStart);
@ -60,5 +61,6 @@ public class TypedFor implements TypedStatement {
inc.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, loopStart);
ctx.getMv().visitLabel(loopEnd);
ctx.getContinueLabels().pop();
}
}

View File

@ -39,6 +39,7 @@ public class TypedWhile implements TypedStatement {
Label loopStart = new Label();
Label loopEnd = new Label();
ctx.getBreakLabels().push(loopEnd);
ctx.getContinueLabels().push(loopStart);
ctx.getMv().visitLabel(loopStart);
cond.codeGen(ctx);
@ -48,5 +49,6 @@ public class TypedWhile implements TypedStatement {
typedBlock.codeGen(ctx);
ctx.getMv().visitJumpInsn(Opcodes.GOTO, loopStart);
ctx.getMv().visitLabel(loopEnd);
ctx.getContinueLabels().pop();
}
}

View File

@ -9,7 +9,10 @@ public class ClassCanBeBytecoded {
for (int i = 0; i < 12; i = i + 1) {
var1 = this.c.c.c.c.x + this.x;
if (var1 * 3 == 0) {
break;
continue;
}
while (var1 < 10) {
var1 = var1 + 1;
}
}
return var1;