8 latest dup c@ flag_immediate OR
12 : ( 41 word drop ; immediate
13 ( That was the definition for the comment word. )
14 ( Now we can add comments to what we are doing! )
15 ( Note that we are in decimal numeric input mode. )
17 : \ ( <line> -- , comment out rest of line )
21 \ 1 echo ! \ Uncomment this line to echo Forth code while compiling.
23 \ *********************************************************************
24 \ This is another style of comment that is common in Forth.
25 \ pFORTH - Portable Forth System
29 \ Copyright 1994 3DO, Phil Burk, Larry Polansky, David Rosenboom
31 \ Permission to use, copy, modify, and/or distribute this
32 \ software for any purpose with or without fee is hereby granted.
34 \ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
35 \ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
36 \ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
37 \ THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
38 \ CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
39 \ FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
40 \ CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
41 \ OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42 \ *********************************************************************
44 : COUNT dup 1+ swap c@ ;
46 \ Miscellaneous support words
47 : ON ( addr -- , set true )
50 : OFF ( addr -- , set false )
54 : CELL+ ( n -- n+cell ) cell + ;
55 : CELL- ( n -- n+cell ) cell - ;
56 : CELL* ( n -- n*cell ) cells ;
58 : CHAR+ ( n -- n+size_of_char ) 1+ ;
59 : CHARS ( n -- n*size_of_char , don't do anything) ; immediate
61 \ useful stack manipulation words
62 : -ROT ( a b c -- c a b )
65 : 3DUP ( a b c -- a b c a b c )
74 : TUCK ( a b -- b a b )
78 : <= ( a b -- f , true if A <= b )
81 : >= ( a b -- f , true if A >= b )
85 : INVERT ( n -- 1'comp )
89 : NOT ( n -- !n , logical negation )
97 : DNEGATE ( d -- -d , negate by doing 0-d )
102 \ --------------------------------------------------------------------
108 : DECIMAL 10 base ! ;
117 : $MOVE ( $src $dst -- )
120 : BETWEEN ( n lo hi -- flag , true if between lo & hi )
124 : [ ( -- , enter interpreter mode )
127 : ] ( -- enter compile mode )
131 : EVEN-UP ( n -- n | n+1 , make even ) dup 1 and + ;
132 : ALIGNED ( addr -- a-addr )
133 [ cell 1- ] literal +
134 [ cell 1- invert ] literal and
136 : ALIGN ( -- , align DP ) dp @ aligned dp ! ;
137 : ALLOT ( nbytes -- , allot space in dictionary ) dp +! ( align ) ;
139 : C, ( c -- ) here c! 1 chars dp +! ;
140 : W, ( w -- ) dp @ even-up dup dp ! w! 2 chars dp +! ;
141 : , ( n -- , lay into dictionary ) align here ! cell allot ;
143 \ Dictionary conversions ------------------------------------------
145 : N>NEXTLINK ( nfa -- nextlink , traverses name field )
146 dup c@ 31 and 1+ + aligned
149 : NAMEBASE ( -- base-of-names )
152 : CODEBASE ( -- base-of-code dictionary )
156 : NAMELIMIT ( -- limit-of-names )
159 : CODELIMIT ( -- limit-of-code, last address in dictionary )
163 : NAMEBASE+ ( rnfa -- nfa , convert relocatable nfa to actual )
167 : >CODE ( xt -- secondary_code_address, not valid for primitives )
171 : CODE> ( secondary_code_address -- xt , not valid for primitives )
175 : N>LINK ( nfa -- lfa )
179 : >BODY ( xt -- pfa )
183 : BODY> ( pfa -- xt )
187 \ convert between addresses useable by @, and relocatable addresses.
188 : USE->REL ( useable_addr -- rel_addr )
191 : REL->USE ( rel_addr -- useable_addr )
196 \ : >REL ( adr -- adr ) ; immediate
197 \ : >ABS ( adr -- adr ) ; immediate
199 : X@ ( addr -- xt , fetch execution token from relocatable ) @ ;
200 : X! ( addr -- xt , store execution token as relocatable ) ! ;
202 \ Compiler support ------------------------------------------------
203 : COMPILE, ( xt -- , compile call to xt )
207 ( Compiler support , based on FIG )
208 : [COMPILE] ( <name> -- , compile now even if immediate )
212 : (COMPILE) ( xt -- , postpone compilation of token )
213 [compile] literal ( compile a call to literal )
214 ( store xt of word to be compiled )
216 [ ' compile, ] literal \ compile call to compile,
220 : COMPILE ( <name> -- , save xt and compile later )
225 : :NONAME ( -- xt , begin compilation of headerless secondary )
227 here code> \ convert here to execution token
231 \ Error codes defined in ANSI Exception word set.
232 : ERR_ABORT -1 ; \ general abort
233 : ERR_ABORTQ -2 ; \ for abort"
234 : ERR_EXECUTING -14 ; \ compile time word while not compiling
235 : ERR_PAIRS -22 ; \ mismatch in conditional
236 : ERR_DEFER -258 ; \ not a deferred word
242 \ Conditionals in '83 form -----------------------------------------
243 : CONDITIONAL_KEY ( -- , lazy constant ) 29521 ;
244 : ?CONDITION ( f -- ) conditional_key - err_pairs ?error ;
245 : >MARK ( -- addr ) here 0 , ;
246 : >RESOLVE ( addr -- ) here over - swap ! ;
247 : <MARK ( -- addr ) here ;
248 : <RESOLVE ( addr -- ) here - , ;
250 : ?COMP ( -- , error if not compiling )
251 state @ 0= err_executing ?error
256 \ conditional primitives
257 : IF ( -- f orig ) ?comp compile 0branch conditional_key >mark ; immediate
258 : THEN ( f orig -- ) swap ?condition >resolve ; immediate
259 : BEGIN ( -- f dest ) ?comp conditional_key <mark ; immediate
260 : AGAIN ( f dest -- ) compile branch swap ?condition <resolve ; immediate
261 : UNTIL ( f dest -- ) compile 0branch swap ?condition <resolve ; immediate
262 : AHEAD ( -- f orig ) compile branch conditional_key >mark ; immediate
264 \ conditionals built from primitives
265 : ELSE ( f orig1 -- f orig2 )
266 [compile] AHEAD 2swap [compile] THEN ; immediate
267 : WHILE ( f dest -- f orig f dest ) [compile] if 2swap ; immediate
268 : REPEAT ( -- f orig f dest ) [compile] again [compile] then ; immediate
270 : ['] ( <name> -- xt , define compile time tick )
271 ?comp ' [compile] literal
275 \ compile time: compile create , (does>) then ;
276 \ execution time: create <name>, ',' data, then patch pi to point to @
277 \ : con create , does> @ ;
281 : (DOES>) ( xt -- , modify previous definition to execute code at xt )
282 latest name> >code \ get address of code for new word
283 cell + \ offset to second cell in create word
284 ! \ store execution token of DOES> code in new word
287 : DOES> ( -- , define execution code for CREATE word )
288 0 [compile] literal \ dummy literal to hold xt
289 here cell- \ address of zero in literal
290 compile (does>) \ call (DOES>) from new creation word
291 >r \ move addrz to return stack so ; doesn't see stack garbage
292 [compile] ; \ terminate part of code before does>
295 swap ! \ save execution token in literal
298 : VARIABLE ( <name> -- )
299 CREATE 0 , \ IMMEDIATE
300 \ DOES> [compile] aliteral \ %Q This could be optimised
303 : 2VARIABLE ( <name> -c- ) ( -x- addr )
307 : CONSTANT ( n <name> -c- ) ( -x- n )
317 : 2! ( x1 x2 addr -- , store x2 followed by x1 )
320 : 2@ ( addr -- x1 x2 )
336 : S>D ( s -- d , extend signed single precision to double )
343 : D>S ( d -- s ) drop ;
345 : /MOD ( a b -- rem quo , unsigned version, FIXME )
362 cell 8 * 1- rshift or swap
366 : D= ( xd1 xd2 -- flag )
370 : D< ( d1 d2 -- flag )
374 : D> ( d1 d2 -- flag )
378 \ define some useful constants ------------------------------
384 \ Store and Fetch relocatable data addresses. ---------------
385 : IF.USE->REL ( use -- rel , preserve zero )
388 : IF.REL->USE ( rel -- use , preserve zero )
392 : A! ( dictionary_address addr -- )
395 : A@ ( addr -- dictionary_address )
399 : A, ( dictionary_address -- )
403 \ Stack data structure ----------------------------------------
404 \ This is a general purpose stack utility used to implement necessary
405 \ stacks for the compiler or the user. Not real fast.
406 \ These stacks grow up which is different then normal.
407 \ cell 0 - stack pointer, offset from pfa of word
408 \ cell 1 - limit for range checking
409 \ cell 2 - first data location
411 : :STACK ( #cells -- )
412 CREATE 2 cells , ( offset of first data location )
413 dup , ( limit for range checking, not currently used )
414 cells cell+ allot ( allot an extra cell for safety )
417 : >STACK ( n stack -- , push onto stack, postincrement )
418 dup @ 2dup cell+ swap ! ( -- n stack offset )
422 : STACK> ( stack -- n , pop , predecrement )
423 dup @ cell- 2dup swap !
427 : STACK@ ( stack -- n , copy )
431 : STACK.PICK ( index stack -- n , grab Nth from top of stack )
433 swap cells - \ offset for index
436 : STACKP ( stack -- ptr , to next empty location on stack )
440 : 0STACKP ( stack -- , clear stack)
447 \ Define JForth like words.
448 : >US ustack >stack ;
449 : US> ustack stack> ;
450 : US@ ustack stack@ ;
451 : 0USP ustack 0stackp ;
454 \ DO LOOP ------------------------------------------------
457 4 constant leave_flag
460 : DO ( -- , loop-back do_flag jump-from ?do_flag )
463 here >us do_flag >us ( for backward branch )
466 : ?DO ( -- , loop-back do_flag jump-from ?do_flag , on user stack )
468 ( leave address to set for forward branch )
471 here >us do_flag >us ( for backward branch )
472 >us ( for forward branch ) ?do_flag >us
475 : LEAVE ( -- addr leave_flag )
481 : LOOP-FORWARD ( -us- jump-from ?do_flag -- )
489 us> here over - cell+ swap !
498 : LOOP-BACK ( loop-addr do_flag -us- )
505 : LOOP ( -- , loop-back do_flag jump-from ?do_flag )
507 loop-forward loop-back
510 \ : DOTEST 5 0 do 333 . loop 888 . ;
511 \ : ?DOTEST0 0 0 ?do 333 . loop 888 . ;
512 \ : ?DOTEST1 5 0 ?do 333 . loop 888 . ;
514 : +LOOP ( -- , loop-back do_flag jump-from ?do_flag )
516 loop-forward loop-back
519 : UNLOOP ( loop-sys -r- )
520 r> \ save return pointer
525 : RECURSE ( ? -- ? , call the word currently being defined )
526 latest name> compile,
532 : SPACES 512 min 0 max 0 ?DO space LOOP ;
533 : 0SP depth 0 ?do drop loop ;
535 : >NEWLINE ( -- , CR if needed )
542 \ Support for DEFER --------------------
543 : CHECK.DEFER ( xt -- , error if not a deferred word by comparing to type )
549 : >is ( xt -- address_of_vector )
554 : (IS) ( xt_do xt_deferred -- )
558 : IS ( xt <name> -- , act like normal IS )
562 IF [compile] literal compile (is)
567 : (WHAT'S) ( xt -- xt_do )
570 : WHAT'S ( <name> -- xt , what will deferred word call? )
574 IF [compile] literal compile (what's)
579 : /STRING ( addr len n -- addr' len' )
580 over min rot over + -rot -
582 : PLACE ( addr len to -- , move string )
583 3dup 1+ swap cmove c! drop
586 : PARSE-WORD ( char -- addr len )
587 >r source tuck >in @ /string r@ skip over swap r> scan
588 >r over - rot r> dup 0<> + - >in !
590 : PARSE ( char -- addr len )
591 >r source >in @ /string over swap r> scan
592 >r over - dup r> 0<> - >in +!
595 : LWORD ( char -- addr )
596 parse-word here place here \ 00002 , use PARSE-WORD
599 : ASCII ( <char> -- char , state smart )
606 : CHAR ( <char> -- char , interpret mode )
610 : [CHAR] ( <char> -- char , for compile mode )
611 char [compile] literal
614 : $TYPE ( $string -- )
618 : 'word ( -- addr ) here ;
620 : EVEN ( addr -- addr' ) dup 1 and + ;
622 : (C") ( -- $addr , some Forths return addr AND count, OBSOLETE?)
623 r> dup count + aligned >r
625 : (S") ( -- c-addr cnt )
626 r> count 2dup + aligned >r
629 : (.") ( -- , type following string )
630 r> count 2dup + aligned >r type
633 : ", ( adr len -- , place string into dictionary )
634 tuck 'word place 1+ allot align
640 : .( ( <string> -- , type string delimited by parentheses )
644 : ." ( <string> -- , type string )
647 ELSE [char] " parse type
652 : .' ( <string> -- , type string delimited by single quote )
654 IF compile (.") [char] ' parse ",
655 ELSE [char] ' parse type
659 : C" ( <string> -- addr , return string address, ANSI )
662 ELSE [char] " parse pad place pad
666 : S" ( <string> -- , -- addr , return string address, ANSI )
669 ELSE [char] " parse pad place pad count
673 : " ( <string> -- , -- addr , return string address )
676 : P" ( <string> -- , -- addr , return string address )
680 : "" ( <string> -- addr )
686 bl parse-word pad place pad
690 : SLITERAL ( addr cnt -- , compile string )
695 : $APPEND ( addr count $1 -- , append text to $1 )
698 count + ( -- a2 c2 end1 )
701 r> + ( -- a1 totalcount )
706 \ ANSI word to replace [COMPILE] and COMPILE ----------------
707 : POSTPONE ( <name> -- )
711 ." Postpone could not find " count type cr abort
714 IF compile, \ immediate
715 ELSE (compile) \ normal
720 \ -----------------------------------------------------------------
721 \ Auto Initialization
723 \ Kernel finds AUTO.INIT and executes it after loading dictionary.
724 \ ." Begin AUTO.INIT ------" cr
727 \ Kernel finds AUTO.TERM and executes it on bye.
728 \ ." End AUTO.TERM ------" cr
731 \ -------------- INCLUDE ------------------------------------------
732 variable TRACE-INCLUDE
734 : INCLUDE.MARK.START ( c-addr u -- , mark start of include for FILE?)
735 dup 5 + allocate throw >r
742 : INCLUDE.MARK.END ( -- , mark end of include )
746 : INCLUDED ( c-addr u -- )
750 >newline ." Include " 2dup type cr
754 IF ( -- c-addr u bad-fid )
755 drop ." Could not find file " type cr abort
756 ELSE ( -- c-addr u good-fid )
757 -rot include.mark.start
759 include-file \ will also close the file
762 ." Warning: stack depth changed during include!" cr
770 ." include added " here r@ - . ." bytes,"
771 codelimit here - . ." left." cr
776 : $INCLUDE ( $filename -- ) count included ;
778 create INCLUDE-SAVE-NAME 128 allot
779 : INCLUDE ( <fname> -- )
781 dup include-save-name $move \ save for RI
785 : RI ( -- , ReInclude previous file as a convenience )
786 include-save-name $include
789 : INCLUDE? ( <word> <file> -- , load file if word not defined )
791 IF drop bl word drop ( eat word from source )
796 \ desired sizes for dictionary loaded after SAVE-FORTH
797 variable HEADERS-SIZE
802 codelimit codebase - code-size !
803 namelimit namebase - headers-size !
807 : SAVE-FORTH ( $name -- )
809 headers-ptr @ namebase - 65536 + \ NameSize
811 here codebase - 131072 + \ CodeSize
815 ." SAVE-FORTH failed!" cr abort
819 : TURNKEY ( $name entry-token-- )
820 0 \ NameSize = 0, names not saved in turnkey dictionary
821 here codebase - 131072 + \ CodeSize, remember that base is HEX
824 ." TURNKEY failed!" cr abort
828 \ Now that we can load from files, load remainder of dictionary.
831 \ Turn this OFF if you do not want to see the contents of the stack after each entry.
838 : ;;;; ; \ Mark end of this file so FILE? can find things in here.
839 FREEZE \ prevent forgetting below this point
841 .( Dictionary compiled, save in "pforth.dic".) cr
842 \ 300000 headers-size !
844 c" pforth.dic" save-forth