package abstractSyntaxTree; import TypeCheck.TypeCheckResult; import abstractSyntaxTree.Class.FieldDecl; import abstractSyntaxTree.Class.MethodDecl; import org.objectweb.asm.MethodVisitor; import abstractSyntaxTree.Class.RefType; import org.objectweb.asm.ClassWriter; import org.objectweb.asm.Opcodes; import java.util.*; import java.io.FileOutputStream; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.jar.JarEntry; import java.util.jar.JarOutputStream; public class Program { public List classes; public HashMap> typeContext; // (class, (type, identifier)) public HashMap>>> methodContext; // (class, (returntype, (identifier, parameter))) public Program(List classes){ this.classes = classes; this.typeContext = new HashMap<>(); this.methodContext = new HashMap<>(); } public TypeCheckResult typeCheck() throws Exception{ for(RefType oneClass : classes){ HashMap classVars = new HashMap<>(); for (FieldDecl fieldDecl: oneClass.fieldDecls){ classVars.put(fieldDecl.type, fieldDecl.identifier); } typeContext.put(oneClass.name, classVars); HashMap> methodIdentifierAndParameter = new HashMap<>(); HashMap>> returnTypeAndMethod = new HashMap<>(); for (MethodDecl methodDecl : oneClass.methodDecls){ methodIdentifierAndParameter.put(methodDecl.name, methodDecl.parameters); returnTypeAndMethod.put(methodDecl.returnType, methodIdentifierAndParameter); } methodContext.put(oneClass.name, returnTypeAndMethod); oneClass.typeCheck(methodContext, typeContext, oneClass.methodDecls); } return null; } public void codeGen() throws Exception{ try (JarOutputStream jos = new JarOutputStream(new FileOutputStream("output.jar"))) { for (RefType oneClass : classes) { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, oneClass.name, null, "java/lang/Object", null); oneClass.codeGen(cw); cw.visitEnd(); byte[] bytecode = cw.toByteArray(); // Write the bytecode to a .class file in the .jar file JarEntry entry = new JarEntry(oneClass.name + ".class"); jos.putNextEntry(entry); jos.write(bytecode); jos.closeEntry(); } } catch (IOException e) { e.printStackTrace(); } /* for(RefType oneClass : classes){ ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES); cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC,oneClass.name, null, "java/lang/Object", null); oneClass.codeGen(cw); cw.visitEnd(); byte[] bytecode = cw.toByteArray(); } */ } }