diff options
author | Nadrieril | 2020-04-06 17:19:31 +0100 |
---|---|---|
committer | Nadrieril | 2020-04-06 17:19:31 +0100 |
commit | 08e1d8ece4314b56d64fa08595c2e043b97896d1 (patch) | |
tree | 22d9fcf951d7e6e86d33e8e1e89465b0e61381c3 /dhall/src/syntax/binary | |
parent | d35cb130d80d628807a4247ddf84a8d0230c87ab (diff) |
Split off operations from main expr enum
Diffstat (limited to 'dhall/src/syntax/binary')
-rw-r--r-- | dhall/src/syntax/binary/decode.rs | 34 | ||||
-rw-r--r-- | dhall/src/syntax/binary/encode.rs | 33 |
2 files changed, 38 insertions, 29 deletions
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<DecodedExpr, DecodeError> { 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<DecodedExpr, DecodeError> { 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<DecodedExpr, DecodeError> { [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<DecodedExpr, DecodeError> { )) } }; - 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<DecodedExpr, DecodeError> { // 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<DecodedExpr, DecodeError> { [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<DecodedExpr, DecodeError> { )), }) .collect::<Result<_, _>>()?; - 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<DecodedExpr, DecodeError> { 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<DecodedExpr, DecodeError> { } [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<Vec<u8>, 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) } |