From 6b3497c5fc88f70520dc69427008af7b83036a63 Mon Sep 17 00:00:00 2001 From: mehbark Date: Thu, 3 Jul 2025 17:13:41 -0400 Subject: [PATCH] inst::test --- examples/play.puyo | 12 ++++++++++++ src/inst.rs | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 examples/play.puyo diff --git a/examples/play.puyo b/examples/play.puyo new file mode 100644 index 0000000..1e0af85 --- /dev/null +++ b/examples/play.puyo @@ -0,0 +1,12 @@ +1 ->i +( + i println + i 2 * ->i + + i 1e100 < + loop + () + ite +)->loop + +loop! diff --git a/src/inst.rs b/src/inst.rs index 803823f..11bc791 100644 --- a/src/inst.rs +++ b/src/inst.rs @@ -23,6 +23,7 @@ impl fmt::Display for Inst<'_> { } else if !insts.is_empty() { for window in insts.windows(2) { match window { + [a @ Inst::Set(_), Inst::Call] => write!(f, "{a} ")?, [a, Inst::Call] => write!(f, "{a}")?, [a, _] => write!(f, "{a} ")?, [last] => write!(f, "{last}")?, @@ -36,3 +37,46 @@ impl fmt::Display for Inst<'_> { } } } + +#[cfg(test)] +mod test { + use super::Inst::*; + + #[test] + fn display_trivial() { + assert_eq!(Set("hello").to_string(), "→hello"); + assert_eq!(Var("hello").to_string(), "hello"); + for n in [-1., 1., 1e100, 10_949_102_470_912., -2_189_024.] { + assert_eq!(Num(n).to_string(), n.to_string()); + } + assert_eq!(Call.to_string(), "!"); + } + + #[test] + fn display_block() { + assert_eq!(Block(vec![]).to_string(), "()"); + + assert_eq!(Block(vec![Var("x")]).to_string(), "(x)"); + assert_eq!(Block(vec![Set("x")]).to_string(), "(→x)"); + + assert_eq!(Block(vec![Var("x"), Block(vec![])]).to_string(), "(x ())"); + + assert_eq!(Block(vec![Var("x"), Call]).to_string(), "(x!)"); + assert_eq!(Block(vec![Block(vec![]), Call]).to_string(), "(()!)"); + assert_eq!(Block(vec![Set("x"), Call]).to_string(), "(→x !)"); + + assert_eq!( + Block(vec![Set("x"), Var("a"), Var("b")]).to_string(), + "(→x a b)" + ); + + assert_eq!(Block(vec![Call, Call, Call]).to_string(), "(!!!)"); + assert_eq!(Block(vec![Var("x"), Call, Call]).to_string(), "(x!!)"); + assert_eq!(Block(vec![Set("x"), Call, Call]).to_string(), "(→x !!)"); + + assert_eq!( + Block(vec![Num(3.), Set("x"), Call, Var("x"), Call, Call]).to_string(), + "(3 →x ! x!!)" + ); + } +}