Merge branch 'refs/heads/main' into testsuites

This commit is contained in:
JonathanFleischmann 2024-06-22 16:51:17 +02:00
commit b3ce4b5ff8
18 changed files with 60 additions and 72 deletions

View File

@ -8,44 +8,28 @@ public class TypedExpressionHelp {
public static TypedExpression convertExpression( TypedProgram typedProgram, Expression expression) {
if (expression instanceof BoolLiteral boolLiteral) {
TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral( typedProgram, boolLiteral);
typedBoolLiteral.typeCheck( typedProgram);
return typedBoolLiteral;
return new TypedBoolLiteral(boolLiteral);
}
else if (expression instanceof CharLiteral charLiteral) {
TypedCharLiteral typedCharLiteral = new TypedCharLiteral( typedProgram, charLiteral);
typedCharLiteral.typeCheck( typedProgram);
return typedCharLiteral;
return new TypedCharLiteral(charLiteral);
}
else if (expression instanceof IntLiteral intLiteral) {
TypedIntLiteral typedIntLiteral = new TypedIntLiteral( typedProgram, intLiteral);
typedIntLiteral.typeCheck( typedProgram);
return typedIntLiteral;
return new TypedIntLiteral(intLiteral);
}
else if (expression instanceof Binary binary) {
TypedBinary typedBinary = new TypedBinary( typedProgram, binary);
typedBinary.typeCheck( typedProgram);
return typedBinary;
return new TypedBinary(typedProgram, binary);
}
else if (expression instanceof FieldVarAccess fieldVarAccess) {
TypedFieldVarAccess typedFieldVarAccess = new TypedFieldVarAccess( typedProgram, fieldVarAccess);
typedFieldVarAccess.typeCheck( typedProgram);
return typedFieldVarAccess;
return new TypedFieldVarAccess(typedProgram, fieldVarAccess);
}
else if (expression instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall( typedProgram, methodCall);
typedMethodCall.typeCheck(typedProgram);
return typedMethodCall;
return new TypedMethodCall(typedProgram, methodCall);
}
else if (expression instanceof New newStmt) {
TypedNew typedNew = new TypedNew( typedProgram, newStmt);
typedNew.typeCheck( typedProgram);
return typedNew;
return new TypedNew(typedProgram, newStmt);
}
else if (expression instanceof Unary unary) {
TypedUnary typedUnary = new TypedUnary( typedProgram, unary);
typedUnary.typeCheck( typedProgram);
return typedUnary;
return new TypedUnary(typedProgram, unary);
} else {
return null;
}

View File

@ -20,12 +20,13 @@ public class TypedAssignment implements TypedStatement {
public TypedAssignment(TypedProgram typedProgram, Assignment untyped) {
convertToTypedAssignment(typedProgram, untyped);
typeCheck(typedProgram);
}
public void convertToTypedAssignment(TypedProgram typedProgram, Assignment untyped) {
value = convertExpression(typedProgram, untyped.value());
location = new TypedFieldVarAccess(typedProgram, untyped.location());
location.typeCheck(typedProgram);
//location.typeCheck(typedProgram);
}
@Override

View File

@ -27,6 +27,7 @@ public class TypedBinary implements TypedExpression {
public TypedBinary(TypedProgram typedProgram, Binary unTypedBinary) {
convertToTypedBinary(typedProgram, unTypedBinary);
typeCheck(typedProgram);
}
public void convertToTypedBinary(TypedProgram typedProgram, Binary unTypedBinary) {

View File

@ -18,11 +18,12 @@ import java.util.List;
public class TypedBlock implements TypedNode {
private List<TypedLocalVariable> vars = new ArrayList<>();
private List<TypedStatement> stmts = new ArrayList<>();
private Type type;
private Type type = Type.VOID;
public TypedBlock(TypedProgram typedProgram, Block unTypedBlock) {
convertToTypedBlock(typedProgram, unTypedBlock);
clearLocalVariable(typedProgram);
}
public TypedBlock(List<TypedLocalVariable> vars, List<TypedStatement> stmts) {
@ -41,45 +42,31 @@ public class TypedBlock implements TypedNode {
vars.add(new TypedLocalVariable(typedProgram, declaration));
}
if (stmt instanceof Assignment assignment) {
TypedAssignment typedAssignment = new TypedAssignment(typedProgram, assignment);
typedAssignment.typeCheck(typedProgram);
stmts.add(typedAssignment);
stmts.add(new TypedAssignment(typedProgram, assignment));
continue;
}
if (stmt instanceof For forStmt) {
TypedFor typedFor = new TypedFor(typedProgram, forStmt);
typedFor.typeCheck(typedProgram);
stmts.add(typedFor);
stmts.add(new TypedFor(typedProgram, forStmt));
continue;
}
if (stmt instanceof IfElse ifElse) {
TypedIfElse typedIfElse = new TypedIfElse(typedProgram, ifElse);
typedIfElse.typeCheck(typedProgram);
stmts.add(typedIfElse);
stmts.add(new TypedIfElse(typedProgram, ifElse));
continue;
}
if (stmt instanceof While whileStmt) {
TypedWhile typedWhile = new TypedWhile(typedProgram, whileStmt);
typedWhile.typeCheck(typedProgram);
stmts.add(typedWhile);
stmts.add(new TypedWhile(typedProgram, whileStmt));
continue;
}
if (stmt instanceof DoWhile doWhile) {
TypedDoWhile typedDoWhile = new TypedDoWhile(typedProgram, doWhile);
typedDoWhile.typeCheck(typedProgram);
stmts.add(typedDoWhile);
stmts.add(new TypedDoWhile(typedProgram, doWhile));
continue;
}
if (stmt instanceof Return returnStmt) {
TypedReturn typedReturn = new TypedReturn(typedProgram, returnStmt);
typedReturn.typeCheck(typedProgram);
stmts.add(typedReturn);
stmts.add(new TypedReturn(typedProgram, returnStmt));
continue;
}
if (stmt instanceof New newStmt) {
TypedNew typedNew = new TypedNew(typedProgram, newStmt);
typedNew.typeCheck(typedProgram);
stmts.add(typedNew);
stmts.add(new TypedNew(typedProgram, newStmt));
continue;
}
@ -94,9 +81,7 @@ public class TypedBlock implements TypedNode {
}
if (stmt instanceof MethodCall methodCall) {
TypedMethodCall typedMethodCall = new TypedMethodCall(typedProgram, methodCall);
typedMethodCall.typeCheck(typedProgram);
stmts.add(typedMethodCall);
stmts.add(new TypedMethodCall(typedProgram, methodCall));
}
if (stmt instanceof Print printStmt) {
@ -105,24 +90,26 @@ public class TypedBlock implements TypedNode {
stmts.add(typedPrint);
}
}
this.typeCheck(typedProgram);
}
@Override
public Type typeCheck(TypedProgram typedProgram) {
Type chekType = null;
for (TypedStatement stmt : stmts) {
stmt.typeCheck(typedProgram);
if (stmt instanceof TypedReturn returnStmt) {
chekType = returnStmt.getType();
return type;
}
private void clearLocalVariable(TypedProgram typedProgram){
for(var typedLocalVariable : vars){
if (typedProgram.getCurrentClass().isCurrentMethodPresent() && !typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariableInMethod(typedLocalVariable.getName())) {
typedProgram.getCurrentClass().getCurrentMethod().deleteLocalVariableInMethod(typedLocalVariable.getName());
}
}
if (!typedProgram.getCurrentClass().isCurrentMethodPresent() && typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariableInConstructor(typedLocalVariable.getName())) {
typedProgram.getCurrentClass().getCurrentConstructor().deleteLocalVariableInConstructor(typedLocalVariable.getName());
}
}
}
if (chekType == null) {
chekType = Type.VOID;
}
type = chekType;
return type;
}
public void codeGen(MethodContext ctx) {

View File

@ -18,11 +18,11 @@ public class TypedBoolLiteral implements TypedExpression {
private Type type;
public TypedBoolLiteral(TypedProgram typedProgram, BoolLiteral unTypedBoolLiteral) {
convertToTypedBoolLiteral(typedProgram, unTypedBoolLiteral);
public TypedBoolLiteral(BoolLiteral unTypedBoolLiteral) {
convertToTypedBoolLiteral(unTypedBoolLiteral);
}
public void convertToTypedBoolLiteral(TypedProgram typedProgram, BoolLiteral unTypedBoolLiteral) {
public void convertToTypedBoolLiteral(BoolLiteral unTypedBoolLiteral) {
value = unTypedBoolLiteral.value();
type = Type.BOOL;
}

View File

@ -13,11 +13,11 @@ public class TypedCharLiteral implements TypedExpression {
private char value;
private Type type;
public TypedCharLiteral(TypedProgram typedProgram, CharLiteral unTypedCharLiteral) {
convertToCharLiteral(typedProgram, unTypedCharLiteral);
public TypedCharLiteral( CharLiteral unTypedCharLiteral) {
convertToCharLiteral(unTypedCharLiteral);
}
public void convertToCharLiteral(TypedProgram typedProgram, CharLiteral unTypedCharLiteral) {
public void convertToCharLiteral(CharLiteral unTypedCharLiteral) {
value = unTypedCharLiteral.value();
type = Type.CHAR;
}

View File

@ -71,6 +71,9 @@ public class TypedConstructor implements TypedNode {
this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block());
typeCheck(typedProgram);
}
public void deleteLocalVariableInConstructor(String localVarName){
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));
}
@Override
public Type typeCheck(TypedProgram typedProgram) {

View File

@ -18,6 +18,7 @@ public class TypedDoWhile implements TypedStatement {
public TypedDoWhile(TypedProgram typedProgram, DoWhile unTypedDoWhile) {
convertToTypedDoWhile(typedProgram, unTypedDoWhile);
typeCheck(typedProgram);
}
public void convertToTypedDoWhile(TypedProgram typedProgram, DoWhile unTypedDoWhile) {

View File

@ -25,6 +25,7 @@ public class TypedFieldVarAccess implements TypedExpression {
public TypedFieldVarAccess(TypedProgram typedProgram, FieldVarAccess unTypedFieldVarAccess) {
convertToTypedFieldVarAccess(typedProgram, unTypedFieldVarAccess);
typeCheck(typedProgram);
}
public void convertToTypedFieldVarAccess(TypedProgram typedProgram, FieldVarAccess unTypedFieldVarAccess) {

View File

@ -22,6 +22,7 @@ public class TypedFor implements TypedStatement {
public TypedFor(TypedProgram typedProgram, For unTypedFor) {
convertToTypedFor(typedProgram, unTypedFor);
typeCheck(typedProgram);
}
public void convertToTypedFor(TypedProgram typedProgram, For unTypedFor) {

View File

@ -21,6 +21,7 @@ public class TypedIfElse implements TypedStatement {
public TypedIfElse(TypedProgram typedProgram, IfElse unTypedIfElse) {
convertToTypedIfElse(typedProgram, unTypedIfElse);
typeCheck(typedProgram);
}
public void convertToTypedIfElse(TypedProgram typedProgram, IfElse unTypedIfElse) {

View File

@ -20,11 +20,11 @@ public class TypedIntLiteral implements TypedExpression {
private Type type;
public TypedIntLiteral(TypedProgram typedProgram, IntLiteral unTypedIntLiteral) {
convertToTypedIntLiteral(typedProgram, unTypedIntLiteral);
public TypedIntLiteral(IntLiteral unTypedIntLiteral) {
convertToTypedIntLiteral(unTypedIntLiteral);
}
public void convertToTypedIntLiteral(TypedProgram typedProgram, IntLiteral unTypedIntLiteral) {
public void convertToTypedIntLiteral(IntLiteral unTypedIntLiteral) {
value = unTypedIntLiteral.value();
type = Type.INT;
}

View File

@ -66,6 +66,9 @@ public class TypedMethod implements TypedNode {
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
}
public void deleteLocalVariableInMethod(String localVarName){
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));
}
public Type getLocalVariableType(String localVarName) {
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
}

View File

@ -25,11 +25,12 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
public TypedMethodCall(TypedProgram typedProgram, MethodCall unTypedMethodCall) {
convertToTypedMethodCall(typedProgram, unTypedMethodCall);
typeCheck(typedProgram);
}
public void convertToTypedMethodCall(TypedProgram typedProgram, MethodCall unTypedMethodCall) {
recipient = new TypedFieldVarAccess(typedProgram, unTypedMethodCall.recipient());
recipient.typeCheck(typedProgram);
//recipient.typeCheck(typedProgram);
for (Expression arg : unTypedMethodCall.args()) {
args.add(convertExpression(typedProgram, arg));
}

View File

@ -21,6 +21,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
public TypedNew(TypedProgram typedProgram, New unTypedNew) {
convertToTypedNew(typedProgram, unTypedNew);
typeCheck(typedProgram);
}
public void convertToTypedNew(TypedProgram typedProgram, New unTypedNew) {

View File

@ -19,6 +19,7 @@ public class TypedReturn implements TypedStatement {
public TypedReturn(TypedProgram typedProgram, Return unTypedReturn) {
convertToTypedReturn(typedProgram, unTypedReturn);
typeCheck(typedProgram);
}
public void convertToTypedReturn(TypedProgram typedProgram, Return unTypedReturn) {

View File

@ -18,6 +18,7 @@ public class TypedUnary implements TypedExpression {
public TypedUnary(TypedProgram typedProgram, Unary unTypedUnary) {
convertToTypedUnary(typedProgram, unTypedUnary);
typeCheck(typedProgram);
}
public void convertToTypedUnary(TypedProgram typedProgram, Unary unTypedUnary) {

View File

@ -17,6 +17,7 @@ public class TypedWhile implements TypedStatement {
public TypedWhile(TypedProgram typedProgram, While unTypedWhile) {
convertToTypedWhile(typedProgram, unTypedWhile);
typeCheck(typedProgram);
}
public void convertToTypedWhile(TypedProgram typedProgram, While unTypedWhile) {