This commit is contained in:
Julian Murek 2024-05-31 12:11:32 +02:00
commit 06dc01f629
2 changed files with 29 additions and 18 deletions

View File

@ -57,8 +57,9 @@ 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){
if (classThatHasTheMethodIfNotThis != null) {
//TODO: classThatHasTheMethodIfNotThis must be an object --> instance of the class not the class itself
// Need to call codeGen so it pushes the instance onto the stack, which will be popped of
//classThatHasTheMethodIfNotThis.codeGen();
String descriptor;
@ -66,30 +67,30 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr
for (MethodDecl methodDecl : methodDecls) {
if (methodDecl.name.equals(methodName)) {
//Get the method descriptor
//descriptor = methodDecl.getMethodDescriptor(methodContext);
descriptor = methodDecl.getMethodDescriptor(methodContext);
}
}
//mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, classThatHasTheMethodIfNotThis.name, methodName, descriptor, false);
} else {
// Load this onto the stack
mv.visitVarInsn(Opcodes.ALOAD, 0);
}
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);
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);
}
}
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
}
//mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
}
@Override

View File

@ -6,6 +6,7 @@ import abstractSyntaxTree.Expression.IExpression;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Statement.IStatement;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.HashMap;
import java.util.LinkedHashMap;
@ -13,6 +14,11 @@ import java.util.List;
public class NewStatementExpression extends AbstractType implements IExpression, IStatement {
private String className;
public NewStatementExpression(String className) {
this.className = className;
}
@Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
@ -21,8 +27,12 @@ public class NewStatementExpression extends AbstractType implements IExpression,
@Override
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
//Create new instance of the class
mv.visitTypeInsn(Opcodes.NEW, className);
//Duplicate the reference, so I can use it after the INVOKE consumes one
mv.visitInsn(Opcodes.DUP);
//Call the constructor
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, className, "<init>", "()V", false);
}
}