2022-04-11 01:34:08 +02:00
|
|
|
Forth in Rust.
|
|
|
|
|
|
|
|
FRUSTRATION has got a foot standing on its own tail because writing a
|
|
|
|
monolithic outer interpreter in a high level language makes it really
|
|
|
|
annoying to monkey with the functioning of the interpreter from within
|
|
|
|
the language it's interpreting. PARSE/WORD and the input stream
|
|
|
|
handling was the first place this became obvious. This design is a
|
|
|
|
dead end. The path forward would be stripping it back to primitives
|
|
|
|
and rewriting the outer interpreter in Forth.
|
2022-04-11 02:11:49 +02:00
|
|
|
|
|
|
|
Here are things it can do today:
|
|
|
|
|
|
|
|
Print some terms of the fibonacci sequence:
|
|
|
|
```
|
|
|
|
: over >r dup r> swap ;
|
|
|
|
: fib recursive r> drop over + swap dup . dup 144 - ? fib ;
|
|
|
|
: fib 1 0 fib ;
|
|
|
|
|
|
|
|
fib
|
|
|
|
1 1 2 3 5 8 13 21 34 55 89 144 ok
|
|
|
|
```
|
|
|
|
|
|
|
|
Compute the number of cans in a triangular stack of height n.
|
|
|
|
For example a stack of height 3 contains 6 cans.
|
|
|
|
```
|
|
|
|
x
|
|
|
|
x x
|
|
|
|
x x x
|
|
|
|
```
|
|
|
|
|
|
|
|
```
|
|
|
|
variable cans
|
|
|
|
: c recursive r> drop dup cans @ + cans ! 1 - dup ? c ;
|
|
|
|
: c c ;
|
|
|
|
: can-stack 0 cans ! c cans @ ;
|
|
|
|
|
|
|
|
3 can-stack .
|
|
|
|
6 ok
|
|
|
|
10 can-stack .
|
|
|
|
55 ok
|
|
|
|
```
|