Added other implementation for methodCallStatement

This commit is contained in:
Jochen Seyfried 2024-06-23 16:42:27 +02:00
parent 90936affb9
commit 1dac9245f2
3 changed files with 38 additions and 30 deletions

View File

@ -43,9 +43,10 @@ public class LocalVarDecl extends AbstractType implements IStatement{
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
localVars.put(identifier, type);
// Set a default value for the variable --> less problems
int index = localVars.size()-1;
// Set a default value for the variable --> less problems
switch (type){
case "int":
mv.visitInsn(Opcodes.ICONST_0);

View File

@ -65,41 +65,45 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
@Override
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
//Generate Bytecode for the receiver
if (classThatHasTheMethodIfNotThis != null) {
//TODO: classThatHasTheMethodIfNotThis must be an object --> instance of the class not the class itself
// This is not finished
// Need to call codeGen so it pushes the instance onto the stack, which will be popped of
//classThatHasTheMethodIfNotThis.codeGen();
String descriptor;
List<MethodDecl> methodDecls = thisClass.methodDecls;
for (MethodDecl methodDecl : methodDecls) {
if (methodDecl.name.equals(methodName)) {
//Get the method descriptor
// descriptor = methodDecl.getMethodDescriptor(methodContext);
}
}
// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false);
} else {
// Load this onto the stack
if (receiver.thisExpression) {
// If the receiver is "this" then load "this" onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0);
} else if (receiver.instVarExpression != null) {
receiver.instVarExpression.codeGen(mv, localVars, typeContext);
} else if (receiver.newStatementExpression != null) {
receiver.newStatementExpression.codeGen(mv, localVars, typeContext);
// Not sure about this part
} else if (receiver.identifier != null) {
// Load local variable onto the stack
for (String key : localVars.keySet()) {
if (key.equals(receiver.identifier)) {
String type = localVars.get(key);
int opcode = type.equals("int") ? Opcodes.ILOAD : Opcodes.ALOAD;
mv.visitVarInsn(opcode, Integer.parseInt(key));
break;
}
}
}
// Generate Bytecode for the arguments
for (IExpression argument : arguments) {
argument.codeGen(mv, localVars, typeContext);
}
// Get the method descriptor
String descriptor;
List<MethodDecl> methodDecls = thisClass.methodDecls;
for (MethodDecl methodDecl : methodDecls) {
if (methodDecl.name.equals(methodName)) {
//Get the method descriptor
//descriptor = methodDecl.getMethodDescriptor(methodContext);
//descriptor = methodDecl.getMethodDescriptor(methodContext); //methodContext is missing
}
}
// mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
}
// Invoke the method
String className = classThatHasTheMethodIfNotThis != null ? classThatHasTheMethodIfNotThis.name : thisClass.name;
//mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, className, methodName, descriptor, false);
}
@Override

View File

@ -53,6 +53,9 @@ public class NewStatementExpression extends AbstractType implements IExpression,
//Call the constructor
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", descriptor, false);
// One instance of the class remains now on the stack
// Which will then be used in an assignment or method call
}
private String getConstructorDescriptor() {