diff --git a/examples/count-forever.puyo b/examples/count-forever.puyo new file mode 100644 index 0000000..96764a1 --- /dev/null +++ b/examples/count-forever.puyo @@ -0,0 +1,9 @@ +0 ->i + +( + i println + i 1 + ->i + loop! +)->loop + +loop! diff --git a/examples/foo.puyo b/examples/foo.puyo new file mode 100644 index 0000000..79e9037 --- /dev/null +++ b/examples/foo.puyo @@ -0,0 +1,28 @@ +(70 put 105 put 122 put 122 put)→fizz +(66 put 117 put 122 put 122 put)→buzz +(10 put)→newline +1→i + +( + i 15 % 0 = + (fizz! buzz!) + ( + i 3 % 0 = + fizz + ( + i 5 % 0 = + buzz + (i print) + ite + ) + ite + ) + ite + newline! + i 1 + →i + i 101 < + go + () + ite +)→go +go! diff --git a/examples/mandelbrot.puyo b/examples/mandelbrot.puyo new file mode 100644 index 0000000..e291c9a --- /dev/null +++ b/examples/mandelbrot.puyo @@ -0,0 +1,55 @@ +( + ( + ->body + ->test + ( + test! + (body! loop!) + () + ite + )->loop + loop! + )! +)->while + +-1.5 ->xmin +0.47 ->xmax + +-1.12 ->ymin +1.12 ->ymax + +27 ->itermax + +xmin ->x0 +ymin ->y0 + + (y0 ymax <) + ( + (x0 xmax <) + ( + 0 ->x + 0 ->y + 0 ->iter + + (x x * y y * + 4 < iter itermax < *) + ( + x x * y y * - x0 + ->xtemp + 2 x y * * y0 + ->y + xtemp ->x + iter 1 + ->iter + ) + while! + + iter itermax = + (32 put) + (64 iter + put) + ite + + x0 0.03 + ->x0 + ) + while! + 10 put + y0 0.08 + ->y0 + xmin ->x0 + ) +while! diff --git a/src/eval.rs b/src/eval.rs index 51e2680..ab776d1 100644 --- a/src/eval.rs +++ b/src/eval.rs @@ -30,8 +30,8 @@ impl fmt::Display for Val<'_> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { Val::Num(n) => write!(f, "{n}"), - Val::Fun { insts, env: _ } => { - write!(f, "{}", Inst::Block(insts.to_vec())) + Val::Fun { insts, env } => { + write!(f, "{} {}", Inst::Block(insts.to_vec()), env.borrow()) } } } @@ -66,6 +66,26 @@ enum Env<'a> { }, } +impl fmt::Display for Env<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Env::Local(map) => { + for key in map.keys() { + write!(f, "{key} ")?; + } + Ok(()) + } + Env::Closure { local, above } => { + for key in local.keys() { + write!(f, "{key} ")?; + } + write!(f, "| ")?; + write!(f, "{}", above.borrow()) + } + } + } +} + impl Default for Env<'_> { fn default() -> Self { Env::Local(HashMap::new()) @@ -211,6 +231,8 @@ fn eval<'a>(insts: &'a [Inst<'a>], stack: &mut Stack<'a>, env: &Rc {