From 0f33caf4c1ee4d1f95d6ac3a41b5cf2f8efa7b54 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 16 Mar 2019 22:41:22 +0100 Subject: Use Box more uniformly in AST Closes #28 --- dhall_core/src/core.rs | 78 ++++++++++++++++++++++++++---------------------- dhall_core/src/parser.rs | 38 ++++++++++------------- 2 files changed, 58 insertions(+), 58 deletions(-) (limited to 'dhall_core') 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 { head: String, - tail: Vec<(Expr, String)>, + tail: Vec<(Box>, String)>, } -impl From<(String, Vec<(Expr, String)>)> +impl From<(String, Vec<(Box>, String)>)> for InterpolatedText { - fn from(x: (String, Vec<(Expr, String)>)) -> Self { + fn from(x: (String, Vec<(Box>, String)>)) -> Self { InterpolatedText { head: x.0, tail: x.1, @@ -203,7 +203,7 @@ impl From for InterpolatedText { // This one is needed when parsing, because we need to own the Expr pub enum OwnedInterpolatedTextContents<'a, Note, Embed> { Text(&'a str), - Expr(Expr), + Expr(Box>), } // 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 InterpolatedText { pub fn map(&self, mut f: F) -> InterpolatedText where - F: FnMut(&Expr) -> Expr, + F: FnMut(&Box>) -> Box>, { InterpolatedText { head: self.head.clone(), @@ -304,7 +304,7 @@ pub enum Expr { /// `Pi x A B ~ ∀(x : A) -> B` Pi(Label, Box>, Box>), /// `App f A ~ f A` - App(Box>, Vec>), + App(Box>, Vec>>), /// `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 { /// `TextLit t ~ t` TextLit(InterpolatedText), /// `ListLit t [x, y, z] ~ [x, y, z] : List t` - ListLit(Option>>, Vec>), + ListLit(Option>>, Vec>>), /// `OptionalLit t [e] ~ [e] : Optional t` /// `OptionalLit t [] ~ [] : Optional t` OptionalLit( @@ -344,16 +344,16 @@ pub enum Expr { Option>>, ), /// `Record [(k1, t1), (k2, t2)] ~ { k1 : t1, k2 : t1 }` - Record(BTreeMap>), + Record(BTreeMap>>), /// `RecordLit [(k1, v1), (k2, v2)] ~ { k1 = v1, k2 = v2 }` - RecordLit(BTreeMap>), + RecordLit(BTreeMap>>), /// `Union [(k1, t1), (k2, t2)] ~ < k1 : t1, k2 : t2 >` - Union(BTreeMap>), + Union(BTreeMap>>), /// `UnionLit (k1, v1) [(k2, t2), (k3, t3)] ~ < k1 = t1, k2 : t2, k3 : t3 >` UnionLit( Label, Box>, - BTreeMap>, + BTreeMap>>, ), /// `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(f: Ef, x: Vec) -> Expr +pub fn app(f: Ef, x: Vec>) -> Expr where Ef: Into>, Ex: Into>, { - 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| -> Box> { bx(map(x)) }; + let bxbxmap = |x: &Box>| -> Box> { 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(d: isize, v: &V, e: &Expr) -> Expr { } 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(d: isize, v: &V, e: &Expr) -> Expr { 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; // TODO: parse escapes rule!(double_quote_chunk>; children!(c: interpolation) => { - OwnedInterpolatedTextContents::Expr(*c) + OwnedInterpolatedTextContents::Expr(c) }, captured_str!(s) => { OwnedInterpolatedTextContents::Text(s) @@ -461,7 +461,7 @@ rule!(interpolation; rule!(single_quote_continue>>; 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; rule!(application_expression; 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; rule!(non_empty_record_type_or_literal; 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)>; +rule!(non_empty_record_type<(BoxExpr, BTreeMap)>; self!(x: partial_record_entries) => x ); -named!(partial_record_entries<(BoxExpr, BTreeMap)>; +named!(partial_record_entries<(BoxExpr, BTreeMap)>; children!(expr: expression, entries*: record_entry) => { - let mut map: BTreeMap = 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)>; +rule!(non_empty_record_literal<(BoxExpr, BTreeMap)>; self!(x: partial_record_entries) => x ); @@ -846,29 +842,25 @@ rule!(union_type_or_literal; rule!(empty_union_type<()>; children!() => ()); rule!(non_empty_union_type_or_literal - <(Option<(Label, BoxExpr)>, BTreeMap)>; + <(Option<(Label, BoxExpr)>, BTreeMap)>; 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>; +rule!(union_type_entries>; children!(entries*: union_type_entry) => { - let mut map: BTreeMap = 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; children!(items*: expression) => { - bx(Expr::ListLit(None, items.map(|x| *x).collect())) + bx(Expr::ListLit(None, items.collect())) } ); -- cgit v1.2.3