diff --git a/src/main/java/ast/members/ConstructorNode.java b/src/main/java/ast/members/ConstructorNode.java index 2d5400d..b3fda52 100644 --- a/src/main/java/ast/members/ConstructorNode.java +++ b/src/main/java/ast/members/ConstructorNode.java @@ -9,13 +9,10 @@ import java.util.List; import java.util.Objects; public class ConstructorNode extends MethodNode { - public AccessModifierNode accessType; public String identifier; - public List parameters = new ArrayList<>(); - public BlockNode block; public ConstructorNode(String accessType, String identifier, BlockNode block) { - this.accessType = new AccessModifierNode(accessType); + this.accesModifier = new AccessModifierNode(accessType); this.identifier = identifier; this.block = block; } diff --git a/src/main/java/semantic/Scope.java b/src/main/java/semantic/Scope.java index b6b40c4..cde47c0 100644 --- a/src/main/java/semantic/Scope.java +++ b/src/main/java/semantic/Scope.java @@ -16,7 +16,7 @@ public class Scope { public void addLocalVar(String name, ITypeNode type) { if (this.contains(name)) { - throw new AlreadyDeclaredException("Variable " + name + " already exists in this scope"); + SemanticAnalyzer.errors.add(new AlreadyDeclaredException("Duplicate local variable " + name)); } localVars.peek().put(name, type); } diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 6ebd124..06fe7e4 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -112,53 +112,49 @@ public class SemanticAnalyzer implements SemanticVisitor { @Override public TypeCheckResult analyze(MethodNode methodNode) { - if (methodNode instanceof ConstructorNode) { - return new TypeCheckResult(true, new BaseType(TypeEnum.VOID)); - } else { - var valid = true; + var valid = true; - for (var otherMethod : currentClass.getMethods()) { - if (Objects.equals(otherMethod, methodNode)) - break; - if (otherMethod.isSame(methodNode)) { - errors.add(new AlreadyDeclaredException( - "Method " + methodNode.getIdentifier() + " is already defined in class " - + currentClass.identifier)); - valid = false; - } + for (var otherMethod : currentClass.getMethods()) { + if (Objects.equals(otherMethod, methodNode)) + break; + if (otherMethod.isSame(methodNode)) { + errors.add(new AlreadyDeclaredException( + "Method " + methodNode.getIdentifier() + " is already defined in class " + + currentClass.identifier)); + valid = false; } + } - currentScope.pushScope(); - for (var parameter : methodNode.getParameters()) { - var result = parameter.accept(this); - valid = valid && result.isValid(); - try { - currentScope.addLocalVar(parameter.identifier, parameter.type); - } catch (AlreadyDeclaredException e) { - errors.add(new AlreadyDeclaredException(parameter.identifier)); - } - - } - - currentMethodReturnType = methodNode.getType(); - currentNullType = currentMethodReturnType; - - var result = methodNode.block.accept(this); + currentScope.pushScope(); + for (var parameter : methodNode.getParameters()) { + var result = parameter.accept(this); valid = valid && result.isValid(); - currentScope.popScope(); - ITypeNode resultType = result.getType(); - - if (resultType == null) { - resultType = new BaseType(TypeEnum.VOID); + try { + currentScope.addLocalVar(parameter.identifier, parameter.type); + } catch (AlreadyDeclaredException e) { + errors.add(new AlreadyDeclaredException(parameter.identifier)); } - if (methodNode.getType() == null) { - methodNode.setType(new BaseType(TypeEnum.VOID)); - } - - return new TypeCheckResult(valid, resultType); } + + currentMethodReturnType = methodNode.getType(); + currentNullType = currentMethodReturnType; + + var result = methodNode.block.accept(this); + valid = valid && result.isValid(); + currentScope.popScope(); + ITypeNode resultType = result.getType(); + + if (resultType == null) { + resultType = new BaseType(TypeEnum.VOID); + } + if (methodNode.getType() == null) { + methodNode.setType(new BaseType(TypeEnum.VOID)); + } + + return new TypeCheckResult(valid, resultType); + } @Override @@ -224,7 +220,7 @@ public class SemanticAnalyzer implements SemanticVisitor { } for (IStatementNode statementNode : blockNode.statements) { var result = statementNode.accept(this); - if(!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)){ + if (!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)) { if (result.getType() != null) { if (blockReturnType == null) { blockReturnType = result.getType(); diff --git a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java index 5ef0b26..828f6f3 100644 --- a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java +++ b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java @@ -1,38 +1,11 @@ public class AllFeaturesClassExample { - int a; - boolean b; - char c; - - public void controlStructures(int adf, boolean bool) { - if (a > (10 + 8)) { - } else { - } - - - while (a > adf) { - a--; - } - - for (int i = 0; i < 5; i++) { - } + public void controlStructures(int a, boolean b) { } -// void logicalOperations() { - // Logische UND-Operation -// if (b && a > 5) { -// System.out.println("a ist größer als 5 und b ist wahr"); -// } + public AllFeaturesClassExample(boolean b){ + + } - // Logische ODER-Operation -// if (b || a < 5) { -// System.out.println("b ist wahr oder a ist kleiner als 5"); -// } -// } - -// public static void main(String[] args) { -// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a'); -// obj.controlStructures(); -// } }