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