30 lines
885 B
Java
30 lines
885 B
Java
package abstractSyntaxTree.Statement;
|
|
|
|
import TypeCheck.TypeCheckResult;
|
|
import TypeCheck.AbstractType;
|
|
import abstractSyntaxTree.Expression.IExpression;
|
|
|
|
public class WhileStatement extends AbstractType implements IStatement{
|
|
IExpression condition;
|
|
IStatement statement;
|
|
public WhileStatement(IExpression condition, IStatement statement) {
|
|
this.condition = condition;
|
|
this.statement = statement;
|
|
}
|
|
@Override
|
|
public TypeCheckResult typeCheck() throws Exception {
|
|
TypeCheckResult result = new TypeCheckResult();
|
|
|
|
TypeCheckResult conditionType = condition.typeCheck();
|
|
|
|
if (!conditionType.equals("bool")) {
|
|
throw new IllegalArgumentException("Expected boolean");
|
|
}
|
|
|
|
TypeCheckResult statementType = statement.typeCheck();
|
|
|
|
result.type = statementType.type;
|
|
return result;
|
|
}
|
|
}
|