added some e2e tests for CodeGenFiles with reflections

This commit is contained in:
JonathanFleischmann 2024-06-27 19:10:43 +02:00
parent a9bebece45
commit 78a34b915e
79 changed files with 4439 additions and 14 deletions

BIN
output/Operators.class Normal file

Binary file not shown.

View File

@ -75,6 +75,12 @@
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<!-- JUnit 5 end -->
<dependency>
<groupId>commons-cli</groupId>

View File

@ -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;
}
}

View File

@ -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());
// }
}

View File

@ -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?
}

View File

@ -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<String> 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<String> getConstructorNames() {
List<String> 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<String> getConstructorParameterTypes(int index) {
List<String> 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<String> getMethodNames() {
List<String> 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<String> getMethodParameterTypes(String methodName, Class<?>... params) throws Exception {
List<String> 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<String> getFieldNames() {
List<String> fieldNames = new ArrayList<>();
for (Field field : clazz.getDeclaredFields()) {
fieldNames.add(field.getName());
}
return fieldNames;
}
public List<String> getFieldTypes() {
List<String> 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;
}
}
}

View File

@ -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);
}
}
}

View File

@ -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();
}
}
}

View File

@ -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);
}
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}

View File

@ -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();
}
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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
);
}
}

View File

@ -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(

View File

@ -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;
}
}
}
}

View File

@ -0,0 +1,2 @@
public class Class {
}

View File

@ -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;
}
}

View File

@ -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 /
*/
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,11 @@
public class Main {
int i;
public Main() {
this.i = 0;
}
public static void main(String[] args) {
this.i = 10;
}
}

View File

@ -0,0 +1,11 @@
public class Method {
int x;
public Method() {
this.x = 0;
}
public void method() {
this.x = 10;
}
}

View File

@ -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;
}
}

View File

@ -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();
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}

View File

@ -0,0 +1,9 @@
public class Print {
public void printIt(char c) {
print(c);
}
public void printMoreComplex(int x, int y) {
print(x < y);
}
}

View File

@ -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();
}
}

View File

@ -0,0 +1,10 @@
public class Unary {
public int testMinus(int a) {
return -a;
}
public boolean testNot(boolean a) {
return !a;
}
}

View File

@ -0,0 +1,13 @@
public class VariableDefWithDecl {
public int testDefWithDeclInOne() {
int a = 10;
return a;
}
public int testDefWithDeclSeparate() {
int a;
a = 10;
return a;
}
}

View File

@ -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);
}
}