Compare commits
2 Commits
6bf2c75e02
...
aa7d82b9ac
Author | SHA1 | Date | |
---|---|---|---|
|
aa7d82b9ac | ||
|
94ea539fab |
@ -1,5 +1,5 @@
|
|||||||
package AST;
|
package AST;
|
||||||
import ASTs.emptyClassAST;
|
import ASTs.*;
|
||||||
import abstractSyntaxTree.Program;
|
import abstractSyntaxTree.Program;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -8,7 +8,7 @@ public class testAll {
|
|||||||
AstComparer astComparer;
|
AstComparer astComparer;
|
||||||
public testAll()
|
public testAll()
|
||||||
{
|
{
|
||||||
this.astComparer = new AstComparer("src/test/resources");
|
this.astComparer = new AstComparer("test/resources");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -20,10 +20,24 @@ public class testAll {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyClassWithConstructor(){
|
public void testEmptyClassWithConstructor(){
|
||||||
Program ast = emptyClassAST.getEmptyProgramm();
|
Program ast = EmptyClassWithConstructorAST.getProgram();
|
||||||
String pathToCode = "basicClasses/emptyClassWithConstructor.java";
|
String pathToCode = "basicClasses/emptyClassWithConstructor.java";
|
||||||
testAst(ast, pathToCode);
|
testAst(ast, pathToCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFakultaet(){
|
||||||
|
Program ast = FakultaetAST.getProgram();
|
||||||
|
String pathToCode = "basicClasses/Fakultaet.java";
|
||||||
|
testAst(ast, pathToCode);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testAssignWrongType(){
|
||||||
|
Program ast = AssignWrongTypeAST.getProgram();
|
||||||
|
String pathToCode = "failTests/AssignWrongType.java";
|
||||||
|
testAst(ast, pathToCode);
|
||||||
|
}
|
||||||
public void testAst(Program ast, String pathToCode)
|
public void testAst(Program ast, String pathToCode)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
59
src/test/java/ASTs/AssignWrongTypeAST.java
Normal file
59
src/test/java/ASTs/AssignWrongTypeAST.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package ASTs;
|
||||||
|
|
||||||
|
import abstractSyntaxTree.Class.FieldDecl;
|
||||||
|
import abstractSyntaxTree.Class.MethodDecl;
|
||||||
|
import abstractSyntaxTree.Class.RefType;
|
||||||
|
import abstractSyntaxTree.Expression.BooleanConstantExpression;
|
||||||
|
import abstractSyntaxTree.Expression.IntConstantExpression;
|
||||||
|
import abstractSyntaxTree.Expression.LocalVarIdentifier;
|
||||||
|
import abstractSyntaxTree.Parameter.ParameterList;
|
||||||
|
import abstractSyntaxTree.Program;
|
||||||
|
import abstractSyntaxTree.Statement.BlockStatement;
|
||||||
|
import abstractSyntaxTree.Statement.IStatement;
|
||||||
|
import abstractSyntaxTree.Statement.LocalVarDecl;
|
||||||
|
import abstractSyntaxTree.Statement.ReturnStatement;
|
||||||
|
import abstractSyntaxTree.StatementExpression.AssignStatementExpression;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class AssignWrongTypeAST {
|
||||||
|
public static Program getProgram(){
|
||||||
|
|
||||||
|
// TestMethod
|
||||||
|
//Expressions
|
||||||
|
// 0
|
||||||
|
IntConstantExpression intConstantExpression0 = new IntConstantExpression(1);
|
||||||
|
LocalVarDecl localVarDecl = new LocalVarDecl("int", "x", intConstantExpression0);
|
||||||
|
|
||||||
|
//1
|
||||||
|
LocalVarIdentifier localVarIdentifier0 = new LocalVarIdentifier("x");
|
||||||
|
BooleanConstantExpression booleanConstantExpression0 = new BooleanConstantExpression(true);
|
||||||
|
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", localVarIdentifier0, booleanConstantExpression0);
|
||||||
|
|
||||||
|
//2
|
||||||
|
ReturnStatement returnStatement0 = new ReturnStatement(localVarIdentifier0);
|
||||||
|
|
||||||
|
String classThatContainsMethod = "AssignWrongType";
|
||||||
|
String nameMethod0 = "test";
|
||||||
|
String returnType = "int";
|
||||||
|
ParameterList parameterList = new ParameterList(new ArrayList<>());
|
||||||
|
List<IStatement> iStatementList = new ArrayList<>();
|
||||||
|
iStatementList.add(localVarDecl);
|
||||||
|
iStatementList.add(assignStatementExpression0);
|
||||||
|
iStatementList.add(returnStatement0);
|
||||||
|
BlockStatement codeBlock = new BlockStatement(iStatementList, null);
|
||||||
|
MethodDecl test = new MethodDecl(classThatContainsMethod, returnType, nameMethod0, parameterList, codeBlock);
|
||||||
|
|
||||||
|
String name = "AssignWrongType";
|
||||||
|
List<FieldDecl> fieldDeclList = new ArrayList<>();
|
||||||
|
List<MethodDecl> methodDeclList = new ArrayList<>();
|
||||||
|
methodDeclList.add(test);
|
||||||
|
boolean hasMain = false;
|
||||||
|
RefType assignWrongType = new RefType(name, fieldDeclList, methodDeclList, hasMain);
|
||||||
|
List<RefType> refTypeList = new ArrayList<>();
|
||||||
|
refTypeList.add(assignWrongType);
|
||||||
|
Program program = new Program(refTypeList);
|
||||||
|
return program;
|
||||||
|
}
|
||||||
|
}
|
@ -3,7 +3,6 @@ package ASTs;
|
|||||||
import abstractSyntaxTree.Class.FieldDecl;
|
import abstractSyntaxTree.Class.FieldDecl;
|
||||||
import abstractSyntaxTree.Class.MethodDecl;
|
import abstractSyntaxTree.Class.MethodDecl;
|
||||||
import abstractSyntaxTree.Class.RefType;
|
import abstractSyntaxTree.Class.RefType;
|
||||||
import abstractSyntaxTree.Parameter.Parameter;
|
|
||||||
import abstractSyntaxTree.Parameter.ParameterList;
|
import abstractSyntaxTree.Parameter.ParameterList;
|
||||||
import abstractSyntaxTree.Program;
|
import abstractSyntaxTree.Program;
|
||||||
import abstractSyntaxTree.Statement.BlockStatement;
|
import abstractSyntaxTree.Statement.BlockStatement;
|
||||||
@ -12,23 +11,21 @@ import abstractSyntaxTree.Statement.IStatement;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class emptyClassWithConstructorAST extends Program {
|
public class EmptyClassWithConstructorAST {
|
||||||
public emptyClassWithConstructorAST()
|
public static Program getProgram() {
|
||||||
{
|
|
||||||
super(staticClasses());
|
|
||||||
}
|
|
||||||
private static List<RefType> staticClasses() {
|
|
||||||
List<FieldDecl> fieldDeclList = new ArrayList<>();
|
List<FieldDecl> fieldDeclList = new ArrayList<>();
|
||||||
|
|
||||||
ParameterList emptyParameterList = new ParameterList(new ArrayList<>());
|
ParameterList emptyParameterList = new ParameterList(new ArrayList<>());
|
||||||
List<IStatement> emptyIStatementList = new ArrayList<>();
|
List<IStatement> emptyIStatementList = new ArrayList<>();
|
||||||
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, "void");
|
BlockStatement emptyBlockStatement = new BlockStatement(emptyIStatementList, null);
|
||||||
MethodDecl constructor = new MethodDecl("emptyClassWithConstructor", "null", "emptyClassWithConstructor", emptyParameterList, emptyBlockStatement);
|
MethodDecl constructor = new MethodDecl("EmptyClassWithConstructor", null, "EmptyClassWithConstructor", emptyParameterList, emptyBlockStatement);
|
||||||
List<MethodDecl> methodDeclList = new ArrayList<>();
|
List<MethodDecl> methodDeclList = new ArrayList<>();
|
||||||
methodDeclList.add(constructor);
|
methodDeclList.add(constructor);
|
||||||
RefType emptyClass = new RefType("emptyClass", fieldDeclList, methodDeclList, false);
|
RefType emptyClass = new RefType("EmptyClassWithConstructor", fieldDeclList, methodDeclList, false);
|
||||||
List<RefType> classes = new ArrayList<>();
|
List<RefType> classes = new ArrayList<>();
|
||||||
classes.add(emptyClass);
|
classes.add(emptyClass);
|
||||||
return classes;
|
|
||||||
|
Program program = new Program(classes);
|
||||||
|
return program;
|
||||||
}
|
}
|
||||||
}
|
}
|
106
src/test/java/ASTs/FakultaetAST.java
Normal file
106
src/test/java/ASTs/FakultaetAST.java
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
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 FakultaetAST {
|
||||||
|
public static Program getProgram() {
|
||||||
|
|
||||||
|
String name = "Fakultaet";
|
||||||
|
List<FieldDecl> fieldDeclList = new ArrayList<>();
|
||||||
|
List<MethodDecl> methodDeclList = new ArrayList<>();
|
||||||
|
methodDeclList.add(method0());
|
||||||
|
methodDeclList.add(method1());
|
||||||
|
List<RefType> classes = new ArrayList<>();
|
||||||
|
RefType fakultaet = new RefType(name, fieldDeclList, methodDeclList, true);
|
||||||
|
classes.add(fakultaet);
|
||||||
|
Program program = new Program(classes);
|
||||||
|
|
||||||
|
return program;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MethodDecl method0() {
|
||||||
|
String classThatContainsMethod = "Fakultaet";
|
||||||
|
String name = "main";
|
||||||
|
ParameterList parameterList = new ParameterList(new ArrayList<>());
|
||||||
|
String returnType = "void";
|
||||||
|
List<IStatement> iStmtList = new ArrayList<>();
|
||||||
|
BlockStatement blockStatement = new BlockStatement(iStmtList, null);
|
||||||
|
return new MethodDecl(classThatContainsMethod, returnType, name, parameterList, blockStatement);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static MethodDecl method1() {
|
||||||
|
String classThatContainsMethod = "Fakultaet";
|
||||||
|
String name = "fak";
|
||||||
|
// Parameters
|
||||||
|
Parameter param0 = new Parameter("int", "number");
|
||||||
|
List<Parameter> paramList = new ArrayList<>() {{
|
||||||
|
add(param0);
|
||||||
|
}};
|
||||||
|
ParameterList parameterListObj = new ParameterList(paramList);
|
||||||
|
|
||||||
|
String returnType = "int";
|
||||||
|
|
||||||
|
// Block Statement
|
||||||
|
List<IStatement> iStmtList0 = new ArrayList<>();
|
||||||
|
// If Statament
|
||||||
|
//condition
|
||||||
|
LocalVarIdentifier binExprIdentifier = new LocalVarIdentifier("number");
|
||||||
|
IntConstantExpression binExprIntConst = new IntConstantExpression(0);
|
||||||
|
BinaryExpression binExpr = new BinaryExpression("<", binExprIdentifier, binExprIntConst);
|
||||||
|
|
||||||
|
IntConstantExpression return0Expr0 = new IntConstantExpression(1);
|
||||||
|
ReturnStatement returnStatement0 = new ReturnStatement(return0Expr0);
|
||||||
|
List<IStatement> iStmtList1 = new ArrayList<>();
|
||||||
|
iStmtList1.add(returnStatement0);
|
||||||
|
BlockStatement ifStmt = new BlockStatement(iStmtList1, null);
|
||||||
|
IfStatement ifStatement = new IfStatement(binExpr, ifStmt);
|
||||||
|
|
||||||
|
iStmtList0.add(ifStatement);
|
||||||
|
|
||||||
|
// Expression 1
|
||||||
|
IntConstantExpression intConst0 = new IntConstantExpression(1);
|
||||||
|
LocalVarDecl localVarExpr0 = new LocalVarDecl("int", "factorial", intConst0);
|
||||||
|
|
||||||
|
iStmtList0.add(localVarExpr0);
|
||||||
|
// Expression 2
|
||||||
|
IntConstantExpression intConst1 = new IntConstantExpression(0);
|
||||||
|
LocalVarDecl localVarExpr1 = new LocalVarDecl("int", "i", intConst1);
|
||||||
|
|
||||||
|
iStmtList0.add(localVarExpr1);
|
||||||
|
|
||||||
|
//While Statement
|
||||||
|
BinaryExpression whileCondition = new BinaryExpression("<", new LocalVarIdentifier("i"), new LocalVarIdentifier("number"));
|
||||||
|
|
||||||
|
BinaryExpression whileBinExpr = new BinaryExpression("*", new LocalVarIdentifier("factorial"), new LocalVarIdentifier("i"));
|
||||||
|
AssignStatementExpression assignStatementExpression0 = new AssignStatementExpression("=", new LocalVarIdentifier("factorial"), whileBinExpr);
|
||||||
|
List<IStatement> whileBlockStmts = new ArrayList<>();
|
||||||
|
whileBlockStmts.add(assignStatementExpression0);
|
||||||
|
BlockStatement whileBlock = new BlockStatement( whileBlockStmts, null);
|
||||||
|
|
||||||
|
WhileStatement whileStatement0 = new WhileStatement(whileCondition, whileBlock);
|
||||||
|
|
||||||
|
iStmtList0.add(whileStatement0);
|
||||||
|
|
||||||
|
// Return Statement
|
||||||
|
LocalVarIdentifier returnIdentifier = new LocalVarIdentifier("factorial");
|
||||||
|
ReturnStatement returnStatement = new ReturnStatement(returnIdentifier);
|
||||||
|
|
||||||
|
iStmtList0.add(returnStatement);
|
||||||
|
BlockStatement blockStatement = new BlockStatement(iStmtList0, null);
|
||||||
|
return new MethodDecl(classThatContainsMethod, returnType, name, parameterListObj, blockStatement);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
package Typecheck;
|
package Typecheck;
|
||||||
|
|
||||||
import ASTs.emptyClassAST;
|
import ASTs.*;
|
||||||
import abstractSyntaxTree.Program;
|
import abstractSyntaxTree.Program;
|
||||||
import astGenerator.ASTGenerator;
|
import astGenerator.ASTGenerator;
|
||||||
import gen.DecafLexer;
|
import gen.DecafLexer;
|
||||||
@ -12,7 +12,6 @@ import org.antlr.v4.runtime.tree.ParseTree;
|
|||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.lang.reflect.Type;
|
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
|
|
||||||
@ -23,14 +22,14 @@ public class TestAll {
|
|||||||
@Test
|
@Test
|
||||||
public void testEmptyClass() {
|
public void testEmptyClass() {
|
||||||
Program emptyClassAst = emptyClassAST.getEmptyProgramm();
|
Program emptyClassAst = emptyClassAST.getEmptyProgramm();
|
||||||
testTypeCheck(emptyClassAst, true);
|
testTypeCheck(emptyClassAst, false);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyClassWithConstructor(){
|
public void testEmptyClassWithConstructor(){
|
||||||
Program abstractSyntaxTree = getAst("test/resources/basicClasses/EmptyClassWithConstructor.java");
|
Program abstractSyntaxTree = EmptyClassWithConstructorAST.getProgram();
|
||||||
boolean expectedResult = true;
|
boolean expectedResult = false;
|
||||||
testTypeCheck(abstractSyntaxTree, expectedResult);
|
testTypeCheck(abstractSyntaxTree, expectedResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,11 +41,18 @@ public class TestAll {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testFail(){
|
public void testAssignWrongType(){
|
||||||
Program assignWrongType = getAst("test/resources/FailTests/AssignWrongType.java");
|
Program assignWrongType = AssignWrongTypeAST.getProgram();
|
||||||
testTypeCheck(assignWrongType, false);
|
testTypeCheck(assignWrongType, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFakultaet(){
|
||||||
|
Program assignWrongType = FakultaetAST.getProgram();
|
||||||
|
testTypeCheck(assignWrongType, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void testTypeCheck(Program abstractSyntaxTree, boolean expectedResult){
|
public void testTypeCheck(Program abstractSyntaxTree, boolean expectedResult){
|
||||||
try {
|
try {
|
||||||
TypeChecker.assertTypeCheckResult(abstractSyntaxTree, expectedResult);
|
TypeChecker.assertTypeCheckResult(abstractSyntaxTree, expectedResult);
|
||||||
|
Loading…
Reference in New Issue
Block a user