NichtHaskell/Source/abstractSyntaxTree/Statement/IfElseStatement.java

39 lines
1.2 KiB
Java

package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
public class IfElseStatement extends AbstractType implements IStatement{
IExpression condition;
IStatement ifStatement;
IStatement elseStatement;
public IfElseStatement(IExpression condition, IStatement ifStatement, IStatement elseStatement) {
this.condition = condition;
this.ifStatement = ifStatement;
this.elseStatement = elseStatement;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
TypeCheckResult conditionType = condition.typeCheck();
if (!conditionType.equals("bool")) {
throw new IllegalArgumentException("should be boolean");
}
TypeCheckResult ifStatementType = ifStatement.typeCheck();
TypeCheckResult elseStatementType = elseStatement.typeCheck();
if (!ifStatementType.equals(elseStatementType)) {
throw new IllegalArgumentException("if and else have different types");
}
result.type = elseStatementType.type;
return result;
}
}