diff --git a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java index 5f77f5b..384d8a1 100644 --- a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java +++ b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java @@ -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; } diff --git a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java index c5accf9..e0eb7e1 100644 --- a/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/BlockStatement.java @@ -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; } diff --git a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java index 928c935..f373d76 100644 --- a/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/IfElseStatement.java @@ -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; } diff --git a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java index c374d12..b08226f 100644 --- a/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java +++ b/src/main/java/abstractSyntaxTree/Statement/ReturnStatement.java @@ -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);