Skip to content

Commit 4a5df2d

Browse files
authored
Merge pull request #297 from 01mf02/try-catch-without-label
Implement `try`-`catch` without `label $x | ...`.
2 parents e776b61 + fe9e775 commit 4a5df2d

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

jaq-core/src/compile.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -672,15 +672,9 @@ impl<'s, F> Compiler<&'s str, F> {
672672
t
673673
}
674674
Num(n) => n.parse().map_or_else(|_| Term::Num(n.into()), Term::Int),
675-
// map `try f catch g` to `label $x | try f catch (g, break $x)`
676-
// and `try f` or `f?` to `label $x | try f catch ( break $x)`
677675
TryCatch(try_, catch) => {
678-
let catch = match catch {
679-
None => Break(""),
680-
Some(c) => BinOp(c, parse::BinaryOp::Comma, Break("").into()),
681-
};
682-
let tc = self.with_label("", |c| Term::TryCatch(c.iterm(*try_), c.iterm(catch)));
683-
Term::Label(self.lut.insert_term(tc))
676+
let catch = catch.map_or_else(|| Call("!empty", Vec::new()), |t| *t);
677+
Term::TryCatch(self.iterm(*try_), self.iterm(catch))
684678
}
685679
Fold(name, xs, pat, args) => {
686680
use self::Fold::{Foreach, Reduce};

jaq-core/src/filter.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,18 @@ impl<F: FilterT<F>> FilterT<F> for Id {
341341
Ast::ObjSingle(k, v) => Box::new(
342342
Self::cartesian(k, v, lut, cv).map(|(k, v)| Ok(Self::V::from_map([(k?, v?)])?)),
343343
),
344-
// TODO: write test for `try (break $x)`
345344
Ast::TryCatch(f, c) => {
346-
Box::new(f.run(lut, (cv.0.clone(), cv.1)).flat_map(move |y| match y {
347-
Err(Exn(exn::Inner::Err(e))) => c.run(lut, (cv.0.clone(), e.into_val())),
348-
y => box_once(y),
345+
let mut ys = f.run(lut, (cv.0.clone(), cv.1));
346+
let mut end: Option<ValXs<_>> = None;
347+
Box::new(core::iter::from_fn(move || match &mut end {
348+
Some(end) => end.next(),
349+
None => match ys.next()? {
350+
Err(Exn(exn::Inner::Err(e))) => {
351+
end = Some(c.run(lut, (cv.0.clone(), e.into_val())));
352+
end.as_mut().and_then(|end| end.next())
353+
}
354+
y => Some(y),
355+
},
349356
}))
350357
}
351358
Ast::Neg(f) => Box::new(f.run(lut, cv).map(|v| Ok((-v?)?))),

0 commit comments

Comments
 (0)