Fixed change errors
This commit is contained in:
parent
993bd8fc60
commit
b173d77620
@ -1,11 +1,11 @@
|
||||
import ast.ClassNode;
|
||||
import ast.ProgramNode;
|
||||
import bytecode.ByteCodeGenerator;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import parser.ASTBuilder;
|
||||
import parser.ClassDeclarationNode;
|
||||
import parser.ProgramNode;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
@ -37,8 +37,8 @@ public class Main {
|
||||
|
||||
// Optionally print or process the AST
|
||||
System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:");
|
||||
for (ClassDeclarationNode classNode : ast.classes) {
|
||||
System.out.println(classNode.identifier);
|
||||
for (ClassNode classNode : ast.classes) {
|
||||
System.out.println(classNode.name);
|
||||
}
|
||||
|
||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||
|
@ -3,6 +3,7 @@ package bytecode;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
import ast.ClassNode;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
import ast.ProgramNode;
|
||||
@ -10,9 +11,9 @@ import ast.ProgramNode;
|
||||
public class ByteCodeGenerator {
|
||||
|
||||
public void generateByteCode(ProgramNode ast) {
|
||||
for (ClassDeclarationNode classDeclarationNode : ast.classes) {
|
||||
for (ClassNode classNode : ast.classes) {
|
||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null,
|
||||
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null,
|
||||
"java/lang/Object", null);
|
||||
|
||||
FieldCodeGen fieldCodeGen = new FieldCodeGen();
|
||||
@ -22,7 +23,7 @@ public class ByteCodeGenerator {
|
||||
methodCodeGen.generateMethodCode(classWriter);
|
||||
|
||||
classWriter.visitEnd();
|
||||
printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.identifier);
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.name);
|
||||
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
|
39
src/main/java/bytecode/ClassCodeGen.java
Normal file
39
src/main/java/bytecode/ClassCodeGen.java
Normal file
@ -0,0 +1,39 @@
|
||||
package bytecode;
|
||||
|
||||
import ast.ClassNode;
|
||||
import org.objectweb.asm.ClassWriter;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ClassCodeGen {
|
||||
public void generateClassCode(ClassNode classNode) {
|
||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classNode.name, null,
|
||||
"java/lang/Object", null);
|
||||
|
||||
FieldCodeGen fieldCodeGen = new FieldCodeGen();
|
||||
fieldCodeGen.generateFieldCode(classWriter);
|
||||
|
||||
MethodCodeGen methodCodeGen = new MethodCodeGen();
|
||||
methodCodeGen.generateMethodCode(classWriter);
|
||||
|
||||
classWriter.visitEnd();
|
||||
printIntoClassFile(classWriter.toByteArray(), classNode.name);
|
||||
|
||||
classWriter.visitEnd();
|
||||
}
|
||||
|
||||
private void printIntoClassFile(byte[] byteCode, String name) {
|
||||
String filePath = "./classFileOutput/" + name + ".class";
|
||||
|
||||
try {
|
||||
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||
fileOutputStream.write(byteCode);
|
||||
fileOutputStream.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user