Skip to content

Commit

Permalink
Implement compilation of enum patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
yannham committed Feb 14, 2024
1 parent 81018e4 commit af68e72
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions core/src/term/pattern/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,46 @@ impl CompilePart for RecordPattern {
}

impl CompilePart for EnumPattern {
fn compile_part(&self, _value_id: LocIdent, _bindings_id: LocIdent) -> RichTerm {
todo!()
fn compile_part(&self, value_id: LocIdent, bindings_id: LocIdent) -> RichTerm {
if let Some(pat) = &self.pattern {
// if %enum_is_variant% value_id then
// let value_id = %enum_unwrap_variant% value_id in
// <pattern.compile(value_id, bindings_id)>
// else
// null
make::if_then_else(
make::op1(UnaryOp::EnumIsVariant(), Term::Var(value_id)),
make::let_in(
value_id,
make::op1(UnaryOp::EnumUnwrapVariant(), Term::Var(value_id)),
pat.compile_part(value_id, bindings_id),
),
Term::Null,
)
} else {
// if %typeof% value_id == 'Enum && !(%enum_is_variant% value_id) then
// bindings_id
// else
// null
make::if_then_else(
mk_app!(
make::op1(
UnaryOp::BoolAnd(),
make::op2(
BinaryOp::Eq(),
make::op1(UnaryOp::Typeof(), Term::Var(value_id)),
Term::Enum("Enum".into()),
),
),
make::op1(
UnaryOp::BoolNot(),
make::op1(UnaryOp::EnumIsVariant(), Term::Var(value_id))
)
),
Term::Var(bindings_id),
Term::Null,
)
}
}
}

Expand Down

0 comments on commit af68e72

Please sign in to comment.