From 78a34b915e3865c1a374971661952e5e6dbf1e2f Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Thu, 27 Jun 2024 19:10:43 +0200 Subject: [PATCH 1/7] added some e2e tests for CodeGenFiles with reflections --- output/Operators.class | Bin 0 -> 488 bytes pom.xml | 6 + .../typedast/typedclass/TypedClass.java | 19 + src/test/java/CodegeneratorTests.java | 45 +- src/test/java/ScannerParserTests.java | 3 - .../CodeGen/BytecodeTestUtil.java | 177 ++++++ .../CodeGen/Features/ByteCode_Break.java | 144 +++++ .../CodeGen/Features/ByteCode_Class.java | 66 ++ .../Features/ByteCode_ClassObjects.java | 150 +++++ .../CodeGen/Features/ByteCode_Comment.java | 71 +++ .../CodeGen/Features/ByteCode_CompAssign.java | 156 +++++ .../Features/ByteCode_ComplexCalls.java | 178 ++++++ .../Features/ByteCode_Constructor.java | 159 +++++ .../CodeGen/Features/ByteCode_Continue.java | 145 +++++ .../CodeGen/Features/ByteCode_DataTypes.java | 184 ++++++ .../CodeGen/Features/ByteCode_Field.java | 86 +++ .../CodeGen/Features/ByteCode_For.java | 176 ++++++ .../CodeGen/Features/ByteCode_If.java | 86 +++ .../CodeGen/Features/ByteCode_LogicExpr.java | 568 ++++++++++++++++++ .../CodeGen/Features/ByteCode_Main.java | 72 +++ .../CodeGen/Features/ByteCode_Method.java | 50 ++ .../CodeGen/Features/ByteCode_MethodCall.java | 214 +++++++ .../Features/ByteCode_MultipleClasses.java | 98 +++ .../CodeGen/Features/ByteCode_Operators.java | 446 ++++++++++++++ .../CodeGen/Features/ByteCode_Overloaded.java | 125 ++++ .../CodeGen/Features/ByteCode_Print.java | 106 ++++ .../CodeGen/Features/ByteCode_Return.java | 131 ++++ .../CodeGen/Features/ByteCode_Unary.java | 102 ++++ .../ByteCode_VariableDefWithDecl.java | 74 +++ .../CodeGen/Features/ByteCode_While.java | 190 ++++++ .../TypedASTFeatures/TypedAST_Main.java | 7 +- .../Break.java | 0 .../Class.java | 0 .../ClassObjects.java | 0 .../Comment.java | 0 .../CompAssign.java | 0 .../ComplexCalls.java | 0 .../Constructor.java | 0 .../Continue.java | 0 .../DataTypes.java | 0 .../Field.java | 0 .../For.java | 0 .../If.java | 0 .../LogicExpr.java | 0 .../Main.java | 0 .../Method.java | 0 .../MethodCall.java | 0 .../MultipleClasses.java | 0 .../Operators.java | 0 .../Overloaded.java | 0 .../Print.java | 0 .../Return.java | 0 .../Unary.java | 0 .../VariableDefWithDecl.java | 0 .../While.java | 0 src/test/testFiles/CodeGenFeatures/Break.java | 21 + src/test/testFiles/CodeGenFeatures/Class.java | 2 + .../CodeGenFeatures/ClassObjects.java | 19 + .../testFiles/CodeGenFeatures/Comment.java | 12 + .../testFiles/CodeGenFeatures/CompAssign.java | 26 + .../CodeGenFeatures/ComplexCalls.java | 22 + .../CodeGenFeatures/Constructor.java | 18 + .../testFiles/CodeGenFeatures/Continue.java | 12 + .../testFiles/CodeGenFeatures/DataTypes.java | 23 + src/test/testFiles/CodeGenFeatures/Field.java | 17 + src/test/testFiles/CodeGenFeatures/For.java | 21 + src/test/testFiles/CodeGenFeatures/If.java | 11 + .../testFiles/CodeGenFeatures/LogicExpr.java | 33 + src/test/testFiles/CodeGenFeatures/Main.java | 11 + .../testFiles/CodeGenFeatures/Method.java | 11 + .../testFiles/CodeGenFeatures/MethodCall.java | 26 + .../CodeGenFeatures/MultipleClasses.java | 15 + .../testFiles/CodeGenFeatures/Operators.java | 29 + .../testFiles/CodeGenFeatures/Overloaded.java | 13 + src/test/testFiles/CodeGenFeatures/Print.java | 9 + .../testFiles/CodeGenFeatures/Return.java | 21 + src/test/testFiles/CodeGenFeatures/Unary.java | 10 + .../CodeGenFeatures/VariableDefWithDecl.java | 13 + src/test/testFiles/CodeGenFeatures/While.java | 24 + 79 files changed, 4439 insertions(+), 14 deletions(-) create mode 100644 output/Operators.class create mode 100644 src/test/java/testResources/CodeGen/BytecodeTestUtil.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Break.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Class.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_ClassObjects.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Comment.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_CompAssign.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_ComplexCalls.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_DataTypes.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Field.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_For.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_If.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_LogicExpr.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Main.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Method.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_MethodCall.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_MultipleClasses.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Operators.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Overloaded.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Print.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Return.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_Unary.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_VariableDefWithDecl.java create mode 100644 src/test/java/testResources/CodeGen/Features/ByteCode_While.java rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Break.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Class.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/ClassObjects.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Comment.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/CompAssign.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/ComplexCalls.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Constructor.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Continue.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/DataTypes.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Field.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/For.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/If.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/LogicExpr.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Main.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Method.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/MethodCall.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/MultipleClasses.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Operators.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Overloaded.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Print.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Return.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/Unary.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/VariableDefWithDecl.java (100%) rename src/test/testFiles/{JavaTestfilesFeatures => ASTandTypedASTFeatures}/While.java (100%) create mode 100644 src/test/testFiles/CodeGenFeatures/Break.java create mode 100644 src/test/testFiles/CodeGenFeatures/Class.java create mode 100644 src/test/testFiles/CodeGenFeatures/ClassObjects.java create mode 100644 src/test/testFiles/CodeGenFeatures/Comment.java create mode 100644 src/test/testFiles/CodeGenFeatures/CompAssign.java create mode 100644 src/test/testFiles/CodeGenFeatures/ComplexCalls.java create mode 100644 src/test/testFiles/CodeGenFeatures/Constructor.java create mode 100644 src/test/testFiles/CodeGenFeatures/Continue.java create mode 100644 src/test/testFiles/CodeGenFeatures/DataTypes.java create mode 100644 src/test/testFiles/CodeGenFeatures/Field.java create mode 100644 src/test/testFiles/CodeGenFeatures/For.java create mode 100644 src/test/testFiles/CodeGenFeatures/If.java create mode 100644 src/test/testFiles/CodeGenFeatures/LogicExpr.java create mode 100644 src/test/testFiles/CodeGenFeatures/Main.java create mode 100644 src/test/testFiles/CodeGenFeatures/Method.java create mode 100644 src/test/testFiles/CodeGenFeatures/MethodCall.java create mode 100644 src/test/testFiles/CodeGenFeatures/MultipleClasses.java create mode 100644 src/test/testFiles/CodeGenFeatures/Operators.java create mode 100644 src/test/testFiles/CodeGenFeatures/Overloaded.java create mode 100644 src/test/testFiles/CodeGenFeatures/Print.java create mode 100644 src/test/testFiles/CodeGenFeatures/Return.java create mode 100644 src/test/testFiles/CodeGenFeatures/Unary.java create mode 100644 src/test/testFiles/CodeGenFeatures/VariableDefWithDecl.java create mode 100644 src/test/testFiles/CodeGenFeatures/While.java diff --git a/output/Operators.class b/output/Operators.class new file mode 100644 index 0000000000000000000000000000000000000000..bf364f70e4591058cbd13b95771efb0cba000adb GIT binary patch literal 488 zcmZXPy;6ff6ot>qZ*WzT_z#^;l7`08v9Y3X(phI=8AHU70g|UmW1}+`K7bG9c*z0{ z%{}MtH+S#3zklC90Pf*44Cb4p5LI5378rxO5<3xOV*MJ-qg4`D7BmL)Azi1{BZD@X zzHk^Y9q6#A7g919lQ5iy)Ro(a>iagMD$`x z;5azKkmFdnF$Y)fg5yNFD~?m;ZW#2ZTqa;Nq|+sUO`c*=^{5+?GE!sq1^c7s)Y6g$ zyELZJo9_9GeM4z+pURd}m_A*$lmhl?)=~=Hr<<0Zb#&CEGAWhU{q1%RELEASE test + + junit + junit + 4.13.1 + test + commons-cli diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index c4740b4..aec8ef4 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -223,4 +223,23 @@ public class TypedClass implements TypedNode { return cw.toByteArray(); } + + public ClassWriter codeGenClassWriter() { + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); + ClassContext ctx = new ClassContext(className, cw); + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null); + for (TypedDeclaration declaration : typedDeclarations) { + declaration.codeGen(cw); + } + + for (TypedConstructor constructor : typedConstructors) { + constructor.codeGen(ctx); + } + + for (TypedMethod m : typedMethods) { + m.codeGen(ctx); + } + + return cw; + } } diff --git a/src/test/java/CodegeneratorTests.java b/src/test/java/CodegeneratorTests.java index be9ea37..d1f9b50 100644 --- a/src/test/java/CodegeneratorTests.java +++ b/src/test/java/CodegeneratorTests.java @@ -1,5 +1,12 @@ -import e2e.BytecodeTestUtil; +import de.maishai.Compiler; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Suite; +import testResources.CodeGen.BytecodeTestUtil; +import testResources.CodeGen.Features.*; + +import java.util.List; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -12,14 +19,32 @@ public class CodegeneratorTests { // assertEquals(AbstractSyntax_PublicClass.get(), resultBytecode); // } - @Test - public void testMethodCall() { - assertDoesNotThrow(() -> { - BytecodeTestUtil testUtility = new BytecodeTestUtil("src/test/testFiles/JavaTestfilesFeatures/MethodCall.java", "MethodCall"); +// @Test +// public void testMethodCall() { +// assertDoesNotThrow(() -> { +// BytecodeTestUtil testUtility = new BytecodeTestUtil("src/test/testFiles/ASTandTypedASTFeatures/MethodCall.java", "MethodCall"); +// +// assertEquals(0, testUtility.invokeMethod("method", null)); +// assertEquals(1, testUtility.invokeMethod("method1", new Class[]{int.class}, 1)); +// assertEquals(3, testUtility.invokeMethod("method2", new Class[]{int.class, int.class}, 1, 2)); +// }); +// } - assertEquals(0, testUtility.invokeMethod("method", null)); - assertEquals(1, testUtility.invokeMethod("method1", new Class[]{int.class}, 1)); - assertEquals(3, testUtility.invokeMethod("method2", new Class[]{int.class, int.class}, 1, 2)); - }); - } +// @Test +// public void testBreak() { +// ByteCode_Break ByteCode_Break = new ByteCode_Break(); +// Assertions.assertTrue(ByteCode_Break.allTestsSuccessful()); +// } + +// @Test +// public void testClass() { +// ByteCode_Class ByteCode_Class = new ByteCode_Class(); +// Assertions.assertTrue(ByteCode_Class.allTestsSuccessful()); +// } + +// @Test +// public void testClassObjects() { +// ByteCode_ClassObjects ByteCode_ClassObjects = new ByteCode_ClassObjects(); +// Assertions.assertTrue(ByteCode_ClassObjects.allTestsSuccessful()); +// } } diff --git a/src/test/java/ScannerParserTests.java b/src/test/java/ScannerParserTests.java index e5524ed..a77b2ca 100644 --- a/src/test/java/ScannerParserTests.java +++ b/src/test/java/ScannerParserTests.java @@ -217,7 +217,4 @@ public class ScannerParserTests { Program resultAst = Compiler.generateASTFromFiles(List.of("src/test/testFiles/JavaTestfilesFeatures/While.java")); assertEquals(AST_While.get(), resultAst); } - - //TODO: Anschauen: Warum sind die FieldVarAccess von Methodenaufrufen manchmal fields und manchmal ned (z.B. bei ComplexCalls) - // Warum geht MultipleClasses nicht? } diff --git a/src/test/java/testResources/CodeGen/BytecodeTestUtil.java b/src/test/java/testResources/CodeGen/BytecodeTestUtil.java new file mode 100644 index 0000000..498d687 --- /dev/null +++ b/src/test/java/testResources/CodeGen/BytecodeTestUtil.java @@ -0,0 +1,177 @@ +package testResources.CodeGen; + +import de.maishai.Compiler; + +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; + +public class BytecodeTestUtil { + + private Class clazz; + private Object instance; + + public BytecodeTestUtil(List sourceFilePaths, String className) throws Exception { + byte[] resultBytecode = Compiler.generateByteCodeArrayFromFiles(sourceFilePaths).get(0); + + ClassLoader classLoader = new ClassLoader() { + @Override + protected Class findClass(String name) { + return defineClass(name, resultBytecode, 0, resultBytecode.length); + } + }; + clazz = classLoader.loadClass(className); + this.instance = clazz.getDeclaredConstructor().newInstance(); + } + + + + public int getConstructorCount() { + return clazz.getDeclaredConstructors().length; + } + + public List getConstructorNames() { + List constructorNames = new ArrayList<>(); + for (Constructor constructor : clazz.getDeclaredConstructors()) { + constructorNames.add(constructor.getName()); + } + return constructorNames; + } + + public int getConstructorParameterCount(int index) { + return clazz.getDeclaredConstructors()[index].getParameterCount(); + } + + public List getConstructorParameterTypes(int index) { + List parameterTypes = new ArrayList<>(); + for (Class parameterType : clazz.getDeclaredConstructors()[index].getParameterTypes()) { + parameterTypes.add(parameterType.getSimpleName()); + } + return parameterTypes; + } + + public Object invokeConstructor(Class[] parameterTypes, Object... args) throws Exception { + return clazz.getDeclaredConstructor(parameterTypes).newInstance(args); + } + + + + public int getMethodCount() { + return clazz.getDeclaredMethods().length; + } + + public List getMethodNames() { + List methodNames = new ArrayList<>(); + for (Method method : clazz.getDeclaredMethods()) { + methodNames.add(method.getName()); + } + return methodNames; + } + + public String getMethodReturnType(String methodName, Class... params) throws Exception { + return clazz.getMethod(methodName, params).getReturnType().getSimpleName(); + } + + public int getMethodParameterCount(String methodName, Class... params) throws Exception { + return clazz.getMethod(methodName, params).getParameterCount(); + } + + public List getMethodParameterTypes(String methodName, Class... params) throws Exception { + List parameterTypes = new ArrayList<>(); + for (Class parameterType : clazz.getMethod(methodName, params).getParameterTypes()) { + parameterTypes.add(parameterType.getSimpleName()); + } + return parameterTypes; + } + + public Object invokeMethod(String methodName, Class[] parameterTypes, Object... args) throws Exception { + try { + Method method = clazz.getMethod(methodName, parameterTypes); + return method.invoke(instance, args); + } catch (InvocationTargetException e) { + e.getTargetException().printStackTrace(); + throw new RuntimeException(e.getTargetException()); + } + } + + + + public int getFieldCount() { + return clazz.getDeclaredFields().length; + } + + public List getFieldNames() { + List fieldNames = new ArrayList<>(); + for (Field field : clazz.getDeclaredFields()) { + fieldNames.add(field.getName()); + } + return fieldNames; + } + + public List getFieldTypes() { + List fieldTypes = new ArrayList<>(); + for (Field field : clazz.getDeclaredFields()) { + fieldTypes.add(field.getType().getSimpleName()); + } + return fieldTypes; + } + + public Object getFieldValue(String fieldName) throws Exception { + return clazz.getField(fieldName).get(instance); + } + + public void setFieldValue(String fieldName, Object value) throws Exception { + clazz.getField(fieldName).set(instance, value); + } + + + + public Object getFieldValueAfterMethodInvocation(String fieldName, String methodName, Class[] parameterTypes, Object... args) throws Exception { + Object instance = clazz.getDeclaredConstructor().newInstance(); + + // Methode aufrufen + Method method = clazz.getMethod(methodName, parameterTypes); + method.invoke(instance, args); + + // Wert des Feldes nach dem Methodenaufruf abrufen + return clazz.getField(fieldName).get(instance); + } + + + public Object getFieldValueOfObject(Object obj, String fieldName) { + try { + Field field = obj.getClass().getField(fieldName); + return field.get(obj); + } catch (NoSuchFieldException e) { + System.out.println("Das Feld " + fieldName + " existiert nicht."); + return null; + } catch (IllegalAccessException e) { + System.out.println("Zugriff auf das Feld " + fieldName + " ist nicht erlaubt."); + return null; + } + } + + + + private Class[] toClassArray(Object... args) { + return java.util.Arrays.stream(args) + .map(Object::getClass) + .toArray(Class[]::new); + } + + + + public Object getValueOfFieldInObject(Object instance, String fieldName) { + try { + Field field = instance.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(instance); + } catch (NoSuchFieldException | IllegalAccessException e) { + e.printStackTrace(); + return null; + } + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java new file mode 100644 index 0000000..9b82327 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java @@ -0,0 +1,144 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; + +import java.util.List; + +public class ByteCode_Break { + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Break.java"), "Break"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("Break", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("Break", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + Assertions.fail(); + } + } + + + + @Test + public void testMethodCount() { + Assertions.assertEquals(1, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertEquals("breakMethod", util.getMethodNames().get(0)); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("void", util.getMethodReturnType("breakMethod", new Class[]{boolean.class})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(1, util.getMethodParameterCount("breakMethod", new Class[]{})); + Assertions.assertEquals("boolean", util.getMethodParameterTypes("breakMethod", new Class[]{}).get(0)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(2, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertEquals("whileRepetition", util.getFieldNames().get(0)); + Assertions.assertEquals("forRepetition", util.getFieldNames().get(1)); + } + + @Test + public void testFieldTypes() { + Assertions.assertEquals("int", util.getFieldTypes().get(0)); + Assertions.assertEquals("int", util.getFieldTypes().get(1)); + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(0, util.getFieldValue("whileRepetition")); + Assertions.assertEquals(0, util.getFieldValue("forRepetition")); + } catch (Exception e) { + Assertions.fail(); + } + } + + + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "Break"); + Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "whileRepetition")); + Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "forRepetition")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeBreakMethodWithFalse() { + try { + int fieldWhileRepetition = (int) util.getFieldValue("whileRepetition"); + int fieldForRepetition = (int) util.getFieldValue("forRepetition"); + util.invokeMethod("breakMethod", new Class[]{boolean.class}, new Object[]{false}); + Assertions.assertNotEquals(fieldWhileRepetition, util.getFieldValue("whileRepetition")); + Assertions.assertEquals(10, util.getFieldValue("whileRepetition")); + Assertions.assertNotEquals(fieldForRepetition, util.getFieldValue("forRepetition")); + Assertions.assertEquals(10, util.getFieldValue("forRepetition")); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testInvokeBreakMethodWithTrue() { + try { + int fieldWhileRepetition = (int) util.getFieldValue("whileRepetition"); + int fieldForRepetition = (int) util.getFieldValue("forRepetition"); + util.invokeMethod("breakMethod", new Class[]{boolean.class}, new Object[]{true}); + Assertions.assertNotEquals(fieldWhileRepetition, util.getFieldValue("whileRepetition")); + Assertions.assertEquals(5, util.getFieldValue("whileRepetition")); + Assertions.assertNotEquals(fieldForRepetition, util.getFieldValue("forRepetition")); + Assertions.assertEquals(5, util.getFieldValue("forRepetition")); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Class.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Class.java new file mode 100644 index 0000000..47fc361 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Class.java @@ -0,0 +1,66 @@ +package testResources.CodeGen.Features; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; + +import java.util.List; + +public class ByteCode_Class { + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Class.java"), "Class"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("Class", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("Class", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + @Test + public void testMethodCount() { + Assertions.assertEquals(0, util.getMethodCount()); + } + + + + @Test + public void testFieldCount() { + Assertions.assertEquals(0, util.getFieldCount()); + } + + + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "Class"); + } catch (Exception e) { + Assertions.fail(); + } + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_ClassObjects.java b/src/test/java/testResources/CodeGen/Features/ByteCode_ClassObjects.java new file mode 100644 index 0000000..0085d39 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_ClassObjects.java @@ -0,0 +1,150 @@ +package testResources.CodeGen.Features; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; + +import java.util.List; + +public class ByteCode_ClassObjects { + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/ClassObjects.java"), "ClassObjects"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + @Test + public void testConstructorCount() { + Assertions.assertEquals(2, util.getConstructorCount()); + } + + @Test + public void testConstructor1() { + Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + } + + @Test + public void testConstructor2() { + Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(1)); + Assertions.assertEquals(1, util.getConstructorParameterCount(1)); + } + + + + @Test + public void testMethodCount() { + Assertions.assertEquals(1, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertEquals("objectsMethod", util.getMethodNames().get(0)); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("void", util.getMethodReturnType("objectsMethod", new Class[]{})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(0, util.getMethodParameterCount("objectsMethod", new Class[]{})); + } catch (Exception e) { + Assertions.fail(); + } + } + + + + @Test + public void testFieldCount() { + Assertions.assertEquals(3, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertEquals("object", util.getFieldNames().get(0)); + Assertions.assertEquals("objectWithValue", util.getFieldNames().get(1)); + Assertions.assertEquals("integerValue", util.getFieldNames().get(2)); + } + + @Test + public void testFieldTypes() { + Assertions.assertEquals("ClassObjects", util.getFieldTypes().get(0)); + Assertions.assertEquals("ClassObjects", util.getFieldTypes().get(1)); + Assertions.assertEquals("int", util.getFieldTypes().get(2)); + } + + @Test + public void testFieldValues() { + try { + Assertions.assertNull(util.getFieldValue("object")); + Assertions.assertNull(util.getFieldValue("objectWithValue")); + Assertions.assertEquals(0, util.getFieldValue("integerValue")); + } catch (Exception e) { + Assertions.fail(); + } + } + + + + @Test + public void testInvokeConstructor1() { + try { + Object constructor1ReturnValue = util.invokeConstructor(new Class[]{}, new Object[]{}); + Assertions.assertEquals(constructor1ReturnValue.getClass().getName(), "ClassObjects"); + Assertions.assertNull(util.getFieldValueOfObject(constructor1ReturnValue,"object")); + Assertions.assertNull(util.getFieldValueOfObject(constructor1ReturnValue, "objectWithValue")); + Assertions.assertEquals(0, + (int) util.getFieldValueOfObject(constructor1ReturnValue,"integerValue")); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testInvokeConstructor2() { + try { + Object constructor2ReturnValue = util.invokeConstructor(new Class[]{int.class}, new Object[]{2}); + Assertions.assertEquals(constructor2ReturnValue.getClass().getName(), "ClassObjects"); + Assertions.assertNull(util.getFieldValueOfObject(constructor2ReturnValue, "object")); + Assertions.assertNull(util.getFieldValueOfObject(constructor2ReturnValue, "objectWithValue")); + Assertions.assertEquals(2, + (int) util.getFieldValueOfObject(constructor2ReturnValue,"integerValue")); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testInvokeMethod() { + try { + Object fieldObject = util.getFieldValue("object"); + Object fieldObjectWithValue = util.getFieldValue("objectWithValue"); + int fieldIntegerValue = (int) util.getFieldValue("integerValue"); + util.invokeMethod("objectsMethod", new Class[]{}, new Object[]{}); + Assertions.assertNotSame(util.getFieldValue("object"), fieldObject); + Assertions.assertEquals(util.getFieldValue("object").getClass().getName(), "ClassObjects"); + Assertions.assertNotSame(util.getFieldValue("objectWithValue"), fieldObjectWithValue); + Assertions.assertEquals(util.getFieldValue("objectWithValue").getClass().getName(), "ClassObjects"); + Assertions.assertEquals((int) util.getFieldValue("integerValue"), fieldIntegerValue); + } catch (Exception e) { + throw new RuntimeException(e); + } + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Comment.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Comment.java new file mode 100644 index 0000000..d257d7e --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Comment.java @@ -0,0 +1,71 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.TypedBlock; +import de.maishai.typedast.typedclass.TypedClass; +import de.maishai.typedast.typedclass.TypedConstructor; +import de.maishai.typedast.typedclass.TypedProgram; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; + +import java.util.List; + +public class ByteCode_Comment { + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Comment.java"), "Comment"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("Comment", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("Comment", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + @Test + public void testMethodCount() { + Assertions.assertEquals(0, util.getMethodCount()); + } + + + + @Test + public void testFieldCount() { + Assertions.assertEquals(0, util.getFieldCount()); + } + + + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "Comment"); + } catch (Exception e) { + Assertions.fail(); + } + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_CompAssign.java b/src/test/java/testResources/CodeGen/Features/ByteCode_CompAssign.java new file mode 100644 index 0000000..a03eccc --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_CompAssign.java @@ -0,0 +1,156 @@ +package testResources.CodeGen.Features; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; + +import java.util.List; + +public class ByteCode_CompAssign { + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/CompAssign.java"), "CompAssign"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("CompAssign", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("CompAssign", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + @Test + public void testMethodCount() { + Assertions.assertEquals(5, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertTrue(util.getMethodNames().contains("increase")); + Assertions.assertTrue(util.getMethodNames().contains("decrease")); + Assertions.assertTrue(util.getMethodNames().contains("multiply")); + Assertions.assertTrue(util.getMethodNames().contains("divide")); + Assertions.assertTrue(util.getMethodNames().contains("modulus")); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("int", util.getMethodReturnType("increase", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodReturnType("decrease", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodReturnType("multiply", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodReturnType("divide", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodReturnType("modulus", new Class[]{int.class})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(1, util.getMethodParameterCount("increase", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("increase", new Class[]{int.class}).get(0)); + Assertions.assertEquals(1, util.getMethodParameterCount("decrease", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("decrease", new Class[]{int.class}).get(0)); + Assertions.assertEquals(1, util.getMethodParameterCount("multiply", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("multiply", new Class[]{int.class}).get(0)); + Assertions.assertEquals(1, util.getMethodParameterCount("divide", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("divide", new Class[]{int.class}).get(0)); + Assertions.assertEquals(1, util.getMethodParameterCount("modulus", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("modulus", new Class[]{int.class}).get(0)); + } catch (Exception e) { + Assertions.fail(); + } + } + + + @Test + public void testFieldCount() { + Assertions.assertEquals(0, util.getFieldCount()); + } + + + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "CompAssign"); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeIncrease() { + try { + Assertions.assertEquals(1, util.invokeMethod("increase", new Class[]{int.class}, new Object[]{0})); + Assertions.assertEquals(6, util.invokeMethod("increase", new Class[]{int.class}, new Object[]{5})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeDecrease() { + try { + Assertions.assertEquals(0, util.invokeMethod("decrease", new Class[]{int.class}, new Object[]{1})); + Assertions.assertEquals(4, util.invokeMethod("decrease", new Class[]{int.class}, new Object[]{5})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMultiply() { + try { + Assertions.assertEquals(0, util.invokeMethod("multiply", new Class[]{int.class}, new Object[]{0})); + Assertions.assertEquals(10, util.invokeMethod("multiply", new Class[]{int.class}, new Object[]{5})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeDivide() { + try { + Assertions.assertEquals(0, util.invokeMethod("divide", new Class[]{int.class}, new Object[]{0})); + Assertions.assertEquals(2, util.invokeMethod("divide", new Class[]{int.class}, new Object[]{5})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeModulus() { + try { + Assertions.assertEquals(0, util.invokeMethod("modulus", new Class[]{int.class}, new Object[]{0})); + Assertions.assertEquals(1, util.invokeMethod("modulus", new Class[]{int.class}, new Object[]{5})); + } catch (Exception e) { + Assertions.fail(); + } + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_ComplexCalls.java b/src/test/java/testResources/CodeGen/Features/ByteCode_ComplexCalls.java new file mode 100644 index 0000000..b1c9f51 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_ComplexCalls.java @@ -0,0 +1,178 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; + +import java.util.List; + +public class ByteCode_ComplexCalls { + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/ComplexCalls.java"), "ComplexCalls"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("ComplexCalls", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("ComplexCalls", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + + + @Test + public void testMethodCount() { + Assertions.assertEquals(3, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertTrue(util.getMethodNames().contains("makeComplexCalls")); + Assertions.assertTrue(util.getMethodNames().contains("getClassObject")); + Assertions.assertTrue(util.getMethodNames().contains("getA")); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("int", util.getMethodReturnType("makeComplexCalls", new Class[]{})); + Assertions.assertEquals("ComplexCalls", util.getMethodReturnType("getClassObject", new Class[]{})); + Assertions.assertEquals("int", util.getMethodReturnType("getA", new Class[]{int.class})); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(0, util.getMethodParameterCount("makeComplexCalls", new Class[]{})); + Assertions.assertEquals(0, util.getMethodParameterCount("getClassObject", new Class[]{})); + Assertions.assertEquals(1, util.getMethodParameterCount("getA", new Class[]{int.class})); + Assertions.assertEquals("int", util.getMethodParameterTypes("getA", new Class[]{int.class}).get(0)); + } catch (Exception e) { + Assertions.fail(); + } + } + + + + @Test + public void testFieldCount() { + Assertions.assertEquals(2, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertTrue(util.getFieldNames().contains("a")); + Assertions.assertTrue(util.getFieldNames().contains("classObject")); + } + + @Test + public void testFieldTypes() { + Assertions.assertEquals("int", util.getFieldTypes().get(1)); + Assertions.assertEquals("ComplexCalls", util.getFieldTypes().get(0)); + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(0, util.getFieldValue("a")); + Assertions.assertNull(util.getFieldValue("classObject")); + } catch (Exception e) { + Assertions.fail(); + } + } + + + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "ComplexCalls"); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethodMakeComplexCalls() { + try { + Object fieldClassObject = util.getFieldValue("classObject"); + Assertions.assertNull(fieldClassObject); + int fieldA = (int) util.getFieldValue("a"); + Assertions.assertEquals(0, fieldA); + int returnValue = (int) util.invokeMethod("makeComplexCalls", new Class[]{}, new Object[]{}); + Assertions.assertEquals(3, returnValue); + Assertions.assertNotEquals(fieldClassObject, util.getFieldValue("classObject")); + Assertions.assertEquals("ComplexCalls", util.getFieldValue("classObject").getClass().getName()); + Assertions.assertNotEquals(fieldA, util.getFieldValue("a")); + Assertions.assertEquals(1, util.getFieldValue("a")); + Object fieldClassObjectContent = util.getFieldValue("classObject"); + Assertions.assertEquals(2, util.getFieldValueOfObject(fieldClassObjectContent, "a")); + Assertions.assertEquals("ComplexCalls", util.getFieldValueOfObject( + fieldClassObjectContent, "classObject").getClass().getName()); + Assertions.assertEquals(3, util.getFieldValueOfObject( + util.getFieldValueOfObject(fieldClassObjectContent, "classObject"), "a")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethodGetClassObject() { + try { + Object fieldClassObject = util.getFieldValue("classObject"); + Assertions.assertNull(fieldClassObject); + int fieldA = (int) util.getFieldValue("a"); + Assertions.assertEquals(0, fieldA); + Object returnValue = util.invokeMethod("getClassObject", new Class[]{}, new Object[]{}); + Assertions.assertEquals(util.getFieldValue("classObject"), returnValue); + Assertions.assertEquals(fieldClassObject, util.getFieldValue("classObject")); + Assertions.assertEquals(fieldA, util.getFieldValue("a")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethodGetA() { + try { + Object fieldClassObject = util.getFieldValue("classObject"); + Assertions.assertNull(fieldClassObject); + int fieldA = (int) util.getFieldValue("a"); + Assertions.assertEquals(0, fieldA); + int returnValue = (int) util.invokeMethod("getA", new Class[]{int.class}, new Object[]{5}); + Assertions.assertEquals(5, returnValue); + Assertions.assertEquals(fieldClassObject, util.getFieldValue("classObject")); + Assertions.assertNotEquals(fieldA, util.getFieldValue("a")); + Assertions.assertEquals(5, util.getFieldValue("a")); + } catch (Exception e) { + Assertions.fail(); + } + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java new file mode 100644 index 0000000..a77f5d5 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java @@ -0,0 +1,159 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Constructor { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Constructor", + List.of(), + List.of(), + List.of( + new TypedConstructor( + "Constructor", + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "i", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 1, + Type.INT), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ) + ), + Type.VOID + ), + Type.VOID, + List.of() + ), + new TypedConstructor( + "Constructor", + List.of( + new TypedParameter( + "x", + Type.INT + ) + ), + new TypedBlock( + List.of( + new TypedLocalVariable( + "i", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ) + ), + Type.VOID + ), + Type.VOID, + List.of( + new TypedLocalVariable( + "x", + Type.INT + ) + ) + ), + new TypedConstructor( + "Constructor", + List.of( + new TypedParameter( + "x", + Type.INT + ), + new TypedParameter( + "y", + Type.INT + ) + ), + new TypedBlock( + List.of( + new TypedLocalVariable( + "i", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.ADD, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ) + ), + Type.VOID + ), + Type.VOID, + List.of( + new TypedLocalVariable( + "x", + Type.INT + ), + new TypedLocalVariable( + "y", + Type.INT + ) + ) + ) + ), + null, + null, + null, + Type.REFERENCE("Constructor") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java new file mode 100644 index 0000000..9b05d9f --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java @@ -0,0 +1,145 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Continue { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Continue", + List.of(), + List.of( + new TypedMethod( + "continueLoop", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "i", + Type.INT + )), + List.of( + new TypedFor( + new TypedAssignment( + new TypedIntLiteral( + 0, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ), + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.LT, + new TypedIntLiteral( + 5, + Type.INT + ), + Type.BOOL + ), + new TypedAssignment( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.ADD, + new TypedIntLiteral( + 1, + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ), + new TypedBlock( + List.of(), + List.of( + new TypedIfElse( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.EQ, + new TypedIntLiteral( + 3, + Type.INT + ), + Type.BOOL + ), + new TypedBlock( + List.of(), + List.of( + new TypedContinue() + ), + Type.VOID + + ), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID + ) + ), + Type.VOID + ), + Type.INT + ) + ), + Type.VOID + ) + ) + ), + List.of( + new TypedConstructor( + "Continue", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("Continue") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_DataTypes.java b/src/test/java/testResources/CodeGen/Features/ByteCode_DataTypes.java new file mode 100644 index 0000000..1f2dd98 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_DataTypes.java @@ -0,0 +1,184 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_DataTypes { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "DataTypes", + List.of( + new TypedDeclaration( + "x", + Type.INT + ), + new TypedDeclaration( + "y", + Type.BOOL + ), + new TypedDeclaration( + "z", + Type.CHAR + ) + ), + List.of( + new TypedMethod( + "integer", + Type.INT, + List.of( + new TypedParameter( + "i", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "a", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 1, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Type.INT + ), + new TypedReturn( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "bool", + Type.BOOL, + List.of( + new TypedParameter( + "b", + Type.BOOL + ) + ), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "a", + Type.BOOL + ) + ), + List.of( + new TypedAssignment( + new TypedBoolLiteral( + true, + Type.BOOL + ), + new TypedFieldVarAccess( + false, + null, + "a", + Type.BOOL + ), + Type.BOOL + ), + new TypedReturn( + new TypedFieldVarAccess( + false, + null, + "a", + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "character", + Type.CHAR, + List.of( + new TypedParameter( + "c", + Type.CHAR + ) + ), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "a", + Type.CHAR + ) + ), + List.of( + new TypedAssignment( + new TypedCharLiteral( + 'a', + Type.CHAR + ), + new TypedFieldVarAccess( + false, + null, + "a", + Type.CHAR + ), + Type.CHAR + ), + new TypedReturn( + new TypedFieldVarAccess( + false, + null, + "a", + Type.CHAR + ), + Type.CHAR + ) + ), + Type.CHAR + ) + ) + ), + List.of( + new TypedConstructor( + "DataTypes", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("DataTypes") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java new file mode 100644 index 0000000..236d8c8 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java @@ -0,0 +1,86 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Field { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Field", + List.of( + new TypedDeclaration( + "x", + Type.INT + ), + new TypedDeclaration( + "c", + Type.CHAR + ) + ), + List.of( + new TypedMethod( + "fieldAccess", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 0, + Type.INT + ), + new TypedFieldVarAccess( + true, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedCharLiteral( + 'a', + Type.CHAR + ), + new TypedFieldVarAccess( + true, + null, + "c", + Type.CHAR + ), + Type.CHAR + ) + ), + Type.VOID + ) + ) + ), + List.of( + new TypedConstructor( + "Field", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("Field") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_For.java b/src/test/java/testResources/CodeGen/Features/ByteCode_For.java new file mode 100644 index 0000000..6670d6d --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_For.java @@ -0,0 +1,176 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_For { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "For", + List.of(), + List.of( + new TypedMethod( + "testFor", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "i", + Type.INT + ), + new TypedLocalVariable( + "j", + Type.INT + ) + ), + List.of( + new TypedFor( + new TypedAssignment( + new TypedIntLiteral( + 0, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ), + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.LT, + new TypedIntLiteral( + 10, + Type.INT + ), + Type.BOOL + ), + new TypedAssignment( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.ADD, + new TypedIntLiteral( + 1, + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID + ), + new TypedFor( + new TypedAssignment( + new TypedIntLiteral( + 0, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "j", + Type.INT + ), + Type.INT + ), + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "j", + Type.INT + ), + Operator.LT, + new TypedIntLiteral( + 10, + Type.INT + ), + Type.BOOL + ), + new TypedAssignment( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "j", + Type.INT + ), + Operator.ADD, + new TypedIntLiteral( + 1, + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "j", + Type.INT + ), + Type.INT + ), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID + ) + ) + ) + ) + ), + List.of( + new TypedConstructor( + "For", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("For") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_If.java b/src/test/java/testResources/CodeGen/Features/ByteCode_If.java new file mode 100644 index 0000000..3733c16 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_If.java @@ -0,0 +1,86 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_If { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "If", + List.of(), + List.of( + new TypedMethod( + "ifMethod", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedIfElse( + new TypedBoolLiteral( + false, + Type.BOOL + ), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + new TypedBlock( + List.of(), + List.of( + new TypedIfElse( + new TypedBoolLiteral( + true, + Type.BOOL + ), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID + ) + ), + Type.VOID + ), + Type.VOID + ) + ), + Type.VOID + ) + ) + ), + List.of( + new TypedConstructor( + "If", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("If") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_LogicExpr.java b/src/test/java/testResources/CodeGen/Features/ByteCode_LogicExpr.java new file mode 100644 index 0000000..be4dcdc --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_LogicExpr.java @@ -0,0 +1,568 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_LogicExpr { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "LogicExpr", + List.of(), + List.of( + new TypedMethod( + "test", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "x", + Type.INT + ), + new TypedLocalVariable( + "y", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 10, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral( + 20, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.LT, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "test2", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "x", + Type.INT + ), + new TypedLocalVariable( + "y", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 10, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral( + 20, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.GT, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "test3", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "x", + Type.INT + ), + new TypedLocalVariable( + "y", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 10, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral( + 20, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.EQ, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "test4", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "x", + Type.INT + ), + new TypedLocalVariable( + "y", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 10, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral( + 20, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.NE, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "test5", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "x", + Type.INT + ), + new TypedLocalVariable( + "y", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 10, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral( + 20, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.LE, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "test6", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "x", + Type.INT + ), + new TypedLocalVariable( + "y", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 10, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral( + 20, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.GE, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "test7", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "x", + Type.BOOL + ), + new TypedLocalVariable( + "y", + Type.BOOL + ) + ), + List.of( + new TypedAssignment( + new TypedBoolLiteral( + true, + Type.BOOL + ), + new TypedFieldVarAccess( + false, + null, + "x", + Type.BOOL + ), + Type.BOOL + ), + new TypedAssignment( + new TypedBoolLiteral( + false, + Type.BOOL + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.BOOL + ), + Type.BOOL + ), + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.BOOL + ), + Operator.AND, + new TypedFieldVarAccess( + false, + null, + "y", + Type.BOOL + ), + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "test8", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "x", + Type.BOOL + ), + new TypedLocalVariable( + "y", + Type.BOOL + ) + ), + List.of( + new TypedAssignment( + new TypedBoolLiteral( + true, + Type.BOOL + ), + new TypedFieldVarAccess( + false, + null, + "x", + Type.BOOL + ), + Type.BOOL + ), + new TypedAssignment( + new TypedBoolLiteral( + false, + Type.BOOL + ), + new TypedFieldVarAccess( + false, + null, + "y", + Type.BOOL + ), + Type.BOOL + ), + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.BOOL + ), + Operator.OR, + new TypedFieldVarAccess( + false, + null, + "y", + Type.BOOL + ), + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ) + ), + List.of( + new TypedConstructor( + "LogicExpr", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("LogicExpr") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Main.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Main.java new file mode 100644 index 0000000..d33d24f --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Main.java @@ -0,0 +1,72 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Main { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Main", + List.of(), + List.of(), + List.of( + new TypedConstructor( + "Main", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + new TypedMain( + Type.VOID, + new TypedMethod( + "main", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "i", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 0, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ) + ), + Type.VOID + ) + + ) + + ), + null, + null, + Type.REFERENCE("Main") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Method.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Method.java new file mode 100644 index 0000000..a18dd24 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Method.java @@ -0,0 +1,50 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Method { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Method", + List.of(), + List.of( + new TypedMethod( + "method", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ) + ) + ), + List.of( + new TypedConstructor( + "Method", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("Method") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_MethodCall.java b/src/test/java/testResources/CodeGen/Features/ByteCode_MethodCall.java new file mode 100644 index 0000000..de2116b --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_MethodCall.java @@ -0,0 +1,214 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_MethodCall { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "MethodCall", + List.of(), + List.of( + new TypedMethod( + "methodCall", + Type.INT, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "method", + Type.INT + ), + List.of(), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "methodCall1", + Type.INT, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "method1", + Type.INT + ), + List.of( + new TypedIntLiteral( + 1, + Type.INT + ) + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "methodCall2", + Type.INT, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "method2", + Type.INT + ), + List.of( + new TypedIntLiteral( + 1, + Type.INT + ), + new TypedIntLiteral( + 2, + Type.INT + ) + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "method", + Type.INT, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedIntLiteral( + 0, + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "method1", + Type.INT, + List.of( + new TypedParameter( + "x", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "method2", + Type.INT, + List.of( + new TypedParameter( + "x", + Type.INT + ), + new TypedParameter( + "y", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.ADD, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ) + ), + List.of( + new TypedConstructor( + "MethodCall", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("MethodCall") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_MultipleClasses.java b/src/test/java/testResources/CodeGen/Features/ByteCode_MultipleClasses.java new file mode 100644 index 0000000..d2dc293 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_MultipleClasses.java @@ -0,0 +1,98 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_MultipleClasses { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "MultipleClasses", + List.of( + new TypedDeclaration( + "anotherClass", + Type.REFERENCE("AnotherClass") + ) + ), + List.of(), + List.of( + new TypedConstructor( + "MultipleClasses", + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedNew( + Type.REFERENCE("AnotherClass"), + List.of() + ), + new TypedFieldVarAccess( + true, + null, + "anotherClass", + Type.REFERENCE("AnotherClass") + ), + Type.REFERENCE("AnotherClass") + ) + ), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("MultipleClasses") + ), + new TypedClass( + "AnotherClass", + List.of( + new TypedDeclaration( + "multipleClasses", + Type.REFERENCE("MultipleClasses") + ) + ), + List.of(), + List.of( + new TypedConstructor( + "AnotherClass", + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedNew( + Type.REFERENCE("MultipleClasses"), + List.of() + ), + new TypedFieldVarAccess( + true, + null, + "multipleClasses", + Type.REFERENCE("MultipleClasses") + ), + Type.REFERENCE("MultipleClasses") + ) + ), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("AnotherClass") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Operators.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Operators.java new file mode 100644 index 0000000..cfddc31 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Operators.java @@ -0,0 +1,446 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + + +public class ByteCode_Operators { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Operators", + List.of(), + List.of( + new TypedMethod( + "add", + Type.INT, + List.of( + new TypedParameter( + "a", + Type.INT + ), + new TypedParameter( + "b", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Operator.ADD, + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "sub", + Type.INT, + List.of( + new TypedParameter( + "a", + Type.INT + ), + new TypedParameter( + "b", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Operator.SUB, + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "mul", + Type.INT, + List.of( + new TypedParameter( + "a", + Type.INT + ), + new TypedParameter( + "b", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Operator.MUL, + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "div", + Type.INT, + List.of( + new TypedParameter( + "a", + Type.INT + ), + new TypedParameter( + "b", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Operator.DIV, + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "mod", + Type.INT, + List.of( + new TypedParameter( + "a", + Type.INT + ), + new TypedParameter( + "b", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Operator.MOD, + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "brackets", + Type.INT, + List.of( + new TypedParameter( + "a", + Type.INT + ), + new TypedParameter( + "b", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Operator.ADD, + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ), + Type.INT + ), + Operator.MUL, + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "callOperatedMethods", + Type.INT, + List.of( + new TypedParameter( + "a", + Type.INT + ), + new TypedParameter( + "b", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedBinary( + new TypedBinary( + new TypedBinary( + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "add", + Type.INT + ), + List.of( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ) + ), + Type.INT + ), + Operator.ADD, + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "sub", + Type.INT + ), + List.of( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ) + ), + Type.INT + ), + Type.INT + ), + Operator.ADD, + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "mul", + Type.INT + ), + List.of( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ) + ), + Type.INT + ), + Type.INT + ), + Operator.ADD, + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "div", + Type.INT + ), + List.of( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ) + ), + Type.INT + ), + Type.INT + ), + Operator.ADD, + new TypedMethodCall( + new TypedFieldVarAccess( + false, + null, + "mod", + Type.INT + ), + List.of( + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ) + ), + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ) + ), + List.of( + new TypedConstructor( + "Operators", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("Operators") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Overloaded.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Overloaded.java new file mode 100644 index 0000000..a679cdc --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Overloaded.java @@ -0,0 +1,125 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Overloaded { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Overloaded", + List.of(), + List.of( + new TypedMethod( + "overloadedMethod", + Type.INT, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedIntLiteral( + 0, + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "overloadedMethod", + Type.INT, + List.of( + new TypedParameter( + "x", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "overloadedMethod", + Type.INT, + List.of( + new TypedParameter( + "x", + Type.INT + ), + new TypedParameter( + "y", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.ADD, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ) + ), + List.of( + new TypedConstructor( + "Overloaded", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("Overloaded") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Print.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Print.java new file mode 100644 index 0000000..14557ff --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Print.java @@ -0,0 +1,106 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Print { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Print", + List.of(), + List.of( + new TypedMethod( + "printIt", + Type.VOID, + List.of( + new TypedParameter( + "c", + Type.CHAR + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedPrint( + new TypedFieldVarAccess( + false, + null, + "c", + Type.CHAR + ), + Type.VOID + ) + ), + Type.VOID + ) + ), + new TypedMethod( + "printMoreComplex", + Type.VOID, + List.of( + new TypedParameter( + "x", + Type.INT + ), + new TypedParameter( + "y", + Type.INT + ) + ), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedPrint( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "x", + Type.INT + ), + Operator.LT, + new TypedFieldVarAccess( + false, + null, + "y", + Type.INT + ), + Type.BOOL + ), + Type.VOID + ) + ), + Type.VOID + ) + ) + ), + List.of( + new TypedConstructor( + "Print", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("Print") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Return.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Return.java new file mode 100644 index 0000000..d6fd43f --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Return.java @@ -0,0 +1,131 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Return { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Return", + List.of(), + List.of( + new TypedMethod( + "returnInt", + Type.INT, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedIntLiteral( + 10, + Type.INT + ), + Type.INT + ) + ), + Type.INT + ) + ), + new TypedMethod( + "returnVoid", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + null, + Type.VOID + ) + ), + Type.VOID + ) + ), + new TypedMethod( + "returnBoolean", + Type.BOOL, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedBoolLiteral( + true, + Type.BOOL + ), + Type.BOOL + ) + ), + Type.BOOL + ) + ), + new TypedMethod( + "returnChar", + Type.CHAR, + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedCharLiteral( + 'a', + Type.CHAR + ), + Type.CHAR + ) + ), + Type.CHAR + ) + ), + new TypedMethod( + "returnClass", + Type.REFERENCE("Return"), + List.of(), + List.of(), + new TypedBlock( + List.of(), + List.of( + new TypedReturn( + new TypedNew( + Type.REFERENCE("Return"), + List.of() + ), + Type.REFERENCE("Return") + ) + ), + Type.REFERENCE("Return") + ) + ) + ), + List.of( + new TypedConstructor( + "Return", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("Return") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Unary.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Unary.java new file mode 100644 index 0000000..255356e --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Unary.java @@ -0,0 +1,102 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.UnaryOperator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_Unary { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "Unary", + List.of(), + List.of(), + List.of( + new TypedConstructor( + "Unary", + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "a", + Type.INT + ), + new TypedLocalVariable( + "b", + Type.INT + ), + new TypedLocalVariable( + "c", + Type.BOOL + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 5, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedUnary( + UnaryOperator.SUB, + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedUnary( + UnaryOperator.NOT, + new TypedBoolLiteral( + true, + Type.BOOL + ), + Type.BOOL + ), + new TypedFieldVarAccess( + false, + null, + "c", + Type.BOOL + ), + Type.BOOL + ) + ), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("Unary") + ) + ), + null + ); + } +} diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_VariableDefWithDecl.java b/src/test/java/testResources/CodeGen/Features/ByteCode_VariableDefWithDecl.java new file mode 100644 index 0000000..f33ed3f --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_VariableDefWithDecl.java @@ -0,0 +1,74 @@ +package testResources.CodeGen.Features; + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_VariableDefWithDecl { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "VariableDefWithDecl", + List.of(), + List.of(), + List.of( + new TypedConstructor( + "VariableDefWithDecl", + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "a", + Type.INT + ), + new TypedLocalVariable( + "b", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 10, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "a", + Type.INT + ), + Type.INT + ), + new TypedAssignment( + new TypedIntLiteral( + 20, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "b", + Type.INT + ), + Type.INT + ) + ), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("VariableDefWithDecl") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_While.java b/src/test/java/testResources/CodeGen/Features/ByteCode_While.java new file mode 100644 index 0000000..9fbde62 --- /dev/null +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_While.java @@ -0,0 +1,190 @@ +package testResources.CodeGen.Features; + +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class ByteCode_While { + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "While", + List.of(), + List.of( + new TypedMethod( + "whileLoop", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "i", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 0, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ), + new TypedWhile( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.LT, + new TypedIntLiteral( + 5, + Type.INT + ), + Type.BOOL + ), + new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.ADD, + new TypedIntLiteral( + 1, + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ) + ), + Type.VOID + ), + Type.VOID + ) + ) + ) + ), + new TypedMethod( + "doWhileLoop", + Type.VOID, + List.of(), + List.of(), + new TypedBlock( + List.of( + new TypedLocalVariable( + "i", + Type.INT + ) + ), + List.of( + new TypedAssignment( + new TypedIntLiteral( + 0, + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ), + new TypedDoWhile( + new TypedBlock( + List.of(), + List.of( + new TypedAssignment( + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.ADD, + new TypedIntLiteral( + 1, + Type.INT + ), + Type.INT + ), + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Type.INT + ) + ), + Type.VOID + ), + new TypedBinary( + new TypedFieldVarAccess( + false, + null, + "i", + Type.INT + ), + Operator.LT, + new TypedIntLiteral( + 5, + Type.INT + ), + Type.BOOL + ), + Type.VOID + ) + ) + ) + ) + ), + List.of( + new TypedConstructor( + "While", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + null, + Type.REFERENCE("While") + ) + ), + null + ); + } +} \ No newline at end of file diff --git a/src/test/java/testResources/TypedAST/TypedASTFeatures/TypedAST_Main.java b/src/test/java/testResources/TypedAST/TypedASTFeatures/TypedAST_Main.java index 76e3aae..fa665d6 100644 --- a/src/test/java/testResources/TypedAST/TypedASTFeatures/TypedAST_Main.java +++ b/src/test/java/testResources/TypedAST/TypedASTFeatures/TypedAST_Main.java @@ -31,7 +31,12 @@ public class TypedAST_Main { new TypedMethod( "main", Type.VOID, - List.of(), + List.of( + new TypedParameter( + "args", + Type.REFERENCE("String[]") + ) + ), List.of(), new TypedBlock( List.of( diff --git a/src/test/testFiles/JavaTestfilesFeatures/Break.java b/src/test/testFiles/ASTandTypedASTFeatures/Break.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Break.java rename to src/test/testFiles/ASTandTypedASTFeatures/Break.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Class.java b/src/test/testFiles/ASTandTypedASTFeatures/Class.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Class.java rename to src/test/testFiles/ASTandTypedASTFeatures/Class.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/ClassObjects.java b/src/test/testFiles/ASTandTypedASTFeatures/ClassObjects.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/ClassObjects.java rename to src/test/testFiles/ASTandTypedASTFeatures/ClassObjects.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Comment.java b/src/test/testFiles/ASTandTypedASTFeatures/Comment.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Comment.java rename to src/test/testFiles/ASTandTypedASTFeatures/Comment.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/CompAssign.java b/src/test/testFiles/ASTandTypedASTFeatures/CompAssign.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/CompAssign.java rename to src/test/testFiles/ASTandTypedASTFeatures/CompAssign.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/ComplexCalls.java b/src/test/testFiles/ASTandTypedASTFeatures/ComplexCalls.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/ComplexCalls.java rename to src/test/testFiles/ASTandTypedASTFeatures/ComplexCalls.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Constructor.java b/src/test/testFiles/ASTandTypedASTFeatures/Constructor.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Constructor.java rename to src/test/testFiles/ASTandTypedASTFeatures/Constructor.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Continue.java b/src/test/testFiles/ASTandTypedASTFeatures/Continue.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Continue.java rename to src/test/testFiles/ASTandTypedASTFeatures/Continue.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/DataTypes.java b/src/test/testFiles/ASTandTypedASTFeatures/DataTypes.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/DataTypes.java rename to src/test/testFiles/ASTandTypedASTFeatures/DataTypes.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Field.java b/src/test/testFiles/ASTandTypedASTFeatures/Field.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Field.java rename to src/test/testFiles/ASTandTypedASTFeatures/Field.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/For.java b/src/test/testFiles/ASTandTypedASTFeatures/For.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/For.java rename to src/test/testFiles/ASTandTypedASTFeatures/For.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/If.java b/src/test/testFiles/ASTandTypedASTFeatures/If.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/If.java rename to src/test/testFiles/ASTandTypedASTFeatures/If.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/LogicExpr.java b/src/test/testFiles/ASTandTypedASTFeatures/LogicExpr.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/LogicExpr.java rename to src/test/testFiles/ASTandTypedASTFeatures/LogicExpr.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Main.java b/src/test/testFiles/ASTandTypedASTFeatures/Main.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Main.java rename to src/test/testFiles/ASTandTypedASTFeatures/Main.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Method.java b/src/test/testFiles/ASTandTypedASTFeatures/Method.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Method.java rename to src/test/testFiles/ASTandTypedASTFeatures/Method.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/MethodCall.java b/src/test/testFiles/ASTandTypedASTFeatures/MethodCall.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/MethodCall.java rename to src/test/testFiles/ASTandTypedASTFeatures/MethodCall.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/MultipleClasses.java b/src/test/testFiles/ASTandTypedASTFeatures/MultipleClasses.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/MultipleClasses.java rename to src/test/testFiles/ASTandTypedASTFeatures/MultipleClasses.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Operators.java b/src/test/testFiles/ASTandTypedASTFeatures/Operators.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Operators.java rename to src/test/testFiles/ASTandTypedASTFeatures/Operators.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Overloaded.java b/src/test/testFiles/ASTandTypedASTFeatures/Overloaded.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Overloaded.java rename to src/test/testFiles/ASTandTypedASTFeatures/Overloaded.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Print.java b/src/test/testFiles/ASTandTypedASTFeatures/Print.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Print.java rename to src/test/testFiles/ASTandTypedASTFeatures/Print.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Return.java b/src/test/testFiles/ASTandTypedASTFeatures/Return.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Return.java rename to src/test/testFiles/ASTandTypedASTFeatures/Return.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/Unary.java b/src/test/testFiles/ASTandTypedASTFeatures/Unary.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/Unary.java rename to src/test/testFiles/ASTandTypedASTFeatures/Unary.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/VariableDefWithDecl.java b/src/test/testFiles/ASTandTypedASTFeatures/VariableDefWithDecl.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/VariableDefWithDecl.java rename to src/test/testFiles/ASTandTypedASTFeatures/VariableDefWithDecl.java diff --git a/src/test/testFiles/JavaTestfilesFeatures/While.java b/src/test/testFiles/ASTandTypedASTFeatures/While.java similarity index 100% rename from src/test/testFiles/JavaTestfilesFeatures/While.java rename to src/test/testFiles/ASTandTypedASTFeatures/While.java diff --git a/src/test/testFiles/CodeGenFeatures/Break.java b/src/test/testFiles/CodeGenFeatures/Break.java new file mode 100644 index 0000000..ae9eded --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Break.java @@ -0,0 +1,21 @@ +public class Break { + int whileRepetition; + int forRepetition; + + public void breakMethod(boolean breakCondition) { + this.whileRepetition = 0; + while (this.whileRepetition < 10) { + this.whileRepetition += 1; + if (breakCondition && (this.whileRepetition == 5)) { + break; + } + } + this.forRepetition = 0; + for (int i = 0; i < 10; i += 1) { + this.forRepetition += 1; + if (breakCondition && (this.forRepetition == 5)) { + break; + } + } + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Class.java b/src/test/testFiles/CodeGenFeatures/Class.java new file mode 100644 index 0000000..366f84e --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Class.java @@ -0,0 +1,2 @@ +public class Class { +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/ClassObjects.java b/src/test/testFiles/CodeGenFeatures/ClassObjects.java new file mode 100644 index 0000000..565f559 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/ClassObjects.java @@ -0,0 +1,19 @@ +public class ClassObjects { + + ClassObjects object; + ClassObjects objectWithValue; + int integerValue; + + public ClassObjects() { + } + + public ClassObjects(int value) { + this.integerValue = value; + } + + public void objectsMethod() { + this.object = new ClassObjects(); + this.objectWithValue = new ClassObjects(2); + this.integerValue = 2; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Comment.java b/src/test/testFiles/CodeGenFeatures/Comment.java new file mode 100644 index 0000000..a8ce7f3 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Comment.java @@ -0,0 +1,12 @@ +public class Comment { + // This is a comment + + + // This is another comment + // With multiple lines + + /* This is a comment + with multiple lines written in a block + which is closed/started with a * and a / + */ +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/CompAssign.java b/src/test/testFiles/CodeGenFeatures/CompAssign.java new file mode 100644 index 0000000..e0d42c2 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/CompAssign.java @@ -0,0 +1,26 @@ +public class CompAssign { + public int increase(int a) { + a += 1; + return a; + } + + public int decrease(int a) { + a -= 1; + return a; + } + + public int multiply(int a) { + a *= 2; + return a; + } + + public int divide(int a) { + a /= 2; + return a; + } + + public int modulus(int a) { + a %= 2; + return a; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/ComplexCalls.java b/src/test/testFiles/CodeGenFeatures/ComplexCalls.java new file mode 100644 index 0000000..2b33c79 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/ComplexCalls.java @@ -0,0 +1,22 @@ +public class ComplexCalls { + ComplexCalls classObject; + int a; + + public int makeComplexCalls() { + this.classObject = new ComplexCalls(); + this.a = 1; + this.classObject.a = 2; + this.classObject.classObject = new ComplexCalls(); + this.classObject.classObject.a = this.getClassObject().a; + return this.classObject.classObject.getA(3); + } + + public ComplexCalls getClassObject() { + return this.classObject; + } + + public int getA(int x) { + this.a = x; + return x; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Constructor.java b/src/test/testFiles/CodeGenFeatures/Constructor.java new file mode 100644 index 0000000..4262853 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Constructor.java @@ -0,0 +1,18 @@ +public class Constructor { + int i; + + public Constructor() { + this.i; + i = 1; + } + + public Constructor(this.x) { + this.i; + i= x; + } + + public Constructor(this.x, this.y) { + this.i; + i= x + y; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Continue.java b/src/test/testFiles/CodeGenFeatures/Continue.java new file mode 100644 index 0000000..69f4c6c --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Continue.java @@ -0,0 +1,12 @@ +public class Continue { + int repetitions; + public void continueLoop(boolean shouldContinue) { + this.repetitions = 0; + for (int i = 0; i < 5; i+=1) { + if (shouldContinue && i == 3) { + continue; + } + repetitions += 1; + } + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/DataTypes.java b/src/test/testFiles/CodeGenFeatures/DataTypes.java new file mode 100644 index 0000000..9c3b8db --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/DataTypes.java @@ -0,0 +1,23 @@ +public class DataTypes { + int x; + boolean y; + char z; + + public int integer(int i) { + int a = 1; + this.x = a; + return a; + } + + public boolean bool(boolean b) { + boolean a = true; + this.y = a; + return a; + } + + public char character(char c) { + char a = 'a'; + this.z = a; + return a; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Field.java b/src/test/testFiles/CodeGenFeatures/Field.java new file mode 100644 index 0000000..5c23526 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Field.java @@ -0,0 +1,17 @@ +public class Field { + int x; + public char c; + + public void fieldAccess() { + this.x = 0; + this.c = 'a'; + } + + public void setX(int x) { + this.x = x; + } + + public void setC(char c) { + this.c = c; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/For.java b/src/test/testFiles/CodeGenFeatures/For.java new file mode 100644 index 0000000..012eb0b --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/For.java @@ -0,0 +1,21 @@ +public class For { + int repititionsFirtsFor; + int repititionsSecondFor; + + public For() { + this.repititionsFirtsFor = 0; + this.repititionsSecondFor = 0; + } + + public void testFor() { + this.repititionsFirtsFor = 0; + for (int i = 0; i < 10; i = i+1) { + this.repititionsFirtsFor = this.repititionsFirtsFor + i; + } + this.repititionsSecondFor = 0; + int j; + for (j = 0; j < 10; j += 1) { + this.repititionsSecondFor = this.repititionsSecondFor + j; + } + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/If.java b/src/test/testFiles/CodeGenFeatures/If.java new file mode 100644 index 0000000..53b929f --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/If.java @@ -0,0 +1,11 @@ +public class If { + public int ifMethod(boolean ifShouldRun, boolean elseIfShouldRun) { + if (ifShouldRun) { + return 1; + } else if (elseIfShouldRun) { + return 2; + } else { + return 3; + } + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/LogicExpr.java b/src/test/testFiles/CodeGenFeatures/LogicExpr.java new file mode 100644 index 0000000..520ba79 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/LogicExpr.java @@ -0,0 +1,33 @@ +public class LogicExpr { + public boolean testSmaller(int x, int y) { + return x < y; + } + + public boolean testLarger(int x, int y) { + return x > y; + } + + public boolean testEqual(int x, int y) { + return x == y; + } + + public boolean testNotEqual(int x, int y) { + return x != y; + } + + public boolean testSmallerEqual(int x, int y) { + return x <= y; + } + + public boolean testLargerEqual(int x, int y) { + return x >= y; + } + + public boolean testAND(boolean x, boolean y) { + return x && y; + } + + public boolean testOR(boolean x, boolean y) { + return x || y; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Main.java b/src/test/testFiles/CodeGenFeatures/Main.java new file mode 100644 index 0000000..5fab2d3 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Main.java @@ -0,0 +1,11 @@ +public class Main { + int i; + + public Main() { + this.i = 0; + } + + public static void main(String[] args) { + this.i = 10; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Method.java b/src/test/testFiles/CodeGenFeatures/Method.java new file mode 100644 index 0000000..f19481b --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Method.java @@ -0,0 +1,11 @@ +public class Method { + int x; + + public Method() { + this.x = 0; + } + + public void method() { + this.x = 10; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/MethodCall.java b/src/test/testFiles/CodeGenFeatures/MethodCall.java new file mode 100644 index 0000000..3bc0840 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/MethodCall.java @@ -0,0 +1,26 @@ +public class MethodCall { + + public int methodCall() { + return method(); + } + + public int methodCall1() { + return method1(1); + } + + public int methodCall2() { + return method2(1, 2); + } + + public int method() { + return 0; + } + + public int method1(int x) { + return x; + } + + public int method2(int x, int y) { + return x + y; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/MultipleClasses.java b/src/test/testFiles/CodeGenFeatures/MultipleClasses.java new file mode 100644 index 0000000..84c63bf --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/MultipleClasses.java @@ -0,0 +1,15 @@ +public class MultipleClasses { + AnotherClass anotherClass; + + public MultipleClasses() { + this.anotherClass = new AnotherClass(); + } +} + +public class AnotherClass { + MultipleClasses multipleClasses; + + public AnotherClass() { + this.multipleClasses = new MultipleClasses(); + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Operators.java b/src/test/testFiles/CodeGenFeatures/Operators.java new file mode 100644 index 0000000..55b6b40 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Operators.java @@ -0,0 +1,29 @@ +public class Operators { + public int add(int a, int b) { + return a + b; + } + + public int sub(int a, int b) { + return a - b; + } + + public int mul(int a, int b) { + return a * b; + } + + public int div(int a, int b) { + return a / b; + } + + public int mod(int a, int b) { + return a % b; + } + + public int brackets(int a, int b) { + return (a + b) * a; + } + + public int callOperatedMethods(int a, int b) { + return add(a, b) + sub(a, b) + mul(a, b) + div(a, b) + mod(a, b); + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Overloaded.java b/src/test/testFiles/CodeGenFeatures/Overloaded.java new file mode 100644 index 0000000..bcb688c --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Overloaded.java @@ -0,0 +1,13 @@ +public class Overloaded { + public int overloadedMethod() { + return 0; + } + + public int overloadedMethod(int x) { + return x; + } + + public int overloadedMethod(int x, int y) { + return x + y; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Print.java b/src/test/testFiles/CodeGenFeatures/Print.java new file mode 100644 index 0000000..5a2ceb9 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Print.java @@ -0,0 +1,9 @@ +public class Print { + public void printIt(char c) { + print(c); + } + + public void printMoreComplex(int x, int y) { + print(x < y); + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Return.java b/src/test/testFiles/CodeGenFeatures/Return.java new file mode 100644 index 0000000..491c22a --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Return.java @@ -0,0 +1,21 @@ +public class Return { + public int returnInt() { + return 10; + } + + public void returnVoid() { + return; + } + + public boolean returnBoolean() { + return true; + } + + public char returnChar() { + return 'a'; + } + + public Return returnClass() { + return new Return(); + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Unary.java b/src/test/testFiles/CodeGenFeatures/Unary.java new file mode 100644 index 0000000..d0ca0e1 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/Unary.java @@ -0,0 +1,10 @@ +public class Unary { + + public int testMinus(int a) { + return -a; + } + + public boolean testNot(boolean a) { + return !a; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/VariableDefWithDecl.java b/src/test/testFiles/CodeGenFeatures/VariableDefWithDecl.java new file mode 100644 index 0000000..514de1e --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/VariableDefWithDecl.java @@ -0,0 +1,13 @@ +public class VariableDefWithDecl { + + public int testDefWithDeclInOne() { + int a = 10; + return a; + } + + public int testDefWithDeclSeparate() { + int a; + a = 10; + return a; + } +} \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/While.java b/src/test/testFiles/CodeGenFeatures/While.java new file mode 100644 index 0000000..93d88e9 --- /dev/null +++ b/src/test/testFiles/CodeGenFeatures/While.java @@ -0,0 +1,24 @@ +public class While { + int whileRepititions; + int doWhileRepititions; + + public While() { + this.whileRepititions = 0; + this.doWhileRepititions = 0; + } + + + public void whileLoop() { + this.whileRepititions = 0; + while (i < 5) { + this.whileRepititions = this.whileRepititions + 1; + } + } + + public void doWhileLoop() { + this.doWhileRepititions = 0; + do { + this.doWhileRepititions = this.doWhileRepititions + 1; + } while (i < 5); + } +} \ No newline at end of file From 2029f496ced143391d0f5bde95ff1e0ae677b798 Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Thu, 27 Jun 2024 19:24:27 +0200 Subject: [PATCH 2/7] added some e2e tests for CodeGenFiles with reflections --- .../Features/ByteCode_Constructor.java | 173 +++--------------- .../CodeGenFeatures/Constructor.java | 13 +- 2 files changed, 30 insertions(+), 156 deletions(-) diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java index a77f5d5..48bfaf2 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java @@ -3,157 +3,34 @@ package testResources.CodeGen.Features; import de.maishai.ast.Operator; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_Constructor { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "Constructor", - List.of(), - List.of(), - List.of( - new TypedConstructor( - "Constructor", - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "i", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 1, - Type.INT), - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Type.INT - ) - ), - Type.VOID - ), - Type.VOID, - List.of() - ), - new TypedConstructor( - "Constructor", - List.of( - new TypedParameter( - "x", - Type.INT - ) - ), - new TypedBlock( - List.of( - new TypedLocalVariable( - "i", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Type.INT - ) - ), - Type.VOID - ), - Type.VOID, - List.of( - new TypedLocalVariable( - "x", - Type.INT - ) - ) - ), - new TypedConstructor( - "Constructor", - List.of( - new TypedParameter( - "x", - Type.INT - ), - new TypedParameter( - "y", - Type.INT - ) - ), - new TypedBlock( - List.of( - new TypedLocalVariable( - "i", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "x", - Type.INT - ), - Operator.ADD, - new TypedFieldVarAccess( - false, - null, - "y", - Type.INT - ), - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Type.INT - ) - ), - Type.VOID - ), - Type.VOID, - List.of( - new TypedLocalVariable( - "x", - Type.INT - ), - new TypedLocalVariable( - "y", - Type.INT - ) - ) - ) - ), - null, - null, - null, - Type.REFERENCE("Constructor") - ) - ), - null - ); + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Constructor.java"), "Constructor"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(3, util.getConstructorCount()); + } + + @Test + public void testConstructor1() { + Assertions.assertEquals("Constructor", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); } } \ No newline at end of file diff --git a/src/test/testFiles/CodeGenFeatures/Constructor.java b/src/test/testFiles/CodeGenFeatures/Constructor.java index 4262853..7b2f7a9 100644 --- a/src/test/testFiles/CodeGenFeatures/Constructor.java +++ b/src/test/testFiles/CodeGenFeatures/Constructor.java @@ -2,17 +2,14 @@ public class Constructor { int i; public Constructor() { - this.i; - i = 1; + this.i = 1; } - public Constructor(this.x) { - this.i; - i= x; + public Constructor(int x) { + this.i= x; } - public Constructor(this.x, this.y) { - this.i; - i= x + y; + public Constructor(int x, int y) { + this.i= x + y; } } \ No newline at end of file From 71df6cc1d5b5783c38fb74aba663d874f458c2b4 Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Thu, 27 Jun 2024 20:10:51 +0200 Subject: [PATCH 3/7] added some e2e tests for CodeGenFiles with reflections --- .../CodeGen/Features/ByteCode_Break.java | 21 +- .../Features/ByteCode_Constructor.java | 77 ++++ .../CodeGen/Features/ByteCode_Continue.java | 255 +++++++------ .../CodeGen/Features/ByteCode_DataTypes.java | 339 +++++++++--------- .../CodeGen/Features/ByteCode_Field.java | 140 ++++---- 5 files changed, 436 insertions(+), 396 deletions(-) diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java index 9b82327..f7e6cfe 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Break.java @@ -49,7 +49,7 @@ public class ByteCode_Break { @Test public void testMethodNames() { - Assertions.assertEquals("breakMethod", util.getMethodNames().get(0)); + Assertions.assertTrue(util.getMethodNames().contains("breakMethod")); } @Test @@ -64,8 +64,7 @@ public class ByteCode_Break { @Test public void testMethodParameters() { try { - Assertions.assertEquals(1, util.getMethodParameterCount("breakMethod", new Class[]{})); - Assertions.assertEquals("boolean", util.getMethodParameterTypes("breakMethod", new Class[]{}).get(0)); + Assertions.assertEquals("boolean", util.getMethodParameterTypes("breakMethod", new Class[]{boolean.class}).get(0)); } catch (Exception e) { Assertions.fail(); } @@ -78,8 +77,8 @@ public class ByteCode_Break { @Test public void testFieldNames() { - Assertions.assertEquals("whileRepetition", util.getFieldNames().get(0)); - Assertions.assertEquals("forRepetition", util.getFieldNames().get(1)); + Assertions.assertTrue(util.getFieldNames().contains("whileRepetition")); + Assertions.assertTrue(util.getFieldNames().contains("forRepetition")); } @Test @@ -115,12 +114,10 @@ public class ByteCode_Break { @Test public void testInvokeBreakMethodWithFalse() { try { - int fieldWhileRepetition = (int) util.getFieldValue("whileRepetition"); - int fieldForRepetition = (int) util.getFieldValue("forRepetition"); + Assertions.assertEquals(0, util.getFieldValue("whileRepetition")); + Assertions.assertEquals(0, util.getFieldValue("forRepetition")); util.invokeMethod("breakMethod", new Class[]{boolean.class}, new Object[]{false}); - Assertions.assertNotEquals(fieldWhileRepetition, util.getFieldValue("whileRepetition")); Assertions.assertEquals(10, util.getFieldValue("whileRepetition")); - Assertions.assertNotEquals(fieldForRepetition, util.getFieldValue("forRepetition")); Assertions.assertEquals(10, util.getFieldValue("forRepetition")); } catch (Exception e) { throw new RuntimeException(e); @@ -130,12 +127,10 @@ public class ByteCode_Break { @Test public void testInvokeBreakMethodWithTrue() { try { - int fieldWhileRepetition = (int) util.getFieldValue("whileRepetition"); - int fieldForRepetition = (int) util.getFieldValue("forRepetition"); + Assertions.assertEquals(0, util.getFieldValue("whileRepetition")); + Assertions.assertEquals(0, util.getFieldValue("forRepetition")); util.invokeMethod("breakMethod", new Class[]{boolean.class}, new Object[]{true}); - Assertions.assertNotEquals(fieldWhileRepetition, util.getFieldValue("whileRepetition")); Assertions.assertEquals(5, util.getFieldValue("whileRepetition")); - Assertions.assertNotEquals(fieldForRepetition, util.getFieldValue("forRepetition")); Assertions.assertEquals(5, util.getFieldValue("forRepetition")); } catch (Exception e) { throw new RuntimeException(e); diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java index 48bfaf2..d568353 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Constructor.java @@ -33,4 +33,81 @@ public class ByteCode_Constructor { Assertions.assertEquals("Constructor", util.getConstructorNames().get(0)); Assertions.assertEquals(0, util.getConstructorParameterCount(0)); } + + @Test + public void testConstructor2() { + Assertions.assertEquals("Constructor", util.getConstructorNames().get(1)); + Assertions.assertEquals(1, util.getConstructorParameterCount(1)); + Assertions.assertEquals("int", util.getConstructorParameterTypes(1).get(0)); + } + + @Test + public void testConstructor3() { + Assertions.assertEquals("Constructor", util.getConstructorNames().get(2)); + Assertions.assertEquals(2, util.getConstructorParameterCount(2)); + Assertions.assertEquals("int", util.getConstructorParameterTypes(2).get(0)); + Assertions.assertEquals("int", util.getConstructorParameterTypes(2).get(1)); + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(0, util.getMethodCount()); + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(1, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertEquals("i", util.getFieldNames().get(0)); + } + + @Test + public void testFieldTypes() { + Assertions.assertEquals("int", util.getFieldTypes().get(0)); + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(1, util.getFieldValue("i")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeConstructor1() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "Constructor"); + Assertions.assertEquals(1, util.getFieldValueOfObject(instance, "i")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeConstructor2() { + try { + Object instance = util.invokeConstructor(new Class[]{int.class}, new Object[]{5}); + Assertions.assertEquals(instance.getClass().getName(), "Constructor"); + Assertions.assertEquals(5, util.getFieldValueOfObject(instance, "i")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeConstructor3() { + try { + Object instance = util.invokeConstructor(new Class[]{int.class, int.class}, new Object[]{5, 10}); + Assertions.assertEquals(instance.getClass().getName(), "Constructor"); + Assertions.assertEquals(15, util.getFieldValueOfObject(instance, "i")); + } catch (Exception e) { + Assertions.fail(); + } + } } \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java index 9b05d9f..76f8ad9 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Continue.java @@ -3,143 +3,132 @@ package testResources.CodeGen.Features; import de.maishai.ast.Operator; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_Continue { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "Continue", - List.of(), - List.of( - new TypedMethod( - "continueLoop", - Type.VOID, - List.of(), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "i", - Type.INT - )), - List.of( - new TypedFor( - new TypedAssignment( - new TypedIntLiteral( - 0, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Type.INT - ), - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Operator.LT, - new TypedIntLiteral( - 5, - Type.INT - ), - Type.BOOL - ), - new TypedAssignment( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Operator.ADD, - new TypedIntLiteral( - 1, - Type.INT - ), - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Type.INT - ), - new TypedBlock( - List.of(), - List.of( - new TypedIfElse( - new TypedBinary( - new TypedFieldVarAccess( - false, - null, - "i", - Type.INT - ), - Operator.EQ, - new TypedIntLiteral( - 3, - Type.INT - ), - Type.BOOL - ), - new TypedBlock( - List.of(), - List.of( - new TypedContinue() - ), - Type.VOID - ), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID - ) - ), - Type.VOID - ), - Type.INT - ) - ), - Type.VOID - ) - ) - ), - List.of( - new TypedConstructor( - "Continue", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - null, - null, - null, - Type.REFERENCE("Continue") - ) - ), - null - ); + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Continue.java"), "Continue"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("Continue", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("Continue", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(1, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertEquals("continueMethod", util.getMethodNames().get(0)); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("void", util.getMethodReturnType("continueMethod", new Class[]{boolean.class})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(1, util.getMethodParameterCount("continueMethod", new Class[]{boolean.class})); + Assertions.assertEquals("boolean", util.getMethodParameterTypes("continueMethod", new Class[]{boolean.class}).get(0)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(1, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertEquals("repetitions", util.getFieldNames().get(0)); + } + + @Test + public void testFieldTypes() { + try { + Assertions.assertEquals("int", util.getFieldTypes().get(0)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(0, util.getFieldValue("repetitions")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "Continue"); + Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitions")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethodWithFalse() { + try { + int fieldRepetitions = (int) util.getFieldValue("repetitions"); + Assertions.assertEquals(0, fieldRepetitions); + util.invokeMethod("continueMethod", new Class[]{boolean.class}, new Object[]{false}); + Assertions.assertEquals(5, util.getFieldValue("repetitions")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethodWithTrue() { + try { + int fieldRepetitions = (int) util.getFieldValue("repetitions"); + Assertions.assertEquals(0, fieldRepetitions); + util.invokeMethod("continueMethod", new Class[]{boolean.class}, new Object[]{true}); + Assertions.assertEquals(4, util.getFieldValue("repetitions")); + } catch (Exception e) { + Assertions.fail(); + } } } \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_DataTypes.java b/src/test/java/testResources/CodeGen/Features/ByteCode_DataTypes.java index 1f2dd98..a7e2b1d 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_DataTypes.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_DataTypes.java @@ -2,183 +2,174 @@ package testResources.CodeGen.Features; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_DataTypes { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "DataTypes", - List.of( - new TypedDeclaration( - "x", - Type.INT - ), - new TypedDeclaration( - "y", - Type.BOOL - ), - new TypedDeclaration( - "z", - Type.CHAR - ) - ), - List.of( - new TypedMethod( - "integer", - Type.INT, - List.of( - new TypedParameter( - "i", - Type.INT - ) - ), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "a", - Type.INT - ) - ), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 1, - Type.INT - ), - new TypedFieldVarAccess( - false, - null, - "a", - Type.INT - ), - Type.INT - ), - new TypedReturn( - new TypedFieldVarAccess( - false, - null, - "a", - Type.INT - ), - Type.INT - ) - ), - Type.INT - ) - ), - new TypedMethod( - "bool", - Type.BOOL, - List.of( - new TypedParameter( - "b", - Type.BOOL - ) - ), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "a", - Type.BOOL - ) - ), - List.of( - new TypedAssignment( - new TypedBoolLiteral( - true, - Type.BOOL - ), - new TypedFieldVarAccess( - false, - null, - "a", - Type.BOOL - ), - Type.BOOL - ), - new TypedReturn( - new TypedFieldVarAccess( - false, - null, - "a", - Type.BOOL - ), - Type.BOOL - ) - ), - Type.BOOL - ) - ), - new TypedMethod( - "character", - Type.CHAR, - List.of( - new TypedParameter( - "c", - Type.CHAR - ) - ), - List.of(), - new TypedBlock( - List.of( - new TypedLocalVariable( - "a", - Type.CHAR - ) - ), - List.of( - new TypedAssignment( - new TypedCharLiteral( - 'a', - Type.CHAR - ), - new TypedFieldVarAccess( - false, - null, - "a", - Type.CHAR - ), - Type.CHAR - ), - new TypedReturn( - new TypedFieldVarAccess( - false, - null, - "a", - Type.CHAR - ), - Type.CHAR - ) - ), - Type.CHAR - ) - ) - ), - List.of( - new TypedConstructor( - "DataTypes", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - null, - null, - null, - Type.REFERENCE("DataTypes") - ) - ), - null - ); + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/DataTypes.java"), "DataTypes"); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("DataTypes", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("DataTypes", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(3, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertTrue(util.getMethodNames().contains("integer")); + Assertions.assertTrue(util.getMethodNames().contains("bool")); + Assertions.assertTrue(util.getMethodNames().contains("character")); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("int", util.getMethodReturnType("integer", new Class[]{int.class})); + Assertions.assertEquals("boolean", util.getMethodReturnType("bool", new Class[]{boolean.class})); + Assertions.assertEquals("char", util.getMethodReturnType("character", new Class[]{char.class})); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testMethodParameters() { + try { + Assertions.assertEquals(1, + util.getMethodParameterCount("integer", new Class[]{int.class})); + Assertions.assertEquals("int", + util.getMethodParameterTypes("integer", new Class[]{int.class}).get(0)); + Assertions.assertEquals(1, + util.getMethodParameterCount("bool", new Class[]{boolean.class})); + Assertions.assertEquals("boolean", + util.getMethodParameterTypes("bool", new Class[]{boolean.class}).get(0)); + Assertions.assertEquals(1, + util.getMethodParameterCount("character", new Class[]{char.class})); + Assertions.assertEquals("char", + util.getMethodParameterTypes("character", new Class[]{char.class}).get(0)); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testFieldCount() { + Assertions.assertEquals(3, util.getFieldCount()); + } + + @Test + public void testFieldNames() { + Assertions.assertTrue(util.getFieldNames().contains("x")); + Assertions.assertTrue(util.getFieldNames().contains("y")); + Assertions.assertTrue(util.getFieldNames().contains("z")); + } + + @Test + public void testFieldTypes() { + Assertions.assertEquals("int", util.getFieldTypes().get(0)); + Assertions.assertEquals("boolean", util.getFieldTypes().get(1)); + Assertions.assertEquals("char", util.getFieldTypes().get(2)); + } + + @Test + public void testFieldValues() { + try { + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertEquals(false, util.getFieldValue("y")); + Assertions.assertEquals('\u0000', util.getFieldValue("z")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeDefaultConstructor() { + try { + Object instance = util.invokeConstructor(new Class[]{}, null); + Assertions.assertEquals(instance.getClass().getName(), "DataTypes"); + Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "x")); + Assertions.assertEquals(false, util.getFieldValueOfObject(instance, "y")); + Assertions.assertEquals('\u0000', util.getFieldValueOfObject(instance, "z")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethodIntegerWith3() { + try { + Assertions.assertEquals(0, (int) util.getFieldValue("x")); + Assertions.assertFalse((boolean) util.getFieldValue("y")); + Assertions.assertEquals('\u0000', (char) util.getFieldValue("z")); + int returnValue = (int) util.invokeMethod("integer", new Class[]{int.class}, new Object[]{3}); + Assertions.assertEquals(1, returnValue); + Assertions.assertEquals(1, util.getFieldValue("x")); + Assertions.assertFalse((boolean) util.getFieldValue("y")); + Assertions.assertEquals('\u0000', util.getFieldValue("z")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethodBoolWithTrue() { + try { + Assertions.assertEquals(0, (int) util.getFieldValue("x")); + Assertions.assertFalse((boolean) util.getFieldValue("y")); + Assertions.assertEquals('\u0000', (char) util.getFieldValue("z")); + boolean returnValue = (boolean) util.invokeMethod("bool", new Class[]{boolean.class}, new Object[]{true}); + Assertions.assertTrue(returnValue); + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertTrue((boolean) util.getFieldValue("y")); + Assertions.assertEquals('\u0000', util.getFieldValue("z")); + } catch (Exception e) { + Assertions.fail(); + } + } + + @Test + public void testInvokeMethodCharacterWithB() { + try { + Assertions.assertEquals(0, (int) util.getFieldValue("x")); + Assertions.assertFalse((boolean) util.getFieldValue("y")); + Assertions.assertEquals('\u0000', (char) util.getFieldValue("z")); + char returnValue = (char) util.invokeMethod("character", new Class[]{char.class}, new Object[]{'B'}); + Assertions.assertEquals('a', returnValue); + Assertions.assertEquals(0, util.getFieldValue("x")); + Assertions.assertFalse((boolean) util.getFieldValue("y")); + Assertions.assertEquals('a', util.getFieldValue("z")); + } catch (Exception e) { + Assertions.fail(); + } } } \ No newline at end of file diff --git a/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java b/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java index 236d8c8..1a60241 100644 --- a/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java +++ b/src/test/java/testResources/CodeGen/Features/ByteCode_Field.java @@ -2,85 +2,73 @@ package testResources.CodeGen.Features; import de.maishai.typedast.Type; import de.maishai.typedast.typedclass.*; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import testResources.CodeGen.BytecodeTestUtil; import java.util.List; public class ByteCode_Field { - public static TypedProgram get() { - return new TypedProgram( - List.of( - new TypedClass( - "Field", - List.of( - new TypedDeclaration( - "x", - Type.INT - ), - new TypedDeclaration( - "c", - Type.CHAR - ) - ), - List.of( - new TypedMethod( - "fieldAccess", - Type.VOID, - List.of(), - List.of(), - new TypedBlock( - List.of(), - List.of( - new TypedAssignment( - new TypedIntLiteral( - 0, - Type.INT - ), - new TypedFieldVarAccess( - true, - null, - "x", - Type.INT - ), - Type.INT - ), - new TypedAssignment( - new TypedCharLiteral( - 'a', - Type.CHAR - ), - new TypedFieldVarAccess( - true, - null, - "c", - Type.CHAR - ), - Type.CHAR - ) - ), - Type.VOID - ) - ) - ), - List.of( - new TypedConstructor( - "Field", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - null, - null, - null, - Type.REFERENCE("Field") - ) - ), - null - ); + + private BytecodeTestUtil util; + + @BeforeEach + public void setUp() { + try { + util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Field.java"), "Field"); + } catch (Exception e) { + throw new RuntimeException(e); + } } + + @Test + public void testConstructorCount() { + Assertions.assertEquals(1, util.getConstructorCount()); + // default constructor + } + + @Test + public void testDefaultConstructor() { + Assertions.assertEquals("Field", util.getConstructorNames().get(0)); + Assertions.assertEquals(0, util.getConstructorParameterCount(0)); + try { + Assertions.assertEquals("Field", util.invokeConstructor(new Class[]{}, null).getClass().getName()); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Test + public void testMethodCount() { + Assertions.assertEquals(3, util.getMethodCount()); + } + + @Test + public void testMethodNames() { + Assertions.assertTrue(util.getMethodNames().contains("fieldAccess")); + Assertions.assertTrue(util.getMethodNames().contains("setX")); + Assertions.assertTrue(util.getMethodNames().contains("setC")); + } + + @Test + public void testMethodReturnType() { + try { + Assertions.assertEquals("void", util.getMethodReturnType("fieldAccess", new Class[]{})); + Assertions.assertEquals("void", util.getMethodReturnType("setX", new Class[]{int.class})); + Assertions.assertEquals("void", util.getMethodReturnType("setC", new Class[]{char.class})); + } catch (Exception e) { + Assertions.fail(); + } + } + +// @Test +// public void testFieldCount() { +// Assertions.assertEquals(2, util.getFieldCount()); +// } +// +// @Test +// public void testFieldCount() { +// Assertions.assertEquals(2, util.getFieldCount()); +// } } \ No newline at end of file From c823894eb2fb82a229eb9810d70466b818c5d585 Mon Sep 17 00:00:00 2001 From: simon Date: Thu, 27 Jun 2024 21:43:13 +0200 Subject: [PATCH 4/7] fix problem with new --- .../typedast/LoggingMethodVisitor.java | 80 +++++++++++++++++++ .../de/maishai/typedast/MethodContext.java | 7 +- .../typedast/typedclass/TypedAssignment.java | 2 +- .../maishai/typedast/typedclass/TypedNew.java | 1 + .../Features/ByteCode_ClassObjects.java | 70 ++++++++-------- 5 files changed, 121 insertions(+), 39 deletions(-) create mode 100644 src/main/java/de/maishai/typedast/LoggingMethodVisitor.java diff --git a/src/main/java/de/maishai/typedast/LoggingMethodVisitor.java b/src/main/java/de/maishai/typedast/LoggingMethodVisitor.java new file mode 100644 index 0000000..5e4b2c1 --- /dev/null +++ b/src/main/java/de/maishai/typedast/LoggingMethodVisitor.java @@ -0,0 +1,80 @@ +package de.maishai.typedast; + +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; + +public class LoggingMethodVisitor extends MethodVisitor { + + public LoggingMethodVisitor(MethodVisitor methodVisitor) { + super(Opcodes.ASM9, methodVisitor); + System.out.println("\n--- Visiting Method ---"); + } + + public static String opcodeToString(int opcode) { + for (java.lang.reflect.Field field : Opcodes.class.getDeclaredFields()) { + try { + if (field.getType() == int.class && field.getInt(null) == opcode) { + return field.getName(); + } + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } + return "UNKNOWN"; + } + + @Override + public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { + System.out.println("visitMethodInsn: " + opcodeToString(opcode) + ", " + owner + ", " + name + ", " + descriptor + ", " + isInterface); + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); + } + + @Override + public void visitVarInsn(int opcode, int varIndex) { + System.out.println("visitVarInsn: " + opcodeToString(opcode) + ", " + varIndex); + super.visitVarInsn(opcode, varIndex); + } + + @Override + public void visitTypeInsn(int opcode, String type) { + System.out.println("visitTypeInsn: " + opcodeToString(opcode) + ", " + type); + super.visitTypeInsn(opcode, type); + } + + @Override + public void visitInsn(int opcode) { + System.out.println("visitInsn: " + opcodeToString(opcode)); + super.visitInsn(opcode); + } + + @Override + public void visitMethodInsn(int opcode, String owner, String name, String descriptor) { + System.out.println("visitMethodInsn: " + opcodeToString(opcode) + ", " + owner + ", " + name + ", " + descriptor); + super.visitMethodInsn(opcode, owner, name, descriptor); + } + + @Override + public void visitFieldInsn(int opcode, String owner, String name, String descriptor) { + System.out.println("visitFieldInsn: " + opcodeToString(opcode) + ", " + owner + ", " + name + ", " + descriptor); + super.visitFieldInsn(opcode, owner, name, descriptor); + } + + @Override + public void visitMaxs(int maxStack, int maxLocals) { + System.out.println("visitMaxs: " + maxStack + ", " + maxLocals); + super.visitMaxs(maxStack, maxLocals); + } + + @Override + public void visitIntInsn(int opcode, int operand) { + String opcodeString = opcode == Opcodes.BIPUSH ? "BIPUSH" : "SIPUSH"; + System.out.println("visitIntInsn: " + opcodeString + ", " + operand); + super.visitIntInsn(opcode, operand); + } + + @Override + public void visitLdcInsn(Object value) { + System.out.println("visitLdcInsn: " + value); + super.visitLdcInsn(value); + } +} diff --git a/src/main/java/de/maishai/typedast/MethodContext.java b/src/main/java/de/maishai/typedast/MethodContext.java index 9e741e1..41b4e3b 100644 --- a/src/main/java/de/maishai/typedast/MethodContext.java +++ b/src/main/java/de/maishai/typedast/MethodContext.java @@ -28,7 +28,12 @@ public class MethodContext { private final Deque