From e8d1221a5f536d1d05d2bbaf176ec8e1cdc55295 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 20 Apr 2019 11:21:28 +0200 Subject: Text interpolation --- dhall/src/normalize.rs | 46 +++++++++++++++++++++++++++++++--------------- dhall_core/src/text.rs | 8 ++++---- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index d006cc5..5eada3f 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -282,17 +282,35 @@ impl WHNF { }) .collect(), )), - WHNF::TextLit(elts) => rc(ExprF::TextLit( - elts.into_iter() - .map(|contents| { - use InterpolatedTextContents::{Expr, Text}; - match contents { - Expr(n) => Expr(n.normalize().normalize_to_expr()), - Text(s) => Text(s), - } - }) - .collect(), - )), + WHNF::TextLit(elts) => { + fn normalize_textlit( + elts: Vec>, + ) -> InterpolatedText { + elts.into_iter() + .flat_map(|contents| { + use InterpolatedTextContents::{Expr, Text}; + let new_interpolated = match contents { + Expr(n) => match n.normalize() { + WHNF::TextLit(elts2) => { + normalize_textlit(elts2) + } + e => InterpolatedText::from(( + String::new(), + vec![( + e.normalize_to_expr(), + String::new(), + )], + )), + }, + Text(s) => InterpolatedText::from(s), + }; + new_interpolated.into_iter() + }) + .collect() + } + + rc(ExprF::TextLit(normalize_textlit(elts))) + } WHNF::Expr(e) => e, } } @@ -821,8 +839,6 @@ fn normalize_last_layer( App(Builtin(_), _) => unreachable!(), App(Lam(_, _, _), _) => unreachable!(), BoolIf(BoolLit(_), _, _) => unreachable!(), - // TODO: interpolation - // TextLit(t) => OldOptionalLit(_, _) => unreachable!(), BinOp(BoolAnd, BoolLit(_), BoolLit(_)) => unreachable!(), BinOp(BoolOr, BoolLit(_), BoolLit(_)) => unreachable!(), @@ -1006,7 +1022,7 @@ mod spec_tests { norm!(success_prelude_Optional_unzip_1, "prelude/Optional/unzip/1"); norm!(success_prelude_Text_concat_0, "prelude/Text/concat/0"); norm!(success_prelude_Text_concat_1, "prelude/Text/concat/1"); - // norm!(success_prelude_Text_concatMap_0, "prelude/Text/concatMap/0"); + norm!(success_prelude_Text_concatMap_0, "prelude/Text/concatMap/0"); norm!(success_prelude_Text_concatMap_1, "prelude/Text/concatMap/1"); // norm!(success_prelude_Text_concatMapSep_0, "prelude/Text/concatMapSep/0"); // norm!(success_prelude_Text_concatMapSep_1, "prelude/Text/concatMapSep/1"); @@ -1192,7 +1208,7 @@ mod spec_tests { norm!(success_unit_SomeNormalizeArguments, "unit/SomeNormalizeArguments"); norm!(success_unit_Sort, "unit/Sort"); norm!(success_unit_Text, "unit/Text"); - // norm!(success_unit_TextInterpolate, "unit/TextInterpolate"); + norm!(success_unit_TextInterpolate, "unit/TextInterpolate"); norm!(success_unit_TextLiteral, "unit/TextLiteral"); norm!(success_unit_TextNormalizeInterpolations, "unit/TextNormalizeInterpolations"); norm!(success_unit_TextShow, "unit/TextShow"); diff --git a/dhall_core/src/text.rs b/dhall_core/src/text.rs index e886659..ff5c441 100644 --- a/dhall_core/src/text.rs +++ b/dhall_core/src/text.rs @@ -86,7 +86,7 @@ impl InterpolatedText { } } -impl<'a, SubExpr: 'a> FromIterator> +impl FromIterator> for InterpolatedText { fn from_iter(iter: T) -> Self @@ -94,15 +94,15 @@ impl<'a, SubExpr: 'a> FromIterator> T: IntoIterator>, { let mut res = InterpolatedText { - head: "".to_owned(), - tail: vec![], + head: String::new(), + tail: Vec::new(), }; let mut crnt_str = &mut res.head; for x in iter.into_iter() { match x { InterpolatedTextContents::Text(s) => crnt_str.push_str(&s), InterpolatedTextContents::Expr(e) => { - res.tail.push((e, "".to_owned())); + res.tail.push((e, String::new())); crnt_str = &mut res.tail.last_mut().unwrap().1; } } -- cgit v1.2.3