Merge branch 'refs/heads/testsuites'

This commit is contained in:
simon 2024-06-28 18:53:40 +02:00
commit 576fceb89b
83 changed files with 4355 additions and 16 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

@ -0,0 +1,103 @@
package de.maishai.typedast;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.HashMap;
import java.util.Map;
public class LoggingMethodVisitor extends MethodVisitor {
private final Map<Label, String> labelNames = new HashMap<>();
private int labelCounter = 0;
public LoggingMethodVisitor(MethodVisitor methodVisitor) {
super(Opcodes.ASM9, methodVisitor);
System.out.println("\n--- Visiting Method ---");
}
private String getLabelName(Label label) {
return labelNames.computeIfAbsent(label, k -> "label" + labelCounter++);
}
@Override
public void visitJumpInsn(int opcode, Label label) {
System.out.println("visitJumpInsn: " + opcodeToString(opcode) + ", " + getLabelName(label));
super.visitJumpInsn(opcode, label);
}
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 visitLabel(Label label) {
String labelName = getLabelName(label);
System.out.println("visitLabel: " + labelName);
super.visitLabel(label);
}
@Override
public void visitLdcInsn(Object value) {
System.out.println("visitLdcInsn: " + value);
super.visitLdcInsn(value);
}
}

View File

@ -28,7 +28,12 @@ public class MethodContext {
private final Deque<Label> continueLabels = new ArrayDeque<>();
public MethodContext(ClassContext classContext, MethodVisitor mv, Type returnType) {
this.mv = mv;
// if DEBUG env variable is set, log all method calls
if (System.getenv("DEBUG") != null) {
this.mv = new LoggingMethodVisitor(mv);
} else {
this.mv = mv;
}
this.classContext = classContext;
this.returnType = returnType;
registerVariable("this", classContext.getType());

View File

@ -88,7 +88,7 @@ public class TypedAssignment implements TypedStatement {
@Override
public void codeGen(MethodContext ctx) {
if(value instanceof TypedNew || value instanceof TypedMethodCall) {
if(value instanceof TypedMethodCall) {
value.codeGen(ctx);
getOwnerChain(ctx);
} else {

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

@ -71,6 +71,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
}
String descriptor = CodeGenUtils.generateDescriptor(args.stream().map(TypedExpression::getType).toList(), Type.VOID);
ctx.getMv().visitMethodInsn(Opcodes.INVOKESPECIAL, type.getReference(), "<init>", descriptor, false);
ctx.popStack();
LOGGER.log(Level.FINEST, "INVOKESPECIAL {0} <init> {1}", new Object[]{type.getReference(), descriptor});
for (int i = 0; i < args.size(); i++) {
ctx.popStack();

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,147 @@
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 {
try {
return clazz.getField(fieldName).get(instance);
} catch (IllegalAccessException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
public void setFieldValue(String fieldName, Object value) throws Exception {
clazz.getField(fieldName).set(instance, value);
}
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;
}
}
}

View File

@ -0,0 +1,140 @@
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.assertTrue(util.getMethodNames().contains("breakMethod"));
}
@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<?>[]{boolean.class}));
Assertions.assertEquals("boolean", util.getMethodParameterTypes("breakMethod", new Class<?>[]{boolean.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("whileRepetition"));
Assertions.assertTrue(util.getFieldNames().contains("forRepetition"));
}
@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 {
Assertions.assertEquals(0, util.getFieldValue("whileRepetition"));
Assertions.assertEquals(0, util.getFieldValue("forRepetition"));
util.invokeMethod("breakMethod", new Class<?>[]{boolean.class}, new Object[]{false});
Assertions.assertEquals(10, util.getFieldValue("whileRepetition"));
Assertions.assertEquals(10, util.getFieldValue("forRepetition"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testInvokeBreakMethodWithTrue() {
try {
Assertions.assertEquals(0, util.getFieldValue("whileRepetition"));
Assertions.assertEquals(0, util.getFieldValue("forRepetition"));
util.invokeMethod("breakMethod", new Class<?>[]{boolean.class}, new Object[]{true});
Assertions.assertEquals(5, util.getFieldValue("whileRepetition"));
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,146 @@
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;
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
void testConstructorCount() {
Assertions.assertEquals(2, util.getConstructorCount());
}
@Test
void testConstructor1() {
Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
}
@Test
void testConstructor2() {
Assertions.assertEquals("ClassObjects", util.getConstructorNames().get(1));
Assertions.assertEquals(1, util.getConstructorParameterCount(1));
}
@Test
void testMethodCount() {
Assertions.assertEquals(1, util.getMethodCount());
}
@Test
void testMethodNames() {
Assertions.assertEquals("objectsMethod", util.getMethodNames().get(0));
}
@Test
void testMethodReturnType() {
try {
Assertions.assertEquals("void", util.getMethodReturnType("objectsMethod", new Class<?>[]{}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
void testMethodParameters() {
try {
Assertions.assertEquals(0, util.getMethodParameterCount("objectsMethod", new Class<?>[]{}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
void testFieldCount() {
Assertions.assertEquals(3, util.getFieldCount());
}
@Test
void testFieldNames() {
Assertions.assertEquals("object", util.getFieldNames().get(0));
Assertions.assertEquals("objectWithValue", util.getFieldNames().get(1));
Assertions.assertEquals("integerValue", util.getFieldNames().get(2));
}
@Test
void testFieldTypes() {
Assertions.assertEquals("ClassObjects", util.getFieldTypes().get(0));
Assertions.assertEquals("ClassObjects", util.getFieldTypes().get(1));
Assertions.assertEquals("int", util.getFieldTypes().get(2));
}
@Test
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
void testInvokeConstructor1() {
try {
Object constructor1ReturnValue = util.invokeConstructor(new Class<?>[]{}, new Object[]{});
Assertions.assertEquals("ClassObjects", constructor1ReturnValue.getClass().getName());
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
void testInvokeConstructor2() {
try {
Object constructor2ReturnValue = util.invokeConstructor(new Class<?>[]{int.class}, new Object[]{2});
Assertions.assertEquals("ClassObjects", constructor2ReturnValue.getClass().getName());
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
void testInvokeMethod() {
try {
Object fieldObject = util.getFieldValue("object");
Object fieldObjectWithValue = util.getFieldValue("objectWithValue");
Assertions.assertEquals(0, (int) util.getFieldValue("integerValue"));
util.invokeMethod("objectsMethod", null);
Assertions.assertNotSame(util.getFieldValue("object"), fieldObject);
Assertions.assertEquals("ClassObjects", util.getFieldValue("object").getClass().getName());
Assertions.assertNotSame(util.getFieldValue("objectWithValue"), fieldObjectWithValue);
Assertions.assertEquals("ClassObjects", util.getFieldValue("objectWithValue").getClass().getName());
Assertions.assertEquals(2, (int) util.getFieldValue("integerValue"));
} 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,113 @@
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 {
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));
}
@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();
}
}
}

View File

@ -0,0 +1,134 @@
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 {
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();
}
}
}

View File

@ -0,0 +1,175 @@
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 {
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();
}
}
}

View File

@ -0,0 +1,159 @@
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 {
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 testMethodParameters() {
try {
Assertions.assertEquals(0, util.getMethodParameterCount("fieldAccess", new Class<?>[]{}));
Assertions.assertEquals(1, util.getMethodParameterCount("setX", new Class<?>[]{int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("setX", new Class<?>[]{int.class}).get(0));
Assertions.assertEquals(1, util.getMethodParameterCount("setC", new Class<?>[]{char.class}));
Assertions.assertEquals("char", util.getMethodParameterTypes("setC", new Class<?>[]{char.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("x"));
Assertions.assertTrue(util.getFieldNames().contains("c"));
}
@Test
public void testFieldTypes() {
try {
Assertions.assertEquals("int", util.getFieldTypes().get(0));
Assertions.assertEquals("char", util.getFieldTypes().get(1));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testFieldValues() {
try {
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertEquals('\u0000', util.getFieldValue("c"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeDefaultConstructor() {
try {
Object instance = util.invokeConstructor(new Class<?>[]{}, null);
Assertions.assertEquals(instance.getClass().getName(), "Field");
Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "x"));
Assertions.assertEquals('\u0000', util.getFieldValueOfObject(instance, "c"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeFieldAccess() {
try {
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertEquals('\u0000', util.getFieldValue("c"));
util.invokeMethod("fieldAccess", new Class<?>[]{}, new Object[]{});
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertEquals('a', util.getFieldValue("c"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeSetXWith5() {
try {
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertEquals('\u0000', util.getFieldValue("c"));
util.invokeMethod("setX", new Class<?>[]{int.class}, new Object[]{5});
Assertions.assertEquals(5, util.getFieldValue("x"));
Assertions.assertEquals('\u0000', util.getFieldValue("c"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeSetCWithH() {
try {
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertEquals('\u0000', util.getFieldValue("c"));
util.invokeMethod("setC", new Class<?>[]{char.class}, new Object[]{'h'});
Assertions.assertEquals(0, util.getFieldValue("x"));
Assertions.assertEquals('h', util.getFieldValue("c"));
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -0,0 +1,122 @@
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_For {
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/For.java"), "For");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("For", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("For", 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.assertTrue(util.getMethodNames().contains("testFor"));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("void", util.getMethodReturnType("testFor", new Class<?>[]{}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(0, util.getMethodParameterCount("testFor"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testFieldCount() {
Assertions.assertEquals(2, util.getFieldCount());
}
@Test
public void testFieldNames() {
Assertions.assertTrue(util.getFieldNames().contains("repetitionsFirstFor"));
Assertions.assertTrue(util.getFieldNames().contains("repetitionsSecondFor"));
}
@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("repetitionsFirstFor"));
Assertions.assertEquals(0, util.getFieldValue("repetitionsSecondFor"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeDefaultConstructor() {
try {
Object instance = util.invokeConstructor(new Class<?>[]{}, null);
Assertions.assertEquals(instance.getClass().getName(), "For");
Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsFirstFor"));
Assertions.assertEquals(0, util.getFieldValueOfObject(instance, "repetitionsSecondFor"));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeMethod() {
try {
Assertions.assertEquals(0, util.getFieldValue("repetitionsFirstFor"));
Assertions.assertEquals(0, util.getFieldValue("repetitionsSecondFor"));
util.invokeMethod("testFor", new Class<?>[]{}, new Object[]{});
Assertions.assertEquals(45, util.getFieldValue("repetitionsFirstFor"));
Assertions.assertEquals(45, util.getFieldValue("repetitionsSecondFor"));
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -0,0 +1,134 @@
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_If {
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/If.java"), "If");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("If", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("If", 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.assertTrue(util.getMethodNames().contains("ifMethod"));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals("int",
util.getMethodReturnType("ifMethod", new Class<?>[]{boolean.class, boolean.class}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(2,
util.getMethodParameterCount("ifMethod", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals("boolean",
util.getMethodParameterTypes("ifMethod", new Class<?>[]{boolean.class, boolean.class}).get(0));
Assertions.assertEquals("boolean",
util.getMethodParameterTypes("ifMethod", new Class<?>[]{boolean.class, boolean.class}).get(1));
} 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(), "If");
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeIfMethodWithTrueTrue() {
try {
int returnValue = (int) util.invokeMethod("ifMethod",
new Class<?>[]{boolean.class, boolean.class}, true, true);
Assertions.assertEquals(1, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeIfMethodWithTrueFalse() {
try {
int returnValue = (int) util.invokeMethod("ifMethod",
new Class<?>[]{boolean.class, boolean.class}, true, false);
Assertions.assertEquals(1, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeIfMethodWithFalseTrue() {
try {
int returnValue = (int) util.invokeMethod("ifMethod",
new Class<?>[]{boolean.class, boolean.class}, false, true);
Assertions.assertEquals(2, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeIfMethodWithFalseFalse() {
try {
int returnValue = (int) util.invokeMethod("ifMethod",
new Class<?>[]{boolean.class, boolean.class}, false, false);
Assertions.assertEquals(3, returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
}

View File

@ -0,0 +1,416 @@
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_LogicExpr {
private BytecodeTestUtil util;
@BeforeEach
public void setUp() {
try {
util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/LogicExpr.java"), "LogicExpr");
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testConstructorCount() {
Assertions.assertEquals(1, util.getConstructorCount());
// default constructor
}
@Test
public void testDefaultConstructor() {
Assertions.assertEquals("LogicExpr", util.getConstructorNames().get(0));
Assertions.assertEquals(0, util.getConstructorParameterCount(0));
try {
Assertions.assertEquals("LogicExpr", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Test
public void testMethodCount() {
Assertions.assertEquals(8, util.getMethodCount());
}
@Test
public void testMethodNames() {
Assertions.assertTrue(util.getMethodNames().contains("testSmaller"));
Assertions.assertTrue(util.getMethodNames().contains("testLarger"));
Assertions.assertTrue(util.getMethodNames().contains("testEqual"));
Assertions.assertTrue(util.getMethodNames().contains("testNotEqual"));
Assertions.assertTrue(util.getMethodNames().contains("testSmallerEqual"));
Assertions.assertTrue(util.getMethodNames().contains("testLargerEqual"));
Assertions.assertTrue(util.getMethodNames().contains("testAND"));
Assertions.assertTrue(util.getMethodNames().contains("testOR"));
}
@Test
public void testMethodReturnType() {
try {
Assertions.assertEquals(1, util.getMethodParameterCount("testSmaller", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testSmaller", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testLarger", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testLarger", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testNotEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testNotEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testSmallerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testSmallerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testLargerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testLargerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testAND", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testAND", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals(1, util.getMethodParameterCount("testOR", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals("boolean", util.getMethodReturnType("testOR", new Class<?>[]{boolean.class, boolean.class}));
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testMethodParameters() {
try {
Assertions.assertEquals(2, util.getMethodParameterCount("testSmaller", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("testSmaller", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("testSmaller", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals(2, util.getMethodParameterCount("testLarger", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("testLarger", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("testLarger", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals(2, util.getMethodParameterCount("testEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("testEqual", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("testEqual", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals(2, util.getMethodParameterCount("testNotEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("testNotEqual", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("testNotEqual", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals(2, util.getMethodParameterCount("testSmallerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("testSmallerEqual", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("testSmallerEqual", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals(2, util.getMethodParameterCount("testLargerEqual", new Class<?>[]{int.class, int.class}));
Assertions.assertEquals("int", util.getMethodParameterTypes("testLargerEqual", new Class<?>[]{int.class, int.class}).get(0));
Assertions.assertEquals("int", util.getMethodParameterTypes("testLargerEqual", new Class<?>[]{int.class, int.class}).get(1));
Assertions.assertEquals(2, util.getMethodParameterCount("testAND", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals("boolean", util.getMethodParameterTypes("testAND", new Class<?>[]{boolean.class, boolean.class}).get(0));
Assertions.assertEquals("boolean", util.getMethodParameterTypes("testAND", new Class<?>[]{boolean.class, boolean.class}).get(1));
Assertions.assertEquals(2, util.getMethodParameterCount("testOR", new Class<?>[]{boolean.class, boolean.class}));
Assertions.assertEquals("boolean", util.getMethodParameterTypes("testOR", new Class<?>[]{boolean.class, boolean.class}).get(0));
Assertions.assertEquals("boolean", util.getMethodParameterTypes("testOR", new Class<?>[]{boolean.class, boolean.class}).get(1));
} 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(), "LogicExpr");
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeSmallerWith4And5() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testSmaller", new Class<?>[]{int.class, int.class}, new Object[]{4, 5});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeSmallerWith5And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testSmaller", new Class<?>[]{int.class, int.class}, new Object[]{5, 4});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeSmallerWith4And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testSmaller", new Class<?>[]{int.class, int.class}, new Object[]{4, 4});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeLargerWith4And5() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testLarger", new Class<?>[]{int.class, int.class}, new Object[]{4, 5});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeLargerWith5And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testLarger", new Class<?>[]{int.class, int.class}, new Object[]{5, 4});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeLargerWith4And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testLarger", new Class<?>[]{int.class, int.class}, new Object[]{4, 4});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeEqualWith4And5() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testEqual", new Class<?>[]{int.class, int.class}, new Object[]{4, 5});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeEqualWith5And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testEqual", new Class<?>[]{int.class, int.class}, new Object[]{5, 4});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeEqualWith4And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testEqual", new Class<?>[]{int.class, int.class}, new Object[]{4, 4});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeNotEqualWith4And5() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testNotEqual", new Class<?>[]{int.class, int.class}, new Object[]{4, 5});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeNotEqualWith5And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testNotEqual", new Class<?>[]{int.class, int.class}, new Object[]{5, 4});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeNotEqualWith4And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testNotEqual", new Class<?>[]{int.class, int.class}, new Object[]{4, 4});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeSmallerEqualWith4And5() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testSmallerEqual", new Class<?>[]{int.class, int.class}, new Object[]{4, 5});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeSmallerEqualWith5And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testSmallerEqual", new Class<?>[]{int.class, int.class}, new Object[]{5, 4});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeSmallerEqualWith4And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testSmallerEqual", new Class<?>[]{int.class, int.class}, new Object[]{4, 4});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeLargerEqualWith4And5() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testLargerEqual", new Class<?>[]{int.class, int.class}, new Object[]{4, 5});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeLargerEqualWith5And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testLargerEqual", new Class<?>[]{int.class, int.class}, new Object[]{5, 4});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeLargerEqualWith4And4() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testLargerEqual", new Class<?>[]{int.class, int.class}, new Object[]{4, 4});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeANDWithTrueAndTrue() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testAND", new Class<?>[]{boolean.class, boolean.class}, new Object[]{true, true});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeANDWithTrueAndFalse() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testAND", new Class<?>[]{boolean.class, boolean.class}, new Object[]{true, false});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeANDWithFalseAndTrue() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testAND", new Class<?>[]{boolean.class, boolean.class}, new Object[]{false, true});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeANDWithFalseAndFalse() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testAND", new Class<?>[]{boolean.class, boolean.class}, new Object[]{false, false});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeORWithTrueAndTrue() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testOR", new Class<?>[]{boolean.class, boolean.class}, new Object[]{true, true});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeORWithTrueAndFalse() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testOR", new Class<?>[]{boolean.class, boolean.class}, new Object[]{true, false});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeORWithFalseAndTrue() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testOR", new Class<?>[]{boolean.class, boolean.class}, new Object[]{false, true});
Assertions.assertTrue(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
@Test
public void testInvokeORWithFalseAndFalse() {
try {
boolean returnValue = (boolean) util.invokeMethod(
"testOR", new Class<?>[]{boolean.class, boolean.class}, new Object[]{false, false});
Assertions.assertFalse(returnValue);
} catch (Exception e) {
Assertions.fail();
}
}
}

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,15 @@
public class Constructor {
int i;
public Constructor() {
this.i = 1;
}
public Constructor(int x) {
this.i= x;
}
public Constructor(int x, int y) {
this.i= x + y;
}
}

View File

@ -0,0 +1,14 @@
public class Continue {
int repetitions;
public void continueLoop(boolean shouldContinue) {
this.repetitions = 0;
for (int i = 0; i < 5; i+=1) {
// the second condition has to be in brackets
if (shouldContinue && i == 3) {
continue;
}
// repetition is a field variable, it should be retrieved with this
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,16 @@
public class For {
int repetitionsFirstFor;
int repetitionsSecondFor;
public void testFor() {
this.repetitionsFirstFor = 0;
for (int i = 0; i < 10; i = i+1) {
this.repetitionsFirstFor = this.repetitionsFirstFor + i;
}
this.repetitionsSecondFor = 0;
int j;
for (j = 0; j < 10; j += 1) {
this.repetitionsSecondFor = this.repetitionsSecondFor + 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);
}
}