Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
fix: use a macro instead of or_else
Browse files Browse the repository at this point in the history
  • Loading branch information
aripiprazole committed Jan 13, 2024
1 parent 264216e commit dcb227b
Showing 1 changed file with 20 additions and 9 deletions.
29 changes: 20 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,9 @@ pub mod list {

impl ExprKind for List {
fn try_new(term: Term) -> Result<Option<Expr>> {
println!("podim");
if let Term::Vec(ref vec) | Term::SrcPos(_, box Term::Vec(ref vec)) = term {
println!("pao de acucar");
let items = vec.clone().into();
return Ok(Some(List(term.transport(items)).into()));
}
Expand Down Expand Up @@ -439,15 +441,15 @@ impl TryFrom<Term> for Expr {
type Error = SemanticError;

fn try_from(value: Term) -> Result<Self, Self::Error> {
DefMacro::try_new(value.clone())
.or_else(|_| Recur::try_new(value.clone()))
.or_else(|_| Def::try_new(value.clone()))
.or_else(|_| Fun::try_new(value.clone()))
.or_else(|_| Quote::try_new(value.clone()))
.or_else(|_| Apply::try_new(value.clone()))
.or_else(|_| List::try_new(value.clone()))
.or_else(|_| Literal::try_new(value.clone()))
.and_then(|value| value.ok_or(SemanticError::FailedToMatch))
try_expr!(DefMacro, value);
try_expr!(Def, value);
try_expr!(Recur, value);
try_expr!(Fun, value);
try_expr!(Quote, value);
try_expr!(Apply, value);
try_expr!(List, value);
try_expr!(Literal, value);
Err(SemanticError::FailedToMatch)
}
}

Expand Down Expand Up @@ -475,6 +477,15 @@ impl From<Vec<Term>> for Term {
}
}

#[macro_export]
macro_rules! try_expr {
($name:ident, $value:expr) => {
if let Ok(Some(value)) = $name::try_new($value.clone()) {
return Ok(value);
}
};
}

#[macro_export]
macro_rules! soft_vec {
($($expr:expr),*) => {
Expand Down

0 comments on commit dcb227b

Please sign in to comment.