From a60cf58e5f0cc2f638b788720f9a12cabfdbe3f2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 20 Apr 2019 11:02:27 +0200 Subject: Text literals --- dhall/src/normalize.rs | 53 +++++++++++++++++++++++++++++++++++++++++++++++++- dhall_core/src/text.rs | 16 +++++++++++++-- 2 files changed, 66 insertions(+), 3 deletions(-) diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index b038efd..d006cc5 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -226,6 +226,7 @@ enum WHNF { Now, (NormalizationContext, BTreeMap>), ), + TextLit(Vec>), Expr(OutputSubExpr), } @@ -281,6 +282,17 @@ 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::Expr(e) => e, } } @@ -322,6 +334,17 @@ impl WHNF { .collect(), ), ), + WHNF::TextLit(elts) => WHNF::TextLit( + elts.into_iter() + .map(|contents| { + use InterpolatedTextContents::{Expr, Text}; + match contents { + Expr(n) => Expr(n.shift0(delta, label)), + Text(s) => Text(s), + } + }) + .collect(), + ), } } } @@ -555,6 +578,17 @@ fn reval(ctx: &NormalizationContext, expr: OutputSubExpr) -> WHNF { ExprF::UnionLit(l, x, kts) => { WHNF::UnionLit(l, Now::new(ctx.clone(), x), (ctx.clone(), kts)) } + ExprF::TextLit(elts) => WHNF::TextLit( + elts.into_iter() + .map(|contents| { + use InterpolatedTextContents::{Expr, Text}; + match contents { + Expr(n) => Expr(Now::new(ctx.clone(), n)), + Text(s) => Text(s), + } + }) + .collect(), + ), _ => WHNF::Expr(expr), } } @@ -621,6 +655,19 @@ fn normalize_whnf(ctx: &NormalizationContext, expr: InputSubExpr) -> WHNF { (ctx.clone(), kts.clone()), ) } + ExprF::TextLit(elts) => { + return WHNF::TextLit( + elts.iter() + .map(|contents| { + use InterpolatedTextContents::{Expr, Text}; + match contents { + Expr(n) => Expr(Now::new(ctx.clone(), n.clone())), + Text(s) => Text(s.clone()), + } + }) + .collect(), + ) + } ExprF::BoolIf(b, e1, e2) => { let b = normalize_whnf(ctx, b.clone()); match b { @@ -702,6 +749,10 @@ fn normalize_last_layer( xs.append(&mut ys); return WHNF::NEListLit(xs); } + BinOp(TextAppend, WHNF::TextLit(mut x), WHNF::TextLit(mut y)) => { + x.append(&mut y); + return WHNF::TextLit(x); + } Field(WHNF::UnionType(ctx, kts), l) => { return WHNF::Closure(Closure::UnionConstructor(ctx, l, kts)) @@ -779,7 +830,7 @@ fn normalize_last_layer( BinOp(BoolNE, BoolLit(_), BoolLit(_)) => unreachable!(), BinOp(NaturalPlus, NaturalLit(_), NaturalLit(_)) => unreachable!(), BinOp(NaturalTimes, NaturalLit(_), NaturalLit(_)) => unreachable!(), - BinOp(TextAppend, TextLit(x), TextLit(y)) => Done(TextLit(x + y)), + BinOp(TextAppend, TextLit(_), TextLit(_)) => unreachable!(), BinOp(ListAppend, EmptyListLit(_), _) => unreachable!(), BinOp(ListAppend, _, EmptyListLit(_)) => unreachable!(), BinOp(ListAppend, NEListLit(_), NEListLit(_)) => unreachable!(), diff --git a/dhall_core/src/text.rs b/dhall_core/src/text.rs index 0cfbd7b..e886659 100644 --- a/dhall_core/src/text.rs +++ b/dhall_core/src/text.rs @@ -72,9 +72,21 @@ impl InterpolatedText { }), ) } + + pub fn into_iter( + self, + ) -> impl Iterator> { + use std::iter::once; + once(InterpolatedTextContents::Text(self.head)).chain( + self.tail.into_iter().flat_map(|(e, s)| { + once(InterpolatedTextContents::Expr(e)) + .chain(once(InterpolatedTextContents::Text(s))) + }), + ) + } } -impl<'a, SubExpr: Clone + 'a> FromIterator> +impl<'a, SubExpr: 'a> FromIterator> for InterpolatedText { fn from_iter(iter: T) -> Self @@ -90,7 +102,7 @@ impl<'a, SubExpr: Clone + 'a> FromIterator> match x { InterpolatedTextContents::Text(s) => crnt_str.push_str(&s), InterpolatedTextContents::Expr(e) => { - res.tail.push((e.clone(), "".to_owned())); + res.tail.push((e, "".to_owned())); crnt_str = &mut res.tail.last_mut().unwrap().1; } } -- cgit v1.2.3