Structure and Fixes
This commit is contained in:
parent
993bd8fc60
commit
d014cb28c7
@ -1,13 +1,13 @@
|
|||||||
import bytecode.ByteCodeGenerator;
|
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
import parser.ASTBuilder;
|
import parser.ASTBuilder;
|
||||||
import parser.ClassDeclarationNode;
|
|
||||||
import parser.ProgramNode;
|
|
||||||
import parser.generated.SimpleJavaLexer;
|
import parser.generated.SimpleJavaLexer;
|
||||||
import parser.generated.SimpleJavaParser;
|
import parser.generated.SimpleJavaParser;
|
||||||
|
import ast.ClassNode;
|
||||||
|
import ast.ProgramNode;
|
||||||
|
import bytecode.ByteCodeGenerator;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
@ -37,8 +37,8 @@ public class Main {
|
|||||||
|
|
||||||
// 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 with identifiers/names:");
|
||||||
for (ClassDeclarationNode classNode : ast.classes) {
|
for (ClassNode classNode : ast.classes) {
|
||||||
System.out.println(classNode.identifier);
|
System.out.println(classNode.name);
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||||
|
@ -3,6 +3,7 @@ package bytecode;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import ast.ClassNode;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.objectweb.asm.Opcodes;
|
import org.objectweb.asm.Opcodes;
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
@ -10,9 +11,9 @@ import ast.ProgramNode;
|
|||||||
public class ByteCodeGenerator {
|
public class ByteCodeGenerator {
|
||||||
|
|
||||||
public void generateByteCode(ProgramNode ast) {
|
public void generateByteCode(ProgramNode ast) {
|
||||||
for (ClassDeclarationNode classDeclarationNode : ast.classes) {
|
for (ClassNode classDeclarationNode : ast.classes) {
|
||||||
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
|
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, classDeclarationNode.name, null,
|
||||||
"java/lang/Object", null);
|
"java/lang/Object", null);
|
||||||
|
|
||||||
FieldCodeGen fieldCodeGen = new FieldCodeGen();
|
FieldCodeGen fieldCodeGen = new FieldCodeGen();
|
||||||
@ -22,7 +23,7 @@ public class ByteCodeGenerator {
|
|||||||
methodCodeGen.generateMethodCode(classWriter);
|
methodCodeGen.generateMethodCode(classWriter);
|
||||||
|
|
||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.identifier);
|
printIntoClassFile(classWriter.toByteArray(), classDeclarationNode.name);
|
||||||
|
|
||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,19 @@
|
|||||||
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.junit.jupiter.api.Test;
|
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;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
|
||||||
@ -7,10 +22,14 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
|||||||
*/
|
*/
|
||||||
public class MainTest {
|
public class MainTest {
|
||||||
@Test
|
@Test
|
||||||
void testAdd() {
|
void testEmptyClass() {
|
||||||
//Calculator calculator = new Calculator();
|
CharStream codeCharStream = null;
|
||||||
//int result = calculator.add(2, 3);
|
try {
|
||||||
//assertEquals(5, result, "2 + 3 should equal 5");
|
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