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