Compare commits

..

18 Commits

Author SHA1 Message Date
22c3850062 Merge branch 'main' into code-generator
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 17:14:10 -04:00
3112660880 Merge pull request 'johns-branch' (#17) from johns-branch into main
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Reviewed-on: #17
Reviewed-by: i22035 <i22035@hb.dhbw-stuttgart.de>
2024-07-01 21:08:23 +00:00
Bruder John
71ffb8bb83 added for loops to the test
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 16:04:27 +02:00
Bruder John
2d455ba197 added som more checks
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 15:45:57 +02:00
Bruder John
77fecfa476 added Type to Assignable
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 13:25:27 +02:00
Bruder John
36e56fa66e added some tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 12:58:24 +02:00
Bruder John
f3e3158460 added private fields
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 12:58:11 +02:00
Bruder John
7b1e6fced4 Merge branch 'NewParser' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 09:35:01 +02:00
Bruder John
4d5c0017b4 Merge branch 'main' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-01 08:32:44 +02:00
Bruder John
4d21d8e94e fix method overlaoding semantic check
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
2024-06-27 15:17:05 +02:00
Bruder John
a34c7ded50 added Some Semantic Checks
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-27 15:04:01 +02:00
Bruder John
10eb17497e added some Semantic Tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-27 10:45:38 +02:00
Bruder John
576146c4fc Merge branch 'Tests' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-27 08:35:07 +02:00
Bruder John
bf365194be Merge branch 'NewParser' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-27 08:31:56 +02:00
Bruder John
35b3e9ee46 Added Some Semantice Checks
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-27 08:11:17 +02:00
Bruder John
8163d0b61e removing Semantic Tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-26 09:55:12 +02:00
Bruder John
92e1daab5b Merge branch 'NewParser' into johns-branch
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-06-26 09:51:33 +02:00
Bruder John
8cd22c6e4c Removed some Exercices from Tests because parser is not ready for
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
2024-06-23 20:57:53 +02:00
52 changed files with 829 additions and 234 deletions

View File

@ -8,4 +8,6 @@ public interface IExpressionNode extends ASTNode, Visitable {
ITypeNode getType(); ITypeNode getType();
void setType(ITypeNode type);
} }

View File

@ -9,6 +9,8 @@ import visitor.Visitable;
public class BinaryNode implements IExpressionNode, Visitable { public class BinaryNode implements IExpressionNode, Visitable {
private ITypeNode typeNode;
@Override @Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.analyze(this); return visitor.analyze(this);
@ -16,7 +18,12 @@ public class BinaryNode implements IExpressionNode, Visitable {
@Override @Override
public ITypeNode getType() { public ITypeNode getType() {
return null; return typeNode;
}
@Override
public void setType(ITypeNode type) {
this.typeNode = type;
} }
@Override @Override

View File

@ -9,6 +9,7 @@ public class CalculationNode extends BinaryNode {
public CalculationNode calculationExpression; public CalculationNode calculationExpression;
public EnumLineOperator operator; public EnumLineOperator operator;
public DotNode dotExpression; public DotNode dotExpression;
private ITypeNode typeNode;
public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) { public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) {
this.calculationExpression = calculationExpression; this.calculationExpression = calculationExpression;
@ -33,10 +34,6 @@ public class CalculationNode extends BinaryNode {
return visitor.analyze(this); return visitor.analyze(this);
} }
@Override
public ITypeNode getType() {
return null;
}
@Override @Override
public void accept(MethodVisitor methodVisitor) { public void accept(MethodVisitor methodVisitor) {

View File

@ -35,11 +35,6 @@ public class DotNode extends BinaryNode {
return visitor.analyze(this); return visitor.analyze(this);
} }
@Override
public ITypeNode getType() {
return null;
}
@Override @Override
public void accept(MethodVisitor methodVisitor) { public void accept(MethodVisitor methodVisitor) {
methodVisitor.visit(this); methodVisitor.visit(this);

View File

@ -37,11 +37,6 @@ public class DotSubstractionNode extends BinaryNode {
return visitor.analyze(this); return visitor.analyze(this);
} }
@Override
public ITypeNode getType() {
return null;
}
@Override @Override
public void accept(MethodVisitor methodVisitor) { public void accept(MethodVisitor methodVisitor) {
methodVisitor.visit(this); methodVisitor.visit(this);

View File

@ -43,11 +43,6 @@ public class NonCalculationNode extends BinaryNode {
return visitor.analyze(this); return visitor.analyze(this);
} }
@Override
public ITypeNode getType() {
return null;
}
@Override @Override
public void accept(MethodVisitor methodVisitor) { public void accept(MethodVisitor methodVisitor) {
methodVisitor.visit(this); methodVisitor.visit(this);

View File

@ -2,6 +2,7 @@ package ast.expressions.unaryexpressions;
import ast.ASTNode; import ast.ASTNode;
import bytecode.visitor.MethodVisitor; import bytecode.visitor.MethodVisitor;
import ast.type.type.ITypeNode;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
import visitor.Visitable; import visitor.Visitable;
@ -12,6 +13,7 @@ import java.util.List;
public class MemberAccessNode implements ASTNode, Visitable { public class MemberAccessNode implements ASTNode, Visitable {
public Boolean thisExpr; public Boolean thisExpr;
public List<String> identifiers = new ArrayList<>(); public List<String> identifiers = new ArrayList<>();
private ITypeNode typeNode;
public MemberAccessNode(Boolean thisExpr) { public MemberAccessNode(Boolean thisExpr) {
this.thisExpr = thisExpr; this.thisExpr = thisExpr;
@ -26,9 +28,16 @@ public class MemberAccessNode implements ASTNode, Visitable {
methodVisitor.visit(this); methodVisitor.visit(this);
} }
@Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return null; return visitor.analyze(this);
}
public ITypeNode getTypeNode() {
return typeNode;
}
public void setTypeNode(ITypeNode typeNode) {
this.typeNode = typeNode;
} }
} }

View File

@ -8,6 +8,7 @@ import visitor.Visitable;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Objects;
public class ConstructorNode extends MethodNode implements Visitable { public class ConstructorNode extends MethodNode implements Visitable {
public AccessModifierNode accessType; public AccessModifierNode accessType;
@ -30,4 +31,17 @@ public class ConstructorNode extends MethodNode implements Visitable {
methodVisitor.visit(this); methodVisitor.visit(this);
} }
public boolean isSame(MethodNode methodNode) {
if (!(Objects.equals(this.identifier, methodNode.getIdentifier()))
|| getParameters().size() != methodNode.getParameters().size()) {
return false;
}
for (int i = 0; i < this.getParameters().size(); i++) {
if (!this.getParameters().get(i).type.equals(methodNode.getParameters().get(i).type)) {
return false;
}
}
return true;
}
} }

View File

@ -47,7 +47,7 @@ public class MethodNode implements MemberNode, Visitable {
} }
for (int i = 0; i < this.getParameters().size(); i++) { for (int i = 0; i < this.getParameters().size(); i++) {
if (this.getParameters().get(i).type.equals(methodNode.getParameters().get(i).type)) { if (!this.getParameters().get(i).type.equals(methodNode.getParameters().get(i).type)) {
return false; return false;
} }
} }

View File

@ -1,12 +1,13 @@
package ast.statementexpressions; package ast.statementexpressions;
import ast.expressions.unaryexpressions.MemberAccessNode; import ast.expressions.unaryexpressions.MemberAccessNode;
import bytecode.visitor.MethodVisitor; import ast.type.type.ITypeNode;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
public class AssignableNode implements IStatementExpressionNode { public class AssignableNode implements IStatementExpressionNode {
public String identifier; public String identifier;
private ITypeNode typeNode;
public MemberAccessNode memberAccess; public MemberAccessNode memberAccess;
@ -23,4 +24,12 @@ public class AssignableNode implements IStatementExpressionNode {
return visitor.analyze(this); return visitor.analyze(this);
} }
public ITypeNode getTypeNode() {
return typeNode;
}
public void setTypeNode(ITypeNode typeNode) {
this.typeNode = typeNode;
}
} }

View File

@ -35,8 +35,9 @@ public class TargetNode implements ASTNode, Visitable {
methodVisitor.visit(this); methodVisitor.visit(this);
} }
@Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return null; return visitor.analyze(this);
} }
} }

View File

@ -13,6 +13,6 @@ public class ElseNode implements IStatementNode {
@Override @Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return null; return visitor.analyze(this);
} }
} }

View File

@ -22,6 +22,6 @@ public class IfElseNode implements IStatementNode {
@Override @Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return null; return visitor.analyze(this);
} }
} }

View File

@ -15,6 +15,6 @@ public class IfNode implements IStatementNode {
@Override @Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return null; return visitor.analyze(this);
} }
} }

View File

@ -20,6 +20,6 @@ public class WhileNode implements IStatementNode {
@Override @Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return null; return visitor.analyze(this);
} }
} }

View File

@ -22,6 +22,6 @@ public class ValueNode implements ASTNode, Visitable {
@Override @Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return null; return visitor.analyze(this);
} }
} }

View File

@ -9,6 +9,7 @@ import java.util.Objects;
import ast.*; import ast.*;
import ast.expressions.IExpressionNode; import ast.expressions.IExpressionNode;
import ast.expressions.binaryexpressions.*; import ast.expressions.binaryexpressions.*;
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;
@ -21,8 +22,13 @@ 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.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
import ast.statements.*; import ast.statements.*;
import ast.type.AccessModifierNode;
import ast.type.EnumAccessModifierNode;
import ast.type.ValueNode;
import ast.type.type.*; import ast.type.type.*;
import com.sun.jdi.IntegerType;
import semantic.context.Context; import semantic.context.Context;
import semantic.exceptions.*; import semantic.exceptions.*;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
@ -134,26 +140,20 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
} }
// Check if this method is already declared
currentMethodReturnType = methodNode.getType(); currentMethodReturnType = methodNode.getType();
currentNullType = currentMethodReturnType; // Solange nicht in einem Assign oder Methoden-Aufruf dieser Typ currentNullType = currentMethodReturnType;
ITypeNode resultType = new BaseType(TypeEnum.VOID);
// gesetzt ist, ist dieser der Rückgabewert der Methode
var result = methodNode.block.accept(this); var result = methodNode.block.accept(this);
valid = valid && result.isValid(); valid = valid && result.isValid();
currentScope.popScope(); currentScope.popScope();
resultType = result.getType(); ITypeNode resultType = result.getType();
if (resultType == null) { if (resultType == null) {
resultType = new BaseType(TypeEnum.VOID); resultType = new BaseType(TypeEnum.VOID);
} }
if (!resultType.equals(methodNode.getType())) { if (methodNode.getType() == null) {
errors.add(new TypeMismatchException("Method-Declaration " + methodNode.getIdentifier() + " with type " methodNode.setType(new BaseType(TypeEnum.VOID));
+ methodNode.getType() + " has at least one Mismatching return Type:"));
valid = false;
} }
return new TypeCheckResult(valid, resultType); return new TypeCheckResult(valid, resultType);
@ -174,65 +174,98 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(IfNode toCheck) { public TypeCheckResult analyze(IfNode toCheck) {
toCheck.block.accept(this);
var resultExpression = toCheck.expression.accept(this);
if (resultExpression.isValid()) {
if (!resultExpression.getType().equals(new BaseType(TypeEnum.BOOL))) {
errors.add(new TypeMismatchException("Expression must be Boolean"));
return new TypeCheckResult(false, new BaseType(TypeEnum.VOID));
}
}
return new TypeCheckResult(true, null); return new TypeCheckResult(true, null);
} }
@Override @Override
public TypeCheckResult analyze(ReturnNode toCheck) { public TypeCheckResult analyze(ReturnNode toCheck) {
if (toCheck.expression != null) {
if(toCheck.expression != null){
var result = toCheck.expression.accept(this); var result = toCheck.expression.accept(this);
return new TypeCheckResult(true, result.getType()); if (result.isValid()) {
} else { if (!result.getType().equals(currentMethodReturnType)) {
return new TypeCheckResult(false, null); errors.add(new TypeMismatchException("Mismatched return Type from method"));
} }
}
return new TypeCheckResult(true, result.getType());
} else if (toCheck.voidReturn) {
return new TypeCheckResult(true, new BaseType(TypeEnum.VOID));
}
return new TypeCheckResult(true, null);
} }
@Override @Override
public TypeCheckResult analyze(WhileNode toCheck) { public TypeCheckResult analyze(WhileNode toCheck) {
return null; var expResult = toCheck.expression.accept(this);
var blockRes = toCheck.block.accept(this);
return new TypeCheckResult(expResult.isValid() && blockRes.isValid(), blockRes.getType());
} }
@Override @Override
public TypeCheckResult analyze(ParameterNode toCheck) { public TypeCheckResult analyze(ParameterNode toCheck) {
return new TypeCheckResult(true, toCheck.type);
return new TypeCheckResult(true, null);
} }
@Override @Override
public TypeCheckResult analyze(BlockNode blockNode) { public TypeCheckResult analyze(BlockNode blockNode) {
ITypeNode blockReturnType = null; ITypeNode blockReturnType = null;
if (blockNode.statements.isEmpty()) {
return new TypeCheckResult(true, null);
}
for (IStatementNode statementNode : blockNode.statements) { for (IStatementNode statementNode : blockNode.statements) {
var result = statementNode.accept(this); var result = statementNode.accept(this);
if(result.getType() != null){ if(!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)){
if(blockReturnType == null){ if (result.getType() != null) {
if (blockReturnType == null) {
blockReturnType = result.getType(); blockReturnType = result.getType();
} else { } else {
if (!blockReturnType.equals(result.getType())) {
errors.add(new MultipleReturnTypes("There are multiple Return types")); errors.add(new MultipleReturnTypes("There are multiple Return types"));
} }
} }
} }
}
}
return new TypeCheckResult(true, blockReturnType); return new TypeCheckResult(true, blockReturnType);
} }
@Override @Override
public TypeCheckResult analyze(AssignableNode toCheck) { public TypeCheckResult analyze(AssignableNode toCheck) {
return new TypeCheckResult(true, currentFields.get(toCheck.identifier));
if (toCheck.memberAccess != null) {
var result = toCheck.memberAccess.accept(this);
toCheck.setTypeNode(result.getType());
return result;
} else {
if (currentFields.get(toCheck.identifier) != null) {
var type = currentFields.get(toCheck.identifier);
toCheck.setTypeNode(type);
return new TypeCheckResult(true, type);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
var type = currentScope.getLocalVar(toCheck.identifier);
return new TypeCheckResult(true, type);
}
}
return new TypeCheckResult(true, null);
} }
@Override @Override
public TypeCheckResult analyze(ElseNode toCheck) { public TypeCheckResult analyze(ElseNode toCheck) {
return null; return toCheck.block.accept(this);
} }
/*@Override
public TypeCheckResult analyze(ForNode toCheck) {
return null;
}*/
@Override @Override
public TypeCheckResult analyze(AssignNode toCheck) { public TypeCheckResult analyze(AssignNode toCheck) {
AssignableNode assignable = toCheck.assignable; AssignableNode assignable = toCheck.assignable;
@ -258,33 +291,61 @@ public class SemanticAnalyzer implements SemanticVisitor {
currentNullType = lResult.getType(); currentNullType = lResult.getType();
var rResult = rExpression.accept(this); var rResult = rExpression.accept(this);
if (!Objects.equals(currentScope.getLocalVar(toCheck.assignable.identifier), rExpression.getType())) { if (!Objects.equals(lResult.getType(), rResult.getType())) {
errors.add(new TypeMismatchException( errors.add(new TypeMismatchException(
"Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \"" "Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \""
+ rResult.getType() + "\"")); + rResult.getType() + "\""));
valid = false; valid = false;
} }
// else {
// toCheck.setType(assignable.getType());
// }
valid = valid && lResult.isValid() && rResult.isValid(); valid = valid && lResult.isValid() && rResult.isValid();
currentNullType = null; currentNullType = null;
return new TypeCheckResult(valid, null); // return type is null to get the return type sufficently return new TypeCheckResult(valid, null); // return type is null to get the return type sufficently
} }
@Override @Override
public TypeCheckResult analyze(DecrementNode toCheck) { public TypeCheckResult analyze(DecrementNode decrementNode) {
return null; return decrementNode.assignableExpression.accept(this);
} }
@Override @Override
public TypeCheckResult analyze(IfElseNode toCheck) { public TypeCheckResult analyze(IfElseNode toCheck) {
return new TypeCheckResult(true, null); var resultIf = toCheck.ifStatement.accept(this);
var resultElse = toCheck.elseStatement.accept(this);
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID));
} }
@Override @Override
public TypeCheckResult analyze(MethodCallNode toCheck) { public TypeCheckResult analyze(MethodCallNode toCheck) {
return null;
if (toCheck.target.identifier != null) {
var targetType = currentScope.getLocalVar(toCheck.target.identifier);
if (targetType == null) {
targetType = currentFields.get(toCheck.target.identifier);
}
if (targetType instanceof ReferenceType reference) {
var type = getTypeFromMethod(toCheck, reference);
if (type != null) {
return new TypeCheckResult(true, type);
} else {
return new TypeCheckResult(false, null);
}
}
} else {
if (toCheck.target.thisTar) {
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
if (type != null) {
return new TypeCheckResult(true, type);
}
} else {
var result = toCheck.target.accept(this);
if (result.getType() instanceof ReferenceType reference) {
return new TypeCheckResult(true, getTypeFromMethod(toCheck, reference));
}
}
}
return new TypeCheckResult(false, null);
} }
@Override @Override
@ -295,6 +356,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
TypeCheckResult result = localVarDecl.expression.accept(this); TypeCheckResult result = localVarDecl.expression.accept(this);
var resultType = localVarDecl.expression.getType(); var resultType = localVarDecl.expression.getType();
if (result.getType() != null) {
resultType = result.getType();
}
valid = result.isValid() && valid; valid = result.isValid() && valid;
if (!Objects.equals(resultType, localVarDecl.type)) { if (!Objects.equals(resultType, localVarDecl.type)) {
@ -316,12 +380,17 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(NewDeclarationNode toCheck) { public TypeCheckResult analyze(NewDeclarationNode toCheck) {
if (context.containsClass(toCheck.identifier)) {
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
}
return null; return null;
} }
@Override @Override
public TypeCheckResult analyze(IncrementNode toCheck) { public TypeCheckResult analyze(IncrementNode toCheck) {
return null; return toCheck.assignableExpression.accept(this);
} }
@Override @Override
@ -332,38 +401,223 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(CalculationNode calcNode) { public TypeCheckResult analyze(CalculationNode calcNode) {
if (calcNode.calculationExpression != null) { if (calcNode.calculationExpression != null) {
calcNode.calculationExpression.accept(this); var calcRes = calcNode.calculationExpression.accept(this);
if (calcNode.dotExpression != null) {
var dotRes = calcNode.dotExpression.accept(this);
switch (calcNode.operator) {
case PLUS, MINUS:
if (calcRes.getType() instanceof BaseType calcType && dotRes.getType() instanceof BaseType dotType &&
calcType.getTypeEnum().equals(TypeEnum.INT) && dotType.getTypeEnum().equals(TypeEnum.INT)) {
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
} }
return null; break;
default:
}
} else {
return new TypeCheckResult(calcRes.isValid(), calcRes.getType());
}
} else if (calcNode.dotExpression != null) {
var dotRes = calcNode.dotExpression.accept(this);
return new TypeCheckResult(dotRes.isValid(), dotRes.getType());
}
return new TypeCheckResult(false, null);
} }
@Override @Override
public TypeCheckResult analyze(DotNode toCheck) { public TypeCheckResult analyze(DotNode toCheck) {
return null; if (toCheck.dotSubstractionExpression != null) {
return toCheck.dotSubstractionExpression.accept(this);
}
return new TypeCheckResult(false, null);
} }
@Override @Override
public TypeCheckResult analyze(DotSubstractionNode toCheck) { public TypeCheckResult analyze(DotSubstractionNode toCheck) {
if (toCheck.value != null) {
return toCheck.value.accept(this);
} else if (toCheck.memberAccess != null) {
return toCheck.memberAccess.accept(this);
} else if (toCheck.methodCall != null) {
return toCheck.methodCall.accept(this);
} else if (toCheck.identifier != null) {
if (currentScope.contains(toCheck.identifier)) {
return new TypeCheckResult(true, currentScope.getLocalVar(toCheck.identifier));
} else if (currentFields.get(toCheck.identifier) != null) {
return new TypeCheckResult(true, currentFields.get(toCheck.identifier));
}
} else if (toCheck.calculationExpression != null) {
return toCheck.calculationExpression.accept(this);
}
return null; return null;
} }
@Override @Override
public TypeCheckResult analyze(NonCalculationNode toCheck) { public TypeCheckResult analyze(NonCalculationNode nonCalculationNode) {
return null; var expResult = nonCalculationNode.expression.accept(this);
var unaryResult = nonCalculationNode.unaryExpression.accept(this);
switch (nonCalculationNode.operator) {
case LESS, LESS_EQUAL, GREATER, GREATER_EQUAL:
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
} else {
errors.add(new TypeMismatchException("Both types must be Integer"));
}
break;
case OR, AND:
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
} else {
errors.add(new TypeMismatchException("Both types must be Boolean"));
}
break;
case EQUAL, NOT_EQUAL:
if (expResult.getType() instanceof BaseType expResultType && unaryResult.getType() instanceof BaseType unaryResultType
&& Objects.equals(expResultType, unaryResultType)) {
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
} else {
errors.add(new TypeMismatchException("Both types must be the same"));
}
}
return new TypeCheckResult(false, null);
} }
@Override @Override
public TypeCheckResult analyze(UnaryNode unary) { public TypeCheckResult analyze(UnaryNode unary) {
var valid = true; var valid = true;
if (unary.identifier != null) {
if (currentScope.contains(unary.identifier)) { if (currentScope.contains(unary.identifier)) {
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier)); return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
} else if(currentFields.get(unary.identifier) != null) { } else if (currentFields.get(unary.identifier) != null) {
return new TypeCheckResult(valid, currentFields.get(unary.identifier)); return new TypeCheckResult(valid, currentFields.get(unary.identifier));
} else if (unary.statement != null) {
var result = unary.statement.accept(this);
unary.setType(result.getType());
return result;
} else { } else {
errors.add(new NotDeclaredException("Var is not Declared")); errors.add(new NotDeclaredException("Var is not Declared"));
} }
return new TypeCheckResult(valid, null); } else if (unary.statement != null) {
var result = unary.statement.accept(this);
return new TypeCheckResult(result.isValid(), result.getType());
} else if (unary.value != null) {
var result = unary.value.accept(this);
return new TypeCheckResult(result.isValid(), result.getType());
} else if (unary.memberAccess != null) {
var result = unary.memberAccess.accept(this);
return new TypeCheckResult(result.isValid(), result.getType());
} else if (unary.expression != null) {
var result = unary.expression.accept(this);
return new TypeCheckResult(result.isValid(), result.getType());
}
return new TypeCheckResult(false, null);
}
@Override
public TypeCheckResult analyze(MemberAccessNode memberAccessNode) {
ITypeNode currentType = null;
for (String s : memberAccessNode.identifiers) {
if (currentType == null) {
if (currentScope.getLocalVar(s) != null) {
currentType = currentScope.getLocalVar(s);
} else if (currentFields.get(s) != null) {
currentType = currentFields.get(s);
} else {
errors.add(new NotDeclaredException(s + "Not Declared"));
return new TypeCheckResult(false, null);
}
} else {
if (currentType instanceof ReferenceType reference) {
var currentTypeClass = context.getClass(reference.getIdentifier());
var currentField = currentTypeClass.getField(s);
if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC) {
currentType = currentField.getType();
} else {
errors.add(new NotVisibleException("This field is not visible"));
return new TypeCheckResult(false, null);
}
}
}
}
return new TypeCheckResult(true, currentType);
}
@Override
public TypeCheckResult analyze(TargetNode targetNode) {
if (targetNode.memberAccess != null) {
return targetNode.memberAccess.accept(this);
}
return null;
}
@Override
public TypeCheckResult analyze(ValueNode valueNode) {
switch (valueNode.valueType) {
case INT_VALUE -> {
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
}
case CHAR_VALUE -> {
return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR));
}
case BOOLEAN_VALUE -> {
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
}
default -> {
return new TypeCheckResult(false, null);
}
}
}
private ITypeNode getTypeFromMethod(MethodCallNode toCheck, ReferenceType reference) {
var classContext = context.getClass(reference.getIdentifier());
if (classContext == null) {
errors.add(new NotDeclaredException(toCheck.target.identifier + "is not Defined"));
} else {
var methods = classContext.getMethods();
for (var method : methods) {
if (toCheck.identifier.equals(method.getIdentifier())) {
if (method.getParameters().size() == toCheck.parameters.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.parameters.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"));
}
}
}
}
}
errors.add(new WrongOverloading("No Method found with this parameters"));
}
return null;
} }
} }

View File

@ -2,6 +2,7 @@ package semantic;
import ast.*; import ast.*;
import ast.expressions.binaryexpressions.*; import ast.expressions.binaryexpressions.*;
import ast.expressions.unaryexpressions.MemberAccessNode;
import ast.expressions.unaryexpressions.UnaryNode; import ast.expressions.unaryexpressions.UnaryNode;
import ast.members.*; import ast.members.*;
import ast.parameters.ParameterNode; import ast.parameters.ParameterNode;
@ -11,7 +12,9 @@ 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.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
import ast.statements.*; import ast.statements.*;
import ast.type.ValueNode;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
public interface SemanticVisitor { public interface SemanticVisitor {
@ -38,8 +41,6 @@ public interface SemanticVisitor {
TypeCheckResult analyze(ElseNode toCheck); TypeCheckResult analyze(ElseNode toCheck);
//TypeCheckResult analyze(ForNode toCheck);
TypeCheckResult analyze(AssignNode toCheck); TypeCheckResult analyze(AssignNode toCheck);
TypeCheckResult analyze(DecrementNode toCheck); TypeCheckResult analyze(DecrementNode toCheck);
@ -66,4 +67,10 @@ public interface SemanticVisitor {
TypeCheckResult analyze(UnaryNode toCheck); TypeCheckResult analyze(UnaryNode toCheck);
TypeCheckResult analyze(MemberAccessNode toCheck);
TypeCheckResult analyze(TargetNode toCheck);
TypeCheckResult analyze(ValueNode toCheck);
} }

View File

@ -2,11 +2,15 @@ package semantic.context;
import ast.ClassNode; import ast.ClassNode;
import ast.members.FieldNode; import ast.members.FieldNode;
import ast.members.MethodNode;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
public class ClassContext { public class ClassContext {
private HashMap<String, FieldContext> fields; private HashMap<String, FieldContext> fields;
private ArrayList<MethodNode> methods = new ArrayList<>();
public ClassContext(ClassNode classNode) { public ClassContext(ClassNode classNode) {
@ -15,6 +19,8 @@ public class ClassContext {
classNode.members.forEach(member -> { classNode.members.forEach(member -> {
if(member instanceof FieldNode fieldNode) { if(member instanceof FieldNode fieldNode) {
fields.put(fieldNode.identifier, new FieldContext(fieldNode)); fields.put(fieldNode.identifier, new FieldContext(fieldNode));
}else if(member instanceof MethodNode methodNode) {
methods.add(methodNode);
} }
}); });
@ -24,4 +30,8 @@ public class ClassContext {
return fields.get(name); return fields.get(name);
} }
public ArrayList<MethodNode> getMethods() {
return methods;
}
} }

View File

@ -21,4 +21,8 @@ public class Context {
return classes.get(identifier); return classes.get(identifier);
} }
public boolean containsClass(String identifier) {
return classes.containsKey(identifier);
}
} }

View File

@ -18,4 +18,8 @@ public class FieldContext {
return type; return type;
} }
public AccessModifierNode getAccessModifier() {
return accessModifier;
}
} }

View File

@ -0,0 +1,9 @@
package semantic.exceptions;
public class NotVisibleException extends RuntimeException {
public NotVisibleException(String message) {
super(message);
}
}

View File

@ -0,0 +1,9 @@
package semantic.exceptions;
public class WrongOverloading extends RuntimeException {
public WrongOverloading(String message) {
super(message);
}
}

View File

@ -26,11 +26,37 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
public class EndToTypedAstTest { public class EndToTypedAstTest {
private static final Map<String, Class<?>> exceptionMap = new HashMap<>(); private static final Map<String, Class<?>> exceptionMap = new HashMap<>();
@Test
public void testOnlyOneFile() {
SemanticAnalyzer.clearAnalyzer();
CharStream codeCharStream = null;
try {
codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/typedAstFeaturesTests/CorrectTest.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());
}
@Test @Test
public void exceptionsTest() { public void exceptionsTest() {
String directoryPath = "src/test/resources/input/typedAstExceptionsTests"; String directoryPath = "src/test/resources/input/typedAstExceptionsTests";
@ -88,7 +114,7 @@ public class EndToTypedAstTest {
} }
@Test @Test
public void featureTest(){ public void featureTest() {
String directoryPath = "src/test/resources/input/typedAstFeaturesTests"; String directoryPath = "src/test/resources/input/typedAstFeaturesTests";
File folder = new File(directoryPath); File folder = new File(directoryPath);
if (folder.isDirectory()) { if (folder.isDirectory()) {
@ -115,6 +141,9 @@ public class EndToTypedAstTest {
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree); ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
System.out.println("Testing the file: " + file.getName()); System.out.println("Testing the file: " + file.getName());
for(Exception runtimeException : SemanticAnalyzer.errors){
runtimeException.printStackTrace();
}
assertTrue(SemanticAnalyzer.errors.isEmpty()); assertTrue(SemanticAnalyzer.errors.isEmpty());
assertNotNull(typedAst); assertNotNull(typedAst);
} }
@ -127,9 +156,8 @@ public class EndToTypedAstTest {
} }
// ------------------ Helpers ------------------ // ------------------ Helpers ------------------
/** /**
* This method is used to extract the expected exception from a given file. * This method is used to extract the expected exception from a given file.
* It reads the file line by line and uses a regular expression to match the expected exception annotation. * It reads the file line by line and uses a regular expression to match the expected exception annotation.
@ -167,7 +195,7 @@ public class EndToTypedAstTest {
* @return The Class object associated with the given exception name. * @return The Class object associated with the given exception name.
* @throws RuntimeException If the Class object for the given exception name is not found in the exceptionMap. * @throws RuntimeException If the Class object for the given exception name is not found in the exceptionMap.
*/ */
private Class<?> getExceptionClass(String exceptionName) { private Class<?> getExceptionClass(String exceptionName) {
System.out.println(exceptionName); System.out.println(exceptionName);
exceptionName = "semantic.exceptions." + exceptionName; exceptionName = "semantic.exceptions." + exceptionName;
System.out.println(exceptionName); System.out.println(exceptionName);
@ -176,7 +204,7 @@ private Class<?> getExceptionClass(String exceptionName) {
throw new RuntimeException("Exception class not found: " + exceptionName); throw new RuntimeException("Exception class not found: " + exceptionName);
} }
return exceptionClass; return exceptionClass;
} }
/** /**
* This method is used to load custom exceptions from a specified package. * This method is used to load custom exceptions from a specified package.

View File

@ -1,102 +0,0 @@
package semantic;
import ast.*;
import ast.members.FieldNode;
import ast.members.MethodNode;
import ast.parameters.ParameterNode;
import ast.type.AccessModifierNode;
import ast.type.type.*;
public class Mocker {
public static ASTNode mockTwoSameFields(){
ProgramNode p = new ProgramNode();
ClassNode c = new ClassNode();
c.identifier = "testClass";
FieldNode f1 = new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a");
c.members.add(f1);
FieldNode f2 = new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a");
c.members.add(f2);
p.classes.add(c);
return p;
}
public static ASTNode mockSimpleMethod(){
ProgramNode p = new ProgramNode();
ClassNode c = new ClassNode();
MethodNode methodNode = new MethodNode();
//Parameter
ParameterNode parameterNode = new ParameterNode(new BaseType(TypeEnum.INT), "a");
methodNode.addParameter(parameterNode);
//Statements
//Block
methodNode.block = new ast.statements.BlockNode();
c.members.add(methodNode);
p.classes.add(c);
return p;
}
public static ASTNode mockTwoSameMethods(){
ProgramNode p = new ProgramNode();
ClassNode c = new ClassNode();
MethodNode methodNode = new MethodNode();
methodNode.block = new ast.statements.BlockNode();
methodNode.setType(new BaseType(TypeEnum.INT));
methodNode.setIdentifier("testMethod");
c.members.add(methodNode);
MethodNode methodNode1 = new MethodNode();
methodNode1.block = new ast.statements.BlockNode();
methodNode1.setType(new BaseType(TypeEnum.INT));
methodNode1.setIdentifier("testMethod");
c.members.add(methodNode1);
p.classes.add(c);
return p;
}
public static ASTNode mockTwoDifferentMethods(){
ProgramNode p = new ProgramNode();
ClassNode c = new ClassNode();
MethodNode methodNode = new MethodNode();
methodNode.block = new ast.statements.BlockNode();
methodNode.setIdentifier("testMethod");
c.members.add(methodNode);
MethodNode methodNode1 = new MethodNode();
methodNode1.block = new ast.statements.BlockNode();
methodNode1.setIdentifier("testMethod1");
c.members.add(methodNode1);
p.classes.add(c);
return p;
}
}

View File

@ -3,6 +3,18 @@ package semantic;
public class SemanticTest { public class SemanticTest {
public void test(){
}
public void test(int a, boolean b){
}
public void test(boolean b, int a){
}
// @Test // @Test
// public void alreadyDeclaredLocalFieldVar() { // public void alreadyDeclaredLocalFieldVar() {
// ProgramNode programNode = new ProgramNode(); // ProgramNode programNode = new ProgramNode();

View File

@ -0,0 +1,9 @@
// @expected: TypeMismatchException
public class AllFeaturesClassExample {
public void controlStructures(int a, boolean bool) {
while (a > bool) {
a--;
}
}
}

View File

@ -0,0 +1,13 @@
// @expected: TypeMismatchException
public class Test{
public void test(boolean b){
if(b == 2){
} else {
}
}
}

View File

@ -0,0 +1,20 @@
// @expected: NotVisibleException
public class Test{
public Car c;
public int test(){
return c.speed;
}
}
public class Car{
private int speed;
public int getSpeed(){
return speed;
}
}

View File

@ -0,0 +1,14 @@
// @expected: TypeMismatchException
public class Test{
public void test(int x){
if(x){
} else {
}
}
}

View File

@ -0,0 +1,21 @@
// @expected: NotVisibleException
public class Test {
public int firstInt;
public Car ca;
public int speed(){
return ca.getSpeed();
}
}
public class Car{
private int speed;
private int getSpeed(){
return speed;
}
}

View File

@ -0,0 +1,19 @@
// @expected: WrongOverloading
public class Test{
public void test(int x){
if(this.get()){
} else {
}
}
public boolean b;
public boolean get(int c){
return b;
}
}

View File

@ -1,4 +1,4 @@
// @expected: MultipleReturnTypes // @expected: TypeMismatchException
public class Example { public class Example {
public static int testMethod(int x, char c){ public static int testMethod(int x, char c){

View File

@ -0,0 +1,22 @@
// @expected: TypeMismatchException
public class Test{
public int i;
public boolean b;
public int test(){
return this.test(i);
}
public void test(int a){
}
public int test(boolean bool){
int ret = 1;
return ret;
}
}

View File

@ -1,8 +0,0 @@
// @expected: TypeMismatchException
public class Example {
public static void testMethod(int x){
}
}

View File

@ -0,0 +1,20 @@
public class Test {
public int firstInt;
public Car ca;
public int speed(){
return ca.getSpeed();
}
}
public class Car{
private int speed;
public int getSpeed(){
return speed;
}
}

View File

@ -0,0 +1,19 @@
public class Test{
public Car c;
public int test(){
return c.getSpeed();
}
}
public class Car{
private int speed;
public int getSpeed(){
return speed;
}
}

View File

@ -0,0 +1,18 @@
public class Test{
public void test(int x){
if(this.get(x)){
} else {
}
}
public boolean b;
public boolean get(int c){
return b;
}
}

View File

@ -0,0 +1,12 @@
public class Test{
public void test(boolean b){
if(b == true){
} else {
}
}
}

View File

@ -1,18 +1,38 @@
public class Example { public class AllFeaturesClassExample {
int a;
public int a; boolean b;
char c;
public static int testMethod(int b, boolean bo){
a = b;
if(bo){
public void controlStructures(int adf, boolean bool) {
if (a > (10 + 8)) {
} else {
} }
return a;
while (a > adf) {
a--;
}
for (int i = 0; i < 5; i++) {
}
} }
public static void testMethod(int b){ // void logicalOperations() {
// Logische UND-Operation
// if (b && a > 5) {
// System.out.println("a ist größer als 5 und b ist wahr");
// }
} // Logische ODER-Operation
// if (b || a < 5) {
// System.out.println("b ist wahr oder a ist kleiner als 5");
// }
// }
// public static void main(String[] args) {
// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
// obj.controlStructures();
// }
} }

View File

@ -0,0 +1,64 @@
public class AllFeaturesClassExample {
int a;
boolean b;
char c;
public void controlStructures(int adf, boolean bool) {
if (a > (10 + 8)) {
} else {
}
while (a > adf) {
a--;
}
for (int i = 0; i < 5; i++) {
}
}
// void logicalOperations() {
// // Logische UND-Operation
// if (b && a > 5) {
//// System.out.println("a ist größer als 5 und b ist wahr");
// }
//
// // Logische ODER-Operation
// if (b || a < 5) {
//// System.out.println("b ist wahr oder a ist kleiner als 5");
// }
// }
// public static void main(String[] args) {
// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
// obj.controlStructures();
// }
}
public class Test {
public Car c;
public int test(boolean b, int x) {
if (b == true) {
return c.getSpeed();
} else {
return x;
}
}
}
public class Car {
private int speed;
public int getSpeed() {
return speed;
}
}

View File

@ -0,0 +1,13 @@
public class Car{
public void test(boolean boo){
if(boo){
} else {
}
}
}

View File

@ -0,0 +1,13 @@
public class Car{
public int getSpeed(boolean bool, int a, int b){
if(bool){
return a;
} else {
return b;
}
}
}

View File

@ -0,0 +1,21 @@
public class Test{
public int i;
public boolean b;
public int test(){
return this.test(b);
}
public void test(int a){
}
public int test(boolean bool){
int ret = 1;
return ret;
}
}

View File

@ -0,0 +1,13 @@
public class Car{
private int speed;
public int getSpeed(){
return speed;
}
public int test(){
return this.getSpeed();
}
}

View File

@ -0,0 +1,13 @@
public class Car{
private int speed;
public void getSpeed(boolean boo){
if(boo){
} else {
}
}
}