Merge remote-tracking branch 'origin/master'

This commit is contained in:
StefanZ3 2024-06-18 16:09:46 +02:00
commit afd2319d78
4 changed files with 14 additions and 13 deletions

View File

@ -41,7 +41,9 @@ public class MethodDecl implements Node {
}
TypeCheckResult result = new TypeCheckResult();
codeBlock.typeCheck(methodContext, typeContext, localVars);
String CodeBlockType = codeBlock.typeCheck(methodContext, typeContext, localVars).type;
if(!Objects.equals(this.returnType, CodeBlockType))
throw new Exception("TypeCheck Exception: Method retruns wrong type");
result.type = codeBlock.returnType;
return result;
}

View File

@ -51,11 +51,12 @@ public class BlockStatement extends AbstractType implements IStatement {
String firstType = substrings[0];
String secondType = substrings[1];
if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) {
if (!firstType.equals("void")) {
if (!firstType.equals("void") && this.returnType != null) {
throw new Exception("TypeCheck Exception: if paths return wrong type");
}
if (!secondType.equals("void")) {
if (!secondType.equals("void") && this.returnType != null) {
throw new Exception("TypeCheck Exception: else paths return wrong type");
}
boolean firstIsVoid = firstType.equals("void");
@ -73,7 +74,7 @@ public class BlockStatement extends AbstractType implements IStatement {
continue;
// set return of block if not known yet
if(this.returnType.equals("not"))
if(this.returnType == null)
this.returnType = typeOfCurrentStatement.type;
if (!typeOfCurrentStatement.type.equals(this.returnType))
@ -82,6 +83,7 @@ public class BlockStatement extends AbstractType implements IStatement {
result.type = this.returnType;
// todo check if the block returns the needed return type in every case
// todo ignore unreachable statements?
setTypeCheckResult(result);
return result;
}

View File

@ -29,21 +29,18 @@ public class IfElseStatement extends AbstractType implements IStatement{
TypeCheckResult conditionType = condition.typeCheck(methodContext, typeContext, localVars);
if (!conditionType.equals("bool")) {
if (!conditionType.type.equals("bool")) {
throw new IllegalArgumentException("should be boolean");
}
TypeCheckResult ifStatementType = ifStatement.typeCheck(methodContext, typeContext, localVars);
TypeCheckResult elseStatementType = elseStatement.typeCheck(methodContext, typeContext, localVars);
if (!ifStatementType.equals(elseStatementType)) {
throw new IllegalArgumentException("if and else have different types");
}
if(ifStatementType.type != "void" && elseStatementType.type != "void"){
if(Objects.equals(ifStatementType.type, elseStatementType.type)){
throw new Exception("TypeCeck Exception: If and else return different not-void types");
}
if (!ifStatementType.type.equals(elseStatementType.type)) {
if(ifStatementType.type != "void" && elseStatementType.type != "void")
throw new IllegalArgumentException("TypeCeck Exception: if and else have different types");
}
result.type = ifStatementType.type + "," + elseStatementType.type;
return result;
}

View File

@ -43,7 +43,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
expression.codeGen(mv, localVars, typeContext);
//Get the Type of the expression
//TODO: Resolve how do we get the type of the expression
String type = expression.typeCheck(null, null, null).type;
String type = expression.getTypeCheckResult().type;
if (type.equals("int") || type.equals("bool") || type.equals("char")) {
mv.visitInsn(Opcodes.IRETURN);