Tests and Main
This commit is contained in:
parent
1d9d7e1f00
commit
8b6189ea33
@ -1,18 +1,19 @@
|
||||
import ast.ProgramNode;
|
||||
import bytecode.ByteCodeGenerator;
|
||||
import ast.ASTNode;
|
||||
import org.antlr.v4.runtime.*;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
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 ast.ProgramNode;
|
||||
import semantic.SemanticAnalyzer;
|
||||
import bytecode.ByteCodeGenerator;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.sql.SQLOutput;
|
||||
import java.util.List;
|
||||
|
||||
public class Main {
|
||||
@ -34,37 +35,86 @@ public class Main {
|
||||
// Printing the tokens
|
||||
tokenStream.fill();
|
||||
List<Token> tokens = tokenStream.getTokens();
|
||||
System.out.println("-------------------- Scanner -> tokens --------------------");
|
||||
for (Token token : tokens) {
|
||||
String tokenType = SimpleJavaLexer.VOCABULARY.getSymbolicName(token.getType());
|
||||
String tokenText = token.getText();
|
||||
// System.out.println("Token Type: " + tokenType + ", Token Text: " + tokenText);
|
||||
System.out.println(tokenType + " " + tokenText);
|
||||
}
|
||||
System.out.println();
|
||||
|
||||
/* ------------------------- Parser -> Parsetree ------------------------- */
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree tree = parser.program(); // parse the input
|
||||
ParseTree parseTree = parser.program(); // parse the input
|
||||
|
||||
// Printing the parse tree
|
||||
System.out.println ("Parse tree: " + tree.toStringTree(parser));
|
||||
System.out.println("-------------------- Parser -> Parsetree --------------------");
|
||||
System.out.println (parseTree.toStringTree(parser));
|
||||
printTree(parseTree, parser, 0);
|
||||
System.out.println();
|
||||
|
||||
/* AST builder -> AST*/
|
||||
ASTBuilder builder = new ASTBuilder();
|
||||
ProgramNode ast = (ProgramNode) builder.visit(tree); // build the AST
|
||||
/* ------------------------- AST builder -> AST ------------------------- */
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
||||
|
||||
// Printing the AST
|
||||
System.out.println("AST: " + ast.toString());
|
||||
System.out.println("-------------------- AST builder -> AST --------------------");
|
||||
//System.out.println("AST: " + ast.toString());
|
||||
printAST(abstractSyntaxTree, 0);
|
||||
System.out.println();
|
||||
|
||||
/* ------------------------- Semantic Analyzer -> Tast ------------------------- */
|
||||
SemanticAnalyzer.generateTast(ast);
|
||||
SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
|
||||
// Printing the Tast
|
||||
System.out.println("Tast generated");
|
||||
|
||||
/* ------------------------- Bytecode Generator -> Bytecode ------------------------- */
|
||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator();
|
||||
byteCodeGenerator.generateByteCode(ast);
|
||||
byteCodeGenerator.generateByteCode(abstractSyntaxTree);
|
||||
System.out.println("Bytecode generated");
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* This method is used to print the parse tree in a structured format.
|
||||
* It recursively traverses the tree and prints the rule names and text of the nodes.
|
||||
*
|
||||
* @param tree The parse tree to be printed.
|
||||
* @param parser The parser used to parse the input. It's used to get the rule names.
|
||||
* @param indent The current indentation level. It's used to format the output.
|
||||
*/
|
||||
public static void printTree(ParseTree tree, Parser parser, int indent) {
|
||||
// Create an indentation string based on the current indentation level
|
||||
String indentString = " ".repeat(indent * 2);
|
||||
|
||||
// If the tree node is an instance of RuleContext (i.e., it's an internal node),
|
||||
// print the rule name
|
||||
if (tree instanceof RuleContext) {
|
||||
int ruleIndex = ((RuleContext) tree).getRuleIndex();
|
||||
String ruleName = parser.getRuleNames()[ruleIndex];
|
||||
System.out.println(indentString + ruleName);
|
||||
} else {
|
||||
// If the tree node is not an instance of RuleContext (i.e., it's a leaf node),
|
||||
// print the text of the node
|
||||
System.out.println(indentString + tree.getText());
|
||||
}
|
||||
|
||||
// Recursively print the children of the current node, increasing the indentation level
|
||||
for (int i = 0; i < tree.getChildCount(); i++) {
|
||||
printTree(tree.getChild(i), parser, indent + 1);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static void printAST(ASTNode node, int indent) {
|
||||
String indentString = " ".repeat(indent * 2);
|
||||
System.out.println(indentString + node.getClass().toString());
|
||||
|
||||
for (ASTNode child : node.) {
|
||||
printAST(child, indent + 1);
|
||||
}
|
||||
}
|
||||
}
|
@ -17,6 +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 "<EOF>"]
|
||||
|
||||
Bsp von Ihm mal:
|
||||
[TokPublic,TokClass,TokIdentifier "Name",TokLeftBrace,TokRightBrace]
|
||||
|
||||
### Beispiel 2: Filled Class
|
||||
|
6
src/main/test/resources/failureTests/TestClass1.java
Normal file
6
src/main/test/resources/failureTests/TestClass1.java
Normal file
@ -0,0 +1,6 @@
|
||||
// Syntax Error: Missing semicolon
|
||||
public class TestClass1 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!") // Missing semicolon here
|
||||
}
|
||||
}
|
10
src/main/test/resources/failureTests/TestClass10.java
Normal file
10
src/main/test/resources/failureTests/TestClass10.java
Normal file
@ -0,0 +1,10 @@
|
||||
// Semantic Error: Non-static method called from static context
|
||||
public class TestClass10 {
|
||||
public static void main(String[] args) {
|
||||
greet(); // Non-static method 'greet' cannot be referenced from a static context
|
||||
}
|
||||
|
||||
public void greet() {
|
||||
System.out.println("Hi!");
|
||||
}
|
||||
}
|
10
src/main/test/resources/failureTests/TestClass11.java
Normal file
10
src/main/test/resources/failureTests/TestClass11.java
Normal file
@ -0,0 +1,10 @@
|
||||
//Compile error: (4, 9) java: variable number is already defined in method main(java.lang.String[])
|
||||
public class TestClass10 {
|
||||
public static void main(String[] args) {
|
||||
// Declare and initialize an integer variable named 'number' with the value 12
|
||||
int number = 12;
|
||||
|
||||
// This line will cause a compile-time error because 'number' is already defined in this scope
|
||||
int number =13;
|
||||
}
|
||||
}
|
6
src/main/test/resources/failureTests/TestClass2.java
Normal file
6
src/main/test/resources/failureTests/TestClass2.java
Normal file
@ -0,0 +1,6 @@
|
||||
// Syntax Error: Unclosed string literal
|
||||
public class TestClass2 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!); // Unclosed string literal
|
||||
}
|
||||
}
|
10
src/main/test/resources/failureTests/TestClass3.java
Normal file
10
src/main/test/resources/failureTests/TestClass3.java
Normal file
@ -0,0 +1,10 @@
|
||||
// Syntax Error: Missing parentheses in method declaration
|
||||
public class TestClass3 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
|
||||
public void greet { // Missing parentheses
|
||||
System.out.println("Hi!");
|
||||
}
|
||||
}
|
4
src/main/test/resources/failureTests/TestClass4.java
Normal file
4
src/main/test/resources/failureTests/TestClass4.java
Normal file
@ -0,0 +1,4 @@
|
||||
// Syntax Error: Missing class body
|
||||
public class TestClass4 {
|
||||
// Missing class body
|
||||
}
|
7
src/main/test/resources/failureTests/TestClass5.java
Normal file
7
src/main/test/resources/failureTests/TestClass5.java
Normal file
@ -0,0 +1,7 @@
|
||||
// Semantic Error: Variable used before declaration
|
||||
public class TestClass5 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println(number); // Variable 'number' used before declaration
|
||||
int number = 10;
|
||||
}
|
||||
}
|
10
src/main/test/resources/failureTests/TestClass6.java
Normal file
10
src/main/test/resources/failureTests/TestClass6.java
Normal file
@ -0,0 +1,10 @@
|
||||
// Semantic Error: Method with wrong return type
|
||||
public class TestClass6 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
|
||||
public int greet() { // Method should return int, but no return statement is provided
|
||||
System.out.println("Hi!");
|
||||
}
|
||||
}
|
5
src/main/test/resources/failureTests/TestClass7.java
Normal file
5
src/main/test/resources/failureTests/TestClass7.java
Normal file
@ -0,0 +1,5 @@
|
||||
// Syntax Error: Unmatched braces
|
||||
public class TestClass7 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!");
|
||||
// Missing closing brace for the class
|
14
src/main/test/resources/failureTests/TestClass8.java
Normal file
14
src/main/test/resources/failureTests/TestClass8.java
Normal file
@ -0,0 +1,14 @@
|
||||
// Semantic Error: Duplicate method definition
|
||||
public class TestClass8 {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello, World!");
|
||||
}
|
||||
|
||||
public void greet() {
|
||||
System.out.println("Hi!");
|
||||
}
|
||||
|
||||
public void greet() { // Duplicate method definition
|
||||
System.out.println("Hello!");
|
||||
}
|
||||
}
|
6
src/main/test/resources/failureTests/TestClass9.java
Normal file
6
src/main/test/resources/failureTests/TestClass9.java
Normal file
@ -0,0 +1,6 @@
|
||||
// Syntax Error: Incompatible types
|
||||
public class TestClass9 {
|
||||
public static void main(String[] args) {
|
||||
int number = "Hello"; // Incompatible types: String cannot be converted to int
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user