From 8d6190b1304e82b3a3625f3d18dc475aea452ebe Mon Sep 17 00:00:00 2001 From: Lucas <89882946+notbad3500@users.noreply.github.com> Date: Fri, 31 May 2024 17:09:04 +0200 Subject: [PATCH] Structure, Makefile, Docs, TestCompilerOutput, more; TODO: fix marked Problems in Makefile; fix Compiler (look at TestCompilerOutput docs) --- .../antlr-4.12.0-complete.jar | Bin pom.xml | 4 +- src/main/java/Main.java | 12 ++--- src/main/java/bytecode/ClassCodeGen.java | 2 +- src/main/resources/CompilerInput.java | 18 +++---- src/test/Makefile | 24 +++++++++ src/test/TestSpecs.md | 51 +++++++++++------- src/test/java/TestCompilerOutput.java | 41 ++++++++++++++ src/test/java/Tester.java | 12 ----- src/test/java/make.md | 0 src/test/resources/CompilerInput.java | 14 ++--- 11 files changed, 117 insertions(+), 61 deletions(-) rename antlr-4.12.0-complete.jar => .lib/antlr-4.12.0-complete.jar (100%) create mode 100644 src/test/Makefile create mode 100644 src/test/java/TestCompilerOutput.java delete mode 100644 src/test/java/Tester.java delete mode 100644 src/test/java/make.md diff --git a/antlr-4.12.0-complete.jar b/.lib/antlr-4.12.0-complete.jar similarity index 100% rename from antlr-4.12.0-complete.jar rename to .lib/antlr-4.12.0-complete.jar diff --git a/pom.xml b/pom.xml index 9ee63e3..a11bfc7 100644 --- a/pom.xml +++ b/pom.xml @@ -9,8 +9,8 @@ 1.0-SNAPSHOT - 21 - 21 + 22 + 22 UTF-8 diff --git a/src/main/java/Main.java b/src/main/java/Main.java index c78f3ea..acea692 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,18 +1,14 @@ import ast.ASTNode; -import org.antlr.v4.runtime.*; -import org.antlr.v4.runtime.Token; import ast.ProgramNode; -import bytecode.ByteCodeGenerator; -import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.CharStreams; -import org.antlr.v4.runtime.tree.ParseTree; -import org.antlr.v4.runtime.CommonTokenStream; import parser.ASTBuilder; import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaParser; import semantic.SemanticAnalyzer; import bytecode.ByteCodeGenerator; +import org.antlr.v4.runtime.*; +import org.antlr.v4.runtime.tree.ParseTree; + import java.io.IOException; import java.nio.file.Paths; import java.util.List; @@ -20,7 +16,7 @@ import java.util.List; public class Main { public static void main(String[] args) throws Exception { if (args.length > 0) { - + System.out.println("Main file has args: " + args[0]); } else { try { CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/resources/CompilerInput.java")); diff --git a/src/main/java/bytecode/ClassCodeGen.java b/src/main/java/bytecode/ClassCodeGen.java index b0afa69..6bddd61 100644 --- a/src/main/java/bytecode/ClassCodeGen.java +++ b/src/main/java/bytecode/ClassCodeGen.java @@ -52,7 +52,7 @@ public class ClassCodeGen implements ClassVisitor { } private void printIntoClassFile(byte[] byteCode, String name) { - String directoryPath = "src/main/java/classFileOutput"; + String directoryPath = "src/main/resources/classFileOutput"; File directory = new File(directoryPath); if (!directory.exists()) { directory.mkdirs(); diff --git a/src/main/resources/CompilerInput.java b/src/main/resources/CompilerInput.java index 1cbe5ca..d850a3e 100644 --- a/src/main/resources/CompilerInput.java +++ b/src/main/resources/CompilerInput.java @@ -1,18 +1,16 @@ -public class Example { +public class CompilerInput { public int a; public static int testMethod(char x){ - + return 0; } + public class Test { + + public static int testMethod(char x, int a){ + return 0; + } + } } -public class Test { - - public static int testMethod(char x, int a){ - - - - } -} \ No newline at end of file diff --git a/src/test/Makefile b/src/test/Makefile new file mode 100644 index 0000000..da39d6b --- /dev/null +++ b/src/test/Makefile @@ -0,0 +1,24 @@ +# Makefile +### IntelliJs play buttons do not work. Run in "src/test" folder with "make" command to run all +### Or run only parts with "make compile-javac", "make delete" etc. + +all: delete compile-javac compile-raupenpiler + +compile-javac: + javac -d .\resources\output\javac .\resources\CompilerInput.java + +compile-raupenpiler: +## funktioniert bisher nicht, das will die Klasse nicht laden, der traditionelle Weg findet externe Libraries (antlr) nicht, maven hat andere Probleme +## Unseren Compiler also erstmal händisch starten: main.java/Main.java + #javac -d ./resources/output/raupenpiler -cp ../main/java;../../.lib/antlr-4.12.0-complete.jar ../main/java/Main.java + #java -cp ./resources/output/raupenpiler Main + #mvn -f ../../ compile + #mvn -f ../../ exec:java -Dexec.mainClass="Main" + +delete: + rm -f ./resources/output/javac/*.class + rm -f ./resources/output/raupenpiler/*.class + rm -f ./java/*.class + rm -f ../main/resources/classFileOutput/*.class + + diff --git a/src/test/TestSpecs.md b/src/test/TestSpecs.md index 7ae3416..75f979a 100644 --- a/src/test/TestSpecs.md +++ b/src/test/TestSpecs.md @@ -1,5 +1,7 @@ # Scanner + ## Scanner Input + ### Beispiel 1: Empty Class String empty class = "public class Name {}"; @@ -15,11 +17,12 @@ "}" ## Scanner Output + ### Beispiel 1: Empty Class Token Type; Token Text Type gibts nur bei Terminalen, Text bei allen - + [null "public", null "class", IDENTIFIER "Name", null "{", null "}", EOF ""] Bsp von Ihm mal: @@ -35,52 +38,62 @@ Type gibts nur bei Terminalen, Text bei allen [TokRightBrace] # Parser -## Parser Input + +## Parser Input + (Scanner Output) ## Parser Output (AST) -### Beispiel 1: Empty Class - +### Beispiel 1: Empty Class ### Beispiel 2: Filled Class - - # Semantische Analyse / Typcheck -## Typcheck Input + +## Typcheck Input + (Parser Output = AST) ## Typcheck Output ### Beispiel 1: Empty Class - - ### Beispiel 2: Filled Class - # Bytecodegenerierung -## Bytecodegenerierung Input + +## Bytecodegenerierung Input + (Typcheck Output = vom Typcheck eventuell manipulierter AST) ## Bytecodegenerierung Output ### Beispiel 1: Empty Class + Compiled Classfile public class javaFileInput.Example { } - - ### Beispiel 2: Filled Class +## E2E Tests: - für E2E Tests: - Testdatei mit Main ausführen/kompilieren - Testdatei mit "javac CompilerInput.java" kompilieren +- Testdatei mit Main ausführen/kompilieren +- Testdatei mit "javac -d output .\CompilerInput.java" kompilieren +- -> Dateien mit javap vergleichen - wenn beides erfolgreich - Ergebnis vom eigenen Compiler mit "java myOutput" ausführen - Ergebnis von javac mit "java CompilerInput" ausführen +wenn beides erfolgreich +- Ergebnis vom eigenen Compiler mithilfe von TestCompilerOutput ausführen +- (Ergebnis von javac mithilfe von TestCompilerOutput ausführen) + + +### Andis Tipps: +- cp mitgeben +- makefile +- java -jar pfadtocompiler.jar EmptyClass.java +- mvn package +- javac tester // tester compilen +- java tester // tester ausführen +- -> tester ist in unserem Fall TestCompilerOutput.java \ No newline at end of file diff --git a/src/test/java/TestCompilerOutput.java b/src/test/java/TestCompilerOutput.java new file mode 100644 index 0000000..cf6aac0 --- /dev/null +++ b/src/test/java/TestCompilerOutput.java @@ -0,0 +1,41 @@ +/** + * This class is used to test the output of the compiler. + *

+ * Im gleichen Ordner wie die diese Datei (TestCompilerOutput.java) muss die selbstkompilierte CompilerInput.java Datei sein. + * --->>>> Diese muss man also vom Ordner classFileOutput in diesen ordner hier (test/java) rein kopieren. (bis es eine bessere Lösung gibt) <<<<--- + * Die selbstkompilierte .class Datei wird dann hier drin geladen und eine Instanz von ihr erstellt, es können auch Methoden aufgerufen werden. + * Diese TestCompilerOutput.java Datei wird dann mit "javac .\TestCompilerOutput.java" kompiliert und mit "java TestCompilerOutput" ausgeführt. + * Wenn unser Compiler funktioniert, sollten keine Errors kommen (sondern nur die Ausgaben, die wir in der CompilerInput.java Datei gemacht haben, + * oder Methoden, die wir hier aufrufen). + * PROBLEM: hier kommen errors, was eigentlich heißt, dass der Compiler nicht funktioniert, der der Test sollt eigentlich passen + */ +public class TestCompilerOutput { + + public static void main(String[] args) { + try { + // Try to load the class named "CompilerInput" + Class cls = Class.forName("CompilerInput"); + // Print a success message if the class is loaded successfully + System.out.println("Class loaded successfully: " + cls.getName()); + + // Try to create an instance of the loaded class + Object instance = cls.getDeclaredConstructor().newInstance(); + // Print a success message if the instance is created successfully + System.out.println("Instance created: " + instance); + + + // If the class has a main method, you can invoke it + // cls.getMethod("main", String[].class).invoke(null, (Object) new String[]{}); + + // If the class has other methods, you can invoke them as well + // Example: cls.getMethod("someMethod").invoke(instance); + + } catch (ClassNotFoundException e) { + // Print an error message if the class is not found + System.err.println("Class not found: " + e.getMessage()); + } catch (Exception e) { + // Print an error message if any other exception occurs during class loading or instance creation + System.err.println("Error during class loading or execution: " + e.getMessage()); + } + } +} diff --git a/src/test/java/Tester.java b/src/test/java/Tester.java deleted file mode 100644 index 2c62e40..0000000 --- a/src/test/java/Tester.java +++ /dev/null @@ -1,12 +0,0 @@ -public class Tester { - public static void main(String[] args) { - - - } -} -// cp mitgeben -// java -jar pfadtocompiler.jar EmptyClass.java -// mit bash scipt ode rmakefile test automatisieren -// mvn package -// javac tester // tester compilen -// java tester // tester ausführen \ No newline at end of file diff --git a/src/test/java/make.md b/src/test/java/make.md deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/resources/CompilerInput.java b/src/test/resources/CompilerInput.java index 918c686..91b2625 100644 --- a/src/test/resources/CompilerInput.java +++ b/src/test/resources/CompilerInput.java @@ -1,13 +1,9 @@ -public class Example { - - public int testVar; - - public static int testMethod(char b){ - - int a; - int a; +public class CompilerInput { + public int a; + public static int testMethod(char x){ + return 0; } +} -} \ No newline at end of file