This commit is contained in:
Krauß, Josefine 2024-07-05 15:32:43 +02:00
commit 5239e608bc
22 changed files with 666 additions and 246 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 766 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 MiB

View File

@ -1,166 +0,0 @@
Grammatik:
1. Ausdrücke
Primary:
Literal
Variable
IncDecExpression
( Expression )
MethodInvocation
Variable:
Identifier { [ Expression ] }
Identifier:
[ Identifier . ] Name
IncDecExpression:
Variable IncDec
IncDec Variable
IncDec:
++ | --
Expression:
UnaryExpression
BinaryExpression
AssignmentExpression
MethodInvocation
CreationExpression
UnaryExpression:
Primary
UnaryExpression
UnaryOperator
+ | - | ! | ...
BinaryExpression:
Expression BinaryOperator Expression
BinaryOperator:
== | + | - | * | / | & | && | | | || |
AssignmentExpression:
Variable = Expression
MethodInvocation:
Method ( [ ActualArguments ] )
Method:
Identifier
ActualArguments:
Expression { , Expression }
CreationExpression:
new ClassIdentifier ( [ ActualArguments ] )
2. Anweisungen
Statement:
SimpleStatement
CompositeStatement
Label : Statement
SimpleStatement:
EmptyStatement
StatementExpression
EmptyStatement ;
ReturnStatement
StatementExpression:
AssignmentExpression
IncDecExpression
MethodInvocation
CreationExpression
ContinueStatement:
continue [ Name ] ;
ReturnStatement.
return [ Expression ] ;
CompositeStatement:
Block
CaseStatement
Block:
"{" { VariableDeclaration | Statement } "}"
VariableDeclaration:
Type VariableDeclarator ;
VariableDeclarator:
Name [ = Expression ]
Type:
Name # konkrete Typen hinzufügen
Identifier
CaseStatement: # Andere CaseStatements heraussuchen. Assign, MethodCall,
ConditionalStatement
WhileStatement
ConditionalStatement:
if ( Expression ) Statement [ else Statement ]
ConstantExpression:
Expression
WhileStatement:
while ( Expression ) Statement
Initialization:
StatementExpression { , StatementExpression }
VariableDeclaration
3. Methoden
MethodDeclaration:
MethodHeader Block
MethodHeader:
{ Modifier } ResultType MethodDeclarator
Modifier:
public | static | ...
ResultType:
Type | void
MethodDeclarator:
Identifier "(" [ FormalArguments ] ")"
FormalArguments:
FormalArgument { , FormalArgument }
FormalArgument:
Type Name
4. Klassen
Start:
ClassDeclaration {ClassDeclaration}
ClassDeclaration:
[ public ] class Name ClassBody
ClassBody:
"{" { { Modifier } Declaration } "}"
Declaration:
VariableDeclaration
MethodDeclaration
ConstructorDeclaration
ConstructorDeclaration:
[ public ] Name ( [ FormalArguments ] ) Block

View File

@ -1,11 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<module version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src/test/resources" type="java-test-resource" />
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
</component>
</module>

View File

@ -138,7 +138,7 @@ public class BlockStatement extends AbstractType implements IStatement {
&& Objects.equals(statements, blockStatement.statements && Objects.equals(statements, blockStatement.statements
/*&& (Objects.equals(localVars, blockStatement.localVars)*/ /*&& (Objects.equals(localVars, blockStatement.localVars)*/
); );
System.out.println("In ParameterList: " + result); System.out.println("In BlockStatement: " + result);
return result; return result;
} }

View File

@ -70,7 +70,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
boolean result = (Objects.equals(expression, returnStatement.expression) boolean result = (Objects.equals(expression, returnStatement.expression)
&& Objects.equals(expression.getTypeCheckResult(), returnStatement.expression.getTypeCheckResult()) && Objects.equals(expression.getTypeCheckResult(), returnStatement.expression.getTypeCheckResult())
); );
System.out.println("In PrintStatement: " + result); System.out.println("In ReturnStatement: " + result);
return result; return result;
} }

View File

@ -12,12 +12,47 @@ public class testAll {
} }
@Test
public void testAssignWrongType(){
Program ast = AssignWrongTypeAST.getProgram();
String pathToCode = "failTests/AssignWrongType.java";
testAst(ast, pathToCode);
}
@Test
public void testCharArgument(){
Program ast = CharArgumentAST.getProgram();
String pathToCode = "SimpleTests/CharArgument.java";
testAst(ast, pathToCode);
}
@Test @Test
public void testClassWithMain(){ public void testClassWithMain(){
Program ast = ClassWithMainAST.getProgram(); Program ast = ClassWithMainAST.getProgram();
String pathToCode = "basicClasses/classWithMain.java"; String pathToCode = "basicClasses/classWithMain.java";
testAst(ast, pathToCode); testAst(ast, pathToCode);
} }
@Test
public void testConstructorThisDot(){
Program ast = ConstructorThisDotAST.getProgram();
String pathToCode = "SimpleTests/ConstructorThisDot.java";
testAst(ast, pathToCode);
}
@Test
public void testConstructorParams(){
Program ast = ConstructorParamsAST.getProgram();
String pathToCode = "SimpleTests/ConstructorParams.java";
testAst(ast, pathToCode);
}
@Test
public void testDivideByZero(){
Program ast = DivideByZeroAST.getProgram();
String pathToCode = "failTests/DivideByzero.java";
testAst(ast, pathToCode);
}
@Test @Test
public void testEmptyClass(){ public void testEmptyClass(){
Program ast = emptyClassAST.getEmptyProgramm(); Program ast = emptyClassAST.getEmptyProgramm();
@ -40,24 +75,16 @@ public class testAll {
} }
@Test @Test
public void testAssignWrongType(){ public void testFieldVariable(){
Program ast = AssignWrongTypeAST.getProgram(); Program ast = FieldVarAST.getProgram();
String pathToCode = "failTests/AssignWrongType.java"; String pathToCode = "SimpleTests/FieldVar.java";
testAst(ast, pathToCode);
}
@Test
public void testDivideByZero(){
Program ast = DivideByZeroAST.getProgram();
String pathToCode = "failTests/DivideByzero.java";
testAst(ast, pathToCode); testAst(ast, pathToCode);
} }
@Test @Test
public void testcharArgument(){ public void testIfElseIfStatementWithOneReturne(){
Program ast = CharArgumentAST.getProgram(); Program ast = IfElseIfStatementWithOneReturnAST.getProgram();
String pathToCode = "SimpleTests/CharArgument.java"; String pathToCode = "SimpleTests/IfElseIfStatementWithOneReturn.java";
testAst(ast, pathToCode); testAst(ast, pathToCode);
} }

View File

@ -0,0 +1,45 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import java.util.ArrayList;
import java.util.List;
public class ConstructorParamsAST {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
return program;
}
public static RefType getRefType() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType refType = new RefType("ConstructorParams", fieldDeclList, methodDeclList, false);
return refType;
}
public static MethodDecl method0() {
Parameter parameter0 = new Parameter("int", "i");
List<Parameter> parameterList = new ArrayList<>();
parameterList.add(parameter0);
ParameterList parameterListObj = new ParameterList(parameterList);
BlockStatement blockStatement = new BlockStatement(new ArrayList<>(), null);
MethodDecl methodDecl = new MethodDecl("ConstructorParams", null, "ConstructorParams",
parameterListObj, blockStatement);
return methodDecl;
}
}

View File

@ -0,0 +1,59 @@
package ASTs;
import Typecheck.TypingHelper;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ConstructorParamsASTTyped {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
addTyping(program);
return program;
}
public static RefType getRefType() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType refType = new RefType("ConstructorParams", fieldDeclList, methodDeclList, false);
return refType;
}
public static MethodDecl method0() {
Parameter parameter0 = new Parameter("int", "i");
List<Parameter> parameterList = new ArrayList<>();
parameterList.add(parameter0);
ParameterList parameterListObj = new ParameterList(parameterList);
BlockStatement blockStatement = new BlockStatement(new ArrayList<>(), "void");
MethodDecl methodDecl = new MethodDecl("ConstructorParams", null, "ConstructorParams",
parameterListObj, blockStatement);
return methodDecl;
}
public static void addTyping(Program program){
// TypeContext
TypingHelper.addTypeContext(program, "ConstructorParams", new HashMap<>());
// MethodContext
HashMap<String, ParameterList> method0 = new HashMap<>();
//method0.put("void", new ParameterList(new ArrayList<>()));
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
//methods.put("main", method0);
TypingHelper.addMethodContext(program, "ConstructorParams", methods);
}
}

View File

@ -0,0 +1,60 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.InstVarExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Expression.SubReceiver;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import java.util.ArrayList;
import java.util.List;
public class ConstructorThisDotAST {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
return program;
}
public static RefType getRefType() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
fieldDeclList.add(new FieldDecl("int", "i", null));
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType refType = new RefType("ConstructorThisDot", fieldDeclList, methodDeclList, false);
return refType;
}
public static MethodDecl method0() {
List<Parameter> parameterList = new ArrayList<>();
ParameterList parameterListObj = new ParameterList(parameterList);
List<IStatement> iStatementList = new ArrayList<>();
SubReceiver subReceiver = new SubReceiver(true);
List<SubReceiver> subReceiverList = new ArrayList<>();
subReceiverList.add(subReceiver);
InstVarExpression instVarExpressionLeft = new InstVarExpression(subReceiverList, new ArrayList<>(), "i");
IntConstantExpression intConstantExpression = new IntConstantExpression(5);
AssignStatementExpression assignStatementExpression = new AssignStatementExpression("=", instVarExpressionLeft, intConstantExpression);
iStatementList.add(assignStatementExpression);
BlockStatement blockStatement = new BlockStatement(iStatementList, null);
MethodDecl methodDecl = new MethodDecl("ConstructorThisDot", null, "ConstructorThisDot",
parameterListObj, blockStatement);
return methodDecl;
}
}

View File

@ -0,0 +1,77 @@
package ASTs;
import Typecheck.TypingHelper;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.InstVarExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Expression.SubReceiver;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.BlockStatement;
import abstractSyntaxTree.Statement.IStatement;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ConstructorThisDotASTTyped {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
addTyping(program);
return program;
}
public static RefType getRefType() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
fieldDeclList.add(new FieldDecl("int", "i", null));
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType refType = new RefType("ConstructorThisDot", fieldDeclList, methodDeclList, false);
return refType;
}
public static MethodDecl method0() {
List<Parameter> parameterList = new ArrayList<>();
ParameterList parameterListObj = new ParameterList(parameterList);
List<IStatement> iStatementList = new ArrayList<>();
SubReceiver subReceiver = new SubReceiver(true);
List<SubReceiver> subReceiverList = new ArrayList<>();
subReceiverList.add(subReceiver);
InstVarExpression instVarExpressionLeft = new InstVarExpression(subReceiverList, new ArrayList<>(), "i");
instVarExpressionLeft.thisClass = "ConstructorThisDot";
IntConstantExpression intConstantExpression = new IntConstantExpression(5);
AssignStatementExpression assignStatementExpression = new AssignStatementExpression("=", instVarExpressionLeft, intConstantExpression);
iStatementList.add(assignStatementExpression);
BlockStatement blockStatement = new BlockStatement(iStatementList, "void");
MethodDecl methodDecl = new MethodDecl("ConstructorThisDot", null, "ConstructorThisDot",
parameterListObj, blockStatement);
return methodDecl;
}
public static void addTyping(Program program){
// TypeContext
HashMap<String, String> typeContextMap = new HashMap<>();
typeContextMap.put("i", "int");
TypingHelper.addTypeContext(program, "ConstructorThisDot", typeContextMap);
// MethodContext
HashMap<String, ParameterList> method0 = new HashMap<>();
//method0.put("void", new ParameterList(new ArrayList<>()));
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
//methods.put("main", method0);
TypingHelper.addMethodContext(program, "ConstructorThisDot", methods);
}
}

View File

@ -0,0 +1,24 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Program;
import java.util.ArrayList;
import java.util.List;
public class FieldVarAST {
public static Program getProgram() {
// int variable
FieldDecl fieldDecl = new FieldDecl("int", "variable", null);
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
fieldDeclList.add(fieldDecl);
RefType class0 = new RefType("FieldVar", fieldDeclList, methodDeclList, false);
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(class0);
return new Program(refTypeList);
}
}

View File

@ -0,0 +1,43 @@
package ASTs;
import Typecheck.TypingHelper;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class FieldVarASTTyped {
public static Program getProgram() {
// int variable
FieldDecl fieldDecl = new FieldDecl("int", "variable", null);
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
fieldDeclList.add(fieldDecl);
RefType class0 = new RefType("FieldVar", fieldDeclList, methodDeclList, false);
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(class0);
Program program = new Program(refTypeList);
addTyping(program);
return program;
}
public static void addTyping(Program program){
// TypeContext
HashMap<String, String> typeContextMap = new HashMap<>();
typeContextMap.put("variable", "int");
TypingHelper.addTypeContext(program, "FieldVar", typeContextMap);
// MethodContext
HashMap<String, ParameterList> method0 = new HashMap<>();
//method0.put("void", new ParameterList(new ArrayList<>()));
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
//methods.put("main", method0);
TypingHelper.addMethodContext(program, "FieldVar", methods);
}
}

View File

@ -0,0 +1,104 @@
package ASTs;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.BinaryExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.*;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import java.util.ArrayList;
import java.util.List;
public class IfElseIfStatementWithOneReturnAST {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
return program;
}
public static RefType getRefType() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType refType = new RefType("IfElseIfStatementWithOneReturn", fieldDeclList, methodDeclList, false);
return refType;
}
public static MethodDecl method0() {
List<Parameter> parameterList = new ArrayList<>();
parameterList.add(new Parameter("int", "i"));
ParameterList parameterListObj = new ParameterList(parameterList);
List<IStatement> iStatementList = new ArrayList<>();
iStatementList.add(statement00());
iStatementList.add(statement01());
iStatementList.add(statement02());
BlockStatement blockStatement0 = new BlockStatement(iStatementList, null);
MethodDecl methodDecl = new MethodDecl("IfElseIfStatementWithOneReturn", "int", "foo", parameterListObj, blockStatement0);
return methodDecl;
}
public static LocalVarDecl statement00() {
LocalVarDecl localVarDecl = new LocalVarDecl("int", "result", null);
return localVarDecl;
}
public static IfElseStatement statement01() {
LocalVarIdentifier conditionLeft = new LocalVarIdentifier("i");
BinaryExpression condition = new BinaryExpression("==", conditionLeft, new IntConstantExpression(1));
LocalVarIdentifier identifierResult = new LocalVarIdentifier("result");
List<IStatement> ifStatementList = new ArrayList<>();
ifStatementList.add(new AssignStatementExpression("=", identifierResult, new IntConstantExpression(10)));
BlockStatement ifStatement = new BlockStatement(ifStatementList, null);
IfElseStatement elseStatement = statement010();
IfElseStatement ifElseStatement = new IfElseStatement(condition, ifStatement, elseStatement);
return ifElseStatement;
}
public static IfElseStatement statement010() {
LocalVarIdentifier identifieri = new LocalVarIdentifier("i");
BinaryExpression condition0 = new BinaryExpression("==", identifieri, new IntConstantExpression(2));
List<IStatement> iStatementList = new ArrayList<>();
LocalVarIdentifier identifierResult = new LocalVarIdentifier("result");
AssignStatementExpression assignStatementExpression = new AssignStatementExpression("=", identifierResult, new IntConstantExpression(10));
iStatementList.add(assignStatementExpression);
BlockStatement ifStatement = new BlockStatement(iStatementList, null);
BinaryExpression condition1 = new BinaryExpression("==", identifieri, new IntConstantExpression(2));
List<IStatement> iStatementList1 = new ArrayList<>();
iStatementList1.add(new ReturnStatement(new IntConstantExpression(20)));
BlockStatement blockStatementIf = new BlockStatement(iStatementList1, null);
List<IStatement> iStatementList2 = new ArrayList<>();
AssignStatementExpression assignStatementExpression1 = new AssignStatementExpression("=", identifierResult, new IntConstantExpression(30));
iStatementList2.add(assignStatementExpression1);
BlockStatement blockStatementElse = new BlockStatement(iStatementList2, null);
IfElseStatement ifElseStatement = new IfElseStatement(condition1, blockStatementIf, blockStatementElse);
return new IfElseStatement(condition1, blockStatementIf, blockStatementElse);
}
public static ReturnStatement statement02() {
LocalVarIdentifier identifierResult = new LocalVarIdentifier("result");
return new ReturnStatement(identifierResult);
}
}

View File

@ -0,0 +1,135 @@
package ASTs;
import TypeCheck.TypeCheckResult;
import Typecheck.TypingHelper;
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Expression.BinaryExpression;
import abstractSyntaxTree.Expression.IntConstantExpression;
import abstractSyntaxTree.Expression.LocalVarIdentifier;
import abstractSyntaxTree.Parameter.Parameter;
import abstractSyntaxTree.Parameter.ParameterList;
import abstractSyntaxTree.Program;
import abstractSyntaxTree.Statement.*;
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class IfElseIfStatementWithOneReturnASTTyped {
public static Program getProgram() {
List<RefType> refTypeList = new ArrayList<>();
refTypeList.add(getRefType());
Program program = new Program(refTypeList);
addTyping(program);
return program;
}
public static RefType getRefType() {
List<FieldDecl> fieldDeclList = new ArrayList<>();
List<MethodDecl> methodDeclList = new ArrayList<>();
methodDeclList.add(method0());
RefType refType = new RefType("IfElseIfStatementWithOneReturn", fieldDeclList, methodDeclList, false);
return refType;
}
public static MethodDecl method0() {
List<Parameter> parameterList = new ArrayList<>();
parameterList.add(new Parameter("int", "i"));
ParameterList parameterListObj = new ParameterList(parameterList);
List<IStatement> iStatementList = new ArrayList<>();
iStatementList.add(statement00());
iStatementList.add(statement01());
iStatementList.add(statement02());
BlockStatement blockStatement0 = new BlockStatement(iStatementList, "int");
MethodDecl methodDecl = new MethodDecl("IfElseIfStatementWithOneReturn", "int", "foo", parameterListObj, blockStatement0);
return methodDecl;
}
public static LocalVarDecl statement00() {
LocalVarDecl localVarDecl = new LocalVarDecl("int", "result", null);
return localVarDecl;
}
public static IfElseStatement statement01() {
LocalVarIdentifier conditionLeft = new LocalVarIdentifier("i");
conditionLeft.setTypeCheckResult(new TypeCheckResult("int"));
BinaryExpression condition = new BinaryExpression("==", conditionLeft, new IntConstantExpression(1));
condition.setTypeCheckResult(new TypeCheckResult("boolean"));
LocalVarIdentifier identifierResult = new LocalVarIdentifier("result");
identifierResult.setTypeCheckResult(new TypeCheckResult("int"));
List<IStatement> ifStatementList = new ArrayList<>();
ifStatementList.add(new AssignStatementExpression("=", identifierResult, new IntConstantExpression(10)));
BlockStatement ifStatement = new BlockStatement(ifStatementList, "void");
IfElseStatement elseStatement = statement010();
IfElseStatement ifElseStatement = new IfElseStatement(condition, ifStatement, elseStatement);
return ifElseStatement;
}
public static IfElseStatement statement010() {
LocalVarIdentifier identifieri = new LocalVarIdentifier("i");
identifieri.setTypeCheckResult(new TypeCheckResult("int"));
BinaryExpression condition0 = new BinaryExpression("==", identifieri, new IntConstantExpression(2));
condition0.setTypeCheckResult(new TypeCheckResult("boolean"));
List<IStatement> iStatementList = new ArrayList<>();
LocalVarIdentifier identifierResult = new LocalVarIdentifier("result");
identifierResult.setTypeCheckResult(new TypeCheckResult("int"));
AssignStatementExpression assignStatementExpression = new AssignStatementExpression("=", identifierResult, new IntConstantExpression(10));
iStatementList.add(assignStatementExpression);
BlockStatement ifStatement = new BlockStatement(iStatementList, "void");
BinaryExpression condition1 = new BinaryExpression("==", identifieri, new IntConstantExpression(2));
condition1.setTypeCheckResult(new TypeCheckResult("boolean"));
List<IStatement> iStatementList1 = new ArrayList<>();
IntConstantExpression intConstantExpression20 = new IntConstantExpression(20);
intConstantExpression20.setTypeCheckResult(new TypeCheckResult("int"));
ReturnStatement return20 = new ReturnStatement(intConstantExpression20);
return20.setTypeCheckResult(new TypeCheckResult("int"));
iStatementList1.add(return20);
BlockStatement blockStatementIf = new BlockStatement(iStatementList1, "int");
List<IStatement> iStatementList2 = new ArrayList<>();
AssignStatementExpression assignStatementExpression1 = new AssignStatementExpression("=", identifierResult, new IntConstantExpression(30));
iStatementList2.add(assignStatementExpression1);
BlockStatement blockStatementElse = new BlockStatement(iStatementList2, "void");
IfElseStatement ifElseStatement = new IfElseStatement(condition1, blockStatementIf, blockStatementElse);
return new IfElseStatement(condition1, blockStatementIf, blockStatementElse);
}
public static ReturnStatement statement02() {
LocalVarIdentifier identifierResult = new LocalVarIdentifier("result");
identifierResult.setTypeCheckResult(new TypeCheckResult("int"));
return new ReturnStatement(identifierResult);
}
public static void addTyping(Program program){
// TypeContext
TypingHelper.addTypeContext(program, "IfElseIfStatementWithOneReturn", new HashMap<>());
// MethodContext
HashMap<String, ParameterList> method0 = new HashMap<>();
List<Parameter> parameterList = new ArrayList<>();
parameterList.add(new Parameter("int", "i"));
ParameterList parameterListObj = new ParameterList(parameterList);
method0.put("int", parameterListObj);
HashMap<String, HashMap<String, ParameterList>> methods = new HashMap<>();
methods.put("foo", method0);
TypingHelper.addMethodContext(program, "IfElseIfStatementWithOneReturn", methods);
}
}

View File

@ -77,7 +77,6 @@ public class ByteCodeTester {
private Program generateAST(String pathToJavaFile){ private Program generateAST(String pathToJavaFile){
String content = ""; String content = "";
try { try {
System.out.println("Classpath: " + Path.of(pathToJavaFile));
content = Files.readString(Path.of(pathToJavaFile)); content = Files.readString(Path.of(pathToJavaFile));
} catch (IOException e) { } catch (IOException e) {
System.out.println("File not found!"); System.out.println("File not found!");
@ -115,7 +114,6 @@ public class ByteCodeTester {
public void compareJarFilesFromScratch(String javaCodePath, String jarPath, String[] classNames){ public void compareJarFilesFromScratch(String javaCodePath, String jarPath, String[] classNames){
String content = ""; String content = "";
try { try {
System.out.println("Classpath: " + Path.of(javaCodePath));
content = Files.readString(Path.of(javaCodePath)); content = Files.readString(Path.of(javaCodePath));
} catch (IOException e) { } catch (IOException e) {
System.out.println("File not found!"); System.out.println("File not found!");

View File

@ -98,7 +98,7 @@ public class CompareByteCodeBehaviour {
Method method1 = methods1[i]; Method method1 = methods1[i];
Method method2 = methods2[i]; Method method2 = methods2[i];
System.out.println(method1); System.out.println("\n" + method1);
System.out.println(method2); System.out.println(method2);
method1.setAccessible(true); method1.setAccessible(true);
@ -108,7 +108,7 @@ public class CompareByteCodeBehaviour {
// Test with some sample inputs // Test with some sample inputs
Object[] testInputs = getTestInputs(method1.getParameterTypes()); Object[] testInputs = getTestInputs(method1.getParameterTypes());
for(int j = 0; j < testInputs.length; j++){ for(int j = 0; j < testInputs.length; j++){
System.out.println("TestInput: " + testInputs[j]); System.out.println("Parameter[" + i + "]: " + testInputs[j]);
} }
Object result1 = method1.invoke(obj1, testInputs); Object result1 = method1.invoke(obj1, testInputs);

View File

@ -35,11 +35,11 @@ public class TestAll {
@Test @Test
public void testFakultaet() { public void testFakultaet() {
String classPath = "src/test/resources/basicClasses/Fakultaet.class"; String classPath = "src/test/resources/basicClasses/Fakultaet.class";
Program ast = FakultaetASTTyped.getProgram(); Program ast = FakultaetAST.getProgram();
String className = "Fakultaet"; String className = "Fakultaet";
String javacode = "src/test/resources/basicClasses/Fakultaet.java"; String javacode = "src/test/resources/basicClasses/Fakultaet.java";
//byteCodeTester.testByteCodeFromTypedAst(classPath, ast ,className); byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className); //byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
} }
@Test @Test
@ -92,15 +92,6 @@ public class TestAll {
byteCodeTester.testClassFileFromScratch(classPath, javacode, className); byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
} }
@Test
public void testDivMethod() {
String classPath = "src/test/resources/SimpleTests/DivMethod.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "DivMethod";
String javacode = "src/test/resources/SimpleTests/DivMethod.java";
//testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
// We dont do null assignments // We dont do null assignments
/* /*
@ -118,10 +109,10 @@ public class TestAll {
@Test @Test
public void testFieldVar() { public void testFieldVar() {
String classPath = "src/test/resources/SimpleTests/FieldVar.class"; String classPath = "src/test/resources/SimpleTests/FieldVar.class";
//Program ast = ClassWithMainASTTyped.getProgram(); Program ast = FieldVarASTTyped.getProgram();
String className = "FieldVar"; String className = "FieldVar";
String javacode = "src/test/resources/SimpleTests/FieldVar.java"; String javacode = "src/test/resources/SimpleTests/FieldVar.java";
//testByteCodeFromAst(classPath, ast ,className); byteCodeTester.testByteCodeFromTypedAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className); byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
} }
@ -135,6 +126,7 @@ public class TestAll {
byteCodeTester.testClassFileFromScratch(classPath, javacode, className); byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
} }
@Test @Test
public void testGetterFunction() { public void testGetterFunction() {
String classPath = "src/test/resources/SimpleTests/GetterFunction.class"; String classPath = "src/test/resources/SimpleTests/GetterFunction.class";
@ -145,6 +137,16 @@ public class TestAll {
byteCodeTester.testClassFileFromScratch(classPath, javacode, className); byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
} }
@Test
public void testDivMethod() {
String classPath = "src/test/resources/SimpleTests/DivMethod.class";
//Program ast = ClassWithMainASTTyped.getProgram();
String className = "DivMethod";
String javacode = "src/test/resources/SimpleTests/DivMethod.java";
//byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
}
@Test @Test
public void testIfElseIfStatement() { public void testIfElseIfStatement() {
String classPath = "src/test/resources/SimpleTests/IfElseIfStatement.class"; String classPath = "src/test/resources/SimpleTests/IfElseIfStatement.class";
@ -158,10 +160,10 @@ public class TestAll {
@Test @Test
public void testIfElseIfStatementWithOneReturn() { public void testIfElseIfStatementWithOneReturn() {
String classPath = "src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.class"; String classPath = "src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.class";
//Program ast = ClassWithMainASTTyped.getProgram(); Program ast = IfElseIfStatementWithOneReturnAST.getProgram();
String className = "IfElseIfStatementWithOneReturn"; String className = "IfElseIfStatementWithOneReturn";
String javacode = "src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.java"; String javacode = "src/test/resources/SimpleTests/IfElseIfStatementWithOneReturn.java";
//testByteCodeFromAst(classPath, ast ,className); byteCodeTester.testByteCodeFromAst(classPath, ast ,className);
byteCodeTester.testClassFileFromScratch(classPath, javacode, className); byteCodeTester.testClassFileFromScratch(classPath, javacode, className);
} }
@ -185,17 +187,6 @@ public class TestAll {
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames); byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
} }
/*
@Test
public void testDijkstra() {
String jarPath = "src/test/resources/Integration/Dijkstra.jar";
//Program ast = ClassWithMainASTTyped.getProgram();
String[] classNames = {"FourClasses", "Test", "Test2", "Test3"};
String javacode = "src/test/resources/Integration/Dijkstra.java";
byteCodeTester.compareJarFilesFromScratch(javacode, jarPath, classNames);
}
*/

View File

@ -21,44 +21,44 @@ public class TestAll {
@Test @Test
public void testClassWithMain() { public void testClassWithMain() {
Program emptyClassAst = ClassWithMainAST.getProgram(); Program astToBeTested = ClassWithMainAST.getProgram();
Program correctAST = ClassWithMainASTTyped.getProgram(); Program correctAST = ClassWithMainASTTyped.getProgram();
boolean expectedResult = true; boolean expectedResult = true;
testTypeCheck(emptyClassAst, correctAST, expectedResult); testTypeCheck(astToBeTested, correctAST, expectedResult);
} }
@Test @Test
public void testEmptyClass() { public void testEmptyClass() {
Program emptyClassAst = emptyClassAST.getEmptyProgramm(); Program astToBeTested = emptyClassAST.getEmptyProgramm();
Program correctAST = emptyClassASTTyped.getEmptyProgramm(); Program correctAST = emptyClassASTTyped.getEmptyProgramm();
boolean expectedResult = false; boolean expectedResult = false;
testTypeCheck(emptyClassAst, correctAST, expectedResult); testTypeCheck(astToBeTested, correctAST, expectedResult);
} }
@Test @Test
public void testEmptyClassWM() { public void testEmptyClassWM() {
Program emptyClassAst = emptyClassAST.getEmptyProgramm(); Program astToBeTested = emptyClassAST.getEmptyProgramm();
Program correctAST = emptyClassASTTyped.getEmptyProgramm(); Program correctAST = emptyClassASTTyped.getEmptyProgramm();
boolean expectedResult = true; boolean expectedResult = true;
testTypeCheckWM(emptyClassAst, correctAST, expectedResult); testTypeCheckWOM(astToBeTested, correctAST, expectedResult);
} }
@Test @Test
public void testEmptyClassWithConstructor(){ public void testEmptyClassWithConstructor(){
Program abstractSyntaxTree = EmptyClassWithConstructorAST.getProgram(); Program astToBeTested = EmptyClassWithConstructorAST.getProgram();
Program correctAST = EmptyClassWithConstructorASTTyped.getProgram(); Program correctAST = EmptyClassWithConstructorASTTyped.getProgram();
boolean expectedResult = false; boolean expectedResult = false;
testTypeCheck(abstractSyntaxTree, correctAST, expectedResult); testTypeCheck(astToBeTested, correctAST, expectedResult);
} }
@Test @Test
public void testEmptyClassWithConstructorWM(){ public void testEmptyClassWithConstructorWM(){
Program abstractSyntaxTree = EmptyClassWithConstructorAST.getProgram(); Program astToBeTested = EmptyClassWithConstructorAST.getProgram();
Program correctAST = EmptyClassWithConstructorASTTyped.getProgram(); Program correctAST = EmptyClassWithConstructorASTTyped.getProgram();
boolean expectedResult = true; boolean expectedResult = true;
testTypeCheckWM(abstractSyntaxTree, correctAST, expectedResult); testTypeCheckWOM(astToBeTested, correctAST, expectedResult);
} }
/* /*
@ -79,44 +79,78 @@ public class TestAll {
@Test @Test
public void testAssignWrongType(){ public void testAssignWrongType(){
Program assignWrongType = AssignWrongTypeAST.getProgram(); Program astToBeTested = AssignWrongTypeAST.getProgram();
Program correctAST = AssignWrongTypeASTTyped.getProgram(); Program correctAST = AssignWrongTypeASTTyped.getProgram();
boolean expectedResult = false; boolean expectedResult = false;
testTypeCheck(assignWrongType, correctAST,expectedResult); testTypeCheck(astToBeTested, correctAST,expectedResult);
} }
@Test @Test
public void testAssignWrongTypeWM(){ public void testAssignWrongTypeWM(){
Program assignWrongType = AssignWrongTypeAST.getProgram(); Program astToBeTested = AssignWrongTypeAST.getProgram();
Program correctAST = AssignWrongTypeASTTyped.getProgram(); Program correctAST = AssignWrongTypeASTTyped.getProgram();
boolean expectedResult = false; boolean expectedResult = false;
testTypeCheckWM(assignWrongType, correctAST, expectedResult); testTypeCheckWOM(astToBeTested, correctAST, expectedResult);
} }
@Test @Test
public void testFakultaet(){ public void testFakultaet(){
Program fakultaet = FakultaetAST.getProgram(); Program astToBeTested = FakultaetAST.getProgram();
Program correctAST = FakultaetASTTyped.getProgram(); Program correctAST = FakultaetASTTyped.getProgram();
boolean expectedResult = true; boolean expectedResult = true;
testTypeCheck(fakultaet, correctAST, expectedResult); testTypeCheck(astToBeTested, correctAST, expectedResult);
} }
@Test @Test
public void testDivideByZero(){ public void testDivideByZero(){
Program fakultaet = DivideByZeroAST.getProgram(); Program astToBeTested = DivideByZeroAST.getProgram();
Program correctAST = DivideByZeroASTTyped.getProgram(); Program correctAST = DivideByZeroASTTyped.getProgram();
boolean expectedResult = true; boolean expectedResult = true;
testTypeCheck(fakultaet, correctAST, expectedResult); testTypeCheck(astToBeTested, correctAST, expectedResult);
} }
@Test @Test
public void testCharArgument(){ public void testCharArgument(){
Program fakultaet = CharArgumentAST.getProgram(); Program astToBeTested = CharArgumentAST.getProgram();
Program correctAST = CharArgumentASTTyped.getProgram(); Program correctAST = CharArgumentASTTyped.getProgram();
boolean expectedResult = true; boolean expectedResult = true;
testTypeCheckWOM(fakultaet, correctAST, expectedResult); testTypeCheckWOM(astToBeTested, correctAST, expectedResult);
} }
@Test
public void testFieldVar(){
Program astToBeTested = FieldVarAST.getProgram();
Program correctAST = FieldVarASTTyped.getProgram();
boolean expectedResult = true;
testTypeCheckWOM(astToBeTested, correctAST, expectedResult);
}
@Test
public void testConstructorParams(){
Program astToBeTested = ConstructorParamsAST.getProgram();
Program correctAST = ConstructorParamsASTTyped.getProgram();
boolean expectedResult = true;
testTypeCheckWOM(astToBeTested, correctAST, expectedResult);
}
@Test
public void testConstructorThisDot(){
Program astToBeTested = ConstructorThisDotAST.getProgram();
Program correctAST = ConstructorThisDotASTTyped.getProgram();
boolean expectedResult = true;
testTypeCheckWOM(astToBeTested, correctAST, expectedResult);
}
@Test
public void testIfElseIfStatementWithOneReturn(){
Program astToBeTested = IfElseIfStatementWithOneReturnAST.getProgram();
Program correctAST = IfElseIfStatementWithOneReturnASTTyped.getProgram();
boolean expectedResult = true;
testTypeCheckWOM(astToBeTested, correctAST, expectedResult);
}
public void testTypeCheckWOM(Program abstractSyntaxTree, Program correctAST, boolean expectedResult) { public void testTypeCheckWOM(Program abstractSyntaxTree, Program correctAST, boolean expectedResult) {
try { try {

View File

@ -1,5 +1,5 @@
class FieldVar { class FieldVar {
int field; int variable;
} }

View File

@ -2,7 +2,7 @@ Class: "class"
Identifier: "FieldVar" Identifier: "FieldVar"
OpenCurlyBracket: "{" OpenCurlyBracket: "{"
Int: "int" Int: "int"
Identifier: "field" Identifier: "variable"
Semicolon: ";" Semicolon: ";"
ClosedCurlyBracket: "}" ClosedCurlyBracket: "}"
EOF: "<EOF>" EOF: "<EOF>"