changed fun display, add examples

This commit is contained in:
mehbark 2025-07-03 11:45:13 -04:00
parent 6ee6ebfb64
commit 6183009481
4 changed files with 116 additions and 2 deletions

View file

@ -0,0 +1,9 @@
0 ->i
(
i println
i 1 + ->i
loop!
)->loop
loop!

28
examples/foo.puyo Normal file
View file

@ -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!

55
examples/mandelbrot.puyo Normal file
View file

@ -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!

View file

@ -30,8 +30,8 @@ impl fmt::Display for Val<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
Val::Num(n) => write!(f, "{n}"), Val::Num(n) => write!(f, "{n}"),
Val::Fun { insts, env: _ } => { Val::Fun { insts, env } => {
write!(f, "{}", Inst::Block(insts.to_vec())) 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<'_> { impl Default for Env<'_> {
fn default() -> Self { fn default() -> Self {
Env::Local(HashMap::new()) Env::Local(HashMap::new())
@ -211,6 +231,8 @@ fn eval<'a>(insts: &'a [Inst<'a>], stack: &mut Stack<'a>, env: &Rc<RefCell<Env<'
for (i, val) in stack.0.iter().rev().enumerate() { for (i, val) in stack.0.iter().rev().enumerate() {
eprintln!("{}. {val}", i + 1); eprintln!("{}. {val}", i + 1);
} }
io::stdout().flush().unwrap();
io::stderr().flush().unwrap();
process::exit(3); process::exit(3);
} }
"ite" => { "ite" => {