Compare commits
2 Commits
ce2ea07f96
...
d7a8500109
Author | SHA1 | Date | |
---|---|---|---|
|
d7a8500109 | ||
|
d014cb28c7 |
@ -1,6 +1,3 @@
|
||||
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;
|
||||
@ -8,6 +5,9 @@ import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import parser.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
import ast.ClassNode;
|
||||
import ast.ProgramNode;
|
||||
import bytecode.ByteCodeGenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
|
@ -1,14 +1,43 @@
|
||||
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;
|
||||
|
||||
public class ByteCodeGenerator {
|
||||
|
||||
public void generateByteCode(ProgramNode ast) {
|
||||
for (ClassNode classNode : ast.classes) {
|
||||
ClassCodeGen classCodeGen = new ClassCodeGen();
|
||||
classCodeGen.generateClassCode(classNode);
|
||||
for (ClassNode classDeclarationNode : ast.classes) {
|
||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
||||
classWriter.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, classDeclarationNode.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(), classDeclarationNode.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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,19 @@
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
import ast.ClassNode;
|
||||
import ast.ProgramNode;
|
||||
import bytecode.ByteCodeGenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@ -7,10 +22,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
*/
|
||||
public class MainTest {
|
||||
@Test
|
||||
void testAdd() {
|
||||
//Calculator calculator = new Calculator();
|
||||
//int result = calculator.add(2, 3);
|
||||
//assertEquals(5, result, "2 + 3 should equal 5");
|
||||
void testEmptyClass() {
|
||||
CharStream codeCharStream = null;
|
||||
try {
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/Example.java"));
|
||||
Main.parsefile(codeCharStream);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user