NewStatementExpression start

This commit is contained in:
Jochen Seyfried 2024-05-31 12:08:47 +02:00
parent 21dff015b5
commit 519d891743
2 changed files with 29 additions and 21 deletions

View File

@ -56,8 +56,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;
@ -65,15 +66,14 @@ 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);
@ -85,9 +85,10 @@ 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, thisClass.name, methodName, descriptor, false);
mv.visitMethodInsn(Opcodes.INVOKEVIRTUAL, thisClass.name, methodName, descriptor, false);
}
}
}

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,11 +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);
}
@Override
public void codeGen(MethodVisitor mv, HashMap<String, HashMap<String, String>> typeContext, LinkedHashMap<String, String> localVars) throws Exception {
//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);
}
}