johns-branch #17

Merged
i22005 merged 17 commits from johns-branch into main 2024-07-01 21:08:24 +00:00
4 changed files with 63 additions and 16 deletions
Showing only changes of commit f3e3158460 - Show all commits

View File

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

View File

@ -18,4 +18,8 @@ public class FieldContext {
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 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;
}
}
}