Fixed TestClassInput to run through which included the right usage of the class fields as it always loaded this onto the stack, regardless of the class in which the field is in

This commit is contained in:
Jochen Seyfried 2024-07-02 18:35:25 +02:00
parent 47a8d50185
commit 5dae167443
2 changed files with 45 additions and 3 deletions

View File

@ -50,7 +50,30 @@ public class InstVarExpression extends AbstractType implements IExpression{
// typeContext: (ClassName, (FieldName, FieldType))
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 {
// Load "this" onto the stack
// Determine if the reference is this or not
if (this.receivers.get(0).identifier != null) {
// Load the local variable onto the stack
int index = -1;
int counter = 0;
for (String key : localVars.keySet()){
if (key.equals(this.receivers.get(0).identifier)){
index = counter+1; // +1 because the first local variable is at index 1, 0 is used for "this"
break;
}
counter++;
}
if (index == -1){
throw new Exception("Variable " + this.receivers.get(0).identifier + " not found");
}
mv.visitVarInsn(Opcodes.ALOAD, index);
} else {
// Load "this" onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0);
}
//Get the field information
String fieldType = typeContext.get(thisClass).get(fieldName);

View File

@ -117,8 +117,27 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
} else if (left instanceof InstVarExpression) {
instVar = (InstVarExpression) left;
// Load the reference onto the stack
// Determine if the reference is this or another object
if (instVar.receivers.get(0).identifier != null) {
// Load the local variable (another object) onto the stack
int index = -1;
int counter = 0;
for (String key : localVars.keySet()) {
if (key.equals(instVar.receivers.get(0).identifier)) {
index = counter + 1; // Local variables start at index 1, 0 is "this"
break;
}
counter++;
}
if (index == -1) {
throw new Exception("Variable " + instVar.receivers.get(0).identifier + " not found");
}
mv.visitVarInsn(Opcodes.ALOAD, index);
} else {
// Load "this" onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0);
}
// Call the codeGen on the right expression which will push the value of the right expression onto the stack
right.codeGen(mv, localVars, typeContext, methodContext);