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_EXECUTING -14 ; \ compile time word while not compiling
\r
231 : ERR_PAIRS -22 ; \ mismatch in conditional
\r
232 : ERR_DEFER -258 ; \ not a deferred word
\r
238 \ Conditionals in '83 form -----------------------------------------
\r
239 : CONDITIONAL_KEY ( -- , lazy constant ) 29521 ;
\r
240 : ?CONDITION ( f -- ) conditional_key - err_pairs ?error ;
\r
241 : >MARK ( -- addr ) here 0 , ;
\r
242 : >RESOLVE ( addr -- ) here over - swap ! ;
\r
243 : <MARK ( -- addr ) here ;
\r
244 : <RESOLVE ( addr -- ) here - , ;
\r
246 : ?COMP ( -- , error if not compiling )
\r
247 state @ 0= err_executing ?error
\r
249 : ?PAIRS ( n m -- )
\r
252 \ conditional primitives
\r
253 : IF ( -- f orig ) ?comp compile 0branch conditional_key >mark ; immediate
\r
254 : THEN ( f orig -- ) swap ?condition >resolve ; immediate
\r
255 : BEGIN ( -- f dest ) ?comp conditional_key <mark ; immediate
\r
256 : AGAIN ( f dest -- ) compile branch swap ?condition <resolve ; immediate
\r
257 : UNTIL ( f dest -- ) compile 0branch swap ?condition <resolve ; immediate
\r
258 : AHEAD ( -- f orig ) compile branch conditional_key >mark ; immediate
\r
260 \ conditionals built from primitives
\r
261 : ELSE ( f orig1 -- f orig2 )
\r
262 [compile] AHEAD 2swap [compile] THEN ; immediate
\r
263 : WHILE ( f dest -- f orig f dest ) [compile] if 2swap ; immediate
\r
264 : REPEAT ( -- f orig f dest ) [compile] again [compile] then ; immediate
\r
266 : ['] ( <name> -- xt , define compile time tick )
\r
267 ?comp ' [compile] literal
\r
271 \ compile time: compile create , (does>) then ;
\r
272 \ execution time: create <name>, ',' data, then patch pi to point to @
\r
273 \ : con create , does> @ ;
\r
277 : (DOES>) ( xt -- , modify previous definition to execute code at xt )
\r
278 latest name> >code \ get address of code for new word
\r
279 cell + \ offset to second cell in create word
\r
280 ! \ store execution token of DOES> code in new word
\r
283 : DOES> ( -- , define execution code for CREATE word )
\r
284 0 [compile] literal \ dummy literal to hold xt
\r
285 here cell- \ address of zero in literal
\r
286 compile (does>) \ call (DOES>) from new creation word
\r
287 >r \ move addrz to return stack so ; doesn't see stack garbage
\r
288 [compile] ; \ terminate part of code before does>
\r
290 :noname ( addrz xt )
\r
291 swap ! \ save execution token in literal
\r
294 : VARIABLE ( <name> -- )
\r
295 CREATE 0 , \ IMMEDIATE
\r
296 \ DOES> [compile] aliteral \ %Q This could be optimised
\r
299 : 2VARIABLE ( <name> -c- ) ( -x- addr )
\r
303 : CONSTANT ( n <name> -c- ) ( -x- n )
\r
313 : 2! ( x1 x2 addr -- , store x2 followed by x1 )
\r
314 swap over ! cell+ !
\r
316 : 2@ ( addr -- x1 x2 )
\r
326 : DABS ( d -- |d| )
\r
332 : S>D ( s -- d , extend signed single precision to double )
\r
339 : D>S ( d -- s ) drop ;
\r
341 : /MOD ( a b -- rem quo , unsigned version, FIXME )
\r
345 : MOD ( a b -- rem )
\r
358 cell 8 * 1- rshift or swap
\r
362 \ define some useful constants ------------------------------
\r
363 1 0= constant FALSE
\r
368 \ Store and Fetch relocatable data addresses. ---------------
\r
369 : IF.USE->REL ( use -- rel , preserve zero )
\r
370 dup IF use->rel THEN
\r
372 : IF.REL->USE ( rel -- use , preserve zero )
\r
373 dup IF rel->use THEN
\r
376 : A! ( dictionary_address addr -- )
\r
377 >r if.use->rel r> !
\r
379 : A@ ( addr -- dictionary_address )
\r
383 : A, ( dictionary_address -- )
\r
387 \ Stack data structure ----------------------------------------
\r
388 \ This is a general purpose stack utility used to implement necessary
\r
389 \ stacks for the compiler or the user. Not real fast.
\r
390 \ These stacks grow up which is different then normal.
\r
391 \ cell 0 - stack pointer, offset from pfa of word
\r
392 \ cell 1 - limit for range checking
\r
393 \ cell 2 - first data location
\r
395 : :STACK ( #cells -- )
\r
396 CREATE 2 cells , ( offset of first data location )
\r
397 dup , ( limit for range checking, not currently used )
\r
398 cells cell+ allot ( allot an extra cell for safety )
\r
401 : >STACK ( n stack -- , push onto stack, postincrement )
\r
402 dup @ 2dup cell+ swap ! ( -- n stack offset )
\r
406 : STACK> ( stack -- n , pop , predecrement )
\r
407 dup @ cell- 2dup swap !
\r
411 : STACK@ ( stack -- n , copy )
\r
415 : STACK.PICK ( index stack -- n , grab Nth from top of stack )
\r
417 swap cells - \ offset for index
\r
420 : STACKP ( stack -- ptr , to next empty location on stack )
\r
424 : 0STACKP ( stack -- , clear stack)
\r
431 \ Define JForth like words.
\r
432 : >US ustack >stack ;
\r
433 : US> ustack stack> ;
\r
434 : US@ ustack stack@ ;
\r
435 : 0USP ustack 0stackp ;
\r
438 \ DO LOOP ------------------------------------------------
\r
441 4 constant leave_flag
\r
442 5 constant ?do_flag
\r
444 : DO ( -- , loop-back do_flag jump-from ?do_flag )
\r
447 here >us do_flag >us ( for backward branch )
\r
450 : ?DO ( -- , loop-back do_flag jump-from ?do_flag , on user stack )
\r
452 ( leave address to set for forward branch )
\r
455 here >us do_flag >us ( for backward branch )
\r
456 >us ( for forward branch ) ?do_flag >us
\r
459 : LEAVE ( -- addr leave_flag )
\r
465 : LOOP-FORWARD ( -us- jump-from ?do_flag -- )
\r
473 us> here over - cell+ swap !
\r
482 : LOOP-BACK ( loop-addr do_flag -us- )
\r
489 : LOOP ( -- , loop-back do_flag jump-from ?do_flag )
\r
491 loop-forward loop-back
\r
494 \ : DOTEST 5 0 do 333 . loop 888 . ;
\r
495 \ : ?DOTEST0 0 0 ?do 333 . loop 888 . ;
\r
496 \ : ?DOTEST1 5 0 ?do 333 . loop 888 . ;
\r
498 : +LOOP ( -- , loop-back do_flag jump-from ?do_flag )
\r
500 loop-forward loop-back
\r
503 : UNLOOP ( loop-sys -r- )
\r
504 r> \ save return pointer
\r
509 : RECURSE ( ? -- ? , call the word currently being defined )
\r
510 latest name> compile,
\r
516 : SPACES 512 min 0 max 0 ?DO space LOOP ;
\r
517 : 0SP depth 0 ?do drop loop ;
\r
519 : >NEWLINE ( -- , CR if needed )
\r
526 \ Support for DEFER --------------------
\r
527 : CHECK.DEFER ( xt -- , error if not a deferred word by comparing to type )
\r
533 : >is ( xt -- address_of_vector )
\r
538 : (IS) ( xt_do xt_deferred -- )
\r
542 : IS ( xt <name> -- , act like normal IS )
\r
546 IF [compile] literal compile (is)
\r
551 : (WHAT'S) ( xt -- xt_do )
\r
554 : WHAT'S ( <name> -- xt , what will deferred word call? )
\r
558 IF [compile] literal compile (what's)
\r
563 : /STRING ( addr len n -- addr' len' )
\r
564 over min rot over + -rot -
\r
566 : PLACE ( addr len to -- , move string )
\r
567 3dup 1+ swap cmove c! drop
\r
570 : PARSE-WORD ( char -- addr len )
\r
571 >r source tuck >in @ /string r@ skip over swap r> scan
\r
572 >r over - rot r> dup 0<> + - >in !
\r
574 : PARSE ( char -- addr len )
\r
575 >r source >in @ /string over swap r> scan
\r
576 >r over - dup r> 0<> - >in +!
\r
579 : LWORD ( char -- addr )
\r
580 parse-word here place here \ 00002 , use PARSE-WORD
\r
583 : ASCII ( <char> -- char , state smart )
\r
586 IF [compile] literal
\r
590 : CHAR ( <char> -- char , interpret mode )
\r
594 : [CHAR] ( <char> -- char , for compile mode )
\r
595 char [compile] literal
\r
598 : $TYPE ( $string -- )
\r
602 : 'word ( -- addr ) here ;
\r
604 : EVEN ( addr -- addr' ) dup 1 and + ;
\r
606 : (C") ( -- $addr , some Forths return addr AND count, OBSOLETE?)
\r
607 r> dup count + aligned >r
\r
609 : (S") ( -- c-addr cnt )
\r
610 r> count 2dup + aligned >r
\r
613 : (.") ( -- , type following string )
\r
614 r> count 2dup + aligned >r type
\r
617 : ", ( adr len -- , place string into dictionary )
\r
618 tuck 'word place 1+ allot align
\r
624 : .( ( <string> -- , type string delimited by parentheses )
\r
625 [CHAR] ) PARSE TYPE
\r
628 : ." ( <string> -- , type string )
\r
631 ELSE [char] " parse type
\r
636 : .' ( <string> -- , type string delimited by single quote )
\r
638 IF compile (.") [char] ' parse ",
\r
639 ELSE [char] ' parse type
\r
643 : C" ( <string> -- addr , return string address, ANSI )
\r
646 ELSE [char] " parse pad place pad
\r
650 : S" ( <string> -- , -- addr , return string address, ANSI )
\r
653 ELSE [char] " parse pad place pad count
\r
657 : " ( <string> -- , -- addr , return string address )
\r
660 : P" ( <string> -- , -- addr , return string address )
\r
664 : "" ( <string> -- addr )
\r
670 bl parse-word pad place pad
\r
674 : SLITERAL ( addr cnt -- , compile string )
\r
679 : $APPEND ( addr count $1 -- , append text to $1 )
\r
682 count + ( -- a2 c2 end1 )
\r
684 r> dup c@ ( a1 c1 )
\r
685 r> + ( -- a1 totalcount )
\r
690 \ ANSI word to replace [COMPILE] and COMPILE ----------------
\r
691 : POSTPONE ( <name> -- )
\r
695 ." Postpone could not find " count type cr abort
\r
698 IF compile, \ immediate
\r
699 ELSE (compile) \ normal
\r
704 \ -----------------------------------------------------------------
\r
705 \ Auto Initialization
\r
707 \ Kernel finds AUTO.INIT and executes it after loading dictionary.
\r
708 \ ." Begin AUTO.INIT ------" cr
\r
711 \ Kernel finds AUTO.TERM and executes it on bye.
\r
712 \ ." End AUTO.TERM ------" cr
\r
715 \ -------------- INCLUDE ------------------------------------------
\r
716 variable TRACE-INCLUDE
\r
718 : INCLUDE.MARK.START ( $filename -- , mark start of include for FILE?)
\r
724 : INCLUDE.MARK.END ( -- , mark end of include )
\r
725 " ;;;;" ['] noop (:)
\r
728 : $INCLUDE ( $filename -- )
\r
732 >newline ." Include " dup count type cr
\r
736 count r/o open-file
\r
737 IF ( -- $filename bad-fid )
\r
738 drop ." Could not find file " $type cr abort
\r
739 ELSE ( -- $filename good-fid )
\r
740 swap include.mark.start
\r
742 include-file \ will also close the file
\r
745 ." Warning: stack depth changed during include!" cr
\r
753 ." include added " here r@ - . ." bytes,"
\r
754 codelimit here - . ." left." cr
\r
759 create INCLUDE-SAVE-NAME 128 allot
\r
760 : INCLUDE ( <fname> -- )
\r
762 dup include-save-name $move \ save for RI
\r
766 : RI ( -- , ReInclude previous file as a convenience )
\r
767 include-save-name $include
\r
770 : INCLUDE? ( <word> <file> -- , load file if word not defined )
\r
772 IF drop bl word drop ( eat word from source )
\r
777 \ desired sizes for dictionary loaded after SAVE-FORTH
\r
778 variable HEADERS-SIZE
\r
783 codelimit codebase - code-size !
\r
784 namelimit namebase - headers-size !
\r
788 : SAVE-FORTH ( $name -- )
\r
790 headers-ptr @ namebase - 65536 + \ NameSize
\r
792 here codebase - 131072 + \ CodeSize
\r
796 ." SAVE-FORTH failed!" cr abort
\r
800 : TURNKEY ( $name entry-token-- )
\r
801 0 \ NameSize = 0, names not saved in turnkey dictionary
\r
802 here codebase - 131072 + \ CodeSize, remember that base is HEX
\r
805 ." TURNKEY failed!" cr abort
\r
809 \ Now that we can load from files, load remainder of dictionary.
\r
814 include loadp4th.fth
\r
818 : ;;;; ; \ Mark end of this file so FILE? can find things in here.
\r
819 FREEZE \ prevent forgetting below this point
\r
821 .( Dictionary compiled, save in "pforth.dic".) cr
\r
822 c" pforth.dic" save-forth
\r
824 \ Save the dictionary in "pfdicdat.h" file so pForth can be compiled for standalone mode.
\r