Skip to content

Commit

Permalink
draft pretty printer types and smoke test;
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Jul 24, 2023
1 parent af665a1 commit 6c7e963
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
9 changes: 4 additions & 5 deletions ergotree-ir/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ authors = ["Denys Zadorozhnyi <[email protected]>"]
repository.workspace = true
edition.workspace = true
description = "ErgoTree IR, serialization"
exclude = [
"proptest-regressions/*"
]
exclude = ["proptest-regressions/*"]

[lib]
crate-type = ["cdylib", "rlib"]
Expand All @@ -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 }
Expand All @@ -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"
Expand All @@ -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 }
1 change: 1 addition & 0 deletions ergotree-ir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ pub mod source_span;
pub mod type_check;
pub mod types;
pub mod util;
mod pretty_printer;
62 changes: 62 additions & 0 deletions ergotree-ir/src/pretty_printer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::mir::expr::Expr;

pub(crate) struct PrintExpr {

Check failure on line 3 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Build without default features

struct `PrintExpr` is never constructed

Check failure on line 3 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

struct `PrintExpr` is never constructed

Check failure on line 3 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / clippy

struct `PrintExpr` is never constructed

error: struct `PrintExpr` is never constructed --> ergotree-ir/src/pretty_printer.rs:3:19 | 3 | pub(crate) struct PrintExpr { | ^^^^^^^^^ | note: the lint level is defined here --> ergotree-ir/src/lib.rs:9:9 | 9 | #![deny(dead_code)] | ^^^^^^^^^

Check failure on line 3 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

struct `PrintExpr` is never constructed
expr: Expr,
text: String,
}

#[derive(PartialEq, Eq, Debug, Clone)]
pub(crate) enum PrintError {}

#[allow(clippy::todo)]
pub(crate) fn print_expr(expr: Expr) -> Result<PrintExpr, PrintError> {

Check warning on line 12 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Build without default features

unused variable: `expr`

Check failure on line 12 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Build without default features

function `print_expr` is never used

Check warning on line 12 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

unused variable: `expr`

Check failure on line 12 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Tests on ubuntu-latest

function `print_expr` is never used

Check failure on line 12 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / clippy

function `print_expr` is never used

error: function `print_expr` is never used --> ergotree-ir/src/pretty_printer.rs:12:15 | 12 | pub(crate) fn print_expr(expr: Expr) -> Result<PrintExpr, PrintError> { | ^^^^^^^^^^

Check failure on line 12 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / clippy

unused variable: `expr`

error: unused variable: `expr` --> ergotree-ir/src/pretty_printer.rs:12:26 | 12 | pub(crate) fn print_expr(expr: Expr) -> Result<PrintExpr, PrintError> { | ^^^^ help: if this is intentional, prefix it with an underscore: `_expr` | = note: `-D unused-variables` implied by `-D warnings`

Check warning on line 12 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

unused variable: `expr`

Check failure on line 12 in ergotree-ir/src/pretty_printer.rs

View workflow job for this annotation

GitHub Actions / Check intra-documentation links

function `print_expr` is never used
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
}
"#]],
);
}
}

0 comments on commit 6c7e963

Please sign in to comment.