added some e2e tests

This commit is contained in:
JonathanFleischmann 2024-06-28 21:31:01 +02:00
parent 88061275b8
commit 133e7824ba
5 changed files with 379 additions and 741 deletions

View File

@ -8,11 +8,14 @@ import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class BytecodeTestUtil { public class BytecodeTestUtil {
private Class<?> clazz; private final Class<?> clazz;
private Object instance; private final Object instance;
private static final Logger LOGGER = Logger.getLogger(BytecodeTestUtil.class.getName());
public BytecodeTestUtil(List<String> sourceFilePaths, String className) throws Exception { public BytecodeTestUtil(List<String> sourceFilePaths, String className) throws Exception {
byte[] resultBytecode = Compiler.generateByteCodeArrayFromFiles(sourceFilePaths).get(0); byte[] resultBytecode = Compiler.generateByteCodeArrayFromFiles(sourceFilePaths).get(0);
@ -23,8 +26,14 @@ public class BytecodeTestUtil {
return defineClass(name, resultBytecode, 0, resultBytecode.length); return defineClass(name, resultBytecode, 0, resultBytecode.length);
} }
}; };
clazz = classLoader.loadClass(className);
this.instance = clazz.getDeclaredConstructor().newInstance(); try {
clazz = classLoader.loadClass(className);
this.instance = clazz.getDeclaredConstructor().newInstance();
} catch (ClassNotFoundException | SecurityException | NoSuchMethodException e) {
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e);
}
} }
@ -76,15 +85,25 @@ public class BytecodeTestUtil {
} }
public int getMethodParameterCount(String methodName, Class<?>... params) throws Exception { public int getMethodParameterCount(String methodName, Class<?>... params) throws Exception {
return clazz.getMethod(methodName, params).getParameterCount(); try {
return clazz.getMethod(methodName, params).getParameterCount();
} catch (NoSuchMethodException | SecurityException e) {
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e);
}
} }
public List<String> getMethodParameterTypes(String methodName, Class<?>... params) throws Exception { public List<String> getMethodParameterTypes(String methodName, Class<?>... params) {
List<String> parameterTypes = new ArrayList<>(); try {
for (Class<?> parameterType : clazz.getMethod(methodName, params).getParameterTypes()) { List<String> parameterTypes = new ArrayList<>();
parameterTypes.add(parameterType.getSimpleName()); for (Class<?> parameterType : clazz.getMethod(methodName, params).getParameterTypes()) {
parameterTypes.add(parameterType.getSimpleName());
}
return parameterTypes;
} catch (NoSuchMethodException | SecurityException e) {
LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e);
} }
return parameterTypes;
} }
public Object invokeMethod(String methodName, Class<?>[] parameterTypes, Object... args) throws Exception { public Object invokeMethod(String methodName, Class<?>[] parameterTypes, Object... args) throws Exception {
@ -92,7 +111,7 @@ public class BytecodeTestUtil {
Method method = clazz.getMethod(methodName, parameterTypes); Method method = clazz.getMethod(methodName, parameterTypes);
return method.invoke(instance, args); return method.invoke(instance, args);
} catch (InvocationTargetException e) { } catch (InvocationTargetException e) {
e.getTargetException().printStackTrace(); LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e.getTargetException()); throw new RuntimeException(e.getTargetException());
} }
} }
@ -123,7 +142,7 @@ public class BytecodeTestUtil {
try { try {
return clazz.getField(fieldName).get(instance); return clazz.getField(fieldName).get(instance);
} catch (IllegalAccessException e) { } catch (IllegalAccessException e) {
e.printStackTrace(); LOGGER.log(Level.SEVERE, "Exception occur", e);
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }

View File

@ -3,212 +3,161 @@ package testResources.CodeGen.Features;
import de.maishai.ast.Operator; import de.maishai.ast.Operator;
import de.maishai.typedast.Type; import de.maishai.typedast.Type;
import de.maishai.typedast.typedclass.*; 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; import java.util.List;
public class ByteCode_MethodCall { public class ByteCode_MethodCall {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"MethodCall", public void setUp() {
List.of(), try {
List.of( util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/MethodCall.java"), "MethodCall");
new TypedMethod( } catch (Exception e) {
"methodCall", throw new RuntimeException(e);
Type.INT, }
List.of(), }
List.of(),
new TypedBlock( @Test
List.of(), public void testConstructorCount() {
List.of( Assertions.assertEquals(1, util.getConstructorCount());
new TypedReturn( // default constructor
new TypedMethodCall( }
new TypedFieldVarAccess(
false, @Test
null, public void testDefaultConstructor() {
"method", Assertions.assertEquals("MethodCall", util.getConstructorNames().get(0));
Type.INT Assertions.assertEquals(0, util.getConstructorParameterCount(0));
), try {
List.of(), Assertions.assertEquals("MethodCall", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
Type.INT } catch (Exception e) {
), Assertions.fail();
Type.INT }
) }
),
Type.INT @Test
) public void testMethodCount() {
), Assertions.assertEquals(6, util.getMethodCount());
new TypedMethod( }
"methodCall1",
Type.INT, @Test
List.of(), public void testMethodNames() {
List.of(), Assertions.assertTrue(util.getMethodNames().contains("methodCall"));
new TypedBlock( Assertions.assertTrue(util.getMethodNames().contains("methodCall1"));
List.of(), Assertions.assertTrue(util.getMethodNames().contains("methodCall2"));
List.of( Assertions.assertTrue(util.getMethodNames().contains("method"));
new TypedReturn( Assertions.assertTrue(util.getMethodNames().contains("method1"));
new TypedMethodCall( Assertions.assertTrue(util.getMethodNames().contains("method2"));
new TypedFieldVarAccess( }
false,
null, @Test
"method1", public void testMethodReturnType() {
Type.INT try {
), Assertions.assertEquals("int", util.getMethodReturnType("methodCall", new Class<?>[]{}));
List.of( Assertions.assertEquals("int", util.getMethodReturnType("methodCall1", new Class<?>[]{}));
new TypedIntLiteral( Assertions.assertEquals("int", util.getMethodReturnType("methodCall2", new Class<?>[]{}));
1, Assertions.assertEquals("int", util.getMethodReturnType("method", new Class<?>[]{}));
Type.INT Assertions.assertEquals("int", util.getMethodReturnType("method1", new Class<?>[]{int.class}));
) Assertions.assertEquals("int", util.getMethodReturnType("method2", new Class<?>[]{int.class, int.class}));
), } catch (Exception e) {
Type.INT Assertions.fail();
), }
Type.INT }
)
), @Test
Type.INT public void testMethodParameters() {
) try {
), Assertions.assertEquals(0, util.getMethodParameterCount("methodCall", new Class<?>[]{}));
new TypedMethod( Assertions.assertEquals(0, util.getMethodParameterCount("methodCall1", new Class<?>[]{}));
"methodCall2", Assertions.assertEquals(0, util.getMethodParameterCount("methodCall2", new Class<?>[]{}));
Type.INT, Assertions.assertEquals(0, util.getMethodParameterCount("method", new Class<?>[]{}));
List.of(), Assertions.assertEquals(1, util.getMethodParameterCount("method1", new Class<?>[]{int.class}));
List.of(), Assertions.assertEquals(2, util.getMethodParameterCount("method2", new Class<?>[]{int.class, int.class}));
new TypedBlock( Assertions.assertEquals("int", util.getMethodParameterTypes("method1", new Class<?>[]{int.class}).get(0));
List.of(), Assertions.assertEquals("int", util.getMethodParameterTypes("method2", new Class<?>[]{int.class, int.class}).get(0));
List.of( Assertions.assertEquals("int", util.getMethodParameterTypes("method2", new Class<?>[]{int.class, int.class}).get(1));
new TypedReturn( } catch (Exception e) {
new TypedMethodCall( Assertions.fail();
new TypedFieldVarAccess( }
false, }
null,
"method2", @Test
Type.INT public void testFieldCount() {
), Assertions.assertEquals(0, util.getFieldCount());
List.of( }
new TypedIntLiteral(
1, @Test
Type.INT public void testInvokeDefaultConstructor() {
), try {
new TypedIntLiteral( Object instance = util.invokeConstructor(new Class<?>[]{}, null);
2, Assertions.assertEquals(instance.getClass().getName(), "MethodCall");
Type.INT } catch (Exception e) {
) Assertions.fail();
), }
Type.INT }
),
Type.INT @Test
) public void testInvokeMethod() {
), try {
Type.INT Object returnValue = util.invokeMethod("method", new Class<?>[]{}, null);
) Assertions.assertEquals(0, returnValue);
), } catch (Exception e) {
new TypedMethod( Assertions.fail();
"method", }
Type.INT, }
List.of(),
List.of(), @Test
new TypedBlock( public void testInvokeMethod1() {
List.of(), try {
List.of( Object returnValue = util.invokeMethod("method1", new Class<?>[]{int.class}, new Object[]{3});
new TypedReturn( Assertions.assertEquals(3, returnValue);
new TypedIntLiteral( } catch (Exception e) {
0, Assertions.fail();
Type.INT }
), }
Type.INT
) @Test
), public void testInvokeMethod2() {
Type.INT try {
) Object returnValue = util.invokeMethod("method2", new Class<?>[]{int.class, int.class}, new Object[]{3, 4});
), Assertions.assertEquals(7, returnValue);
new TypedMethod( } catch (Exception e) {
"method1", Assertions.fail();
Type.INT, }
List.of( }
new TypedParameter(
"x", @Test
Type.INT public void testInvokeMethodCall() {
) try {
), Object returnValue = util.invokeMethod("methodCall", new Class<?>[]{}, null);
List.of(), Assertions.assertEquals(0, returnValue);
new TypedBlock( } catch (Exception e) {
List.of(), Assertions.fail();
List.of( }
new TypedReturn( }
new TypedFieldVarAccess(
false, @Test
null, public void testInvokeMethodCall1() {
"x", try {
Type.INT Object returnValue = util.invokeMethod("methodCall1", new Class<?>[]{}, null);
), Assertions.assertEquals(1, returnValue);
Type.INT } catch (Exception e) {
) Assertions.fail();
), }
Type.INT }
)
), @Test
new TypedMethod( public void testInvokeMethodCall2() {
"method2", try {
Type.INT, Object returnValue = util.invokeMethod("methodCall2", new Class<?>[]{}, null);
List.of( Assertions.assertEquals(3, returnValue);
new TypedParameter( } catch (Exception e) {
"x", Assertions.fail();
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

@ -6,93 +6,5 @@ import de.maishai.typedast.typedclass.*;
import java.util.List; import java.util.List;
public class ByteCode_MultipleClasses { public class ByteCode_MultipleClasses {
public static TypedProgram get() { // TODO: implement with multiple Files
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

@ -1,446 +1,200 @@
package testResources.CodeGen.Features; package testResources.CodeGen.Features;
import de.maishai.ast.Operator; import org.junit.jupiter.api.Assertions;
import de.maishai.typedast.Type; import org.junit.jupiter.api.BeforeEach;
import de.maishai.typedast.typedclass.*; import org.junit.jupiter.api.Test;
import testResources.CodeGen.BytecodeTestUtil;
import java.util.List; import java.util.List;
public class ByteCode_Operators { public class ByteCode_Operators {
public static TypedProgram get() {
return new TypedProgram( private BytecodeTestUtil util;
List.of(
new TypedClass( @BeforeEach
"Operators", public void setUp() {
List.of(), try {
List.of( util = new BytecodeTestUtil(List.of("src/test/testFiles/CodeGenFeatures/Operators.java"), "Operators");
new TypedMethod( } catch (Exception e) {
"add", throw new RuntimeException(e);
Type.INT, }
List.of( }
new TypedParameter(
"a", @Test
Type.INT public void testConstructorCount() {
), Assertions.assertEquals(1, util.getConstructorCount());
new TypedParameter( // default constructor
"b", }
Type.INT
) @Test
), public void testDefaultConstructor() {
List.of(), Assertions.assertEquals("Operators", util.getConstructorNames().get(0));
new TypedBlock( Assertions.assertEquals(0, util.getConstructorParameterCount(0));
List.of(), try {
List.of( Assertions.assertEquals("Operators", util.invokeConstructor(new Class<?>[]{}, null).getClass().getName());
new TypedReturn( } catch (Exception e) {
new TypedBinary( Assertions.fail();
new TypedFieldVarAccess( }
false, }
null,
"a", @Test
Type.INT public void testMethodCount() {
), Assertions.assertEquals(8, util.getMethodCount());
Operator.ADD, }
new TypedFieldVarAccess(
false, @Test
null, public void testMethodNames() {
"b", Assertions.assertTrue(util.getMethodNames().contains("add"));
Type.INT Assertions.assertTrue(util.getMethodNames().contains("sub"));
), Assertions.assertTrue(util.getMethodNames().contains("mul"));
Type.INT Assertions.assertTrue(util.getMethodNames().contains("div"));
), Assertions.assertTrue(util.getMethodNames().contains("mod"));
Type.INT Assertions.assertTrue(util.getMethodNames().contains("dotBeforeLine"));
) Assertions.assertTrue(util.getMethodNames().contains("brackets"));
), Assertions.assertTrue(util.getMethodNames().contains("callOperatedMethods"));
Type.INT }
)
), @Test
new TypedMethod( public void testMethodReturnType() {
"sub", try {
Type.INT, Assertions.assertEquals("int", util.getMethodReturnType("add", new Class<?>[]{int.class, int.class}));
List.of( Assertions.assertEquals("int", util.getMethodReturnType("sub", new Class<?>[]{int.class, int.class}));
new TypedParameter( Assertions.assertEquals("int", util.getMethodReturnType("mul", new Class<?>[]{int.class, int.class}));
"a", Assertions.assertEquals("int", util.getMethodReturnType("div", new Class<?>[]{int.class, int.class}));
Type.INT Assertions.assertEquals("int", util.getMethodReturnType("mod", new Class<?>[]{int.class, int.class}));
), Assertions.assertEquals("int", util.getMethodReturnType("dotBeforeLine", new Class<?>[]{int.class, int.class}));
new TypedParameter( Assertions.assertEquals("int", util.getMethodReturnType("brackets", new Class<?>[]{int.class, int.class}));
"b", Assertions.assertEquals("int", util.getMethodReturnType("callOperatedMethods", new Class<?>[]{int.class, int.class}));
Type.INT } catch (Exception e) {
) Assertions.fail();
), }
List.of(), }
new TypedBlock(
List.of(), @Test
List.of( public void testMethodParameters() {
new TypedReturn( try {
new TypedBinary( Assertions.assertEquals(2, util.getMethodParameterCount("add", new Class<?>[]{int.class, int.class}));
new TypedFieldVarAccess( Assertions.assertEquals(2, util.getMethodParameterCount("sub", new Class<?>[]{int.class, int.class}));
false, Assertions.assertEquals(2, util.getMethodParameterCount("mul", new Class<?>[]{int.class, int.class}));
null, Assertions.assertEquals(2, util.getMethodParameterCount("div", new Class<?>[]{int.class, int.class}));
"a", Assertions.assertEquals(2, util.getMethodParameterCount("mod", new Class<?>[]{int.class, int.class}));
Type.INT Assertions.assertEquals(2, util.getMethodParameterCount("dotBeforeLine", new Class<?>[]{int.class, int.class}));
), Assertions.assertEquals(2, util.getMethodParameterCount("brackets", new Class<?>[]{int.class, int.class}));
Operator.SUB, Assertions.assertEquals(2, util.getMethodParameterCount("callOperatedMethods", new Class<?>[]{int.class, int.class}));
new TypedFieldVarAccess( Assertions.assertEquals("int", util.getMethodParameterTypes("add", new Class<?>[]{int.class, int.class}).get(0));
false, Assertions.assertEquals("int", util.getMethodParameterTypes("add", new Class<?>[]{int.class, int.class}).get(1));
null, Assertions.assertEquals("int", util.getMethodParameterTypes("sub", new Class<?>[]{int.class, int.class}).get(0));
"b", Assertions.assertEquals("int", util.getMethodParameterTypes("sub", new Class<?>[]{int.class, int.class}).get(1));
Type.INT Assertions.assertEquals("int", util.getMethodParameterTypes("mul", new Class<?>[]{int.class, int.class}).get(0));
), Assertions.assertEquals("int", util.getMethodParameterTypes("mul", new Class<?>[]{int.class, int.class}).get(1));
Type.INT Assertions.assertEquals("int", util.getMethodParameterTypes("div", new Class<?>[]{int.class, int.class}).get(0));
), Assertions.assertEquals("int", util.getMethodParameterTypes("div", new Class<?>[]{int.class, int.class}).get(1));
Type.INT Assertions.assertEquals("int", util.getMethodParameterTypes("mod", new Class<?>[]{int.class, int.class}).get(0));
) Assertions.assertEquals("int", util.getMethodParameterTypes("mod", new Class<?>[]{int.class, int.class}).get(1));
), Assertions.assertEquals("int", util.getMethodParameterTypes("dotBeforeLine", new Class<?>[]{int.class, int.class}).get(0));
Type.INT Assertions.assertEquals("int", util.getMethodParameterTypes("dotBeforeLine", new Class<?>[]{int.class, int.class}).get(1));
) Assertions.assertEquals("int", util.getMethodParameterTypes("brackets", new Class<?>[]{int.class, int.class}).get(0));
), Assertions.assertEquals("int", util.getMethodParameterTypes("brackets", new Class<?>[]{int.class, int.class}).get(1));
new TypedMethod( Assertions.assertEquals("int", util.getMethodParameterTypes("callOperatedMethods", new Class<?>[]{int.class, int.class}).get(0));
"mul", Assertions.assertEquals("int", util.getMethodParameterTypes("callOperatedMethods", new Class<?>[]{int.class, int.class}).get(1));
Type.INT, } catch (Exception e) {
List.of( Assertions.fail();
new TypedParameter( }
"a", }
Type.INT
), @Test
new TypedParameter( public void testFieldCount() {
"b", Assertions.assertEquals(0, util.getFieldCount());
Type.INT }
)
), @Test
List.of(), public void testInvokeDefaultConstructor() {
new TypedBlock( try {
List.of(), Object instance = util.invokeConstructor(new Class<?>[]{}, null);
List.of( Assertions.assertEquals(instance.getClass().getName(), "Operators");
new TypedReturn( } catch (Exception e) {
new TypedBinary( Assertions.fail();
new TypedFieldVarAccess( }
false, }
null,
"a", @Test
Type.INT public void testAdd() {
), try {
Operator.MUL, Object result = util.invokeMethod("add", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
new TypedFieldVarAccess( Assertions.assertEquals(8, result);
false, } catch (Exception e) {
null, Assertions.fail();
"b", }
Type.INT }
),
Type.INT @Test
), public void testSub() {
Type.INT try {
) Object result = util.invokeMethod("sub", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
), Assertions.assertEquals(2, result);
Type.INT } catch (Exception e) {
) Assertions.fail();
), }
new TypedMethod( }
"div",
Type.INT, @Test
List.of( public void testMul() {
new TypedParameter( try {
"a", Object result = util.invokeMethod("mul", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Type.INT Assertions.assertEquals(15, result);
), } catch (Exception e) {
new TypedParameter( Assertions.fail();
"b", }
Type.INT }
)
), @Test
List.of(), public void testDiv() {
new TypedBlock( try {
List.of(), Object result = util.invokeMethod("div", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
List.of( Assertions.assertEquals(1, result);
new TypedReturn( } catch (Exception e) {
new TypedBinary( Assertions.fail();
new TypedFieldVarAccess( }
false, }
null,
"a", @Test
Type.INT public void testMod() {
), try {
Operator.DIV, Object result = util.invokeMethod("mod", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
new TypedFieldVarAccess( Assertions.assertEquals(2, result);
false, } catch (Exception e) {
null, Assertions.fail();
"b", }
Type.INT }
),
Type.INT @Test
), public void testDotBeforeLine() {
Type.INT try {
) Object result = util.invokeMethod("dotBeforeLine", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
), Assertions.assertEquals(20, result);
Type.INT } catch (Exception e) {
) Assertions.fail();
), }
new TypedMethod( }
"mod",
Type.INT, @Test
List.of( public void testBrackets() {
new TypedParameter( try {
"a", Object result = util.invokeMethod("brackets", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
Type.INT Assertions.assertEquals(40, result);
), } catch (Exception e) {
new TypedParameter( Assertions.fail();
"b", }
Type.INT }
)
), @Test
List.of(), public void testCallOperatedMethods() {
new TypedBlock( try {
List.of(), Object result = util.invokeMethod("callOperatedMethods", new Class<?>[]{int.class, int.class}, new Object[]{5, 3});
List.of( Assertions.assertEquals(28, result);
new TypedReturn( } catch (Exception e) {
new TypedBinary( Assertions.fail();
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

@ -19,6 +19,10 @@ public class Operators {
return a % b; return a % b;
} }
public int dotBeforeLine(int a, int b) {
return a + b * a - b / a;
}
public int brackets(int a, int b) { public int brackets(int a, int b) {
return (a + b) * a; return (a + b) * a;
} }