Annotation of gforth/kernel/toolsext.fs, revision 1.30

1.30    ! anton       1: \ Copyright (C) 1995,1998,2000,2003,2005,2007,2009,2010,2012 Free Software Foundation, Inc.
1.1       anton       2: 
                      3: \ This file is part of Gforth.
                      4: 
                      5: \ Gforth is free software; you can redistribute it and/or
                      6: \ modify it under the terms of the GNU General Public License
1.22      anton       7: \ as published by the Free Software Foundation, either version 3
1.1       anton       8: \ of the License, or (at your option) any later version.
                      9: 
                     10: \ This program is distributed in the hope that it will be useful,
                     11: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     12: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     13: \ GNU General Public License for more details.
                     14: 
                     15: \ You should have received a copy of the GNU General Public License
1.22      anton      16: \ along with this program. If not, see http://www.gnu.org/licenses/.
1.1       anton      17: 
1.3       jwilke     18: Warnings off
1.1       anton      19: 
                     20: Variable countif
                     21: 
                     22: : dummy ;  immediate
                     23: : >exec  >r ; restrict ( :-)
1.10      anton      24: : scanIF   f83find  dup 0=  IF  drop ['] dummy >head-noprim  THEN  ;
1.1       anton      25: 
1.2       jwilke     26: Create [struct]-search    ' scanIF A,  ' (reveal) A,  ' drop A, ' drop A,
1.5       pazsan     27: Create [struct]-voc       [struct]-search A,
                     28:                           NIL A,       NIL A,       NIL A,
1.1       anton      29: 
                     30: : ?if  countif @ 0<
                     31:   IF  [ [struct]-voc 3 cells + ] ALiteral @ lookup !  THEN ;
                     32: 
                     33: UNLOCK  Tlast @ TNIL Tlast !  LOCK
                     34: \ last @  0 last !
                     35: 
1.8       crook      36: : [IF]
                     37:   1 countif +! ?if ;       immediate
                     38: : [THEN]
                     39:   -1 countif +! ?if ;       immediate
                     40: : [ELSE]
1.26      anton      41:   postpone [THEN] postpone [IF] ; immediate
                     42: 
1.1       anton      43: ' [IF]   Alias [IFDEF]               immediate
                     44: ' [IF]   Alias [IFUNDEF]             immediate
1.8       crook      45: ' [THEN] Alias [ENDIF]               immediate
1.1       anton      46: ' [IF]   Alias [BEGIN]               immediate
                     47: ' [IF]   Alias [WHILE]               immediate
                     48: ' [THEN] Alias [UNTIL]               immediate
                     49: ' [THEN] Alias [AGAIN]               immediate
                     50: ' [IF]   Alias [DO]                  immediate
                     51: ' [IF]   Alias [?DO]                 immediate
                     52: ' [THEN] Alias [LOOP]                immediate
                     53: ' [THEN] Alias [+LOOP]               immediate
                     54: : [REPEAT]  postpone [AGAIN] postpone [THEN] ;
                     55:                                      immediate
1.17      anton      56: \ The following was too smart for its own good; consider "postpone (".
                     57: \ Moreover, ANS Forth specifies that the next [THEN] ends an [IF]
                     58: \ (even if its in a '( ... )').
                     59: 
                     60: \ ' ( Alias (                          immediate ( keep fontify happy)
                     61: \ ' \ Alias \                          immediate
1.1       anton      62: 
                     63: UNLOCK Tlast @ swap Tlast ! LOCK
                     64: \ last @ swap last !
1.5       pazsan     65: 1 cells - [struct]-voc cell+ !
1.1       anton      66: 
                     67: \ Interpretative Structuren                            30apr92py
                     68: 
1.27      anton      69: : [defined] ( "<spaces>name" -- flag )   parse-name find-name 0<> ; immediate
1.13      pazsan     70:   \G returns true if name is found in current search order
1.14      anton      71: ' [defined] alias defined immediate
1.13      pazsan     72: : [undefined] ( "<spaces>name" -- flag ) postpone [defined] 0= ; immediate
                     73:   \G returns false if name is found in current search order
1.8       crook      74: 
1.13      pazsan     75: : [IF] ( flag -- ) \ tools-ext bracket-if
1.8       crook      76:   \G If flag is @code{TRUE} do nothing (and therefore
                     77:   \G execute subsequent words as normal). If flag is @code{FALSE},
                     78:   \G parse and discard words from the parse
                     79:   \G area (refilling it if necessary using
                     80:   \G @code{REFILL}) including nested instances of @code{[IF]}..
                     81:   \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
                     82:   \G until the balancing @code{[ELSE]} or @code{[THEN]} has been
                     83:   \G parsed and discarded. Immediate word.
                     84:        0= IF  countif off
1.1       anton      85:               lookup @ [ [struct]-voc 3 cells + ] ALiteral !
                     86:              [struct]-voc lookup !
                     87:           THEN ;                                      immediate
1.8       crook      88: 
                     89: : [IFDEF] ( "<spaces>name" -- ) \ gforth bracket-if-def
                     90:   \G If name is found in the current search-order, behave like
1.9       crook      91:   \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
1.8       crook      92:   \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
1.13      pazsan     93:   postpone [defined]    postpone [IF] ;                 immediate
1.8       crook      94: 
                     95: : [IFUNDEF] ( "<spaces>name" -- ) \ gforth bracket-if-un-def
                     96:   \G If name is not found in the current search-order, behave like
1.9       crook      97:   \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
1.8       crook      98:   \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
1.13      pazsan     99:   postpone [defined] 0= postpone [IF] ;                 immediate
1.8       crook     100: 
1.13      pazsan    101: : [ELSE]  ( -- ) \ tools-ext bracket-else
1.8       crook     102:   \G Parse and discard words from the parse
                    103:   \G area (refilling it if necessary using
                    104:   \G @code{REFILL}) including nested instances of @code{[IF]}..
                    105:   \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
                    106:   \G until the balancing @code{[THEN]} has been parsed and discarded.
                    107:   \G @code{[ELSE]} only gets executed if the balancing @code{[IF]}
                    108:   \G was @code{TRUE}; if it was @code{FALSE}, @code{[IF]} would
                    109:   \G have parsed and discarded the @code{[ELSE]}, leaving the
                    110:   \G subsequent words to be executed as normal.
                    111:   \G Immediate word.
                    112:   0 postpone [IF] ;                                   immediate
                    113: 
                    114: : [THEN] ( -- ) \ tools-ext bracket-then
                    115:   \G Do nothing; used as a marker for other words to parse
1.9       crook     116:   \G and discard up to. Immediate word.
1.8       crook     117:   ;                                                   immediate
                    118: 
                    119: : [ENDIF] ( -- ) \ gforth bracket-end-if
1.9       crook     120:   \G Do nothing; synonym for @code{[THEN]}
1.8       crook     121:   ;                                                   immediate
1.1       anton     122: 
                    123: \ Structs for interpreter                              28nov92py
                    124: 
                    125: User (i)
                    126: 
1.8       crook     127: : [DO]  ( n-limit n-index -- ) \ gforth bracket-do
                    128:   >in @ -rot
1.1       anton     129:   DO   I (i) ! dup >r >in ! interpret r> swap +LOOP  drop ;
                    130:                                                       immediate
1.8       crook     131: 
                    132: : [?DO] ( n-limit n-index -- ) \ gforth bracket-question-do
                    133:   2dup = IF 2drop postpone [ELSE] ELSE postpone [DO] THEN ;
1.1       anton     134:                                                       immediate
1.8       crook     135: 
                    136: : [+LOOP] ( n -- ) \ gforth bracket-question-plus-loop
1.29      pazsan    137:   rdrop lp+ ;                                         immediate
1.8       crook     138: 
                    139: : [LOOP] ( -- ) \ gforth bracket-loop
1.29      pazsan    140:   1 rdrop lp+ ;                                       immediate
1.8       crook     141: 
                    142: : [FOR] ( n -- ) \ gforth bracket-for
                    143:   0 swap postpone [DO] ;                              immediate
                    144: 
                    145: : [NEXT] ( n -- ) \ gforth bracket-next
1.29      pazsan    146:   -1 rdrop lp+ ;                                      immediate
1.8       crook     147: 
1.4       pazsan    148: :noname (i) @ ;
                    149: :noname (i) @ postpone Literal ;
1.8       crook     150: interpret/compile: [I] ( -- n ) \ gforth bracket-i
                    151: 
                    152: : [BEGIN] ( -- ) \ gforth bracket-begin
1.29      pazsan    153:   >in @ >r BEGIN r@ >in ! interpret UNTIL rdrop lp+ ; immediate
1.8       crook     154: 
                    155: ' [+LOOP]  Alias [UNTIL] ( flag -- ) \ gforth bracket-until
1.1       anton     156:                                                       immediate
1.8       crook     157: 
                    158: : [REPEAT]  ( -- ) \ gforth bracket-repeat
1.29      pazsan    159:   false rdrop lp+ ;                                   immediate
1.8       crook     160: 
                    161: ' [REPEAT] Alias [AGAIN] ( -- ) \ gforth bracket-again
                    162:                                                       immediate
                    163: 
                    164: : [WHILE]   ( flag -- ) \ gforth bracket-while
1.29      pazsan    165:   0= IF   postpone [ELSE] true rdrop lp+ 1 countif +!  THEN ;
1.1       anton     166:                                                       immediate
                    167: 
1.24      pazsan    168: \ Warnings on

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>