Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement match condition #861

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/formatter/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,7 @@ pub fn format_cst<'a>(
}
CstKind::MatchCase {
pattern,
condition: _, // TODO: format match case conditions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I opened a follow-up issue: #868

arrow,
body,
} => {
Expand Down
32 changes: 30 additions & 2 deletions compiler/frontend/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ pub struct Match {
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct MatchCase {
pub pattern: Box<Ast>,
pub condition: Option<Box<Ast>>,
pub body: Vec<Ast>,
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
Expand Down Expand Up @@ -248,7 +249,14 @@ impl FindAst for Match {
}
impl FindAst for MatchCase {
fn find(&self, id: &Id) -> Option<&Ast> {
self.pattern.find(id).or_else(|| self.body.find(id))
self.pattern
.find(id)
.or_else(|| {
self.condition
.as_ref()
.and_then(|box condition| condition.find(id))
})
.or_else(|| self.body.find(id))
}
}
impl FindAst for OrPattern {
Expand Down Expand Up @@ -352,8 +360,15 @@ impl CollectErrors for Ast {
expression.collect_errors(errors);
cases.collect_errors(errors);
}
AstKind::MatchCase(MatchCase { pattern, body }) => {
AstKind::MatchCase(MatchCase {
pattern,
condition,
body,
}) => {
pattern.collect_errors(errors);
if let Some(box condition) = condition {
condition.collect_errors(errors);
}
body.collect_errors(errors);
}
AstKind::OrPattern(OrPattern(patterns)) => {
Expand Down Expand Up @@ -540,9 +555,22 @@ impl ToRichIr for Match {
}
impl ToRichIr for MatchCase {
fn build_rich_ir(&self, builder: &mut RichIrBuilder) {
builder.push("case ", None, EnumSet::empty());
self.pattern.build_rich_ir(builder);
if let Some(box condition) = &self.condition {
builder.push(", ", None, EnumSet::empty());
builder.indent();
builder.push_newline();
builder.push("condition ", None, EnumSet::empty());
condition.build_rich_ir(builder);
}
builder.push(" -> ", None, EnumSet::empty());
builder.indent();
builder.push_foldable(|builder| builder.push_children_multiline(&self.body));
if self.condition.is_some() {
builder.dedent();
}
builder.dedent();
}
}
impl ToRichIr for OrPattern {
Expand Down
67 changes: 49 additions & 18 deletions compiler/frontend/src/ast_to_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,34 +385,65 @@ impl Context<'_> {
cases
.iter()
.map(|case| match &case.kind {
AstKind::MatchCase(MatchCase { box pattern, body }) => {
AstKind::MatchCase(MatchCase {
box pattern,
condition,
body,
}) => {
let (pattern, pattern_identifiers) = scope.lower_pattern(pattern);

let (body, ()) = scope.with_scope(None, |scope| {
for (name, (ast_id, identifier_id)) in
pattern_identifiers.clone()
{
scope.push(
ast_id,
Expression::PatternIdentifierReference(identifier_id),
name.clone(),
);
}
scope.compile(body.as_ref());
});

(pattern, body)
let (outer_body, (condition_body, body)) =
scope.with_scope(None, |scope| {
for (name, (ast_id, identifier_id)) in
pattern_identifiers.clone()
{
scope.push(
ast_id,
Expression::PatternIdentifierReference(
identifier_id,
),
name.clone(),
);
}

let condition = condition.as_ref().map(|box condition| {
scope
.with_scope(match_id.clone(), |condition_scope| {
condition_scope.compile_single(condition);
})
.0
});

let (body, ()) =
scope.with_scope(match_id.clone(), |scope| {
scope.compile(body);
});

(condition, body)
});

hir::MatchCase {
pattern,
identifier_expressions: outer_body,
condition: condition_body,
body,
}
}
AstKind::Error { errors } => {
let pattern = Pattern::Error {
errors: errors.clone(),
};

let (body, ()) = scope.with_scope(None, |scope| {
scope.compile(&[]);
let (body, inner_body) = scope.with_scope(None, |scope| {
scope.with_scope(match_id.clone(), |_scope| {}).0
});

(pattern, body)
hir::MatchCase {
pattern,
identifier_expressions: body,
condition: None,
body: inner_body,
}
}
_ => unreachable!("Expected match case in match cases, got {case:?}."),
})
Expand Down
1 change: 1 addition & 0 deletions compiler/frontend/src/cst/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum CstError {
ListNotClosed,
MatchCaseMissesArrow,
MatchCaseMissesBody,
MatchCaseMissesCondition,
MatchMissesCases,
OpeningParenthesisMissesExpression,
OrPatternMissesRight,
Expand Down
10 changes: 9 additions & 1 deletion compiler/frontend/src/cst/is_multiline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,17 @@ impl<D> IsMultiline for CstKind<D> {
} => expression.is_multiline() || percent.is_multiline() || cases.is_multiline(),
Self::MatchCase {
pattern,
condition,
arrow,
body,
} => pattern.is_multiline() || arrow.is_multiline() || body.is_multiline(),
} => {
pattern.is_multiline()
|| condition.as_deref().map_or(false, |(comma, condition)| {
comma.is_multiline() || condition.is_multiline()
})
|| arrow.is_multiline()
|| body.is_multiline()
}
Self::Function {
opening_curly_brace,
parameters_and_arrow,
Expand Down
Loading
Loading