diff --git a/ergotree-ir/Cargo.toml b/ergotree-ir/Cargo.toml index d6ec1ac92..b7062ecc4 100644 --- a/ergotree-ir/Cargo.toml +++ b/ergotree-ir/Cargo.toml @@ -6,9 +6,7 @@ authors = ["Denys Zadorozhnyi "] repository.workspace = true edition.workspace = true description = "ErgoTree IR, serialization" -exclude = [ - "proptest-regressions/*" -] +exclude = ["proptest-regressions/*"] [lib] crate-type = ["cdylib", "rlib"] @@ -22,7 +20,7 @@ elliptic-curve = { workspace = true } thiserror = { workspace = true } lazy_static = { workspace = true } derive_more = { workspace = true } -proptest = { workspace = true , optional = true } +proptest = { workspace = true, optional = true } proptest-derive = { workspace = true, optional = true } bs58 = { workspace = true } base16 = { workspace = true } @@ -32,7 +30,7 @@ num-traits = { workspace = true } num-derive = { workspace = true } num-integer = { workspace = true } indexmap = { workspace = true } -serde = { workspace = true , optional = true } +serde = { workspace = true, optional = true } serde_json = { workspace = true, optional = true } serde_with = { workspace = true, optional = true } num256 = "0.3.1" @@ -49,3 +47,4 @@ json = ["serde", "serde_json", "serde_with", "bounded-vec/serde"] sigma-test-util = { workspace = true } rand = { workspace = true } pretty_assertions = { workspace = true } +expect-test = { workspace = true } diff --git a/ergotree-ir/src/lib.rs b/ergotree-ir/src/lib.rs index b2ac46230..872c3fa42 100644 --- a/ergotree-ir/src/lib.rs +++ b/ergotree-ir/src/lib.rs @@ -32,3 +32,4 @@ pub mod source_span; pub mod type_check; pub mod types; pub mod util; +mod pretty_printer; diff --git a/ergotree-ir/src/pretty_printer.rs b/ergotree-ir/src/pretty_printer.rs new file mode 100644 index 000000000..e771c1413 --- /dev/null +++ b/ergotree-ir/src/pretty_printer.rs @@ -0,0 +1,62 @@ +use crate::mir::expr::Expr; + +pub(crate) struct PrintExpr { + expr: Expr, + text: String, +} + +#[derive(PartialEq, Eq, Debug, Clone)] +pub(crate) enum PrintError {} + +#[allow(clippy::todo)] +pub(crate) fn print_expr(expr: Expr) -> Result { + todo!() +} + +#[allow(clippy::unwrap_used)] +#[cfg(test)] +mod tests { + + use expect_test::expect; + + use crate::mir::block::BlockValue; + use crate::mir::val_def::ValDef; + use crate::mir::val_use::ValUse; + use crate::types::stype::SType; + + use super::*; + + fn check(expr: Expr, expected_tree: expect_test::Expect) { + let expected_out = print_expr(expr).unwrap(); + expected_tree.assert_eq(&expected_out.text); + } + + #[test] + fn smoke() { + let val_id = 2.into(); + let body = Expr::BlockValue(BlockValue { + items: vec![ValDef { + id: val_id, + rhs: Box::new(Expr::Const(1i32.into())), + } + .into()], + result: Box::new( + ValUse { + val_id, + tpe: SType::SInt, + } + .into(), + ), + }); + let expr = Expr::Const(1i32.into()); + check( + expr, + expect![[r#" + { + val v1 = 1 + v1 + } + "#]], + ); + } +}