From af68e72f8b6a5f4c97234f6fbeb00d3697e91499 Mon Sep 17 00:00:00 2001 From: Yann Hamdaoui Date: Wed, 14 Feb 2024 09:40:55 +0100 Subject: [PATCH] Implement compilation of enum patterns --- core/src/term/pattern/compile.rs | 42 ++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/core/src/term/pattern/compile.rs b/core/src/term/pattern/compile.rs index e4dc0bf55d..f32c41ec85 100644 --- a/core/src/term/pattern/compile.rs +++ b/core/src/term/pattern/compile.rs @@ -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 + // + // 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, + ) + } } }