Annotation of gforth/extend.fs, revision 1.32

1.1       anton       1: \ EXTEND.FS    CORE-EXT Word not fully tested!         12may93jaw
                      2: 
1.30      anton       3: \ Copyright (C) 1995,1998 Free Software Foundation, Inc.
1.12      anton       4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation; either version 2
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program; if not, write to the Free Software
                     19: \ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
                     20: 
                     21: 
1.1       anton      22: \ May be cross-compiled
                     23: 
                     24: decimal
                     25: 
                     26: \ .(                                                    12may93jaw
                     27: 
1.32    ! crook      28: : .(   ( "ccc<paren>" -- ) \ core-ext dot-paren
        !            29:   \G Parse a string ccc delimited by a ) (right parenthesis). Display
        !            30:   \G the string. This is often used to display progress information
        !            31:   \G during compilation. See examples below.
        !            32:   [char] ) parse type ; immediate
1.1       anton      33: 
                     34: \ VALUE 2>R 2R> 2R@                                     17may93jaw
                     35: 
1.3       anton      36: \ !! 2value
1.1       anton      37: 
1.11      anton      38: : 2Literal ( compilation w1 w2 -- ; run-time  -- w1 w2 ) \ double two-literal
1.32    ! crook      39:     \G Compile appropriate code such that, at run-time, cell pair w1, w2 are
        !            40:     \G placed on the stack. Interpretation semantics are undefined.
1.11      anton      41:     swap postpone Literal  postpone Literal ; immediate restrict
1.2       pazsan     42: 
1.16      anton      43: ' drop alias d>s ( d -- n ) \ double           d_to_s
                     44: 
1.11      anton      45: : m*/ ( d1 n2 u3 -- dqout ) \ double m-star-slash
                     46:     >r s>d >r abs -rot
                     47:     s>d r> xor r> swap >r >r dabs rot tuck um* 2swap um*
                     48:     swap >r 0 d+ r> -rot r@ um/mod -rot r> um/mod nip swap
                     49:     r> IF dnegate THEN ;
1.4       pazsan     50: 
1.1       anton      51: \ CASE OF ENDOF ENDCASE                                 17may93jaw
                     52: 
                     53: \ just as described in dpANS5
                     54: 
1.11      anton      55: 0 CONSTANT case ( compilation  -- case-sys ; run-time  -- ) \ core-ext
                     56:     immediate
1.1       anton      57: 
1.11      anton      58: : of ( compilation  -- of-sys ; run-time x1 x2 -- |x1 ) \ core-ext
                     59:     \ !! the implementation does not match the stack effect
                     60:     1+ >r
                     61:     postpone over postpone = postpone if postpone drop
                     62:     r> ; immediate
                     63: 
                     64: : endof ( compilation case-sys1 of-sys -- case-sys2 ; run-time  -- ) \ core-ext end-of
                     65:     >r postpone else r> ; immediate
                     66: 
                     67: : endcase ( compilation case-sys -- ; run-time x -- ) \ core-ext end-case
                     68:     postpone drop
                     69:     0 ?do postpone then loop ; immediate
1.1       anton      70: 
                     71: \ C"                                                    17may93jaw
                     72: 
                     73: : (c")     "lit ;
                     74: 
1.11      anton      75: : CLiteral
                     76:     postpone (c") here over char+ allot  place align ; immediate restrict
                     77: 
1.32    ! crook      78: : C" ( compilation "ccc<quote>" -- ; run-time  -- c-addr ) \ core-ext c-quote
        !            79:     \G Compilation: parse a string ccc delimited by a " (double quote). At
        !            80:     \G run-time, return c-addr which specifies the counted string ccc.
        !            81:     \G Interpretation semantics are undefined.
1.11      anton      82:     [char] " parse postpone CLiteral ; immediate restrict
1.1       anton      83: 
                     84: \ [COMPILE]                                             17may93jaw
                     85: 
1.11      anton      86: : [compile] ( compilation "name" -- ; run-time ? -- ? ) \ core-ext bracket-compile
1.24      pazsan     87:     comp' drop compile, ; immediate
1.1       anton      88: 
                     89: \ CONVERT                                               17may93jaw
                     90: 
1.11      anton      91: : convert ( ud1 c-addr1 -- ud2 c-addr2 ) \ core-ext
1.26      anton      92:     \G obsolescent; superseded by @code{>number}.
                     93:     char+ true >number drop ;
1.1       anton      94: 
                     95: \ ERASE                                                 17may93jaw
                     96: 
1.11      anton      97: : erase ( addr len -- ) \ core-ext
1.32    ! crook      98:     \G If len>0, clear all bits in each location of a memory region
        !            99:     \G of len address units starting at address addr.
1.11      anton     100:     \ !! dependence on "1 chars 1 ="
                    101:     ( 0 1 chars um/mod nip )  0 fill ;
                    102: : blank ( addr len -- ) \ string
1.32    ! crook     103:     \G If len>0, store the character value for a space in each
        !           104:     \G location of a memory region
        !           105:     \G of len character units starting at address addr.
1.11      anton     106:     bl fill ;
1.1       anton     107: 
1.7       pazsan    108: \ SEARCH                                                02sep94py
                    109: 
1.28      anton     110: : search ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ string
1.32    ! crook     111:     \G Search the string specified by c-addr1, u1 for the string
        !           112:     \G speficied by c-addr2, u2. If flag is true: match was found
        !           113:     \G at c-addr3 with u3 characters remaining. If flag is false:
        !           114:     \G no match was found; c-addr3, u3 are equal to c-addr1, u1.
1.28      anton     115:     \ not very efficient; but if we want efficiency, we'll do it as primitive
                    116:     2>r 2dup
                    117:     begin
                    118:        dup r@ >=
                    119:     while
                    120:        over 2r@ swap -text 0= if
                    121:            2swap 2drop 2r> 2drop true exit
                    122:        endif
                    123:        1 /string
                    124:     repeat
                    125:     2drop 2r> 2drop false ;
1.7       pazsan    126: 
1.1       anton     127: \ SOURCE-ID SAVE-INPUT RESTORE-INPUT                    11jun93jaw
                    128: 
1.22      anton     129: : source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
1.13      anton     130:   loadfile @ dup 0= IF  drop sourceline# 0 min  THEN ;
1.1       anton     131: 
1.11      anton     132: : save-input ( -- x1 .. xn n ) \ core-ext
1.14      anton     133:     >in @
                    134:     loadfile @
                    135:     if
                    136:        loadfile @ file-position throw
                    137:     else
                    138:        blk @
                    139:        linestart @
                    140:     then
                    141:     sourceline#
                    142:     >tib @
                    143:     source-id
                    144:     6 ;
1.1       anton     145: 
1.11      anton     146: : restore-input ( x1 .. xn n -- flag ) \ core-ext
1.14      anton     147:     6 <> -12 and throw
                    148:     source-id <> -12 and throw
                    149:     >tib !
                    150:     >r ( line# )
                    151:     loadfile @ 0<>
                    152:     if
                    153:        loadfile @ reposition-file throw
                    154:     else
                    155:        linestart !
                    156:        blk !
                    157:        sourceline# r@ <> blk @ 0= and loadfile @ 0= and
                    158:        if
                    159:            drop rdrop true EXIT
                    160:        then
                    161:     then
                    162:     r> loadline !
                    163:     >in !
                    164:     false ;
                    165: 
1.1       anton     166: \ This things we don't need, but for being complete... jaw
                    167: 
                    168: \ EXPECT SPAN                                           17may93jaw
                    169: 
1.11      anton     170: variable span ( -- a-addr ) \ core-ext
                    171: \ obsolescent
1.1       anton     172: 
1.11      anton     173: : expect ( c-addr +len -- ) \ core-ext
                    174:     \ obsolescent; use accept
1.9       anton     175:     0 rot over
                    176:     BEGIN ( maxlen span c-addr pos1 )
                    177:        key decode ( maxlen span c-addr pos2 flag )
                    178:        >r 2over = r> or
                    179:     UNTIL
1.17      pazsan    180:     2 pick swap /string type
                    181:     nip span ! ;
1.27      jwilke    182: 
                    183: \ marker                                               18dec94py
                    184: 
                    185: \ Marker creates a mark that is removed (including everything 
                    186: \ defined afterwards) when executing the mark.
                    187: 
1.31      anton     188: : included-files-mark ( -- u )
                    189:     included-files 2@ nip
                    190:     blk @ 0=
                    191:     if \ not input from blocks
                    192:        source-id 1 -1 within
                    193:        if \ input from file
                    194:            1- \ do not include the last file (hopefully this is the
                    195:               \ currently included file)
                    196:        then
                    197:     then ;  
                    198: 
                    199: \ hmm, most of the saving appears to be pretty unnecessary: we could
                    200: \ derive the wordlists and the words that have to be kept from the
                    201: \ saved value of dp value. - anton
                    202: 
                    203: : marker, ( -- mark )
                    204:     here
                    205:     included-files-mark ,
                    206:     dup A, \ here
                    207:     voclink @ A, \ vocabulary list start
                    208:     \ for all wordlists, remember wordlist-id (the linked list)
                    209:     voclink
                    210:     BEGIN
                    211:        @ dup
                    212:     WHILE
                    213:        dup 0 wordlist-link - wordlist-id @ A,
                    214:     REPEAT
                    215:     drop
                    216:     \ remember udp
                    217:     udp @ , ;
1.27      jwilke    218: 
                    219: : marker! ( mark -- )
1.31      anton     220:     \ reset included files count; resize will happen on next add-included-file
                    221:     included-files 2@ drop over @ included-files 2! cell+
                    222:     \ rest of marker!
                    223:     dup @ swap cell+ ( here rest-of-marker )
1.27      jwilke    224:     dup @ voclink ! cell+
1.31      anton     225:     \ restore wordlists to former words
1.27      jwilke    226:     voclink
                    227:     BEGIN
                    228:        @ dup 
                    229:     WHILE
1.31      anton     230:        over @ over 0 wordlist-link - wordlist-id !
1.27      jwilke    231:        swap cell+ swap
                    232:     REPEAT
1.31      anton     233:     drop
                    234:     \ rehash wordlists to remove forgotten words
                    235:     \ why don't we do this in a single step? - anton
                    236:     voclink
1.27      jwilke    237:     BEGIN
                    238:        @ dup
                    239:     WHILE
                    240:        dup 0 wordlist-link - rehash
                    241:     REPEAT
                    242:     drop
1.31      anton     243:     \ restore udp and dp
1.29      pazsan    244:     @ udp !  dp !
                    245:     \ clean up vocabulary stack
                    246:     0 vp @ 0
                    247:     ?DO
                    248:        vp cell+ I cells + @ dup here >
                    249:        IF  drop  ELSE  swap 1+  THEN
                    250:     LOOP
                    251:     dup 0= or set-order \ -1 set-order if order is empty
                    252:     get-current here > IF
                    253:        forth-wordlist set-current
                    254:     THEN ;
1.27      jwilke    255: 
1.32    ! crook     256: : marker ( "<spaces> name" -- ) \ core-ext
        !           257:     \G Create a definition, @var{name} (called a @var{mark}) whose
        !           258:     \G execution semantics are to remove itself and everything 
        !           259:     \G defined after it.
1.27      jwilke    260:     marker, Create A,
                    261: DOES> ( -- )
                    262:     @ marker! ;
1.1       anton     263: 

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