Fixed usage of fields

This commit is contained in:
Jochen Seyfried 2024-06-28 20:29:58 +02:00
parent 27ca4a978f
commit b787b333fb

View File

@ -46,21 +46,11 @@ public class LocalVarIdentifier extends AbstractType implements IExpression{
@Override @Override
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext) throws Exception { public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext) throws Exception {
// Check if the variable is in the list of local variables
String type = null; String type = null;
// if it's a local variable
if (localVars.containsKey(identifier)) { if (localVars.containsKey(identifier)) {
type = localVars.get(identifier); type = localVars.get(identifier);
} else {
// check if instvar
type = typeContext.get(thisClass).get(identifier);
}
if (type == null){
throw new Exception("Variable " + identifier + " not declared");
}
//TODO: What if field
// Find the index of the variable // Find the index of the variable
int index = -1; int index = -1;
int counter = 0; int counter = 0;
@ -87,6 +77,30 @@ public class LocalVarIdentifier extends AbstractType implements IExpression{
mv.visitVarInsn(Opcodes.ALOAD, index); mv.visitVarInsn(Opcodes.ALOAD, index);
break; break;
} }
// If it's a field
} else if (typeContext.get(thisClass).get(identifier) != null){
type = typeContext.get(thisClass).get(identifier);
// Load "this" onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0);
// Get the field from "this"
mv.visitFieldInsn(Opcodes.GETFIELD, thisClass, identifier, getFieldDescriptor(type));
} else
throw new Exception("Variable " + identifier + " not found");
}
//TODO move this to a helper class and remove the doubled code in MethodDecl and in other places
private String getFieldDescriptor(String type) {
switch (type) {
case "int":
return "I";
case "boolean":
return "Z";
case "char":
return "C";
default:
return "L" + type + ";";
}
} }
@Override @Override