File:  [gforth] / gforth / kernel / toolsext.fs
Revision 1.17: download - view: text, annotated - select for diffs
Sat Mar 8 19:20:39 2003 UTC (21 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
bugfix (dealing with "(" inside 0 [if] ... [then])

    1: \ Copyright (C) 1995,1998,2000 Free Software Foundation, Inc.
    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
    7: \ as published by the Free Software Foundation; either version 2
    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
   16: \ along with this program; if not, write to the Free Software
   17: \ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
   18: 
   19: Warnings off
   20: 
   21: Variable countif
   22: 
   23: : dummy ;  immediate
   24: : >exec  >r ; restrict ( :-)
   25: : scanIF   f83find  dup 0=  IF  drop ['] dummy >head-noprim  THEN  ;
   26: 
   27: Create [struct]-search    ' scanIF A,  ' (reveal) A,  ' drop A, ' drop A,
   28: Create [struct]-voc       [struct]-search A,
   29:                           NIL A,       NIL A,       NIL A,
   30: 
   31: : ?if  countif @ 0<
   32:   IF  [ [struct]-voc 3 cells + ] ALiteral @ lookup !  THEN ;
   33: 
   34: UNLOCK  Tlast @ TNIL Tlast !  LOCK
   35: \ last @  0 last !
   36: 
   37: : [IF]
   38:   1 countif +! ?if ;       immediate
   39: : [THEN]
   40:   -1 countif +! ?if ;       immediate
   41: : [ELSE]
   42:   postpone [THEN] postpone [IF] ;
   43:                                      immediate
   44: ' [IF]   Alias [IFDEF]               immediate
   45: ' [IF]   Alias [IFUNDEF]             immediate
   46: ' [THEN] Alias [ENDIF]               immediate
   47: ' [IF]   Alias [BEGIN]               immediate
   48: ' [IF]   Alias [WHILE]               immediate
   49: ' [THEN] Alias [UNTIL]               immediate
   50: ' [THEN] Alias [AGAIN]               immediate
   51: ' [IF]   Alias [DO]                  immediate
   52: ' [IF]   Alias [?DO]                 immediate
   53: ' [THEN] Alias [LOOP]                immediate
   54: ' [THEN] Alias [+LOOP]               immediate
   55: : [REPEAT]  postpone [AGAIN] postpone [THEN] ;
   56:                                      immediate
   57: \ The following was too smart for its own good; consider "postpone (".
   58: \ Moreover, ANS Forth specifies that the next [THEN] ends an [IF]
   59: \ (even if its in a '( ... )').
   60: 
   61: \ ' ( Alias (                          immediate ( keep fontify happy)
   62: \ ' \ Alias \                          immediate
   63: 
   64: UNLOCK Tlast @ swap Tlast ! LOCK
   65: \ last @ swap last !
   66: 1 cells - [struct]-voc cell+ !
   67: 
   68: \ Interpretative Structuren                            30apr92py
   69: 
   70: : [defined] ( "<spaces>name" -- flag )   bl word find nip 0<> ; immediate
   71:   \G returns true if name is found in current search order
   72: ' [defined] alias defined immediate
   73: : [undefined] ( "<spaces>name" -- flag ) postpone [defined] 0= ; immediate
   74:   \G returns false if name is found in current search order
   75: 
   76: : [IF] ( flag -- ) \ tools-ext bracket-if
   77:   \G If flag is @code{TRUE} do nothing (and therefore
   78:   \G execute subsequent words as normal). If flag is @code{FALSE},
   79:   \G parse and discard words from the parse
   80:   \G area (refilling it if necessary using
   81:   \G @code{REFILL}) including nested instances of @code{[IF]}..
   82:   \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
   83:   \G until the balancing @code{[ELSE]} or @code{[THEN]} has been
   84:   \G parsed and discarded. Immediate word.
   85:        0= IF  countif off
   86:               lookup @ [ [struct]-voc 3 cells + ] ALiteral !
   87: 	      [struct]-voc lookup !
   88:           THEN ;                                      immediate
   89: 
   90: : [IFDEF] ( "<spaces>name" -- ) \ gforth bracket-if-def
   91:   \G If name is found in the current search-order, behave like
   92:   \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
   93:   \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
   94:   postpone [defined]    postpone [IF] ;                 immediate
   95: 
   96: : [IFUNDEF] ( "<spaces>name" -- ) \ gforth bracket-if-un-def
   97:   \G If name is not found in the current search-order, behave like
   98:   \G @code{[IF]} with a @code{TRUE} flag, otherwise behave like
   99:   \G @code{[IF]} with a @code{FALSE} flag. Immediate word.
  100:   postpone [defined] 0= postpone [IF] ;                 immediate
  101: 
  102: : [ELSE]  ( -- ) \ tools-ext bracket-else
  103:   \G Parse and discard words from the parse
  104:   \G area (refilling it if necessary using
  105:   \G @code{REFILL}) including nested instances of @code{[IF]}..
  106:   \G @code{[ELSE]}.. @code{[THEN]} and @code{[IF]}.. @code{[THEN]}
  107:   \G until the balancing @code{[THEN]} has been parsed and discarded.
  108:   \G @code{[ELSE]} only gets executed if the balancing @code{[IF]}
  109:   \G was @code{TRUE}; if it was @code{FALSE}, @code{[IF]} would
  110:   \G have parsed and discarded the @code{[ELSE]}, leaving the
  111:   \G subsequent words to be executed as normal.
  112:   \G Immediate word.
  113:   0 postpone [IF] ;                                   immediate
  114: 
  115: : [THEN] ( -- ) \ tools-ext bracket-then
  116:   \G Do nothing; used as a marker for other words to parse
  117:   \G and discard up to. Immediate word.
  118:   ;                                                   immediate
  119: 
  120: : [ENDIF] ( -- ) \ gforth bracket-end-if
  121:   \G Do nothing; synonym for @code{[THEN]}
  122:   ;                                                   immediate
  123: 
  124: \ Structs for interpreter                              28nov92py
  125: 
  126: User (i)
  127: 
  128: : [DO]  ( n-limit n-index -- ) \ gforth bracket-do
  129:   >in @ -rot
  130:   DO   I (i) ! dup >r >in ! interpret r> swap +LOOP  drop ;
  131:                                                       immediate
  132: 
  133: : [?DO] ( n-limit n-index -- ) \ gforth bracket-question-do
  134:   2dup = IF 2drop postpone [ELSE] ELSE postpone [DO] THEN ;
  135:                                                       immediate
  136: 
  137: : [+LOOP] ( n -- ) \ gforth bracket-question-plus-loop
  138:   rdrop rdrop ;                                       immediate
  139: 
  140: : [LOOP] ( -- ) \ gforth bracket-loop
  141:   1 rdrop rdrop ;                                     immediate
  142: 
  143: : [FOR] ( n -- ) \ gforth bracket-for
  144:   0 swap postpone [DO] ;                              immediate
  145: 
  146: : [NEXT] ( n -- ) \ gforth bracket-next
  147:   -1 rdrop rdrop ;                                    immediate
  148: 
  149: :noname (i) @ ;
  150: :noname (i) @ postpone Literal ;
  151: interpret/compile: [I] ( -- n ) \ gforth bracket-i
  152: 
  153: : [BEGIN] ( -- ) \ gforth bracket-begin
  154:   >in @ >r BEGIN r@ >in ! interpret UNTIL rdrop ;     immediate
  155: 
  156: ' [+LOOP]  Alias [UNTIL] ( flag -- ) \ gforth bracket-until
  157:                                                       immediate
  158: 
  159: : [REPEAT]  ( -- ) \ gforth bracket-repeat
  160:   false rdrop rdrop ;                                 immediate
  161: 
  162: ' [REPEAT] Alias [AGAIN] ( -- ) \ gforth bracket-again
  163:                                                       immediate
  164: 
  165: : [WHILE]   ( flag -- ) \ gforth bracket-while
  166:   0= IF   postpone [ELSE] true rdrop rdrop 1 countif +!  THEN ;
  167:                                                       immediate
  168: 
  169: Warnings on

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