Structure and Fixes

This commit is contained in:
Lucas 2024-05-08 13:10:45 +02:00
parent 993bd8fc60
commit d014cb28c7
3 changed files with 32 additions and 12 deletions

View File

@ -1,13 +1,13 @@
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;
import ast.ClassNode;
import ast.ProgramNode;
import bytecode.ByteCodeGenerator;
import java.io.IOException;
import java.nio.file.Paths;
@ -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();

View File

@ -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 classDeclarationNode : 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, classDeclarationNode.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(), classDeclarationNode.name);
classWriter.visitEnd();
}

View File

@ -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());
}
}
}