Updated TypedField

This commit is contained in:
ahmad 2024-05-11 11:43:49 +02:00
parent e1a0540a06
commit 6e2a331a66

View File

@ -12,6 +12,8 @@ import org.objectweb.asm.Opcodes;
import java.util.Map;
import static de.maishai.typedast.Type.Kind.REFERENCE;
@AllArgsConstructor
@RequiredArgsConstructor
@Data
@ -24,20 +26,25 @@ public class TypedField implements TypedNode {
}
public void convertToTypedField(Map<String, Type> localVar, TypedClass clas, Declaration declaration){
if(localVar.containsKey(declaration.name())){
throw new RuntimeException("Variable " + declaration.name() + " already declared");
}
this.type = declaration.type();
varName = declaration.name();
type = declaration.type();
localVar.put(this.varName, this.type);
this.typeCheck(localVar, clas);
}
@Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
if (localVar.containsKey(varName)) {
throw new RuntimeException("Variable " + varName + " already declared");
if(clas.isThereField(varName)){
throw new RuntimeException("Field " + varName + " already declared");
}
localVar.put(varName, type);
if(type.getKind() == REFERENCE){
if(!type.getReference().equals(clas.getClassName())){
throw new RuntimeException("Field " + varName + " has wrong type");
}
}
return type;
}