From 08e1d8ece4314b56d64fa08595c2e043b97896d1 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 6 Apr 2020 17:19:31 +0100 Subject: Split off operations from main expr enum --- dhall/src/syntax/binary/decode.rs | 34 +++++++++++++++++++--------------- dhall/src/syntax/binary/encode.rs | 33 +++++++++++++++++++-------------- 2 files changed, 38 insertions(+), 29 deletions(-) (limited to 'dhall/src/syntax/binary') diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index 3c93419..b183f6c 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -6,7 +6,7 @@ use crate::error::DecodeError; use crate::syntax; use crate::syntax::{ Expr, ExprKind, FilePath, FilePrefix, Hash, ImportMode, ImportTarget, - Integer, InterpolatedText, Label, Natural, NumKind, Scheme, Span, + Integer, InterpolatedText, Label, Natural, NumKind, OpKind, Scheme, Span, UnspannedExpr, URL, V, }; type DecodedExpr = Expr; @@ -27,6 +27,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { use cbor::Value::*; use syntax::{BinOp, Builtin, Const}; use ExprKind::*; + use OpKind::*; Ok(rc(match data { String(s) => match Builtin::parse(s) { Some(b) => ExprKind::Builtin(b), @@ -66,7 +67,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { let mut f = cbor_value_to_dhall(&f)?; for a in args { let a = cbor_value_to_dhall(&a)?; - f = rc(App(f, a)) + f = rc(Op(App(f, a))) } return Ok(f); } @@ -105,7 +106,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { [U64(3), U64(13), x, y] => { let x = cbor_value_to_dhall(&x)?; let y = cbor_value_to_dhall(&y)?; - Completion(x, y) + Op(Completion(x, y)) } [U64(3), U64(n), x, y] => { let x = cbor_value_to_dhall(&x)?; @@ -131,11 +132,14 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { )) } }; - BinOp(op, x, y) + Op(BinOp(op, x, y)) } [U64(4), t] => { let t = cbor_value_to_dhall(&t)?; - EmptyListLit(rc(App(rc(ExprKind::Builtin(Builtin::List)), t))) + EmptyListLit(rc(Op(App( + rc(ExprKind::Builtin(Builtin::List)), + t, + )))) } [U64(4), Null, rest @ ..] => { let rest = rest @@ -151,26 +155,26 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { // Old-style optional literals [U64(5), t] => { let t = cbor_value_to_dhall(&t)?; - App(rc(ExprKind::Builtin(Builtin::OptionalNone)), t) + Op(App(rc(ExprKind::Builtin(Builtin::OptionalNone)), t)) } [U64(5), t, x] => { let x = cbor_value_to_dhall(&x)?; let t = cbor_value_to_dhall(&t)?; Annot( rc(SomeLit(x)), - rc(App(rc(ExprKind::Builtin(Builtin::Optional)), t)), + rc(Op(App(rc(ExprKind::Builtin(Builtin::Optional)), t))), ) } [U64(6), x, y] => { let x = cbor_value_to_dhall(&x)?; let y = cbor_value_to_dhall(&y)?; - Merge(x, y, None) + Op(Merge(x, y, None)) } [U64(6), x, y, 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)) + Op(Merge(x, y, Some(z))) } [U64(7), Object(map)] => { let map = cbor_map_to_dhall_map(map)?; @@ -183,13 +187,13 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { [U64(9), x, String(l)] => { let x = cbor_value_to_dhall(&x)?; let l = Label::from(l.as_str()); - Field(x, l) + Op(Field(x, l)) } [U64(10), x, Array(arr)] => { let x = cbor_value_to_dhall(&x)?; if let [y] = arr.as_slice() { let y = cbor_value_to_dhall(&y)?; - ProjectionByExpr(x, y) + Op(ProjectionByExpr(x, y)) } else { return Err(DecodeError::WrongFormatError( "projection-by-expr".to_owned(), @@ -207,7 +211,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { )), }) .collect::>()?; - Projection(x, labels) + Op(Projection(x, labels)) } [U64(11), Object(map)] => { let map = cbor_map_to_dhall_opt_map(map)?; @@ -222,7 +226,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { 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) + Op(BoolIf(x, y, z)) } [U64(15), U64(x)] => Num(NumKind::Natural(*x as Natural)), [U64(16), U64(x)] => Num(NumKind::Integer(*x as Integer)), @@ -416,12 +420,12 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { } [U64(27), x] => { let x = cbor_value_to_dhall(&x)?; - ToMap(x, None) + Op(ToMap(x, None)) } [U64(27), x, y] => { let x = cbor_value_to_dhall(&x)?; let y = cbor_value_to_dhall(&y)?; - ToMap(x, Some(y)) + Op(ToMap(x, Some(y))) } [U64(28), x] => { let x = cbor_value_to_dhall(&x)?; diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index 8d22a9b..f2bc6f1 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -7,7 +7,7 @@ use crate::syntax; use crate::syntax::map::DupTreeMap; use crate::syntax::{ Expr, ExprKind, FilePrefix, Hash, Import, ImportMode, ImportTarget, Label, - Scheme, V, + OpKind, Scheme, V, }; pub fn encode(expr: &Expr) -> Result, EncodeError> { @@ -49,6 +49,7 @@ where use syntax::Builtin; use syntax::ExprKind::*; use syntax::NumKind::*; + use syntax::OpKind::*; use self::Serialize::{RecordDupMap, RecordMap, UnionMap}; fn expr(x: &Expr) -> self::Serialize<'_> { @@ -70,7 +71,9 @@ where let n: f64 = (*n).into(); ser.serialize_f64(n) } - BoolIf(x, y, z) => ser_seq!(ser; tag(14), expr(x), expr(y), expr(z)), + Op(BoolIf(x, y, z)) => { + ser_seq!(ser; tag(14), expr(x), expr(y), expr(z)) + } Var(V(l, n)) if l == &"_".into() => ser.serialize_u64(*n as u64), Var(V(l, n)) => ser_seq!(ser; label(l), U64(*n as u64)), Lam(l, x, y) if l == &"_".into() => { @@ -99,7 +102,7 @@ where ser_seq.serialize_element(&expr(bound_e))?; ser_seq.end() } - App(_, _) => { + Op(App(_, _)) => { let (f, args) = collect_nested_applications(e); ser.collect_seq( once(tag(0)) @@ -111,7 +114,7 @@ where Assert(x) => ser_seq!(ser; tag(19), expr(x)), SomeLit(x) => ser_seq!(ser; tag(5), null(), expr(x)), EmptyListLit(x) => match x.as_ref() { - App(f, a) => match f.as_ref() { + Op(App(f, a)) => match f.as_ref() { ExprKind::Builtin(Builtin::List) => { ser_seq!(ser; tag(4), expr(a)) } @@ -132,8 +135,8 @@ where RecordType(map) => ser_seq!(ser; tag(7), RecordDupMap(map)), RecordLit(map) => ser_seq!(ser; tag(8), RecordMap(map)), UnionType(map) => ser_seq!(ser; tag(11), UnionMap(map)), - Field(x, l) => ser_seq!(ser; tag(9), expr(x), label(l)), - BinOp(op, x, y) => { + Op(Field(x, l)) => ser_seq!(ser; tag(9), expr(x), label(l)), + Op(BinOp(op, x, y)) => { use syntax::BinOp::*; let op = match op { BoolOr => 0, @@ -152,21 +155,23 @@ where }; ser_seq!(ser; tag(3), U64(op), expr(x), expr(y)) } - Merge(x, y, None) => ser_seq!(ser; tag(6), expr(x), expr(y)), - Merge(x, y, Some(z)) => { + Op(Merge(x, y, None)) => ser_seq!(ser; tag(6), expr(x), expr(y)), + Op(Merge(x, y, Some(z))) => { ser_seq!(ser; tag(6), expr(x), expr(y), expr(z)) } - ToMap(x, None) => ser_seq!(ser; tag(27), expr(x)), - ToMap(x, Some(y)) => ser_seq!(ser; tag(27), expr(x), expr(y)), - Projection(x, ls) => ser.collect_seq( + Op(ToMap(x, None)) => ser_seq!(ser; tag(27), expr(x)), + Op(ToMap(x, Some(y))) => ser_seq!(ser; tag(27), expr(x), expr(y)), + Op(Projection(x, ls)) => ser.collect_seq( once(tag(10)) .chain(once(expr(x))) .chain(ls.iter().map(label)), ), - ProjectionByExpr(x, y) => { + Op(ProjectionByExpr(x, y)) => { ser_seq!(ser; tag(10), expr(x), vec![expr(y)]) } - Completion(x, y) => ser_seq!(ser; tag(3), tag(13), expr(x), expr(y)), + Op(Completion(x, y)) => { + ser_seq!(ser; tag(3), tag(13), expr(x), expr(y)) + } Import(import) => serialize_import(ser, import), } } @@ -286,7 +291,7 @@ impl<'a> serde::ser::Serialize for Serialize<'a> { fn collect_nested_applications<'a>(e: &'a Expr) -> (&'a Expr, Vec<&'a Expr>) { fn go<'a>(e: &'a Expr, vec: &mut Vec<&'a Expr>) -> &'a Expr { match e.as_ref() { - ExprKind::App(f, a) => { + ExprKind::Op(OpKind::App(f, a)) => { vec.push(a); go(f, vec) } -- cgit v1.2.3 From fff4c46e09d4edf25eba737f4d71bfdb1dbf4a82 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 6 Apr 2020 22:11:54 +0100 Subject: Extract operation-related code to a new module --- dhall/src/syntax/binary/decode.rs | 3 ++- dhall/src/syntax/binary/encode.rs | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'dhall/src/syntax/binary') diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index b183f6c..f062089 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -3,10 +3,11 @@ use serde_cbor::value::value as cbor; use std::iter::FromIterator; use crate::error::DecodeError; +use crate::operations::OpKind; use crate::syntax; use crate::syntax::{ Expr, ExprKind, FilePath, FilePrefix, Hash, ImportMode, ImportTarget, - Integer, InterpolatedText, Label, Natural, NumKind, OpKind, Scheme, Span, + Integer, InterpolatedText, Label, Natural, NumKind, Scheme, Span, UnspannedExpr, URL, V, }; type DecodedExpr = Expr; diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index f2bc6f1..cef2af8 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -3,11 +3,12 @@ use std::collections::BTreeMap; use std::vec; use crate::error::EncodeError; +use crate::operations::OpKind; use crate::syntax; use crate::syntax::map::DupTreeMap; use crate::syntax::{ Expr, ExprKind, FilePrefix, Hash, Import, ImportMode, ImportTarget, Label, - OpKind, Scheme, V, + Scheme, V, }; pub fn encode(expr: &Expr) -> Result, EncodeError> { @@ -49,7 +50,7 @@ where use syntax::Builtin; use syntax::ExprKind::*; use syntax::NumKind::*; - use syntax::OpKind::*; + use OpKind::*; use self::Serialize::{RecordDupMap, RecordMap, UnionMap}; fn expr(x: &Expr) -> self::Serialize<'_> { -- cgit v1.2.3 From 56efd2ac39149d8652bd625fbf0679c10823b137 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 6 Apr 2020 22:24:15 +0100 Subject: Move BinOp and Builtin definitions in the relevant module --- dhall/src/syntax/binary/decode.rs | 4 +++- dhall/src/syntax/binary/encode.rs | 8 ++++---- 2 files changed, 7 insertions(+), 5 deletions(-) (limited to 'dhall/src/syntax/binary') diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index f062089..195bd0a 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -25,8 +25,10 @@ fn rc(x: UnspannedExpr) -> Expr { } fn cbor_value_to_dhall(data: &cbor::Value) -> Result { + use crate::builtins::Builtin; + use crate::operations::BinOp; use cbor::Value::*; - use syntax::{BinOp, Builtin, Const}; + use syntax::Const; use ExprKind::*; use OpKind::*; Ok(rc(match data { diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index cef2af8..913dd67 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -2,8 +2,9 @@ use serde_cbor::value::value as cbor; use std::collections::BTreeMap; use std::vec; +use crate::builtins::Builtin; use crate::error::EncodeError; -use crate::operations::OpKind; +use crate::operations::{BinOp, OpKind}; use crate::syntax; use crate::syntax::map::DupTreeMap; use crate::syntax::{ @@ -47,7 +48,6 @@ where { use cbor::Value::{String, I64, U64}; use std::iter::once; - use syntax::Builtin; use syntax::ExprKind::*; use syntax::NumKind::*; use OpKind::*; @@ -116,7 +116,7 @@ where SomeLit(x) => ser_seq!(ser; tag(5), null(), expr(x)), EmptyListLit(x) => match x.as_ref() { Op(App(f, a)) => match f.as_ref() { - ExprKind::Builtin(Builtin::List) => { + ExprKind::Builtin(self::Builtin::List) => { ser_seq!(ser; tag(4), expr(a)) } _ => ser_seq!(ser; tag(28), expr(x)), @@ -138,7 +138,7 @@ where UnionType(map) => ser_seq!(ser; tag(11), UnionMap(map)), Op(Field(x, l)) => ser_seq!(ser; tag(9), expr(x), label(l)), Op(BinOp(op, x, y)) => { - use syntax::BinOp::*; + use self::BinOp::*; let op = match op { BoolOr => 0, BoolAnd => 1, -- cgit v1.2.3 From 092b7208ffd22f8facc7af387ac307e59208d52c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 7 Apr 2020 11:19:36 +0100 Subject: Move duplicate field checking to parser --- dhall/src/syntax/binary/encode.rs | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) (limited to 'dhall/src/syntax/binary') diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index 913dd67..b6bbabe 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -6,7 +6,6 @@ use crate::builtins::Builtin; use crate::error::EncodeError; use crate::operations::{BinOp, OpKind}; use crate::syntax; -use crate::syntax::map::DupTreeMap; use crate::syntax::{ Expr, ExprKind, FilePrefix, Hash, Import, ImportMode, ImportTarget, Label, Scheme, V, @@ -21,8 +20,7 @@ enum Serialize<'a> { Expr(&'a Expr), CBOR(cbor::Value), RecordMap(&'a BTreeMap), - RecordDupMap(&'a DupTreeMap), - UnionMap(&'a DupTreeMap>), + UnionMap(&'a BTreeMap>), } macro_rules! count { @@ -52,7 +50,7 @@ where use syntax::NumKind::*; use OpKind::*; - use self::Serialize::{RecordDupMap, RecordMap, UnionMap}; + use self::Serialize::{RecordMap, UnionMap}; fn expr(x: &Expr) -> self::Serialize<'_> { self::Serialize::Expr(x) } @@ -133,7 +131,7 @@ where Text(x) => cbor(String(x)), }))) } - RecordType(map) => ser_seq!(ser; tag(7), RecordDupMap(map)), + RecordType(map) => ser_seq!(ser; tag(7), RecordMap(map)), RecordLit(map) => ser_seq!(ser; tag(8), RecordMap(map)), UnionType(map) => ser_seq!(ser; tag(11), UnionMap(map)), Op(Field(x, l)) => ser_seq!(ser; tag(9), expr(x), label(l)), @@ -266,11 +264,6 @@ impl<'a> serde::ser::Serialize for Serialize<'a> { match self { Serialize::Expr(e) => serialize_subexpr(ser, e), Serialize::CBOR(v) => v.serialize(ser), - Serialize::RecordDupMap(map) => { - ser.collect_map(map.iter().map(|(k, v)| { - (cbor::Value::String(k.into()), Serialize::Expr(v)) - })) - } Serialize::RecordMap(map) => { ser.collect_map(map.iter().map(|(k, v)| { (cbor::Value::String(k.into()), Serialize::Expr(v)) -- cgit v1.2.3