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 { public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
localVars.put(identifier, type); localVars.put(identifier, type);
// Set a default value for the variable --> less problems
int index = localVars.size()-1; int index = localVars.size()-1;
// Set a default value for the variable --> less problems
switch (type){ switch (type){
case "int": case "int":
mv.visitInsn(Opcodes.ICONST_0); mv.visitInsn(Opcodes.ICONST_0);

View File

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

View File

@ -53,6 +53,9 @@ public class NewStatementExpression extends AbstractType implements IExpression,
//Call the constructor //Call the constructor
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", descriptor, false); 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() { private String getConstructorDescriptor() {