Some changes
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
i22007 2024-07-02 21:43:03 -04:00
parent 3227d69fc1
commit bd76135895
3 changed files with 58 additions and 63 deletions

View File

@ -30,11 +30,6 @@ public class TargetNode implements ASTNode, Visitable {
this.identifier = identifier;
}
@Override
public void accept(MethodVisitor methodVisitor) {
methodVisitor.visit(this);
}
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.analyze(this);
}

View File

@ -39,12 +39,12 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
private Mapper mapper;
private MethodVisitor methodVisitor;
private List<String> localVaribales;
private List<String> localVariables;
public MethodCodeGen(ClassWriter classWriter) {
this.classWriter = classWriter;
mapper = new Mapper();
localVaribales = new ArrayList<>();
localVariables = new ArrayList<>();
}
@ -60,10 +60,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
null);
methodVisitor.visitCode();
localVaribales.add("this");
localVariables.add("this");
// Add all method parameters to localVariables
for (ParameterNode parameterNode : constructorNode.parameters) {
localVaribales.add(parameterNode.identifier);
localVariables.add(parameterNode.identifier);
}
methodVisitor.visitVarInsn(ALOAD, 0);
@ -89,8 +89,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
null);
methodVisitor.visitCode();
localVaribales.add("this");
localVaribales.add("args");
localVariables.add("this");
localVariables.add("args");
// Visit all statements
for (IStatementNode statementNode : mainMethodNode.block.statements) {
@ -110,10 +110,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
null);
methodVisitor.visitCode();
localVaribales.add("this");
localVariables.add("this");
// Add all method parameters to localVariables
for (ParameterNode parameterNode : methodNode.parameters) {
localVaribales.add(parameterNode.identifier);
localVariables.add(parameterNode.identifier);
}
// Visit all statements
@ -181,7 +181,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
if (dotSubstractionNode.value != null) {
dotSubstractionNode.value.accept(this);
} else if (dotSubstractionNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(dotSubstractionNode.identifier));
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(dotSubstractionNode.identifier));
} else if (dotSubstractionNode.memberAccess != null) {
dotSubstractionNode.memberAccess.accept(this);
} else if (dotSubstractionNode.methodCall != null) {
@ -195,7 +195,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
public void visit(NonCalculationNode nonCalculationNode) {
Label labelFalse = new Label();
Label labelTrue = new Label();
// TODO: Null check
switch (nonCalculationNode.operator) {
case AND:
nonCalculationNode.unaryExpression.accept(this);
@ -266,8 +265,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
public void visit(MemberAccessNode memberAccessNode) {
if (memberAccessNode.thisExpr) {
// methodVisitor.visitFieldInsn(PUTFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), );
// Only used to get, not to put
if (memberAccessNode.thisExpr) { // Field
// methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), );
} else { // Object Attribut
}
}
@ -296,7 +297,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
if (unaryNode.thisExp != null) {
methodVisitor.visitVarInsn(ALOAD, 0); // this
} else if (unaryNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(unaryNode.identifier));
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
} else if (unaryNode.memberAccess != null) {
unaryNode.memberAccess.accept(this);
} else if (unaryNode.value != null) {
@ -362,23 +363,23 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
// Process expression
localVariableDeclarationNode.expression.accept(this);
// Store result of expression in variable
if (localVaribales.contains(localVariableDeclarationNode.identifier)) {
if (localVariables.contains(localVariableDeclarationNode.identifier)) {
if (localVariableDeclarationNode.type instanceof BaseType) {
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(localVariableDeclarationNode.identifier));
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
} else if (localVariableDeclarationNode.type instanceof ReferenceType) {
methodVisitor.visitVarInsn(ASTORE, localVaribales.indexOf(localVariableDeclarationNode.identifier));
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
}
} else {
localVaribales.add(localVariableDeclarationNode.identifier);
localVariables.add(localVariableDeclarationNode.identifier);
if (localVariableDeclarationNode.type instanceof BaseType) {
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(localVariableDeclarationNode.identifier));
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
} else if (localVariableDeclarationNode.type instanceof ReferenceType) {
methodVisitor.visitVarInsn(ASTORE, localVaribales.indexOf(localVariableDeclarationNode.identifier));
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
}
}
} else {
if (!localVaribales.contains(localVariableDeclarationNode.identifier)) {
localVaribales.add(localVariableDeclarationNode.identifier);
if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
localVariables.add(localVariableDeclarationNode.identifier);
}
}
}
@ -389,43 +390,49 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
if (assignNode.expression instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) assignNode.expression;
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
methodVisitor.visitIincInsn(localVaribales.indexOf(incrementNode.assignableExpression.identifier), 1);
methodVisitor.visitIincInsn(localVariables.indexOf(incrementNode.assignableExpression.identifier), 1);
assign(assignNode);
} else if (incrementNode.crementType.equals(CrementType.SUFFIX)) { // Suffix: i++
assign(assignNode);
methodVisitor.visitIincInsn(localVaribales.indexOf(incrementNode.assignableExpression.identifier), 1);
methodVisitor.visitIincInsn(localVariables.indexOf(incrementNode.assignableExpression.identifier), 1);
}
} else if (assignNode.expression instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) assignNode.expression;
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
methodVisitor.visitIincInsn(localVaribales.indexOf(decrementNode.assignableExpression.identifier), -1);
methodVisitor.visitIincInsn(localVariables.indexOf(decrementNode.assignableExpression.identifier), -1);
assign(assignNode);
} else if (decrementNode.crementType.equals(CrementType.SUFFIX)) {
assign(assignNode);
methodVisitor.visitIincInsn(localVaribales.indexOf(decrementNode.assignableExpression.identifier), 1);
methodVisitor.visitIincInsn(localVariables.indexOf(decrementNode.assignableExpression.identifier), 1);
}
} else {
assignNode.expression.accept(this);
assign(assignNode);
}
}
private void assign(AssignNode assignNode) {
// Store result of expression in variable
if (assignNode.assignable.memberAccess.thisExpr) {
// Global var
methodVisitor.visitVarInsn(ALOAD, 0);
if (assignNode.expression instanceof BaseType) {
//methodVisitor.visitFieldInsn(PUTFIELD, class name, var identifier, mapper.getTypeChar(((BaseTypeNode) type).enumType));
} else if (assignNode.expression instanceof ReferenceType) {
//methodVisitor.visitFieldInsn(PUTFIELD, class name, var identifier, "L"class name object +";");
}
assignField(assignNode);
} else {
// Local var
if (assignNode.expression instanceof BaseType) {
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(assignNode.assignable.identifier));
} else if (assignNode.expression instanceof ReferenceType) {
methodVisitor.visitVarInsn(ASTORE, localVaribales.indexOf(assignNode.assignable.identifier));
}
assignLocalVar(assignNode);
}
}
private void assignLocalVar(AssignNode assignNode) {
if (assignNode.expression instanceof BaseType) {
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier));
} else if (assignNode.expression instanceof ReferenceType) {
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(assignNode.assignable.identifier));
}
}
private void assignField(AssignNode assignNode) {
if (assignNode.expression 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 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()+";");
}
}
@ -433,13 +440,13 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
public void visit(NewDeclarationNode newDeclarationNode) {
methodVisitor.visitTypeInsn(NEW, newDeclarationNode.identifier);
methodVisitor.visitInsn(DUP);
List<ParameterNode> parameterNodes = new ArrayList<>();
for (IExpressionNode expressionNode : newDeclarationNode.expressions) {
expressionNode.accept(this);
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
}
// TODO
//methodVisitor.visitMethodInsn(INVOKESPECIAL, class name, "<init>", mapper.generateMethodDescriptor(), false);
// TODO: kann ein Field auch definiert werden? Abfrage ob local var oder field
localVaribales.add(newDeclarationNode.identifier);
methodVisitor.visitMethodInsn(INVOKESPECIAL, newDeclarationNode.identifier, "<init>", mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID),parameterNodes), false);
localVariables.add(newDeclarationNode.identifier);
}
@Override
@ -505,13 +512,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
whileNode.expression.accept(this);
methodVisitor.visitJumpInsn(IFEQ, endOfLoopLabel); // if condition is false, jump out of loop
// TODO: Unterscheidung bei increment/decrement der for Schleife
if (whileNode.block.statements.size() == 2) { // For loop
whileNode.block.statements.get(0).accept(this);
} else {
whileNode.block.statements.get(0).accept(this);
}
whileNode.block.accept(this);
methodVisitor.visitJumpInsn(GOTO, loopLabel);
@ -520,16 +520,17 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override
public void visit(ChainedMethodNode chainedMethodNode) {
// TODO: Erstmal abwarten
}
@Override
public void visit(MethodCallNode methodCallNode) {
}
@Override
public void visit(TargetNode targetNode) {
List<ParameterNode> parameterNodes = new ArrayList<>();
for(IExpressionNode expressionNode : methodCallNode.parameters) {
expressionNode.accept(this);
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
}
// TODO: Klassenname und Returntype
//methodVisitor.visitMethodInsn(INVOKEVIRTUAL, classname, methodCallNode.identifier, mapper.generateMethodDescriptor(returntype, parameterNodes), false);
}
}

View File

@ -46,7 +46,6 @@ public interface MethodVisitor {
// statement expression
void visit(ChainedMethodNode chainedMethodNode);
void visit(MethodCallNode methodCallNode);
void visit(TargetNode targetNode);
void visit(AssignNode assignNode);
void visit(NewDeclarationNode newDeclarationNode);