From bb2da3fd725bbc0672ad7e61fd441434c51da630 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Sun, 1 May 2016 01:23:32 +1200 Subject: [PATCH] Ported last of relevant jonesforth library code. --- examples/mandelbrot.4th | 16 +++++++++---- src/lib.4th | 53 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 5 deletions(-) diff --git a/examples/mandelbrot.4th b/examples/mandelbrot.4th index d31b028..b8195f4 100644 --- a/examples/mandelbrot.4th +++ b/examples/mandelbrot.4th @@ -1,7 +1,7 @@ ( --- Complex arithmetic --- ) ( Location of floating point. ) -: precision 10000 ; +10000 value precision : sign dup abs / ; @@ -47,11 +47,13 @@ 2over 2swap csq c+ ; +100 value maxiter + : inSet? ( cr ci -- res ) 0 0 ( z_0 = 0 ) true ( flag indicating set membership ) - 100 0 do + maxiter 0 do drop iterate @@ -68,11 +70,15 @@ -rot 2drop -rot 2drop ; -: xsteps 100 ; -: ysteps 30 ; +100 value xsteps +30 value ysteps + +: mandelDomain + -2 0 >scaled -1 0 >scaled 0 5 >scaled 1 0 >scaled +; ( Draw the Mandelbrot Set!) -: mandel ( x1 y1 x2 y2 -- ) +: mandelDraw ( x1 y1 x2 y2 -- ) 0 pick 3 pick - ysteps / 1 pick 4 pick do diff --git a/src/lib.4th b/src/lib.4th index 5d6507d..eae7a5d 100644 --- a/src/lib.4th +++ b/src/lib.4th @@ -448,6 +448,59 @@ ' EXIT , ( append the codeword EXIT ) ; +: ALLOT ( n -- addr ) + HERE @ SWAP ( here n ) + HERE +! ( adds n to HERE, after this the old value of HERE is still on the stack ) +; + +: VARIABLE + 1 CELLS ALLOT ( allocate 1 cell of memory, push the pointer to this memory ) + WORD CREATE ( make the dictionary entry (the name follows VARIABLE) ) + DOCOL , ( append DOCOL (the codeword field of this word) ) + ' LIT , ( append the codeword LIT ) + , ( append the pointer to the new memory ) + ' EXIT , ( append the codeword EXIT ) +; + + +: VALUE ( n -- ) + WORD CREATE ( make the dictionary entry (the name follows VALUE) ) + DOCOL , ( append DOCOL ) + ' LIT , ( append the codeword LIT ) + , ( append the initial value ) + ' EXIT , ( append the codeword EXIT ) +; + +: TO IMMEDIATE ( n -- ) + WORD ( get the name of the value ) + FIND ( look it up in the dictionary ) + >DFA ( get a pointer to the first data field (the 'LIT') ) + 1+ ( increment to point at the value ) + STATE @ IF ( compiling? ) + ' LIT , ( compile LIT ) + , ( compile the address of the value ) + ' ! , ( compile ! ) + ELSE ( immediate mode ) + ! ( update it straightaway ) + THEN +; + +( x +TO VAL adds x to VAL ) +: +TO IMMEDIATE + WORD ( get the name of the value ) + FIND ( look it up in the dictionary ) + >DFA ( get a pointer to the first data field (the 'LIT') ) + 1+ ( increment to point at the value ) + STATE @ IF ( compiling? ) + ' LIT , ( compile LIT ) + , ( compile the address of the value ) + ' +! , ( compile +! ) + ELSE ( immediate mode ) + +! ( update it straightaway ) + THEN +; + + ( PRINTING THE DICTIONARY ------------------------------------------------------ ) : ID. -- 2.20.1