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 {
int instVar;
Example e;
public Example1(){}
int meth(int n){
Example e = new Example();
instVar = 2;
int i = m1(instVar).m2().m3();
return 0;
}
Example m1(int n){
return new Example();
public int fak(int number){
if(number < 0){
return 1;
}
int factorial = 1;
int i = 0;
while(i < number){
factorial = factorial * i;
}
return factorial;
}
}
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 "!=":
result.type = TypeCheckHelper.upperBound(leftType.type, rightType.type);
TypeCheckHelper.upperBound(leftType.type, rightType.type);
result.type = "boolean";
break;
case "-":

View File

@ -93,14 +93,16 @@ public class BlockStatement extends AbstractType implements IStatement {
if(typeOfCurrentStatement.type.equals("void"))
continue;
// set return of block if not known yet
// set return of block if not known yet
if(this.returnType == null|| this.returnType == "not")
this.returnType = typeOfCurrentStatement.type;
if (!typeOfCurrentStatement.type.equals(this.returnType))
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;
setTypeCheckResult(result);
return result;

View File

@ -31,7 +31,7 @@ public class IfStatement extends AbstractType implements IStatement{
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
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){