66 lines
2.2 KiB
Java
66 lines
2.2 KiB
Java
package abstractSyntaxTree;
|
|
|
|
import TypeCheck.TypeCheckResult;
|
|
import abstractSyntaxTree.Class.FieldDecl;
|
|
import abstractSyntaxTree.Class.RefType;
|
|
import org.objectweb.asm.ClassWriter;
|
|
import org.objectweb.asm.Opcodes;
|
|
|
|
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<RefType> classes;
|
|
|
|
public HashMap<String, HashMap<String, String>> typeContext; // (class, (type, identifier))
|
|
public HashMap<String, HashMap<String, HashMap<String, String>>> methodContext; // (class, (returntype, (identifier, parameter)))
|
|
|
|
public TypeCheckResult typeCheck() throws Exception{
|
|
for(RefType oneClass : classes){
|
|
HashMap<String, String> classVars = new HashMap<>();
|
|
for (FieldDecl fielsDecl: oneClass.fieldDecls)
|
|
classVars.put(fielsDecl.type, fielsDecl.identifier);
|
|
oneClass.typeCheck();
|
|
}
|
|
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();
|
|
}
|
|
*/
|
|
}
|
|
}
|