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();
}
@ -165,13 +164,13 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
public void visit(DotSubstractionNode dotSubstractionNode) {
if (dotSubstractionNode.value != null) {
dotSubstractionNode.value.accept(this);
} else if(dotSubstractionNode.identifier != null) {
} else if (dotSubstractionNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(dotSubstractionNode.identifier));
} else if(dotSubstractionNode.memberAccess != null) {
} else if (dotSubstractionNode.memberAccess != null) {
dotSubstractionNode.memberAccess.accept(this);
} else if(dotSubstractionNode.methodCall != null) {
} else if (dotSubstractionNode.methodCall != null) {
dotSubstractionNode.methodCall.accept(this);
} else if(dotSubstractionNode.calculationExpression != null) {
} else if (dotSubstractionNode.calculationExpression != null) {
dotSubstractionNode.calculationExpression.accept(this);
}
}
@ -250,7 +249,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
public void visit(MemberAccessNode memberAccessNode) {
if(memberAccessNode.thisExpr) {
if (memberAccessNode.thisExpr) {
//methodVisitor.visitFieldInsn(PUTFIELD);
}
}
@ -339,17 +338,17 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
public void visit(ValueNode valueNode) {
switch (valueNode.valueType) {
case INT_VALUE:
int intValue = Integer.parseInt(valueNode.value);
if(intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE ) { // load int as byte
int intValue = Integer.parseInt(valueNode.value);
if (intValue >= Byte.MIN_VALUE && intValue <= Byte.MAX_VALUE) { // load int as byte
methodVisitor.visitIntInsn(BIPUSH, intValue);
} else if (intValue >= Short.MIN_VALUE && intValue <= Short.MAX_VALUE ) { // load int as short
} else if (intValue >= Short.MIN_VALUE && intValue <= Short.MAX_VALUE) { // load int as short
methodVisitor.visitIntInsn(SIPUSH, intValue);
} else { // load int as const
methodVisitor.visitLdcInsn(intValue);
}
break;
case BOOLEAN_VALUE:
if(valueNode.value.equals("true")) {
if (valueNode.value.equals("true")) {
methodVisitor.visitInsn(ICONST_1);
} else {
methodVisitor.visitInsn(ICONST_0);
@ -357,7 +356,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
break;
case CHAR_VALUE:
char charValue = valueNode.value.charAt(0);
if(charValue <= Byte.MAX_VALUE ) { // load char as byte
if (charValue <= Byte.MAX_VALUE) { // load char as byte
methodVisitor.visitIntInsn(BIPUSH, charValue);
} else if (charValue <= Short.MAX_VALUE) { // load char as short
methodVisitor.visitIntInsn(SIPUSH, charValue);
@ -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);