More Tests, Structure, etc. Huge Changes #14
@ -20,13 +20,11 @@ import java.nio.file.Paths;
|
|||||||
* <p> <code> cd .\src\test\ </code>
|
* <p> <code> cd .\src\test\ </code>
|
||||||
* <p> <code> make clean compile-raupenpiler </code>
|
* <p> <code> make clean compile-raupenpiler </code>
|
||||||
* <p> Start Raupenpiler using jar:
|
* <p> Start Raupenpiler using jar:
|
||||||
* <p> <code> java.exe -jar path_to_jar\JavaCompiler-1.0-SNAPSHOT-jar-with-dependencies.jar 'path_to_input_file.java' 'path_to_output_directory' </code>
|
* <p> <code> java.exe -jar path_to_jar\JavaCompiler-1.0-jar-with-dependencies.jar 'path_to_input_file.java' 'path_to_output_directory' </code>
|
||||||
* <p> Example (jar needs to be in the target directory, compile with make or mvn package first):
|
* <p> Example (jar needs to be in the target directory, compile with make or mvn package first):
|
||||||
* <code> java.exe -jar .\target\JavaCompiler-1.0-SNAPSHOT-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' </code>
|
* <code> java.exe -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' </code>
|
||||||
*/
|
*/
|
||||||
public class Main {
|
public class Main {
|
||||||
|
|
||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
if (args.length == 2) {
|
if (args.length == 2) {
|
||||||
// args[0] is the input file path
|
// args[0] is the input file path
|
||||||
|
@ -1,18 +1,11 @@
|
|||||||
package parser;
|
package parser;
|
||||||
|
|
||||||
import ast.BlockNode;
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.LiteralNode;
|
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import ast.expression.ExpressionNode;
|
|
||||||
import ast.member.FieldNode;
|
import ast.member.FieldNode;
|
||||||
import ast.member.MemberNode;
|
|
||||||
import ast.member.MethodNode;
|
import ast.member.MethodNode;
|
||||||
import ast.parameter.ParameterListNode;
|
|
||||||
import ast.parameter.ParameterNode;
|
import ast.parameter.ParameterNode;
|
||||||
import ast.statement.ReturnStatementNode;
|
import ast.statement.ReturnStatementNode;
|
||||||
import ast.statement.StatementNode;
|
|
||||||
import ast.type.*;
|
|
||||||
import org.antlr.v4.runtime.*;
|
import org.antlr.v4.runtime.*;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
import org.antlr.v4.runtime.tree.ParseTree;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
@ -49,9 +42,9 @@ public class ParserTest {
|
|||||||
tokenStream.fill();
|
tokenStream.fill();
|
||||||
|
|
||||||
// Prepare the expected results
|
// Prepare the expected results
|
||||||
List<Token> actualTokens = tokenStream.getTokens();
|
|
||||||
List<String> expectedTokens = Arrays.asList("public", "class", "Name", "{", "}", "<EOF>");
|
List<String> expectedTokens = Arrays.asList("public", "class", "Name", "{", "}", "<EOF>");
|
||||||
List<String> expectedTokenTypes = Arrays.asList(null, null, "IDENTIFIER", null, null, "EOF");
|
List<String> expectedTokenTypes = Arrays.asList("AccessModifier", "Class", "Identifier", "OpenCurlyBracket", "ClosedCurlyBracket", "EOF");
|
||||||
|
List<Token> actualTokens = tokenStream.getTokens();
|
||||||
|
|
||||||
// Compare the actual tokens and their types to the expected tokens and their types
|
// Compare the actual tokens and their types to the expected tokens and their types
|
||||||
assertEquals(expectedTokens.size(), actualTokens.size());
|
assertEquals(expectedTokens.size(), actualTokens.size());
|
||||||
@ -76,33 +69,32 @@ public class ParserTest {
|
|||||||
ParseTree parseTree = parser.program(); // parse the input
|
ParseTree parseTree = parser.program(); // parse the input
|
||||||
|
|
||||||
//Variante 1 (geht)
|
//Variante 1 (geht)
|
||||||
|
String expectedParseTreeAsString = "(program (classDeclaration public class Name { }))";
|
||||||
String actualParseTreeAsString = parseTree.toStringTree(parser);
|
String actualParseTreeAsString = parseTree.toStringTree(parser);
|
||||||
String expectedParseTreeAsString = "(program (classDeclaration (accessType public) class Name { }))";
|
|
||||||
|
|
||||||
assertEquals(actualParseTreeAsString, expectedParseTreeAsString);
|
assertEquals(expectedParseTreeAsString, actualParseTreeAsString);
|
||||||
|
|
||||||
// Variante 2 (geht nicht)
|
// Variante 2 (geht nicht)
|
||||||
// - Sollte es gehen und es liegt am Parser? (keine Ahnung) -> Bitte Fehler (actual und expected) durchlesen
|
// - Sollte es gehen und es liegt am Parser? (keine Ahnung) -> Bitte Fehler (actual und expected) durchlesen
|
||||||
Map<String, Object> actualTreeStructure = buildTreeStructure(parseTree, parser);
|
// ist die Methode parseStringToTree() korrekt? -> (glaub nicht)
|
||||||
Map<String, Object> expectedTreeStructure = parseStringToTree(expectedParseTreeAsString);
|
Map<String, Object> expectedTreeStructure = parseStringToTree(expectedParseTreeAsString);
|
||||||
|
Map<String, Object> actualTreeStructure = buildTreeStructure(parseTree, parser);
|
||||||
|
|
||||||
assertEquals(actualTreeStructure, expectedTreeStructure);
|
// assertEquals(expectedTreeStructure, actualTreeStructure);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void astBuilderTest() {
|
public void astBuilderTest() {
|
||||||
// TODO: Implement this test method
|
// TODO: Implement this test method
|
||||||
|
|
||||||
// ---------------- Aktuellen CompilerInput nachbauen ----------------
|
// ---------------- Alter CompilerInput nachgebaut ----------------
|
||||||
ProgramNode startNode = new ProgramNode();
|
// ProgramNode startNode = new ProgramNode();
|
||||||
// public class CompilerInput {}
|
// public class CompilerInput {}
|
||||||
ClassNode compilerInputClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "CompilerInput");
|
// ClassNode compilerInputClass = new ClassNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), "CompilerInput");
|
||||||
// public int a;
|
// public int a;
|
||||||
compilerInputClass.addMember(new FieldNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), new BaseTypeNode(EnumTypeNode.INT), "a"));
|
// compilerInputClass.addMember(new FieldNode(new AccessTypeNode(EnumAccessTypeNode.PUBLIC), new BaseTypeNode(EnumTypeNode.INT), "a"));
|
||||||
// public static int testMethod(char x) { return 0; }
|
// public static int testMethod(char x) { return 0; }
|
||||||
compilerInputClass.addMember(
|
/* compilerInputClass.addMember(
|
||||||
new MethodNode(
|
new MethodNode(
|
||||||
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
new AccessTypeNode(EnumAccessTypeNode.PUBLIC),
|
||||||
new BaseTypeNode(EnumTypeNode.INT),
|
new BaseTypeNode(EnumTypeNode.INT),
|
||||||
@ -122,28 +114,48 @@ public class ParserTest {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
*/
|
||||||
|
|
||||||
//compilerInputClass.addClass(testClass);
|
//compilerInputClass.addClass(testClass);
|
||||||
|
|
||||||
startNode.addClass(compilerInputClass);
|
// startNode.addClass(compilerInputClass);
|
||||||
startNode.addClass(testClass);
|
// startNode.addClass(testClass);
|
||||||
|
|
||||||
// ---------------- Aktuellen CompilerInput nachbauen ----------------
|
|
||||||
|
|
||||||
|
|
||||||
|
// ---------------- Leere Klasse nachgebaut ----------------
|
||||||
|
|
||||||
|
ProgramNode expectedASTEmptyClass = new ProgramNode();
|
||||||
|
|
||||||
|
// public class Name {}
|
||||||
|
ClassNode nameClass = new ClassNode("public", "Name");
|
||||||
|
|
||||||
|
expectedASTEmptyClass.addClass(nameClass);
|
||||||
|
|
||||||
|
|
||||||
|
// ---------------- Leere Klasse erzeugt ----------------
|
||||||
|
|
||||||
|
// init
|
||||||
|
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
||||||
|
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
||||||
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
|
tokenStream.fill();
|
||||||
|
|
||||||
|
/* Parser -> Parsetree */
|
||||||
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
|
ParseTree parseTreeEmptyClass = parser.program(); // parse the input
|
||||||
|
|
||||||
/* AST builder -> AST */
|
/* AST builder -> AST */
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
// ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
ProgramNode actualASTEmptyClass = (ProgramNode) new ASTBuilder().visit(parseTreeEmptyClass);
|
||||||
|
|
||||||
//String actualASTasString = new ASTBuilder().visit(parseTree).toString();
|
|
||||||
|
|
||||||
// ProgramNode actualAST = new ASTBuilder().visit(parseTree);
|
// ---------------- Vergleichen ----------------
|
||||||
// ProgramNode expectedAST = new ProgramNode();
|
|
||||||
// expectedAST.add(new ProgramNode.ClassNode("Name", new ProgramNode()));
|
String expectedASTasString = expectedASTEmptyClass.toString();
|
||||||
|
String actualASTasString = new ASTBuilder().visit(parseTreeEmptyClass).toString();
|
||||||
|
|
||||||
|
// Wie vergleiche ich das?
|
||||||
|
assertEquals(expectedASTasString, actualASTasString);
|
||||||
|
assertEquals(expectedASTEmptyClass, actualASTEmptyClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -196,7 +208,7 @@ public class ParserTest {
|
|||||||
for (char ch : input.toCharArray()) {
|
for (char ch : input.toCharArray()) {
|
||||||
if (ch == '(') {
|
if (ch == '(') {
|
||||||
if (depth == 0) {
|
if (depth == 0) {
|
||||||
if (currentToken.length() > 0) {
|
if (!currentToken.isEmpty()) {
|
||||||
node.put("node", currentToken.toString().trim());
|
node.put("node", currentToken.toString().trim());
|
||||||
currentToken.setLength(0);
|
currentToken.setLength(0);
|
||||||
}
|
}
|
||||||
@ -213,7 +225,7 @@ public class ParserTest {
|
|||||||
currentToken.append(ch);
|
currentToken.append(ch);
|
||||||
}
|
}
|
||||||
} else if (Character.isWhitespace(ch) && depth == 0) {
|
} else if (Character.isWhitespace(ch) && depth == 0) {
|
||||||
if (currentToken.length() > 0) {
|
if (!currentToken.isEmpty()) {
|
||||||
node.put("node", currentToken.toString().trim());
|
node.put("node", currentToken.toString().trim());
|
||||||
currentToken.setLength(0);
|
currentToken.setLength(0);
|
||||||
}
|
}
|
||||||
@ -222,7 +234,7 @@ public class ParserTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentToken.length() > 0) {
|
if (!currentToken.isEmpty()) {
|
||||||
node.put("node", currentToken.toString().trim());
|
node.put("node", currentToken.toString().trim());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package resources;
|
|
||||||
|
|
||||||
public class AllFeaturesClassExample {
|
public class AllFeaturesClassExample {
|
||||||
int a;
|
int a;
|
||||||
boolean b;
|
boolean b;
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package resources;
|
|
||||||
|
|
||||||
public class CombinedExample {
|
public class CombinedExample {
|
||||||
int number;
|
int number;
|
||||||
boolean flag;
|
boolean flag;
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package resources;
|
|
||||||
|
|
||||||
public class MoreFeaturesClassExample {
|
public class MoreFeaturesClassExample {
|
||||||
int hallo;
|
int hallo;
|
||||||
private class Inner {
|
private class Inner {
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package resources.featureTests;
|
|
||||||
|
|
||||||
public class BooleanOperations {
|
public class BooleanOperations {
|
||||||
boolean flag;
|
boolean flag;
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package resources.featureTests;
|
|
||||||
|
|
||||||
public class CharManipulation {
|
public class CharManipulation {
|
||||||
char letter;
|
char letter;
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package resources.featureTests;
|
|
||||||
|
|
||||||
public class ConditionalStatements {
|
public class ConditionalStatements {
|
||||||
int number;
|
int number;
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package resources.featureTests;
|
|
||||||
|
|
||||||
public class LoopExamples {
|
public class LoopExamples {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// For loop example
|
// For loop example
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
package resources.featureTests;
|
|
||||||
|
|
||||||
public class MethodOverloading {
|
public class MethodOverloading {
|
||||||
public int add(int a, int b) {
|
public int add(int a, int b) {
|
||||||
return a + b;
|
return a + b;
|
||||||
|
Loading…
Reference in New Issue
Block a user