From 519d89174350733eb882ec38436be4496c3c8d48 Mon Sep 17 00:00:00 2001 From: Jochen Seyfried Date: Fri, 31 May 2024 12:08:47 +0200 Subject: [PATCH] NewStatementExpression start --- .../MethodCallStatementExpression.java | 33 ++++++++++--------- .../NewStatementExpression.java | 17 +++++++--- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java index cf526f9..a257f6b 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/MethodCallStatementExpression.java @@ -56,8 +56,9 @@ public class MethodCallStatementExpression extends AbstractType implements IExpr @Override public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> 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,29 +66,29 @@ 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 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 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); } } diff --git a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java index 0aee650..6342503 100644 --- a/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java +++ b/src/main/java/abstractSyntaxTree/StatementExpression/NewStatementExpression.java @@ -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>> methodContext, HashMap> typeContext, HashMap localVars) throws Exception { @@ -21,11 +27,12 @@ public class NewStatementExpression extends AbstractType implements IExpression, @Override public void codeGen(MethodVisitor mv, LinkedHashMap localVars, HashMap> typeContext) throws Exception { + //Create new instance of the class + mv.visitTypeInsn(Opcodes.NEW, className); - } - - @Override - public void codeGen(MethodVisitor mv, HashMap> typeContext, LinkedHashMap 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, "", "()V", false); } }