Compare commits
15 Commits
49195c754c
...
johns-bran
Author | SHA1 | Date | |
---|---|---|---|
|
ab5a51d3b1 | ||
|
a4c1c502ab | ||
|
7d441116bd | ||
|
8afa3b8461 | ||
|
84ecf316cd | ||
|
7ddea8f18d | ||
46154fbb01 | |||
9aac795c07 | |||
e59f4e7190 | |||
|
44c2f551de | ||
|
bad034acfd | ||
|
ba73e1bd45 | ||
|
b7affd75ae | ||
bd76135895 | |||
233725778f |
@@ -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);
|
||||||
|
@@ -21,6 +21,7 @@ import ast.statementexpressions.AssignableNode;
|
|||||||
import ast.statementexpressions.NewDeclarationNode;
|
import ast.statementexpressions.NewDeclarationNode;
|
||||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||||
|
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
@@ -29,6 +30,7 @@ import ast.type.EnumAccessModifierNode;
|
|||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import com.sun.jdi.IntegerType;
|
import com.sun.jdi.IntegerType;
|
||||||
|
import semantic.context.ClassContext;
|
||||||
import semantic.context.Context;
|
import semantic.context.Context;
|
||||||
import semantic.exceptions.*;
|
import semantic.exceptions.*;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
@@ -163,6 +165,11 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(FieldNode toCheck) {
|
public TypeCheckResult analyze(FieldNode toCheck) {
|
||||||
|
if (toCheck.type instanceof ReferenceType referenceType) {
|
||||||
|
if(!context.containsClass(referenceType.getIdentifier())){
|
||||||
|
errors.add(new NotDeclaredException(referenceType.getIdentifier() + " not declared"));
|
||||||
|
}
|
||||||
|
}
|
||||||
if (currentFields.get(toCheck.identifier) != null) {
|
if (currentFields.get(toCheck.identifier) != null) {
|
||||||
errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier));
|
errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier));
|
||||||
return new TypeCheckResult(false, null);
|
return new TypeCheckResult(false, null);
|
||||||
@@ -170,6 +177,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
currentFields.put(toCheck.identifier, toCheck.type);
|
currentFields.put(toCheck.identifier, toCheck.type);
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(true, null);
|
return new TypeCheckResult(true, null);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -275,13 +284,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
currentNullType = oldNullType;
|
currentNullType = oldNullType;
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
// This check currently handles things like :
|
|
||||||
/**
|
|
||||||
* private int i;
|
|
||||||
* void foo(int i){
|
|
||||||
* i = i;
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
if (assignable.equals(rExpression)) {
|
if (assignable.equals(rExpression)) {
|
||||||
errors.add(new TypeMismatchException("Cannot assign to self"));
|
errors.add(new TypeMismatchException("Cannot assign to self"));
|
||||||
valid = false;
|
valid = false;
|
||||||
@@ -324,6 +326,15 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
targetType = currentFields.get(toCheck.target.identifier);
|
targetType = currentFields.get(toCheck.target.identifier);
|
||||||
}
|
}
|
||||||
if (targetType instanceof ReferenceType reference) {
|
if (targetType instanceof ReferenceType reference) {
|
||||||
|
if (!toCheck.chainedMethods.isEmpty()) {
|
||||||
|
for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) {
|
||||||
|
var type = getTypeFromMethod(chainedMethod, reference);
|
||||||
|
if (type instanceof ReferenceType referenceType)
|
||||||
|
reference = referenceType;
|
||||||
|
else
|
||||||
|
errors.add(new TypeMismatchException("Ein Basetyp hat keine funktionen"));
|
||||||
|
}
|
||||||
|
}
|
||||||
var type = getTypeFromMethod(toCheck, reference);
|
var type = getTypeFromMethod(toCheck, reference);
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
return new TypeCheckResult(true, type);
|
return new TypeCheckResult(true, type);
|
||||||
@@ -620,4 +631,37 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ITypeNode getTypeFromMethod(ChainedMethodNode toCheck, ReferenceType reference) {
|
||||||
|
var classContext = context.getClass(reference.getIdentifier());
|
||||||
|
|
||||||
|
var methods = classContext.getMethods();
|
||||||
|
for (var method : methods) {
|
||||||
|
if (toCheck.identifier.equals(method.getIdentifier())) {
|
||||||
|
if (method.getParameters().size() == toCheck.expressions.size() && !(method instanceof ConstructorNode)) {
|
||||||
|
boolean same = true;
|
||||||
|
for (int i = 0; i < method.getParameters().size(); i++) {
|
||||||
|
var result1 = method.getParameters().get(i).accept(this);
|
||||||
|
var result2 = toCheck.expressions.get(i).accept(this);
|
||||||
|
if (!Objects.equals(result1.getType(), result2.getType())) {
|
||||||
|
same = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (same) {
|
||||||
|
if (method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC) {
|
||||||
|
if (method.getType() == null) {
|
||||||
|
return new BaseType(TypeEnum.VOID);
|
||||||
|
}
|
||||||
|
return method.getType();
|
||||||
|
} else {
|
||||||
|
errors.add(new NotVisibleException("This Method is not Visible"));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
@@ -21,6 +21,10 @@ public class Context {
|
|||||||
return classes.get(identifier);
|
return classes.get(identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HashMap<String, ClassContext> getClasses() {
|
||||||
|
return classes;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean containsClass(String identifier) {
|
public boolean containsClass(String identifier) {
|
||||||
return classes.containsKey(identifier);
|
return classes.containsKey(identifier);
|
||||||
}
|
}
|
||||||
|
@@ -12,11 +12,16 @@ import java.nio.file.Paths;
|
|||||||
* run every test: mvn test
|
* run every test: mvn test
|
||||||
* Nutzen dieser Klasse: Eigentlich nicht vorhanden, in der Main gibts nichts zu testen
|
* Nutzen dieser Klasse: Eigentlich nicht vorhanden, in der Main gibts nichts zu testen
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
|
||||||
public class MainTest {
|
public class MainTest {
|
||||||
@Test
|
@Test
|
||||||
void test() {
|
void test() {
|
||||||
System.out.println("MainTest");
|
CharStream codeCharStream = null;
|
||||||
|
try {
|
||||||
|
codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/resources/CompilerInput.java"));
|
||||||
|
Main.compileFile(codeCharStream, "src/main/test/resources/output");
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.err.println("Error reading the file: " + e.getMessage());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -11,7 +11,7 @@ import org.junit.jupiter.api.Test;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
@Deprecated
|
|
||||||
public class ReflectionsTest {
|
public class ReflectionsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@@ -9,20 +9,34 @@ import ast.expressions.unaryexpressions.MemberAccessNode;
|
|||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
import ast.expressions.unaryexpressions.UnaryNode;
|
||||||
import ast.members.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.members.FieldNode;
|
import ast.members.FieldNode;
|
||||||
|
import ast.members.MemberNode;
|
||||||
import ast.members.MethodNode;
|
import ast.members.MethodNode;
|
||||||
import ast.parameters.ParameterNode;
|
import ast.parameters.ParameterNode;
|
||||||
import ast.statementexpressions.AssignNode;
|
import ast.statementexpressions.AssignNode;
|
||||||
import ast.statementexpressions.AssignableNode;
|
import ast.statementexpressions.AssignableNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
import ast.statements.BlockNode;
|
import ast.statements.BlockNode;
|
||||||
|
import ast.statements.IStatementNode;
|
||||||
import ast.statements.ReturnNode;
|
import ast.statements.ReturnNode;
|
||||||
import ast.type.AccessModifierNode;
|
import ast.type.AccessModifierNode;
|
||||||
import ast.type.EnumValueNode;
|
import ast.type.EnumValueNode;
|
||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
import ast.type.type.BaseType;
|
import ast.type.type.BaseType;
|
||||||
|
import ast.type.type.ITypeNode;
|
||||||
import ast.type.type.TypeEnum;
|
import ast.type.type.TypeEnum;
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
import org.junit.jupiter.api.DisplayName;
|
import org.junit.jupiter.api.DisplayName;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import parser.astBuilder.ASTBuilder;
|
||||||
|
import parser.generated.SimpleJavaLexer;
|
||||||
|
import parser.generated.SimpleJavaParser;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.lang.reflect.Member;
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
@@ -329,7 +343,7 @@ class AstBuilderTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Self Reference Test")
|
@DisplayName("Self Reference Test")
|
||||||
public void selfReferenceTest() {
|
public void selfReferneceTest() {
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
45
src/test/java/semantic/BeginnToTAST.java
Normal file
45
src/test/java/semantic/BeginnToTAST.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package semantic;
|
||||||
|
|
||||||
|
import ast.ProgramNode;
|
||||||
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import parser.astBuilder.ASTBuilder;
|
||||||
|
import parser.generated.SimpleJavaLexer;
|
||||||
|
import parser.generated.SimpleJavaParser;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Paths;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class BeginnToTAST {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void FieldTests() {
|
||||||
|
SemanticAnalyzer.clearAnalyzer();
|
||||||
|
|
||||||
|
CharStream codeCharStream = null;
|
||||||
|
try {
|
||||||
|
codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/johnsTests/FieldTests.java"));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||||
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
|
|
||||||
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
|
ParseTree parseTree = parser.program(); // parse the input
|
||||||
|
|
||||||
|
/* ------------------------- AST builder -> AST ------------------------- */
|
||||||
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
|
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
||||||
|
|
||||||
|
var result = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||||
|
|
||||||
|
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,443 +1,5 @@
|
|||||||
package semantic;
|
package semantic;
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.ClassNode;
|
|
||||||
import ast.ProgramNode;
|
|
||||||
import ast.expressions.IExpressionNode;
|
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
|
||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
|
||||||
import ast.members.ConstructorNode;
|
|
||||||
import ast.members.FieldNode;
|
|
||||||
import ast.members.MethodNode;
|
|
||||||
import ast.parameters.ParameterNode;
|
|
||||||
import ast.statementexpressions.AssignNode;
|
|
||||||
import ast.statementexpressions.AssignableNode;
|
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
|
||||||
import ast.statements.BlockNode;
|
|
||||||
import ast.statements.ReturnNode;
|
|
||||||
import ast.type.AccessModifierNode;
|
|
||||||
import ast.type.EnumValueNode;
|
|
||||||
import ast.type.ValueNode;
|
|
||||||
import ast.type.type.BaseType;
|
|
||||||
import ast.type.type.TypeEnum;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.DisplayName;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import parser.Helper;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
||||||
|
|
||||||
public class SemanticTest {
|
public class SemanticTest {
|
||||||
|
|
||||||
private final static String directoryPath = "src/test/resources/input/singleFeatureTests/";
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setUp() {
|
|
||||||
SemanticAnalyzer.clearAnalyzer();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Empty Class Test")
|
|
||||||
public void emptyClassTest() {
|
|
||||||
ClassNode emptyClass = Helper.generateEmptyClass("EmptyClass");
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(emptyClass);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Multiple Empty Classes Test")
|
|
||||||
public void multipleEmptyClassesTest() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("MultipleClasses");
|
|
||||||
ClassNode class2 = Helper.generateEmptyClass("TestClass2");
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
abstractSyntaxTree.addClass(class2);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Empty Class Test with Constructor")
|
|
||||||
public void emptyClassWithConstructorTest() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("EmptyClassWithConstructor");
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Field Test")
|
|
||||||
public void fieldTest() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("Field");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Field Test with Accessmodifier")
|
|
||||||
public void fieldTestWithModifier() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("FieldWithAccessModifier");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Comments Ignore Test")
|
|
||||||
public void commentsIgnoreTest() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("Comments");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Constructor Parameter Test")
|
|
||||||
public void constructorParameterTest() {
|
|
||||||
BlockNode block = new BlockNode();
|
|
||||||
block.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorParameter", block);
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "ConstructorParameter");
|
|
||||||
class1.addMember(constructor);
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("This Dot Test")
|
|
||||||
public void thisDotTest() {
|
|
||||||
BlockNode block = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
ValueNode value = new ValueNode(EnumValueNode.INT_VALUE, "1");
|
|
||||||
IExpressionNode expression = new UnaryNode(value);
|
|
||||||
|
|
||||||
block.addStatement(new AssignNode(assignable, expression));
|
|
||||||
block.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "ThisDot", block);
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "ThisDot");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Constructor This Dot Test")
|
|
||||||
public void constructorThisDotTest() {
|
|
||||||
BlockNode block = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
IExpressionNode expression = new UnaryNode("a");
|
|
||||||
|
|
||||||
block.addStatement(new AssignNode(assignable, expression));
|
|
||||||
block.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorThisDot", block);
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "ConstructorThisDot");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Void Methoden Test")
|
|
||||||
public void voidMethodenTest() {
|
|
||||||
ClassNode class1 = Helper.generateEmptyClass("VoidMethod");
|
|
||||||
BlockNode block = new BlockNode();
|
|
||||||
block.addStatement(new ReturnNode(null));
|
|
||||||
class1.addMember(new MethodNode("public", null, true, "test", block));
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Constructor Method call Test")
|
|
||||||
public void constructorMethodCallTest() {
|
|
||||||
BlockNode blockCon = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
IExpressionNode expression = new UnaryNode(new MethodCallNode(null, "testMethod"));
|
|
||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorMethodCall", blockCon);
|
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
|
||||||
blockMethod.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))));
|
|
||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "ConstructorMethodCall");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
class1.addMember(method);
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Constructor Method call Parameters Test")
|
|
||||||
public void constructorMethodCallParametersTest() {
|
|
||||||
BlockNode blockCon = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
MethodCallNode methodCall = new MethodCallNode(null, "testMethod");
|
|
||||||
methodCall.addExpression(new UnaryNode("a"));
|
|
||||||
IExpressionNode expression = new UnaryNode(methodCall);
|
|
||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorMethodCallParameters", blockCon);
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
|
||||||
blockMethod.addStatement(new ReturnNode(new UnaryNode("a")));
|
|
||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
|
||||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "ConstructorMethodCallParameters");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
class1.addMember(method);
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Char Test")
|
|
||||||
public void charTest() {
|
|
||||||
BlockNode blockCon = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
MethodCallNode methodCall = new MethodCallNode(null, "testMethod");
|
|
||||||
methodCall.addExpression(new UnaryNode("a"));
|
|
||||||
IExpressionNode expression = new UnaryNode(methodCall);
|
|
||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "Char", blockCon);
|
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
|
||||||
blockMethod.addStatement(new ReturnNode(new UnaryNode("a")));
|
|
||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.CHAR), false, "testMethod", blockMethod);
|
|
||||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "Char");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.CHAR), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
class1.addMember(method);
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Null Test")
|
|
||||||
public void nullTest() {
|
|
||||||
BlockNode blockCon = new BlockNode();
|
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
|
||||||
memberAccess.addIdentifier("a");
|
|
||||||
|
|
||||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
|
||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
|
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "Null", blockCon);
|
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "Null");
|
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
class1.addMember(constructor);
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(class1);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Self Reference Test")
|
|
||||||
public void selfReferenceTest() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Variable Compare Test")
|
|
||||||
public void variableCompareTest() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Variable Calculation Test")
|
|
||||||
public void variableCalculationTest() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Main Method Test")
|
|
||||||
public void mainMethodTest() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("While Test")
|
|
||||||
public void whileTest() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Do While Test")
|
|
||||||
public void doWhileTest() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("For Test")
|
|
||||||
public void forTest() {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
@DisplayName("Increment Test")
|
|
||||||
public void incrementTest() {
|
|
||||||
ClassNode classNode = Helper.generateEmptyClass("Increment");
|
|
||||||
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
|
||||||
|
|
||||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
|
||||||
abstractSyntaxTree.addClass(classNode);
|
|
||||||
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
|
||||||
runtimeException.printStackTrace();
|
|
||||||
}
|
|
||||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
}
|
|
||||||
}
|
}
|
5
src/test/resources/input/johnsTests/FieldTests.java
Normal file
5
src/test/resources/input/johnsTests/FieldTests.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class Test{
|
||||||
|
|
||||||
|
int a = 10;
|
||||||
|
|
||||||
|
}
|
@@ -3,6 +3,6 @@ class Null{
|
|||||||
int a;
|
int a;
|
||||||
|
|
||||||
public Null(){
|
public Null(){
|
||||||
this.a = null;
|
// this.a = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -0,0 +1,12 @@
|
|||||||
|
// @expected: NotDeclaredException
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
public House1 h;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class House {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@@ -0,0 +1,29 @@
|
|||||||
|
public class Test {
|
||||||
|
|
||||||
|
public House h;
|
||||||
|
|
||||||
|
public int test(House h){
|
||||||
|
return h.getW().getSize();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class House {
|
||||||
|
|
||||||
|
private Window w;
|
||||||
|
|
||||||
|
public Window getW(){
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Window{
|
||||||
|
|
||||||
|
private int size;
|
||||||
|
|
||||||
|
public int getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user