Update IfExpression to follow AST conventions (#1167)

This commit is contained in:
Geoff Romer
2022-04-12 12:28:48 -07:00
committed by GitHub
parent baf832b18b
commit 883f2a3ecd
6 changed files with 31 additions and 35 deletions
+2 -3
View File
@@ -161,9 +161,8 @@ void Expression::Print(llvm::raw_ostream& out) const {
break;
case ExpressionKind::IfExpression: {
const auto& if_expr = cast<IfExpression>(*this);
out << "if " << *if_expr.condition() << " then "
<< *if_expr.then_expression() << " else "
<< *if_expr.else_expression();
out << "if " << if_expr.condition() << " then "
<< if_expr.then_expression() << " else " << if_expr.else_expression();
break;
}
case ExpressionKind::UnimplementedExpression: {
+10 -5
View File
@@ -519,13 +519,18 @@ class IfExpression : public Expression {
return InheritsFromIfExpression(node->kind());
}
auto condition() const -> Nonnull<Expression*> { return condition_; }
auto then_expression() const -> Nonnull<Expression*> {
return then_expression_;
auto condition() const -> const Expression& { return *condition_; }
auto condition() -> Expression& { return *condition_; }
auto then_expression() const -> const Expression& {
return *then_expression_;
}
auto else_expression() const -> Nonnull<Expression*> {
return else_expression_;
auto then_expression() -> Expression& { return *then_expression_; }
auto else_expression() const -> const Expression& {
return *else_expression_;
}
auto else_expression() -> Expression& { return *else_expression_; }
private:
Nonnull<Expression*> condition_;
+6 -12
View File
@@ -174,18 +174,12 @@ static auto ExpressionToProto(const Expression& expression)
case ExpressionKind::IfExpression: {
const auto& if_expression = cast<IfExpression>(expression);
auto* if_proto = expression_proto.mutable_if_expression();
if (if_expression.condition()) {
*if_proto->mutable_condition() =
ExpressionToProto(*if_expression.condition());
}
if (if_expression.then_expression()) {
*if_proto->mutable_then_expression() =
ExpressionToProto(*if_expression.then_expression());
}
if (if_expression.else_expression()) {
*if_proto->mutable_else_expression() =
ExpressionToProto(*if_expression.else_expression());
}
*if_proto->mutable_condition() =
ExpressionToProto(if_expression.condition());
*if_proto->mutable_then_expression() =
ExpressionToProto(if_expression.then_expression());
*if_proto->mutable_else_expression() =
ExpressionToProto(if_expression.else_expression());
break;
}
@@ -888,12 +888,12 @@ auto Interpreter::StepExp() -> ErrorOr<Success> {
const auto& if_expr = cast<IfExpression>(exp);
if (act.pos() == 0) {
return todo_.Spawn(
std::make_unique<ExpressionAction>(if_expr.condition()));
std::make_unique<ExpressionAction>(&if_expr.condition()));
} else if (act.pos() == 1) {
const auto& condition = cast<BoolValue>(*act.results()[0]);
return todo_.Spawn(std::make_unique<ExpressionAction>(
condition.value() ? if_expr.then_expression()
: if_expr.else_expression()));
condition.value() ? &if_expr.then_expression()
: &if_expr.else_expression()));
} else {
return todo_.FinishAction(act.results()[1]);
}
@@ -144,11 +144,9 @@ static auto ResolveNames(Expression& expression,
break;
case ExpressionKind::IfExpression: {
auto& if_expr = cast<IfExpression>(expression);
RETURN_IF_ERROR(ResolveNames(*if_expr.condition(), enclosing_scope));
RETURN_IF_ERROR(
ResolveNames(*if_expr.then_expression(), enclosing_scope));
RETURN_IF_ERROR(
ResolveNames(*if_expr.else_expression(), enclosing_scope));
RETURN_IF_ERROR(ResolveNames(if_expr.condition(), enclosing_scope));
RETURN_IF_ERROR(ResolveNames(if_expr.then_expression(), enclosing_scope));
RETURN_IF_ERROR(ResolveNames(if_expr.else_expression(), enclosing_scope));
break;
}
case ExpressionKind::ArrayTypeLiteral: {
@@ -1002,19 +1002,19 @@ auto TypeChecker::TypeCheckExp(Nonnull<Expression*> e,
return Success();
case ExpressionKind::IfExpression: {
auto& if_expr = cast<IfExpression>(*e);
RETURN_IF_ERROR(TypeCheckExp(if_expr.condition(), impl_scope));
RETURN_IF_ERROR(TypeCheckExp(&if_expr.condition(), impl_scope));
RETURN_IF_ERROR(ExpectType(if_expr.source_loc(), "condition of `if`",
arena_->New<BoolType>(),
&if_expr.condition()->static_type()));
&if_expr.condition().static_type()));
// TODO: Compute the common type and convert both operands to it.
RETURN_IF_ERROR(TypeCheckExp(if_expr.then_expression(), impl_scope));
RETURN_IF_ERROR(TypeCheckExp(if_expr.else_expression(), impl_scope));
RETURN_IF_ERROR(TypeCheckExp(&if_expr.then_expression(), impl_scope));
RETURN_IF_ERROR(TypeCheckExp(&if_expr.else_expression(), impl_scope));
RETURN_IF_ERROR(
ExpectExactType(e->source_loc(), "expression of `if` expression",
&if_expr.then_expression()->static_type(),
&if_expr.else_expression()->static_type()));
e->set_static_type(&if_expr.then_expression()->static_type());
&if_expr.then_expression().static_type(),
&if_expr.else_expression().static_type()));
e->set_static_type(&if_expr.then_expression().static_type());
e->set_value_category(ValueCategory::Let);
return Success();
}