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
+1
View File
@@ -52,4 +52,5 @@ abstract class Expression : AstNode;
class TypeTypeLiteral : Expression;
class IdentifierExpression : Expression;
class IntrinsicExpression : Expression;
class IfExpression : Expression;
class UnimplementedExpression : Expression;
+7
View File
@@ -187,6 +187,13 @@ void Expression::Print(llvm::raw_ostream& out) const {
}
out << ")";
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();
break;
}
case ExpressionKind::UnimplementedExpression: {
const auto& unimplemented = cast<UnimplementedExpression>(*this);
out << "UnimplementedExpression<" << unimplemented.label() << ">(";
+29
View File
@@ -490,6 +490,35 @@ class IntrinsicExpression : public Expression {
Nonnull<TupleLiteral*> args_;
};
class IfExpression : public Expression {
public:
explicit IfExpression(SourceLocation source_loc,
Nonnull<Expression*> condition,
Nonnull<Expression*> then_expression,
Nonnull<Expression*> else_expression)
: Expression(AstNodeKind::IfExpression, source_loc),
condition_(condition),
then_expression_(then_expression),
else_expression_(else_expression) {}
static auto classof(const AstNode* node) -> bool {
return InheritsFromIfExpression(node->kind());
}
auto condition() const -> Nonnull<Expression*> { return condition_; }
auto then_expression() const -> Nonnull<Expression*> {
return then_expression_;
}
auto else_expression() const -> Nonnull<Expression*> {
return else_expression_;
}
private:
Nonnull<Expression*> condition_;
Nonnull<Expression*> then_expression_;
Nonnull<Expression*> else_expression_;
};
// An expression whose semantics have not been implemented. This can be used
// as a placeholder during development, in order to implement and test parsing
// of a new expression syntax without having to implement its semantics.