fixed program instead of classes and implemented some tests for typecheck

This commit is contained in:
JonathanFleischmann 2024-05-14 15:03:26 +02:00
parent e97b9e55c8
commit 99d2e7a104
18 changed files with 421 additions and 228 deletions

View File

@ -12,7 +12,7 @@ public class Type {
}
private final Kind kind;
private final String reference;
public Type(Kind kind, String reference){
private Type(Kind kind, String reference){
this.kind = kind;
this.reference = reference;
}

View File

@ -6,6 +6,7 @@ import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
@ -15,6 +16,7 @@ import java.util.Map;
import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
@AllArgsConstructor
public class TypedAssignment implements TypedStatement {
private TypedExpression value;
private TypedFieldVarAccess location;

View File

@ -5,6 +5,7 @@ import de.maishai.ast.records.Binary;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
@ -15,6 +16,7 @@ import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TypedBinary implements TypedExpression {
private TypedExpression left;
private Operator op;

View File

@ -4,6 +4,7 @@ import de.maishai.ast.records.*;
import de.maishai.typedast.MethodContext;
import de.maishai.typedast.TypedExpression;
import de.maishai.typedast.Type;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.objectweb.asm.MethodVisitor;
@ -14,6 +15,7 @@ import static de.maishai.typedast.Help.TypedExpressionHelp.convertExpression;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class TypedFieldVarAccess implements TypedExpression {
private Boolean field;
private TypedExpression recursiveOwnerChain;

View File

@ -20,7 +20,7 @@ import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithConstructor {
public static Class get() {
public static Program get() {
List<Declaration> declarations = List.of(
new Declaration(
"x",
@ -29,11 +29,15 @@ public class AbstractSyntax_ClassWithConstructor {
);
List<Method> methods = List.of();
List<Constructor> constructors = getConstructors();
return new Class(
"ClassWithConstructor",
declarations,
methods,
constructors
return new Program(
List.of(
new Class(
"ClassWithConstructor",
declarations,
methods,
constructors
)
)
);
}

View File

@ -26,7 +26,7 @@ import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithConstructorAndMethodCall {
public static Class get() {
public static Program get() {
List<Declaration> declarationList = List.of(
new Declaration(
"x",
@ -35,11 +35,15 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall {
);
List<Method> methodList = getMethods();
List<Constructor> constructorList = getConstructors();
return new Class(
"ClassWithConstructorAndMethodCall",
declarationList,
methodList,
constructorList
return new Program(
List.of(
new Class(
"ClassWithConstructorAndMethodCall",
declarationList,
methodList,
constructorList
)
)
);
}
@ -61,7 +65,7 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall {
new While(
new MethodCall(
new FieldVarAccess(
true,
false,
null,
"methodCall"),
List.of()

View File

@ -21,7 +21,7 @@ import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithConstructorWithCodeInComments {
public static Class get() {
public static Program get() {
List<Declaration> declarations = List.of(
new Declaration(
"x",
@ -30,11 +30,15 @@ public class AbstractSyntax_ClassWithConstructorWithCodeInComments {
);
List<Method> methods = List.of();
List<Constructor> constructors = getConstructors();
return new Class(
"ClassWithConstructorWithCodeInComments",
declarations,
methods,
constructors
return new Program(
List.of(
new Class(
"ClassWithConstructorWithCodeInComments",
declarations,
methods,
constructors
)
)
);
}
@ -109,7 +113,7 @@ public class AbstractSyntax_ClassWithConstructorWithCodeInComments {
"x")
)
)
)
)
)
)

View File

@ -13,6 +13,7 @@
// }
// }
//}
import de.maishai.ast.Operator;
import de.maishai.ast.records.*;
import de.maishai.ast.records.Class;
@ -21,7 +22,7 @@ import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithConstructorWithParameters {
public static Class get() {
public static Program get() {
List<Declaration> declarations = List.of(
new Declaration(
"x",
@ -29,11 +30,15 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
)
);
List<Constructor> constructors = getConstructors();
return new Class(
"ClassWithConstructorWithParameters",
declarations,
List.of(),
constructors
return new Program(
List.of(
new Class(
"ClassWithConstructorWithParameters",
declarations,
List.of(),
constructors
)
)
);
}

View File

@ -9,7 +9,7 @@ import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithField {
public static Class get() {
public static Program get() {
List<Declaration> declarations = List.of(
new Declaration(
"x",
@ -18,21 +18,25 @@ public class AbstractSyntax_ClassWithField {
);
List<Method> methods = List.of();
List<Constructor> constructors =
List.of(
new Constructor(
"ClassWithField",
List.of(),
new Block(
List.of(
new Constructor(
"ClassWithField",
List.of(),
List.of()
new Block(
List.of(),
List.of()
)
)
);
return new Program(
List.of(
new Class(
"ClassWithField",
declarations,
methods,
constructors
)
)
);
return new Class(
"ClassWithField",
declarations,
methods,
constructors
);
}
}

View File

@ -11,19 +11,23 @@ import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithMethod {
public static Class get() {
public static Program get() {
List<Method> methods = getMethods();
return new Class(
"ClassWithMethod",
List.of(),
methods,
return new Program(
List.of(
new Constructor(
new Class(
"ClassWithMethod",
List.of(),
new Block(
List.of(),
List.of()
methods,
List.of(
new Constructor(
"ClassWithMethod",
List.of(),
new Block(
List.of(),
List.of()
)
)
)
)
)

View File

@ -18,7 +18,7 @@ import de.maishai.typedast.Type;
import java.util.List;
class AbstractSyntax_ClassWithMethodAndField {
public static Class get() {
public static Program get() {
List<Declaration> declarations = List.of(
new Declaration(
"c",
@ -27,11 +27,15 @@ class AbstractSyntax_ClassWithMethodAndField {
);
List<Method> methods = getMethods();
List<Constructor> constructors = getConstructors();
return new Class(
"ClassWithMethodAndField",
declarations,
methods,
constructors
return new Program(
List.of(
new Class(
"ClassWithMethodAndField",
declarations,
methods,
constructors
)
)
);
}

View File

@ -26,7 +26,7 @@ import de.maishai.typedast.Type;
import java.util.List;
public class AbstractSyntax_ClassWithMultipleMethods {
public static Class get() {
public static Program get() {
List<Declaration> declarations = List.of(
new Declaration(
"instance",
@ -35,11 +35,14 @@ public class AbstractSyntax_ClassWithMultipleMethods {
);
List<Method> methods = getMethods();
List<Constructor> constructors = List.of();
return new Class(
"ClassWithMoreComplexMethodAndMain",
declarations,
methods,
constructors
return new Program(
List.of(new Class(
"ClassWithMoreComplexMethodAndMain",
declarations,
methods,
constructors
)
)
);
}

View File

@ -4,22 +4,27 @@
import de.maishai.ast.records.Block;
import de.maishai.ast.records.Class;
import de.maishai.ast.records.Constructor;
import de.maishai.ast.records.Program;
import java.util.List;
public class AbstractSyntax_PublicClass {
public static Class get() {
return new Class(
"PublicClass",
List.of(),
List.of(),
public static Program get() {
return new Program(
List.of(
new Constructor(
new Class(
"PublicClass",
List.of(),
new Block(
List.of(),
List.of()
List.of(),
List.of(
new Constructor(
"PublicClass",
List.of(),
new Block(
List.of(),
List.of()
)
)
)
)
)

View File

@ -14,140 +14,230 @@
import de.maishai.ast.Operator;
import de.maishai.typedast.Type;
import de.maishai.typedast.TypedStatement;
import de.maishai.typedast.typedclass.*;
import java.util.List;
public class TypedAbstractSyntax_ClassWithConstructor {
// public static TypedClass get() {
// TypedClass typedClass = new TypedClass();
// typedClass.setIsPublic(true);
// typedClass.setTypedId(new TypedId("ClassWithConstructor"));
//
// TypedField typedField = new TypedField();
// typedField.setTypedId(new TypedId("x"));
// typedField.setType(Type.INT);
//
// typedClass.setTypedFields(List.of(typedField));
// typedClass.setTypedMethods(List.of());
// typedClass.setTypedMainMethod(null);
// typedClass.setTypedConstructors(getConstructors());
// return typedClass;
// }
//
// private static List<TypedConstructor> getConstructors() {
// return List.of(getConstructor1());
// }
//
// private static TypedConstructor getConstructor1() {
// TypedConstructor typedConstructor = new TypedConstructor();
// typedConstructor.setIsPublic(true);
// typedConstructor.setTypedId(new TypedId("ClassWithConstructor"));
// typedConstructor.setTypedParameters(List.of());
//
// TypedBlock typedBlock = new TypedBlock();
//
// TypedLocalVariable typedLocalVariable = new TypedLocalVariable();
// typedLocalVariable.setTypedId(new TypedId("i"));
// typedLocalVariable.setType(Type.INT);
//
// typedBlock.setVars(List.of(typedLocalVariable));
//
// TypedAssignment typedAssignment = new TypedAssignment();
// typedAssignment.setLoc(new TypedId("x"));
//// typedAssignment.setAssignSign(AssignSign.ASSIGN);
// typedAssignment.setValue(new TypedIntLiteral(10));
//
// TypedFor typedFor = new TypedFor();
//
// TypedAssignment typedAssignmentFor = new TypedAssignment();
// typedAssignmentFor.setLoc(new TypedId("i"));
//// typedAssignmentFor.setAssignSign(AssignSign.ASSIGN);
// typedAssignmentFor.setValue(new TypedIntLiteral(0));
//
//// typedFor.setAssign(typedAssignmentFor);
//
// TypedBinary typedBinaryFor = new TypedBinary();
// typedBinaryFor.setLeft(new TypedId("i"));
// typedBinaryFor.setOp(Operator.LT);
// typedBinaryFor.setRight(new TypedIntLiteral(6));
//
// typedFor.setCond(typedBinaryFor);
//
// TypedBinary typedBinaryForIncr = new TypedBinary();
// typedBinaryForIncr.setLeft(new TypedId("i"));
// typedBinaryForIncr.setOp(Operator.ADD);
// typedBinaryForIncr.setRight(new TypedIntLiteral(1));
//
// TypedAssignment typedAssignmentForIncr = new TypedAssignment();
// typedAssignmentForIncr.setLoc(new TypedId("i"));
//// typedAssignmentForIncr.setAssignSign(AssignSign.ASSIGN);
// typedAssignmentForIncr.setValue(typedBinaryForIncr);
//
//// typedFor.setInc(typedAssignmentForIncr);
//
// TypedBlock typedBlockFor = new TypedBlock();
//
// TypedLocalVariable typedLocalVariableFor = new TypedLocalVariable();
// typedLocalVariableFor.setTypedId(new TypedId("j"));
// typedLocalVariableFor.setType(Type.INT);
//
// typedBlockFor.setVars(List.of(typedLocalVariableFor));
//
// TypedFor typedInnerFor = new TypedFor();
//
// TypedAssignment typedAssignmentInnerFor = new TypedAssignment();
// typedAssignmentInnerFor.setLoc(new TypedId("j"));
//// typedAssignmentInnerFor.setAssignSign(AssignSign.ASSIGN);
// typedAssignmentInnerFor.setValue(new TypedIntLiteral(0));
//
//// typedInnerFor.setAssign(typedAssignmentInnerFor);
//
// TypedBinary typedBinaryInnerFor = new TypedBinary();
// typedBinaryInnerFor.setLeft(new TypedId("j"));
// typedBinaryInnerFor.setOp(Operator.LT);
// typedBinaryInnerFor.setRight(new TypedId("x"));
//
// typedInnerFor.setCond(typedBinaryInnerFor);
//
// TypedAssignment typedAssignmentInnerForIncr = new TypedAssignment();
// typedAssignmentInnerForIncr.setLoc(new TypedId("j"));
//// typedAssignmentInnerForIncr.setAssignSign(AssignSign.ADD_ASSIGN);
// typedAssignmentInnerForIncr.setValue(new TypedIntLiteral(1));
//
//// typedInnerFor.setInc(typedAssignmentInnerForIncr);
//
// TypedBlock typedBlockInnerFor = new TypedBlock();
// typedBlockInnerFor.setVars(List.of());
//
// TypedAssignment typedAssignmentInnerForBlock = new TypedAssignment();
// typedAssignmentInnerForBlock.setLoc(new TypedId("x"));
//// typedAssignmentInnerForBlock.setAssignSign(AssignSign.ASSIGN);
//
// TypedBinary typedBinaryInnerForBlock = new TypedBinary();
// typedBinaryInnerForBlock.setLeft(new TypedId("x"));
// typedBinaryInnerForBlock.setOp(Operator.MUL);
// typedBinaryInnerForBlock.setRight(new TypedId("x"));
//
// typedAssignmentInnerForBlock.setValue(typedBinaryInnerForBlock);
//
// typedBlockInnerFor.setStmts(List.of(typedAssignmentInnerForBlock));
//
// typedInnerFor.setTypedBlock(typedBlockInnerFor);
//
// typedBlockFor.setStmts(List.of(typedInnerFor));
//
// typedFor.setTypedBlock(typedBlockFor);
//
// typedBlock.setStmts(
// List.of(
// typedAssignment,
// typedFor
// )
// );
//
// typedConstructor.setTypedBlock(typedBlock);
//
// return typedConstructor;
// }
}
public static TypedClass get() {
return new TypedClass(
"ClassWithConstructor",
List.of(
new TypedDeclaration(
"x",
Type.INT
)
),
List.of(),
List.of(
new TypedConstructor(
"ClassWithConstructor",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of(
new TypedLocalVariable(
"i",
Type.INT
)
)
)
),
null,
null,
Type.REFERENCE("ClassWithField")
);
}
private static List<TypedConstructor> getConstructors() {
return List.of(getTypedConstructor1());
}
private static TypedConstructor getTypedConstructor1() {
List<TypedParameter> typedParameters = List.of();
List<TypedLocalVariable> typedLocalVariables = List.of(
new TypedLocalVariable(
"i",
Type.INT
)
);
List<TypedStatement> typedStatementList =
List.of(
new TypedAssignment(
new TypedIntLiteral(
10,
Type.INT),
new TypedFieldVarAccess(
true,
null,
"x",
Type.INT
),
Type.INT
),
new TypedFor(
new TypedAssignment(
new TypedIntLiteral(
0,
Type.INT),
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Type.INT
),
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Operator.LT,
new TypedIntLiteral(
6,
Type.INT
),
Type.BOOL
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT),
Operator.ADD,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"i",
Type.INT
),
Type.INT
),
new TypedBlock(
List.of(
new TypedLocalVariable(
"j",
Type.INT
)
),
List.of(
new TypedFor(
new TypedAssignment(
new TypedIntLiteral(
0,
Type.INT),
new TypedFieldVarAccess(
false,
null,
"j",
Type.INT
),
Type.INT
),
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"j",
Type.INT
),
Operator.LT,
new TypedFieldVarAccess(
true,
null,
"x",
Type.INT
),
Type.BOOL
),
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
false,
null,
"j",
Type.INT),
Operator.ADD,
new TypedIntLiteral(
1,
Type.INT
),
Type.INT
),
new TypedFieldVarAccess(
false,
null,
"j",
Type.INT
),
Type.INT
),
new TypedBlock(
List.of(),
List.of(
new TypedAssignment(
new TypedBinary(
new TypedFieldVarAccess(
true,
null,
"x",
Type.INT),
Operator.MUL,
new TypedFieldVarAccess(
true,
null,
"x",
Type.INT),
Type.INT
),
new TypedFieldVarAccess(
true,
null,
"x",
Type.INT
),
Type.INT
)
),
Type.INT
),
Type.INT
)
),
Type.INT
),
Type.INT
)
);
TypedBlock typedBlock = new TypedBlock(
typedLocalVariables,
typedStatementList,
Type.INT
);
return new TypedConstructor(
"ClassWithConstructor",
typedParameters,
typedBlock,
Type.VOID,
List.of()
);
}
}

View File

@ -9,18 +9,32 @@ import de.maishai.typedast.typedclass.*;
import java.util.List;
public class TypedAbstractSyntax_ClassWithField {
// public static TypedClass get() {
// TypedClass typedClass = new TypedClass();
// typedClass.setIsPublic(true);
// typedClass.setTypedId(new TypedId("ClassWithField"));
// typedClass.setTypedFields(
// List.of(
// new TypedField(new TypedId("x"), Type.INT)
// )
// );
// typedClass.setTypedMethods(List.of());
// typedClass.setTypedMainMethod(null);
// typedClass.setTypedConstructors(List.of());
// return typedClass;
// }
public static TypedClass get() {
return new TypedClass(
"ClassWithField",
List.of(
new TypedDeclaration(
"x",
Type.INT
)
),
List.of(),
List.of(
new TypedConstructor(
"ClassWithField",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
Type.REFERENCE("ClassWithField")
);
}
}

View File

@ -1,19 +1,33 @@
//public class PublicClass {
//}
import de.maishai.typedast.Type;
import de.maishai.typedast.typedclass.*;
import java.util.List;
public class TypedAbstractSyntax_PublicClass {
// public static TypedClass get() {
// TypedClass typedClass = new TypedClass();
// typedClass.setIsPublic(true);
// typedClass.setTypedId(new TypedId("PublicClass"));
// typedClass.setTypedFields(List.of());
// typedClass.setTypedMethods(List.of());
// typedClass.setTypedMainMethod(null);
// typedClass.setTypedConstructors(List.of());
// return typedClass;
// }
public static TypedClass get() {
return new TypedClass(
"PublicClass",
List.of(),
List.of(),
List.of(
new TypedConstructor(
"PublicClass",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
),
null,
null,
Type.REFERENCE("PublicClass")
);
}
}

View File

@ -48,9 +48,15 @@ public class ScannerParserTests {
assertEquals(AbstractSyntax_ClassWithMethodAndField.get(), resultAst);
}
@Test
public void testClassWithConstructorAndMethodCall() {
Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java");
assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst);
}
// @Test
// public void testClassWithConstructorAndMethodCall() {
// Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java");
// public void testClassCanBeTyped() {
// Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassCanBeTyped.java");
// assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst);
// }
}

View File

@ -0,0 +1,26 @@
import de.maishai.Compiler;
import de.maishai.typedast.typedclass.TypedClass;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class TypingTests {
@Test
public void testPublicClass() {
TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_PublicClass.get());
assertEquals(TypedAbstractSyntax_PublicClass.get(), resultTypedAst);
}
@Test
public void testClassWithField() {
TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithField.get());
assertEquals(TypedAbstractSyntax_ClassWithField.get(), resultTypedAst);
}
@Test
public void testClassWithConstructor() {
TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithConstructor.get());
assertEquals(TypedAbstractSyntax_ClassWithConstructor.get(), resultTypedAst);
}
}