diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java new file mode 100644 index 0000000..9f442ba --- /dev/null +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldVarAccess.java @@ -0,0 +1,52 @@ +package de.maishai.typedast.typedclass; + +import de.maishai.ast.records.*; +import de.maishai.typedast.TypedExpression; +import de.maishai.typedast.TypedNode; +import de.maishai.typedast.Type; +import lombok.Data; + +import java.util.Map; + +import static de.maishai.typedast.Util.TypedExpressionUtil.GET_KIND_OF_EXPRESSION; + +@Data +public class TypedFieldVarAccess implements TypedExpression { + private Boolean field; + private TypedExpression recursiveOwnerChain; + private String name; + private Type type; + + public TypedFieldVarAccess convertToTypedFieldVarAccess(Map localVar, Map classes, FieldVarAccess unTypedFieldVarAccess) { + field = unTypedFieldVarAccess.field(); + recursiveOwnerChain = GET_KIND_OF_EXPRESSION(localVar, classes, unTypedFieldVarAccess.recursiveOwnerChain()); + name = unTypedFieldVarAccess.id(); + type = localVar.get(unTypedFieldVarAccess.id()); + return this; + } + + @Override + public Type typeCheck(Map localVar, Map classes) { + Type type = localVar.get(name); + + if (type == null) { + //TODO: check if field variable or local variable + if(name.startsWith("this.")){ + type = localVar.get(name.substring(5)); + }else{ + throw new RuntimeException("Variable " + name + " not declared"); + } + } + return type; + } + @Override + public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { + FieldVarAccess untypedFieldVarAccess = (FieldVarAccess) unTypedAST; + TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess(); + typedFieldVarAccess.setField(untypedFieldVarAccess.field()); + typedFieldVarAccess.setRecursiveOwnerChain((TypedExpression) recursiveOwnerChain.convertToTypedAST(localVar, classes, untypedFieldVarAccess.recursiveOwnerChain())); + typedFieldVarAccess.setName(untypedFieldVarAccess.id()); + + return typedFieldVarAccess; + } +}