Bytecode for loops
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
i22007 2024-06-27 17:32:50 -04:00
parent 70a3788030
commit c5fc378eed
3 changed files with 54 additions and 22 deletions

View File

@ -23,9 +23,4 @@ public class AssignableNode implements IStatementExpressionNode {
return visitor.analyze(this);
}
@Override
public void accept(MethodVisitor methodVisitor) {
methodVisitor.visit(this);
}
}

View File

@ -72,7 +72,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
statementNode.accept(this);
}
methodVisitor.visitInsn(RETURN);
methodVisitor.visitMaxs(0, 0);
methodVisitor.visitEnd();
}
@ -386,17 +385,61 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
public void visit(WhileNode whileNode) {
Label loopLabel = new Label();
Label endOfLoopLabel = new Label();
methodVisitor.visitLabel(loopLabel);
// while loop
whileNode.expression.accept(this);
methodVisitor.visitJumpInsn(IFEQ, endOfLoopLabel); // if condition is false, jump out of loop
whileNode.block.accept(this);
methodVisitor.visitJumpInsn(GOTO, loopLabel);
methodVisitor.visitLabel(endOfLoopLabel);
}
@Override
public void visit(DecrementNode decrementNode) {
switch (decrementNode.crementType) {
case PREFIX: // --i
if(decrementNode.assignableExpression.memberAccess == null) { // local Var
methodVisitor.visitIincInsn(localVaribales.indexOf(decrementNode.assignableExpression.identifier), -1);
} else { // Field or var from other object
}
break;
case SUFFIX: // i--
if(decrementNode.assignableExpression.memberAccess == null) { // local Var
methodVisitor.visitIincInsn(localVaribales.indexOf(decrementNode.assignableExpression.identifier), -1);
} else { // Field or var from other object
}
break;
}
}
@Override
public void visit(IncrementNode incrementNode) {
switch (incrementNode.crementType) {
case PREFIX: // ++i
if(incrementNode.assignableExpression.memberAccess == null) { // local Var
methodVisitor.visitIincInsn(localVaribales.indexOf(incrementNode.assignableExpression.identifier), 1);
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(incrementNode.assignableExpression.identifier));
} else { // Field or var from other object
}
break;
case SUFFIX: // i++
if(incrementNode.assignableExpression.memberAccess == null) { // local Var
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(incrementNode.assignableExpression.identifier));
methodVisitor.visitIincInsn(localVaribales.indexOf(incrementNode.assignableExpression.identifier), 1);
} else { // Field or var from other object
}
break;
}
}
@Override
@ -413,9 +456,4 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
public void visit(TargetNode targetNode) {
}
@Override
public void visit(AssignableNode assignableNode) {
}
}

View File

@ -53,7 +53,6 @@ public interface MethodVisitor {
void visit(MethodCallNode methodCallNode);
void visit(TargetNode targetNode);
void visit(AssignableNode assignableNode);
void visit(AssignNode assignNode);
void visit(NewDeclarationNode newDeclarationNode);