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(); ITypeNode getType();
void setType(ITypeNode type);
} }

View File

@ -7,6 +7,8 @@ import typechecker.TypeCheckResult;
public class BinaryNode implements IExpressionNode { public class BinaryNode implements IExpressionNode {
private ITypeNode typeNode;
@Override @Override
public TypeCheckResult accept(SemanticVisitor visitor) { public TypeCheckResult accept(SemanticVisitor visitor) {
return visitor.analyze(this); return visitor.analyze(this);
@ -14,6 +16,11 @@ public class BinaryNode implements IExpressionNode {
@Override @Override
public ITypeNode getType() { 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 CalculationNode calculationExpression;
public EnumLineOperator operator; public EnumLineOperator operator;
public DotNode dotExpression; public DotNode dotExpression;
private ITypeNode typeNode;
public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) { public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) {
this.calculationExpression = calculationExpression; this.calculationExpression = calculationExpression;
@ -32,9 +33,4 @@ public class CalculationNode extends BinaryNode {
return visitor.analyze(this); 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); 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); 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); return visitor.analyze(this);
} }
@Override
public ITypeNode getType() {
return null;
}
} }

View File

@ -1,6 +1,7 @@
package ast.expressions.unaryexpressions; package ast.expressions.unaryexpressions;
import ast.ASTNode; import ast.ASTNode;
import ast.type.type.ITypeNode;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
import visitor.Visitable; import visitor.Visitable;
@ -11,6 +12,7 @@ import java.util.List;
public class MemberAccessNode implements ASTNode, Visitable { public class MemberAccessNode implements ASTNode, Visitable {
public Boolean thisExpr; public Boolean thisExpr;
public List<String> identifiers = new ArrayList<>(); public List<String> identifiers = new ArrayList<>();
private ITypeNode typeNode;
public MemberAccessNode(Boolean thisExpr) { public MemberAccessNode(Boolean thisExpr) {
this.thisExpr = thisExpr; this.thisExpr = thisExpr;
@ -25,4 +27,12 @@ public class MemberAccessNode implements ASTNode, Visitable {
return visitor.analyze(this); 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; package ast.statementexpressions;
import ast.expressions.unaryexpressions.MemberAccessNode; import ast.expressions.unaryexpressions.MemberAccessNode;
import ast.type.type.ITypeNode;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
public class AssignableNode implements IStatementExpressionNode { public class AssignableNode implements IStatementExpressionNode {
public String identifier; public String identifier;
private ITypeNode typeNode;
public MemberAccessNode memberAccess; public MemberAccessNode memberAccess;
@ -22,4 +24,12 @@ public class AssignableNode implements IStatementExpressionNode {
return visitor.analyze(this); 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 @Override
public TypeCheckResult analyze(AssignableNode toCheck) { public TypeCheckResult analyze(AssignableNode toCheck) {
if (currentFields.get(toCheck.identifier) != null) { if(toCheck.memberAccess != null){
return new TypeCheckResult(true, currentFields.get(toCheck.identifier)); var result = toCheck.memberAccess.accept(this);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) { toCheck.setTypeNode(result.getType());
return new TypeCheckResult(true, currentScope.getLocalVar(toCheck.identifier)); 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); return new TypeCheckResult(true, null);
@ -280,12 +289,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
currentNullType = lResult.getType(); currentNullType = lResult.getType();
var rResult = rExpression.accept(this); var rResult = rExpression.accept(this);
var variable = currentScope.getLocalVar(toCheck.assignable.identifier); if (!Objects.equals(lResult.getType(), rResult.getType())) {
if (variable == null) {
variable = currentFields.get(toCheck.assignable.identifier);
}
if (!Objects.equals(variable, rResult.getType())) {
errors.add(new TypeMismatchException( errors.add(new TypeMismatchException(
"Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \"" "Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \""
+ rResult.getType() + "\"")); + rResult.getType() + "\""));

View File

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