Support for if ... then ... else following #911. (#944)

This commit is contained in:
Richard Smith
2022-03-03 16:34:34 -08:00
committed by GitHub
parent 4a98acc3ea
commit 2325c50b3b
9 changed files with 113 additions and 3 deletions
@@ -346,6 +346,7 @@ void Interpreter::StepLvalue() {
case ExpressionKind::StringLiteral:
case ExpressionKind::StringTypeLiteral:
case ExpressionKind::IntrinsicExpression:
case ExpressionKind::IfExpression:
FATAL() << "Can't treat expression as lvalue: " << exp;
case ExpressionKind::UnimplementedExpression:
FATAL() << "Unimplemented: " << exp;
@@ -690,6 +691,21 @@ void Interpreter::StepExp() {
CHECK(act.pos() == 0);
return todo_.FinishAction(arena_->New<StringType>());
}
case ExpressionKind::IfExpression: {
const auto& if_expr = cast<IfExpression>(exp);
if (act.pos() == 0) {
return todo_.Spawn(
std::make_unique<ExpressionAction>(if_expr.condition()));
} else if (act.pos() == 1) {
const BoolValue& condition = cast<BoolValue>(*act.results()[0]);
return todo_.Spawn(std::make_unique<ExpressionAction>(
condition.value() ? if_expr.then_expression()
: if_expr.else_expression()));
} else {
return todo_.FinishAction(act.results()[1]);
}
break;
}
case ExpressionKind::UnimplementedExpression:
FATAL() << "Unimplemented: " << exp;
} // switch (exp->kind)