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) { for (IStatement statement : statements) {
System.out.println(statement.getClass()); //todo remove TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
// todo remove later if there are no null statement any more if(statement instanceof LocalVarDecl){
if (statement != null) { LocalVarDecl localVarDecl = (LocalVarDecl) statement;
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); localVars.put(localVarDecl.identifier, localVarDecl.type);
}
if(this.returnType == "not" && statement instanceof ReturnStatement){ if (typeOfCurrentStatement.type.contains(",")) {
ReturnStatement returnStatement = (ReturnStatement) statement; // else if has 2 returns, all code paths must retrun a value.
this.returnType = returnStatement.getTypeCheckResult().type; String[] substrings = typeOfCurrentStatement.type.split(",");
}
if(statement instanceof LocalVarDecl){ String firstType = substrings[0];
LocalVarDecl localVarDecl = (LocalVarDecl) statement; String secondType = substrings[1];
localVars.put(localVarDecl.identifier, localVarDecl.type);
}
if (typeOfCurrentStatement.type.contains(",")) { if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) {
// else if has 2 returns, all code paths must retrun a value. if (!firstType.equals("void")) {
String[] substrings = typeOfCurrentStatement.type.split(","); 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]; if (!firstIsVoid) {
String secondType = substrings[1]; typeOfCurrentStatement.type = firstType;
} else {
if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) { typeOfCurrentStatement.type = secondType;
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 (!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; result.type = this.returnType;
// todo check if the block returns the needed return type in every case // todo check if the block returns the needed return type in every case