Compare commits

...

2 Commits

Author SHA1 Message Date
Krauß, Josefine
6f9791455d Merge remote-tracking branch 'origin/master' 2024-06-18 10:08:25 +02:00
Krauß, Josefine
0f76b1fdae small change due to "not" as block return 2024-06-18 10:08:14 +02:00

View File

@ -38,50 +38,46 @@ public class BlockStatement extends AbstractType implements IStatement {
for (IStatement statement : statements) {
System.out.println(statement.getClass()); //todo remove
// todo remove later if there are no null statement any more
if (statement != null) {
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
if(statement instanceof LocalVarDecl){
LocalVarDecl localVarDecl = (LocalVarDecl) statement;
localVars.put(localVarDecl.identifier, localVarDecl.type);
}
if(this.returnType == "not" && statement instanceof ReturnStatement){
ReturnStatement returnStatement = (ReturnStatement) statement;
this.returnType = returnStatement.getTypeCheckResult().type;
}
if (typeOfCurrentStatement.type.contains(",")) {
// else if has 2 returns, all code paths must retrun a value.
String[] substrings = typeOfCurrentStatement.type.split(",");
if(statement instanceof LocalVarDecl){
LocalVarDecl localVarDecl = (LocalVarDecl) statement;
localVars.put(localVarDecl.identifier, localVarDecl.type);
}
String firstType = substrings[0];
String secondType = substrings[1];
if (typeOfCurrentStatement.type.contains(",")) {
// else if has 2 returns, all code paths must retrun a value.
String[] substrings = typeOfCurrentStatement.type.split(",");
if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) {
if (!firstType.equals("void")) {
throw new Exception("TypeCheck Exception: if paths return wrong type");
}
if (!secondType.equals("void")) {
throw new Exception("TypeCheck Exception: else paths return wrong type");
}
boolean firstIsVoid = firstType.equals("void");
String firstType = substrings[0];
String secondType = substrings[1];
if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) {
if (!firstType.equals("void")) {
throw new Exception("TypeCheck Exception: if paths return wrong type");
}
if (!secondType.equals("void")) {
throw new Exception("TypeCheck Exception: else paths return wrong type");
}
boolean firstIsVoid = firstType.equals("void");
if (!firstIsVoid) {
typeOfCurrentStatement.type = firstType;
} else {
typeOfCurrentStatement.type = secondType;
}
if (!firstIsVoid) {
typeOfCurrentStatement.type = firstType;
} else {
typeOfCurrentStatement.type = secondType;
}
}
if (!typeOfCurrentStatement.type.equals(this.returnType)) {
if (!typeOfCurrentStatement.type.equals("void"))
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
}
}
if(typeOfCurrentStatement.type.equals("void"))
continue;
// set return of block if not known yet
if(this.returnType.equals("not"))
this.returnType = typeOfCurrentStatement.type;
if (!typeOfCurrentStatement.type.equals(this.returnType))
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
}
result.type = this.returnType;
// todo check if the block returns the needed return type in every case