Compare commits

..

No commits in common. "6f9791455d83e442d698966f0d13f49a74634bb9" and "c44ed5446d0059a3bfc6f1265460ff88eca882a6" have entirely different histories.

View File

@ -38,46 +38,50 @@ public class BlockStatement extends AbstractType implements IStatement {
for (IStatement statement : statements) {
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
if(statement instanceof LocalVarDecl){
LocalVarDecl localVarDecl = (LocalVarDecl) statement;
localVars.put(localVarDecl.identifier, localVarDecl.type);
}
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);
if (typeOfCurrentStatement.type.contains(",")) {
// else if has 2 returns, all code paths must retrun a value.
String[] substrings = typeOfCurrentStatement.type.split(",");
if(this.returnType == "not" && statement instanceof ReturnStatement){
ReturnStatement returnStatement = (ReturnStatement) statement;
this.returnType = returnStatement.getTypeCheckResult().type;
}
String firstType = substrings[0];
String secondType = substrings[1];
if(statement instanceof LocalVarDecl){
LocalVarDecl localVarDecl = (LocalVarDecl) statement;
localVars.put(localVarDecl.identifier, localVarDecl.type);
}
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 (typeOfCurrentStatement.type.contains(",")) {
// else if has 2 returns, all code paths must retrun a value.
String[] substrings = typeOfCurrentStatement.type.split(",");
if (!firstIsVoid) {
typeOfCurrentStatement.type = firstType;
} else {
typeOfCurrentStatement.type = secondType;
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 (!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