Skip to content

Commit

Permalink
add a deconstructor binder for pattern matching
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenthz committed Feb 17, 2024
1 parent a3b3654 commit a5b755a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
3 changes: 3 additions & 0 deletions werbolg-compile/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ fn generate_expression_code<'a, L: Clone + Eq + core::hash::Hash>(
// TODO, not sure ignore one is the best to do here
state.write_code().push(Instruction::IgnoreOne);
}
ir::Binder::Deconstruct(_name, _) => {
todo!()
}
}
let tc = generate_expression_code(state, local, funpos, *in_expr)?;
Ok(tc)
Expand Down
20 changes: 20 additions & 0 deletions werbolg-core/src/ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,26 @@ pub enum Binder {
Ignore,
/// equivalent of `let $ident = ...`
Ident(Ident),
/// equivalent of `let <path> (<sequential-binder>) = ...` or `let <path> {<named-binder>} = ...`
Deconstruct(Path, FieldsBinder),
}

/// Fields to bind inside a struct or enum
#[derive(Clone, Debug)]
pub enum FieldsBinder {
/// Sequential fields binder, where the fields are bind sequentially in order of declaration
Sequential(Vec<Binder>, BindEllipsis),
/// Named fields binder, where the fields are bound by their name
Named(Vec<(Ident, Binder)>, BindEllipsis),
}

/// Whether to only allow partially bind for struct/enum
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum BindEllipsis {
/// Expect all the fields of a struct/enum to be matched
No,
/// Similar to `...` in a sequence/tuple matching, ignore all the unspecified field for matching
Yes,
}

/// Expression
Expand Down

0 comments on commit a5b755a

Please sign in to comment.