Updated the typeCheck of TypedIfElse

This commit is contained in:
ahmad 2024-05-12 15:45:18 +02:00
parent 2322d161e6
commit 092cbb5af9
2 changed files with 9 additions and 5 deletions

View File

@ -19,7 +19,6 @@ public class TypedFor implements TypedStatement {
private TypedExpression cond; private TypedExpression cond;
private TypedAssignment inc; private TypedAssignment inc;
private TypedBlock typedBlock; private TypedBlock typedBlock;
//TODO: type of block in for loop
private Type type; private Type type;
public TypedFor(TypedClass clas, For unTypedFor) { public TypedFor(TypedClass clas, For unTypedFor) {

View File

@ -18,7 +18,6 @@ public class TypedIfElse implements TypedStatement {
private TypedExpression typedCon; private TypedExpression typedCon;
private TypedBlock ifTypedBlock; private TypedBlock ifTypedBlock;
private TypedBlock elseTypedBlock; private TypedBlock elseTypedBlock;
//TODO: add Type
private Type type; private Type type;
public TypedIfElse(TypedClass clas, IfElse unTypedIfElse) { public TypedIfElse(TypedClass clas, IfElse unTypedIfElse) {
@ -33,18 +32,24 @@ public class TypedIfElse implements TypedStatement {
@Override @Override
public Type typeCheck(TypedClass clas) { public Type typeCheck(TypedClass clas) {
/*
if (typedCon.typeCheck(clas) != Type.BOOL) { if (typedCon.typeCheck(clas) != Type.BOOL) {
throw new RuntimeException("If condition must be a boolean"); throw new RuntimeException("If condition must be a boolean");
} }
if (ifTypedBlock.typeCheck(clas) != Type.VOID) { if (ifTypedBlock.typeCheck(clas) != Type.VOID) {
throw new RuntimeException("If block must be of type void"); throw new RuntimeException("If block must be of type void");
} }
*/
//TODO: catch all cases of return when one method has a return type //TODO: it still not catching the all cases when return is used
if (ifTypedBlock.typeCheck(clas) == elseTypedBlock.typeCheck(clas)) { if (ifTypedBlock.typeCheck(clas) == elseTypedBlock.typeCheck(clas)) {
type = ifTypedBlock.typeCheck(clas); type = ifTypedBlock.typeCheck(clas);
} }
if (elseTypedBlock.typeCheck(clas) == Type.VOID) {
type = ifTypedBlock.typeCheck(clas);
}
if (ifTypedBlock.typeCheck(clas) == Type.VOID) {
type = elseTypedBlock.typeCheck(clas);
}
return type; return type;
} }