Skip to content

Commit

Permalink
Convert definitions and main.
Browse files Browse the repository at this point in the history
  • Loading branch information
01mf02 committed Jun 27, 2024
1 parent 645dccf commit 3209191
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions jaq-syn/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,41 @@ pub struct Main<F = Filter> {
/// Body of the filter, e.g. `[.[] | f]`.
pub body: Spanned<F>,
}

use crate::parse;

impl From<&parse::Def<&str, parse::Term<&str>>> for Def {
fn from(def: &parse::Def<&str, parse::Term<&str>>) -> Self {
use alloc::string::ToString;
let args = def.args.iter().map(|arg| {
if let Some(v) = arg.strip_prefix('$') {
Arg::Var(v.to_string())
} else {
Arg::Fun(arg.to_string())
}
});
Def {
lhs: Call {
name: def.name.to_string(),
args: args.collect(),
},
rhs: (&def.body).into(),
}
}
}

impl From<&parse::Term<&str>> for Main {
fn from(tm: &parse::Term<&str>) -> Self {
use alloc::string::ToString;
match tm {
parse::Term::Def(defs, tm) => Main {
defs: defs.iter().map(Def::from).collect(),
body: ((&**tm).into(), 0..42),
},
tm => Main {
defs: Vec::new(),
body: ((&*tm).into(), 0..42),
},
}
}
}

0 comments on commit 3209191

Please sign in to comment.