fixed code bloc without return

This commit is contained in:
Krauß, Josefine 2024-06-28 11:28:58 +02:00
parent d4f98693d6
commit 5c81b88ca5
4 changed files with 16 additions and 24 deletions

View File

@ -1,24 +1,13 @@
class Example1 { class Example1 {
int instVar; public int fak(int number){
Example e; if(number < 0){
public Example1(){} return 1;
int meth(int n){ }
Example e = new Example(); int factorial = 1;
instVar = 2; int i = 0;
int i = m1(instVar).m2().m3(); while(i < number){
factorial = factorial * i;
return 0; }
} return factorial;
Example m1(int n){
return new Example();
} }
} }
class Example {
Example1 example1;
Example1 m(int a){
return new Example1();
}
Example m2(){return new Example();}
int m3(){return 1;}
}

View File

@ -46,7 +46,8 @@ public class BinaryExpression extends AbstractType implements IExpression{
case "<=": case "<=":
case ">=": case ">=":
case "!=": case "!=":
result.type = TypeCheckHelper.upperBound(leftType.type, rightType.type); TypeCheckHelper.upperBound(leftType.type, rightType.type);
result.type = "boolean";
break; break;
case "-": case "-":

View File

@ -93,14 +93,16 @@ public class BlockStatement extends AbstractType implements IStatement {
if(typeOfCurrentStatement.type.equals("void")) if(typeOfCurrentStatement.type.equals("void"))
continue; continue;
// set return of block if not known yet
// set return of block if not known yet
if(this.returnType == null|| this.returnType == "not") if(this.returnType == null|| this.returnType == "not")
this.returnType = typeOfCurrentStatement.type; this.returnType = typeOfCurrentStatement.type;
if (!typeOfCurrentStatement.type.equals(this.returnType)) if (!typeOfCurrentStatement.type.equals(this.returnType))
throw new TypeCheckException("At least some statements of the block returns the wrong type or missing return statement."); throw new TypeCheckException("At least some statements of the block returns the wrong type or missing return statement.");
} }
if(this.returnType == null)
this.returnType = "void";
result.type = this.returnType; result.type = this.returnType;
setTypeCheckResult(result); setTypeCheckResult(result);
return result; return result;

View File

@ -31,7 +31,7 @@ public class IfStatement extends AbstractType implements IStatement{
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars); TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
if (!conditionType.type.equals("boolean")) { if (!conditionType.type.equals("boolean")) {
throw new TypeCheckException("Condition of If-Statement should is " + conditionType.type + ", but should be boolean."); throw new TypeCheckException("Condition of If-Statement is " + conditionType.type + ", but should be boolean.");
} }
if(ifStatement instanceof BlockStatement blockStatement){ if(ifStatement instanceof BlockStatement blockStatement){