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);
|
||||
@ -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,8 +219,12 @@ 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();
|
||||
@ -233,6 +235,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return new TypeCheckResult(true, blockReturnType);
|
||||
}
|
||||
|
||||
@ -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
|
||||
@ -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);
|
||||
}
|
||||
|
||||
@ -455,6 +512,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
} 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);
|
||||
@ -486,7 +546,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
currentType = currentField.getType();
|
||||
} else {
|
||||
errors.add(new NotVisibleException("This field is not visible"));
|
||||
return new TypeCheckResult(false, null); }
|
||||
return new TypeCheckResult(false, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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++) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class Car {
|
||||
// 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");
|
||||
// }
|
||||
// }
|
||||
|
||||
private int speed;
|
||||
|
||||
public int getSpeed() {
|
||||
return speed;
|
||||
// public static void main(String[] args) {
|
||||
// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
|
||||
// obj.controlStructures();
|
||||
// }
|
||||
}
|
||||
|
||||
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