set types of expressions
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Bruder John 2024-07-02 10:36:22 +02:00
parent 561eafbf4c
commit 82356ec189
6 changed files with 97 additions and 8 deletions

View File

@ -9,7 +9,6 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class ConstructorNode extends MethodNode { public class ConstructorNode extends MethodNode {
public String identifier;
public ConstructorNode(String accessType, String identifier, BlockNode block) { public ConstructorNode(String accessType, String identifier, BlockNode block) {
this.accesModifier = new AccessModifierNode(accessType); this.accesModifier = new AccessModifierNode(accessType);

View File

@ -17,7 +17,7 @@ public class MethodNode implements MemberNode, Visitable {
public AccessModifierNode accesModifier; public AccessModifierNode accesModifier;
private ITypeNode type; private ITypeNode type;
public Boolean voidType; public Boolean voidType;
private String identifier; protected String identifier;
public List<ParameterNode> parameters = new ArrayList<>(); public List<ParameterNode> parameters = new ArrayList<>();
public BlockNode block; public BlockNode block;

View File

@ -405,6 +405,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
case PLUS, MINUS: case PLUS, MINUS:
if (calcRes.getType() instanceof BaseType calcType && dotRes.getType() instanceof BaseType dotType && if (calcRes.getType() instanceof BaseType calcType && dotRes.getType() instanceof BaseType dotType &&
calcType.getTypeEnum().equals(TypeEnum.INT) && dotType.getTypeEnum().equals(TypeEnum.INT)) { calcType.getTypeEnum().equals(TypeEnum.INT) && dotType.getTypeEnum().equals(TypeEnum.INT)) {
calcNode.setType(new BaseType(TypeEnum.INT));
return new TypeCheckResult(true, new BaseType(TypeEnum.INT)); return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
} }
break; break;
@ -412,10 +413,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
} else { } else {
calcNode.setType(calcRes.getType());
return new TypeCheckResult(calcRes.isValid(), calcRes.getType()); return new TypeCheckResult(calcRes.isValid(), calcRes.getType());
} }
} else if (calcNode.dotExpression != null) { } else if (calcNode.dotExpression != null) {
var dotRes = calcNode.dotExpression.accept(this); var dotRes = calcNode.dotExpression.accept(this);
calcNode.setType(dotRes.getType());
return new TypeCheckResult(dotRes.isValid(), dotRes.getType()); return new TypeCheckResult(dotRes.isValid(), dotRes.getType());
} }
return new TypeCheckResult(false, null); return new TypeCheckResult(false, null);
@ -458,6 +461,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
case LESS, LESS_EQUAL, GREATER, GREATER_EQUAL: case LESS, LESS_EQUAL, GREATER, GREATER_EQUAL:
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) && if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) { unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
nonCalculationNode.setType(new BaseType(TypeEnum.BOOL));
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL)); return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
} else { } else {
errors.add(new TypeMismatchException("Both types must be Integer")); errors.add(new TypeMismatchException("Both types must be Integer"));
@ -466,6 +470,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
case OR, AND: case OR, AND:
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) && if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) { unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
nonCalculationNode.setType(new BaseType(TypeEnum.BOOL));
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL)); return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
} else { } else {
errors.add(new TypeMismatchException("Both types must be Boolean")); errors.add(new TypeMismatchException("Both types must be Boolean"));
@ -474,6 +479,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
case EQUAL, NOT_EQUAL: case EQUAL, NOT_EQUAL:
if (expResult.getType() instanceof BaseType expResultType && unaryResult.getType() instanceof BaseType unaryResultType if (expResult.getType() instanceof BaseType expResultType && unaryResult.getType() instanceof BaseType unaryResultType
&& Objects.equals(expResultType, unaryResultType)) { && Objects.equals(expResultType, unaryResultType)) {
nonCalculationNode.setType(new BaseType(TypeEnum.BOOL));
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL)); return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
} else { } else {
errors.add(new TypeMismatchException("Both types must be the same")); errors.add(new TypeMismatchException("Both types must be the same"));
@ -489,9 +495,13 @@ public class SemanticAnalyzer implements SemanticVisitor {
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)); var type = currentScope.getLocalVar(unary.identifier);
unary.setType(type);
return new TypeCheckResult(valid, type);
} else if (currentFields.get(unary.identifier) != null) { } else if (currentFields.get(unary.identifier) != null) {
return new TypeCheckResult(valid, currentFields.get(unary.identifier)); var type = currentFields.get(unary.identifier);
unary.setType(type);
return new TypeCheckResult(valid, type);
} else if (unary.statement != null) { } else if (unary.statement != null) {
var result = unary.statement.accept(this); var result = unary.statement.accept(this);
unary.setType(result.getType()); unary.setType(result.getType());
@ -501,15 +511,19 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
} else if (unary.statement != null) { } else if (unary.statement != null) {
var result = unary.statement.accept(this); var result = unary.statement.accept(this);
unary.setType(result.getType());
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);
unary.setType(result.getType());
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);
unary.setType(result.getType());
return new TypeCheckResult(result.isValid(), result.getType()); return new TypeCheckResult(result.isValid(), result.getType());
} else if (unary.expression != null) { } else if (unary.expression != null) {
var result = unary.expression.accept(this); var result = unary.expression.accept(this);
unary.setType(result.getType());
return new TypeCheckResult(result.isValid(), result.getType()); return new TypeCheckResult(result.isValid(), result.getType());
} }

View File

@ -0,0 +1,12 @@
// @expected: AlreadyDeclaredException
public class AllFeaturesClassExample {
public AllFeaturesClassExample(boolean b){
}
public AllFeaturesClassExample(boolean b){
}
}

View File

@ -0,0 +1,11 @@
public class AllFeaturesClassExample {
public AllFeaturesClassExample(boolean b){
}
public AllFeaturesClassExample(boolean b, int a){
}
}

View File

@ -1,11 +1,64 @@
public class AllFeaturesClassExample {
public void controlStructures(int a, boolean b) { 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++) {
}
} }
public AllFeaturesClassExample(boolean 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();
// }
}
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;
}
}