diff options
author | Nadrieril Feneanar | 2020-02-19 17:25:57 +0000 |
---|---|---|
committer | GitHub | 2020-02-19 17:25:57 +0000 |
commit | ffbde252a850c7d96e1000e1be52792c41733a28 (patch) | |
tree | e668b7f764fb4981a802bc619e0b2ff62fa9ce16 /dhall/src/syntax/binary | |
parent | e4b3a879907b6dcc75d25847ae21a23d0201aae1 (diff) | |
parent | 7cbfc1a0d32766a383d1f48902502adaa2234d2f (diff) |
Merge pull request #131 from Nadrieril/hir
Decouple main expression types
Diffstat (limited to '')
-rw-r--r-- | dhall/src/syntax/binary/decode.rs | 20 | ||||
-rw-r--r-- | dhall/src/syntax/binary/encode.rs | 53 |
2 files changed, 30 insertions, 43 deletions
diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index 52b699c..2e50d61 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -6,8 +6,8 @@ use crate::error::DecodeError; use crate::syntax; use crate::syntax::{ Expr, ExprKind, FilePath, FilePrefix, Hash, ImportLocation, ImportMode, - Integer, InterpolatedText, Label, Natural, Scheme, Span, UnspannedExpr, - URL, V, + Integer, InterpolatedText, Label, LitKind, Natural, Scheme, Span, + UnspannedExpr, URL, V, }; use crate::DecodedExpr; @@ -19,7 +19,7 @@ pub(crate) fn decode(data: &[u8]) -> Result<DecodedExpr, DecodeError> { } // Should probably rename this -fn rc<E>(x: UnspannedExpr<E>) -> Expr<E> { +fn rc(x: UnspannedExpr) -> Expr { Expr::new(x, Span::Decoded) } @@ -31,8 +31,8 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<DecodedExpr, DecodeError> { String(s) => match Builtin::parse(s) { Some(b) => ExprKind::Builtin(b), None => match s.as_str() { - "True" => BoolLit(true), - "False" => BoolLit(false), + "True" => Lit(LitKind::Bool(true)), + "False" => Lit(LitKind::Bool(false)), "Type" => Const(Const::Type), "Kind" => Const(Const::Kind), "Sort" => Const(Const::Sort), @@ -40,8 +40,8 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<DecodedExpr, DecodeError> { }, }, U64(n) => Var(V(Label::from("_"), *n as usize)), - F64(x) => DoubleLit((*x).into()), - Bool(b) => BoolLit(*b), + F64(x) => Lit(LitKind::Double((*x).into())), + Bool(b) => Lit(LitKind::Bool(*b)), Array(vec) => match vec.as_slice() { [String(l), U64(n)] => { if l.as_str() == "_" { @@ -216,9 +216,9 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result<DecodedExpr, DecodeError> { let z = cbor_value_to_dhall(&z)?; BoolIf(x, y, z) } - [U64(15), U64(x)] => NaturalLit(*x as Natural), - [U64(16), U64(x)] => IntegerLit(*x as Integer), - [U64(16), I64(x)] => IntegerLit(*x as Integer), + [U64(15), U64(x)] => Lit(LitKind::Natural(*x as Natural)), + [U64(16), U64(x)] => Lit(LitKind::Integer(*x as Integer)), + [U64(16), I64(x)] => Lit(LitKind::Integer(*x as Integer)), [U64(18), String(first), rest @ ..] => { TextLit(InterpolatedText::from(( first.clone(), diff --git a/dhall/src/syntax/binary/encode.rs b/dhall/src/syntax/binary/encode.rs index 5e79f2d..291ac4a 100644 --- a/dhall/src/syntax/binary/encode.rs +++ b/dhall/src/syntax/binary/encode.rs @@ -9,17 +9,16 @@ use crate::syntax::{ Label, Scheme, V, }; -/// Warning: will fail if `expr` contains an `Embed` node. -pub(crate) fn encode<E>(expr: &Expr<E>) -> Result<Vec<u8>, EncodeError> { +pub(crate) fn encode(expr: &Expr) -> Result<Vec<u8>, EncodeError> { serde_cbor::ser::to_vec(&Serialize::Expr(expr)) .map_err(|e| EncodeError::CBORError(e)) } -enum Serialize<'a, E> { - Expr(&'a Expr<E>), +enum Serialize<'a> { + Expr(&'a Expr), CBOR(cbor::Value), - RecordMap(&'a DupTreeMap<Label, Expr<E>>), - UnionMap(&'a DupTreeMap<Label, Option<Expr<E>>>), + RecordMap(&'a DupTreeMap<Label, Expr>), + UnionMap(&'a DupTreeMap<Label, Option<Expr>>), } macro_rules! count { @@ -39,7 +38,7 @@ macro_rules! ser_seq { }}; } -fn serialize_subexpr<S, E>(ser: S, e: &Expr<E>) -> Result<S::Ok, S::Error> +fn serialize_subexpr<S>(ser: S, e: &Expr) -> Result<S::Ok, S::Error> where S: serde::ser::Serializer, { @@ -47,13 +46,14 @@ where use std::iter::once; use syntax::Builtin; use syntax::ExprKind::*; + use syntax::LitKind::*; use self::Serialize::{RecordMap, UnionMap}; - fn expr<E>(x: &Expr<E>) -> self::Serialize<'_, E> { + fn expr(x: &Expr) -> self::Serialize<'_> { self::Serialize::Expr(x) } let cbor = - |v: cbor::Value| -> self::Serialize<'_, E> { self::Serialize::CBOR(v) }; + |v: cbor::Value| -> self::Serialize<'_> { self::Serialize::CBOR(v) }; let tag = |x: u64| cbor(U64(x)); let null = || cbor(cbor::Value::Null); let label = |l: &Label| cbor(cbor::Value::String(l.into())); @@ -61,10 +61,10 @@ where match e.as_ref() { Const(c) => ser.serialize_str(&c.to_string()), Builtin(b) => ser.serialize_str(&b.to_string()), - BoolLit(b) => ser.serialize_bool(*b), - NaturalLit(n) => ser_seq!(ser; tag(15), U64(*n as u64)), - IntegerLit(n) => ser_seq!(ser; tag(16), I64(*n as i64)), - DoubleLit(n) => { + Lit(Bool(b)) => ser.serialize_bool(*b), + Lit(Natural(n)) => ser_seq!(ser; tag(15), U64(*n as u64)), + Lit(Integer(n)) => ser_seq!(ser; tag(16), I64(*n as i64)), + Lit(Double(n)) => { let n: f64 = (*n).into(); ser.serialize_f64(n) } @@ -166,16 +166,10 @@ where } Completion(x, y) => ser_seq!(ser; tag(3), tag(13), expr(x), expr(y)), Import(import) => serialize_import(ser, import), - Embed(_) => unimplemented!( - "An expression with resolved imports cannot be binary-encoded" - ), } } -fn serialize_import<S, E>( - ser: S, - import: &Import<Expr<E>>, -) -> Result<S::Ok, S::Error> +fn serialize_import<S>(ser: S, import: &Import<Expr>) -> Result<S::Ok, S::Error> where S: serde::ser::Serializer, { @@ -256,7 +250,7 @@ where ser_seq.end() } -impl<'a, E> serde::ser::Serialize for Serialize<'a, E> { +impl<'a> serde::ser::Serialize for Serialize<'a> { fn serialize<S>(&self, ser: S) -> Result<S::Ok, S::Error> where S: serde::ser::Serializer, @@ -282,10 +276,8 @@ impl<'a, E> serde::ser::Serialize for Serialize<'a, E> { } } -fn collect_nested_applications<'a, E>( - e: &'a Expr<E>, -) -> (&'a Expr<E>, Vec<&'a Expr<E>>) { - fn go<'a, E>(e: &'a Expr<E>, vec: &mut Vec<&'a Expr<E>>) -> &'a Expr<E> { +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) => { vec.push(a); @@ -299,15 +291,10 @@ fn collect_nested_applications<'a, E>( (e, vec) } -type LetBinding<'a, E> = (&'a Label, &'a Option<Expr<E>>, &'a Expr<E>); +type LetBinding<'a> = (&'a Label, &'a Option<Expr>, &'a Expr); -fn collect_nested_lets<'a, E>( - e: &'a Expr<E>, -) -> (&'a Expr<E>, Vec<LetBinding<'a, E>>) { - fn go<'a, E>( - e: &'a Expr<E>, - vec: &mut Vec<LetBinding<'a, E>>, - ) -> &'a Expr<E> { +fn collect_nested_lets<'a>(e: &'a Expr) -> (&'a Expr, Vec<LetBinding<'a>>) { + fn go<'a>(e: &'a Expr, vec: &mut Vec<LetBinding<'a>>) -> &'a Expr { match e.as_ref() { ExprKind::Let(l, t, v, e) => { vec.push((l, t, v)); |