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 \ The pForth software code is dedicated to the public domain,
32 \ and any third party may reproduce, distribute and modify
33 \ the pForth software code or any derivative works thereof
34 \ without any compensation or license. The pForth software
35 \ code is provided on an "as is" basis without any warranty
36 \ of any kind, including, without limitation, the implied
37 \ warranties of merchantability and fitness for a particular
38 \ purpose and their equivalents under the laws of any jurisdiction.
39 \ *********************************************************************
41 : COUNT dup 1+ swap c@ ;
43 \ Miscellaneous support words
44 : ON ( addr -- , set true )
47 : OFF ( addr -- , set false )
51 : CELL+ ( n -- n+cell ) cell + ;
52 : CELL- ( n -- n+cell ) cell - ;
53 : CELL* ( n -- n*cell ) cells ;
55 : CHAR+ ( n -- n+size_of_char ) 1+ ;
56 : CHARS ( n -- n*size_of_char , don't do anything) ; immediate
58 \ useful stack manipulation words
59 : -ROT ( a b c -- c a b )
62 : 3DUP ( a b c -- a b c a b c )
71 : TUCK ( a b -- b a b )
75 : <= ( a b -- f , true if A <= b )
78 : >= ( a b -- f , true if A >= b )
82 : INVERT ( n -- 1'comp )
86 : NOT ( n -- !n , logical negation )
94 : DNEGATE ( d -- -d , negate by doing 0-d )
99 \ --------------------------------------------------------------------
105 : DECIMAL 10 base ! ;
114 : $MOVE ( $src $dst -- )
117 : BETWEEN ( n lo hi -- flag , true if between lo & hi )
121 : [ ( -- , enter interpreter mode )
124 : ] ( -- enter compile mode )
128 : EVEN-UP ( n -- n | n+1 , make even ) dup 1 and + ;
129 : ALIGNED ( addr -- a-addr )
130 [ cell 1- ] literal +
131 [ cell 1- invert ] literal and
133 : ALIGN ( -- , align DP ) dp @ aligned dp ! ;
134 : ALLOT ( nbytes -- , allot space in dictionary ) dp +! ( align ) ;
136 : C, ( c -- ) here c! 1 chars dp +! ;
137 : W, ( w -- ) dp @ even-up dup dp ! w! 2 chars dp +! ;
138 : , ( n -- , lay into dictionary ) align here ! cell allot ;
140 \ Dictionary conversions ------------------------------------------
142 : N>NEXTLINK ( nfa -- nextlink , traverses name field )
143 dup c@ 31 and 1+ + aligned
146 : NAMEBASE ( -- base-of-names )
149 : CODEBASE ( -- base-of-code dictionary )
153 : NAMELIMIT ( -- limit-of-names )
156 : CODELIMIT ( -- limit-of-code, last address in dictionary )
160 : NAMEBASE+ ( rnfa -- nfa , convert relocatable nfa to actual )
164 : >CODE ( xt -- secondary_code_address, not valid for primitives )
168 : CODE> ( secondary_code_address -- xt , not valid for primitives )
172 : N>LINK ( nfa -- lfa )
176 : >BODY ( xt -- pfa )
180 : BODY> ( pfa -- xt )
184 \ convert between addresses useable by @, and relocatable addresses.
185 : USE->REL ( useable_addr -- rel_addr )
188 : REL->USE ( rel_addr -- useable_addr )
193 \ : >REL ( adr -- adr ) ; immediate
194 \ : >ABS ( adr -- adr ) ; immediate
196 : X@ ( addr -- xt , fetch execution token from relocatable ) @ ;
197 : X! ( addr -- xt , store execution token as relocatable ) ! ;
199 \ Compiler support ------------------------------------------------
200 : COMPILE, ( xt -- , compile call to xt )
204 ( Compiler support , based on FIG )
205 : [COMPILE] ( <name> -- , compile now even if immediate )
209 : (COMPILE) ( xt -- , postpone compilation of token )
210 [compile] literal ( compile a call to literal )
211 ( store xt of word to be compiled )
213 [ ' compile, ] literal \ compile call to compile,
217 : COMPILE ( <name> -- , save xt and compile later )
222 : :NONAME ( -- xt , begin compilation of headerless secondary )
224 here code> \ convert here to execution token
228 \ Error codes defined in ANSI Exception word set.
229 : ERR_ABORT -1 ; \ general abort
230 : ERR_ABORTQ -2 ; \ for abort"
231 : ERR_EXECUTING -14 ; \ compile time word while not compiling
232 : ERR_PAIRS -22 ; \ mismatch in conditional
233 : ERR_DEFER -258 ; \ not a deferred word
239 \ Conditionals in '83 form -----------------------------------------
240 : CONDITIONAL_KEY ( -- , lazy constant ) 29521 ;
241 : ?CONDITION ( f -- ) conditional_key - err_pairs ?error ;
242 : >MARK ( -- addr ) here 0 , ;
243 : >RESOLVE ( addr -- ) here over - swap ! ;
244 : <MARK ( -- addr ) here ;
245 : <RESOLVE ( addr -- ) here - , ;
247 : ?COMP ( -- , error if not compiling )
248 state @ 0= err_executing ?error
253 \ conditional primitives
254 : IF ( -- f orig ) ?comp compile 0branch conditional_key >mark ; immediate
255 : THEN ( f orig -- ) swap ?condition >resolve ; immediate
256 : BEGIN ( -- f dest ) ?comp conditional_key <mark ; immediate
257 : AGAIN ( f dest -- ) compile branch swap ?condition <resolve ; immediate
258 : UNTIL ( f dest -- ) compile 0branch swap ?condition <resolve ; immediate
259 : AHEAD ( -- f orig ) compile branch conditional_key >mark ; immediate
261 \ conditionals built from primitives
262 : ELSE ( f orig1 -- f orig2 )
263 [compile] AHEAD 2swap [compile] THEN ; immediate
264 : WHILE ( f dest -- f orig f dest ) [compile] if 2swap ; immediate
265 : REPEAT ( -- f orig f dest ) [compile] again [compile] then ; immediate
267 : ['] ( <name> -- xt , define compile time tick )
268 ?comp ' [compile] literal
272 \ compile time: compile create , (does>) then ;
273 \ execution time: create <name>, ',' data, then patch pi to point to @
274 \ : con create , does> @ ;
278 : (DOES>) ( xt -- , modify previous definition to execute code at xt )
279 latest name> >code \ get address of code for new word
280 cell + \ offset to second cell in create word
281 ! \ store execution token of DOES> code in new word
284 : DOES> ( -- , define execution code for CREATE word )
285 0 [compile] literal \ dummy literal to hold xt
286 here cell- \ address of zero in literal
287 compile (does>) \ call (DOES>) from new creation word
288 >r \ move addrz to return stack so ; doesn't see stack garbage
289 [compile] ; \ terminate part of code before does>
292 swap ! \ save execution token in literal
295 : VARIABLE ( <name> -- )
296 CREATE 0 , \ IMMEDIATE
297 \ DOES> [compile] aliteral \ %Q This could be optimised
300 : 2VARIABLE ( <name> -c- ) ( -x- addr )
304 : CONSTANT ( n <name> -c- ) ( -x- n )
314 : 2! ( x1 x2 addr -- , store x2 followed by x1 )
317 : 2@ ( addr -- x1 x2 )
333 : S>D ( s -- d , extend signed single precision to double )
340 : D>S ( d -- s ) drop ;
342 : /MOD ( a b -- rem quo , unsigned version, FIXME )
359 cell 8 * 1- rshift or swap
363 : D= ( xd1 xd2 -- flag )
367 \ define some useful constants ------------------------------
373 \ Store and Fetch relocatable data addresses. ---------------
374 : IF.USE->REL ( use -- rel , preserve zero )
377 : IF.REL->USE ( rel -- use , preserve zero )
381 : A! ( dictionary_address addr -- )
384 : A@ ( addr -- dictionary_address )
388 : A, ( dictionary_address -- )
392 \ Stack data structure ----------------------------------------
393 \ This is a general purpose stack utility used to implement necessary
394 \ stacks for the compiler or the user. Not real fast.
395 \ These stacks grow up which is different then normal.
396 \ cell 0 - stack pointer, offset from pfa of word
397 \ cell 1 - limit for range checking
398 \ cell 2 - first data location
400 : :STACK ( #cells -- )
401 CREATE 2 cells , ( offset of first data location )
402 dup , ( limit for range checking, not currently used )
403 cells cell+ allot ( allot an extra cell for safety )
406 : >STACK ( n stack -- , push onto stack, postincrement )
407 dup @ 2dup cell+ swap ! ( -- n stack offset )
411 : STACK> ( stack -- n , pop , predecrement )
412 dup @ cell- 2dup swap !
416 : STACK@ ( stack -- n , copy )
420 : STACK.PICK ( index stack -- n , grab Nth from top of stack )
422 swap cells - \ offset for index
425 : STACKP ( stack -- ptr , to next empty location on stack )
429 : 0STACKP ( stack -- , clear stack)
436 \ Define JForth like words.
437 : >US ustack >stack ;
438 : US> ustack stack> ;
439 : US@ ustack stack@ ;
440 : 0USP ustack 0stackp ;
443 \ DO LOOP ------------------------------------------------
446 4 constant leave_flag
449 : DO ( -- , loop-back do_flag jump-from ?do_flag )
452 here >us do_flag >us ( for backward branch )
455 : ?DO ( -- , loop-back do_flag jump-from ?do_flag , on user stack )
457 ( leave address to set for forward branch )
460 here >us do_flag >us ( for backward branch )
461 >us ( for forward branch ) ?do_flag >us
464 : LEAVE ( -- addr leave_flag )
470 : LOOP-FORWARD ( -us- jump-from ?do_flag -- )
478 us> here over - cell+ swap !
487 : LOOP-BACK ( loop-addr do_flag -us- )
494 : LOOP ( -- , loop-back do_flag jump-from ?do_flag )
496 loop-forward loop-back
499 \ : DOTEST 5 0 do 333 . loop 888 . ;
500 \ : ?DOTEST0 0 0 ?do 333 . loop 888 . ;
501 \ : ?DOTEST1 5 0 ?do 333 . loop 888 . ;
503 : +LOOP ( -- , loop-back do_flag jump-from ?do_flag )
505 loop-forward loop-back
508 : UNLOOP ( loop-sys -r- )
509 r> \ save return pointer
514 : RECURSE ( ? -- ? , call the word currently being defined )
515 latest name> compile,
521 : SPACES 512 min 0 max 0 ?DO space LOOP ;
522 : 0SP depth 0 ?do drop loop ;
524 : >NEWLINE ( -- , CR if needed )
531 \ Support for DEFER --------------------
532 : CHECK.DEFER ( xt -- , error if not a deferred word by comparing to type )
538 : >is ( xt -- address_of_vector )
543 : (IS) ( xt_do xt_deferred -- )
547 : IS ( xt <name> -- , act like normal IS )
551 IF [compile] literal compile (is)
556 : (WHAT'S) ( xt -- xt_do )
559 : WHAT'S ( <name> -- xt , what will deferred word call? )
563 IF [compile] literal compile (what's)
568 : /STRING ( addr len n -- addr' len' )
569 over min rot over + -rot -
571 : PLACE ( addr len to -- , move string )
572 3dup 1+ swap cmove c! drop
575 : PARSE-WORD ( char -- addr len )
576 >r source tuck >in @ /string r@ skip over swap r> scan
577 >r over - rot r> dup 0<> + - >in !
579 : PARSE ( char -- addr len )
580 >r source >in @ /string over swap r> scan
581 >r over - dup r> 0<> - >in +!
584 : LWORD ( char -- addr )
585 parse-word here place here \ 00002 , use PARSE-WORD
588 : ASCII ( <char> -- char , state smart )
595 : CHAR ( <char> -- char , interpret mode )
599 : [CHAR] ( <char> -- char , for compile mode )
600 char [compile] literal
603 : $TYPE ( $string -- )
607 : 'word ( -- addr ) here ;
609 : EVEN ( addr -- addr' ) dup 1 and + ;
611 : (C") ( -- $addr , some Forths return addr AND count, OBSOLETE?)
612 r> dup count + aligned >r
614 : (S") ( -- c-addr cnt )
615 r> count 2dup + aligned >r
618 : (.") ( -- , type following string )
619 r> count 2dup + aligned >r type
622 : ", ( adr len -- , place string into dictionary )
623 tuck 'word place 1+ allot align
629 : .( ( <string> -- , type string delimited by parentheses )
633 : ." ( <string> -- , type string )
636 ELSE [char] " parse type
641 : .' ( <string> -- , type string delimited by single quote )
643 IF compile (.") [char] ' parse ",
644 ELSE [char] ' parse type
648 : C" ( <string> -- addr , return string address, ANSI )
651 ELSE [char] " parse pad place pad
655 : S" ( <string> -- , -- addr , return string address, ANSI )
658 ELSE [char] " parse pad place pad count
662 : " ( <string> -- , -- addr , return string address )
665 : P" ( <string> -- , -- addr , return string address )
669 : "" ( <string> -- addr )
675 bl parse-word pad place pad
679 : SLITERAL ( addr cnt -- , compile string )
684 : $APPEND ( addr count $1 -- , append text to $1 )
687 count + ( -- a2 c2 end1 )
690 r> + ( -- a1 totalcount )
695 \ ANSI word to replace [COMPILE] and COMPILE ----------------
696 : POSTPONE ( <name> -- )
700 ." Postpone could not find " count type cr abort
703 IF compile, \ immediate
704 ELSE (compile) \ normal
709 \ -----------------------------------------------------------------
710 \ Auto Initialization
712 \ Kernel finds AUTO.INIT and executes it after loading dictionary.
713 \ ." Begin AUTO.INIT ------" cr
716 \ Kernel finds AUTO.TERM and executes it on bye.
717 \ ." End AUTO.TERM ------" cr
720 \ -------------- INCLUDE ------------------------------------------
721 variable TRACE-INCLUDE
723 : INCLUDE.MARK.START ( c-addr u -- , mark start of include for FILE?)
724 dup 5 + allocate throw >r
731 : INCLUDE.MARK.END ( -- , mark end of include )
735 : INCLUDED ( c-addr u -- )
739 >newline ." Include " 2dup type cr
743 IF ( -- c-addr u bad-fid )
744 drop ." Could not find file " type cr abort
745 ELSE ( -- c-addr u good-fid )
746 -rot include.mark.start
748 include-file \ will also close the file
751 ." Warning: stack depth changed during include!" cr
759 ." include added " here r@ - . ." bytes,"
760 codelimit here - . ." left." cr
765 : $INCLUDE ( $filename -- ) count included ;
767 create INCLUDE-SAVE-NAME 128 allot
768 : INCLUDE ( <fname> -- )
770 dup include-save-name $move \ save for RI
774 : RI ( -- , ReInclude previous file as a convenience )
775 include-save-name $include
778 : INCLUDE? ( <word> <file> -- , load file if word not defined )
780 IF drop bl word drop ( eat word from source )
785 \ desired sizes for dictionary loaded after SAVE-FORTH
786 variable HEADERS-SIZE
791 codelimit codebase - code-size !
792 namelimit namebase - headers-size !
796 : SAVE-FORTH ( $name -- )
798 headers-ptr @ namebase - 65536 + \ NameSize
800 here codebase - 131072 + \ CodeSize
804 ." SAVE-FORTH failed!" cr abort
808 : TURNKEY ( $name entry-token-- )
809 0 \ NameSize = 0, names not saved in turnkey dictionary
810 here codebase - 131072 + \ CodeSize, remember that base is HEX
813 ." TURNKEY failed!" cr abort
817 \ Now that we can load from files, load remainder of dictionary.
820 \ Turn this OFF if you do not want to see the contents of the stack after each entry.
827 : ;;;; ; \ Mark end of this file so FILE? can find things in here.
828 FREEZE \ prevent forgetting below this point
830 .( Dictionary compiled, save in "pforth.dic".) cr
831 c" pforth.dic" save-forth