Formatting
This commit is contained in:
parent
ecff82659d
commit
a48c45b737
|
@ -1,12 +1,10 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
ast::{Constant, Program, Term, Type},
|
ast::{Constant, Program, Term, Type},
|
||||||
flat::Binder,
|
flat::Binder,
|
||||||
plutus_data_to_bytes,
|
|
||||||
};
|
};
|
||||||
use pallas_codec::utils::KeyValuePairs;
|
use pallas_primitives::babbage::{Constr, PlutusData};
|
||||||
use pallas_primitives::babbage::{PlutusData, Constr};
|
|
||||||
use pretty::RcDoc;
|
use pretty::RcDoc;
|
||||||
use std::{ascii::escape_default, io::Read};
|
use std::ascii::escape_default;
|
||||||
|
|
||||||
impl<'a, T> Program<T>
|
impl<'a, T> Program<T>
|
||||||
where
|
where
|
||||||
|
@ -255,14 +253,14 @@ impl Constant {
|
||||||
|
|
||||||
Constant::Data(data) => RcDoc::text("(")
|
Constant::Data(data) => RcDoc::text("(")
|
||||||
.append(Self::to_doc_list_plutus_data(&data))
|
.append(Self::to_doc_list_plutus_data(&data))
|
||||||
.append(RcDoc::text(")"))
|
.append(RcDoc::text(")")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// This feels a little awkward here; not sure if it should be upstreamed to pallas
|
// This feels a little awkward here; not sure if it should be upstreamed to pallas
|
||||||
fn to_doc_list_plutus_data(data: &PlutusData) -> RcDoc<()> {
|
fn to_doc_list_plutus_data(data: &PlutusData) -> RcDoc<()> {
|
||||||
match data {
|
match data {
|
||||||
PlutusData::Constr(Constr{ tag, fields, ..}) => RcDoc::text("Constr")
|
PlutusData::Constr(Constr { tag, fields, .. }) => RcDoc::text("Constr")
|
||||||
.append(RcDoc::space())
|
.append(RcDoc::space())
|
||||||
.append(RcDoc::as_string(tag))
|
.append(RcDoc::as_string(tag))
|
||||||
.append(RcDoc::space())
|
.append(RcDoc::space())
|
||||||
|
@ -276,22 +274,21 @@ impl Constant {
|
||||||
.append(RcDoc::space())
|
.append(RcDoc::space())
|
||||||
.append(RcDoc::text("["))
|
.append(RcDoc::text("["))
|
||||||
.append(RcDoc::intersperse(
|
.append(RcDoc::intersperse(
|
||||||
kvp.iter().map(|(key, value)| RcDoc::text("(")
|
kvp.iter().map(|(key, value)| {
|
||||||
.append(Self::to_doc_list_plutus_data(key))
|
RcDoc::text("(")
|
||||||
.append(RcDoc::text(", "))
|
.append(Self::to_doc_list_plutus_data(key))
|
||||||
.append(Self::to_doc_list_plutus_data(value))
|
.append(RcDoc::text(", "))
|
||||||
.append(RcDoc::text(")")),
|
.append(Self::to_doc_list_plutus_data(value))
|
||||||
),
|
.append(RcDoc::text(")"))
|
||||||
|
}),
|
||||||
RcDoc::text(", "),
|
RcDoc::text(", "),
|
||||||
))
|
))
|
||||||
.append(RcDoc::text("]")),
|
.append(RcDoc::text("]")),
|
||||||
PlutusData::BigInt(bi) => RcDoc::text("I")
|
PlutusData::BigInt(bi) => RcDoc::text("I").append(RcDoc::space()).append(match bi {
|
||||||
.append(RcDoc::space())
|
pallas_primitives::babbage::BigInt::Int(v) => RcDoc::text(v.to_string()),
|
||||||
.append(match bi {
|
pallas_primitives::babbage::BigInt::BigUInt(v) => RcDoc::text(v.to_string()),
|
||||||
pallas_primitives::babbage::BigInt::Int(v) => RcDoc::text(v.to_string()),
|
pallas_primitives::babbage::BigInt::BigNInt(v) => RcDoc::text(v.to_string()),
|
||||||
pallas_primitives::babbage::BigInt::BigUInt(v) => RcDoc::text(v.to_string()),
|
}),
|
||||||
pallas_primitives::babbage::BigInt::BigNInt(v) => RcDoc::text(v.to_string()),
|
|
||||||
}),
|
|
||||||
PlutusData::BoundedBytes(bs) => RcDoc::text("B")
|
PlutusData::BoundedBytes(bs) => RcDoc::text("B")
|
||||||
.append(RcDoc::space())
|
.append(RcDoc::space())
|
||||||
.append(RcDoc::text("#"))
|
.append(RcDoc::text("#"))
|
||||||
|
|
|
@ -1,36 +1,59 @@
|
||||||
use num_bigint::{ToBigInt};
|
use num_bigint::ToBigInt;
|
||||||
use uplc::{
|
use uplc::{
|
||||||
ast::{DeBruijn, Constant, Type, Term}, PlutusData, Constr,
|
ast::{Constant, DeBruijn, Term, Type},
|
||||||
|
Constr, PlutusData,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Examples sourced from https://github.com/input-output-hk/plutus/issues/4751#issuecomment-1538377273
|
// Examples sourced from https://github.com/input-output-hk/plutus/issues/4751#issuecomment-1538377273
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant_list_integer() {
|
fn constant_list_integer() {
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::ProtoList(Type::Integer.into(), vec![
|
let term = Term::<DeBruijn>::Constant(
|
||||||
Constant::Integer(0.to_bigint().unwrap()),
|
Constant::ProtoList(
|
||||||
Constant::Integer(1.to_bigint().unwrap()),
|
Type::Integer.into(),
|
||||||
Constant::Integer(2.to_bigint().unwrap()),
|
vec![
|
||||||
]).into());
|
Constant::Integer(0.to_bigint().unwrap()),
|
||||||
|
Constant::Integer(1.to_bigint().unwrap()),
|
||||||
|
Constant::Integer(2.to_bigint().unwrap()),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
assert_eq!(term.to_pretty(), "(con (list integer) [0, 1, 2])");
|
assert_eq!(term.to_pretty(), "(con (list integer) [0, 1, 2])");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant_pair_bool_bytestring() {
|
fn constant_pair_bool_bytestring() {
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::ProtoPair(
|
let term = Term::<DeBruijn>::Constant(
|
||||||
Type::Bool.into(), Type::ByteString.into(),
|
Constant::ProtoPair(
|
||||||
Constant::Bool(true).into(), Constant::ByteString(vec![0x01, 0x23, 0x45]).into(),
|
Type::Bool.into(),
|
||||||
).into());
|
Type::ByteString.into(),
|
||||||
assert_eq!(term.to_pretty(), "(con (pair bool bytestring) (True, #012345))");
|
Constant::Bool(true).into(),
|
||||||
|
Constant::ByteString(vec![0x01, 0x23, 0x45]).into(),
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
term.to_pretty(),
|
||||||
|
"(con (pair bool bytestring) (True, #012345))"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant_pair_unit_string() {
|
fn constant_pair_unit_string() {
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::ProtoPair(
|
let term = Term::<DeBruijn>::Constant(
|
||||||
Type::Unit.into(), Type::String.into(),
|
Constant::ProtoPair(
|
||||||
Constant::Unit.into(), Constant::String("hello universe".into()).into(),
|
Type::Unit.into(),
|
||||||
).into());
|
Type::String.into(),
|
||||||
assert_eq!(term.to_pretty(), "(con (pair unit string) ((), \"hello universe\"))")
|
Constant::Unit.into(),
|
||||||
|
Constant::String("hello universe".into()).into(),
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
term.to_pretty(),
|
||||||
|
"(con (pair unit string) ((), \"hello universe\"))"
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@ -38,40 +61,62 @@ fn constant_deeply_nested_list() {
|
||||||
let t0 = Type::Integer;
|
let t0 = Type::Integer;
|
||||||
let t1 = Type::List(t0.clone().into());
|
let t1 = Type::List(t0.clone().into());
|
||||||
let t2 = Type::List(t1.clone().into());
|
let t2 = Type::List(t1.clone().into());
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::ProtoList(t2, vec![
|
let term = Term::<DeBruijn>::Constant(
|
||||||
Constant::ProtoList(t1.clone(), vec![
|
Constant::ProtoList(
|
||||||
Constant::ProtoList(t0.clone(), vec![
|
t2,
|
||||||
Constant::Integer(-1.to_bigint().unwrap()),
|
vec![
|
||||||
]),
|
Constant::ProtoList(
|
||||||
Constant::ProtoList(t0.clone(), vec![])
|
t1.clone(),
|
||||||
]),
|
vec![
|
||||||
Constant::ProtoList(t1.clone(), vec![
|
Constant::ProtoList(
|
||||||
Constant::ProtoList(t0.clone(), vec![]),
|
t0.clone(),
|
||||||
Constant::ProtoList(t0.clone(), vec![
|
vec![Constant::Integer(-1.to_bigint().unwrap())],
|
||||||
Constant::Integer(2.to_bigint().unwrap()),
|
),
|
||||||
Constant::Integer(3.to_bigint().unwrap())
|
Constant::ProtoList(t0.clone(), vec![]),
|
||||||
]),
|
],
|
||||||
])
|
),
|
||||||
]).into());
|
Constant::ProtoList(
|
||||||
assert_eq!(term.to_pretty(), "(con (list (list (list integer))) [[[-1], []], [[], [2, 3]]])");
|
t1.clone(),
|
||||||
|
vec![
|
||||||
|
Constant::ProtoList(t0.clone(), vec![]),
|
||||||
|
Constant::ProtoList(
|
||||||
|
t0.clone(),
|
||||||
|
vec![
|
||||||
|
Constant::Integer(2.to_bigint().unwrap()),
|
||||||
|
Constant::Integer(3.to_bigint().unwrap()),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
term.to_pretty(),
|
||||||
|
"(con (list (list (list integer))) [[[-1], []], [[], [2, 3]]])"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant_data_constr() {
|
fn constant_data_constr() {
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::Data(
|
let term = Term::<DeBruijn>::Constant(
|
||||||
PlutusData::Constr(Constr::<PlutusData> {
|
Constant::Data(PlutusData::Constr(Constr::<PlutusData> {
|
||||||
tag: 1,
|
tag: 1,
|
||||||
any_constructor: None,
|
any_constructor: None,
|
||||||
fields: vec![PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(2.into()))]
|
fields: vec![PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(
|
||||||
})
|
2.into(),
|
||||||
).into());
|
))],
|
||||||
|
}))
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
assert_eq!(term.to_pretty(), "(con data (Constr 1 [I 2]))");
|
assert_eq!(term.to_pretty(), "(con data (Constr 1 [I 2]))");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant_data_map() {
|
fn constant_data_map() {
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::Data(
|
let term = Term::<DeBruijn>::Constant(
|
||||||
PlutusData::Map(uplc::KeyValuePairs::Def(vec![
|
Constant::Data(PlutusData::Map(uplc::KeyValuePairs::Def(vec![
|
||||||
(
|
(
|
||||||
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(0.into())),
|
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(0.into())),
|
||||||
PlutusData::BoundedBytes(vec![0x00].into()),
|
PlutusData::BoundedBytes(vec![0x00].into()),
|
||||||
|
@ -80,27 +125,35 @@ fn constant_data_map() {
|
||||||
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(1.into())),
|
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(1.into())),
|
||||||
PlutusData::BoundedBytes(vec![0x0f].into()),
|
PlutusData::BoundedBytes(vec![0x0f].into()),
|
||||||
),
|
),
|
||||||
]))
|
])))
|
||||||
).into());
|
.into(),
|
||||||
assert_eq!(term.to_pretty(), "(con data (Map [(I 0, B #00), (I 1, B #0f)]))");
|
);
|
||||||
|
assert_eq!(
|
||||||
|
term.to_pretty(),
|
||||||
|
"(con data (Map [(I 0, B #00), (I 1, B #0f)]))"
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant_data_list() {
|
fn constant_data_list() {
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::Data(
|
let term = Term::<DeBruijn>::Constant(
|
||||||
PlutusData::Array(vec![
|
Constant::Data(PlutusData::Array(vec![
|
||||||
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(0.into())),
|
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(0.into())),
|
||||||
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(1.into())),
|
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(1.into())),
|
||||||
])
|
]))
|
||||||
).into());
|
.into(),
|
||||||
|
);
|
||||||
assert_eq!(term.to_pretty(), "(con data (List [I 0, I 1]))");
|
assert_eq!(term.to_pretty(), "(con data (List [I 0, I 1]))");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant_data_int() {
|
fn constant_data_int() {
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::Data(
|
let term = Term::<DeBruijn>::Constant(
|
||||||
PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(2.into())),
|
Constant::Data(PlutusData::BigInt(pallas_primitives::alonzo::BigInt::Int(
|
||||||
).into());
|
2.into(),
|
||||||
|
)))
|
||||||
|
.into(),
|
||||||
|
);
|
||||||
assert_eq!(term.to_pretty(), "(con data (I 2))");
|
assert_eq!(term.to_pretty(), "(con data (I 2))");
|
||||||
|
|
||||||
// TODO: large integers currently encode as bytestrings, which isn't great
|
// TODO: large integers currently encode as bytestrings, which isn't great
|
||||||
|
@ -118,8 +171,8 @@ fn constant_data_int() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn constant_data_bytes() {
|
fn constant_data_bytes() {
|
||||||
let term = Term::<DeBruijn>::Constant(Constant::Data(
|
let term = Term::<DeBruijn>::Constant(
|
||||||
PlutusData::BoundedBytes(vec![0x00, 0x1A].into()),
|
Constant::Data(PlutusData::BoundedBytes(vec![0x00, 0x1A].into())).into(),
|
||||||
).into());
|
);
|
||||||
assert_eq!(term.to_pretty(), "(con data (B #001a))");
|
assert_eq!(term.to_pretty(), "(con data (B #001a))");
|
||||||
}
|
}
|
Loading…
Reference in New Issue