Compare commits
No commits in common. "5344b01384bad66c7dd119b0a871b7ebe9f1ae2c" and "6e8079807a87f202387ebcf275c9bf787d8adcdb" have entirely different histories.
5344b01384
...
6e8079807a
BIN
Tester.class
BIN
Tester.class
Binary file not shown.
@ -1,5 +1,5 @@
|
|||||||
public class Tester {
|
public class Tester {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new Example();
|
new Test();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Binary file not shown.
@ -2,8 +2,8 @@ package bytecode;
|
|||||||
|
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
import parser.ClassDeclarationNode;
|
import parser.ClassDeclarationNode;
|
||||||
import parser.ProgramNode;
|
import parser.ProgramNode;
|
||||||
@ -12,25 +12,24 @@ public class ByteCodeGenerator {
|
|||||||
|
|
||||||
public void generateByteCode(ProgramNode ast) {
|
public void generateByteCode(ProgramNode ast) {
|
||||||
for (ClassDeclarationNode classDeclarationNode : ast.classes) {
|
for (ClassDeclarationNode classDeclarationNode : ast.classes) {
|
||||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||||
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null,
|
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.identifier, null,
|
||||||
"java/lang/Object", null);
|
"java/lang/Object", null);
|
||||||
|
|
||||||
FieldCodeGen fieldCodeGen = new FieldCodeGen();
|
MethodVisitor constructor =
|
||||||
fieldCodeGen.generateFieldCode(classWriter);
|
cw.visitMethod(Opcodes.ACC_PUBLIC,
|
||||||
|
"<init>",
|
||||||
|
"()V",
|
||||||
|
null,
|
||||||
|
null);
|
||||||
|
cw.visitEnd();
|
||||||
|
|
||||||
MethodCodeGen methodCodeGen = new MethodCodeGen();
|
printIntoClassFile(cw.toByteArray(), classDeclarationNode.identifier);
|
||||||
methodCodeGen.generateMethodCode(classWriter);
|
|
||||||
|
|
||||||
classWriter.visitEnd();
|
|
||||||
printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.identifier);
|
|
||||||
|
|
||||||
classWriter.visitEnd();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void printIntoClassFile(byte[] byteCode, String name) {
|
private void printIntoClassFile(byte[] byteCode, String name) {
|
||||||
String filePath = "./classFileOutput/" + name + ".class";
|
String filePath = name + ".class";
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
package bytecode;
|
|
||||||
|
|
||||||
import org.objectweb.asm.ClassWriter;
|
|
||||||
|
|
||||||
public class FieldCodeGen {
|
|
||||||
|
|
||||||
public void generateFieldCode(ClassWriter classWriter) {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,16 +0,0 @@
|
|||||||
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,
|
|
||||||
"<init>",
|
|
||||||
"()V",
|
|
||||||
null,
|
|
||||||
null);
|
|
||||||
}
|
|
||||||
}
|
|
@ -8,23 +8,10 @@ import org.antlr.v4.runtime.tree.ParseTree;
|
|||||||
import parser.generated.SimpleJavaLexer;
|
import parser.generated.SimpleJavaLexer;
|
||||||
import parser.generated.SimpleJavaParser;
|
import parser.generated.SimpleJavaParser;
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
|
|
||||||
public class Main {
|
public class Main {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
CharStream codeCharStream = null;
|
// Assuming args[0] contains the path to the input file
|
||||||
try {
|
CharStream codeCharStream = CharStreams.fromString("class Example { }");
|
||||||
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);
|
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokens);
|
SimpleJavaParser parser = new SimpleJavaParser(tokens);
|
||||||
@ -35,10 +22,8 @@ public class Main {
|
|||||||
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
|
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
|
||||||
|
|
||||||
// Optionally print or process the AST
|
// Optionally print or process the AST
|
||||||
System.out.println("Parsed " + ast.classes.size() + " classes with identifiers/names:");
|
System.out.println("Parsed " + ast.classes.size() + " classes.");
|
||||||
for (ClassDeclarationNode classNode : ast.classes) {
|
System.out.println(ast.classes.getFirst().identifier);
|
||||||
System.out.println(classNode.identifier);
|
|
||||||
}
|
|
||||||
|
|
||||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||||
byteCodeGenerator.generateByteCode(ast);
|
byteCodeGenerator.generateByteCode(ast);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user