From 6fa199da1dab161eda4f7571077c52abc94102c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Tue, 18 Jun 2024 15:42:04 +0200 Subject: [PATCH] makeing else f work without type of code block --- src/main/java/Input.java | 26 +++++-------------- .../abstractSyntaxTree/Class/MethodDecl.java | 4 ++- .../Statement/BlockStatement.java | 8 +++--- .../Statement/IfElseStatement.java | 13 ++++------ .../Statement/ReturnStatement.java | 2 +- 5 files changed, 20 insertions(+), 33 deletions(-) diff --git a/src/main/java/Input.java b/src/main/java/Input.java index a73bfbf..3dd70ab 100644 --- a/src/main/java/Input.java +++ b/src/main/java/Input.java @@ -1,27 +1,13 @@ -class ExampleEmpty { - -} - class Example { int i; boolean b; char c; -} -class Example2 { - boolean instVarBool; - int m(int n){ - int localA; - int localB; - return n; - } -} - -class Example3 { - char isNice; - char m(int n) { - boolean l; - l = false; - return 't'; + public char m(int n) { + if (true) { + return 'a'; + } else { + return 'b'; + } } } 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);