diff --git a/Tester.class b/Tester.class new file mode 100644 index 0000000..69949dc Binary files /dev/null and b/Tester.class differ diff --git a/Tester.java b/Tester.java index 4c79d57..92c20ee 100644 --- a/Tester.java +++ b/Tester.java @@ -1,5 +1,5 @@ public class Tester { public static void main(String[] args) { - new Test(); + new Example(); } } diff --git a/classFileOutput/Example.class b/classFileOutput/Example.class new file mode 100644 index 0000000..e44ef84 Binary files /dev/null and b/classFileOutput/Example.class differ diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index b69b823..aa0a117 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -2,8 +2,8 @@ package bytecode; import java.io.FileOutputStream; import java.io.IOException; + import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; import parser.ClassDeclarationNode; import parser.ProgramNode; @@ -12,24 +12,25 @@ public class ByteCodeGenerator { public void generateByteCode(ProgramNode ast) { for (ClassDeclarationNode classDeclarationNode : ast.classes) { - ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); - cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null, - "java/lang/Object", null); + ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null, + "java/lang/Object", null); - MethodVisitor constructor = - cw.visitMethod(Opcodes.ACC_PUBLIC, - "", - "()V", - null, - null); - cw.visitEnd(); + FieldCodeGen fieldCodeGen = new FieldCodeGen(); + fieldCodeGen.generateFieldCode(classWriter); - printIntoClassFile(cw.toByteArray(), classDeclarationNode.identifier); + MethodCodeGen methodCodeGen = new MethodCodeGen(); + methodCodeGen.generateMethodCode(classWriter); + + classWriter.visitEnd(); + printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.identifier); + + classWriter.visitEnd(); } } private void printIntoClassFile(byte[] byteCode, String name) { - String filePath = name + ".class"; + String filePath = "./classFileOutput/" + name + ".class"; try { FileOutputStream fileOutputStream = new FileOutputStream(filePath); diff --git a/src/main/java/bytecode/FieldCodeGen.java b/src/main/java/bytecode/FieldCodeGen.java new file mode 100644 index 0000000..4cca429 --- /dev/null +++ b/src/main/java/bytecode/FieldCodeGen.java @@ -0,0 +1,10 @@ +package bytecode; + +import org.objectweb.asm.ClassWriter; + +public class FieldCodeGen { + + public void generateFieldCode(ClassWriter classWriter) { + + } +} diff --git a/src/main/java/bytecode/MethodCodeGen.java b/src/main/java/bytecode/MethodCodeGen.java new file mode 100644 index 0000000..a54fda8 --- /dev/null +++ b/src/main/java/bytecode/MethodCodeGen.java @@ -0,0 +1,16 @@ +package bytecode; + +import org.objectweb.asm.ClassWriter; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +public class MethodCodeGen { + public void generateMethodCode(ClassWriter classWriter) { + MethodVisitor constructor = + classWriter.visitMethod(Opcodes.ACC_PUBLIC, + "", + "()V", + null, + null); + } +} diff --git a/src/main/java/parser/Main.java b/src/main/java/parser/Main.java index 1f98fb1..b6a3855 100644 --- a/src/main/java/parser/Main.java +++ b/src/main/java/parser/Main.java @@ -8,10 +8,23 @@ import org.antlr.v4.runtime.tree.ParseTree; import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaParser; +import java.io.IOException; +import java.nio.file.Paths; + public class Main { public static void main(String[] args) throws Exception { - // Assuming args[0] contains the path to the input file - CharStream codeCharStream = CharStreams.fromString("class Example { }"); + CharStream codeCharStream = null; + try { + codeCharStream = CharStreams.fromPath(Paths.get("path/to/your/file.txt")); + parsefile(codeCharStream); + } catch (IOException e) { + System.err.println("Error reading the file: " + e.getMessage()); + } + } + + + static void parsefile(CharStream codeCharStream){ + // CharStream codeCharStream = CharStreams.fromString("class Example { } class Example2 { }"); SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); CommonTokenStream tokens = new CommonTokenStream(lexer); SimpleJavaParser parser = new SimpleJavaParser(tokens); @@ -22,8 +35,10 @@ public class Main { ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST // Optionally print or process the AST - System.out.println("Parsed " + ast.classes.size() + " classes."); - System.out.println(ast.classes.getFirst().identifier); + System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:"); + for (ClassDeclarationNode classNode : ast.classes) { + System.out.println(classNode.identifier); + } ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); byteCodeGenerator.generateByteCode(ast);