diff --git a/.idea/misc.xml b/.idea/misc.xml index f26d89f..eddc20d 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -40,7 +40,7 @@ - + \ No newline at end of file diff --git a/src/main/java/Main.java b/src/main/java/Main.java index 7f55626..24f129b 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,7 +1,6 @@ import ast.ASTNode; import org.antlr.v4.runtime.*; import org.antlr.v4.runtime.Token; -import ast.ClassNode; import ast.ProgramNode; import bytecode.ByteCodeGenerator; import org.antlr.v4.runtime.CharStream; @@ -11,7 +10,6 @@ import org.antlr.v4.runtime.CommonTokenStream; import parser.ASTBuilder; import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaParser; -import ast.ProgramNode; import semantic.SemanticAnalyzer; import bytecode.ByteCodeGenerator; @@ -23,12 +21,13 @@ public class Main { public static void main(String[] args) throws Exception { try { CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/java/CompilerInput.txt")); - parseFile(codeCharStream); + parsefile(codeCharStream); } catch (IOException e) { System.err.println("Error reading the file: " + e.getMessage()); } } + static void parsefile(CharStream codeCharStream) { /* ------------------------- Scanner -> tokens ------------------------- */ SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream); @@ -56,7 +55,6 @@ public class Main { System.out.println(parseTree.toStringTree(parser)); printTree(parseTree, parser, 0); System.out.println(); - ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(ast); /* ------------------------- AST builder -> AST ------------------------- */ ASTBuilder astBuilder = new ASTBuilder(); @@ -72,6 +70,7 @@ public class Main { * ------------------------- Semantic Analyzer -> Tast ------------------------- */ SemanticAnalyzer.generateTast(abstractSyntaxTree); + ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(abstractSyntaxTree); // Printing the Tast System.out.println("Tast generated"); @@ -81,7 +80,8 @@ public class Main { * ------------------------- */ ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(); - byteCodeGenerator.generateByteCode(abstractSyntaxTree); + //byteCodeGenerator.generateByteCode(abstractSyntaxTree); + byteCodeGenerator.visit(typedAst); System.out.println("Bytecode generated"); } @@ -126,6 +126,5 @@ public class Main { // for (ASTNode child : node.) { // printAST(child, indent + 1); // } - byteCodeGenerator.visit(typedAst); } } \ No newline at end of file diff --git a/src/main/java/ast/ASTNode.java b/src/main/java/ast/ASTNode.java index 9944050..3ef48fa 100644 --- a/src/main/java/ast/ASTNode.java +++ b/src/main/java/ast/ASTNode.java @@ -1,7 +1,12 @@ package ast; -public interface ASTNode { +//import java.util.List; +public interface ASTNode { + /** + * Please implement this method to return a list of children of each node. + */ + // public List getChildren(); } diff --git a/src/main/java/semantic/SemanticAnalyzer.java b/src/main/java/semantic/SemanticAnalyzer.java index 7a6002b..30271a6 100644 --- a/src/main/java/semantic/SemanticAnalyzer.java +++ b/src/main/java/semantic/SemanticAnalyzer.java @@ -68,7 +68,7 @@ public class SemanticAnalyzer implements SemanticVisitor { public TypeCheckResult analyze(MethodNode methodNode) { var valid = true; - currentLocalScope.pushScope(); + // currentLocalScope.pushScope(); List statements = methodNode.statements; for (StatementNode statement : statements) { diff --git a/src/main/java/classFileOutput/EmptyClassExample.class b/src/main/test/java/EmptyClassExample.class similarity index 100% rename from src/main/java/classFileOutput/EmptyClassExample.class rename to src/main/test/java/EmptyClassExample.class diff --git a/src/main/test/resources/EmptyClassExample.java b/src/main/test/java/EmptyClassExample.java similarity index 75% rename from src/main/test/resources/EmptyClassExample.java rename to src/main/test/java/EmptyClassExample.java index 9bc5365..9f54ec6 100644 --- a/src/main/test/resources/EmptyClassExample.java +++ b/src/main/test/java/EmptyClassExample.java @@ -1,6 +1,4 @@ -package resources; - public class EmptyClassExample { private class Inner { } -} \ No newline at end of file +} // -o für outout \ No newline at end of file diff --git a/src/main/test/java/FailureTest.java b/src/main/test/java/FailureTest.java new file mode 100644 index 0000000..bc63d91 --- /dev/null +++ b/src/main/test/java/FailureTest.java @@ -0,0 +1,72 @@ +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CharStreams; +import org.junit.jupiter.api.Test; + +import javax.tools.JavaCompiler; +import javax.tools.ToolProvider; +import java.io.File; +import java.io.IOException; +import java.nio.file.Paths; +import java.util.Arrays; +import java.util.List; + +public class FailureTest { + private static final List TEST_FILES = Arrays.asList( + "src/main/test/resources/failureTests/TestClass1.java", + "src/main/test/resources/failureTests/TestClass2.java", + "src/main/test/resources/failureTests/TestClass3.java", + "src/main/test/resources/failureTests/TestClass4.java", + "src/main/test/resources/failureTests/TestClass5.java", + "src/main/test/resources/failureTests/TestClass6.java", + "src/main/test/resources/failureTests/TestClass7.java", + "src/main/test/resources/failureTests/TestClass8.java", + "src/main/test/resources/failureTests/TestClass9.java", + "src/main/test/resources/failureTests/TestClass10.java", + "src/main/test/resources/failureTests/TestClass11.java" + ); + + /** + * This test method checks if invalid Java files fail to compile as expected. + * It uses the JavaCompiler from the ToolProvider to compile the files. + * The test passes if all the files fail to compile. + */ + @Test + public void invalidJavaFilesTest() { + // Get the system Java compiler + JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); + + // Assert that the compiler is available + assertNotNull(compiler, "Java Compiler is not available"); + + // Iterate over the test files + for (String fileName : TEST_FILES) { + // Create a File object for the current file + File file = new File(fileName); + + // Try to compile the file and get the result + // The run method returns 0 if the compilation was successful, and non-zero otherwise + int result = compiler.run(null, null, null, file.getPath()); + + // Assert that the compilation failed (i.e., the result is non-zero) + assertTrue(result != 0, "Expected compilation failure for " + fileName); + } + } + + // schmeißt John Fehler, wenn namen doppelt sind? + // Input: ParseTree mit genanntem Fehler + // Output: Fehlermeldung + @Test + void typedASTTest() throws IOException { + CharStream codeCharStream = null; + try { + codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/resources/EmptyClassExample.java")); + Main.parsefile(codeCharStream); + } catch (IOException e) { + System.err.println("Error reading the file: " + e.getMessage()); + } + } +} + diff --git a/src/main/test/java/Tester.java b/src/main/test/java/Tester.java index f68ae7a..777c50b 100644 --- a/src/main/test/java/Tester.java +++ b/src/main/test/java/Tester.java @@ -1,6 +1,11 @@ -//import main.test.resources.EmptyClassExample.java public class Tester { public static void main(String[] args) { - // new EmptyClassExample(); + new EmptyClassExample(); + // 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/main/test/java/make.md b/src/main/test/java/make.md new file mode 100644 index 0000000..e69de29 diff --git a/src/main/test/resources/failureTests/TestClass4.java b/src/main/test/resources/failureTests/TestClass4.java index cf18bf8..f1ee5ec 100644 --- a/src/main/test/resources/failureTests/TestClass4.java +++ b/src/main/test/resources/failureTests/TestClass4.java @@ -1,4 +1,4 @@ // Syntax Error: Missing class body -public class TestClass4 { +public class TestClass4 {- // Missing class body }