code-generator #13
@ -15,11 +15,6 @@ public class ConstructorNode extends MethodNode implements Visitable {
|
|||||||
public List<ParameterNode> parameters = new ArrayList<>();
|
public List<ParameterNode> parameters = new ArrayList<>();
|
||||||
public BlockNode block;
|
public BlockNode block;
|
||||||
|
|
||||||
public ConstructorNode(AccessModifierNode accessType, String identifier) {
|
|
||||||
this.accessType = accessType;
|
|
||||||
this.identifier = identifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
||||||
this.accessType = new AccessModifierNode(accessType);
|
this.accessType = new AccessModifierNode(accessType);
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
|
@ -63,7 +63,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(!hasConstructor){
|
if(!hasConstructor){
|
||||||
classNode.members.addFirst(new ConstructorNode(new AccessModifierNode("public"), ctx.Identifier().getText()));
|
BlockNode blockNode = new BlockNode();
|
||||||
|
blockNode.addStatement(new ReturnNode(null));
|
||||||
|
classNode.members.addFirst(new ConstructorNode("public", ctx.Identifier().getText(), blockNode));
|
||||||
}
|
}
|
||||||
|
|
||||||
return classNode;
|
return classNode;
|
||||||
@ -111,7 +113,11 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
|
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
|
||||||
return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
|
if(ctx.AccessModifier() != null) {
|
||||||
|
return new FieldNode(new AccessModifierNode(ctx.AccessModifier().getText()), createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
|
||||||
|
} else {
|
||||||
|
return new FieldNode(null, createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -201,10 +207,17 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
crement = (IStatementNode) visit(ctx.statementExpression(i));
|
crement = (IStatementNode) visit(ctx.statementExpression(i));
|
||||||
}
|
}
|
||||||
|
|
||||||
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
|
BlockNode forBlock = new BlockNode();
|
||||||
|
|
||||||
|
BlockNode forStatements = (BlockNode) visit(ctx.blockStatement());
|
||||||
|
if(forStatements != null) {
|
||||||
|
forBlock.addStatement((IStatementNode) forStatements);
|
||||||
|
}
|
||||||
|
|
||||||
if(crement != null){
|
if(crement != null){
|
||||||
forBlock.addStatement((crement));
|
BlockNode forCrement = new BlockNode();
|
||||||
|
forCrement.addStatement((crement));
|
||||||
|
forBlock.addStatement(forCrement);
|
||||||
}
|
}
|
||||||
|
|
||||||
WhileNode While = new WhileNode(condition, forBlock);
|
WhileNode While = new WhileNode(condition, forBlock);
|
||||||
|
@ -5,8 +5,11 @@ import ast.ASTNode;
|
|||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
import ast.members.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
|
import ast.members.FieldNode;
|
||||||
import ast.members.MemberNode;
|
import ast.members.MemberNode;
|
||||||
import ast.type.AccessModifierNode;
|
import ast.type.AccessModifierNode;
|
||||||
|
import ast.type.type.BaseType;
|
||||||
|
import ast.type.type.TypeEnum;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
@ -25,140 +28,189 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
class AstBuilderTest {
|
class AstBuilderTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@DisplayName("Empty Class Test")
|
||||||
public void emptyClassTest(){
|
public void emptyClassTest(){
|
||||||
MemberNode constructor = new ConstructorNode(new AccessModifierNode("public"),"EmptyClass");
|
ClassNode emptyClass = Helper.generateEmptyClass("TestClass");
|
||||||
ClassNode emptyClass = new ClassNode("public", "EmptyClass");
|
ProgramNode expected = new ProgramNode();
|
||||||
emptyClass.addMember(constructor);
|
|
||||||
var expected = new ProgramNode();
|
|
||||||
expected.addClass(emptyClass);
|
expected.addClass(emptyClass);
|
||||||
|
|
||||||
CharStream testFile = null;
|
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClass.java");
|
||||||
try {
|
|
||||||
testFile = CharStreams.fromFileName("src/test/resources/input/javaCases/EmptyClass.java");
|
|
||||||
} catch (IOException e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(testFile);
|
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
|
||||||
tokenStream.fill();
|
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
|
||||||
ParseTree parseTree = parser.program();
|
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
|
||||||
var actual = astBuilder.visit(parseTree);
|
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Multiple Empty Classes Test")
|
||||||
|
public void multipleEmptyClassesTest() {
|
||||||
|
ClassNode classNode1 = Helper.generateEmptyClass("TestClass1");
|
||||||
|
ClassNode classNode2 = Helper.generateEmptyClass("TestClass2");
|
||||||
|
ProgramNode expected = new ProgramNode();
|
||||||
|
expected.addClass(classNode1);
|
||||||
|
expected.addClass(classNode2);
|
||||||
|
|
||||||
|
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java");
|
||||||
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Empty Class Test with Constructor")
|
||||||
|
public void emptyClassWithConstructorTest() {
|
||||||
|
ClassNode classNode = Helper.generateEmptyClass("TestClass");
|
||||||
|
ProgramNode expected = new ProgramNode();
|
||||||
|
expected.addClass(classNode);
|
||||||
|
|
||||||
|
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java");
|
||||||
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Field Test")
|
||||||
|
public void fieldTest() {
|
||||||
|
ClassNode classNode = Helper.generateEmptyClass("TestClass");
|
||||||
|
classNode.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a"));
|
||||||
|
|
||||||
|
ProgramNode expected = new ProgramNode();
|
||||||
|
expected.addClass(classNode);
|
||||||
|
|
||||||
|
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java");
|
||||||
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Field Test with Accessmodifier")
|
||||||
|
public void fieldTestWithModifier() {
|
||||||
|
ClassNode classNode = Helper.generateEmptyClass("TestClass");
|
||||||
|
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
|
|
||||||
|
ProgramNode expected = new ProgramNode();
|
||||||
|
expected.addClass(classNode);
|
||||||
|
|
||||||
|
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java");
|
||||||
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Commments Ignore Test")
|
@DisplayName("Commments Ignore Test")
|
||||||
public void commmentsIgnoreTest(){
|
public void commmentsIgnoreTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Constructor Paramerter Test")
|
@DisplayName("Constructor Paramerter Test")
|
||||||
public void constructorParameterTest(){
|
public void constructorParameterTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("This Dot Test")
|
@DisplayName("This Dot Test")
|
||||||
public void thisDotTest(){
|
public void thisDotTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Constructor This Dot Test")
|
@DisplayName("Constructor This Dot Test")
|
||||||
public void constructorThisDotTest(){
|
public void constructorThisDotTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Void Methoden Test")
|
@DisplayName("Void Methoden Test")
|
||||||
public void voidMethodenTest(){
|
public void voidMethodenTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Constructor Method call Test")
|
@DisplayName("Constructor Method call Test")
|
||||||
public void constructorMethodCallTest(){
|
public void constructorMethodCallTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Constructor Method call Parameters Test")
|
@DisplayName("Constructor Method call Parameters Test")
|
||||||
public void constructorMethodCallParametersTest(){
|
public void constructorMethodCallParametersTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Char Test")
|
@DisplayName("Char Test")
|
||||||
public void charTest(){
|
public void charTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Null Test")
|
@DisplayName("Null Test")
|
||||||
public void nullTest(){
|
public void nullTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Self Reference Test")
|
@DisplayName("Self Reference Test")
|
||||||
public void selfReferneceTest(){
|
public void selfReferneceTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Variable Compare Test")
|
@DisplayName("Variable Compare Test")
|
||||||
public void variableCompareTest(){
|
public void variableCompareTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Variable Calculation Test")
|
@DisplayName("Variable Calculation Test")
|
||||||
public void variableCalculationTest(){
|
public void variableCalculationTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Main Method Test")
|
@DisplayName("Main Method Test")
|
||||||
public void mainMethodTest(){
|
public void mainMethodTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("While Test")
|
@DisplayName("While Test")
|
||||||
public void whileTest(){
|
public void whileTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Do While Test")
|
@DisplayName("Do While Test")
|
||||||
public void doWhileTest(){
|
public void doWhileTest(){
|
||||||
|
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("For Test")
|
@DisplayName("For Test")
|
||||||
public void forTest(){
|
public void forTest(){
|
||||||
|
|
||||||
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
//Noch nicht speziell Increment nur zum Development Testen per Debug
|
||||||
|
@Test
|
||||||
|
@DisplayName("Increment Test")
|
||||||
|
public void incrementTest(){
|
||||||
|
ClassNode classNode = Helper.generateEmptyClass("TestClass");
|
||||||
|
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
|
|
||||||
|
ProgramNode expected = new ProgramNode();
|
||||||
|
expected.addClass(classNode);
|
||||||
|
|
||||||
|
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Increment.java");
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
47
src/test/java/parser/Helper.java
Normal file
47
src/test/java/parser/Helper.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package parser;
|
||||||
|
|
||||||
|
import ast.ASTNode;
|
||||||
|
import ast.ClassNode;
|
||||||
|
import ast.ProgramNode;
|
||||||
|
import ast.members.ConstructorNode;
|
||||||
|
import ast.members.MemberNode;
|
||||||
|
import ast.statements.BlockNode;
|
||||||
|
import ast.statements.ReturnNode;
|
||||||
|
import ast.type.AccessModifierNode;
|
||||||
|
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.ASTBuilder;
|
||||||
|
import parser.generated.SimpleJavaLexer;
|
||||||
|
import parser.generated.SimpleJavaParser;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public class Helper {
|
||||||
|
public static ASTNode generateAST(String filePath) {
|
||||||
|
CharStream testFile = null;
|
||||||
|
try {
|
||||||
|
testFile = CharStreams.fromFileName(filePath);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
SimpleJavaLexer lexer = new SimpleJavaLexer(testFile);
|
||||||
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
|
tokenStream.fill();
|
||||||
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
|
ParseTree parseTree = parser.program();
|
||||||
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
|
|
||||||
|
return astBuilder.visit(parseTree);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ClassNode generateEmptyClass(String className) {
|
||||||
|
BlockNode blockNode = new BlockNode();
|
||||||
|
blockNode.addStatement(new ReturnNode(null));
|
||||||
|
MemberNode constructor = new ConstructorNode("public",className, blockNode);
|
||||||
|
ClassNode classNode = new ClassNode("public", className);
|
||||||
|
classNode.addMember(constructor);
|
||||||
|
return classNode;
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1 @@
|
|||||||
class EmptyClass { }
|
class TestClass {}
|
@ -0,0 +1,5 @@
|
|||||||
|
public class TestClass {
|
||||||
|
public TestClass() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
3
src/test/resources/input/javaCases/Field.java
Normal file
3
src/test/resources/input/javaCases/Field.java
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
public class TestClass {
|
||||||
|
int a;
|
||||||
|
}
|
@ -0,0 +1,3 @@
|
|||||||
|
public class TestClass {
|
||||||
|
public int a;
|
||||||
|
}
|
12
src/test/resources/input/javaCases/Increment.java
Normal file
12
src/test/resources/input/javaCases/Increment.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
public class Increment {
|
||||||
|
|
||||||
|
public int test;
|
||||||
|
|
||||||
|
public void increment(int p) {
|
||||||
|
test = p++;
|
||||||
|
|
||||||
|
for(int i = 1; i<=10, i++) {
|
||||||
|
int a = 5;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,3 @@
|
|||||||
class EmptyClass1{
|
class TestClass1 {}
|
||||||
|
|
||||||
}
|
class TestClass2{}
|
||||||
|
|
||||||
class EmptyClass2{
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user