Fixed crement assign

This commit is contained in:
i22007 2024-07-04 20:04:10 -04:00
parent 64b15af6ef
commit 36f0683240

View File

@ -10,6 +10,7 @@ import ast.members.MainMethodNode;
import ast.members.MethodNode;
import ast.parameters.ParameterNode;
import ast.statementexpressions.AssignNode;
import ast.statementexpressions.AssignableNode;
import ast.statementexpressions.NewDeclarationNode;
import ast.statementexpressions.crementexpressions.CrementType;
import ast.statementexpressions.crementexpressions.DecrementNode;
@ -277,7 +278,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), mapper.getTypeChar((BaseType) memberAccessNode.getTypeNode()));
} else if (memberAccessNode.getTypeNode() instanceof ReferenceType) {
methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), "L" + ((ReferenceType) memberAccessNode.getTypeNode()).getIdentifier() + ";");
}
}
}
@Override
@ -304,9 +305,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
public void visit(UnaryNode unaryNode) {
if (unaryNode.thisExp != null) {
methodVisitor.visitVarInsn(ALOAD, 0); // this
} else if (unaryNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
} else if (unaryNode.memberAccess != null) {
unaryNode.memberAccess.setTypeNode(unaryNode.getType());
unaryNode.memberAccess.accept(this);
} else if (unaryNode.value != null) {
unaryNode.value.accept(this);
@ -316,6 +316,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
unaryNode.statement.accept(this);
} else if (unaryNode.expression != null) {
unaryNode.expression.accept(this);
} else if (unaryNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
}
}
@ -324,7 +326,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
public void visit(BlockNode blockNode) {
for(IStatementNode statementNode : blockNode.statements) {
for (IStatementNode statementNode : blockNode.statements) {
statementNode.accept(this);
}
}
@ -380,6 +382,20 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
if (localVariableDeclarationNode.expression != null) {
// Process expression
localVariableDeclarationNode.expression.accept(this);
if (localVariableDeclarationNode.expression instanceof UnaryNode) {
UnaryNode unaryNode = (UnaryNode) localVariableDeclarationNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.type;
loadInrement(incrementNode);
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.type;
loadDecrement(decrementNode);
}
}
// add local var to list if not in list
if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
localVariables.add(localVariableDeclarationNode.identifier);
@ -400,24 +416,37 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
public void visit(AssignNode assignNode) {
if (assignNode.assignable.memberAccess != null) { // this / object
if (assignNode.expression instanceof IncrementNode) {
if (((IncrementNode) assignNode.expression).crementType.equals(CrementType.PREFIX)) { // ++i
fieldOrObjectVarPrefixCrementAssign(assignNode);
} else { // i++
fieldOrObjectVarSuffixCrementAssign(assignNode);
}
} else if (assignNode.expression instanceof DecrementNode) {
if (((DecrementNode) assignNode.expression).crementType.equals(CrementType.PREFIX)) {
fieldOrObjectVarPrefixCrementAssign(assignNode);
} else {
fieldOrObjectVarSuffixCrementAssign(assignNode);
assignNode.assignable.memberAccess.setTypeNode(assignNode.assignable.typeNode);
if (assignNode.expression instanceof UnaryNode) {
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
incrementNode.accept(this); // crement
fieldOrObjectVarCrementAssign(assignNode); // assign
} else { // i++
fieldOrObjectVarCrementAssign(assignNode); // assign
incrementNode.accept(this); // crement
}
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
decrementNode.accept(this); // crement
fieldOrObjectVarCrementAssign(assignNode); // assign
} else {
fieldOrObjectVarCrementAssign(assignNode); // assign
decrementNode.accept(this); // crement
}
}
} else {
assignFieldOrObjectVar(assignNode);
}
} else { // local var
if (assignNode.expression instanceof IncrementNode || assignNode.expression instanceof DecrementNode) {
localVarCrementAssign(assignNode);
if (assignNode.expression instanceof UnaryNode) {
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
if (unaryNode.statement instanceof IncrementNode || unaryNode.statement instanceof DecrementNode) {
localVarCrementAssign(assignNode);
}
} else {
assignNode.expression.accept(this);
assignLocalVar(assignNode);
@ -425,10 +454,37 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
}
}
private void loadInrement(IncrementNode incrementNode) {
AssignableNode assignableNode = incrementNode.assignableExpression;
if (assignableNode.memberAccess != null) {
assignableNode.memberAccess.accept(this);
} else { // TODO: typeNode incrementNode.assignableExpression = null, erledigt
if (assignableNode.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignableNode.identifier));
} else if (assignableNode.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignableNode.identifier));
}
}
}
private void loadDecrement(DecrementNode decrementNode) {
AssignableNode assignableNode = decrementNode.assignableExpression;
if (assignableNode.memberAccess != null) {
assignableNode.memberAccess.accept(this);
} else { // TODO: typeNode decrementNode.assignableExpression = null, erledigt
if (assignableNode.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignableNode.identifier));
} else if (assignableNode.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignableNode.identifier));
}
}
}
private void assignLocalVar(AssignNode assignNode) {
if (!localVariables.contains(assignNode.assignable.identifier)) {
localVariables.add(assignNode.assignable.identifier);
}
if (assignNode.expression.getType() instanceof BaseType) {
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier));
} else if (assignNode.expression.getType() instanceof ReferenceType) {
@ -444,7 +500,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
methodVisitor.visitVarInsn(ALOAD, 0);
} else {
localVariables.add(assignNode.assignable.identifier);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
assignNode.expression.accept(this);
@ -458,41 +513,56 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
}
private void localVarCrementAssign(AssignNode assignNode) {
if (assignNode.expression instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) assignNode.expression;
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
incrementNode.accept(this);
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadInrement(incrementNode);
assignLocalVar(assignNode);
} else { // i++
loadBeforeCrement(assignNode);
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadInrement(incrementNode);
assignLocalVar(assignNode);
incrementNode.accept(this);
}
} else if (assignNode.expression instanceof DecrementNode decrementNode) {
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
decrementNode.accept(this);
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
assignLocalVar(assignNode);
} else {
loadBeforeCrement(assignNode);
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
assignLocalVar(assignNode);
decrementNode.accept(this);
}
}
}
private void fieldOrObjectVarPrefixCrementAssign(AssignNode assignNode) {
private void fieldOrObjectVarCrementAssign(AssignNode assignNode) {
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
if(localVarIndex >= 0) { // object
if (localVarIndex >= 0) { // object
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
} else if(assignNode.assignable.memberAccess.thisExpr) { // field
} else if (assignNode.assignable.memberAccess.thisExpr) { // field
methodVisitor.visitVarInsn(ALOAD, 0);
} else {
localVariables.add(assignNode.assignable.identifier);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
assignNode.expression.accept(this);
methodVisitor.visitInsn(DUP_X1);
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadInrement((incrementNode));
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
}
if (assignNode.expression.getType() instanceof BaseType) {
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
@ -502,55 +572,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
}
}
private void fieldOrObjectVarSuffixCrementAssign(AssignNode assignNode) {
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
if(localVarIndex >= 0) { // object
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
} else if(assignNode.assignable.memberAccess.thisExpr) { // field
methodVisitor.visitVarInsn(ALOAD, 0);
} else {
localVariables.add(assignNode.assignable.identifier);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
loadBeforeCrement(assignNode);
if (assignNode.expression.getType() instanceof BaseType) {
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
} else if (assignNode.expression.getType() instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
}
assignNode.expression.accept(this);
}
private void loadBeforeCrement(AssignNode assignNode) {
if(assignNode.expression instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) assignNode.expression;
if(incrementNode.assignableExpression.memberAccess != null) {
incrementNode.assignableExpression.memberAccess.accept(this);
} else {
if(assignNode.assignable.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
} else if(assignNode.assignable.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
}
} else if(assignNode.expression instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) assignNode.expression;
if(decrementNode.assignableExpression.memberAccess != null) {
decrementNode.assignableExpression.memberAccess.accept(this);
} else {
if(assignNode.assignable.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
} else if(assignNode.assignable.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
}
}
}
@Override
public void visit(IncrementNode incrementNode) {
if (incrementNode.assignableExpression.memberAccess != null) { // Object var / field
@ -565,7 +586,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
}
methodVisitor.visitInsn(DUP);
methodVisitor.visitInsn(ICONST_1);
methodVisitor.visitInsn(IADD);
if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
@ -587,7 +607,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else { // this
methodVisitor.visitVarInsn(ALOAD, 0);
}
methodVisitor.visitInsn(DUP);
if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode()));
} else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
@ -660,6 +679,21 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else { // Return something
// Process expression
returnNode.expression.accept(this);
if (returnNode.expression instanceof UnaryNode) {
UnaryNode unaryNode = (UnaryNode) returnNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadInrement(incrementNode);
}
if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
}
}
// Return result of expression
if (returnNode.expression.getType() instanceof BaseType) {
methodVisitor.visitInsn(IRETURN);