diff options
author | Nadrieril | 2020-09-14 23:09:22 +0100 |
---|---|---|
committer | Nadrieril | 2020-09-14 23:19:49 +0100 |
commit | 7e523ae1903ae2d8bca8b3a8352167d7bac5b2b5 (patch) | |
tree | 37e53f88a0d3aa0621e021f307bfcfe4b0c8e29b /dhall/src/syntax | |
parent | ffb7cff2ff317c81248a680e4899fa45eed00daa (diff) |
spec: Enable `with` optimizations
Diffstat (limited to 'dhall/src/syntax')
-rw-r--r-- | dhall/src/syntax/ast/span.rs | 1 | ||||
-rw-r--r-- | dhall/src/syntax/binary/decode.rs | 21 | ||||
-rw-r--r-- | dhall/src/syntax/binary/encode.rs | 4 | ||||
-rw-r--r-- | dhall/src/syntax/text/parser.rs | 26 | ||||
-rw-r--r-- | dhall/src/syntax/text/printer.rs | 4 |
5 files changed, 34 insertions, 22 deletions
diff --git a/dhall/src/syntax/ast/span.rs b/dhall/src/syntax/ast/span.rs index ab3279b..b0c7b5a 100644 --- a/dhall/src/syntax/ast/span.rs +++ b/dhall/src/syntax/ast/span.rs @@ -21,7 +21,6 @@ pub enum Span { /// Desugarings DuplicateRecordFieldsSugar, DottedFieldSugar, - WithSugar, RecordPunSugar, /// For expressions obtained from decoding binary Decoded, diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index 195bd0a..d360949 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -434,6 +434,27 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<DecodedExpr, DecodeError> { let x = cbor_value_to_dhall(&x)?; EmptyListLit(x) } + [U64(29), x, labels, y] => { + let x = cbor_value_to_dhall(&x)?; + let y = cbor_value_to_dhall(&y)?; + let labels = match labels { + Array(labels) => labels + .iter() + .map(|s| match s { + String(s) => Ok(Label::from(s.as_str())), + _ => Err(DecodeError::WrongFormatError( + "with".to_owned(), + )), + }) + .collect::<Result<_, _>>()?, + _ => { + return Err(DecodeError::WrongFormatError( + "with".to_owned(), + )) + } + }; + Op(With(x, labels, y)) + } _ => { return Err(DecodeError::WrongFormatError(format!( "{:?}", diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index b6bbabe..33c7fa5 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -171,6 +171,10 @@ where Op(Completion(x, y)) => { ser_seq!(ser; tag(3), tag(13), expr(x), expr(y)) } + Op(With(x, ls, y)) => { + let ls: Vec<_> = ls.iter().map(label).collect(); + ser_seq!(ser; tag(29), expr(x), ls, expr(y)) + } Import(import) => serialize_import(ser, import), } } diff --git a/dhall/src/syntax/text/parser.rs b/dhall/src/syntax/text/parser.rs index 06c1ac3..377f5e4 100644 --- a/dhall/src/syntax/text/parser.rs +++ b/dhall/src/syntax/text/parser.rs @@ -105,26 +105,6 @@ fn insert_recordlit_entry(map: &mut BTreeMap<Label, Expr>, l: Label, e: Expr) { } } -fn desugar_with_expr(x: Expr, labels: &[Label], y: Expr) -> Expr { - use crate::operations::BinOp::RightBiasedRecordMerge; - let expr = |k| Expr::new(k, Span::WithSugar); - match labels { - [] => y, - [l, rest @ ..] => { - let res = desugar_with_expr( - expr(Op(Field(x.clone(), l.clone()))), - rest, - y, - ); - expr(Op(BinOp( - RightBiasedRecordMerge, - x, - expr(RecordLit(once((l.clone(), res)).collect())), - ))) - } - } -} - lazy_static::lazy_static! { static ref PRECCLIMBER: PrecClimber<Rule> = { use Rule::*; @@ -778,7 +758,11 @@ impl DhallParser { clauses.fold( first, |acc, (labels, e)| { - desugar_with_expr(acc, &labels, e) + spanned_union( + acc.span(), + e.span(), + Op(With(acc, labels, e)) + ) } ) }, diff --git a/dhall/src/syntax/text/printer.rs b/dhall/src/syntax/text/printer.rs index 8815d69..0c2eb2e 100644 --- a/dhall/src/syntax/text/printer.rs +++ b/dhall/src/syntax/text/printer.rs @@ -275,6 +275,10 @@ impl<SE: Display + Clone> Display for OpKind<SE> { Completion(a, b) => { write!(f, "{}::{}", a, b)?; } + With(a, ls, b) => { + let ls = ls.iter().join("."); + write!(f, "{} with {} = {}", a, ls, b)?; + } } Ok(()) } |