diff options
author | Nadrieril | 2019-03-16 22:41:22 +0100 |
---|---|---|
committer | Nadrieril | 2019-03-16 22:41:22 +0100 |
commit | 0f33caf4c1ee4d1f95d6ac3a41b5cf2f8efa7b54 (patch) | |
tree | 36607a1eb1eaf327388d2026d192782cdb6d130d | |
parent | 5692bf2c8a7acfb90a5d03d0bd360c105ba2a72b (diff) |
Use Box more uniformly in AST
Closes #28
-rw-r--r-- | dhall/src/binary.rs | 145 | ||||
-rw-r--r-- | dhall/src/normalize.rs | 38 | ||||
-rw-r--r-- | dhall/src/typecheck.rs | 26 | ||||
-rw-r--r-- | dhall_core/src/core.rs | 78 | ||||
-rw-r--r-- | dhall_core/src/parser.rs | 38 | ||||
-rw-r--r-- | dhall_generator/src/lib.rs | 10 |
6 files changed, 171 insertions, 164 deletions
diff --git a/dhall/src/binary.rs b/dhall/src/binary.rs index cc07431..fbd343b 100644 --- a/dhall/src/binary.rs +++ b/dhall/src/binary.rs @@ -2,7 +2,7 @@ use dhall_core::*; use itertools::*; use serde_cbor::value::value as cbor; -type ParsedExpr = Expr<X, Import>; +type ParsedExpr = Box<Expr<X, Import>>; #[derive(Debug)] pub enum DecodeError { @@ -21,24 +21,24 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<ParsedExpr, DecodeError> { use cbor::Value::*; use dhall_core::{BinOp, Builtin, Const}; use Expr::*; - match data { + let e = match data { String(s) => match Builtin::parse(s) { - Some(b) => Ok(Expr::Builtin(b)), + Some(b) => Expr::Builtin(b), None => match s.as_str() { - "True" => Ok(BoolLit(true)), - "False" => Ok(BoolLit(false)), - "Type" => Ok(Const(Const::Type)), - "Kind" => Ok(Const(Const::Kind)), - s => Ok(Var(V(Label::from(s), 0))), + "True" => BoolLit(true), + "False" => BoolLit(false), + "Type" => Const(Const::Type), + "Kind" => Const(Const::Kind), + s => Var(V(Label::from(s), 0)), }, }, - U64(n) => Ok(Var(V(Label::from("_"), *n as usize))), - F64(x) => Ok(DoubleLit(*x)), - Bool(b) => Ok(BoolLit(*b)), + U64(n) => Var(V(Label::from("_"), *n as usize)), + F64(x) => DoubleLit(*x), + Bool(b) => BoolLit(*b), Array(vec) => match vec.as_slice() { [String(l), U64(n)] => { let l = Label::from(l.as_str()); - Ok(Var(V(l, *n as usize))) + Var(V(l, *n as usize)) } [U64(0), f, args..] => { let f = cbor_value_to_dhall(&f)?; @@ -46,33 +46,33 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<ParsedExpr, DecodeError> { .iter() .map(cbor_value_to_dhall) .collect::<Result<Vec<_>, _>>()?; - Ok(App(bx(f), args)) + App(f, args) } [U64(1), x, y] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); - Ok(Lam(Label::from("_"), x, y)) + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; + Lam(Label::from("_"), x, y) } [U64(1), String(l), x, y] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; let l = Label::from(l.as_str()); - Ok(Lam(l, x, y)) + Lam(l, x, y) } [U64(2), x, y] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); - Ok(Pi(Label::from("_"), x, y)) + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; + Pi(Label::from("_"), x, y) } [U64(2), String(l), x, y] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; let l = Label::from(l.as_str()); - Ok(Pi(l, x, y)) + Pi(l, x, y) } [U64(3), U64(n), x, y] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; use BinOp::*; let op = match n { 0 => BoolOr, @@ -89,77 +89,77 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<ParsedExpr, DecodeError> { 11 => ImportAlt, _ => Err(DecodeError::WrongFormatError)?, }; - Ok(BinOp(op, x, y)) + BinOp(op, x, y) } [U64(4), t] => { - let t = bx(cbor_value_to_dhall(&t)?); - Ok(ListLit(Some(t), vec![])) + let t = cbor_value_to_dhall(&t)?; + ListLit(Some(t), vec![]) } [U64(4), Null, rest..] => { let rest = rest .iter() .map(cbor_value_to_dhall) .collect::<Result<Vec<_>, _>>()?; - Ok(ListLit(None, rest)) + ListLit(None, rest) } [U64(5), t] => { - let t = bx(cbor_value_to_dhall(&t)?); - Ok(OptionalLit(Some(t), None)) + let t = cbor_value_to_dhall(&t)?; + OptionalLit(Some(t), None) } [U64(5), Null, x] => { - let x = bx(cbor_value_to_dhall(&x)?); - Ok(OptionalLit(None, Some(x))) + let x = cbor_value_to_dhall(&x)?; + OptionalLit(None, Some(x)) } [U64(5), t, x] => { - let x = bx(cbor_value_to_dhall(&x)?); - let t = bx(cbor_value_to_dhall(&t)?); - Ok(OptionalLit(Some(t), Some(x))) + let x = cbor_value_to_dhall(&x)?; + let t = cbor_value_to_dhall(&t)?; + OptionalLit(Some(t), Some(x)) } [U64(6), x, y] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); - Ok(Merge(x, y, None)) + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; + Merge(x, y, None) } [U64(6), x, y, z] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); - let z = bx(cbor_value_to_dhall(&z)?); - Ok(Merge(x, y, Some(z))) + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; + let z = cbor_value_to_dhall(&z)?; + Merge(x, y, Some(z)) } [U64(7), Object(map)] => { let map = cbor_map_to_dhall_map(map)?; - Ok(Record(map)) + Record(map) } [U64(8), Object(map)] => { let map = cbor_map_to_dhall_map(map)?; - Ok(RecordLit(map)) + RecordLit(map) } [U64(9), x, String(l)] => { - let x = bx(cbor_value_to_dhall(&x)?); + let x = cbor_value_to_dhall(&x)?; let l = Label::from(l.as_str()); - Ok(Field(x, l)) + Field(x, l) } [U64(11), Object(map)] => { let map = cbor_map_to_dhall_map(map)?; - Ok(Union(map)) + Union(map) } [U64(12), String(l), x, Object(map)] => { let map = cbor_map_to_dhall_map(map)?; - let x = bx(cbor_value_to_dhall(&x)?); + let x = cbor_value_to_dhall(&x)?; let l = Label::from(l.as_str()); - Ok(UnionLit(l, x, map)) + UnionLit(l, x, map) } [U64(14), x, y, z] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); - let z = bx(cbor_value_to_dhall(&z)?); - Ok(BoolIf(x, y, z)) - } - [U64(15), U64(x)] => Ok(NaturalLit(*x as Natural)), - [U64(16), U64(x)] => Ok(IntegerLit(*x as Integer)), - [U64(16), I64(x)] => Ok(IntegerLit(*x as Integer)), + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; + let z = cbor_value_to_dhall(&z)?; + BoolIf(x, y, z) + } + [U64(15), U64(x)] => NaturalLit(*x as Natural), + [U64(16), U64(x)] => IntegerLit(*x as Integer), + [U64(16), I64(x)] => IntegerLit(*x as Integer), [U64(18), String(first), rest..] => { - Ok(TextLit(InterpolatedText::from(( + TextLit(InterpolatedText::from(( first.clone(), rest.iter() .tuples() @@ -172,7 +172,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<ParsedExpr, DecodeError> { Ok((x, y)) }) .collect::<Result<_, _>>()?, - )))) + ))) } [U64(25), bindings..] => { let mut tuples = bindings.iter().tuples(); @@ -184,9 +184,9 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<ParsedExpr, DecodeError> { }; let t = match t { Null => None, - t => Some(bx(cbor_value_to_dhall(&t)?)), + t => Some(cbor_value_to_dhall(&t)?), }; - let v = bx(cbor_value_to_dhall(&v)?); + let v = cbor_value_to_dhall(&v)?; Ok((x, t, v)) }) .collect::<Result<Vec<_>, _>>()?; @@ -195,19 +195,20 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<ParsedExpr, DecodeError> { .next() .ok_or(DecodeError::WrongFormatError)?; let expr = cbor_value_to_dhall(expr)?; - Ok(bindings + return Ok(bindings .into_iter() - .fold(expr, |acc, (x, t, v)| Let(x, t, v, bx(acc)))) + .fold(expr, |acc, (x, t, v)| bx(Let(x, t, v, acc)))); } [U64(26), x, y] => { - let x = bx(cbor_value_to_dhall(&x)?); - let y = bx(cbor_value_to_dhall(&y)?); - Ok(Annot(x, y)) + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; + Annot(x, y) } - _ => Err(DecodeError::WrongFormatError), + _ => Err(DecodeError::WrongFormatError)?, }, - _ => Err(DecodeError::WrongFormatError), - } + _ => Err(DecodeError::WrongFormatError)?, + }; + Ok(bx(e)) } fn cbor_map_to_dhall_map( diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 9105ec4..c49d313 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -44,8 +44,8 @@ where (Builtin(b), args) => match ( b, args.iter() - .map(normalize_whnf) - .collect::<Vec<_>>() + .map(|x| normalize_whnf(&*x)) + .collect::<Vec<Expr<_, _>>>() .as_slice(), ) { (OptionalSome, [a]) => OptionalLit(None, Some(bx(a.clone()))), @@ -57,10 +57,10 @@ where (NaturalShow, [NaturalLit(n)]) => TextLit(n.to_string().into()), (ListLength, [_, ListLit(_, ys)]) => NaturalLit(ys.len()), (ListHead, [_, ListLit(t, ys)]) => { - OptionalLit(t.clone(), ys.iter().cloned().map(bx).next()) + OptionalLit(t.clone(), ys.iter().cloned().next()) } (ListLast, [_, ListLit(t, ys)]) => { - OptionalLit(t.clone(), ys.iter().cloned().map(bx).last()) + OptionalLit(t.clone(), ys.iter().cloned().last()) } (ListReverse, [_, ListLit(t, ys)]) => { let xs = ys.iter().rev().cloned().collect(); @@ -72,11 +72,11 @@ where App(f, args) => match (&**f, args.as_slice()) { (Builtin(ListFold), [_, x, rest..]) => { return normalize_whnf(&App( - bx(x.clone()), + x.clone(), rest.to_vec(), )) } - (f, args) => app(f.clone(), args.to_vec()), + (_, args) => App(f.clone(), args.to_vec()), }, g => g.clone(), }; @@ -93,11 +93,11 @@ where App(f, args) => match (&**f, args.as_slice()) { (Builtin(OptionalFold), [_, x, rest..]) => { return normalize_whnf(&App( - bx(x.clone()), + x.clone(), rest.to_vec(), )) } - (f, args) => app(f.clone(), args.to_vec()), + (_, args) => App(f.clone(), args.to_vec()), }, g => g.clone(), }; @@ -109,8 +109,8 @@ where } (ListFold, [_, ListLit(_, xs), _, cons, nil]) => { let e2: Expr<_, _> = - xs.into_iter().rev().fold((*nil).clone(), |acc, x| { - let x = bx((x).clone()); + xs.iter().rev().fold((*nil).clone(), |acc, x| { + let x = (x).clone(); let acc = bx((acc).clone()); let cons = bx((cons).clone()); dhall_expr!(cons x acc) @@ -122,7 +122,7 @@ where // normalize_whnf(&App(bx(x.clone()), rest.to_vec())) // } (OptionalFold, [_, OptionalLit(_, Some(x)), _, just, _]) => { - let x = bx((**x).clone()); + let x = x.clone(); let just = bx(just.clone()); normalize_whnf(&dhall_expr!(just x)) } @@ -137,11 +137,11 @@ where match (&**f, args.as_slice()) { // fold/build fusion (Builtin(NaturalFold), [x, rest..]) => { - normalize_whnf(&App(bx(x.clone()), rest.to_vec())) + normalize_whnf(&App(x.clone(), rest.to_vec())) } - (f, args) => app( + (_, args) => app( Builtin(NaturalBuild), - vec![app(f.clone(), args.to_vec())], + vec![bx(App(f.clone(), args.to_vec()))], ), } } @@ -149,15 +149,17 @@ where match (&**f, args.as_slice()) { // fold/build fusion (Builtin(NaturalBuild), [x, rest..]) => { - normalize_whnf(&App(bx(x.clone()), rest.to_vec())) + normalize_whnf(&App(x.clone(), rest.to_vec())) } - (f, args) => app( + (_, args) => app( Builtin(NaturalFold), - vec![app(f.clone(), args.to_vec())], + vec![bx(App(f.clone(), args.to_vec()))], ), } } - (b, args) => App(bx(Builtin(b)), args.to_vec()), + (b, args) => { + App(bx(Builtin(b)), args.iter().cloned().map(bx).collect()) + } }, (f, args) => App(bx(f), args.to_vec()), }, diff --git a/dhall/src/typecheck.rs b/dhall/src/typecheck.rs index 0f87d67..ca0d5af 100644 --- a/dhall/src/typecheck.rs +++ b/dhall/src/typecheck.rs @@ -270,7 +270,7 @@ where Err(TypeError::new( ctx, e, - TypeMismatch((**f).clone(), nf_A, (*a).clone(), nf_A2), + TypeMismatch((**f).clone(), nf_A, (**a).clone(), nf_A2), )) } } @@ -436,7 +436,7 @@ where return Err(TypeError::new( ctx, e, - InvalidListElement(i, nf_t, x.clone(), nf_t2), + InvalidListElement(i, nf_t, (**x).clone(), nf_t2), )); } } @@ -529,7 +529,7 @@ where return Err(TypeError::new( ctx, e, - InvalidFieldType((*k).clone(), (*t).clone()), + InvalidFieldType((*k).clone(), (**t).clone()), )); } } @@ -548,11 +548,11 @@ where return Err(TypeError::new( ctx, e, - InvalidField((*k).clone(), (*v).clone()), + InvalidField((*k).clone(), (**v).clone()), )); } } - Ok(((*k).clone(), t)) + Ok(((*k).clone(), bx(t))) }) .collect::<Result<_, _>>()?; Ok(Record(kts)) @@ -641,13 +641,15 @@ where Field(ref r, ref x) => { let t = normalize(&type_with(ctx, r)?); match t { - Record(ref kts) => kts.get(x).cloned().ok_or_else(|| { - TypeError::new( - ctx, - e, - MissingField((*x).clone(), t.clone()), - ) - }), + Record(ref kts) => { + kts.get(x).map(|x| &**x).cloned().ok_or_else(|| { + TypeError::new( + ctx, + e, + MissingField((*x).clone(), t.clone()), + ) + }) + } _ => Err(TypeError::new( ctx, e, diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index a7b6b53..34738bb 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -176,13 +176,13 @@ pub enum BinOp { #[derive(Debug, Clone, PartialEq)] pub struct InterpolatedText<Note, Embed> { head: String, - tail: Vec<(Expr<Note, Embed>, String)>, + tail: Vec<(Box<Expr<Note, Embed>>, String)>, } -impl<N, E> From<(String, Vec<(Expr<N, E>, String)>)> +impl<N, E> From<(String, Vec<(Box<Expr<N, E>>, String)>)> for InterpolatedText<N, E> { - fn from(x: (String, Vec<(Expr<N, E>, String)>)) -> Self { + fn from(x: (String, Vec<(Box<Expr<N, E>>, String)>)) -> Self { InterpolatedText { head: x.0, tail: x.1, @@ -203,7 +203,7 @@ impl<N, E> From<String> for InterpolatedText<N, E> { // This one is needed when parsing, because we need to own the Expr pub enum OwnedInterpolatedTextContents<'a, Note, Embed> { Text(&'a str), - Expr(Expr<Note, Embed>), + Expr(Box<Expr<Note, Embed>>), } // This one is needed everywhere else, because we don't want Clone traits bounds @@ -222,7 +222,7 @@ impl<'a, N: Clone + 'a, E: Clone + 'a> OwnedInterpolatedTextContents::Text(s) } BorrowedInterpolatedTextContents::Expr(e) => { - OwnedInterpolatedTextContents::Expr(e.clone()) + OwnedInterpolatedTextContents::Expr(bx(e.clone())) } } } @@ -231,7 +231,7 @@ impl<'a, N: Clone + 'a, E: Clone + 'a> impl<N, E> InterpolatedText<N, E> { pub fn map<N2, E2, F>(&self, mut f: F) -> InterpolatedText<N2, E2> where - F: FnMut(&Expr<N, E>) -> Expr<N2, E2>, + F: FnMut(&Box<Expr<N, E>>) -> Box<Expr<N2, E2>>, { InterpolatedText { head: self.head.clone(), @@ -304,7 +304,7 @@ pub enum Expr<Note, Embed> { /// `Pi x A B ~ ∀(x : A) -> B` Pi(Label, Box<Expr<Note, Embed>>, Box<Expr<Note, Embed>>), /// `App f A ~ f A` - App(Box<Expr<Note, Embed>>, Vec<Expr<Note, Embed>>), + App(Box<Expr<Note, Embed>>, Vec<Box<Expr<Note, Embed>>>), /// `Let x Nothing r e ~ let x = r in e` /// `Let x (Just t) r e ~ let x : t = r in e` Let( @@ -336,7 +336,7 @@ pub enum Expr<Note, Embed> { /// `TextLit t ~ t` TextLit(InterpolatedText<Note, Embed>), /// `ListLit t [x, y, z] ~ [x, y, z] : List t` - ListLit(Option<Box<Expr<Note, Embed>>>, Vec<Expr<Note, Embed>>), + ListLit(Option<Box<Expr<Note, Embed>>>, Vec<Box<Expr<Note, Embed>>>), /// `OptionalLit t [e] ~ [e] : Optional t` /// `OptionalLit t [] ~ [] : Optional t` OptionalLit( @@ -344,16 +344,16 @@ pub enum Expr<Note, Embed> { Option<Box<Expr<Note, Embed>>>, ), /// `Record [(k1, t1), (k2, t2)] ~ { k1 : t1, k2 : t1 }` - Record(BTreeMap<Label, Expr<Note, Embed>>), + Record(BTreeMap<Label, Box<Expr<Note, Embed>>>), /// `RecordLit [(k1, v1), (k2, v2)] ~ { k1 = v1, k2 = v2 }` - RecordLit(BTreeMap<Label, Expr<Note, Embed>>), + RecordLit(BTreeMap<Label, Box<Expr<Note, Embed>>>), /// `Union [(k1, t1), (k2, t2)] ~ < k1 : t1, k2 : t2 >` - Union(BTreeMap<Label, Expr<Note, Embed>>), + Union(BTreeMap<Label, Box<Expr<Note, Embed>>>), /// `UnionLit (k1, v1) [(k2, t2), (k3, t3)] ~ < k1 = t1, k2 : t2, k3 : t3 >` UnionLit( Label, Box<Expr<Note, Embed>>, - BTreeMap<Label, Expr<Note, Embed>>, + BTreeMap<Label, Box<Expr<Note, Embed>>>, ), /// `Merge x y t ~ merge x y : t` Merge( @@ -848,12 +848,15 @@ where Expr::Pi(var.into(), bx(ty.into()), bx(value.into())) } -pub fn app<S, A, Ef, Ex>(f: Ef, x: Vec<Ex>) -> Expr<S, A> +pub fn app<S, A, Ef, Ex>(f: Ef, x: Vec<Box<Ex>>) -> Expr<S, A> where Ef: Into<Expr<S, A>>, Ex: Into<Expr<S, A>>, { - Expr::App(bx(f.into()), x.into_iter().map(|x| x.into()).collect()) + Expr::App( + bx(f.into()), + x.into_iter().map(|x| bx((*x).into())).collect(), + ) } pub type Double = f64; @@ -902,6 +905,7 @@ where { use crate::Expr::*; let bxmap = |x: &Expr<S, A>| -> Box<Expr<T, B>> { bx(map(x)) }; + let bxbxmap = |x: &Box<Expr<S, A>>| -> Box<Expr<T, B>> { bx(map(&**x)) }; let opt = |x| map_opt_box(x, &map); match *e { Const(k) => Const(k), @@ -909,7 +913,7 @@ where Lam(ref x, ref t, ref b) => Lam(map_label(x), bxmap(t), bxmap(b)), Pi(ref x, ref t, ref b) => Pi(map_label(x), bxmap(t), bxmap(b)), App(ref f, ref args) => { - let args = args.iter().map(&map).collect(); + let args = args.iter().map(bxbxmap).collect(); App(bxmap(f), args) } Let(ref l, ref t, ref a, ref b) => { @@ -922,24 +926,26 @@ where NaturalLit(n) => NaturalLit(n), IntegerLit(n) => IntegerLit(n), DoubleLit(n) => DoubleLit(n), - TextLit(ref t) => TextLit(t.map(|e| map(e))), + TextLit(ref t) => TextLit(t.map(&bxbxmap)), BinOp(o, ref x, ref y) => BinOp(o, bxmap(x), bxmap(y)), ListLit(ref t, ref es) => { - let es = es.iter().map(&map).collect(); + let es = es.iter().map(&bxbxmap).collect(); ListLit(opt(t), es) } OptionalLit(ref t, ref es) => OptionalLit(opt(t), opt(es)), Record(ref kts) => { - Record(map_record_value_and_keys(kts, map, map_label)) + Record(map_record_value_and_keys(kts, bxbxmap, map_label)) } RecordLit(ref kvs) => { - RecordLit(map_record_value_and_keys(kvs, map, map_label)) + RecordLit(map_record_value_and_keys(kvs, bxbxmap, map_label)) + } + Union(ref kts) => { + Union(map_record_value_and_keys(kts, bxbxmap, map_label)) } - Union(ref kts) => Union(map_record_value_and_keys(kts, map, map_label)), UnionLit(ref k, ref v, ref kvs) => UnionLit( map_label(k), bxmap(v), - map_record_value_and_keys(kvs, map, map_label), + map_record_value_and_keys(kvs, bxbxmap, map_label), ), Merge(ref x, ref y, ref t) => Merge(bxmap(x), bxmap(y), opt(t)), Field(ref r, ref x) => Field(bxmap(r), map_label(x)), @@ -1087,7 +1093,7 @@ pub fn shift<S, T, A: Clone>(d: isize, v: &V, e: &Expr<S, A>) -> Expr<T, A> { } App(f, args) => { let f = shift(d, v, f); - let args = args.iter().map(|a| shift(d, v, a)).collect(); + let args = args.iter().map(|a| bx(shift(d, v, a))).collect(); app(f, args) } Let(f, mt, r, e) => { @@ -1107,22 +1113,24 @@ pub fn shift<S, T, A: Clone>(d: isize, v: &V, e: &Expr<S, A>) -> Expr<T, A> { NaturalLit(a) => NaturalLit(*a), IntegerLit(a) => IntegerLit(*a), DoubleLit(a) => DoubleLit(*a), - TextLit(a) => TextLit(a.map(|e| shift(d, v, e))), + TextLit(a) => TextLit(a.map(|e| bx(shift(d, v, &*e)))), ListLit(t, es) => ListLit( t.as_ref().map(|t| bx(shift(d, v, t))), - es.iter().map(|e| shift(d, v, e)).collect(), + es.iter().map(|e| bx(shift(d, v, e))).collect(), ), OptionalLit(t, e) => OptionalLit( t.as_ref().map(|t| bx(shift(d, v, t))), e.as_ref().map(|t| bx(shift(d, v, t))), ), - Record(a) => Record(map_record_value(a, |val| shift(d, v, val))), - RecordLit(a) => RecordLit(map_record_value(a, |val| shift(d, v, val))), - Union(a) => Union(map_record_value(a, |val| shift(d, v, val))), + Record(a) => Record(map_record_value(a, |val| bx(shift(d, v, &*val)))), + RecordLit(a) => { + RecordLit(map_record_value(a, |val| bx(shift(d, v, &*val)))) + } + Union(a) => Union(map_record_value(a, |val| bx(shift(d, v, &*val)))), UnionLit(k, uv, a) => UnionLit( k.clone(), bx(shift(d, v, uv)), - map_record_value(a, |val| shift(d, v, val)), + map_record_value(a, |val| bx(shift(d, v, &*val))), ), Merge(a, b, c) => Merge( bx(shift(d, v, a)), @@ -1182,7 +1190,7 @@ where } App(f, args) => { let f2 = subst(v, e, f); - let args = args.iter().map(|a| subst(v, e, a)).collect(); + let args = args.iter().map(|a| bx(subst(v, e, a))).collect(); app(f2, args) } Var(v2) => { @@ -1210,10 +1218,10 @@ where NaturalLit(a) => NaturalLit(*a), IntegerLit(a) => IntegerLit(*a), DoubleLit(a) => DoubleLit(*a), - TextLit(a) => TextLit(a.map(|b| subst(v, e, b))), + TextLit(a) => TextLit(a.map(|b| bx(subst(v, e, &*b)))), ListLit(a, b) => { let a2 = a.as_ref().map(|a| bx(subst(v, e, a))); - let b2 = b.iter().map(|be| subst(v, e, be)).collect(); + let b2 = b.iter().map(|be| bx(subst(v, e, be))).collect(); ListLit(a2, b2) } OptionalLit(a, b) => { @@ -1221,15 +1229,15 @@ where let b2 = b.as_ref().map(|a| bx(subst(v, e, a))); OptionalLit(a2, b2) } - Record(kts) => Record(map_record_value(kts, |t| subst(v, e, t))), + Record(kts) => Record(map_record_value(kts, |t| bx(subst(v, e, &*t)))), RecordLit(kvs) => { - RecordLit(map_record_value(kvs, |val| subst(v, e, val))) + RecordLit(map_record_value(kvs, |val| bx(subst(v, e, &*val)))) } - Union(kts) => Union(map_record_value(kts, |t| subst(v, e, t))), + Union(kts) => Union(map_record_value(kts, |t| bx(subst(v, e, &*t)))), UnionLit(k, uv, kvs) => UnionLit( k.clone(), bx(subst(v, e, uv)), - map_record_value(kvs, |val| subst(v, e, val)), + map_record_value(kvs, |val| bx(subst(v, e, &*val))), ), Merge(a, b, c) => Merge( bx(subst(v, e, a)), diff --git a/dhall_core/src/parser.rs b/dhall_core/src/parser.rs index 9cbd878..b2955fb 100644 --- a/dhall_core/src/parser.rs +++ b/dhall_core/src/parser.rs @@ -437,7 +437,7 @@ rule!(double_quote_literal<ParsedText>; // TODO: parse escapes rule!(double_quote_chunk<ParsedTextContents<'a>>; children!(c: interpolation) => { - OwnedInterpolatedTextContents::Expr(*c) + OwnedInterpolatedTextContents::Expr(c) }, captured_str!(s) => { OwnedInterpolatedTextContents::Text(s) @@ -461,7 +461,7 @@ rule!(interpolation<BoxExpr>; rule!(single_quote_continue<Vec<ParsedTextContents<'a>>>; children!(c: interpolation, rest: single_quote_continue) => { - rest.push(OwnedInterpolatedTextContents::Expr(*c)); rest + rest.push(OwnedInterpolatedTextContents::Expr(c)); rest }, children!(c: escaped_quote_pair, rest: single_quote_continue) => { rest.push(OwnedInterpolatedTextContents::Text(c)); rest @@ -746,7 +746,7 @@ rule!(annotated_expression<BoxExpr>; rule!(application_expression<BoxExpr>; children!(first: expression, rest*: expression) => { - let rest: Vec<_> = rest.map(|x| *x).collect(); + let rest: Vec<_> = rest.collect(); if rest.is_empty() { first } else { @@ -799,27 +799,23 @@ rule!(empty_record_type<BoxExpr>; rule!(non_empty_record_type_or_literal<BoxExpr>; children!(first_label: label, rest: non_empty_record_type) => { let (first_expr, mut map) = rest; - map.insert(first_label, *first_expr); + map.insert(first_label, first_expr); bx(Expr::Record(map)) }, children!(first_label: label, rest: non_empty_record_literal) => { let (first_expr, mut map) = rest; - map.insert(first_label, *first_expr); + map.insert(first_label, first_expr); bx(Expr::RecordLit(map)) }, ); -rule!(non_empty_record_type<(BoxExpr, BTreeMap<Label, ParsedExpr>)>; +rule!(non_empty_record_type<(BoxExpr, BTreeMap<Label, BoxExpr>)>; self!(x: partial_record_entries) => x ); -named!(partial_record_entries<(BoxExpr, BTreeMap<Label, ParsedExpr>)>; +named!(partial_record_entries<(BoxExpr, BTreeMap<Label, BoxExpr>)>; children!(expr: expression, entries*: record_entry) => { - let mut map: BTreeMap<Label, ParsedExpr> = BTreeMap::new(); - for (n, e) in entries { - map.insert(n, *e); - } - (expr, map) + (expr, entries.collect()) } ); @@ -827,7 +823,7 @@ named!(record_entry<(Label, BoxExpr)>; children!(name: label, expr: expression) => (name, expr) ); -rule!(non_empty_record_literal<(BoxExpr, BTreeMap<Label, ParsedExpr>)>; +rule!(non_empty_record_literal<(BoxExpr, BTreeMap<Label, BoxExpr>)>; self!(x: partial_record_entries) => x ); @@ -846,29 +842,25 @@ rule!(union_type_or_literal<BoxExpr>; rule!(empty_union_type<()>; children!() => ()); rule!(non_empty_union_type_or_literal - <(Option<(Label, BoxExpr)>, BTreeMap<Label, ParsedExpr>)>; + <(Option<(Label, BoxExpr)>, BTreeMap<Label, BoxExpr>)>; children!(l: label, e: expression, entries: union_type_entries) => { (Some((l, e)), entries) }, children!(l: label, e: expression, rest: non_empty_union_type_or_literal) => { let (x, mut entries) = rest; - entries.insert(l, *e); + entries.insert(l, e); (x, entries) }, children!(l: label, e: expression) => { let mut entries = BTreeMap::new(); - entries.insert(l, *e); + entries.insert(l, e); (None, entries) }, ); -rule!(union_type_entries<BTreeMap<Label, ParsedExpr>>; +rule!(union_type_entries<BTreeMap<Label, BoxExpr>>; children!(entries*: union_type_entry) => { - let mut map: BTreeMap<Label, ParsedExpr> = BTreeMap::new(); - for (n, e) in entries { - map.insert(n, *e); - } - map + entries.collect() } ); @@ -878,7 +870,7 @@ rule!(union_type_entry<(Label, BoxExpr)>; rule!(non_empty_list_literal_raw<BoxExpr>; children!(items*: expression) => { - bx(Expr::ListLit(None, items.map(|x| *x).collect())) + bx(Expr::ListLit(None, items.collect())) } ); diff --git a/dhall_generator/src/lib.rs b/dhall_generator/src/lib.rs index 1d51f9e..1a74f4f 100644 --- a/dhall_generator/src/lib.rs +++ b/dhall_generator/src/lib.rs @@ -125,12 +125,14 @@ fn label_to_tokenstream(l: &Label) -> TokenStream { } fn map_to_tokenstream( - m: &BTreeMap<Label, Expr<X, X>>, + m: &BTreeMap<Label, Box<Expr<X, X>>>, ctx: &Context<Label, ()>, ) -> TokenStream { let (keys, values): (Vec<TokenStream>, Vec<TokenStream>) = m .iter() - .map(|(k, v)| (label_to_tokenstream(k), dhall_to_tokenstream(v, ctx))) + .map(|(k, v)| { + (label_to_tokenstream(k), dhall_to_tokenstream_bx(&*v, ctx)) + }) .unzip(); quote! { { let mut m = BTreeMap::new(); @@ -151,10 +153,10 @@ fn option_to_tokenstream( } fn vec_to_tokenstream( - e: &Vec<Expr<X, X>>, + e: &Vec<Box<Expr<X, X>>>, ctx: &Context<Label, ()>, ) -> TokenStream { - let e = e.iter().map(|x| dhall_to_tokenstream(x, ctx)); + let e = e.iter().map(|x| dhall_to_tokenstream_bx(&**x, ctx)); quote! { vec![ #(#e),* ] } } |