inst::test

This commit is contained in:
mehbark 2025-07-03 17:13:41 -04:00
parent 6183009481
commit 6b3497c5fc
2 changed files with 56 additions and 0 deletions

12
examples/play.puyo Normal file
View file

@ -0,0 +1,12 @@
1 ->i
(
i println
i 2 * ->i
i 1e100 <
loop
()
ite
)->loop
loop!

View file

@ -23,6 +23,7 @@ impl fmt::Display for Inst<'_> {
} else if !insts.is_empty() { } else if !insts.is_empty() {
for window in insts.windows(2) { for window in insts.windows(2) {
match window { match window {
[a @ Inst::Set(_), Inst::Call] => write!(f, "{a} ")?,
[a, Inst::Call] => write!(f, "{a}")?, [a, Inst::Call] => write!(f, "{a}")?,
[a, _] => write!(f, "{a} ")?, [a, _] => write!(f, "{a} ")?,
[last] => write!(f, "{last}")?, [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!!)"
);
}
}