diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ComplexClass.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ComplexClass.java new file mode 100644 index 0000000..6fee8c7 --- /dev/null +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ComplexClass.java @@ -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 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 methodList = getMethods(); + List constructorList = getConstructors(); + return new Program( + List.of( + new Class( + "ComplexClass", + declarationList, + methodList, + constructorList + ) + ) + ); + } + + private static List 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 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 + ); + } +} diff --git a/src/main/resources/JavaTestfiles/ComplexClass.java b/src/main/resources/JavaTestfiles/ComplexClass.java index e61f30d..f374cee 100644 --- a/src/main/resources/JavaTestfiles/ComplexClass.java +++ b/src/main/resources/JavaTestfiles/ComplexClass.java @@ -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; } - } diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java index b7dbc45..8f2946f 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java @@ -37,7 +37,8 @@ public class TypedAbstractSyntax_ClassWithConstructor { null, Type.REFERENCE("ClassWithField") ) - ) + ), + null ); } diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java index 9c39652..4abf824 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java @@ -38,7 +38,8 @@ public class TypedAbstractSyntax_ClassWithField { null, Type.REFERENCE("ClassWithField") ) - ) + ), + null ); } } \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java index dba4b7e..79541a2 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java @@ -31,7 +31,8 @@ public class TypedAbstractSyntax_PublicClass { null, Type.REFERENCE("PublicClass") ) - ) + ), + null ); } } \ No newline at end of file diff --git a/src/test/java/CodegeneratorTests.java b/src/test/java/CodegeneratorTests.java new file mode 100644 index 0000000..cc580ad --- /dev/null +++ b/src/test/java/CodegeneratorTests.java @@ -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); +// } +} diff --git a/src/test/java/ScannerParserTests.java b/src/test/java/ScannerParserTests.java index 4e27183..20dfd7a 100644 --- a/src/test/java/ScannerParserTests.java +++ b/src/test/java/ScannerParserTests.java @@ -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); } }