From 7498ca6e4d27d296f04b53e25df4941eae37651c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Krau=C3=9F=2C=20Josefine?= Date: Fri, 28 Jun 2024 18:33:21 +0200 Subject: [PATCH] constructor return type --- src/main/java/abstractSyntaxTree/Class/MethodDecl.java | 9 ++++++--- src/main/java/abstractSyntaxTree/Program.java | 5 ++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java index c1143b8..b412f4a 100644 --- a/src/main/java/abstractSyntaxTree/Class/MethodDecl.java +++ b/src/main/java/abstractSyntaxTree/Class/MethodDecl.java @@ -43,9 +43,12 @@ public class MethodDecl implements Node { TypeCheckResult result = new TypeCheckResult(); codeBlock.thisClass = classThatContainsMethod; - String CodeBlockType = codeBlock.typeCheck(methodContext, typeContext, localVars).type; - if(!Objects.equals(this.returnType, CodeBlockType)) - throw new TypeCheckException("Method returns " + CodeBlockType + ", but should return " + this.returnType + ". "); + String codeBlockType = codeBlock.typeCheck(methodContext, typeContext, localVars).type; + if(Objects.equals(this.name, classThatContainsMethod)) + codeBlockType = null; + + if(!Objects.equals(this.returnType, codeBlockType)) + throw new TypeCheckException("Method returns " + codeBlockType + ", but should return " + this.returnType + ". "); result.type = codeBlock.returnType; return result; } diff --git a/src/main/java/abstractSyntaxTree/Program.java b/src/main/java/abstractSyntaxTree/Program.java index 2582f18..1d2a800 100644 --- a/src/main/java/abstractSyntaxTree/Program.java +++ b/src/main/java/abstractSyntaxTree/Program.java @@ -42,7 +42,6 @@ public class Program implements Node { typeContext.put(oneClass.name, classVars); // build method context - HashMap> identifierAndMethod = new HashMap<>(); for (MethodDecl methodDecl : oneClass.methodDecls){ if(methodDecl.returnType == null) continue; @@ -60,8 +59,8 @@ public class Program implements Node { if(oneClass.hasMain) mainCounter++; } - if(mainCounter != 1) - throw new TypeCheckException("There is not 1 Main method."); +// if(mainCounter != 1) +// throw new TypeCheckException("There is not 1 Main method."); // typecheck each class TypeCheckResult result = new TypeCheckResult();