Last active
February 5, 2022 03:14
-
-
Save eparadis/ee436d65f30e57b6f459839d472a096a to your computer and use it in GitHub Desktop.
classic terminal mandelbrot in FORTH
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
\ from proposal http://www.forth200x.org/fvalue.html | |
variable %var | |
: to 1 %var ! ; | |
: fvalue create f, does> %var @ if f! else f@ then 0 %var ! ; | |
0e0 fvalue i3 | |
0e0 fvalue r3 | |
59 value x1 | |
21 value y1 | |
-1e0 fvalue i1 | |
1e0 fvalue i2 | |
-2e0 fvalue r1 | |
1e0 fvalue r2 | |
r2 r1 f- x1 s>f f/ fvalue s1 \ L30 | |
i2 i1 f- y1 s>f f/ fvalue s2 \ L31 | |
: single_iter { F: z1 F: z2 } ( F: z1 F: z2 -- F: z1' F: z2' F: mag ) | |
z1 fdup f* fdup \ L90 ( -- a a ) | |
z2 fdup f* fdup \ L91 ( -- a a b b ) | |
frot \ ( -- a b b a ) | |
f+ \ ( -- a b mag ) L100 | |
frot frot \ ( -- mag a b ) | |
f- r3 f+ \ z1 \ L111 ( -- mag z1' ) | |
2e0 z1 z2 f* f* i3 f+ \ z2 L110 ( -- mag z1' z2' ) | |
frot \ ( -- z1' z2' mag ) | |
; | |
: print_char ( F: x F: y -- ) | |
62 \ character to emit w/ a single iteration | |
30 \ push the max in case we don't exit early | |
30 0 do \ L80 | |
single_iter | |
4e0 f> if | |
drop i \ replace the max with the actual number of times we iterated | |
leave | |
then | |
loop \ L120 | |
fdrop fdrop \ clean z1 and z2 left from single_iter | |
- emit \ L130 | |
; | |
: calc_i3 { y } | |
y s>f s2 f* i1 f+ to i3 \ L50 | |
; | |
: calc_r3 { x } | |
x s>f s1 f* r1 f+ to r3 \ L70 | |
; | |
: mandel | |
cr \ always start on a fresh clean line | |
y1 0 do \ L40 | |
i calc_i3 | |
x1 0 do \ L60 | |
i calc_r3 | |
r3 i3 print_char | |
loop \ L140 | |
cr \ L150 | |
loop \ L160 | |
; | |
mandel | |
bye |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unpacking the "polyfill"
fvalue
at the top:So there is one use of
fvalue
:1e0 fvalue foobar
Create a new word named
foobar
and store 1.0 into its dictionary entry, along with setting the run-time behavior offoobar
You can then use that word two ways:
foobar \ Put the value of foobar onto the (floating point) stack
The default operation of
foobar
just pushes a copy of the value stored inside it's dictionary entry.3e0 to foobar \ Set foobar to the new value of 3.0.
to
putsfoobar
into "storing mode", which sets the value held in it's dictionary entry.