39 lines
1.1 KiB
Rust
39 lines
1.1 KiB
Rust
use core::fmt;
|
|
|
|
#[derive(Debug, Clone)]
|
|
pub enum Inst<'a> {
|
|
Set(&'a str),
|
|
Var(&'a str),
|
|
Num(f64),
|
|
Call,
|
|
Block(Vec<Inst<'a>>),
|
|
}
|
|
|
|
impl fmt::Display for Inst<'_> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
match self {
|
|
Inst::Set(v) => write!(f, "→{v}"),
|
|
Inst::Var(v) => write!(f, "{v}"),
|
|
Inst::Num(n) => write!(f, "{n}"),
|
|
Inst::Call => write!(f, "!"),
|
|
Inst::Block(insts) => {
|
|
write!(f, "(")?;
|
|
if insts.len() == 1 {
|
|
write!(f, "{}", insts[0])?;
|
|
} else if !insts.is_empty() {
|
|
for window in insts.windows(2) {
|
|
match window {
|
|
[a, Inst::Call] => write!(f, "{a}")?,
|
|
[a, _] => write!(f, "{a} ")?,
|
|
[last] => write!(f, "{last}")?,
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
write!(f, "{}", insts.last().unwrap())?;
|
|
}
|
|
write!(f, ")")
|
|
}
|
|
}
|
|
}
|
|
}
|