added som more checks
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
parent
77fecfa476
commit
2d455ba197
@ -6,6 +6,7 @@ import ast.type.AccessModifierNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class ConstructorNode extends MethodNode {
|
||||
public AccessModifierNode accessType;
|
||||
@ -23,4 +24,18 @@ public class ConstructorNode extends MethodNode {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -3,7 +3,6 @@ package ast.members;
|
||||
import ast.statements.BlockNode;
|
||||
|
||||
public class MainMethodNode extends MethodNode {
|
||||
public BlockNode block;
|
||||
|
||||
public MainMethodNode(BlockNode block) {
|
||||
this.block = block;
|
||||
|
@ -6,8 +6,8 @@ import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
|
||||
public class WhileNode implements IStatementNode {
|
||||
IExpressionNode expression;
|
||||
BlockNode block;
|
||||
public IExpressionNode expression;
|
||||
public BlockNode block;
|
||||
|
||||
public WhileNode(IExpressionNode expression, BlockNode block) {
|
||||
this.expression = expression;
|
||||
@ -21,6 +21,6 @@ public class WhileNode implements IStatementNode {
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
}
|
||||
|
@ -140,18 +140,14 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
|
||||
}
|
||||
// Check if this method is already declared
|
||||
|
||||
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);
|
||||
valid = valid && result.isValid();
|
||||
currentScope.popScope();
|
||||
resultType = result.getType();
|
||||
ITypeNode resultType = result.getType();
|
||||
|
||||
if (resultType == null) {
|
||||
resultType = new BaseType(TypeEnum.VOID);
|
||||
@ -181,8 +177,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
toCheck.block.accept(this);
|
||||
|
||||
var resultExpression = toCheck.expression.accept(this);
|
||||
if(resultExpression.isValid()){
|
||||
if(!resultExpression.getType().equals(new BaseType(TypeEnum.BOOL))){
|
||||
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));
|
||||
}
|
||||
@ -195,8 +191,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
public TypeCheckResult analyze(ReturnNode toCheck) {
|
||||
if (toCheck.expression != null) {
|
||||
var result = toCheck.expression.accept(this);
|
||||
if(result.isValid()){
|
||||
if(!result.getType().equals(currentMethodReturnType)){
|
||||
if (result.isValid()) {
|
||||
if (!result.getType().equals(currentMethodReturnType)) {
|
||||
errors.add(new TypeMismatchException("Mismatched return Type from method"));
|
||||
}
|
||||
}
|
||||
@ -210,7 +206,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
@Override
|
||||
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
|
||||
@ -221,25 +219,30 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(BlockNode blockNode) {
|
||||
ITypeNode blockReturnType = null;
|
||||
if (blockNode.statements.isEmpty()) {
|
||||
return new TypeCheckResult(true, null);
|
||||
}
|
||||
for (IStatementNode statementNode : blockNode.statements) {
|
||||
var result = statementNode.accept(this);
|
||||
if(!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)){
|
||||
if (result.getType() != null) {
|
||||
if (blockReturnType == null) {
|
||||
blockReturnType = result.getType();
|
||||
} else {
|
||||
if(!blockReturnType.equals(result.getType())) {
|
||||
if (!blockReturnType.equals(result.getType())) {
|
||||
errors.add(new MultipleReturnTypes("There are multiple Return types"));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TypeCheckResult(true, blockReturnType);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(AssignableNode toCheck) {
|
||||
|
||||
if(toCheck.memberAccess != null){
|
||||
if (toCheck.memberAccess != null) {
|
||||
var result = toCheck.memberAccess.accept(this);
|
||||
toCheck.setTypeNode(result.getType());
|
||||
return result;
|
||||
@ -260,8 +263,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(ElseNode toCheck) {
|
||||
toCheck.block.accept(this);
|
||||
return null;
|
||||
return toCheck.block.accept(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -308,10 +310,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(IfElseNode toCheck) {
|
||||
var resultIf = toCheck.ifStatement.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
|
||||
@ -324,17 +325,17 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
if (targetType instanceof ReferenceType reference) {
|
||||
var type = getTypeFromMethod(toCheck, reference);
|
||||
if(type != null){
|
||||
if (type != null) {
|
||||
return new TypeCheckResult(true, type);
|
||||
}else {
|
||||
} else {
|
||||
return new TypeCheckResult(false, null);
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
if(toCheck.target.thisTar){
|
||||
if (toCheck.target.thisTar) {
|
||||
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
|
||||
if(type != null){
|
||||
if (type != null) {
|
||||
return new TypeCheckResult(true, type);
|
||||
}
|
||||
} else {
|
||||
@ -355,7 +356,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
TypeCheckResult result = localVarDecl.expression.accept(this);
|
||||
|
||||
var resultType = localVarDecl.expression.getType();
|
||||
if(result.getType() != null){
|
||||
if (result.getType() != null) {
|
||||
resultType = result.getType();
|
||||
}
|
||||
valid = result.isValid() && valid;
|
||||
@ -400,33 +401,89 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(CalculationNode calcNode) {
|
||||
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
|
||||
public TypeCheckResult analyze(DotNode toCheck) {
|
||||
return null;
|
||||
if (toCheck.dotSubstractionExpression != null) {
|
||||
return toCheck.dotSubstractionExpression.accept(this);
|
||||
}
|
||||
return new TypeCheckResult(false, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(NonCalculationNode nonCalculationNode) {
|
||||
|
||||
var expResult = nonCalculationNode.expression.accept(this);
|
||||
var unaryResult = nonCalculationNode.unaryExpression.accept(this);
|
||||
|
||||
if(Objects.equals(expResult.getType(), unaryResult.getType())){
|
||||
return new TypeCheckResult(expResult.isValid() && unaryResult.isValid(), expResult.getType());
|
||||
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("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);
|
||||
}
|
||||
|
||||
@ -434,7 +491,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
public TypeCheckResult analyze(UnaryNode unary) {
|
||||
var valid = true;
|
||||
|
||||
if(unary.identifier != null){
|
||||
if (unary.identifier != null) {
|
||||
if (currentScope.contains(unary.identifier)) {
|
||||
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
|
||||
} else if (currentFields.get(unary.identifier) != null) {
|
||||
@ -446,15 +503,18 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
} else {
|
||||
errors.add(new NotDeclaredException("Var is not Declared"));
|
||||
}
|
||||
} else if (unary.statement != null){
|
||||
} else if (unary.statement != null) {
|
||||
var result = unary.statement.accept(this);
|
||||
return new TypeCheckResult(result.isValid(), result.getType());
|
||||
} else if(unary.value != null){
|
||||
} else if (unary.value != null) {
|
||||
var result = unary.value.accept(this);
|
||||
return new TypeCheckResult(result.isValid(), result.getType());
|
||||
} else if(unary.memberAccess != null){
|
||||
} 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);
|
||||
@ -478,15 +538,16 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
|
||||
} else {
|
||||
if(currentType instanceof ReferenceType reference) {
|
||||
if (currentType instanceof ReferenceType reference) {
|
||||
var currentTypeClass = context.getClass(reference.getIdentifier());
|
||||
|
||||
var currentField = currentTypeClass.getField(s);
|
||||
if(currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC){
|
||||
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(false, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -507,7 +568,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(ValueNode valueNode) {
|
||||
|
||||
switch (valueNode.valueType){
|
||||
switch (valueNode.valueType) {
|
||||
case INT_VALUE -> {
|
||||
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
|
||||
}
|
||||
@ -532,22 +593,22 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
var methods = classContext.getMethods();
|
||||
for (var method : methods) {
|
||||
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;
|
||||
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 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){
|
||||
if (same) {
|
||||
if (method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC) {
|
||||
if (method.getType() == null) {
|
||||
return new BaseType(TypeEnum.VOID);
|
||||
}
|
||||
return method.getType();
|
||||
}else {
|
||||
} else {
|
||||
errors.add(new NotVisibleException("This Method is not Visible"));
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,9 @@
|
||||
// @expected: TypeMismatchException
|
||||
public class AllFeaturesClassExample {
|
||||
|
||||
public void controlStructures(int a, boolean bool) {
|
||||
while (a > bool) {
|
||||
a--;
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
||||
public int test(boolean b, int x) {
|
||||
if (b == true) {
|
||||
return c.getSpeed();
|
||||
} else {
|
||||
x++;
|
||||
return x;
|
||||
}
|
||||
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 Car {
|
||||
|
||||
private int speed;
|
||||
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
public void setSpeed(int speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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 Car c;
|
||||
|
Loading…
Reference in New Issue
Block a user