diff options
author | Nadrieril | 2019-04-04 18:12:19 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-04 18:12:19 +0200 |
commit | 04c6cd248aff1cbce68ae2fb997705e921d21809 (patch) | |
tree | 94f296ab1a70247e5019e76095ff4b0f0c060c04 | |
parent | 4fe44eea36eaf3cc21ef46740825e9aae4d1ea9e (diff) |
Tweak matching on Some/None
Diffstat (limited to '')
-rw-r--r-- | dhall_core/src/core.rs | 19 | ||||
-rw-r--r-- | dhall_core/src/parser.rs | 30 |
2 files changed, 28 insertions, 21 deletions
diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index 0ea8c83..afa3d3f 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -324,6 +324,25 @@ pub fn rc<T>(x: T) -> Rc<T> { Rc::new(x) } +pub fn app<N, E>(f: Expr<N, E>, args: Vec<SubExpr<N, E>>) -> Expr<N, E> { + if args.is_empty() { + f + } else { + Expr::App(rc(f), args) + } +} + +pub fn app_rc<N, E>( + f: SubExpr<N, E>, + args: Vec<SubExpr<N, E>>, +) -> SubExpr<N, E> { + if args.is_empty() { + f + } else { + rc(Expr::App(f, args)) + } +} + fn add_ui(u: usize, i: isize) -> usize { if i < 0 { u.checked_sub(i.checked_neg().unwrap() as usize).unwrap() diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index a0e4397..68796c4 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -700,28 +700,16 @@ make_parser! { rule!(application_expression<ParsedExpr> as expression; children!( [expression(e)] => e, - [expression(first), expression(second)] => { - match first { - Expr::Builtin(Builtin::OptionalNone) => - Expr::EmptyOptionalLit(rc(second)), - Expr::Builtin(Builtin::OptionalSome) => - Expr::NEOptionalLit(rc(second)), - _ => Expr::App(rc(first), vec![rc(second)]), - } + [expression(Expr::Builtin(Builtin::OptionalNone)), + expression(e), expression(rest)..] => { + app(Expr::EmptyOptionalLit(rc(e)), rest.map(rc).collect()) }, - [expression(first), expression(second), expression(rest)..] => { - match first { - Expr::Builtin(Builtin::OptionalNone) => - Expr::App(rc(Expr::EmptyOptionalLit(rc(second))), - rest.map(rc).collect()), - Expr::Builtin(Builtin::OptionalSome) => - Expr::App(rc(Expr::NEOptionalLit(rc(second))), - rest.map(rc).collect()), - _ => Expr::App(rc(first), - std::iter::once(rc(second)) - .chain(rest.map(rc)) - .collect()), - } + [expression(Expr::Builtin(Builtin::OptionalSome)), + expression(e), expression(rest)..] => { + app(Expr::NEOptionalLit(rc(e)), rest.map(rc).collect()) + }, + [expression(first), expression(rest)..] => { + app(first, rest.map(rc).collect()) }, )); |