From 2eecf3faa8990a26e2f42d04122bab3f7fe5f682 Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Thu, 16 May 2024 00:09:05 +0200 Subject: [PATCH] implemented test with complex class for type check --- .../typedast/typedclass/TypedAssignment.java | 1 + .../typedast/typedclass/TypedBoolLiteral.java | 2 + .../typedast/typedclass/TypedCharLiteral.java | 2 + .../typedast/typedclass/TypedIfElse.java | 2 + .../typedast/typedclass/TypedMethod.java | 2 + .../typedast/typedclass/TypedMethodCall.java | 2 + .../maishai/typedast/typedclass/TypedNew.java | 2 + .../typedast/typedclass/TypedProgram.java | 14 + .../typedast/typedclass/TypedReturn.java | 2 + .../AbstractSyntax_ClassWithConstructor.java | 1 - ...edAbstractSyntax_ClassWithConstructor.java | 12 +- .../TypedAbstractSyntax_ComplexClass.java | 960 ++++++++++++++++++ src/test/java/ScannerParserTests.java | 2 +- src/test/java/TypingTests.java | 6 + 14 files changed, 1002 insertions(+), 8 deletions(-) create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ComplexClass.java diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index 7985d3f..91b92ea 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -25,6 +25,7 @@ public class TypedAssignment implements TypedStatement { public void convertToTypedAssignment(TypedProgram typedProgram, Assignment untyped) { value = convertExpression(typedProgram, untyped.value()); location = new TypedFieldVarAccess(typedProgram, untyped.location()); + location.typeCheck(typedProgram); } @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java index 22f7b3f..c92301c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBoolLiteral.java @@ -4,12 +4,14 @@ import de.maishai.ast.records.BoolLiteral; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; import de.maishai.typedast.Type; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.objectweb.asm.Opcodes; @Data @NoArgsConstructor +@AllArgsConstructor public class TypedBoolLiteral implements TypedExpression { private Boolean value; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java index 371abfe..74b71d7 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedCharLiteral.java @@ -4,9 +4,11 @@ import de.maishai.ast.records.CharLiteral; import de.maishai.typedast.MethodContext; import de.maishai.typedast.TypedExpression; import de.maishai.typedast.Type; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class TypedCharLiteral implements TypedExpression { private char value; private Type type; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java index 69a5240..8fe4e19 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedIfElse.java @@ -2,6 +2,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.*; import de.maishai.typedast.*; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.objectweb.asm.Label; @@ -11,6 +12,7 @@ import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression; @Data @NoArgsConstructor +@AllArgsConstructor public class TypedIfElse implements TypedStatement { private TypedExpression typedCon; private TypedBlock ifTypedBlock; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index f71d5c8..97e787f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -2,6 +2,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Method; import de.maishai.typedast.*; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.objectweb.asm.MethodVisitor; @@ -12,6 +13,7 @@ import java.util.List; @Data @NoArgsConstructor +@AllArgsConstructor public class TypedMethod implements TypedNode { private String name; private Type returnType; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java index 21fe9d7..83353a4 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java @@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Expression; import de.maishai.ast.records.MethodCall; import de.maishai.typedast.*; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -15,6 +16,7 @@ import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression; @Data @NoArgsConstructor +@AllArgsConstructor public class TypedMethodCall implements TypedExpression, TypedStatement { private TypedFieldVarAccess recipient; private List args = new ArrayList<>(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java index 9c8497e..c5cc3b5 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedNew.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedNew.java @@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Expression; import de.maishai.ast.records.New; import de.maishai.typedast.*; +import lombok.AllArgsConstructor; import lombok.Data; import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.Opcodes; @@ -13,6 +14,7 @@ import java.util.List; import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression; @Data +@AllArgsConstructor public class TypedNew implements TypedExpression, TypedStatement { private Type type; private List args = new ArrayList<>(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java index f8dbd2e..d30440b 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java @@ -59,4 +59,18 @@ public class TypedProgram { currentClass = null; } + @Override + public boolean equals(Object obj) { + if (this == obj) return true; + if (obj == null || getClass() != obj.getClass()) return false; + TypedProgram other = (TypedProgram) obj; + if(typedClasses.equals(other.typedClasses) && + (currentClass == null && other.currentClass == null + || currentClass.equals(other.currentClass))) { + return true; + } else { + System.out.println("TypedPrograms are not equal:\n" + this.typedClasses + "\n" + other.typedClasses + "\n"); + return false; + } + } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java index 06a854e..e11f592 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedReturn.java @@ -3,6 +3,7 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.*; import de.maishai.typedast.*; +import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.objectweb.asm.Opcodes; @@ -11,6 +12,7 @@ import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression; @Data @NoArgsConstructor +@AllArgsConstructor public class TypedReturn implements TypedStatement { private TypedExpression ret; private Type type; diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java index 49d6816..ec616c8 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java @@ -165,7 +165,6 @@ public class AbstractSyntax_ClassWithConstructor { localVariables, statementList ); - return new Constructor( "ClassWithConstructor", parameters, diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java index 8f2946f..49c4cd8 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java @@ -35,7 +35,7 @@ public class TypedAbstractSyntax_ClassWithConstructor { getConstructors(), null, null, - Type.REFERENCE("ClassWithField") + Type.REFERENCE("ClassWithConstructor") ) ), null @@ -204,20 +204,20 @@ public class TypedAbstractSyntax_ClassWithConstructor { Type.INT ) ), - Type.INT + Type.VOID ), - Type.INT + Type.VOID ) ), - Type.INT + Type.VOID ), - Type.INT + Type.VOID ) ); TypedBlock typedBlock = new TypedBlock( typedLocalVariables, typedStatementList, - Type.INT + Type.VOID ); return new TypedConstructor( diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ComplexClass.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ComplexClass.java new file mode 100644 index 0000000..4c4832f --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ComplexClass.java @@ -0,0 +1,960 @@ +//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.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + + +public class TypedAbstractSyntax_ComplexClass { + public static TypedProgram get() { + return new TypedProgram( + getClasses(), + null + ); + } + + private static List getClasses() { + return List.of( + getClass1() + ); + } + + private static TypedClass getClass1() { + return new TypedClass( + "ComplexClass", + getFields(), + getMethods(), + getConstructors(), + null, + null, + Type.REFERENCE("ComplexClass") + ); + } + + private static List getFields() { + return List.of( + new TypedDeclaration("x", Type.INT), + new TypedDeclaration("y", Type.INT), + new TypedDeclaration("b", Type.REFERENCE("ComplexClass")), + new TypedDeclaration("c", Type.REFERENCE("ComplexClass")) + ); + } + + private static List getConstructors() { + return List.of( + getConstructor1(), + getConstructor2(), + getConstructor3(), + getConstructor4() + ); + } + + private static TypedConstructor getConstructor1() { + TypedBlock block = new TypedBlock( + List.of( + new TypedLocalVariable("i", Type.INT), + new TypedLocalVariable("k", Type.INT) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral(10), + new TypedFieldVarAccess( + true, + null, + "y", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral(2), + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedFor( + new TypedAssignment( + new TypedIntLiteral(0), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT), + Type.INT + ), + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT), + Operator.LT, + new TypedBinary( + new TypedFieldVarAccess( + true, + null, + "y", + Type.INT + ), + Operator.ADD, + new TypedIntLiteral(1), + Type.INT + ), + Type.BOOL + ), + new TypedAssignment( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT), + Operator.ADD, + new TypedIntLiteral(1), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT), + Type.INT + ), + new TypedBlock( + List.of( + new TypedLocalVariable( + "j", + Type.INT + ) + ), + List.of( + new TypedFor( + new TypedAssignment( + new TypedIntLiteral(0), + new TypedFieldVarAccess( + false, + null, + "j", + Type.INT), + Type.INT + ), + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "j", + Type.INT), + Operator.LT, + new TypedFieldVarAccess( + true, + null, + "y", + Type.INT + ), + Type.BOOL + ), + new TypedAssignment( + new TypedBinary( + new TypedIntLiteral(1), + Operator.ADD, + new TypedFieldVarAccess( + false, + null, + "j", + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "j", + Type.INT + ), + Type.INT + ), + new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedBinary( + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Operator.MUL, + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedIfElse( + new TypedBinary( + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Operator.EQ, + new TypedIntLiteral(100), + Type.BOOL + ), + new TypedBlock( + List.of(), + List.of( + new TypedBreak() + ) + ), + null, + Type.VOID + ) + ) + ), + Type.VOID + ) + ) + ), + Type.VOID + ) + + ) + ); + return new TypedConstructor( + "ComplexClass", + List.of(), + block + ); + } + + private static TypedConstructor getConstructor2() { + TypedBlock block = new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedNew( + Type.REFERENCE("ComplexClass"), + List.of() + ), + new TypedFieldVarAccess( + true, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + Type.REFERENCE("ComplexClass") + ), + new TypedAssignment( + new TypedNew( + Type.REFERENCE("ComplexClass"), + List.of() + ), + new TypedFieldVarAccess( + true, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + Type.REFERENCE("ComplexClass") + ), + new TypedAssignment( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral(7), + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral(13), + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "y", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedBinary( + new TypedBinary( + new TypedMethodCall( + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "getX", + Type.INT + ), + List.of(), + Type.INT + ), + Operator.MUL, + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "y", + Type.INT + ), + Type.INT + ), + Operator.MUL, + new TypedMethodCall( + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "getX", + Type.INT + ), + List.of( + new TypedCharLiteral( + 'g', + Type.CHAR + ) + ), + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "c", + Type.REFERENCE("ComplexClass") + ), + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral(25), + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "c", + Type.REFERENCE("ComplexClass") + ), + "y", + Type.INT + ), + Type.INT + ) + ) + ); + return new TypedConstructor( + "ComplexClass", + List.of( + new TypedParameter( + "x", + Type.INT + ) + ), + block + ); + } + + private static TypedConstructor getConstructor3() { + TypedBlock block = new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + new TypedFieldVarAccess( + true, + null, + "y", + Type.INT + ), + Type.INT + ) + ) + ); + return new TypedConstructor( + "ComplexClass", + List.of( + new TypedParameter( + "x", + Type.INT + ), + new TypedParameter( + "y", + Type.INT + ) + ), + block + ); + } + + private static TypedConstructor getConstructor4() { + TypedBlock block = new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + new TypedFieldVarAccess( + true, + null, + "y", + Type.INT + ), + Type.INT + ) + ) + ); + return new TypedConstructor( + "ComplexClass", + List.of( + new TypedParameter( + "x", + Type.INT + ), + new TypedParameter( + "y", + Type.INT + ), + new TypedParameter( + "z", + Type.CHAR + ) + ), + block + ); + } + + private static List getMethods() { + return List.of( + getMethod1(), + getMethod2(), + getMethod3(), + getMethod4(), + getMethod5(), + getMethod6() + ); + } + + private static TypedMethod getMethod1() { + TypedBlock block = new TypedBlock( + List.of( + new TypedLocalVariable("a", Type.INT) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral(10), + new TypedFieldVarAccess( + false, + null, + "a", + Type.REFERENCE("ComplexClass") + ), + Type.INT + ), + new TypedAssignment( + new TypedNew( + Type.REFERENCE("ComplexClass"), + List.of( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ) + ) + ), + new TypedFieldVarAccess( + true, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + Type.REFERENCE("ComplexClass") + ), + new TypedAssignment( + new TypedBinary( + new TypedIntLiteral(10), + Operator.ADD, + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral(20), + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "y", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedBinary( + new TypedIntLiteral(20), + Operator.ADD, + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "c", + Type.REFERENCE("ComplexClass") + ), + "x", + Type.INT + ), + Type.INT + ), + new TypedIfElse( + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "methodCall", + Type.BOOL + ), + List.of(), + Type.BOOL + ), + new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "x", + Type.INT + ), + new TypedFieldVarAccess( + true, + new TypedFieldVarAccess( + false, + new TypedMethodCall( + new TypedFieldVarAccess( + false, + new TypedFieldVarAccess( + false, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + "getC", + Type.REFERENCE("ComplexClass") + ), + List.of(), + Type.REFERENCE("ComplexClass") + ), + "b", + Type.REFERENCE("ComplexClass") + ), + "y", + Type.INT + ), + Type.INT + ) + ) + ), + null, + Type.VOID + ), + new TypedReturn( + new TypedFieldVarAccess( + true, + null, + "b", + Type.REFERENCE("ComplexClass") + ), + Type.REFERENCE("ComplexClass") + ) + ) + ); + return new TypedMethod( + "initComplexClass", + Type.REFERENCE("ComplexClass"), + List.of( + new TypedParameter( + "x", + Type.INT + ) + ), + List.of(), + block + ); + } + + private static TypedMethod getMethod2() { + TypedBlock block = new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedNew( + Type.REFERENCE("ComplexClass"), + List.of( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ) + ) + ), + Type.REFERENCE("ComplexClass") + ) + ) + ); + return new TypedMethod( + "init", + Type.REFERENCE("ComplexClass"), + List.of( + new TypedParameter( + "x", + Type.INT + ), + new TypedParameter( + "y", + Type.INT + ) + ), + List.of(), + block + ); + } + + private static TypedMethod getMethod3() { + TypedBlock block = new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ) + ) + ); + return new TypedMethod( + "getX", + Type.INT, + List.of( + new TypedParameter( + "z", + Type.CHAR + ) + ), + List.of(), + block + ); + } + + private static TypedMethod getMethod4() { + TypedBlock block = new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedFieldVarAccess( + true, + null, + "c", + Type.REFERENCE("ComplexClass") + ), + Type.REFERENCE("ComplexClass") + ) + ) + ); + return new TypedMethod( + "getC", + Type.REFERENCE("ComplexClass"), + List.of(), + List.of(), + block + ); + } + + private static TypedMethod getMethod5() { + TypedBlock block = new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ) + ) + ); + return new TypedMethod( + "getX", + Type.INT, + List.of(), + List.of(), + block + ); + } + + private static TypedMethod getMethod6() { + TypedBlock block = new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBoolLiteral( + false, + Type.BOOL + ), + Type.BOOL + ) + ) + ); + return new TypedMethod( + "methodCall", + Type.BOOL, + List.of(), + List.of(), + block + ); + } +} diff --git a/src/test/java/ScannerParserTests.java b/src/test/java/ScannerParserTests.java index 20dfd7a..a490eca 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_ComplexClass.get(), resultAst); + assertEquals(TypedAbstractSyntax_ComplexClass.get(), resultAst); } } diff --git a/src/test/java/TypingTests.java b/src/test/java/TypingTests.java index d81e9dd..6c2cda3 100644 --- a/src/test/java/TypingTests.java +++ b/src/test/java/TypingTests.java @@ -23,4 +23,10 @@ public class TypingTests { TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithConstructor.get()); assertEquals(TypedAbstractSyntax_ClassWithConstructor.get(), resultTypedAst); } + + @Test + public void testComplexClass() { + TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ComplexClass.get()); + assertEquals(TypedAbstractSyntax_ComplexClass.get(), resultTypedAst); + } }