implemented test with complex class for parser and scanner

This commit is contained in:
JonathanFleischmann 2024-05-15 17:47:57 +02:00
parent ab6c71e9d1
commit e51369715c
7 changed files with 968 additions and 39 deletions

View File

@ -0,0 +1,902 @@
//public class ComplexClass {
//
// int x;
// int y;
// ComplexClass b;
// ComplexClass c;
//
// public ComplexClass() {
// this.y = 10;
// this.x = 2;
// int i;
// for (i = 0; i < (this.y + 1); i = i + 1) {
// int j;
// for (j = 0; j < this.y; j += 1) {
// this.x = this.x * this.x;
// if (this.x == 100) {
// break;
// }
// }
// }
// this.y = 2;
// do {
// this.y = this.y + 1;
// } while (this.y < 10);
//
// int k;
// k = 0;
// for (k = 0; k < 10; k = k + 1) {
// if (k == 5) {
// return this;
// }
// }
//
// }
//
// public ComplexClass(int x) {
// this.b = new ComplexClass();
// this.c = new ComplexClass();
// this.x = x;
// this.b.x = 7;
// this.b.y = 13;
// this.c.x = this.b.getX() * this.b.y * this.b.getX('g');
// this.c.y = 25;
// }
//
// public ComplexClass(int x, int y) {
// this.x = x;
// this.y = y;
// }
//
// public ComplexClass initComplexClass(int x) {
// int a;
// a = 10;
// this.b = new ComplexClass(x);
// this.b.x = 10 + a;
// this.b.y = 20;
// this.b.c.x = 20 + a;
// if (methodCall()) {
// this.b.getC().b.y = this.b.x;
// }
// return this.b;
// }
//
// public ComplexClass init(int x, int y) {
// return new ComplexClass(x, y);
// }
//
// public ComplexClass(int x, int y, char z) {
// this.x = x;
// this.y = y;
// }
//
// public int getX(char z) {
// return this.x;
// }
//
// public ComplexClass getC() {
// return this.c;
// }
//
// public int getX() {
// return this.x;
// }
//
// public boolean methodCall() {
// return false;
// }
//
//}
import de.maishai.ast.Operator;
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
import de.maishai.typedast.Type;
import java.util.List;
@SuppressWarnings("DuplicateExpressions")
public class AbstractSyntax_ComplexClass {
public static Program get() {
List<Declaration> declarationList = List.of(
new Declaration(
"x",
Type.INT
),
new Declaration(
"y",
Type.INT
),
new Declaration(
"b",
Type.REFERENCE("ComplexClass")
),
new Declaration(
"c",
Type.REFERENCE("ComplexClass")
)
);
List<Method> methodList = getMethods();
List<Constructor> constructorList = getConstructors();
return new Program(
List.of(
new Class(
"ComplexClass",
declarationList,
methodList,
constructorList
)
)
);
}
private static List<Constructor> getConstructors() {
return List.of(
getConstructor1(),
getConstructor2(),
getConstructor3(),
getConstructor4()
);
}
private static Constructor getConstructor1() {
Block block = new Block(
List.of(
new Declaration(
"i",
Type.INT
),
new Declaration(
"k",
Type.INT
)
),
List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"y"),
new IntLiteral(10)
),
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new IntLiteral(2)
),
new For(
new Assignment(
new FieldVarAccess(
false,
null,
"i"),
new IntLiteral(0)
),
new Binary(
new FieldVarAccess(
false,
null,
"i"),
Operator.LT,
new Binary(
new FieldVarAccess(
true,
null,
"y"),
Operator.ADD,
new IntLiteral(1)
)
),
new Assignment(
new FieldVarAccess(
false,
null,
"i"),
new Binary(
new FieldVarAccess(
false,
null,
"i"),
Operator.ADD,
new IntLiteral(1)
)
),
new Block(
List.of(
new Declaration(
"j",
Type.INT
)
),
List.of(
new For(
new Assignment(
new FieldVarAccess(
false,
null,
"j"),
new IntLiteral(0)
),
new Binary(
new FieldVarAccess(
false,
null,
"j"),
Operator.LT,
new FieldVarAccess(
true,
null,
"y"
)
),
new Assignment(
new FieldVarAccess(
false,
null,
"j"),
new Binary(
new FieldVarAccess(
false,
null,
"j"),
Operator.ADD,
new IntLiteral(1)
)
),
new Block(
List.of(),
List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new Binary(
new FieldVarAccess(
true,
null,
"x"),
Operator.MUL,
new FieldVarAccess(
true,
null,
"x"
)
)
),
new IfElse(
new Binary(
new FieldVarAccess(
true,
null,
"x"),
Operator.EQ,
new IntLiteral(100)
),
new Block(
List.of(),
List.of(
new Break()
)
),
null
)
)
)
)
)
)
),
new Assignment(
new FieldVarAccess(
true,
null,
"y"),
new IntLiteral(2)
),
new DoWhile(
new Block(
List.of(),
List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"y"),
new Binary(
new FieldVarAccess(
true,
null,
"y"),
Operator.ADD,
new IntLiteral(1)
)
)
)
),
new Binary(
new FieldVarAccess(
true,
null,
"y"),
Operator.LT,
new IntLiteral(10)
)
),
new Assignment(
new FieldVarAccess(
false,
null,
"k"),
new IntLiteral(0)
),
new For(
new Assignment(
new FieldVarAccess(
false,
null,
"k"),
new IntLiteral(0)
),
new Binary(
new FieldVarAccess(
false,
null,
"k"),
Operator.LT,
new IntLiteral(10)
),
new Assignment(
new FieldVarAccess(
false,
null,
"k"),
new Binary(
new FieldVarAccess(
false,
null,
"k"),
Operator.ADD,
new IntLiteral(1)
)
),
new Block(
List.of(),
List.of(
new IfElse(
new Binary(
new FieldVarAccess(
false,
null,
"k"),
Operator.EQ,
new IntLiteral(5)
),
new Block(
List.of(),
List.of(
new Return(
new This()
)
)
),
null
)
)
)
)
)
);
return new Constructor(
"ComplexClass",
List.of(),
block
);
}
private static Constructor getConstructor2() {
Block block = new Block(
List.of(),
List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"b"),
new New(
Type.REFERENCE("ComplexClass"),
List.of()
)
),
new Assignment(
new FieldVarAccess(
true,
null,
"c"),
new New(
Type.REFERENCE("ComplexClass"),
List.of()
)
),
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new FieldVarAccess(
false,
null,
"x"
)
),
new Assignment(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"b"),
"x"),
new IntLiteral(7)
),
new Assignment(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"b"),
"y"),
new IntLiteral(13)
),
new Assignment(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"c"),
"x"),
new Binary(
new Binary(
new MethodCall(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"b"),
"getX"
),
List.of()
),
Operator.MUL,
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"b"),
"y"
)
),
Operator.MUL,
new MethodCall(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"b"),
"getX"
),
List.of(
new CharLiteral('g')
)
)
)
),
new Assignment(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"c"),
"y"),
new IntLiteral(25)
)
)
);
return new Constructor(
"ComplexClass",
List.of(
new Parameter(
"x",
Type.INT
)
),
block
);
}
private static Constructor getConstructor3() {
Block block = new Block(
List.of(),
List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new FieldVarAccess(
false,
null,
"x"
)
),
new Assignment(
new FieldVarAccess(
true,
null,
"y"),
new FieldVarAccess(
false,
null,
"y"
)
)
)
);
return new Constructor(
"ComplexClass",
List.of(
new Parameter(
"x",
Type.INT
),
new Parameter(
"y",
Type.INT
)
),
block
);
}
private static Constructor getConstructor4() {
Block block = new Block(
List.of(),
List.of(
new Assignment(
new FieldVarAccess(
true,
null,
"x"),
new FieldVarAccess(
false,
null,
"x"
)
),
new Assignment(
new FieldVarAccess(
true,
null,
"y"),
new FieldVarAccess(
false,
null,
"y"
)
)
)
);
return new Constructor(
"ComplexClass",
List.of(
new Parameter(
"x",
Type.INT
),
new Parameter(
"y",
Type.INT
),
new Parameter(
"z",
Type.CHAR
)
),
block
);
}
private static List<Method> getMethods() {
return List.of(
getMethod1(),
getMethod2(),
getMethod3(),
getMethod4(),
getMethod5(),
getMethod6()
);
}
private static Method getMethod1() {
Block block = new Block(
List.of(
new Declaration(
"a",
Type.INT
)
),
List.of(
new Assignment(
new FieldVarAccess(
false,
null,
"a"),
new IntLiteral(10)
),
new Assignment(
new FieldVarAccess(
true,
null,
"b"),
new New(
Type.REFERENCE("ComplexClass"),
List.of(
new FieldVarAccess(
false,
null,
"x"
)
)
)
),
new Assignment(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"b"),
"x"),
new Binary(
new IntLiteral(10),
Operator.ADD,
new FieldVarAccess(
false,
null,
"a"
)
)
),
new Assignment(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"b"),
"y"),
new IntLiteral(20)
),
new Assignment(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
new FieldVarAccess(
false,
null,
"b"),
"c"),
"x"),
new Binary(
new IntLiteral(20),
Operator.ADD,
new FieldVarAccess(
false,
null,
"a"
)
)
),
new IfElse(
new MethodCall(
new FieldVarAccess(
false,
null,
"methodCall"),
List.of()
),
new Block(
List.of(),
List.of(
new Assignment(
new FieldVarAccess(
true,
new FieldVarAccess(
false,
new MethodCall(
new FieldVarAccess(
false,
new FieldVarAccess(
false,
null,
"b"
),
"getC"
),
List.of()
),
"b"),
"y"),
new FieldVarAccess(
true,
new FieldVarAccess(
false,
null,
"b"
),
"x"
)
)
)
),
null
),
new Return(
new FieldVarAccess(
true,
null,
"b"
)
)
)
);
return new Method(
Type.REFERENCE("ComplexClass"),
"initComplexClass",
List.of(
new Parameter(
"x",
Type.INT
)
),
block
);
}
private static Method getMethod2() {
Block block = new Block(
List.of(),
List.of(
new Return(
new New(
Type.REFERENCE("ComplexClass"),
List.of(
new FieldVarAccess(
false,
null,
"x"
),
new FieldVarAccess(
false,
null,
"y"
)
)
)
)
)
);
return new Method(
Type.REFERENCE("ComplexClass"),
"init",
List.of(
new Parameter(
"x",
Type.INT
),
new Parameter(
"y",
Type.INT
)
),
block
);
}
private static Method getMethod3() {
Block block = new Block(
List.of(),
List.of(
new Return(
new FieldVarAccess(
true,
null,
"x"
)
)
)
);
return new Method(
Type.INT,
"getX",
List.of(
new Parameter(
"z",
Type.CHAR
)
),
block
);
}
private static Method getMethod4() {
Block block = new Block(
List.of(),
List.of(
new Return(
new FieldVarAccess(
true,
null,
"c"
)
)
)
);
return new Method(
Type.REFERENCE("ComplexClass"),
"getC",
List.of(),
block
);
}
private static Method getMethod5() {
Block block = new Block(
List.of(),
List.of(
new Return(
new FieldVarAccess(
true,
null,
"x"
)
)
)
);
return new Method(
Type.INT,
"getX",
List.of(),
block
);
}
private static Method getMethod6() {
Block block = new Block(
List.of(),
List.of(
new Return(
new BoolLiteral(false)
)
)
);
return new Method(
Type.BOOL,
"methodCall",
List.of(),
block
);
}
}

View File

@ -4,44 +4,21 @@ public class ComplexClass {
int y;
ComplexClass b;
ComplexClass c;
public ComplexClass(int x) {
this.x = x;
}
public ComplexClass(int x, int y) {
this.x = x;
this.y = y;
}
public ComplexClass initComplexClass(int x) {
int a;
a = 10;
b = new ComplexClass(x);
b.x = 10 + a;
b.y = 20;
b.c.x = 20 + a;
b.c.b.y = b.x;
return b;
}
public ComplexClass init(int x, int y) {
return new ComplexClass(x, y);
}
public ComplexClass(int x) {
this.x = x;
int i;
b = b.getX().c.getC();
i = b.getX().c.getX();
}
public ComplexClass() {
this.x = 10;
this.y = 10;
this.x = 2;
int i;
for (i = 0; i < (x + 1); i = i + 1) {
for (i = 0; i < (this.y + 1); i = i + 1) {
int j;
for (j = 0; j < this.x; j += 1) {
for (j = 0; j < this.y; j += 1) {
this.x = this.x * this.x;
break;
if (this.x == 100) {
break;
}
}
}
this.y = 2;
do {
this.y = this.y + 1;
} while (this.y < 10);
@ -50,12 +27,44 @@ public class ComplexClass {
k = 0;
for (k = 0; k < 10; k = k + 1) {
if (k == 5) {
return;
return this;
}
}
}
public ComplexClass(int x) {
this.b = new ComplexClass();
this.c = new ComplexClass();
this.x = x;
this.b.x = 7;
this.b.y = 13;
this.c.x = this.b.getX() * this.b.y * this.b.getX('g');
this.c.y = 25;
}
public ComplexClass(int x, int y) {
this.x = x;
this.y = y;
}
public ComplexClass initComplexClass(int x) {
int a;
a = 10;
this.b = new ComplexClass(x);
this.b.x = 10 + a;
this.b.y = 20;
this.b.c.x = 20 + a;
if (methodCall()) {
this.b.getC().b.y = this.b.x;
}
return this.b;
}
public ComplexClass init(int x, int y) {
return new ComplexClass(x, y);
}
public ComplexClass(int x, int y, char z) {
this.x = x;
this.y = y;
@ -66,16 +75,15 @@ public class ComplexClass {
}
public ComplexClass getC() {
return c;
return this.c;
}
public int getX() {
return x;
return this.x;
}
public boolean methodCall() {
return false;
}
}

View File

@ -37,7 +37,8 @@ public class TypedAbstractSyntax_ClassWithConstructor {
null,
Type.REFERENCE("ClassWithField")
)
)
),
null
);
}

View File

@ -38,7 +38,8 @@ public class TypedAbstractSyntax_ClassWithField {
null,
Type.REFERENCE("ClassWithField")
)
)
),
null
);
}
}

View File

@ -31,7 +31,8 @@ public class TypedAbstractSyntax_PublicClass {
null,
Type.REFERENCE("PublicClass")
)
)
),
null
);
}
}

View File

@ -0,0 +1,16 @@
import de.maishai.Compiler;
import de.maishai.ast.records.Program;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CodegeneratorTests {
// @Test
// public void testPublicClass() {
// byte[] resultBytecode = Compiler.generateByteCodeArrayFromTypedAst();
// assertEquals(AbstractSyntax_PublicClass.get(), resultBytecode);
// }
}

View File

@ -59,6 +59,6 @@ public class ScannerParserTests {
@Test
public void testComplexClass() {
Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ComplexClass.java"));
assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst);
assertEquals(AbstractSyntax_ComplexClass.get(), resultAst);
}
}