changed fun display, add examples
This commit is contained in:
parent
6ee6ebfb64
commit
6183009481
4 changed files with 116 additions and 2 deletions
9
examples/count-forever.puyo
Normal file
9
examples/count-forever.puyo
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
0 ->i
|
||||
|
||||
(
|
||||
i println
|
||||
i 1 + ->i
|
||||
loop!
|
||||
)->loop
|
||||
|
||||
loop!
|
||||
28
examples/foo.puyo
Normal file
28
examples/foo.puyo
Normal 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
55
examples/mandelbrot.puyo
Normal 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!
|
||||
26
src/eval.rs
26
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<RefCell<Env<'
|
|||
for (i, val) in stack.0.iter().rev().enumerate() {
|
||||
eprintln!("{}. {val}", i + 1);
|
||||
}
|
||||
io::stdout().flush().unwrap();
|
||||
io::stderr().flush().unwrap();
|
||||
process::exit(3);
|
||||
}
|
||||
"ite" => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue