Comment changes.
[jonesforth.git] / jonesforth.f
1 \ -*- text -*-
2 \       A sometimes minimal FORTH compiler and tutorial for Linux / i386 systems. -*- asm -*-
3 \       By Richard W.M. Jones <rich@annexia.org> http://annexia.org/forth
4 \       This is PUBLIC DOMAIN (see public domain release statement below).
5 \       $Id: jonesforth.f,v 1.3 2007-09-25 09:50:54 rich Exp $
6 \
7 \       The first part of this tutorial is in jonesforth.S.  Get if from http://annexia.org/forth
8 \
9 \       PUBLIC DOMAIN ----------------------------------------------------------------------
10 \
11 \       I, the copyright holder of this work, hereby release it into the public domain. This applies worldwide.
12 \
13 \       In case this is not legally possible, I grant any entity the right to use this work for any purpose,
14 \       without any conditions, unless such conditions are required by law.
15 \
16 \       SETTING UP ----------------------------------------------------------------------
17 \
18 \       Let's get a few housekeeping things out of the way.  Firstly because I need to draw lots of
19 \       ASCII-art diagrams to explain concepts, the best way to look at this is using a window which
20 \       uses a fixed width font and is at least this wide:
21 \
22 \<------------------------------------------------------------------------------------------------------------------------>
23 \
24 \       Secondly make sure TABS are set to 8 characters.  The following should be a vertical
25 \       line.  If not, sort out your tabs.
26 \
27 \       |
28 \       |
29 \       |
30 \
31 \       Thirdly I assume that your screen is at least 50 characters high.
32 \
33 \       START OF FORTH CODE ----------------------------------------------------------------------
34 \
35 \       We've now reached the stage where the FORTH system is running and self-hosting.  All further
36 \       words can be written as FORTH itself, including words like IF, THEN, .", etc which in most
37 \       languages would be considered rather fundamental.
38 \
39 \       Some notes about the code:
40 \
41 \       I use indenting to show structure.  The amount of whitespace has no meaning to FORTH however
42 \       except that you must use at least one whitespace character between words, and words themselves
43 \       cannot contain whitespace.
44 \
45 \       FORTH is case-sensitive.  Use capslock!
46
47 \ Define some character constants
48 : '\n'   10 ;
49 : 'SPACE' 32 ;
50
51 \ CR prints a carriage return
52 : CR '\n' EMIT ;
53
54 \ SPACE prints a space
55 : SPACE 'SPACE' EMIT ;
56
57 \ DUP, DROP are defined in assembly for speed, but this is how you might define them
58 \ in FORTH.  Notice use of the scratch variables _X and _Y.
59 \ : DUP _X ! _X @ _X @ ;
60 \ : DROP _X ! ;
61
62 \ The built-in . (DOT) function doesn't print a space after the number (unlike the real FORTH word).
63 \ However this is very easily fixed by redefining . (DOT).  Any built-in word can be redefined.
64 : .
65         .               \ this refers back to the previous definition (but see also RECURSE below)
66         SPACE
67 ;
68
69 \ The 2... versions of the standard operators work on pairs of stack entries.  They're not used
70 \ very commonly so not really worth writing in assembler.  Here is how they are defined in FORTH.
71 : 2DUP OVER OVER ;
72 : 2DROP DROP DROP ;
73
74 \ More standard FORTH words.
75 : 2* 2 * ;
76 : 2/ 2 / ;
77
78 \ Standard words for manipulating BASE.
79 : DECIMAL 10 BASE ! ;
80 : HEX 16 BASE ! ;
81
82 \ Standard words for booleans.
83 : TRUE 1 ;
84 : FALSE 0 ;
85 : NOT 0= ;
86
87 \ LITERAL takes whatever is on the stack and compiles LIT <foo>
88 : LITERAL IMMEDIATE
89         ' LIT ,         \ compile LIT
90         ,               \ compile the literal itself (from the stack)
91         ;
92
93 \ Now we can use [ and ] to insert literals which are calculated at compile time.
94 \ Within definitions, use [ ... ] LITERAL anywhere that '...' is a constant expression which you
95 \ would rather only compute once (at compile time, rather than calculating it each time your word runs).
96 : ':'
97         [               \ go into immediate mode temporarily
98         CHAR :          \ push the number 58 (ASCII code of colon) on the stack
99         ]               \ go back to compile mode
100         LITERAL         \ compile LIT 58 as the definition of ':' word
101 ;
102
103 \ A few more character constants defined the same way as above.
104 : '(' [ CHAR ( ] LITERAL ;
105 : ')' [ CHAR ) ] LITERAL ;
106 : '"' [ CHAR " ] LITERAL ;
107
108 \ While compiling, '[COMPILE] word' compiles 'word' if it would otherwise be IMMEDIATE.
109 : [COMPILE] IMMEDIATE
110         WORD            \ get the next word
111         FIND            \ find it in the dictionary
112         >CFA            \ get its codeword
113         ,               \ and compile that
114 ;
115
116 \ So far we have defined only very simple definitions.  Before we can go further, we really need to
117 \ make some control structures, like IF ... THEN and loops.  Luckily we can define arbitrary control
118 \ structures directly in FORTH.
119 \
120 \ Please note that the control structures as I have defined them here will only work inside compiled
121 \ words.  If you try to type in expressions using IF, etc. in immediate mode, then they won't work.
122 \ Making these work in immediate mode is left as an exercise for the reader.
123
124 \ condition IF true-part THEN rest
125 \       -- compiles to: --> condition 0BRANCH OFFSET true-part rest
126 \       where OFFSET is the offset of 'rest'
127 \ condition IF true-part ELSE false-part THEN
128 \       -- compiles to: --> condition 0BRANCH OFFSET true-part BRANCH OFFSET2 false-part rest
129 \       where OFFSET if the offset of false-part and OFFSET2 is the offset of rest
130
131 \ IF is an IMMEDIATE word which compiles 0BRANCH followed by a dummy offset, and places
132 \ the address of the 0BRANCH on the stack.  Later when we see THEN, we pop that address
133 \ off the stack, calculate the offset, and back-fill the offset.
134 : IF IMMEDIATE
135         ' 0BRANCH ,     \ compile 0BRANCH
136         HERE @          \ save location of the offset on the stack
137         0 ,             \ compile a dummy offset
138 ;
139
140 : THEN IMMEDIATE
141         DUP
142         HERE @ SWAP -   \ calculate the offset from the address saved on the stack
143         SWAP !          \ store the offset in the back-filled location
144 ;
145
146 : ELSE IMMEDIATE
147         ' BRANCH ,      \ definite branch to just over the false-part
148         HERE @          \ save location of the offset on the stack
149         0 ,             \ compile a dummy offset
150         SWAP            \ now back-fill the original (IF) offset
151         DUP             \ same as for THEN word above
152         HERE @ SWAP -
153         SWAP !
154 ;
155
156 \ BEGIN loop-part condition UNTIL
157 \       -- compiles to: --> loop-part condition 0BRANCH OFFSET
158 \       where OFFSET points back to the loop-part
159 \ This is like do { loop-part } while (condition) in the C language
160 : BEGIN IMMEDIATE
161         HERE @          \ save location on the stack
162 ;
163
164 : UNTIL IMMEDIATE
165         ' 0BRANCH ,     \ compile 0BRANCH
166         HERE @ -        \ calculate the offset from the address saved on the stack
167         ,               \ compile the offset here
168 ;
169
170 \ BEGIN loop-part AGAIN
171 \       -- compiles to: --> loop-part BRANCH OFFSET
172 \       where OFFSET points back to the loop-part
173 \ In other words, an infinite loop which can only be returned from with EXIT
174 : AGAIN IMMEDIATE
175         ' BRANCH ,      \ compile BRANCH
176         HERE @ -        \ calculate the offset back
177         ,               \ compile the offset here
178 ;
179
180 \ BEGIN condition WHILE loop-part REPEAT
181 \       -- compiles to: --> condition 0BRANCH OFFSET2 loop-part BRANCH OFFSET
182 \       where OFFSET points back to condition (the beginning) and OFFSET2 points to after the whole piece of code
183 \ So this is like a while (condition) { loop-part } loop in the C language
184 : WHILE IMMEDIATE
185         ' 0BRANCH ,     \ compile 0BRANCH
186         HERE @          \ save location of the offset2 on the stack
187         0 ,             \ compile a dummy offset2
188 ;
189
190 : REPEAT IMMEDIATE
191         ' BRANCH ,      \ compile BRANCH
192         SWAP            \ get the original offset (from BEGIN)
193         HERE @ - ,      \ and compile it after BRANCH
194         DUP
195         HERE @ SWAP -   \ calculate the offset2
196         SWAP !          \ and back-fill it in the original location
197 ;
198
199 \ FORTH allows ( ... ) as comments within function definitions.  This works by having an IMMEDIATE
200 \ word called ( which just drops input characters until it hits the corresponding ).
201 : ( IMMEDIATE
202         1               \ allowed nested parens by keeping track of depth
203         BEGIN
204                 KEY             \ read next character
205                 DUP '(' = IF    \ open paren?
206                         DROP            \ drop the open paren
207                         1+              \ depth increases
208                 ELSE
209                         ')' = IF        \ close paren?
210                                 1-              \ depth decreases
211                         THEN
212                 THEN
213         DUP 0= UNTIL            \ continue until we reach matching close paren, depth 0
214         DROP            \ drop the depth counter
215 ;
216
217 (
218         From now on we can use ( ... ) for comments.
219
220         In FORTH style we can also use ( ... -- ... ) to show the effects that a word has on the
221         parameter stack.  For example:
222
223         ( n -- )        means that the word consumes an integer (n) from the parameter stack.
224         ( b a -- c )    means that the word uses two integers (a and b, where a is at the top of stack)
225                                 and returns a single integer (c).
226         ( -- )          means the word has no effect on the stack
227 )
228
229 ( With the looping constructs, we can now write SPACES, which writes n spaces to stdout. )
230 : SPACES        ( n -- )
231         BEGIN
232                 DUP 0>          ( while n > 0 )
233         WHILE
234                 SPACE           ( print a space )
235                 1-              ( until we count down to 0 )
236         REPEAT
237         DROP
238 ;
239
240 ( c a b WITHIN returns true if a <= c and c < b )
241 : WITHIN
242         ROT             ( b c a )
243         OVER            ( b c a c )
244         <= IF
245                 > IF            ( b c -- )
246                         TRUE
247                 ELSE
248                         FALSE
249                 THEN
250         ELSE
251                 2DROP           ( b c -- )
252                 FALSE
253         THEN
254 ;
255
256 ( .S prints the contents of the stack.  Very useful for debugging. )
257 : .S            ( -- )
258         DSP@            ( get current stack pointer )
259         BEGIN
260                 DUP S0 @ <
261         WHILE
262                 DUP @ .         ( print the stack element )
263                 4+              ( move up )
264         REPEAT
265         DROP
266 ;
267
268 ( DEPTH returns the depth of the stack. )
269 : DEPTH         ( -- n )
270         S0 @ DSP@ -
271         4-                      ( adjust because S0 was on the stack when we pushed DSP )
272 ;
273
274 (
275         S" string" is used in FORTH to define strings.  It leaves the address of the string and
276         its length on the stack, with the address at the top.  The space following S" is the normal
277         space between FORTH words and is not a part of the string.
278
279         This is tricky to define because it has to do different things depending on whether
280         we are compiling or in immediate mode.  (Thus the word is marked IMMEDIATE so it can
281         detect this and do different things).
282
283         In compile mode we append
284                 LITSTRING <string length> <string rounded up 4 bytes>
285         to the current word.  The primitive LITSTRING does the right thing when the current
286         word is executed.
287
288         In immediate mode there isn't a particularly good place to put the string, but in this
289         case we put the string at HERE (but we _don't_ change HERE).  This is meant as a temporary
290         location, likely to be overwritten soon after.
291 )
292 : S" IMMEDIATE          ( -- len addr )
293         STATE @ IF      ( compiling? )
294                 ' LITSTRING ,   ( compile LITSTRING )
295                 HERE @          ( save the address of the length word on the stack )
296                 0 ,             ( dummy length - we don't know what it is yet )
297                 BEGIN
298                         KEY             ( get next character of the string )
299                         DUP '"' <>
300                 WHILE
301                         HERE @ !b       ( store the character in the compiled image )
302                         1 HERE +!       ( increment HERE pointer by 1 byte )
303                 REPEAT
304                 DROP            ( drop the double quote character at the end )
305                 DUP             ( get the saved address of the length word )
306                 HERE @ SWAP -   ( calculate the length )
307                 4-              ( subtract 4 (because we measured from the start of the length word) )
308                 SWAP !          ( and back-fill the length location )
309                 HERE @          ( round up to next multiple of 4 bytes for the remaining code )
310                 3 +
311                 3 INVERT AND
312                 HERE !
313         ELSE            ( immediate mode )
314                 HERE @          ( get the start address of the temporary space )
315                 BEGIN
316                         KEY
317                         DUP '"' <>
318                 WHILE
319                         OVER !b         ( save next character )
320                         1+              ( increment address )
321                 REPEAT
322                 DROP            ( drop the final " character )
323                 HERE @ -        ( calculate the length )
324                 HERE @          ( push the start address )
325         THEN
326 ;
327
328 (
329         ." is the print string operator in FORTH.  Example: ." Something to print"
330         The space after the operator is the ordinary space required between words and is not
331         a part of what is printed.
332
333         In immediate mode we just keep reading characters and printing them until we get to
334         the next double quote.
335
336         In compile mode we use S" to store the string, then add EMITSTRING afterwards:
337                 LITSTRING <string length> <string rounded up to 4 bytes> EMITSTRING
338
339         It may be interesting to note the use of [COMPILE] to turn the call to the immediate
340         word S" into compilation of that word.  It compiles it into the definition of .",
341         not into the definition of the word being compiled when this is running (complicated
342         enough for you?)
343 )
344 : ." IMMEDIATE          ( -- )
345         STATE @ IF      ( compiling? )
346                 [COMPILE] S"    ( read the string, and compile LITSTRING, etc. )
347                 ' EMITSTRING ,  ( compile the final EMITSTRING )
348         ELSE
349                 ( In immediate mode, just read characters and print them until we get
350                   to the ending double quote. )
351                 BEGIN
352                         KEY
353                         DUP '"' = IF
354                                 DROP    ( drop the double quote character )
355                                 EXIT    ( return from this function )
356                         THEN
357                         EMIT
358                 AGAIN
359         THEN
360 ;
361
362 (
363         In FORTH, global constants and variables are defined like this:
364
365         10 CONSTANT TEN         when TEN is executed, it leaves the integer 10 on the stack
366         VARIABLE VAR            when VAR is executed, it leaves the address of VAR on the stack
367
368         Constants can be read by not written, eg:
369
370         TEN . CR                prints 10
371
372         You can read a variable (in this example called VAR) by doing:
373
374         VAR @                   leaves the value of VAR on the stack
375         VAR @ . CR              prints the value of VAR
376
377         and update the variable by doing:
378
379         20 VAR !                sets VAR to 20
380
381         Note that variables are uninitialised (but see VALUE later on which provides initialised
382         variables with a slightly simpler syntax).
383
384         How can we define the words CONSTANT and VARIABLE?
385
386         The trick is to define a new word for the variable itself (eg. if the variable was called
387         'VAR' then we would define a new word called VAR).  This is easy to do because we exposed
388         dictionary entry creation through the CREATE word (part of the definition of : above).
389         A call to CREATE TEN leaves the dictionary entry:
390
391                                    +--- HERE
392                                    |
393                                    V
394         +---------+---+---+---+---+
395         | LINK    | 3 | T | E | N |
396         +---------+---+---+---+---+
397                    len
398
399         For CONSTANT we can continue by appending DOCOL (the codeword), then LIT followed by
400         the constant itself and then EXIT, forming a little word definition that returns the
401         constant:
402
403         +---------+---+---+---+---+------------+------------+------------+------------+
404         | LINK    | 3 | T | E | N | DOCOL      | LIT        | 10         | EXIT       |
405         +---------+---+---+---+---+------------+------------+------------+------------+
406                    len              codeword
407
408         Notice that this word definition is exactly the same as you would have got if you had
409         written : TEN 10 ;
410 )
411 : CONSTANT
412         CREATE          ( make the dictionary entry (the name follows CONSTANT) )
413         DOCOL ,         ( append DOCOL (the codeword field of this word) )
414         ' LIT ,         ( append the codeword LIT )
415         ,               ( append the value on the top of the stack )
416         ' EXIT ,        ( append the codeword EXIT )
417 ;
418
419 (
420         VARIABLE is a little bit harder because we need somewhere to put the variable.  There is
421         nothing particularly special about the 'user definitions area' (the area of memory pointed
422         to by HERE where we have previously just stored new word definitions).  We can slice off
423         bits of this memory area to store anything we want, so one possible definition of
424         VARIABLE might create this:
425
426            +--------------------------------------------------------------+
427            |                                                              |
428            V                                                              |
429         +---------+---------+---+---+---+---+------------+------------+---|--------+------------+
430         | <var>   | LINK    | 3 | V | A | R | DOCOL      | LIT        | <addr var> | EXIT       |
431         +---------+---------+---+---+---+---+------------+------------+------------+------------+
432                              len              codeword
433
434         where <var> is the place to store the variable, and <addr var> points back to it.
435
436         To make this more general let's define a couple of words which we can use to allocate
437         arbitrary memory from the user definitions area.
438
439         First ALLOT, where n ALLOT allocates n bytes of memory.  (Note when calling this that
440         it's a very good idea to make sure that n is a multiple of 4, or at least that next time
441         a word is compiled that HERE has been left as a multiple of 4).
442 )
443 : ALLOT         ( n -- addr )
444         HERE @ SWAP     ( here n )
445         HERE +!         ( adds n to HERE, after this the old value of HERE is still on the stack )
446 ;
447
448 (
449         Second, CELLS.  In FORTH the phrase 'n CELLS ALLOT' means allocate n integers of whatever size
450         is the natural size for integers on this machine architecture.  On this 32 bit machine therefore
451         CELLS just multiplies the top of stack by 4.
452 )
453 : CELLS ( n -- n ) 4 * ;
454
455 (
456         So now we can define VARIABLE easily in much the same way as CONSTANT above.  Refer to the
457         diagram above to see what the word that this creates will look like.
458 )
459 : VARIABLE
460         1 CELLS ALLOT   ( allocate 1 cell of memory, push the pointer to this memory )
461         CREATE          ( make the dictionary entry (the name follows VARIABLE) )
462         DOCOL ,         ( append DOCOL (the codeword field of this word) )
463         ' LIT ,         ( append the codeword LIT )
464         ,               ( append the pointer to the new memory )
465         ' EXIT ,        ( append the codeword EXIT )
466 ;
467
468 (
469         VALUEs are like VARIABLEs but with a simpler syntax.  You would generally use them when you
470         want a variable which is read often, and written infrequently.
471
472         20 VALUE VAL    creates VAL with initial value 20
473         VAL             pushes the value directly on the stack
474         30 TO VAL       updates VAL, setting it to 30
475
476         Notice that 'VAL' on its own doesn't return the address of the value, but the value itself,
477         making values simpler and more obvious to use than variables (no indirection through '@').
478         The price is a more complicated implementation, although despite the complexity there is no
479         performance penalty at runtime.
480
481         A naive implementation of 'TO' would be quite slow, involving a dictionary search each time.
482         But because this is FORTH we have complete control of the compiler so we can compile TO more
483         efficiently, turning:
484                 TO VAL
485         into:
486                 LIT <addr> !
487         and calculating <addr> (the address of the value) at compile time.
488
489         Now this is the clever bit.  We'll compile our value like this:
490
491         +---------+---+---+---+---+------------+------------+------------+------------+
492         | LINK    | 3 | V | A | L | DOCOL      | LIT        | <value>    | EXIT       |
493         +---------+---+---+---+---+------------+------------+------------+------------+
494                    len              codeword
495
496         where <value> is the actual value itself.  Note that when VAL executes, it will push the
497         value on the stack, which is what we want.
498
499         But what will TO use for the address <addr>?  Why of course a pointer to that <value>:
500
501                 code compiled   - - - - --+------------+------------+------------+-- - - - -
502                 by TO VAL                 | LIT        | <addr>     | !          |
503                                 - - - - --+------------+-----|------+------------+-- - - - -
504                                                              |
505                                                              V
506         +---------+---+---+---+---+------------+------------+------------+------------+
507         | LINK    | 3 | V | A | L | DOCOL      | LIT        | <value>    | EXIT       |
508         +---------+---+---+---+---+------------+------------+------------+------------+
509                    len              codeword
510
511         In other words, this is a kind of self-modifying code.
512
513         (Note to the people who want to modify this FORTH to add inlining: values defined this
514         way cannot be inlined).
515 )
516 : VALUE         ( n -- )
517         CREATE          ( make the dictionary entry (the name follows VALUE) )
518         DOCOL ,         ( append DOCOL )
519         ' LIT ,         ( append the codeword LIT )
520         ,               ( append the initial value )
521         ' EXIT ,        ( append the codeword EXIT )
522 ;
523
524 : TO IMMEDIATE  ( n -- )
525         WORD            ( get the name of the value )
526         FIND            ( look it up in the dictionary )
527         >DFA            ( get a pointer to the first data field (the 'LIT') )
528         4+              ( increment to point at the value )
529         STATE @ IF      ( compiling? )
530                 ' LIT ,         ( compile LIT )
531                 ,               ( compile the address of the value )
532                 ' ! ,           ( compile ! )
533         ELSE            ( immediate mode )
534                 !               ( update it straightaway )
535         THEN
536 ;
537
538 ( x +TO VAL adds x to VAL )
539 : +TO IMMEDIATE
540         WORD            ( get the name of the value )
541         FIND            ( look it up in the dictionary )
542         >DFA            ( get a pointer to the first data field (the 'LIT') )
543         4+              ( increment to point at the value )
544         STATE @ IF      ( compiling? )
545                 ' LIT ,         ( compile LIT )
546                 ,               ( compile the address of the value )
547                 ' +! ,          ( compile +! )
548         ELSE            ( immediate mode )
549                 +!              ( update it straightaway )
550         THEN
551 ;
552
553 (
554         ID. takes an address of a dictionary entry and prints the word's name.
555
556         For example: LATEST @ ID. would print the name of the last word that was defined.
557 )
558 : ID.
559         4+              ( skip over the link pointer )
560         DUP @b          ( get the flags/length byte )
561         F_LENMASK AND   ( mask out the flags - just want the length )
562
563         BEGIN
564                 DUP 0>          ( length > 0? )
565         WHILE
566                 SWAP 1+         ( addr len -- len addr+1 )
567                 DUP @b          ( len addr -- len addr char | get the next character)
568                 EMIT            ( len addr char -- len addr | and print it)
569                 SWAP 1-         ( len addr -- addr len-1    | subtract one from length )
570         REPEAT
571         2DROP           ( len addr -- )
572 ;
573
574 (
575         'WORD word FIND ?HIDDEN' returns true if 'word' is flagged as hidden.
576
577         'WORD word FIND ?IMMEDIATE' returns true if 'word' is flagged as immediate.
578 )
579 : ?HIDDEN
580         4+              ( skip over the link pointer )
581         @b              ( get the flags/length byte )
582         F_HIDDEN AND    ( mask the F_HIDDEN flag and return it (as a truth value) )
583 ;
584 : ?IMMEDIATE
585         4+              ( skip over the link pointer )
586         @b              ( get the flags/length byte )
587         F_IMMED AND     ( mask the F_IMMED flag and return it (as a truth value) )
588 ;
589
590 (
591         WORDS prints all the words defined in the dictionary, starting with the word defined most recently.
592         However it doesn't print hidden words.
593
594         The implementation simply iterates backwards from LATEST using the link pointers.
595 )
596 : WORDS
597         LATEST @        ( start at LATEST dictionary entry )
598         BEGIN
599                 DUP 0<>         ( while link pointer is not null )
600         WHILE
601                 DUP ?HIDDEN NOT IF      ( ignore hidden words )
602                         DUP ID.         ( but if not hidden, print the word )
603                 THEN
604                 SPACE
605                 @               ( dereference the link pointer - go to previous word )
606         REPEAT
607         DROP
608         CR
609 ;
610
611 (
612         So far we have only allocated words and memory.  FORTH provides a rather primitive method
613         to deallocate.
614
615         'FORGET word' deletes the definition of 'word' from the dictionary and everything defined
616         after it, including any variables and other memory allocated after.
617
618         The implementation is very simple - we look up the word (which returns the dictionary entry
619         address).  Then we set HERE to point to that address, so in effect all future allocations
620         and definitions will overwrite memory starting at the word.  We also need to set LATEST to
621         point to the previous word.
622
623         Note that you cannot FORGET built-in words (well, you can try but it will probably cause
624         a segfault).
625
626         XXX: Because we wrote VARIABLE to store the variable in memory allocated before the word,
627         in the current implementation VARIABLE FOO FORGET FOO will leak 1 cell of memory.
628 )
629 : FORGET
630         WORD FIND       ( find the word, gets the dictionary entry address )
631         DUP @ LATEST !  ( set LATEST to point to the previous word )
632         HERE !          ( and store HERE with the dictionary address )
633 ;
634
635 (
636         RECURSE makes a recursive call to the current word that is being compiled.
637
638         Normally while a word is being compiled, it is marked HIDDEN so that references to the
639         same word within are calls to the previous definition of the word.  However we still have
640         access to the word which we are currently compiling through the LATEST pointer so we
641         can use that to compile a recursive call.
642 )
643 : RECURSE IMMEDIATE
644         LATEST @ >CFA   ( LATEST points to the word being compiled at the moment )
645         ,               ( compile it )
646 ;
647
648 (
649         DUMP is used to dump out the contents of memory, in the 'traditional' hexdump format.
650 )
651 : DUMP          ( addr len -- )
652         BASE @ ROT              ( save the current BASE at the bottom of the stack )
653         HEX                     ( and switch the hexadecimal mode )
654
655         BEGIN
656                 DUP 0>          ( while len > 0 )
657         WHILE
658                 OVER .          ( print the address )
659                 SPACE
660
661                 ( print up to 16 words on this line )
662                 2DUP            ( addr len addr len )
663                 1- 15 AND 1+    ( addr len addr linelen )
664                 BEGIN
665                         DUP 0>          ( while linelen > 0 )
666                 WHILE
667                         SWAP            ( addr len linelen addr )
668                         DUP @b          ( addr len linelen addr byte )
669                         . SPACE         ( print the byte )
670                         1+ SWAP 1-      ( addr len linelen addr -- addr len addr+1 linelen-1 )
671                 REPEAT
672                 2DROP           ( addr len )
673
674                 ( print the ASCII equivalents )
675                 2DUP 1- 15 AND 1+ ( addr len addr linelen )
676                 BEGIN
677                         DUP 0>          ( while linelen > 0)
678                 WHILE
679                         SWAP            ( addr len linelen addr )
680                         DUP @b          ( addr len linelen addr byte )
681                         DUP 32 128 WITHIN IF    ( 32 <= c < 128? )
682                                 EMIT
683                         ELSE
684                                 DROP [ CHAR ? ] LITERAL EMIT
685                         THEN
686                         1+ SWAP 1-      ( addr len linelen addr -- addr len addr+1 linelen-1 )
687                 REPEAT
688                 2DROP           ( addr len )
689                 CR
690
691                 DUP 1- 15 AND 1+ ( addr len linelen )
692                 DUP             ( addr len linelen linelen )
693                 ROT             ( addr linelen len linelen )
694                 -               ( addr linelen len-linelen )
695                 ROT             ( len-linelen addr linelen )
696                 +               ( len-linelen addr+linelen )
697                 SWAP            ( addr-linelen len-linelen )
698         REPEAT
699
700         2DROP                   ( restore stack )
701         BASE !                  ( restore saved BASE )
702 ;
703
704 ( Finally print the welcome prompt. )
705 ." JONESFORTH VERSION " VERSION . CR
706 ." OK "