From d014cb28c74a662b3ffa26d7574ea9b0dcb80240 Mon Sep 17 00:00:00 2001 From: Lucas <89882946+notbad3500@users.noreply.github.com> Date: Wed, 8 May 2024 13:10:45 +0200 Subject: [PATCH] Structure and Fixes --- src/main/java/Main.java | 10 +++---- src/main/java/bytecode/ByteCodeGenerator.java | 7 ++--- src/main/test/java/MainTest.java | 27 ++++++++++++++++--- 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/src/main/java/Main.java b/src/main/java/Main.java index ab4a4fe..10322be 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -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(); diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index b00f429..4890004 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -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(); } diff --git a/src/main/test/java/MainTest.java b/src/main/test/java/MainTest.java index c956827..46ad4ee 100644 --- a/src/main/test/java/MainTest.java +++ b/src/main/test/java/MainTest.java @@ -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()); + } } }