add new class TypedFieldVarAccess and implement required methods of it

This commit is contained in:
Ahmad 2024-05-09 15:56:53 +02:00
parent a16ee38ce8
commit f502e58e6e

View File

@ -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<String, Type> localVar, Map<String, TypedClass> 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<String, Type> localVar, Map<String, TypedClass> 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<String, Type> localVar, Map<String, TypedClass> 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;
}
}