From ba73e1bd45d00657b3d14bd04e6a95a8f73f3a10 Mon Sep 17 00:00:00 2001 From: Bruder John Date: Wed, 3 Jul 2024 15:35:39 +0200 Subject: [PATCH] Revert "added chainedmethods" This reverts commit b7affd75ae9b3024492e7b240bd55574c8b6ddd9. --- src/main/java/semantic/SemanticAnalyzer.java | 60 +++---------------- src/main/java/semantic/context/Context.java | 4 -- .../ClassNotDeclared.java | 12 ---- .../typedAstFeaturesTests/ChainedMethods.java | 29 --------- .../typedAstFeaturesTests/CorrectTest.java | 50 ++++++++++------ 5 files changed, 40 insertions(+), 115 deletions(-) delete mode 100644 src/test/resources/input/typedAstExceptionsTests/ClassNotDeclared.java delete mode 100644 src/test/resources/input/typedAstFeaturesTests/ChainedMethods.java diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index bbe2aa1..6ebd124 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -21,7 +21,6 @@ import ast.statementexpressions.AssignableNode; import ast.statementexpressions.NewDeclarationNode; import ast.statementexpressions.crementexpressions.DecrementNode; import ast.statementexpressions.crementexpressions.IncrementNode; -import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statements.*; @@ -30,7 +29,6 @@ import ast.type.EnumAccessModifierNode; import ast.type.ValueNode; import ast.type.type.*; import com.sun.jdi.IntegerType; -import semantic.context.ClassContext; import semantic.context.Context; import semantic.exceptions.*; import typechecker.TypeCheckResult; @@ -165,11 +163,6 @@ public class SemanticAnalyzer implements SemanticVisitor { @Override public TypeCheckResult analyze(FieldNode toCheck) { - if (toCheck.type instanceof ReferenceType referenceType) { - if(!context.containsClass(referenceType.getIdentifier())){ - errors.add(new NotDeclaredException(referenceType.getIdentifier() + " not declared")); - } - } if (currentFields.get(toCheck.identifier) != null) { errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier)); return new TypeCheckResult(false, null); @@ -177,8 +170,6 @@ public class SemanticAnalyzer implements SemanticVisitor { currentFields.put(toCheck.identifier, toCheck.type); } return new TypeCheckResult(true, null); - - } @Override @@ -233,7 +224,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(); @@ -284,6 +275,13 @@ public class SemanticAnalyzer implements SemanticVisitor { currentNullType = oldNullType; var valid = true; + // This check currently handles things like : + /** + * private int i; + * void foo(int i){ + * i = i; + * } + */ if (assignable.equals(rExpression)) { errors.add(new TypeMismatchException("Cannot assign to self")); valid = false; @@ -326,15 +324,6 @@ public class SemanticAnalyzer implements SemanticVisitor { targetType = currentFields.get(toCheck.target.identifier); } if (targetType instanceof ReferenceType reference) { - if (!toCheck.chainedMethods.isEmpty()) { - for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) { - var type = getTypeFromMethod(chainedMethod, reference); - if (type instanceof ReferenceType referenceType) - reference = referenceType; - else - errors.add(new TypeMismatchException("Ein Basetyp hat keine funktionen")); - } - } var type = getTypeFromMethod(toCheck, reference); if (type != null) { return new TypeCheckResult(true, type); @@ -631,37 +620,4 @@ public class SemanticAnalyzer implements SemanticVisitor { return null; } - private ITypeNode getTypeFromMethod(ChainedMethodNode toCheck, ReferenceType reference) { - var classContext = context.getClass(reference.getIdentifier()); - - var methods = classContext.getMethods(); - for (var method : methods) { - if (toCheck.identifier.equals(method.getIdentifier())) { - if (method.getParameters().size() == toCheck.expressions.size() && !(method instanceof ConstructorNode)) { - boolean same = true; - for (int i = 0; i < method.getParameters().size(); i++) { - var result1 = method.getParameters().get(i).accept(this); - var result2 = toCheck.expressions.get(i).accept(this); - if (!Objects.equals(result1.getType(), result2.getType())) { - same = false; - } - } - if (same) { - if (method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC) { - if (method.getType() == null) { - return new BaseType(TypeEnum.VOID); - } - return method.getType(); - } else { - errors.add(new NotVisibleException("This Method is not Visible")); - } - - } - } - } - } - - return null; - } - } \ No newline at end of file diff --git a/src/main/java/semantic/context/Context.java b/src/main/java/semantic/context/Context.java index 4b20c8f..39e279a 100644 --- a/src/main/java/semantic/context/Context.java +++ b/src/main/java/semantic/context/Context.java @@ -21,10 +21,6 @@ public class Context { return classes.get(identifier); } - public HashMap getClasses() { - return classes; - } - public boolean containsClass(String identifier) { return classes.containsKey(identifier); } diff --git a/src/test/resources/input/typedAstExceptionsTests/ClassNotDeclared.java b/src/test/resources/input/typedAstExceptionsTests/ClassNotDeclared.java deleted file mode 100644 index 6e2b81e..0000000 --- a/src/test/resources/input/typedAstExceptionsTests/ClassNotDeclared.java +++ /dev/null @@ -1,12 +0,0 @@ -// @expected: NotDeclaredException -public class Test { - - public House1 h; - - -} - -public class House { - - -} diff --git a/src/test/resources/input/typedAstFeaturesTests/ChainedMethods.java b/src/test/resources/input/typedAstFeaturesTests/ChainedMethods.java deleted file mode 100644 index 7156569..0000000 --- a/src/test/resources/input/typedAstFeaturesTests/ChainedMethods.java +++ /dev/null @@ -1,29 +0,0 @@ -public class Test { - - public House h; - - public int test(House h){ - return h.getW().getSize(); - } - - -} - -public class House { - - private Window w; - - public Window getW(){ - return w; - } - -} - -public class Window{ - - private int size; - - public int getSize() { - return size; - } -} \ No newline at end of file diff --git a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java index 09585f9..5ef0b26 100644 --- a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java +++ b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java @@ -1,24 +1,38 @@ -public class Test { +public class AllFeaturesClassExample { + int a; + boolean b; + char c; - public Car c; - - public int test(boolean b, int x) { - if (b == true) { - return c.getSpeed(); + public void controlStructures(int adf, boolean bool) { + if (a > (10 + 8)) { } else { - return x; } + + + while (a > adf) { + a--; + } + + for (int i = 0; i < 5; i++) { + } + + } +// void logicalOperations() { + // Logische UND-Operation +// if (b && a > 5) { +// System.out.println("a ist größer als 5 und b ist wahr"); +// } + + // 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(); +// } } - -public class Car { - - private int speed; - - public int getSpeed() { - return speed; - } - -} -