From 04c6cd248aff1cbce68ae2fb997705e921d21809 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 4 Apr 2019 18:12:19 +0200 Subject: Tweak matching on Some/None --- dhall_core/src/core.rs | 19 +++++++++++++++++++ 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(x: T) -> Rc { Rc::new(x) } +pub fn app(f: Expr, args: Vec>) -> Expr { + if args.is_empty() { + f + } else { + Expr::App(rc(f), args) + } +} + +pub fn app_rc( + f: SubExpr, + args: Vec>, +) -> SubExpr { + 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 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()) }, )); -- cgit v1.2.3