Merge remote-tracking branch 'origin/master'

This commit is contained in:
David Mueller 2024-05-08 13:54:52 +02:00
commit dfa5588514
134 changed files with 5646 additions and 81 deletions

BIN
Lib/asm-9.7.jar Normal file

Binary file not shown.

1
Readme.MD Normal file
View File

@ -0,0 +1 @@
Please move your stuff from "Source" to "src".

View File

@ -12,4 +12,4 @@ public abstract class AbstractType {
public TypeCheckResult getTypeCheckResult() {
return typeCheckResult;
}
}
}

View File

@ -1,5 +1,6 @@
package TypeCheck;
import java.util.List;
import java.util.Objects;
public class TypeCheckHelper {
@ -20,4 +21,12 @@ public class TypeCheckHelper {
}
return result;
}
public static boolean typeExists(String type, List<String> typeslist) {
if(type.equals("int") || type.equals("bool") || type.equals("char")){
return true;
}
return typeslist.contains(type);
}
}

View File

@ -1,14 +1,23 @@
package abstractSyntaxTree.Class;
import TypeCheck.AbstractType;
import TypeCheck.TypeCheckHelper;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Program;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FieldDecl implements IClass {
private String type;
private String identifier;
public class FieldDecl extends AbstractType implements IClass{
private HashMap<String, HashMap<String, String>> typeContext; // form class from program
public String type; // from parser
public String identifier; // from parser
public FieldDecl(HashMap<String, HashMap<String, String>> typeContext){
this.typeContext = typeContext;
}
public TypeCheckResult typeCheck(List<FieldDecl> classFieldsIdentifier) throws Exception {
TypeCheckResult result = new TypeCheckResult();
if (classFieldsIdentifier.contains(this.identifier)){
@ -16,8 +25,11 @@ public class FieldDecl implements IClass {
} else {
classFieldsIdentifier.add(this);
}
result.type = this.type;
//TypeCheckHelper.typeExists(type, ) // need all types of classes
setTypeCheckResult(result);
return result;
//write field table
}
}

View File

@ -1,11 +1,29 @@
package abstractSyntaxTree.Class;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Program;
import java.util.HashMap;
import java.util.List;
public class MethodDecl implements IClass {
// name
private HashMap<String, HashMap<String, HashMap<String, String>>> methodContext;
private HashMap<String, HashMap<String, String>> typeContext;
//TODO: Move this into the typeCheck
private HashMap<String, String> localVars; // (type, identifier) // add content here
public MethodDecl(HashMap<String, HashMap<String, HashMap<String, String>>> methodContext, HashMap<String, HashMap<String, String>> typeContext){
this.methodContext = methodContext;
this.typeContext = typeContext;
}
public TypeCheckResult typeCheck(List<MethodDecl> fieldsOrMethods) throws Exception {
// write localvars
// jede methode als block statement aufrufen
return null;
}

View File

@ -17,7 +17,7 @@ public class BoolDatatype extends AbstractType implements IDatatype{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
if(value) {
mv.visitInsn(Opcodes.ICONST_1); // Pushes the int 1 on the stack (true)
} else {

View File

@ -17,7 +17,7 @@ public class CharDatatype extends AbstractType implements IDatatype{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
// Possible use of BIPUSH and SIPUSH if the value is small enough
//This saves space in the bytecode which is not very relevant at this point, but could be implemented anyway

View File

@ -9,7 +9,7 @@ public interface IDatatype {
// visit method for code generation
void CodeGen(MethodVisitor mv) throws Exception;
void codeGen(MethodVisitor mv) throws Exception;
}
//TODO: Check if we need to differentiate between primitive types and reference types --> for example in "=="

View File

@ -21,7 +21,7 @@ public class IntDatatype extends AbstractType implements IDatatype{
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
//Example of using BIPUSH and SIPUSH for optimizing bytecode size
if (value >= Byte.MIN_VALUE && value <= Byte.MAX_VALUE)

View File

@ -4,18 +4,33 @@ import TypeCheck.AbstractType;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Program;
import jdk.jshell.spi.ExecutionControl;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.List;
public class RefType extends AbstractType implements IDatatype {
String name;
public List<FieldDecl> fieldDecls;
public List<MethodDecl> methodDecls;
public RefType(List<FieldDecl> fieldDecls, List<MethodDecl> methodDecls){
private HashMap<String, HashMap<String, String>> typeContext; // (class, (type, identifier))
private HashMap<String, HashMap<String, HashMap<String, String>>> methodContext; // (class, (returntype, (identifier, parameter)
private boolean hasMain;
public RefType(List<FieldDecl> fieldDecls,
List<MethodDecl> methodDecls,
HashMap<String, HashMap<String, String>> typeContext,
HashMap<String, HashMap<String, HashMap<String, String>>> methodContext){
this.fieldDecls = fieldDecls;
this.methodDecls = methodDecls;
this.typeContext = typeContext;
this.methodContext = methodContext;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
@ -36,7 +51,7 @@ public class RefType extends AbstractType implements IDatatype {
// Method for code generation which iterates over all the field declarations
// and method declarations and calls their CodeGen methods
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
throw new ExecutionControl.NotImplementedException("CodeGen not implemented for RefType");
}

View File

@ -59,7 +59,7 @@ public class BinaryExpression extends AbstractType implements IExpression{
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
// Label for the jump instruction
Label operationFalse = new Label(); //Operation is false
Label operationTrue = new Label(); //Operation is true
@ -69,88 +69,88 @@ public class BinaryExpression extends AbstractType implements IExpression{
// Bytecode for the binary operation
switch (operator) {
case "&&":
left.CodeGen(mv);
left.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, operationFalse); // IFEQ --> "if equals to zero" (false) --> if left exp is false
right.CodeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, operationFalse); // If right exp is false, jump to the end of the whole expression
mv.visitJumpInsn(Opcodes.GOTO, operationTrue); // If it reaches this point, the right exp is true
break;
case "||":
left.CodeGen(mv);
left.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFNE, operationTrue); // IFNE --> "if not equals to zero" (true) --> if left exp is true
right.CodeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFNE, operationTrue);
break;
case "==":
// Keep in mind that only primitive types are allowed in this case (at this time)
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPEQ, operationTrue); // If the two values are equal, jump to the end of the expression
break;
case "<":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPLT, operationTrue); // Checks only on less than, not equal
break;
case ">":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPGT, operationTrue); // Checks only on greater than, not equal
break;
case "<=":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPLE, operationTrue); // Checks on less than OR equal
break;
case ">=":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPGE, operationTrue); // Checks on greater than OR equal
break;
case "!=":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitJumpInsn(Opcodes.IF_ICMPNE, operationTrue); // Checks on not equal
break;
case "+":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitInsn(Opcodes.IADD);
break;
case "-":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitInsn(Opcodes.ISUB);
break;
case "*":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitInsn(Opcodes.IMUL);
break;
case "/":
left.CodeGen(mv);
right.CodeGen(mv);
left.codeGen(mv);
right.codeGen(mv);
mv.visitInsn(Opcodes.IDIV);
break;

View File

@ -8,5 +8,5 @@ public interface IExpression {
TypeCheckResult typeCheck() throws Exception;
// visit method for code generation
void CodeGen(MethodVisitor mv) throws Exception;
void codeGen(MethodVisitor mv) throws Exception;
}

View File

@ -22,7 +22,7 @@ public class InstVarExpression implements IExpression{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
throw new ExecutionControl.NotImplementedException("CodeGen not implemented for InstVarExpression");
//ALOAD the index of the var

View File

@ -42,9 +42,9 @@ public class UnaryExpression extends AbstractType implements IExpression{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
operand.CodeGen(mv);
operand.codeGen(mv);
switch (operator) {
case "!":

View File

@ -17,7 +17,7 @@ public class VarRefExpression implements IExpression{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
throw new Exception("CodeGen not implemented for VarRefExpression");
}
}

View File

@ -1,11 +0,0 @@
package abstractSyntaxTree.Modifier;
public interface IModifier {
//TODO: Maybe we can just use an enum for the Modifier
// if there is no typeCheck and no CodeGen
// not type or type check
// visit method for code generation
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Modifier;
public class PrivateModifier implements IModifier{
}

View File

@ -1,4 +0,0 @@
package abstractSyntaxTree.Modifier;
public class PublicModifier implements IModifier{
}

View File

@ -1,15 +1,32 @@
package abstractSyntaxTree;
import TypeCheck.TypeCheckResult;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Datatype.RefType;
import org.objectweb.asm.MethodVisitor;
import java.util.HashMap;
import java.util.List;
public class Program {
public List<RefType> classes;
public HashMap<String, HashMap<String, String>> typeContext; // (class, (type, identifier))
public HashMap<String, HashMap<String, HashMap<String, String>>> methodContext; // (class, (returntype, (identifier, parameter)))
public TypeCheckResult typeCheck() throws Exception{
for(RefType oneClass : classes){
HashMap<String, String> classVars = new HashMap<>();
for (FieldDecl fielsDecl: oneClass.fieldDecls)
classVars.put(fielsDecl.type, fielsDecl.identifier);
oneClass.typeCheck();
}
return null;
}
public void codeGen() throws Exception{
for(RefType oneClass : classes){
oneClass.codeGen();
}
}
}

View File

@ -2,16 +2,21 @@ package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.*;
import java.util.HashMap;
import java.util.List;
public class BlockStatement extends AbstractType implements IStatement{
//We will need a parameter which holds the symbol table
HashMap<String, String > localVars;
HashMap<String, String > typeIndentifierTable; // from program
List<IStatement> statements;
public BlockStatement(List<IStatement> statements){
// do we need expression, statementexpression
public BlockStatement(List<IStatement> statements, HashMap<String, String> localVars, HashMap<String, String> typeIndentifierTable){
this.statements = statements;
}
@Override
@ -36,9 +41,9 @@ public class BlockStatement extends AbstractType implements IStatement{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
for (IStatement statement : statements) {
statement.CodeGen(mv); //TODO: I think we need to pass the symbol table here
statement.codeGen(mv); //TODO: I think we need to pass the symbol table here
}
}
}

View File

@ -13,7 +13,7 @@ public class EmptyStatement extends AbstractType implements IStatement{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
//An empty statement does not generate any code
}
}

View File

@ -5,7 +5,9 @@ import org.objectweb.asm.MethodVisitor;
public interface IStatement {
TypeCheckResult typeCheck() throws Exception;
void CodeGen(MethodVisitor mv) throws Exception;
void codeGen(MethodVisitor mv) throws Exception;
}

View File

@ -38,19 +38,19 @@ public class IfElseStatement extends AbstractType implements IStatement{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
Label conditionFalse = new Label();
Label statementEnd = new Label();
condition.CodeGen(mv);
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.CodeGen(mv); //If the condition is true, execute the ifBlock
ifStatement.codeGen(mv); //If the condition is true, execute the ifBlock
mv.visitJumpInsn(Opcodes.GOTO, statementEnd); //Jump to the end of the if-else statement
mv.visitLabel(conditionFalse);
elseStatement.CodeGen(mv); //If the condition is false, execute the elseBlock
elseStatement.codeGen(mv); //If the condition is false, execute the elseBlock
mv.visitLabel(statementEnd); //End of the if-else statement

View File

@ -31,14 +31,14 @@ public class IfStatement extends AbstractType implements IStatement{
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
Label conditionFalse = new Label();
condition.CodeGen(mv);
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); //Checks if the condition is false (0)
ifStatement.CodeGen(mv);
ifStatement.codeGen(mv);
mv.visitLabel(conditionFalse); // If the condition is false, the Statements in the ifBlock will not be executed
}

View File

@ -31,14 +31,14 @@ public class ReturnStatement extends AbstractType implements IStatement{
// This is a problem at "BinaryExpression" and here because we need to know the type to return
// At this point in time we can either return reference types or have an error message
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
if (expression != null) {
expression.CodeGen(mv);
expression.codeGen(mv);
//Get the Type of the expression
String type = expression.typeCheck().type;
if (type.equals("int") || type.equals("bool")) {
if (type.equals("int") || type.equals("bool") || type.equals("char")) {
mv.visitInsn(Opcodes.IRETURN);
} else {
mv.visitInsn(Opcodes.ARETURN);

View File

@ -31,16 +31,16 @@ public class WhileStatement extends AbstractType implements IStatement {
}
@Override
public void CodeGen(MethodVisitor mv) throws Exception {
public void codeGen(MethodVisitor mv) throws Exception {
Label conditionFalse = new Label();
Label LoopStart = new Label();
mv.visitLabel(LoopStart);
condition.CodeGen(mv);
condition.codeGen(mv);
mv.visitJumpInsn(Opcodes.IFEQ, conditionFalse); // Checks if the condition is false (0)
statement.CodeGen(mv);
statement.codeGen(mv);
//TODO: If the block ends with a return statement, we might have to pop it from the stack
// So the next iteration starts with a clean stack
mv.visitJumpInsn(Opcodes.GOTO, LoopStart); // Jump to the start of the while loop

View File

@ -38,7 +38,7 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
right.CodeGen(mv);
if (left instanceof VarRefExpression varRef) {
//TODO: Implement the handling of a variable reference --> I need a lis of local variables
//TODO: Implement the handling of a variable reference --> I need a list of local variables
// for that to determine if the variable is a local or field variable
} else if (left instanceof InstVarExpression instVar) {
mv.visitInsn(Opcodes.DUP_X1);

View File

@ -0,0 +1,10 @@
package abstractSyntaxTree.StatementExpression;
import abstractSyntaxTree.Expression.IExpression;
import java.util.List;
public class SuperStatementExpression extends MethodCallStatementExpression{
public SuperStatementExpression(String methodName, List<IExpression> arguments) {
super(methodName, arguments);
}
}

View File

@ -0,0 +1,273 @@
package AST;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.InputStream;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import Helper.MockGenerator;
import Helper.Resources;
import common.AccessModifier;
import common.BaseType;
import common.Compiler;
import common.Primitives;
import common.PrintableVector;
import syntaxtree.structure.ClassDecl;
import syntaxtree.structure.ConstructorDecl;
import syntaxtree.structure.FieldDecl;
import syntaxtree.structure.Program;
@DisplayName("Abstract Syntax Tree Generation")
public class TestRunner {
@Test
@DisplayName("Control Test")
void controlTest() {
var first = MockGenerator.getConstructorParameterAst();
var second = MockGenerator.getConstructorParameterAst();
assertEquals(first, second);
}
@Test
@DisplayName("Empty Class")
void emptyClass() {
InputStream file = Resources.getFileAsStream("SimpleTests/EmptyClass.java");
Program ast = Compiler.getFactory().getAstAdapter().getAst(file);
PrintableVector<ConstructorDecl> constructors = new PrintableVector<>();
constructors.add(new ConstructorDecl());
ClassDecl classDecl = new ClassDecl("EmptyClass", new PrintableVector<>(), constructors,
new PrintableVector<>());
PrintableVector<ClassDecl> classDecls = new PrintableVector<>();
classDecls.add(classDecl);
var generatedAst = new Program(classDecls);
assertEquals(generatedAst, ast);
}
@Test
@DisplayName("EmptyClassWithConstructor")
void emptyClassWithConstructor() {
InputStream file = Resources.getFileAsStream("SimpleTests/EmptyClassWithConstructor.java");
Program generatedAst = Compiler.getFactory().getAstAdapter().getAst(file);
PrintableVector<ConstructorDecl> constructors = new PrintableVector<>();
constructors.add(new ConstructorDecl());
ClassDecl classDecl = new ClassDecl("EmptyClassWithConstructor", new PrintableVector<>(), constructors,
new PrintableVector<>());
PrintableVector<ClassDecl> classDecls = new PrintableVector<>();
classDecls.add(classDecl);
var ast = new Program(classDecls);
assertEquals(ast, generatedAst);
}
@Test
@DisplayName("ClassFields")
void classFields() {
Program generatedAst = Resources.getProgram("SimpleTests/ClassFields.java");
Program expectedAst = MockGenerator.getClassFieldsAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("ClassField without AccessModifier")
void classFieldWithoutAccessModifier() {
Program generatedAst = Resources.getProgram("SimpleTests/AutoAccessModifierField.java");
Program expectedAst = MockGenerator.getAutoClassFieldAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("Comments")
void commentTest() {
InputStream file = Resources.getFileAsStream("SimpleTests/Comments.java");
Program generatedAst = Compiler.getFactory().getAstAdapter().getAst(file);
PrintableVector<ConstructorDecl> constructors = new PrintableVector<>();
constructors.add(new ConstructorDecl());
PrintableVector<FieldDecl> fields = new PrintableVector<>();
FieldDecl lorem = new FieldDecl("lorem", AccessModifier.PRIVATE);
lorem.setType(new BaseType(Primitives.INT));
fields.add(lorem);
FieldDecl ipsum = new FieldDecl("ipsum", AccessModifier.PRIVATE);
ipsum.setType(new BaseType(Primitives.BOOL));
fields.add(ipsum);
ClassDecl classDecl = new ClassDecl("Comments", fields, constructors, new PrintableVector<>());
PrintableVector<ClassDecl> classDecls = new PrintableVector<>();
classDecls.add(classDecl);
var expectedAst = new Program(classDecls);
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("Constructor With Parameters")
void constructorWithParameters() {
Program generatedAst = Resources.getProgram("SimpleTests/ConstructorParams.java");
Program expectedAst = MockGenerator.getConstructorParameterAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("Constructor With this. assign body")
void constructorWithThisAssignBody() {
Program generatedAst = Resources.getProgram("SimpleTests/ConstructorThisDot.java");
Program expectedAst = MockGenerator.getConstructorThisDotAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("VoidMethod")
void voidMethod() {
Program generatedAst = Resources.getProgram("SimpleTests/VoidMethod.java");
Program expectedAst = MockGenerator.getVoidMethodAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("RealConstructor")
void realConstructor() {
Program generatedAst = Resources.getProgram("SimpleTests/RealConstructor.java");
Program expectedAst = MockGenerator.getRealConstructorAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("MethodCall")
void methodCall() {
Program generatedAst = Resources.getProgram("SimpleTests/MethodCall.java");
Program expectedAst = MockGenerator.getMethodCallAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("ThisDotMethodCall")
void thisDotMethodCall() {
Program generatedAst = Resources.getProgram("SimpleTests/ThisDotMethodCall.java");
Program expectedAst = MockGenerator.getThisDotMethodCallAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("MethodCallWithParameters")
void methodCallWithParameters() {
Program generatedAst = Resources.getProgram("SimpleTests/MethodCallParams.java");
Program expectedAst = MockGenerator.getMethodCallWithParameterAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("GetterFunction")
void getterFunction() {
Program generatedAst = Resources.getProgram("SimpleTests/GetterFunction.java");
Program expectedAst = MockGenerator.getGetterFunctionAst();
System.out.println(generatedAst);
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("CharArgument")
void charArgument() {
Program generatedAst = Resources.getProgram("SimpleTests/CharArgument.java");
Program expectedAst = MockGenerator.getCharArgumentAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("ExplicitNullAssign")
void explicitNullAssign() {
Program generatedAst = Resources.getProgram("SimpleTests/ExplicitNullAssign.java");
Program expectedAst = MockGenerator.getExplicitNullAssignAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("SelfReference")
void selfReference() {
Program generatedAst = Resources.getProgram("SimpleTests/SelfReference.java");
Program expectedAst = MockGenerator.getSelfReferenceAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("ValueAdapterTests")
void valueAdapterTest() {
Program generatedAst = Resources.getProgram("SimpleTests/ValueAdapterTests.java");
Program expectedAst = MockGenerator.getValueAdapterTestAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("System.out.println Test")
void systemOutPrintlnTest() {
Program generatedAst = Resources.getProgram("SimpleTests/SystemOutPrintln.java");
Program expectedAst = MockGenerator.getSystemOutPrintlnAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("System.out.print-String Test")
void systemOutPrintStringTest() {
Program generatedAst = Resources.getProgram("SimpleTests/SystemOutPrintlnString.java");
Program expectedAst = MockGenerator.getSystemOutPrintStringAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("MainMethodTest")
void mainMethodTest() {
Program generatedAst = Resources.getProgram("SimpleTests/MainMethodTest.java");
Program expectedAst = MockGenerator.getMainMethodTestAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("ForTest")
void forTest() {
Program generatedAst = Resources.getProgram("SimpleTests/ForTest.java");
Program expectedAst = MockGenerator.getForTestAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("IncTest")
void incTest() {
Program generatedAst = Resources.getProgram("SimpleTests/IncTest.java");
Program expectedAst = MockGenerator.getIncTestAst();
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("DecTest")
void decTest() {
Program generatedAst = Resources.getProgram("SimpleTests/DecTest.java");
Program expectedAst = MockGenerator.getDecTestAst();
assertEquals(expectedAst, generatedAst);
}
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,356 @@
package CodeGen;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import Helper.MockGenerator;
import Helper.ReflectLoader;
import Helper.Resources;
import common.Compiler;
import common.PrintableVector;
import syntaxtree.structure.ClassDecl;
import syntaxtree.structure.ConstructorDecl;
import syntaxtree.structure.Program;
@DisplayName("Bytecode Generation")
public class TestRunner {
private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
private final PrintStream originalOut = System.out;
private final PrintStream originalErr = System.err;
@Test
@DisplayName("Empty Class")
void main() {
ClassDecl emptyClass = new ClassDecl("EmptyClass", new PrintableVector<>(), new PrintableVector<>(),
new PrintableVector<>());
PrintableVector<ClassDecl> classDecls = new PrintableVector<>();
classDecls.add(emptyClass);
var tast = new Program(classDecls);
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("EmptyClass");
Object o;
try {
o = c.getDeclaredConstructor().newInstance();
assertEquals(o.getClass().getName(), "EmptyClass");
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("EmptyClassWithConstructor")
void emptyClassWithConstructor() {
PrintableVector<ConstructorDecl> constructors = new PrintableVector<>();
constructors.add(new ConstructorDecl());
ClassDecl classDecl = new ClassDecl("EmptyClassWithConstructor", new PrintableVector<>(), constructors,
new PrintableVector<>());
PrintableVector<ClassDecl> classDecls = new PrintableVector<>();
classDecls.add(classDecl);
var tast = new Program(classDecls);
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("EmptyClassWithConstructor");
Object o;
try {
o = c.getDeclaredConstructor().newInstance();
assertEquals(o.getClass().getName(), "EmptyClassWithConstructor");
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
| NoSuchMethodException | SecurityException e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("Constructor With Parameters")
void constructorWithParameters() {
Program tast = MockGenerator.getConstructorParameterTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("ConstructorParams");
Object o = null;
try {
o = c.getDeclaredConstructor(int.class).newInstance(1);
assertEquals(o.getClass().getName(), "ConstructorParams");
} catch (Exception e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("Constructor With this. assign body")
void constructorWithThisAssignBody() {
Program tast = MockGenerator.getConstructorThisDotTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("ConstructorThisDot");
Object o = null;
try {
o = c.getDeclaredConstructor().newInstance();
var i = loader.getField("ConstructorThisDot", "i");
var ivalue = i.get(o);
assertEquals(5, ivalue);
} catch (Exception e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("ClassFields - autoAccess")
void classFieldsAuto() {
Program tast = MockGenerator.getAutoClassFieldAst();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
try {
var autoAccess = loader.getField("AutoAccessModifierField", "autoAccess");
System.out.println(autoAccess.getModifiers());
assertEquals(0, autoAccess.getModifiers());
} catch (Exception e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("ClassFields - privateAccess")
void classFieldsPrivate() {
Program tast = MockGenerator.getClassFieldsTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
try {
var autoAccess = loader.getField("ClassFields", "privateAccess");
assertEquals(Modifier.PRIVATE, autoAccess.getModifiers());
} catch (Exception e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("ClassFields - publicAccess")
void classFieldsPublic() {
Program tast = MockGenerator.getClassFieldsTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
try {
var autoAccess = loader.getField("ClassFields", "publicAccess");
assertEquals(Modifier.PUBLIC, autoAccess.getModifiers());
} catch (Exception e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("ClassFields - protectedAccess")
void classFieldsProtected() {
Program tast = MockGenerator.getClassFieldsTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
try {
var autoAccess = loader.getField("ClassFields", "protectedAccess");
assertEquals(Modifier.PROTECTED, autoAccess.getModifiers());
} catch (Exception e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("VoidMethod")
void voidMethod() {
Program tast = MockGenerator.getVoidMethodTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("VoidMethod");
Object o = null;
try {
o = c.getDeclaredConstructor().newInstance();
var m = loader.getMethod("VoidMethod", "foo");
m.invoke(o);
assertEquals("foo", m.getName());
} catch (Exception e) {
fail(e.getLocalizedMessage());
}
}
@Test
@DisplayName("RealConstructor")
void realConstructor() {
Program tast = MockGenerator.getRealConstructorTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("RealConstructor");
try {
int randomI = 2;
var constructor = c.getDeclaredConstructor(int.class);
Object o = constructor.newInstance(randomI);
var i = loader.getField("RealConstructor", "i");
int ivalue = (int) i.get(o);
assertEquals(randomI, ivalue);
} catch (NoSuchFieldException e) {
fail("No such field");
} catch (Exception e) {
fail(e.getCause());
}
}
@Test
@DisplayName("MethodCall")
void methodCall() {
Program tast = MockGenerator.getMethodCallTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("MethodCall");
Object o = null;
int value = 1;
try {
o = c.getDeclaredConstructor().newInstance();
var i = loader.getField("MethodCall", "i");
int ivalue = (int) i.get(o);
assertEquals(value, ivalue);
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
@DisplayName("MiniLinkedList")
void miniLinkedList() {
Program program = Resources.getProgram("SimpleTests/MiniLinkedList.java");
System.out.println(program);
Program tast = Compiler.getFactory().getTastAdapter().getTast(program);
Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
}
@Test
@DisplayName("SystemOutPrintln Test")
void systemOutPrintlnTest() {
Program tast = MockGenerator.getSystemOutPrintlnTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("SystemOutPrintln");
Object o = null;
try {
o = c.getDeclaredConstructor().newInstance();
var m = loader.getMethod("SystemOutPrintln", "foo");
m.invoke(o);
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
@DisplayName("SystemOutPrintlnString Test")
void systemOutPrintlnStringTest() {
Program tast = MockGenerator.getSystemOutPrintStringTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("SystemOutPrintlnString");
Object o = null;
try {
o = c.getDeclaredConstructor().newInstance();
var m = loader.getMethod("SystemOutPrintlnString", "foo");
m.invoke(o);
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
@DisplayName("MainMethodTest")
void mainMethodTest() {
Program tast = MockGenerator.getMainMethodTestTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("MainMethodTest");
try {
var m = c.getMethod("main", String[].class);
m.invoke(null, new Object[] { new String[] {} });
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
@DisplayName("IncTest")
void incTest() {
Program tast = MockGenerator.getIncTestTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("IncTest");
Object o = null;
try {
o = c.getDeclaredConstructor().newInstance();
var foo = loader.getMethod("IncTest", "foo");
var bar = loader.getMethod("IncTest", "bar");
foo.invoke(o);
bar.invoke(o);
System.setOut(new PrintStream(outContent));
foo.invoke(o);
var expected = new String("0123456789").replaceAll("\\p{C}", "");
var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", "");
outContent.reset();
assertEquals(expected, actual);
bar.invoke(o);
actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", "");
assertEquals(expected, actual);
System.setOut(originalOut);
} catch (Exception e) {
fail(e.getMessage());
}
}
@Test
@DisplayName("DecTest")
void decTest() {
Program tast = MockGenerator.getDecTestTast();
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
ReflectLoader loader = new ReflectLoader(bc);
Class<?> c = loader.findClass("DecTest");
Object o = null;
try {
o = c.getDeclaredConstructor().newInstance();
var foo = loader.getMethod("DecTest", "foo");
var bar = loader.getMethod("DecTest", "bar");
foo.invoke(o);
bar.invoke(o);
System.setOut(new PrintStream(outContent));
foo.invoke(o);
var expected = new String("10987654321").replaceAll("\\p{C}", "");
var actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", "");
outContent.reset();
assertEquals(expected, actual);
bar.invoke(o);
actual = new String(outContent.toByteArray()).replaceAll("\\p{C}", "");
assertEquals(expected, actual);
System.setOut(originalOut);
} catch (Exception e) {
fail(e.getMessage());
}
}
}

View File

@ -0,0 +1,965 @@
package Helper;
import common.AccessModifier;
import common.BaseType;
import common.Operator;
import common.Primitives;
import common.PrintableVector;
import common.ReferenceType;
import syntaxtree.expressions.Binary;
import syntaxtree.expressions.BoolExpr;
import syntaxtree.expressions.CharExpr;
import syntaxtree.expressions.IExpression;
import syntaxtree.expressions.InstVar;
import syntaxtree.expressions.IntegerExpr;
import syntaxtree.expressions.LocalOrFieldVar;
import syntaxtree.expressions.Null;
import syntaxtree.expressions.StringExpr;
import syntaxtree.expressions.This;
import syntaxtree.statementexpression.Assign;
import syntaxtree.statementexpression.CrementStmtExpr;
import syntaxtree.statementexpression.MethodCall;
import syntaxtree.statementexpression.NewDecl;
import syntaxtree.statements.Block;
import syntaxtree.statements.ForStmt;
import syntaxtree.statements.IStatement;
import syntaxtree.statements.LocalVarDecl;
import syntaxtree.statements.ReturnStmt;
import syntaxtree.structure.ClassDecl;
import syntaxtree.structure.ConstructorDecl;
import syntaxtree.structure.FieldDecl;
import syntaxtree.structure.MainMethodDecl;
import syntaxtree.structure.MethodDecl;
import syntaxtree.structure.MethodParameter;
import syntaxtree.structure.Program;
public abstract class MockGenerator {
/**
* @param className
* @return Program
*/
public static Program getEmptyProgram(String className) {
PrintableVector<ClassDecl> classes = new PrintableVector<>();
classes.add(getEmptyClass(className));
return new Program(classes);
}
/**
* @param id
* @return ClassDecl
*/
public static ClassDecl getEmptyClass(String id) {
return new ClassDecl(id, new PrintableVector<>(), new PrintableVector<>(), new PrintableVector<>());
}
/**
* @param expressions
* @return Block
*/
public static Block getBlock(IStatement... expressions) {
PrintableVector<IStatement> expressionsVector = new PrintableVector<>();
for (IStatement expression : expressions) {
expressionsVector.add(expression);
}
return new Block(expressionsVector);
}
/**
* @return Block
*/
public static Block getEmptyBlock() {
return new Block();
}
/**
* @param expressions
* @return PrintableVector<IExpression>
*/
public static PrintableVector<IExpression> getArguments(IExpression... expressions) {
PrintableVector<IExpression> arguments = new PrintableVector<>();
for (IExpression expression : expressions) {
arguments.add(expression);
}
return arguments;
}
/**
* @param constructors
* @return PrintableVector<ConstructorDecl>
*/
public static PrintableVector<ConstructorDecl> getConstructors(ConstructorDecl... constructors) {
PrintableVector<ConstructorDecl> cons = new PrintableVector<>();
if (constructors.length == 0)
cons.add(new ConstructorDecl());
for (ConstructorDecl constructor : constructors) {
cons.add(constructor);
}
return cons;
}
/**
* @return PrintableVector<MethodParameter>
*/
public static PrintableVector<MethodParameter> getEmptyParameters() {
return new PrintableVector<>();
}
/**
* @param parameters
* @return PrintableVector<MethodParameter>
*/
public static PrintableVector<MethodParameter> getParameters(MethodParameter... parameters) {
PrintableVector<MethodParameter> parametersVector = new PrintableVector<>();
for (MethodParameter parameter : parameters) {
parametersVector.add(parameter);
}
return parametersVector;
}
/**
* @return Program
*/
public static Program getClassFieldsTast() {
return getClassFieldsAst();
}
/**
* @return Program
*/
public static Program getClassFieldsAst() {
Program expectedAst = getEmptyProgram("ClassFields");
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
FieldDecl privateField = new FieldDecl("privateAccess", AccessModifier.PRIVATE);
privateField.setType(new BaseType(Primitives.INT));
FieldDecl publicField = new FieldDecl("publicAccess", AccessModifier.PUBLIC);
publicField.setType(new BaseType(Primitives.INT));
FieldDecl protectedField = new FieldDecl("protectedAccess", AccessModifier.PROTECTED);
protectedField.setType(new BaseType(Primitives.INT));
PrintableVector<FieldDecl> fields = expectedAst.getClasses().firstElement().getFieldDelcarations();
fields.add(privateField);
fields.add(publicField);
fields.add(protectedField);
return expectedAst;
}
/**
* @return Program
*/
public static Program getAutoClassFieldAst() {
Program expectedAst = getEmptyProgram("AutoAccessModifierField");
FieldDecl autoField = new FieldDecl(new BaseType(Primitives.INT), "autoAccess");
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
PrintableVector<FieldDecl> fields = expectedAst.getClasses().firstElement().getFieldDelcarations();
fields.add(autoField);
return expectedAst;
}
/**
* @return Program
*/
public static Program getAutoClassFieldTast() {
return getAutoClassFieldAst();
}
/**
* @return Program
*/
public static Program getConstructorParameterTast() {
return getConstructorParameterAst();
}
/**
* @return Program
*/
public static Program getConstructorParameterAst() {
Program expectedAst = getEmptyProgram("ConstructorParams");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
MethodParameter parameter = new MethodParameter(Primitives.INT, "i");
PrintableVector<MethodParameter> parameters = new PrintableVector<>();
parameters.add(parameter);
classDecl.getConstructorDeclarations()
.add(new ConstructorDecl(AccessModifier.PUBLIC, parameters, getEmptyBlock()));
return expectedAst;
}
/**
* @return Program
*/
public static Program getConstructorThisDotAst() {
Program expectedAst = getEmptyProgram("ConstructorThisDot");
FieldDecl i = new FieldDecl("i", AccessModifier.PUBLIC);
i.setType(new BaseType(Primitives.INT));
PrintableVector<FieldDecl> fields = expectedAst.getClasses().firstElement().getFieldDelcarations();
fields.add(i);
ClassDecl classDecl = expectedAst.getClasses().firstElement();
Block block = getEmptyBlock();
Assign assign = new Assign(new InstVar("i", new This()), new IntegerExpr(5));
block.getStatements().add(assign);
classDecl.getConstructorDeclarations()
.add(new ConstructorDecl(AccessModifier.PUBLIC, getParameters(), block));
System.out.println(classDecl);
return expectedAst;
}
/**
* @return Program
*/
public static Program getConstructorThisDotTast() {
Program expectedTast = getEmptyProgram("ConstructorThisDot");
FieldDecl i = new FieldDecl("i", AccessModifier.PUBLIC);
i.setType(new BaseType(Primitives.INT));
PrintableVector<FieldDecl> fields = expectedTast.getClasses().firstElement().getFieldDelcarations();
fields.add(i);
ClassDecl classDecl = expectedTast.getClasses().firstElement();
Block block = getEmptyBlock();
var thisi = new InstVar("i", new This("ConstructorThisDot"));
thisi.setType(Primitives.INT);
Assign assign = new Assign(thisi, new IntegerExpr(5));
assign.setType(Primitives.INT);
block.getStatements().add(assign);
classDecl.getConstructorDeclarations()
.add(new ConstructorDecl(AccessModifier.PUBLIC, getEmptyParameters(), block));
return expectedTast;
}
/**
* @return Program
*/
public static Program getVoidMethodAst() {
Program expectedAst = getEmptyProgram("VoidMethod");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
PrintableVector<MethodDecl> methods = classDecl.getMethodDeclarations();
MethodDecl foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(),
getEmptyBlock());
methods.add(foo);
return expectedAst;
}
/**
* @return Program
*/
public static Program getVoidMethodTast() {
return getVoidMethodAst();
}
/**
* @return Program
*/
public static Program getRealMethodAst() {
Program expectedAst = getEmptyProgram("RealMethod");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
Block block = getEmptyBlock();
ReturnStmt returnStmt = new ReturnStmt(new LocalOrFieldVar("i"));
block.getStatements().add(returnStmt);
var parameters = getEmptyParameters();
parameters.add(new MethodParameter(Primitives.INT, "i"));
PrintableVector<MethodDecl> methods = classDecl.getMethodDeclarations();
MethodDecl foo = new MethodDecl(new BaseType(Primitives.INT), "foo", parameters, block);
methods.add(foo);
return expectedAst;
}
/**
* @return Program
*/
public static Program getRealConstructorAst() {
Program expectedAst = getEmptyProgram("RealConstructor");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
FieldDecl i = new FieldDecl(AccessModifier.PRIVATE, new BaseType(Primitives.INT), "i");
classDecl.getFieldDelcarations().add(i);
Assign assignStmt = new Assign(new InstVar(new This(), "i"), new LocalOrFieldVar("i"));
Block block = getBlock(assignStmt);
var parameters = getParameters(new MethodParameter(Primitives.INT, "i"));
PrintableVector<ConstructorDecl> constructors = classDecl.getConstructorDeclarations();
ConstructorDecl constructor = new ConstructorDecl(AccessModifier.PUBLIC, parameters, block);
constructors.add(constructor);
return expectedAst;
}
/**
* @return Program
*/
public static Program getRealConstructorTast() {
String className = "RealConstructor";
Program expectedTast = getEmptyProgram(className);
ClassDecl classDecl = expectedTast.getClasses().firstElement();
FieldDecl i = new FieldDecl(AccessModifier.PRIVATE, new BaseType(Primitives.INT), "i");
classDecl.getFieldDelcarations().add(i);
Assign assignStmt = new Assign(new InstVar(new BaseType(Primitives.INT), new This(
className), "i"), new LocalOrFieldVar(new BaseType(Primitives.INT), "i"));
assignStmt.setType(new BaseType(Primitives.INT));
Block block = getBlock(assignStmt);
var parameters = getParameters(new MethodParameter(Primitives.INT, "i"));
PrintableVector<ConstructorDecl> constructors = classDecl.getConstructorDeclarations();
ConstructorDecl constructor = new ConstructorDecl(AccessModifier.PUBLIC, parameters, block);
constructors.add(constructor);
return expectedTast;
}
/**
* @return Program
*/
public static Program getMethodCallAst() {
Program expectedAst = getEmptyProgram("MethodCall");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
var fields = classDecl.getFieldDelcarations();
fields.add(new FieldDecl(new BaseType(Primitives.INT), "i"));
PrintableVector<ConstructorDecl> constructors = classDecl.getConstructorDeclarations();
Block block = getBlock(
new Assign(new InstVar(new This(), "i"),
new MethodCall(new This(), "foo", getArguments())));
constructors.add(new ConstructorDecl(AccessModifier.PUBLIC, getParameters(), block));
Block fooBlock = getBlock(new ReturnStmt(new IntegerExpr(1)));
PrintableVector<MethodDecl> methods = classDecl.getMethodDeclarations();
methods.add(new MethodDecl(new BaseType(Primitives.INT), "foo", getEmptyParameters(), fooBlock));
return expectedAst;
}
/**
* @return Program
*/
public static Program getMethodCallTast() {
Program expectedTast = getEmptyProgram("MethodCall");
ClassDecl classDecl = expectedTast.getClasses().firstElement();
var fields = classDecl.getFieldDelcarations();
fields.add(new FieldDecl(new BaseType(Primitives.INT), "i"));
PrintableVector<ConstructorDecl> constructors = classDecl.getConstructorDeclarations();
Block block = getBlock(
new Assign(new BaseType(
Primitives.INT),
new InstVar(new BaseType(Primitives.INT), new This("MethodCall"), "i"),
new MethodCall(new BaseType(Primitives.INT), new This("MethodCall"),
"foo", getArguments())));
constructors.add(new ConstructorDecl(AccessModifier.PUBLIC, getParameters(), block));
Block fooBlock = getBlock(new ReturnStmt(new BaseType(Primitives.INT), new IntegerExpr(1)));
fooBlock.setType(Primitives.INT);
PrintableVector<MethodDecl> methods = classDecl.getMethodDeclarations();
methods.add(new MethodDecl(new BaseType(Primitives.INT), "foo", getEmptyParameters(), fooBlock));
return expectedTast;
}
/**
* @return Program
*/
public static Program getMethodCallWithParameterAst() {
Program expectedAst = getEmptyProgram("MethodCallParams");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
var fields = classDecl.getFieldDelcarations();
fields.add(new FieldDecl(new BaseType(Primitives.INT), "i"));
PrintableVector<ConstructorDecl> constructors = classDecl.getConstructorDeclarations();
Block block = getBlock(
new Assign(new InstVar(new This(), "i"),
new MethodCall(new This(), "foo",
getArguments(new LocalOrFieldVar("i")))));
constructors.add(new ConstructorDecl(AccessModifier.PUBLIC,
getParameters(new MethodParameter(Primitives.INT, "i")), block));
Block fooBlock = getBlock(new ReturnStmt(new LocalOrFieldVar("i")));
PrintableVector<MethodDecl> methods = classDecl.getMethodDeclarations();
methods.add(new MethodDecl(new BaseType(Primitives.INT), "foo",
getParameters(new MethodParameter(Primitives.INT, "i")), fooBlock));
return expectedAst;
}
/**
* @return Program
*/
public static Program getCharArgumentAst() {
Program expectedAst = getEmptyProgram("CharArgument");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
var fields = classDecl.getFieldDelcarations();
fields.add(new FieldDecl(new BaseType(Primitives.CHAR), "c"));
PrintableVector<ConstructorDecl> constructors = classDecl.getConstructorDeclarations();
Block block = getBlock(
new Assign(new InstVar(new This(), "c"),
new MethodCall(new This(), "foo", getArguments(new CharExpr('a')))));
constructors.add(new ConstructorDecl(AccessModifier.PUBLIC,
getParameters(), block));
Block fooBlock = getBlock(new ReturnStmt(new LocalOrFieldVar("c")));
PrintableVector<MethodDecl> methods = classDecl.getMethodDeclarations();
methods.add(new MethodDecl(new BaseType(Primitives.CHAR), "foo",
getParameters(new MethodParameter(Primitives.CHAR, "c")), fooBlock));
return expectedAst;
}
/**
* @return Program
*/
public static Program getGetterFunctionAst() {
Program expectedAst = getEmptyProgram("GetterFunction");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
FieldDecl i = new FieldDecl(AccessModifier.PRIVATE, new BaseType(Primitives.INT), "i");
classDecl.getFieldDelcarations().add(i);
Assign assignStmt = new Assign(new InstVar(new This(), "i"), new LocalOrFieldVar("i"));
Block block = getBlock(assignStmt);
var parameters = getParameters(new MethodParameter(Primitives.INT, "i"));
PrintableVector<ConstructorDecl> constructors = classDecl.getConstructorDeclarations();
ConstructorDecl constructor = new ConstructorDecl(AccessModifier.PUBLIC, parameters, block);
constructors.add(constructor);
var getI = new MethodDecl(AccessModifier.PUBLIC, new BaseType(Primitives.INT), "getI", getParameters(),
getBlock(new ReturnStmt(new InstVar(new This(), "i"))));
classDecl.getMethodDeclarations().add(getI);
return expectedAst;
}
public static Program getExplicitNullAssignAst() {
Program expectedAst = getEmptyProgram("ExplicitNullAssign");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
FieldDecl i = new FieldDecl(AccessModifier.PACKAGE_PRIVATE, new ReferenceType("ExplicitNullAssign"),
"e");
classDecl.getFieldDelcarations().add(i);
var test = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.VOID), "test",
getParameters(),
getBlock(new Assign(new LocalOrFieldVar("e"), new Null())));
classDecl.getMethodDeclarations().add(test);
classDecl.getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getSelfReferenceAst() {
Program expectedAst = getEmptyProgram("SelfReference");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
FieldDecl i = new FieldDecl(AccessModifier.PACKAGE_PRIVATE, new ReferenceType("SelfReference"),
"selfRef");
classDecl.getFieldDelcarations().add(i);
var foo = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "foo",
getParameters(),
getBlock(new ReturnStmt(new MethodCall(new This(), "baz", getArguments()))));
var baz = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "baz",
getParameters(),
getBlock(new ReturnStmt(new IntegerExpr(10))));
var bar = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "bar",
getParameters(),
getBlock(
new LocalVarDecl(new ReferenceType("SelfReference"), "self",
new NewDecl("SelfReference", getArguments())),
new ReturnStmt(new MethodCall(
new InstVar(new LocalOrFieldVar("self"), "selfRef"),
"foo",
getArguments()))));
classDecl.getMethodDeclarations().add(foo);
classDecl.getMethodDeclarations().add(baz);
classDecl.getMethodDeclarations().add(bar);
classDecl.getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getSelfReferenceTast() {
Program expectedAst = getEmptyProgram("SelfReference");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
FieldDecl i = new FieldDecl(AccessModifier.PACKAGE_PRIVATE, new ReferenceType("SelfReference"),
"selfRef");
classDecl.getFieldDelcarations().add(i);
Block fooBlock = getBlock(new ReturnStmt(new BaseType(Primitives.INT),
new MethodCall(new BaseType(Primitives.INT), new This("SelfReference"), "baz",
getArguments())));
fooBlock.setType(new BaseType(Primitives.INT));
Block bazBlock = getBlock(new ReturnStmt(new BaseType(Primitives.INT), new IntegerExpr(10)));
bazBlock.setType(new BaseType(Primitives.INT));
Block barBlock = getBlock(
new LocalVarDecl(new ReferenceType("SelfReference"), "self",
new NewDecl("SelfReference", getArguments())),
new ReturnStmt(new BaseType(Primitives.INT),
new MethodCall(new BaseType(Primitives.INT),
new InstVar(new ReferenceType("SelfReference"),
new LocalOrFieldVar(new ReferenceType(
"SelfReference"),
"self"),
"selfRef"),
"foo",
getArguments())));
barBlock.setType(new BaseType(Primitives.INT));
var foo = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "foo",
getParameters(), fooBlock);
var baz = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "baz",
getParameters(), bazBlock);
var bar = new MethodDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT), "bar",
getParameters(),
barBlock);
classDecl.getMethodDeclarations().add(foo);
classDecl.getMethodDeclarations().add(baz);
classDecl.getMethodDeclarations().add(bar);
classDecl.getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getThisDotMethodCallAst() {
Program expectedAst = getEmptyProgram("ThisDotMethodCall");
ClassDecl classDecl = expectedAst.getClasses().firstElement();
var fields = classDecl.getFieldDelcarations();
fields.add(new FieldDecl(new BaseType(Primitives.INT), "i"));
PrintableVector<ConstructorDecl> constructors = classDecl.getConstructorDeclarations();
Block block = getBlock(
new Assign(new InstVar(new This(), "i"),
new MethodCall(new This(), "foo", getArguments())));
constructors.add(new ConstructorDecl(AccessModifier.PUBLIC, getParameters(), block));
Block fooBlock = getBlock(new ReturnStmt(new IntegerExpr(1)));
PrintableVector<MethodDecl> methods = classDecl.getMethodDeclarations();
methods.add(new MethodDecl(new BaseType(Primitives.INT), "foo", getEmptyParameters(), fooBlock));
return expectedAst;
}
public static Program getValueAdapterTestAst() {
Program expectedAst = getEmptyProgram("ValueAdapterTests");
Block readsTrueBlock = getBlock(new ReturnStmt(new BoolExpr(true)));
Block readsFalseBlock = getBlock(new ReturnStmt(new BoolExpr(false)));
Block readsTrueAndFalseBlock = getBlock(new ReturnStmt(new Binary(new BoolExpr(true),
Operator.AND, new BoolExpr(false))));
Block readsIntBlock = getBlock(new ReturnStmt(new IntegerExpr(1)));
Block readsIntAndIntBlock = getBlock(new ReturnStmt(new Binary(new IntegerExpr(1),
Operator.PLUS, new IntegerExpr(1))));
Block readsCharBlock = getBlock(new ReturnStmt(new CharExpr('a')));
MethodDecl readsTrue = new MethodDecl(new BaseType(Primitives.BOOL), "readsTrue", getEmptyParameters(),
readsTrueBlock);
MethodDecl readsFalse = new MethodDecl(new BaseType(Primitives.BOOL), "readsFalse",
getEmptyParameters(),
readsFalseBlock);
MethodDecl readsTrueAndFalse = new MethodDecl(new BaseType(Primitives.BOOL), "readsTrueAndFalse",
getEmptyParameters(), readsTrueAndFalseBlock);
MethodDecl readsInt = new MethodDecl(new BaseType(Primitives.INT), "readsInt", getEmptyParameters(),
readsIntBlock);
MethodDecl readsIntAndInt = new MethodDecl(new BaseType(Primitives.INT), "readsIntAndInt",
getEmptyParameters(),
readsIntAndIntBlock);
MethodDecl readsChar = new MethodDecl(new BaseType(Primitives.CHAR), "readsChar", getEmptyParameters(),
readsCharBlock);
var methods = expectedAst.getClasses().firstElement().getMethodDeclarations();
methods.add(readsTrue);
methods.add(readsFalse);
methods.add(readsTrueAndFalse);
methods.add(readsInt);
methods.add(readsIntAndInt);
methods.add(readsChar);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getSystemOutPrintlnTast() {
Program expectedAst = getEmptyProgram("SystemOutPrintln");
var iv = new InstVar(new ReferenceType("java/io/PrintStream"),
new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out");
iv.setAccessModifier(AccessModifier.PUBLIC_STATIC);
Block block = getBlock(
new MethodCall(new BaseType(Primitives.VOID),
iv,
"println",
getArguments(new IntegerExpr(1))));
var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(method);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getSystemOutPrintlnAst() {
Program expectedAst = getEmptyProgram("SystemOutPrintln");
Block block = getBlock(
new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println",
getArguments(new IntegerExpr(1))));
var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(method);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getSystemOutPrintStringAst() {
Program expectedAst = getEmptyProgram("SystemOutPrintlnString");
Block block = getBlock(
new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println",
getArguments(new StringExpr("Das ist ein String"))));
var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(method);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getSystemOutPrintStringTast() {
Program expectedAst = getEmptyProgram("SystemOutPrintlnString");
var iv = new InstVar(new ReferenceType("java/io/PrintStream"),
new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out");
iv.setAccessModifier(AccessModifier.PUBLIC_STATIC);
Block block = getBlock(
new MethodCall(new BaseType(Primitives.VOID),
iv,
"println",
getArguments(new StringExpr("Das ist ein String"))));
var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(method);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getMainMethodTestAst() {
Program expectedAst = getEmptyProgram("MainMethodTest");
Block block = getBlock(
new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println",
getArguments(new StringExpr("maintest"))));
var method = new MainMethodDecl(block);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(method);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getMainMethodTestTast() {
Program expectedAst = getEmptyProgram("MainMethodTest");
var iv = new InstVar(new ReferenceType("java/io/PrintStream"),
new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out");
iv.setAccessModifier(AccessModifier.PUBLIC_STATIC);
Block block = getBlock(
new MethodCall(new BaseType(Primitives.VOID),
iv,
"println",
getArguments(new StringExpr("maintest"))));
var method = new MainMethodDecl(block);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(method);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getForTestAst() {
Program expectedAst = getEmptyProgram("ForTest");
Block forBlock = getBlock(new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println",
getArguments(new LocalOrFieldVar("i"))));
Block block = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)),
new Binary(new LocalOrFieldVar("i"), new IntegerExpr(10),
Operator.LESS),
new Assign(new LocalOrFieldVar("i"),
new Binary(new LocalOrFieldVar("i"), new IntegerExpr(1),
Operator.PLUS)),
forBlock));
var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(method);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getForTestTast() {
Program expectedAst = getEmptyProgram("ForTest");
var iv = new InstVar(new ReferenceType("java/io/PrintStream"),
new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out");
iv.setAccessModifier(AccessModifier.PUBLIC_STATIC);
Block forBlock = getBlock(new MethodCall(new BaseType(Primitives.VOID),
iv,
"println",
getArguments(new LocalOrFieldVar(new BaseType(Primitives.INT), "i"))));
Block block = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)),
new Binary(new BaseType(Primitives.BOOL),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
new IntegerExpr(10),
Operator.LESS),
new Assign(new BaseType(Primitives.INT),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
new Binary(new BaseType(Primitives.INT),
new LocalOrFieldVar(new BaseType(
Primitives.INT), "i"),
new IntegerExpr(1),
Operator.PLUS)),
forBlock));
var method = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), block);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(method);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getIncTestAst() {
Program expectedAst = getEmptyProgram("IncTest");
Block forBlock = getBlock(new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println",
getArguments(new LocalOrFieldVar("i"))));
Block blockfoo = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)),
new Binary(new LocalOrFieldVar("i"), new IntegerExpr(10),
Operator.LESS),
new CrementStmtExpr(new LocalOrFieldVar("i"), Operator.INCSUF),
forBlock));
Block blockbar = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)),
new Binary(new LocalOrFieldVar("i"), new IntegerExpr(10),
Operator.LESS),
new CrementStmtExpr(new LocalOrFieldVar("i"), Operator.INCPRE),
forBlock));
var foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), blockfoo);
var bar = new MethodDecl(new BaseType(Primitives.VOID), "bar", getEmptyParameters(), blockbar);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(foo);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(bar);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getIncTestTast() {
Program expectedAst = getEmptyProgram("IncTest");
var iv = new InstVar(new ReferenceType("java/io/PrintStream"),
new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out");
iv.setAccessModifier(AccessModifier.PUBLIC_STATIC);
Block forBlock = getBlock(new MethodCall(new BaseType(Primitives.VOID),
iv,
"println",
getArguments(new LocalOrFieldVar(new BaseType(Primitives.INT), "i"))));
Block fooblock = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)),
new Binary(new BaseType(Primitives.BOOL),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
new IntegerExpr(10),
Operator.LESS),
new CrementStmtExpr(new BaseType(Primitives.INT),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
Operator.INCSUF),
forBlock));
Block barblock = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(0)),
new Binary(new BaseType(Primitives.BOOL),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
new IntegerExpr(10),
Operator.LESS),
new CrementStmtExpr(new BaseType(Primitives.INT),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
Operator.INCPRE),
forBlock));
var foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), fooblock);
var bar = new MethodDecl(new BaseType(Primitives.VOID), "bar", getEmptyParameters(), barblock);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(foo);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(bar);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getDecTestAst() {
Program expectedAst = getEmptyProgram("DecTest");
Block forBlock = getBlock(new MethodCall(new InstVar(new LocalOrFieldVar("System"), "out"), "println",
getArguments(new LocalOrFieldVar("i"))));
Block blockfoo = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(10)),
new Binary(new LocalOrFieldVar("i"), new IntegerExpr(0),
Operator.GREATER),
new CrementStmtExpr(new LocalOrFieldVar("i"), Operator.DECSUF),
forBlock));
Block blockbar = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(10)),
new Binary(new LocalOrFieldVar("i"), new IntegerExpr(0),
Operator.GREATER),
new CrementStmtExpr(new LocalOrFieldVar("i"), Operator.DECPRE),
forBlock));
var foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), blockfoo);
var bar = new MethodDecl(new BaseType(Primitives.VOID), "bar", getEmptyParameters(), blockbar);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(foo);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(bar);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
public static Program getDecTestTast() {
Program expectedAst = getEmptyProgram("DecTest");
var iv = new InstVar(new ReferenceType("java/io/PrintStream"),
new LocalOrFieldVar(new ReferenceType("java/lang/System"), "System"), "out");
iv.setAccessModifier(AccessModifier.PUBLIC_STATIC);
Block forBlock = getBlock(new MethodCall(new BaseType(Primitives.VOID),
iv,
"println",
getArguments(new LocalOrFieldVar(new BaseType(Primitives.INT), "i"))));
Block fooblock = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(10)),
new Binary(new BaseType(Primitives.BOOL),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
new IntegerExpr(0),
Operator.GREATER),
new CrementStmtExpr(new BaseType(Primitives.INT),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
Operator.DECSUF),
forBlock));
Block barblock = getBlock(
new ForStmt(new LocalVarDecl(new BaseType(Primitives.INT), "i", new IntegerExpr(10)),
new Binary(new BaseType(Primitives.BOOL),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
new IntegerExpr(0),
Operator.GREATER),
new CrementStmtExpr(new BaseType(Primitives.INT),
new LocalOrFieldVar(new BaseType(Primitives.INT), "i"),
Operator.DECPRE),
forBlock));
var foo = new MethodDecl(new BaseType(Primitives.VOID), "foo", getEmptyParameters(), fooblock);
var bar = new MethodDecl(new BaseType(Primitives.VOID), "bar", getEmptyParameters(), barblock);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(foo);
expectedAst.getClasses().firstElement().getMethodDeclarations().add(bar);
expectedAst.getClasses().firstElement().getConstructorDeclarations().add(new ConstructorDecl());
return expectedAst;
}
}

View File

@ -0,0 +1,104 @@
package Helper;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import common.Compiler;
import syntaxtree.structure.Program;
public class ReflectLoader extends ClassLoader {
private byte[] byteCode;
private Map<String, byte[]> byteCodes;
private Map<String, Class<?>> classes = new HashMap<>();
public ReflectLoader(byte[] byteCode) {
byteCodes = new HashMap<>();
this.byteCode = byteCode;
}
public ReflectLoader(String fileName) {
Program program = Resources.getProgram(fileName);
Program tast = Compiler.getFactory().getTastAdapter().getTast(program);
var bc = Compiler.getFactory().getProgramGenerator().generateBytecode(tast);
this.byteCodes = bc;
}
/**
* @param fileName
* @param className
* @return Class<?>
* @throws Exception
*/
public static Class<?> getClass(String fileName, String className) throws Exception {
ReflectLoader loader = new ReflectLoader(fileName);
return loader.findClass(className);
}
public ReflectLoader(Map<String, byte[]> byteCodes) {
this.byteCodes = byteCodes;
}
/**
* @param name
* @return Class<?>
*/
@Override
public Class<?> findClass(String name) {
if (!byteCodes.containsKey(name)) {
if (byteCode != null) {
byteCodes.put(name, byteCode);
byteCode = null;
} else {
return null;
}
}
if (classes.containsKey(name)) {
return classes.get(name);
} else {
Class<?> clazz = defineClass(name, byteCodes.get(name), 0, byteCodes.get(name).length);
classes.put(name, clazz);
return clazz;
}
}
/**
* @param className
* @param method
* @param parameterTypes
* @return Method
* @throws NoSuchMethodException
*/
public Method getMethod(String className, String method, Class<?>... parameterTypes) throws NoSuchMethodException {
Method method1 = findClass(className).getDeclaredMethod(method, parameterTypes);
method1.setAccessible(true);
return method1;
}
/**
* @param className
* @param field
* @return Field
* @throws NoSuchFieldException
*/
public Field getField(String className, String field) throws NoSuchFieldException {
Field field1 = findClass(className).getDeclaredField(field);
field1.setAccessible(true);
return field1;
}
/**
* @param classname
* @param parameterTyped
* @return Constructor<?>
* @throws NoSuchMethodException
*/
public Constructor<?> getConstructor(String classname, Class<?>... parameterTyped) throws NoSuchMethodException {
Constructor<?> constructor = findClass(classname).getDeclaredConstructor(parameterTyped);
constructor.setAccessible(true);
return constructor;
}
}

View File

@ -0,0 +1,42 @@
package Helper;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import common.Compiler;
import syntaxtree.structure.Program;
public class Resources {
/**
* @param fileName
* @return Program
*/
public static Program getProgram(String fileName) {
return Compiler.getFactory().getAstAdapter().getAst(getFileAsStream(fileName));
}
/**
* @param fileName
* @return InputStream
*/
public static InputStream getFileAsStream(String fileName) {
ClassLoader classLoader = Resources.class.getClassLoader();
File file = new File(classLoader.getResource(fileName).getFile());
assertNotNull(file);
try {
return new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
fail();
}
throw new IllegalStateException();
}
}

View File

@ -0,0 +1,324 @@
package TAST;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.FileNotFoundException;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import Helper.MockGenerator;
import Helper.Resources;
import common.AccessModifier;
import common.BaseType;
import common.Compiler;
import common.Primitives;
import common.PrintableVector;
import semantic.exceptions.SemanticError;
import syntaxtree.structure.ClassDecl;
import syntaxtree.structure.ConstructorDecl;
import syntaxtree.structure.FieldDecl;
import syntaxtree.structure.Program;
@DisplayName("Typed Abstract Syntax Generation")
public class TestRunner {
/**
* @throws FileNotFoundException
*/
@Test
@DisplayName("EmptyClass")
void emptyClass() throws FileNotFoundException {
ClassDecl emptyClass = new ClassDecl("EmptyClass", new PrintableVector<>(), new PrintableVector<>(),
new PrintableVector<>());
PrintableVector<ClassDecl> classDecls = new PrintableVector<>();
classDecls.add(emptyClass);
var ast = new Program(classDecls);
var tast = ast;
var generatedTast = Compiler.getFactory().getTastAdapter().getTast(ast);
assertEquals(tast, generatedTast);
}
@Test
@DisplayName("ClassFields")
void classFields() {
Program expectedAst = MockGenerator.getEmptyProgram("ClassFields");
FieldDecl autoAccess = new FieldDecl(AccessModifier.PACKAGE_PRIVATE, new BaseType(Primitives.INT),
"autoAccess");
FieldDecl privateField = new FieldDecl(AccessModifier.PRIVATE, new BaseType(Primitives.INT), "private");
FieldDecl publicField = new FieldDecl(AccessModifier.PUBLIC, new BaseType(Primitives.INT), "public");
FieldDecl protectedField = new FieldDecl(AccessModifier.PROTECTED, new BaseType(Primitives.INT), "protected");
PrintableVector<FieldDecl> fields = expectedAst.getClasses().firstElement().getFieldDelcarations();
fields.add(autoAccess);
fields.add(privateField);
fields.add(publicField);
fields.add(protectedField);
var generatedAst = Compiler.getFactory().getTastAdapter().getTast(expectedAst);
assertEquals(expectedAst, generatedAst);
}
@Test
@DisplayName("ClassField without AccessModifier")
void classFieldWithoutAccessModifier() {
Program expectedTast = MockGenerator.getAutoClassFieldTast();
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(expectedTast);
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("EmptyClassWithConstructor")
void emptyClassWithConstructor() {
PrintableVector<ConstructorDecl> constructors = new PrintableVector<>();
constructors.add(new ConstructorDecl());
ClassDecl classDecl = new ClassDecl("EmptyClassWithConstructor", new PrintableVector<>(), constructors,
new PrintableVector<>());
PrintableVector<ClassDecl> classDecls = new PrintableVector<>();
classDecls.add(classDecl);
var ast = new Program(classDecls);
var generatedTast = Compiler.getFactory().getTastAdapter().getTast(ast);
var tast = ast;
assertEquals(tast, generatedTast);
}
@Test
@DisplayName("Constructor With Parameters")
void constructorWithParameters() {
Program generatedTast = Compiler.getFactory().getTastAdapter()
.getTast(MockGenerator.getConstructorParameterAst());
Program expectedTast = MockGenerator.getConstructorParameterTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("Constructor With this. assign body")
void constructorWithThisAssignBody() {
Program generatedTast = Compiler.getFactory().getTastAdapter()
.getTast(MockGenerator.getConstructorThisDotAst());
Program expectedTast = MockGenerator.getConstructorThisDotTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("VoidMethod")
void voidMethod() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getVoidMethodAst());
Program expectedTast = MockGenerator.getVoidMethodTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("RealConstructor")
void realConstructor() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getRealConstructorAst());
Program expectedTast = MockGenerator.getRealConstructorTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("MethodCall")
void methodCall() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getMethodCallAst());
Program expectedTast = MockGenerator.getMethodCallTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("MultipleFields")
@Tag("expectfail")
void multipleFields() {
Program program = Resources.getProgram("FailTests/MultiFieldDecl.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("MismatchingReturnType")
@Tag("expectfail")
void mismatchingReturnType() {
Program program = Resources.getProgram("FailTests/MismatchingReturnType.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("WhileTest")
void whileTest() {
Program program = Resources.getProgram("SimpleTests/WhileTest.java");
Compiler.getFactory().getTastAdapter().getTast(program);
}
@Test
@DisplayName("WhileFailTest")
void whileFailTest() {
Program program = Resources.getProgram("FailTests/WhileBool.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("ScopeFailTest")
void scopeFailTest() {
Program program = Resources.getProgram("FailTests/ScopeTest.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("AssignFailTest")
void assignFailTest() {
Program program = Resources.getProgram("FailTests/AssignWrongType.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("LocalVarDeclaration with wrong init-Type")
void localVarDeclInitFail() {
Program program = Resources.getProgram("FailTests/LocalVarWrongInit.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("ExplicitNullAssign")
void explicitNullAssign() {
Program program = Resources.getProgram("SimpleTests/ExplicitNullAssign.java");
Compiler.getFactory().getTastAdapter().getTast(program);
}
@Test
@DisplayName("SelfReference")
void selfReference() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getSelfReferenceAst());
Program expectedTast = MockGenerator.getSelfReferenceTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("SystemOutPrintln-Test")
void systemOutPrintlnTest() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getSystemOutPrintlnAst());
Program expectedTast = MockGenerator.getSystemOutPrintlnTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("FieldAccessTests")
void fieldAccessTests() {
Program program = Resources.getProgram("FailTests/FieldAccessTests.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("MethodAccessTests")
void methodAccessTests() {
Program program = Resources.getProgram("FailTests/MethodAccessTests.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("DuplicateMethod")
void duplicateMethod() {
Program program = Resources.getProgram("FailTests/DuplicateMethod.java");
System.err.print(assertThrows(
SemanticError.class,
() -> Compiler.getFactory().getTastAdapter().getTast(program),
"Expected SemanticError to be thrown").getMessage());
}
@Test
@DisplayName("SystemOutPrintln-String-Test")
void systemOutPrintlnStringTest() {
Program generatedTast = Compiler.getFactory().getTastAdapter()
.getTast(MockGenerator.getSystemOutPrintStringAst());
Program expectedTast = MockGenerator.getSystemOutPrintStringTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("MainMethodTest")
void mainMethodTest() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getMainMethodTestAst());
Program expectedTast = MockGenerator.getMainMethodTestTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("ForLoopTest")
void forLoopTest() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getForTestAst());
Program expectedTast = MockGenerator.getForTestTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("IncTest")
void incTest() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getIncTestAst());
Program expectedTast = MockGenerator.getIncTestTast();
assertEquals(expectedTast, generatedTast);
}
@Test
@DisplayName("DecTest")
void decTest() {
Program generatedTast = Compiler.getFactory().getTastAdapter().getTast(MockGenerator.getDecTestAst());
Program expectedTast = MockGenerator.getDecTestTast();
assertEquals(expectedTast, generatedTast);
}
}

View File

@ -0,0 +1,10 @@
import org.junit.platform.suite.api.SelectClasses;
import org.junit.platform.suite.api.Suite;
import org.junit.platform.suite.api.SuiteDisplayName;
@SelectClasses({AST.TestRunner.class, TAST.TestRunner.class, CodeGen.TestRunner.class, All.TestRunner.class})
@Suite
@SuiteDisplayName("House of Compiler")
public class TestSuite {
}

View File

@ -0,0 +1,12 @@
package initialTest;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class initialTest {
@Test
public void testSomething() {
//MyClass myClass = new MyClass();
assertEquals(1, 1);
}
}

View File

@ -0,0 +1,19 @@
class Comments{
private int lorem;
// One Line Comment!!!!! IGNORE THAT
private boolean ipsum;
/*
MULT-LINE COMMENT! IGNORE THIS
*/
/**
*
* Should be ignored
*
*/
}

View File

@ -0,0 +1,7 @@
class EmptyClassWithConstructor{
public EmptyClassWithConstructor(){
}
}

View File

@ -0,0 +1 @@
class EmptyClass{}

View File

@ -0,0 +1,11 @@
package FailTests;
class AssignWrongType {
int test() {
int x = 1;
x = true;
return x;
}
}

View File

@ -0,0 +1,9 @@
package FailTests;
class BoolAssignedInt {
void foo() {
boolean bool = 10;
}
}

View File

@ -0,0 +1,9 @@
package FailTests;
class DivideByZero {
public static void main(String[] args) {
int a = 1 / 0;
}
}

View File

@ -0,0 +1,6 @@
package FailTests;
class DuplicateFieldDeclaration {
char i;
int i;
}

View File

@ -0,0 +1,9 @@
package FailTests;
class DuplicateMethod {
public void test() { }
public void test() { }
}

View File

@ -0,0 +1,11 @@
package FailTests;
class FaultyForLoop {
int foo() {
int x;
for (x == true; ;) {
return 4324;
}
}
}

View File

@ -0,0 +1,18 @@
package FailTests;
class MethodAccessTests {
void MethodAccesTestMethod() {
DontTouchMe clazz = new DontTouchMe();
DontTouchMeMethod();
}
}
class DontTouchMe {
public void DontTouchMeMethod() {
}
}

View File

@ -0,0 +1,9 @@
package FailTests;
class MismatchingReturnType {
char foo(){
return 69;
}
}

View File

@ -0,0 +1,23 @@
package FailTests;
class ScopeTest {
int returnDoesntExist() {
int x = 1;
{
int y = 1;
}
return y;
}
int varInMethodDoesntExist() {
int x = 1;
{
int y = 1;
int x = 2;
}
int y = 1;
return y;
}
}

View File

@ -0,0 +1,20 @@
class IntegerWrapper{
public int i;
public IntegerWrapper(){
this.i = 0;
}
public IntegerWrapper(int i) {
this.i = i;
}
public IntegerWrapper add(IntegerWrapper j){ return new IntegerWrapper(j.i+this.i);}
public IntegerWrapper sub(IntegerWrapper j){ return new IntegerWrapper(j.i-this.i);}
public boolean equals(IntegerWrapper j){return j.i == this.i;}
}

View File

@ -0,0 +1,366 @@
package Integration;
class Dijkstra {
void main() {
Vertex v1 = new Vertex(1);
Graph g = new Graph(v1);
Vertex v2 = new Vertex(2);
Vertex v3 = new Vertex(3);
Vertex v4 = new Vertex(4);
Vertex v5 = new Vertex(5);
Vertex v6 = new Vertex(6);
Vertex v7 = new Vertex(7);
Vertex v8 = new Vertex(8);
Vertex v9 = new Vertex(9);
Vertex v10 = new Vertex(10);
Vertex v11 = new Vertex(11);
Vertex v12 = new Vertex(12);
g.addVertex(v2);
g.addVertex(v4);
g.addVertex(v3);
g.addVertex(v5);
g.addVertex(v6);
g.addVertex(v7);
g.addVertex(v8);
g.addVertex(v9);
g.addVertex(v10);
g.addVertex(v11);
g.addVertex(v12);
g.addEdge(v1, v2); // A-B
g.addEdge(v1, v5); // A-E
g.addEdge(v1, v10); // A-J
g.addEdge(v2, v3); // B
g.addEdge(v2, v7);
g.addEdge(v3, v4); // C
g.addEdge(v3, v5);
g.addEdge(v4, v8); // D
g.addEdge(v5, v6); // E
g.addEdge(v5, v9);
g.addEdge(v6, v7); // F
g.addEdge(v7, v8); // G
g.addEdge(v8, v12); // H
g.addEdge(v8, v10);
g.addEdge(v9, v10); // I
g.addEdge(v10, v11); // J
g.addEdge(v11, v12); // K
g.getShortestPath(v1, v8);
g.getShortestPath(v1, v12);
g.getShortestPath(v4, v12);
g.getShortestPath(v4, v9);
g.getShortestPath(v6, v9);
}
}
class Graph {
private VertexSet vertexList;
public Graph(VertexSet vertexList) {
this.vertexList = vertexList;
}
public Graph(Vertex v1) {
this.vertexList = new VertexSet(v1);
}
public VertexSet getVertexSet() {
return vertexList;
}
public void addVertex(Vertex vertex) {
vertexList.add(vertex);
}
public void addEdge(Vertex vertex1, Vertex vertex2) {
vertex1.addAdjancey(vertex2);
vertex2.addAdjancey(vertex1);
}
public void getShortestPath(Vertex source, Vertex destination) {
VertexSet calcList = vertexList.copy();
calcList.get(source.id).setDistance(0);
calcList.get(source.id).setPrevious(null);
Vertex current = calcList.get(source.id);
// Fill the list with the distances
while (current != null) {
current.setVisited(true);
// Search for every adjacent vertex
VertexSet currentAdjanceyList = current.getAdjanceyList();
if (currentAdjanceyList != null) {
for (int i = 0; i < currentAdjanceyList.size(); i = i + 1) {
Vertex adjancey = currentAdjanceyList.getFromIndex(i);
adjancey = calcList.get(adjancey.id);
if ((adjancey != null) && !adjancey.isVisited()) {
int distance = current.getDistance() + 1;
if (distance < adjancey.getDistance()) {
adjancey.setDistance(distance);
adjancey.setPrevious(current);
}
}
}
} else {
}
current = calcList.getNextSmallestUnvisited();
}
Vertex previous = calcList.get(destination.id);
if (previous == null) {
return;
}
IntStack path = new IntStack(previous.id);
previous = previous.getPrevious();
while (previous != null) {
path.push(previous.id);
previous = previous.getPrevious();
}
}
class Vertex {
public int id;
public VertexSet adjanceyList;
public int distance;
public Vertex previous;
public boolean visited;
public Vertex(int id) {
this.id = id;
}
public void setDistance(int distance) {
this.distance = distance;
}
public void setPrevious(Vertex previous) {
this.previous = previous;
}
public void setVisited(boolean visited) {
this.visited = visited;
}
public int getDistance() {
return distance;
}
public VertexSet getAdjanceyList() {
return adjanceyList;
}
public Vertex getPrevious() {
return previous;
}
public boolean isVisited() {
return visited;
}
public Vertex(Vertex vertex) {
this.id = vertex.id;
this.adjanceyList = vertex.adjanceyList;
this.distance = 10000; // No infinity so...
this.previous = null;
this.visited = false;
}
public void addAdjancey(Vertex v) {
if (adjanceyList == null) {
adjanceyList = new VertexSet(v);
} else {
adjanceyList.add(v);
}
}
public Vertex copy() {
return new Vertex(this);
}
public boolean isDirect(Vertex v) {
return adjanceyList.contains(v);
}
}
class VertexSet {
private Vertex vertex;
private VertexSet next;
public VertexSet(Vertex vertex) {
this.vertex = vertex;
this.next = null;
}
public int size() {
int size = 1;
VertexSet v = next;
while (v != null) {
size = size + 1;
v = v.next;
}
return size;
}
public VertexSet(VertexSet vertexList) {
this.vertex = vertexList.vertex.copy();
if (vertexList.next != null) {
this.next = vertexList.next.copy();
} else {
this.next = null;
}
}
public Vertex getNextSmallestUnvisited() {
VertexSet v = this.getUnvisited();
if (v == null) {
return null;
} else {
return v.getSmallestDistance();
}
}
public Vertex getSmallestDistance() {
Vertex smallest = vertex;
VertexSet v = next;
while (v != null) {
if (v.vertex.getDistance() < smallest.getDistance()) {
smallest = v.vertex;
}
v = v.next;
}
return smallest;
}
public VertexSet getUnvisited() {
VertexSet unvisited = null;
VertexSet current = this;
while (current != null) {
if (!current.vertex.isVisited()) {
if (unvisited == null) {
unvisited = new VertexSet(current.vertex);
} else {
unvisited.add(current.vertex);
}
}
current = current.next;
}
return unvisited;
}
public Vertex getFromIndex(int i) {
if (i == 0) {
return vertex;
} else {
return next.getFromIndex(i - 1);
}
}
public void remove(Vertex v) {
if (vertex.id == v.id) {
vertex = next.vertex;
next = next.next;
} else {
if (next != null) {
next.remove(v);
}
}
}
public boolean hasUnvisited() {
if (next != null) {
return next.hasUnvisited();
} else {
return vertex.isVisited();
}
}
public VertexSet copy() {
return new VertexSet(this);
}
public boolean contains(Vertex v) {
if (vertex.id == v.id) {
return true;
}
if (next == null) {
return false;
}
return next.contains(v);
}
public void add(Vertex vertex) {
if (this.next == null) {
this.next = new VertexSet(vertex);
} else {
this.next.add(vertex);
}
}
public Vertex get(int index) {
if ((vertex == null)) {
return null;
} else if (vertex.id == index) {
return this.vertex;
} else if (next != null) {
return this.next.get(index);
} else {
return null;
}
}
}
class IntStack {
public int c;
public IntStack next;
public int size;
public IntStack(int c) {
this.size = 1;
this.c = c;
this.next = null;
}
public void push(int c) {
size = size + 1;
if (this.next == null) {
this.next = new IntStack(c);
} else {
this.next.push(c);
}
}
public int pop() {
size = size - 1;
if (this.next.next == null) {
int temp = this.next.c;
this.next = null;
return temp;
} else {
return this.next.pop();
}
}
}

View File

@ -0,0 +1,23 @@
package Integration;
// Local or Field or Imported var Test
class LOFOI {
// This test is to determine wheter the order of finding the vars is correct
int System;
public LOFOI() {
System = 10;
}
public int foo() {
int System = 20;
return System;
}
public int bar() {
return System;
}
}

View File

@ -0,0 +1,80 @@
package Integration;
class LinkedList {
public int value;
public LinkedList next;
public int size;
public LinkedList(int value) {
this.value = value;
size = 1;
}
public int get() {
return value;
}
public void set(int value) {
this.value = value;
}
public LinkedList getNode(int i) {
if (i == 0) {
return this;
} else {
return this.next.getNode(i - 1);
}
}
public void swap(int i, int j) {
int temp = getNode(i).get();
getNode(i).set(getNode(j).get());
getNode(j).set(temp);
}
public void add(int value) {
size = size + 1;
if (this.next == null) {
this.next = new LinkedList(value);
} else {
this.next.add(value);
}
}
}
class Executer {
void foo() {
LinkedList list = new LinkedList(3);
list.add(2);
list.add(7);
list.add(5);
list.add(1);
list.add(8);
list.add(6);
list.add(4);
}
LinkedList sort(LinkedList list) {
int i = 0;
int j = 0;
while (i < list.size - 1) {
j = 0;
while (j < list.size - 1) {
if (list.getNode(j).get() > list.getNode(j + 1).get()) {
list.swap(j, j + 1);
}
j = j + 1;
}
i = i + 1;
}
return list;
}
}

View File

@ -0,0 +1,55 @@
package Integration;
class Stack {
private int value;
private Stack next;
public int size;
public Stack(int value) {
this.value = value;
size = 1;
}
public int peek(int i) {
if (i == 0) {
return this.value;
} else {
return this.next.peek(i - 1);
}
}
public void push(int value) {
size = size + 1;
if (this.next == null) {
this.next = new Stack(value);
} else {
this.next.push(value);
}
}
public int pop() {
size = size - 1;
if (this.next == null) {
return this.value;
} else {
return this.next.pop();
}
}
}
class Executer {
void foo() {
Stack stack = new Stack(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
}
}

View File

@ -0,0 +1,48 @@
package Integration;
class StringList {
private char value;
private StringList next;
public StringList() {
}
public StringList(char c) {
this.value = c;
}
public char get(int i) {
if (i == 0) {
return this.value;
} else {
return next.get(i - 1);
}
}
public void add(char value) {
if (next == null) {
next = new StringList(value);
} else {
next.add(value);
}
}
public int length() {
if (this.next == null) {
return 1;
} else {
return 1 + next.length();
}
}
public void print() {
System.out.print(this.value);
if (this.next != null) {
next.print();
} else {
System.out.println();
}
}
}

View File

@ -0,0 +1,31 @@
package NotTested;
class BinaryTests{
private int a,b,c;
public int pvs() {
return a+b*c;
}
public int klammern() {
return a * (b+c);
}
public int modulo(){
return a%b;
}
public boolean avo() {
return a || b && c;
}
public boolean binary() {
return a == b && !c;
}
public int endBoss() {
return (a%c) + b / (a * a) * a + a - (c-b/a) && b-c;
}
}

View File

@ -0,0 +1,19 @@
package NotTested;
class CharWrapper{
private char c;
public CharWrapper(char c){
this.c = c;
}
public void setChar(char c){
this.c = c;
}
public char getChar(){
return this.c;
}
}

View File

@ -0,0 +1,33 @@
package NotTested;
class ClassStress{
private int x = 100;
public ClassStress(int x){
this.x = x*2%2+x/10 % 100*x;
}
}
class ClassStress2{
private int x = 2;
private boolean b = true;
private ClassStress cd = new ClassStress(3);
public ClassStress2(int x, ClassStress cs, boolean b){
this.x = x;
this.b = b;
this.cs = cs
}
public ClassStress2(int x){
this.x = x;
}
public foo(){
return new ClassStress2(1, new ClassStress(this.cd.x), this.b);
}
}

View File

@ -0,0 +1,16 @@
package NotTested;
public class Fibonacci
{
public int fibonacci(int n)
{
if (n <= 1)
{
return n;
}
else
{
return fibonacci(n-1) + fibonacci(n-2);
}
}
}

View File

@ -0,0 +1,37 @@
package NotTested;
class IntegerWrapper{
public int i;
public IntegerWrapper(int i){
this.i = i;
}
public IntegerWrapper(){
this.i = 0;
}
public IntegerWrapper Add(IntegerWrapper j){ return new IntegerWrapper(j.i+this.i)}
public IntegerWrapper Sub(IntegerWrapper j){ return new IntegerWrapper(j.i-this.i)}
public boolean equals(IntegerWrapper j){return j.i == this.i}
}
class Fibonacci
{
public IntegerWrapper fibonacci(IntegerWrapper n)
{
if (n.LessThan(new IntegerWrapper(2)))
{
return n;
}
else
{
return fibonacci(n.Sub(new IntegerWrapper(1))).Add(fibonacci(n.Sub(new IntegerWrapper(2))));
}
}
}

View File

@ -0,0 +1,30 @@
package NotTested;
class IfTest{
public int ifTest(){
if(true){
return 1;
}
return 0;
}
public int ifElseTest(){
if(true){
return 1;
}else{
return 0;
}
}
public int ifElseIfTest(){
if(false){
return 0;
}else if(true){
return 1;
}else{
return 0;
}
}
}

View File

@ -0,0 +1,9 @@
package NotTested;
class TwoClasses{
}
class TwoClasses{
}

View File

@ -0,0 +1,27 @@
package NotTested;
class TwoClassesFilled{
private int i;
public int a = 4;
public TwoClassesFilled(int i){
this.i = i;
}
}
class TwoClassesFilled2{
private TwoClassesFilled a;
public TwoClassesFilled2(int a){
this.a = new TwoClassesFilled(a);
}
public int get(){
return this.a.a;
}
}

View File

@ -0,0 +1,10 @@
package NotTested;
class VoidMethod{
public void test(){
}
}

View File

@ -0,0 +1,10 @@
package NotTested;
class VoidMethodReturn{
public void test(){
return;
}
}

View File

@ -0,0 +1,19 @@
package NotTested;
class WhileTest{
public void infinite(){
while(true){}
}
public int ten(){
int i = 0;
int counter = 10;
while(counter>0){
i = i+1;
counter = counter - 1;
}
return i;
}
}

View File

@ -0,0 +1,25 @@
package NotTested;
class ggt{
private int a;
private int b;
public ggt(int a, int b){
this.a = a;
this.b = b;
}
public int calculate(){
int c;
int a = this.a;
int b = this.b;
while ( b != 0) {
c = a % b;
a = b;
b = c;
}
return a;
}
}

View File

@ -0,0 +1,13 @@
package SimpleTests;
class AndVorOr {
boolean foo(boolean a, boolean b, boolean c) {
return a && b || c && b;
}
boolean bar(boolean a, boolean b, boolean c) {
return a || b && c || b && c;
}
}

View File

@ -0,0 +1,7 @@
package SimpleTests;
class AutoAccessModifierField {
int autoAccess;
}

View File

@ -0,0 +1,15 @@
package SimpleTests;
class CharArgument {
char c;
public CharArgument() {
this.c = foo('a');
}
char foo(char c) {
return c;
}
}

View File

@ -0,0 +1,9 @@
package SimpleTests;
class ClassFields {
private int privateAccess;
public int publicAccess;
protected int protectedAccess;
}

View File

@ -0,0 +1,21 @@
package SimpleTests;
class Comments{
private int lorem;
// One Line Comment!!!!! IGNORE THAT
private boolean ipsum;
/*
MULT-LINE COMMENT! IGNORE THIS
*/
/**
*
* Should be ignored
*
*/
}

View File

@ -0,0 +1,16 @@
package SimpleTests;
class ConditionalEvaluation {
boolean foo() {
System.out.println("foo");
return true || bar();
}
boolean bar() {
System.out.println("bar");
return true && foo();
}
}

View File

@ -0,0 +1,9 @@
package SimpleTests;
class ConstructorParams {
public ConstructorParams(int i) {
}
}

View File

@ -0,0 +1,11 @@
package SimpleTests;
class ConstructorThisDot{
public int i;
public ConstructorThisDot(){
this.i = 5;
}
}

View File

@ -0,0 +1,17 @@
package SimpleTests;
class DecTest {
void foo() {
for (int i = 10; i > 0; i--) {
System.out.println(i);
}
}
void bar() {
for (int i = 10; i > 0; --i) {
System.out.println(i);
}
}
}

View File

@ -0,0 +1,9 @@
package SimpleTests;
class DivMethod {
int foo(int i){
return i/i;
}
}

View File

@ -0,0 +1,3 @@
package SimpleTests;
class EmptyClass{}

View File

@ -0,0 +1,9 @@
package SimpleTests;
class EmptyClassWithConstructor{
public EmptyClassWithConstructor(){
}
}

View File

@ -0,0 +1,10 @@
package SimpleTests;
class ExplicitNullAssign {
ExplicitNullAssign e;
void test() {
e = null;
}
}

View File

@ -0,0 +1,18 @@
package SimpleTests;
class ExtendedNotTest {
boolean notequal(int a, int b) {
return !(a == b);
}
boolean multiple(boolean a, boolean b) {
return !(!a || b);
}
boolean notWithAssigns(boolean a) {
boolean b = !a;
return !b;
}
}

View File

@ -0,0 +1,7 @@
package SimpleTests;
class FieldWithExpr {
public int x = 5;
}

View File

@ -0,0 +1,11 @@
package SimpleTests;
class ForTest {
void foo() {
for (int i = 0; i < 10; i = i + 1) {
System.out.println(i);
}
}
}

View File

@ -0,0 +1,63 @@
package SimpleTests;
class FourClasses {
public int main(int i) {
Test t = new Test(i);
Test2 t2 = new Test2(t.y);
return t2.test.test3.getX();
}
}
class Test {
private int x;
public int y;
public Test3 test3;
public Test(int i) {
this.x = i;
this.y = 10;
this.test3 = new Test3(i * 2);
}
public Test3 getTest3() {
return this.test3;
}
public int getX() {
return this.x;
}
}
class Test2 {
public Test test;
public Test2(int i) {
this.test = new Test(i);
}
}
class Test3 {
public int x;
private int y;
public Test3(int i) {
this.x = i;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
}

View File

@ -0,0 +1,64 @@
package SimpleTests;
class FourClassesFieldAssign {
public int fieldAssign(int i) {
Test t = new Test(i);
Test2 t2 = new Test2(t.y);
t2.test.test3.x = i;
return t2.test.test3.getX();
}
}
class Test {
private int x;
public int y;
public Test3 test3;
public Test(int i) {
this.x = i;
this.y = 10;
this.test3 = new Test3(i * 2);
}
public Test3 getTest3() {
return this.test3;
}
public int getX() {
return this.x;
}
}
class Test2 {
public Test test;
public Test2(int i) {
this.test = new Test(i);
}
}
class Test3 {
public int x;
private int y;
public Test3(int i) {
this.x = i;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
}

View File

@ -0,0 +1,65 @@
package SimpleTests;
class FourClassesSetter {
public int setFieldTest(int i) {
Test t = new Test(i);
Test2 t2 = new Test2(t.y);
t2.test.test3.setY(i);
return t2.test.test3.getY();
}
}
class Test {
private int x;
public int y;
public Test3 test3;
public Test(int i) {
this.x = i;
this.y = 10;
this.test3 = new Test3(i * 2);
}
public Test3 getTest3() {
return this.test3;
}
public int getX() {
return this.x;
}
}
class Test2 {
public Test test;
public Test2(int i) {
this.test = new Test(i);
}
}
class Test3 {
public int x;
private int y;
public Test3(int i) {
this.x = i;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public void setY(int y) {
this.y = y;
}
}

View File

@ -0,0 +1,15 @@
package SimpleTests;
class GetterFunction {
private int i;
public GetterFunction(int i) {
this.i = i;
}
public int getI() {
return this.i;
}
}

View File

@ -0,0 +1,11 @@
package SimpleTests;
class IfConstructor {
int i;
public IfConstructor() {
i = 0;
}
}

View File

@ -0,0 +1,15 @@
package SimpleTests;
class IfElseIfStatement {
int foo(int i) {
if (i == 1) {
return 10;
} else if (i == 2) {
return 40000;
} else {
return 42000;
}
}
}

View File

@ -0,0 +1,17 @@
package SimpleTests;
class IfElseIfStatementWithOneReturn {
int foo(int i) {
int result = 0;
if (i == 1) {
result = 10;
} else if (i == 2) {
return 20;
} else {
result = 30;
}
return result;
}
}

View File

@ -0,0 +1,17 @@
package SimpleTests;
class IfElseIfStatementWithoutReturn {
int foo(int i) {
int result;
if (i == 1) {
result = 10;
} else if (i == 2) {
result = 20;
} else {
result = 30;
}
return result;
}
}

View File

@ -0,0 +1,13 @@
package SimpleTests;
class IfElseStatement {
int foo(int i) {
if (i == 1) {
return 10;
} else {
return 100;
}
}
}

View File

@ -0,0 +1,12 @@
package SimpleTests;
class IfStatement {
int foo(int i) {
if (i == 1) {
return 10;
}
return 100;
}
}

View File

@ -0,0 +1,26 @@
package SimpleTests;
class IncDecStressTest {
int incrementThenReturn(int i) {
return ++i;
}
int returnThenIncrement(int i) {
return i++;
}
int decrementThenReturn(int i) {
return --i;
}
int returnThenDecrement(int i) {
return i--;
}
int callInline(int i) {
i++;
return i;
}
}

View File

@ -0,0 +1,17 @@
package SimpleTests;
class IncTest {
void foo() {
for (int i = 0; i < 10; i++) {
System.out.println(i);
}
}
void bar() {
for (int i = 0; i < 10; ++i) {
System.out.println(i);
}
}
}

View File

@ -0,0 +1,9 @@
package SimpleTests;
class IntCompare {
boolean eq(int a, int b) {
return a == b;
}
}

View File

@ -0,0 +1,9 @@
package SimpleTests;
class IntMethod {
int foo(){
return 1;
}
}

View File

@ -0,0 +1,9 @@
package SimpleTests;
class KlammerVorPunkt {
int foo(int a, int b, int c) {
return (b + c) * a;
}
}

View File

@ -0,0 +1,9 @@
package SimpleTests;
class MainMethodTest {
public static void main(String[] args) {
System.out.println("maintest");
}
}

Some files were not shown because too many files have changed in this diff Show More