diff --git a/src/main/java/ast/expressions/IExpressionNode.java b/src/main/java/ast/expressions/IExpressionNode.java index d1d380b..1a7a59e 100644 --- a/src/main/java/ast/expressions/IExpressionNode.java +++ b/src/main/java/ast/expressions/IExpressionNode.java @@ -8,4 +8,6 @@ public interface IExpressionNode extends ASTNode, Visitable { ITypeNode getType(); + void setType(ITypeNode type); + } diff --git a/src/main/java/ast/expressions/binaryexpressions/BinaryNode.java b/src/main/java/ast/expressions/binaryexpressions/BinaryNode.java index 9bfe8c2..f02aa2f 100644 --- a/src/main/java/ast/expressions/binaryexpressions/BinaryNode.java +++ b/src/main/java/ast/expressions/binaryexpressions/BinaryNode.java @@ -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; } } diff --git a/src/main/java/ast/expressions/binaryexpressions/CalculationNode.java b/src/main/java/ast/expressions/binaryexpressions/CalculationNode.java index 4dec81a..f97d957 100644 --- a/src/main/java/ast/expressions/binaryexpressions/CalculationNode.java +++ b/src/main/java/ast/expressions/binaryexpressions/CalculationNode.java @@ -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; - } - } diff --git a/src/main/java/ast/expressions/binaryexpressions/DotNode.java b/src/main/java/ast/expressions/binaryexpressions/DotNode.java index 4cad9ec..0b31b01 100644 --- a/src/main/java/ast/expressions/binaryexpressions/DotNode.java +++ b/src/main/java/ast/expressions/binaryexpressions/DotNode.java @@ -34,9 +34,4 @@ public class DotNode extends BinaryNode { return visitor.analyze(this); } - @Override - public ITypeNode getType() { - return null; - } - } diff --git a/src/main/java/ast/expressions/binaryexpressions/DotSubstractionNode.java b/src/main/java/ast/expressions/binaryexpressions/DotSubstractionNode.java index 9537e15..812c58c 100644 --- a/src/main/java/ast/expressions/binaryexpressions/DotSubstractionNode.java +++ b/src/main/java/ast/expressions/binaryexpressions/DotSubstractionNode.java @@ -36,9 +36,4 @@ public class DotSubstractionNode extends BinaryNode { return visitor.analyze(this); } - @Override - public ITypeNode getType() { - return null; - } - } diff --git a/src/main/java/ast/expressions/binaryexpressions/NonCalculationNode.java b/src/main/java/ast/expressions/binaryexpressions/NonCalculationNode.java index d07a5f6..df0489b 100644 --- a/src/main/java/ast/expressions/binaryexpressions/NonCalculationNode.java +++ b/src/main/java/ast/expressions/binaryexpressions/NonCalculationNode.java @@ -42,9 +42,4 @@ public class NonCalculationNode extends BinaryNode { return visitor.analyze(this); } - @Override - public ITypeNode getType() { - return null; - } - } diff --git a/src/main/java/ast/expressions/unaryexpressions/MemberAccessNode.java b/src/main/java/ast/expressions/unaryexpressions/MemberAccessNode.java index e171f79..3ce6471 100644 --- a/src/main/java/ast/expressions/unaryexpressions/MemberAccessNode.java +++ b/src/main/java/ast/expressions/unaryexpressions/MemberAccessNode.java @@ -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 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; + } + } diff --git a/src/main/java/ast/statementexpressions/AssignableNode.java b/src/main/java/ast/statementexpressions/AssignableNode.java index d3a1677..83568d8 100644 --- a/src/main/java/ast/statementexpressions/AssignableNode.java +++ b/src/main/java/ast/statementexpressions/AssignableNode.java @@ -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; + } + } diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 7f6e9a4..6d2eeec 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -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() + "\"")); diff --git a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java index f908225..bf12c55 100644 --- a/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java +++ b/src/test/resources/input/typedAstFeaturesTests/CorrectTest.java @@ -22,5 +22,8 @@ public class Car { return speed; } + public void setSpeed(int speed) { + this.speed = speed; + } }