johns-branch #17

Merged
i22005 merged 17 commits from johns-branch into main 2024-07-01 21:08:24 +00:00
10 changed files with 48 additions and 31 deletions
Showing only changes of commit 77fecfa476 - Show all commits

View File

@ -8,4 +8,6 @@ public interface IExpressionNode extends ASTNode, Visitable {
ITypeNode getType();
void setType(ITypeNode type);
}

View File

@ -7,6 +7,8 @@ import typechecker.TypeCheckResult;
public class BinaryNode implements IExpressionNode {
private ITypeNode typeNode;
@Override
public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.analyze(this);
@ -14,6 +16,11 @@ public class BinaryNode implements IExpressionNode {
@Override
public ITypeNode getType() {
return null;
return typeNode;
}
@Override
public void setType(ITypeNode type) {
this.typeNode = type;
}
}

View File

@ -8,6 +8,7 @@ public class CalculationNode extends BinaryNode {
public CalculationNode calculationExpression;
public EnumLineOperator operator;
public DotNode dotExpression;
private ITypeNode typeNode;
public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) {
this.calculationExpression = calculationExpression;
@ -32,9 +33,4 @@ public class CalculationNode extends BinaryNode {
return visitor.analyze(this);
}
@Override
public ITypeNode getType() {
return null;
}
}

View File

@ -34,9 +34,4 @@ public class DotNode extends BinaryNode {
return visitor.analyze(this);
}
@Override
public ITypeNode getType() {
return null;
}
}

View File

@ -36,9 +36,4 @@ public class DotSubstractionNode extends BinaryNode {
return visitor.analyze(this);
}
@Override
public ITypeNode getType() {
return null;
}
}

View File

@ -42,9 +42,4 @@ public class NonCalculationNode extends BinaryNode {
return visitor.analyze(this);
}
@Override
public ITypeNode getType() {
return null;
}
}

View File

@ -1,6 +1,7 @@
package ast.expressions.unaryexpressions;
import ast.ASTNode;
import ast.type.type.ITypeNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
import visitor.Visitable;
@ -11,6 +12,7 @@ import java.util.List;
public class MemberAccessNode implements ASTNode, Visitable {
public Boolean thisExpr;
public List<String> identifiers = new ArrayList<>();
private ITypeNode typeNode;
public MemberAccessNode(Boolean thisExpr) {
this.thisExpr = thisExpr;
@ -25,4 +27,12 @@ public class MemberAccessNode implements ASTNode, Visitable {
return visitor.analyze(this);
}
public ITypeNode getTypeNode() {
return typeNode;
}
public void setTypeNode(ITypeNode typeNode) {
this.typeNode = typeNode;
}
}

View File

@ -1,11 +1,13 @@
package ast.statementexpressions;
import ast.expressions.unaryexpressions.MemberAccessNode;
import ast.type.type.ITypeNode;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;
public class AssignableNode implements IStatementExpressionNode {
public String identifier;
private ITypeNode typeNode;
public MemberAccessNode memberAccess;
@ -22,4 +24,12 @@ public class AssignableNode implements IStatementExpressionNode {
return visitor.analyze(this);
}
public ITypeNode getTypeNode() {
return typeNode;
}
public void setTypeNode(ITypeNode typeNode) {
this.typeNode = typeNode;
}
}

View File

@ -239,10 +239,19 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override
public TypeCheckResult analyze(AssignableNode toCheck) {
if (currentFields.get(toCheck.identifier) != null) {
return new TypeCheckResult(true, currentFields.get(toCheck.identifier));
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
return new TypeCheckResult(true, currentScope.getLocalVar(toCheck.identifier));
if(toCheck.memberAccess != null){
var result = toCheck.memberAccess.accept(this);
toCheck.setTypeNode(result.getType());
return result;
} else {
if (currentFields.get(toCheck.identifier) != null) {
var type = currentFields.get(toCheck.identifier);
toCheck.setTypeNode(type);
return new TypeCheckResult(true, type);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
var type = currentScope.getLocalVar(toCheck.identifier);
return new TypeCheckResult(true, type);
}
}
return new TypeCheckResult(true, null);
@ -280,12 +289,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
currentNullType = lResult.getType();
var rResult = rExpression.accept(this);
var variable = currentScope.getLocalVar(toCheck.assignable.identifier);
if (variable == null) {
variable = currentFields.get(toCheck.assignable.identifier);
}
if (!Objects.equals(variable, rResult.getType())) {
if (!Objects.equals(lResult.getType(), rResult.getType())) {
errors.add(new TypeMismatchException(
"Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \""
+ rResult.getType() + "\""));

View File

@ -22,5 +22,8 @@ public class Car {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}