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