added som more checks
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Bruder John 2024-07-01 15:45:57 +02:00
parent 77fecfa476
commit 2d455ba197
7 changed files with 225 additions and 93 deletions

View File

@ -6,6 +6,7 @@ import ast.type.AccessModifierNode;
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 { public class ConstructorNode extends MethodNode {
public AccessModifierNode accessType; public AccessModifierNode accessType;
@ -23,4 +24,18 @@ public class ConstructorNode extends MethodNode {
parameters.add(parameterNode); parameters.add(parameterNode);
} }
@Override
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

@ -3,7 +3,6 @@ package ast.members;
import ast.statements.BlockNode; import ast.statements.BlockNode;
public class MainMethodNode extends MethodNode { public class MainMethodNode extends MethodNode {
public BlockNode block;
public MainMethodNode(BlockNode block) { public MainMethodNode(BlockNode block) {
this.block = block; this.block = block;

View File

@ -6,8 +6,8 @@ import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
public class WhileNode implements IStatementNode { public class WhileNode implements IStatementNode {
IExpressionNode expression; public IExpressionNode expression;
BlockNode block; public BlockNode block;
public WhileNode(IExpressionNode expression, BlockNode block) { public WhileNode(IExpressionNode expression, BlockNode block) {
this.expression = expression; this.expression = expression;
@ -21,6 +21,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

@ -140,18 +140,14 @@ 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);
@ -181,8 +177,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
toCheck.block.accept(this); toCheck.block.accept(this);
var resultExpression = toCheck.expression.accept(this); var resultExpression = toCheck.expression.accept(this);
if(resultExpression.isValid()){ if (resultExpression.isValid()) {
if(!resultExpression.getType().equals(new BaseType(TypeEnum.BOOL))){ if (!resultExpression.getType().equals(new BaseType(TypeEnum.BOOL))) {
errors.add(new TypeMismatchException("Expression must be Boolean")); errors.add(new TypeMismatchException("Expression must be Boolean"));
return new TypeCheckResult(false, new BaseType(TypeEnum.VOID)); return new TypeCheckResult(false, new BaseType(TypeEnum.VOID));
} }
@ -195,8 +191,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
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);
if(result.isValid()){ if (result.isValid()) {
if(!result.getType().equals(currentMethodReturnType)){ if (!result.getType().equals(currentMethodReturnType)) {
errors.add(new TypeMismatchException("Mismatched return Type from method")); errors.add(new TypeMismatchException("Mismatched return Type from method"));
} }
} }
@ -210,7 +206,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
@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
@ -221,25 +219,30 @@ public class SemanticAnalyzer implements SemanticVisitor {
@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(!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)){
if (result.getType() != null) { if (result.getType() != null) {
if (blockReturnType == null) { if (blockReturnType == null) {
blockReturnType = result.getType(); blockReturnType = result.getType();
} else { } else {
if(!blockReturnType.equals(result.getType())) { 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) {
if(toCheck.memberAccess != null){ if (toCheck.memberAccess != null) {
var result = toCheck.memberAccess.accept(this); var result = toCheck.memberAccess.accept(this);
toCheck.setTypeNode(result.getType()); toCheck.setTypeNode(result.getType());
return result; return result;
@ -260,8 +263,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(ElseNode toCheck) { public TypeCheckResult analyze(ElseNode toCheck) {
toCheck.block.accept(this); return toCheck.block.accept(this);
return null;
} }
@Override @Override
@ -308,10 +310,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(IfElseNode toCheck) { public TypeCheckResult analyze(IfElseNode toCheck) {
var resultIf = toCheck.ifStatement.accept(this); var resultIf = toCheck.ifStatement.accept(this);
var resultElse = toCheck.elseStatement.accept(this); var resultElse = toCheck.elseStatement.accept(this);
return new TypeCheckResult(true, new BaseType(TypeEnum.VOID)); return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID));
} }
@Override @Override
@ -324,17 +325,17 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
if (targetType instanceof ReferenceType reference) { if (targetType instanceof ReferenceType reference) {
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);
}else { } else {
return new TypeCheckResult(false, null); return new TypeCheckResult(false, null);
} }
} }
} else { } else {
if(toCheck.target.thisTar){ if (toCheck.target.thisTar) {
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier)); var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
if(type != null){ if (type != null) {
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} }
} else { } else {
@ -355,7 +356,7 @@ 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){ if (result.getType() != null) {
resultType = result.getType(); resultType = result.getType();
} }
valid = result.isValid() && valid; valid = result.isValid() && valid;
@ -400,33 +401,89 @@ 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 nonCalculationNode) { public TypeCheckResult analyze(NonCalculationNode nonCalculationNode) {
var expResult = nonCalculationNode.expression.accept(this); var expResult = nonCalculationNode.expression.accept(this);
var unaryResult = nonCalculationNode.unaryExpression.accept(this); var unaryResult = nonCalculationNode.unaryExpression.accept(this);
if(Objects.equals(expResult.getType(), unaryResult.getType())){ switch (nonCalculationNode.operator) {
return new TypeCheckResult(expResult.isValid() && unaryResult.isValid(), expResult.getType()); 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 { } else {
errors.add(new TypeMismatchException("NonCalculation node " + nonCalculationNode.getType() + " does not match expression " + expResult.getType())); 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); return new TypeCheckResult(false, null);
} }
@ -434,7 +491,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
public TypeCheckResult analyze(UnaryNode unary) { public TypeCheckResult analyze(UnaryNode unary) {
var valid = true; var valid = true;
if(unary.identifier != null){ 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) {
@ -446,15 +503,18 @@ public class SemanticAnalyzer implements SemanticVisitor {
} else { } else {
errors.add(new NotDeclaredException("Var is not Declared")); errors.add(new NotDeclaredException("Var is not Declared"));
} }
} else if (unary.statement != null){ } else if (unary.statement != null) {
var result = unary.statement.accept(this); var result = unary.statement.accept(this);
return new TypeCheckResult(result.isValid(), result.getType()); return new TypeCheckResult(result.isValid(), result.getType());
} else if(unary.value != null){ } else if (unary.value != null) {
var result = unary.value.accept(this); var result = unary.value.accept(this);
return new TypeCheckResult(result.isValid(), result.getType()); return new TypeCheckResult(result.isValid(), result.getType());
} else if(unary.memberAccess != null){ } else if (unary.memberAccess != null) {
var result = unary.memberAccess.accept(this); var result = unary.memberAccess.accept(this);
return new TypeCheckResult(result.isValid(), result.getType()); 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); return new TypeCheckResult(false, null);
@ -478,15 +538,16 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
} else { } else {
if(currentType instanceof ReferenceType reference) { if (currentType instanceof ReferenceType reference) {
var currentTypeClass = context.getClass(reference.getIdentifier()); var currentTypeClass = context.getClass(reference.getIdentifier());
var currentField = currentTypeClass.getField(s); var currentField = currentTypeClass.getField(s);
if(currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC){ if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC) {
currentType = currentField.getType(); currentType = currentField.getType();
} else { } else {
errors.add(new NotVisibleException("This field is not visible")); errors.add(new NotVisibleException("This field is not visible"));
return new TypeCheckResult(false, null); } return new TypeCheckResult(false, null);
}
} }
} }
@ -507,7 +568,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(ValueNode valueNode) { public TypeCheckResult analyze(ValueNode valueNode) {
switch (valueNode.valueType){ switch (valueNode.valueType) {
case INT_VALUE -> { case INT_VALUE -> {
return new TypeCheckResult(true, new BaseType(TypeEnum.INT)); return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
} }
@ -532,22 +593,22 @@ public class SemanticAnalyzer implements SemanticVisitor {
var methods = classContext.getMethods(); var methods = classContext.getMethods();
for (var method : methods) { for (var method : methods) {
if (toCheck.identifier.equals(method.getIdentifier())) { if (toCheck.identifier.equals(method.getIdentifier())) {
if(method.getParameters().size() == toCheck.parameters.size() && !(method instanceof ConstructorNode)){ if (method.getParameters().size() == toCheck.parameters.size() && !(method instanceof ConstructorNode)) {
boolean same = true; boolean same = true;
for(int i = 0; i < method.getParameters().size(); i++){ for (int i = 0; i < method.getParameters().size(); i++) {
var result1 = method.getParameters().get(i).accept(this); var result1 = method.getParameters().get(i).accept(this);
var result2 = toCheck.parameters.get(i).accept(this); var result2 = toCheck.parameters.get(i).accept(this);
if (!Objects.equals(result1.getType(), result2.getType())) { if (!Objects.equals(result1.getType(), result2.getType())) {
same = false; same = false;
} }
} }
if(same){ if (same) {
if(method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC){ if (method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC) {
if(method.getType() == null){ if (method.getType() == null) {
return new BaseType(TypeEnum.VOID); return new BaseType(TypeEnum.VOID);
} }
return method.getType(); return method.getType();
}else { } else {
errors.add(new NotVisibleException("This Method is not Visible")); errors.add(new NotVisibleException("This Method is not Visible"));
} }
} }

View File

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

View File

@ -1,29 +1,38 @@
public class AllFeaturesClassExample {
int a;
boolean b;
char c;
public class Test { public void controlStructures(int adf, boolean bool) {
// if (a > (10 + 8)) {
// } else {
// }
//
//
// while (a > adf) {
// a--;
// }
public Car c; for (int i = 0; i < 5; i++) {
public int test(boolean b, int x) {
if (b == true) {
return c.getSpeed();
} else {
x++;
return x;
}
} }
}
// 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 Car {
private int speed;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}

View File

@ -1,4 +1,43 @@
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 class Test {
public Car c; public Car c;