diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index db9cac0..7f6e9a4 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -24,6 +24,7 @@ import ast.statementexpressions.crementexpressions.IncrementNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statements.*; +import ast.type.AccessModifierNode; import ast.type.EnumAccessModifierNode; import ast.type.ValueNode; import ast.type.type.*; @@ -411,8 +412,18 @@ public class SemanticAnalyzer implements SemanticVisitor { } @Override - public TypeCheckResult analyze(NonCalculationNode toCheck) { - return null; + public TypeCheckResult analyze(NonCalculationNode nonCalculationNode) { + + var expResult = nonCalculationNode.expression.accept(this); + var unaryResult = nonCalculationNode.unaryExpression.accept(this); + + if(Objects.equals(expResult.getType(), unaryResult.getType())){ + return new TypeCheckResult(expResult.isValid() && unaryResult.isValid(), expResult.getType()); + } else { + errors.add(new TypeMismatchException("NonCalculation node " + nonCalculationNode.getType() + " does not match expression " + expResult.getType())); + } + + return new TypeCheckResult(false, null); } @Override @@ -437,9 +448,12 @@ public class SemanticAnalyzer implements SemanticVisitor { } else if(unary.value != null){ var result = unary.value.accept(this); return new TypeCheckResult(result.isValid(), result.getType()); + } else if(unary.memberAccess != null){ + var result = unary.memberAccess.accept(this); + return new TypeCheckResult(result.isValid(), result.getType()); } - return new TypeCheckResult(valid, null); + return new TypeCheckResult(false, null); } @Override @@ -464,7 +478,11 @@ public class SemanticAnalyzer implements SemanticVisitor { var currentTypeClass = context.getClass(reference.getIdentifier()); var currentField = currentTypeClass.getField(s); - currentType = currentField.getType(); + if(currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC){ + currentType = currentField.getType(); + } else { + errors.add(new NotVisibleException("This field is not visible")); + return new TypeCheckResult(false, null); } } } diff --git a/src/main/java/semantic/context/FieldContext.java b/src/main/java/semantic/context/FieldContext.java index 8443520..aba5ba0 100644 --- a/src/main/java/semantic/context/FieldContext.java +++ b/src/main/java/semantic/context/FieldContext.java @@ -18,4 +18,8 @@ public class FieldContext { return type; } + public AccessModifierNode getAccessModifier() { + return accessModifier; + } + } diff --git a/src/test/resources/input/typedAstExceptionsTests/FieldIsNotVisible.java b/src/test/resources/input/typedAstExceptionsTests/FieldIsNotVisible.java new file mode 100644 index 0000000..ce099d3 --- /dev/null +++ b/src/test/resources/input/typedAstExceptionsTests/FieldIsNotVisible.java @@ -0,0 +1,20 @@ +// @expected: NotVisibleException +public class Test{ + + public Car c; + + public int test(){ + return c.speed; + } + +} + +public class Car{ + + private int speed; + + public int getSpeed(){ + return speed; + } + +} \ 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 bcb6b9c..f908225 100644 --- a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java +++ b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java @@ -1,21 +1,26 @@ -public class Test{ - public int i; - public boolean b; +public class Test { - public int test(){ - - return this.test(b); + public Car c; + public int test(boolean b, int x) { + if (b == true) { + return c.getSpeed(); + } else { + x++; + return x; + } } - public void test(int a){ +} +public class Car { + + private int speed; + + public int getSpeed() { + return speed; } - public int test(boolean bool){ - int ret = 1; - return ret; - } +} -} \ No newline at end of file