added private fields
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Bruder John 2024-07-01 12:58:11 +02:00
parent 7b1e6fced4
commit f3e3158460
4 changed files with 63 additions and 16 deletions

View File

@ -24,6 +24,7 @@ import ast.statementexpressions.crementexpressions.IncrementNode;
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode; import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode; import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
import ast.statements.*; import ast.statements.*;
import ast.type.AccessModifierNode;
import ast.type.EnumAccessModifierNode; import ast.type.EnumAccessModifierNode;
import ast.type.ValueNode; import ast.type.ValueNode;
import ast.type.type.*; import ast.type.type.*;
@ -411,8 +412,18 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
@Override @Override
public TypeCheckResult analyze(NonCalculationNode toCheck) { public TypeCheckResult analyze(NonCalculationNode nonCalculationNode) {
return null;
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 @Override
@ -437,9 +448,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
} else if(unary.value != null){ } else if(unary.value != null){
var result = unary.value.accept(this); var result = unary.value.accept(this);
return new TypeCheckResult(result.isValid(), result.getType()); 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 @Override
@ -464,7 +478,11 @@ public class SemanticAnalyzer implements SemanticVisitor {
var currentTypeClass = context.getClass(reference.getIdentifier()); var currentTypeClass = context.getClass(reference.getIdentifier());
var currentField = currentTypeClass.getField(s); 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); }
} }
} }

View File

@ -18,4 +18,8 @@ public class FieldContext {
return type; return type;
} }
public AccessModifierNode getAccessModifier() {
return accessModifier;
}
} }

View File

@ -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;
}
}

View File

@ -1,21 +1,26 @@
public class Test{
public int i; public class Test {
public boolean b;
public int test(){ public Car c;
return this.test(b);
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;
}
}