Added check for field of other classes

This commit is contained in:
ahmad 2024-06-29 14:27:37 +02:00
parent 878794ffa7
commit e6e5433a91
2 changed files with 26 additions and 3 deletions

View File

@ -49,12 +49,20 @@ public class TypedFieldVarAccess implements TypedExpression {
private Type checkFieldOrMethodType(TypedProgram typedProgram) {
if (typedProgram.getCurrentClass().isThereField(name)) {
type = typedProgram.getCurrentClass().getFieldType(name);
return type;
return checkTypeField(typedProgram);
} else if (typedProgram.getCurrentClass().isMethodOfCurrentClass(name)) {
type = typedProgram.getCurrentClass().getMethodType(name);
return type;
} else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) {
if (typedProgram.isClassWithNamePresent(recursiveOwnerChain.getType().getReference())) {
Type typeofFieldNameInClass = typedProgram.getTypeOfFieldNameInClass(recursiveOwnerChain.getType().getReference(), name);
if(typeofFieldNameInClass != null){
return typeofFieldNameInClass;
}else{
throw new RuntimeException("Field " + name + " not declared in class " + recursiveOwnerChain.getType().getReference());
}
}
type = typedProgram.getCurrentClass().getFieldType(typedFieldVarAccess.getName());
return type;
} else {
@ -106,7 +114,15 @@ public class TypedFieldVarAccess implements TypedExpression {
}
return type;
}
private Type checkTypeField(TypedProgram typedProgram) {
if (recursiveOwnerChain != null) {
if (recursiveOwnerChain.getType() != null) {
return typedProgram.getTypeOfFieldNameInClass(recursiveOwnerChain.getType().getReference(), name);
}
}
type = typedProgram.getCurrentClass().getFieldType(name);
return type;
}
@Override
public void codeGen(MethodContext ctx) {
if (recursiveOwnerChain != null) {

View File

@ -1,6 +1,7 @@
package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Program;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
import lombok.Getter;
@ -43,6 +44,12 @@ public class TypedProgram {
}
}
public boolean isClassWithNamePresent(String className) {
return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className));
}
public Type getTypeOfFieldNameInClass(String className, String fieldName) {
return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get().getFieldType(fieldName);
}
public TypedClass getTypedClass(String className) {
return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get();
}