5 : FLAG_IMMEDIATE 64 ;
\r
8 latest dup c@ flag_immediate OR
\r
12 : ( 41 word drop ; immediate
\r
13 ( That was the definition for the comment word. )
\r
14 ( Now we can add comments to what we are doing! )
\r
15 ( Note that we are in decimal numeric input mode. )
\r
17 : \ ( <line> -- , comment out rest of line )
\r
21 \ 1 echo ! \ Uncomment this line to echo Forth code while compiling.
\r
23 \ *********************************************************************
\r
24 \ This is another style of comment that is common in Forth.
\r
25 \ pFORTH - Portable Forth System
\r
26 \ Based on HMSL Forth
\r
29 \ Copyright 1994 3DO, Phil Burk, Larry Polansky, Devid Rosenboom
\r
31 \ The pForth software code is dedicated to the public domain,
\r
32 \ and any third party may reproduce, distribute and modify
\r
33 \ the pForth software code or any derivative works thereof
\r
34 \ without any compensation or license. The pForth software
\r
35 \ code is provided on an "as is" basis without any warranty
\r
36 \ of any kind, including, without limitation, the implied
\r
37 \ warranties of merchantability and fitness for a particular
\r
38 \ purpose and their equivalents under the laws of any jurisdiction.
\r
39 \ *********************************************************************
\r
41 : COUNT dup 1+ swap c@ ;
\r
43 \ Miscellaneous support words
\r
44 : ON ( addr -- , set true )
\r
47 : OFF ( addr -- , set false )
\r
51 : CELL+ ( n -- n+cell ) cell + ;
\r
52 : CELL- ( n -- n+cell ) cell - ;
\r
53 : CELL* ( n -- n*cell ) cells ;
55 : CHAR+ ( n -- n+size_of_char ) 1+ ;
\r
56 : CHARS ( n -- n*size_of_char , don't do anything) ; immediate
\r
58 \ useful stack manipulation words
\r
59 : -ROT ( a b c -- c a b )
\r
62 : 3DUP ( a b c -- a b c a b c )
\r
63 2 pick 2 pick 2 pick
\r
71 : TUCK ( a b -- b a b )
\r
75 : <= ( a b -- f , true if A <= b )
\r
78 : >= ( a b -- f , true if A >= b )
\r
82 : INVERT ( n -- 1'comp )
\r
86 : NOT ( n -- !n , logical negation )
\r
90 : NEGATE ( n -- -n )
\r
94 : DNEGATE ( d -- -d , negate by doing 0-d )
\r
99 \ --------------------------------------------------------------------
\r
105 : DECIMAL 10 base ! ;
\r
108 : BINARY 2 base ! ;
\r
114 : $MOVE ( $src $dst -- )
\r
117 : BETWEEN ( n lo hi -- flag , true if between lo & hi )
\r
121 : [ ( -- , enter interpreter mode )
\r
124 : ] ( -- enter compile mode )
\r
128 : EVEN-UP ( n -- n | n+1 , make even ) dup 1 and + ;
\r
129 : ALIGNED ( addr -- a-addr )
\r
130 [ cell 1- ] literal +
\r
131 [ cell 1- invert ] literal and
\r
133 : ALIGN ( -- , align DP ) dp @ aligned dp ! ;
\r
134 : ALLOT ( nbytes -- , allot space in dictionary ) dp +! ( align ) ;
\r
136 : C, ( c -- ) here c! 1 chars dp +! ;
\r
137 : W, ( w -- ) dp @ even-up dup dp ! w! 2 chars dp +! ;
\r
138 : , ( n -- , lay into dictionary ) align here ! cell allot ;
\r
140 \ Dictionary conversions ------------------------------------------
\r
142 : N>NEXTLINK ( nfa -- nextlink , traverses name field )
\r
143 dup c@ 31 and 1+ + aligned
\r
146 : NAMEBASE ( -- base-of-names )
\r
149 : CODEBASE ( -- base-of-code dictionary )
\r
153 : NAMELIMIT ( -- limit-of-names )
\r
156 : CODELIMIT ( -- limit-of-code, last address in dictionary )
\r
160 : NAMEBASE+ ( rnfa -- nfa , convert relocatable nfa to actual )
\r
164 : >CODE ( xt -- secondary_code_address, not valid for primitives )
\r
168 : CODE> ( secondary_code_address -- xt , not valid for primitives )
\r
172 : N>LINK ( nfa -- lfa )
\r
176 : >BODY ( xt -- pfa )
\r
177 >code body_offset +
\r
180 : BODY> ( pfa -- xt )
\r
181 body_offset - code>
\r
184 \ convert between addresses useable by @, and relocatable addresses.
\r
185 : USE->REL ( useable_addr -- rel_addr )
\r
188 : REL->USE ( rel_addr -- useable_addr )
\r
193 \ : >REL ( adr -- adr ) ; immediate
\r
194 \ : >ABS ( adr -- adr ) ; immediate
\r
196 : X@ ( addr -- xt , fetch execution token from relocatable ) @ ;
\r
197 : X! ( addr -- xt , store execution token as relocatable ) ! ;
\r
199 \ Compiler support ------------------------------------------------
\r
200 : COMPILE, ( xt -- , compile call to xt )
\r
204 ( Compiler support , based on FIG )
\r
205 : [COMPILE] ( <name> -- , compile now even if immediate )
\r
209 : (COMPILE) ( xt -- , postpone compilation of token )
\r
210 [compile] literal ( compile a call to literal )
\r
211 ( store xt of word to be compiled )
\r
213 [ ' compile, ] literal \ compile call to compile,
\r
217 : COMPILE ( <name> -- , save xt and compile later )
\r
222 : :NONAME ( -- xt , begin compilation of headerless secondary )
\r
224 here code> \ convert here to execution token
\r
228 \ Error codes defined in ANSI Exception word set.
\r
229 : ERR_ABORT -1 ; \ general abort
\r
230 : ERR_ABORTQ -2 ; \ for abort"
\r
231 : ERR_EXECUTING -14 ; \ compile time word while not compiling
\r
232 : ERR_PAIRS -22 ; \ mismatch in conditional
\r
233 : ERR_DEFER -258 ; \ not a deferred word
\r
239 \ Conditionals in '83 form -----------------------------------------
\r
240 : CONDITIONAL_KEY ( -- , lazy constant ) 29521 ;
\r
241 : ?CONDITION ( f -- ) conditional_key - err_pairs ?error ;
\r
242 : >MARK ( -- addr ) here 0 , ;
\r
243 : >RESOLVE ( addr -- ) here over - swap ! ;
\r
244 : <MARK ( -- addr ) here ;
\r
245 : <RESOLVE ( addr -- ) here - , ;
\r
247 : ?COMP ( -- , error if not compiling )
\r
248 state @ 0= err_executing ?error
\r
250 : ?PAIRS ( n m -- )
\r
253 \ conditional primitives
\r
254 : IF ( -- f orig ) ?comp compile 0branch conditional_key >mark ; immediate
\r
255 : THEN ( f orig -- ) swap ?condition >resolve ; immediate
\r
256 : BEGIN ( -- f dest ) ?comp conditional_key <mark ; immediate
\r
257 : AGAIN ( f dest -- ) compile branch swap ?condition <resolve ; immediate
\r
258 : UNTIL ( f dest -- ) compile 0branch swap ?condition <resolve ; immediate
\r
259 : AHEAD ( -- f orig ) compile branch conditional_key >mark ; immediate
\r
261 \ conditionals built from primitives
\r
262 : ELSE ( f orig1 -- f orig2 )
\r
263 [compile] AHEAD 2swap [compile] THEN ; immediate
\r
264 : WHILE ( f dest -- f orig f dest ) [compile] if 2swap ; immediate
\r
265 : REPEAT ( -- f orig f dest ) [compile] again [compile] then ; immediate
\r
267 : ['] ( <name> -- xt , define compile time tick )
\r
268 ?comp ' [compile] literal
\r
272 \ compile time: compile create , (does>) then ;
\r
273 \ execution time: create <name>, ',' data, then patch pi to point to @
\r
274 \ : con create , does> @ ;
\r
278 : (DOES>) ( xt -- , modify previous definition to execute code at xt )
\r
279 latest name> >code \ get address of code for new word
\r
280 cell + \ offset to second cell in create word
\r
281 ! \ store execution token of DOES> code in new word
\r
284 : DOES> ( -- , define execution code for CREATE word )
\r
285 0 [compile] literal \ dummy literal to hold xt
\r
286 here cell- \ address of zero in literal
\r
287 compile (does>) \ call (DOES>) from new creation word
\r
288 >r \ move addrz to return stack so ; doesn't see stack garbage
\r
289 [compile] ; \ terminate part of code before does>
\r
291 :noname ( addrz xt )
\r
292 swap ! \ save execution token in literal
\r
295 : VARIABLE ( <name> -- )
\r
296 CREATE 0 , \ IMMEDIATE
\r
297 \ DOES> [compile] aliteral \ %Q This could be optimised
\r
300 : 2VARIABLE ( <name> -c- ) ( -x- addr )
\r
304 : CONSTANT ( n <name> -c- ) ( -x- n )
\r
314 : 2! ( x1 x2 addr -- , store x2 followed by x1 )
\r
315 swap over ! cell+ !
\r
317 : 2@ ( addr -- x1 x2 )
\r
327 : DABS ( d -- |d| )
\r
333 : S>D ( s -- d , extend signed single precision to double )
\r
340 : D>S ( d -- s ) drop ;
\r
342 : /MOD ( a b -- rem quo , unsigned version, FIXME )
\r
346 : MOD ( a b -- rem )
\r
359 cell 8 * 1- rshift or swap
\r
363 \ define some useful constants ------------------------------
\r
364 1 0= constant FALSE
\r
369 \ Store and Fetch relocatable data addresses. ---------------
\r
370 : IF.USE->REL ( use -- rel , preserve zero )
\r
371 dup IF use->rel THEN
\r
373 : IF.REL->USE ( rel -- use , preserve zero )
\r
374 dup IF rel->use THEN
\r
377 : A! ( dictionary_address addr -- )
\r
378 >r if.use->rel r> !
\r
380 : A@ ( addr -- dictionary_address )
\r
384 : A, ( dictionary_address -- )
\r
388 \ Stack data structure ----------------------------------------
\r
389 \ This is a general purpose stack utility used to implement necessary
\r
390 \ stacks for the compiler or the user. Not real fast.
\r
391 \ These stacks grow up which is different then normal.
\r
392 \ cell 0 - stack pointer, offset from pfa of word
\r
393 \ cell 1 - limit for range checking
\r
394 \ cell 2 - first data location
\r
396 : :STACK ( #cells -- )
\r
397 CREATE 2 cells , ( offset of first data location )
\r
398 dup , ( limit for range checking, not currently used )
\r
399 cells cell+ allot ( allot an extra cell for safety )
\r
402 : >STACK ( n stack -- , push onto stack, postincrement )
\r
403 dup @ 2dup cell+ swap ! ( -- n stack offset )
\r
407 : STACK> ( stack -- n , pop , predecrement )
\r
408 dup @ cell- 2dup swap !
\r
412 : STACK@ ( stack -- n , copy )
\r
416 : STACK.PICK ( index stack -- n , grab Nth from top of stack )
\r
418 swap cells - \ offset for index
\r
421 : STACKP ( stack -- ptr , to next empty location on stack )
\r
425 : 0STACKP ( stack -- , clear stack)
\r
432 \ Define JForth like words.
\r
433 : >US ustack >stack ;
\r
434 : US> ustack stack> ;
\r
435 : US@ ustack stack@ ;
\r
436 : 0USP ustack 0stackp ;
\r
439 \ DO LOOP ------------------------------------------------
\r
442 4 constant leave_flag
\r
443 5 constant ?do_flag
\r
445 : DO ( -- , loop-back do_flag jump-from ?do_flag )
\r
448 here >us do_flag >us ( for backward branch )
\r
451 : ?DO ( -- , loop-back do_flag jump-from ?do_flag , on user stack )
\r
453 ( leave address to set for forward branch )
\r
456 here >us do_flag >us ( for backward branch )
\r
457 >us ( for forward branch ) ?do_flag >us
\r
460 : LEAVE ( -- addr leave_flag )
\r
466 : LOOP-FORWARD ( -us- jump-from ?do_flag -- )
\r
474 us> here over - cell+ swap !
\r
483 : LOOP-BACK ( loop-addr do_flag -us- )
\r
490 : LOOP ( -- , loop-back do_flag jump-from ?do_flag )
\r
492 loop-forward loop-back
\r
495 \ : DOTEST 5 0 do 333 . loop 888 . ;
\r
496 \ : ?DOTEST0 0 0 ?do 333 . loop 888 . ;
\r
497 \ : ?DOTEST1 5 0 ?do 333 . loop 888 . ;
\r
499 : +LOOP ( -- , loop-back do_flag jump-from ?do_flag )
\r
501 loop-forward loop-back
\r
504 : UNLOOP ( loop-sys -r- )
\r
505 r> \ save return pointer
\r
510 : RECURSE ( ? -- ? , call the word currently being defined )
\r
511 latest name> compile,
\r
517 : SPACES 512 min 0 max 0 ?DO space LOOP ;
\r
518 : 0SP depth 0 ?do drop loop ;
\r
520 : >NEWLINE ( -- , CR if needed )
\r
527 \ Support for DEFER --------------------
\r
528 : CHECK.DEFER ( xt -- , error if not a deferred word by comparing to type )
\r
534 : >is ( xt -- address_of_vector )
\r
539 : (IS) ( xt_do xt_deferred -- )
\r
543 : IS ( xt <name> -- , act like normal IS )
\r
547 IF [compile] literal compile (is)
\r
552 : (WHAT'S) ( xt -- xt_do )
\r
555 : WHAT'S ( <name> -- xt , what will deferred word call? )
\r
559 IF [compile] literal compile (what's)
\r
564 : /STRING ( addr len n -- addr' len' )
\r
565 over min rot over + -rot -
\r
567 : PLACE ( addr len to -- , move string )
\r
568 3dup 1+ swap cmove c! drop
\r
571 : PARSE-WORD ( char -- addr len )
\r
572 >r source tuck >in @ /string r@ skip over swap r> scan
\r
573 >r over - rot r> dup 0<> + - >in !
\r
575 : PARSE ( char -- addr len )
\r
576 >r source >in @ /string over swap r> scan
\r
577 >r over - dup r> 0<> - >in +!
\r
580 : LWORD ( char -- addr )
\r
581 parse-word here place here \ 00002 , use PARSE-WORD
\r
584 : ASCII ( <char> -- char , state smart )
\r
587 IF [compile] literal
\r
591 : CHAR ( <char> -- char , interpret mode )
\r
595 : [CHAR] ( <char> -- char , for compile mode )
\r
596 char [compile] literal
\r
599 : $TYPE ( $string -- )
\r
603 : 'word ( -- addr ) here ;
\r
605 : EVEN ( addr -- addr' ) dup 1 and + ;
\r
607 : (C") ( -- $addr , some Forths return addr AND count, OBSOLETE?)
\r
608 r> dup count + aligned >r
\r
610 : (S") ( -- c-addr cnt )
\r
611 r> count 2dup + aligned >r
\r
614 : (.") ( -- , type following string )
\r
615 r> count 2dup + aligned >r type
\r
618 : ", ( adr len -- , place string into dictionary )
\r
619 tuck 'word place 1+ allot align
\r
625 : .( ( <string> -- , type string delimited by parentheses )
\r
626 [CHAR] ) PARSE TYPE
\r
629 : ." ( <string> -- , type string )
\r
632 ELSE [char] " parse type
\r
637 : .' ( <string> -- , type string delimited by single quote )
\r
639 IF compile (.") [char] ' parse ",
\r
640 ELSE [char] ' parse type
\r
644 : C" ( <string> -- addr , return string address, ANSI )
\r
647 ELSE [char] " parse pad place pad
\r
651 : S" ( <string> -- , -- addr , return string address, ANSI )
\r
654 ELSE [char] " parse pad place pad count
\r
658 : " ( <string> -- , -- addr , return string address )
\r
661 : P" ( <string> -- , -- addr , return string address )
\r
665 : "" ( <string> -- addr )
\r
671 bl parse-word pad place pad
\r
675 : SLITERAL ( addr cnt -- , compile string )
\r
680 : $APPEND ( addr count $1 -- , append text to $1 )
\r
683 count + ( -- a2 c2 end1 )
\r
685 r> dup c@ ( a1 c1 )
\r
686 r> + ( -- a1 totalcount )
\r
691 \ ANSI word to replace [COMPILE] and COMPILE ----------------
\r
692 : POSTPONE ( <name> -- )
\r
696 ." Postpone could not find " count type cr abort
\r
699 IF compile, \ immediate
\r
700 ELSE (compile) \ normal
\r
705 \ -----------------------------------------------------------------
\r
706 \ Auto Initialization
\r
708 \ Kernel finds AUTO.INIT and executes it after loading dictionary.
\r
709 \ ." Begin AUTO.INIT ------" cr
\r
712 \ Kernel finds AUTO.TERM and executes it on bye.
\r
713 \ ." End AUTO.TERM ------" cr
\r
716 \ -------------- INCLUDE ------------------------------------------
\r
717 variable TRACE-INCLUDE
\r
719 : INCLUDE.MARK.START ( $filename -- , mark start of include for FILE?)
\r
725 : INCLUDE.MARK.END ( -- , mark end of include )
\r
726 " ;;;;" ['] noop (:)
\r
729 : $INCLUDE ( $filename -- )
\r
733 >newline ." Include " dup count type cr
\r
737 count r/o open-file
\r
738 IF ( -- $filename bad-fid )
\r
739 drop ." Could not find file " $type cr abort
\r
740 ELSE ( -- $filename good-fid )
\r
741 swap include.mark.start
\r
743 include-file \ will also close the file
\r
746 ." Warning: stack depth changed during include!" cr
\r
754 ." include added " here r@ - . ." bytes,"
\r
755 codelimit here - . ." left." cr
\r
760 create INCLUDE-SAVE-NAME 128 allot
\r
761 : INCLUDE ( <fname> -- )
\r
763 dup include-save-name $move \ save for RI
\r
767 : RI ( -- , ReInclude previous file as a convenience )
\r
768 include-save-name $include
\r
771 : INCLUDE? ( <word> <file> -- , load file if word not defined )
\r
773 IF drop bl word drop ( eat word from source )
\r
778 \ desired sizes for dictionary loaded after SAVE-FORTH
\r
779 variable HEADERS-SIZE
\r
784 codelimit codebase - code-size !
\r
785 namelimit namebase - headers-size !
\r
789 : SAVE-FORTH ( $name -- )
\r
791 headers-ptr @ namebase - 65536 + \ NameSize
\r
793 here codebase - 131072 + \ CodeSize
\r
797 ." SAVE-FORTH failed!" cr abort
\r
801 : TURNKEY ( $name entry-token-- )
\r
802 0 \ NameSize = 0, names not saved in turnkey dictionary
\r
803 here codebase - 131072 + \ CodeSize, remember that base is HEX
\r
806 ." TURNKEY failed!" cr abort
\r
810 \ Now that we can load from files, load remainder of dictionary.
\r
813 \ Turn this OFF if you do not want to see the contents of the stack after each entry.
\r
816 include loadp4th.fth
\r
820 : ;;;; ; \ Mark end of this file so FILE? can find things in here.
\r
821 FREEZE \ prevent forgetting below this point
\r
823 .( Dictionary compiled, save in "pforth.dic".) cr
\r
824 c" pforth.dic" save-forth
\r