Changed the methodContext to use the ParameterList

This commit is contained in:
Jochen Seyfried 2024-05-14 11:53:00 +02:00
parent 87cde5e048
commit 7ff84e5ad7
4 changed files with 17 additions and 14 deletions

3
.idea/.gitignore vendored
View File

@ -2,4 +2,5 @@
/shelf/ /shelf/
/workspace.xml /workspace.xml
.idea/ .idea/# GitHub Copilot persisted chat sessions
/copilot/chatSessions

View File

@ -14,7 +14,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack; import java.util.Stack;
public class MethodDecl implements IClass, Node { public class MethodDecl implements Node {
//Class Name //Class Name
public String classThatContainsMethod; public String classThatContainsMethod;
@ -43,8 +43,9 @@ public class MethodDecl implements IClass, Node {
return null; return null;
} }
@Override
public void codeGen(ClassWriter cw) throws Exception { // methodContext (class, (returnType, (identifier, parameter)))
public void codeGen(ClassWriter cw, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext) throws Exception {
localVars.put("this", classThatContainsMethod); localVars.put("this", classThatContainsMethod);
for (Parameter param : parameters.parameterList) { for (Parameter param : parameters.parameterList) {
@ -60,19 +61,19 @@ public class MethodDecl implements IClass, Node {
mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); mv.visitMethodInsn(Opcodes.INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitCode(); mv.visitCode();
codeBlock.codeGen(mv, localVars); //TODO: pass the local vars? --> codeGen for block not yet implemented codeBlock.codeGen(mv, localVars);
mv.visitInsn(Opcodes.RETURN); mv.visitInsn(Opcodes.RETURN);
//automatically computed max stack and max locals //automatically computed max stack and max locals
mv.visitMaxs(0, 0); mv.visitMaxs(0, 0);
mv.visitEnd(); mv.visitEnd();
} else if (name.equals("main")) { //TODO: Check how we distinguish the main method } else if (name.equals("main")) {
int access = Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC; int access = Opcodes.ACC_PUBLIC + Opcodes.ACC_STATIC;
MethodVisitor mv = cw.visitMethod(access, name, "([Ljava/lang/String;)V", null, null); MethodVisitor mv = cw.visitMethod(access, name, "([Ljava/lang/String;)V", null, null);
mv.visitCode(); mv.visitCode();
codeBlock.codeGen(mv, localVars); //TODO: pass the local vars? --> codeGen for block not yet implemented codeBlock.codeGen(mv, localVars);
mv.visitInsn(Opcodes.RETURN); mv.visitInsn(Opcodes.RETURN);
mv.visitMaxs(0, 0); mv.visitMaxs(0, 0);
@ -82,7 +83,7 @@ public class MethodDecl implements IClass, Node {
MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, name, getMethodDescriptor(), null, null); MethodVisitor mv = cw.visitMethod(Opcodes.ACC_PUBLIC, name, getMethodDescriptor(), null, null);
mv.visitCode(); mv.visitCode();
codeBlock.codeGen(mv, localVars); //TODO: pass the local vars? --> codeGen for block not yet implemented codeBlock.codeGen(mv, localVars);
// We have to check the return type to get the return opcode // We have to check the return type to get the return opcode
// For methods which return an actual value, the return opcode is created in the method body to ensure the // For methods which return an actual value, the return opcode is created in the method body to ensure the

View File

@ -3,6 +3,7 @@ package abstractSyntaxTree.Class;
import TypeCheck.AbstractType; import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult; import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Node; import abstractSyntaxTree.Node;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
@ -10,7 +11,7 @@ import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
public class RefType extends AbstractType implements IClass, Node { public class RefType extends AbstractType implements Node {
//Class Name //Class Name
public String name; public String name;
@ -72,8 +73,7 @@ public class RefType extends AbstractType implements IClass, Node {
// Method for code generation which iterates over all the field declarations // Method for code generation which iterates over all the field declarations
// and method declarations and calls their CodeGen methods // and method declarations and calls their CodeGen methods
@Override public void codeGen(ClassWriter cw, HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext) throws Exception {
public void codeGen(ClassWriter cw) throws Exception {
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, name, null, cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, name, null,
"java/lang/Object", null); "java/lang/Object", null);
@ -83,7 +83,7 @@ public class RefType extends AbstractType implements IClass, Node {
} }
for (MethodDecl method : methodDecls) { for (MethodDecl method : methodDecls) {
method.codeGen(cw); method.codeGen(cw, methodContext);
} }
cw.visitEnd(); cw.visitEnd();
} }

View File

@ -4,6 +4,7 @@ import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl; import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType; import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.ParameterList;
import org.objectweb.asm.ClassWriter; import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
@ -18,7 +19,7 @@ import java.util.jar.JarOutputStream;
public class Program implements Node { public class Program implements Node {
public List<RefType> classes; public List<RefType> classes;
public HashMap<String, HashMap<String, String>> typeContext; // (class, (type, identifier)) public HashMap<String, HashMap<String, String>> typeContext; // (class, (type, identifier))
public HashMap<String, HashMap<String, HashMap<String, List<String>>>> methodContext; // (class, (returntype, (identifier, parameter))) public HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext; // (class, (returntype, (identifier, parameterList)))
public Program(List<RefType> classes){ public Program(List<RefType> classes){
this.classes = classes; this.classes = classes;
@ -64,7 +65,7 @@ public class Program implements Node {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, oneClass.name, null, "java/lang/Object", null); cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, oneClass.name, null, "java/lang/Object", null);
oneClass.codeGen(cw); oneClass.codeGen(cw, methodContext);
cw.visitEnd(); cw.visitEnd();
byte[] bytecode = cw.toByteArray(); byte[] bytecode = cw.toByteArray();