Annotation of gforth/extend.fs, revision 1.38

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
1.36      crook      29:   \G Parse a string @i{ccc} delimited by a @code{)} (right
1.33      crook      30:   \G parenthesis). Display the string. This is often used to display
                     31:   \G progress information during compilation; see examples below.
1.32      crook      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.36      crook      39:     \G Compile appropriate code such that, at run-time, cell pair @i{w1, w2} are
1.32      crook      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
1.36      crook      79:     \G Compilation: parse a string @i{ccc} delimited by a @code{"}
                     80:     \G (double quote). At run-time, return @i{c-addr} which
                     81:     \G specifies the counted string @i{ccc}.  Interpretation
1.33      crook      82:     \G semantics are undefined.
1.11      anton      83:     [char] " parse postpone CLiteral ; immediate restrict
1.1       anton      84: 
                     85: \ [COMPILE]                                             17may93jaw
                     86: 
1.11      anton      87: : [compile] ( compilation "name" -- ; run-time ? -- ? ) \ core-ext bracket-compile
1.37      anton      88:     comp' drop
                     89:     dup [ comp' exit drop ] literal = if
                     90:        execute \ EXIT has default compilation semantics, perform them
                     91:     else
                     92:        compile,
                     93:     then ; immediate
1.1       anton      94: 
                     95: \ CONVERT                                               17may93jaw
                     96: 
1.11      anton      97: : convert ( ud1 c-addr1 -- ud2 c-addr2 ) \ core-ext
1.34      crook      98:     \G OBSOLESCENT: superseded by @code{>number}.
1.26      anton      99:     char+ true >number drop ;
1.1       anton     100: 
                    101: \ ERASE                                                 17may93jaw
                    102: 
1.11      anton     103: : erase ( addr len -- ) \ core-ext
1.36      crook     104:     \G If @i{len}>0, clear all bits in each location of a memory region
                    105:     \G of @i{len} address units starting at address @i{addr}.
1.11      anton     106:     \ !! dependence on "1 chars 1 ="
                    107:     ( 0 1 chars um/mod nip )  0 fill ;
1.34      crook     108: : blank ( c-addr u -- ) \ string
1.36      crook     109:     \G If @i{u}>0, store the character value for a space in each
1.32      crook     110:     \G location of a memory region
1.36      crook     111:     \G of @i{u} character units starting at address @i{c-addr}.
1.11      anton     112:     bl fill ;
1.1       anton     113: 
1.7       pazsan    114: \ SEARCH                                                02sep94py
                    115: 
1.28      anton     116: : search ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ string
1.36      crook     117:     \G Search the string specified by @i{c-addr1, u1} for the string
                    118:     \G specified by @i{c-addr2, u2}. If @i{flag} is true: match was found
                    119:     \G at @i{c-addr3} with @i{u3} characters remaining. If @i{flag} is false:
                    120:     \G no match was found; @i{c-addr3, u3} are equal to @i{c-addr1, u1}.
1.28      anton     121:     \ not very efficient; but if we want efficiency, we'll do it as primitive
                    122:     2>r 2dup
                    123:     begin
                    124:        dup r@ >=
                    125:     while
                    126:        over 2r@ swap -text 0= if
                    127:            2swap 2drop 2r> 2drop true exit
                    128:        endif
                    129:        1 /string
                    130:     repeat
                    131:     2drop 2r> 2drop false ;
1.7       pazsan    132: 
1.1       anton     133: \ SOURCE-ID SAVE-INPUT RESTORE-INPUT                    11jun93jaw
                    134: 
1.22      anton     135: : source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
1.34      crook     136:     \G Return 0 (the input source is the user input device), -1 (the
                    137:     \G input source is a string being processed by @code{evaluate}) or
1.36      crook     138:     \G a @i{fileid} (the input source is the file specified by
                    139:     \G @i{fileid}).
1.34      crook     140:     loadfile @ dup 0= IF  drop sourceline# 0 min  THEN ;
                    141: 
                    142: : save-input ( -- xn .. x1 n ) \ core-ext
1.36      crook     143:     \G The @i{n} entries @i{xn - x1} describe the current state of the
1.34      crook     144:     \G input source specification, in some platform-dependent way that can
                    145:     \G be used by @code{restore-input}.
1.14      anton     146:     >in @
                    147:     loadfile @
                    148:     if
1.38    ! anton     149:        loadfile @ file-position throw #TIB @ 1+ 0 d- \ !! bug for CRLF and EOF
1.14      anton     150:     else
                    151:        blk @
                    152:        linestart @
                    153:     then
                    154:     sourceline#
                    155:     >tib @
                    156:     source-id
                    157:     6 ;
1.1       anton     158: 
1.34      crook     159: : restore-input ( xn .. x1 n -- flag ) \ core-ext
                    160:     \G Attempt to restore the input source specification to the state
1.36      crook     161:     \G described by the @i{n} entries @i{xn - x1}. @i{flag} is
1.35      anton     162:     \G true if the restore fails.  In Gforth it fails pretty often
                    163:     \G (and sometimes with a @code{throw}).
1.14      anton     164:     6 <> -12 and throw
                    165:     source-id <> -12 and throw
                    166:     >tib !
                    167:     >r ( line# )
                    168:     loadfile @ 0<>
                    169:     if
                    170:        loadfile @ reposition-file throw
1.38    ! anton     171:        refill 0= -36 and throw \ should never throw
1.14      anton     172:     else
                    173:        linestart !
                    174:        blk !
                    175:        sourceline# r@ <> blk @ 0= and loadfile @ 0= and
                    176:        if
                    177:            drop rdrop true EXIT
                    178:        then
                    179:     then
                    180:     r> loadline !
                    181:     >in !
                    182:     false ;
                    183: 
1.1       anton     184: \ This things we don't need, but for being complete... jaw
                    185: 
                    186: \ EXPECT SPAN                                           17may93jaw
                    187: 
1.34      crook     188: variable span ( -- c-addr ) \ core-ext
1.36      crook     189: \G @code{Variable} -- @i{c-addr} is the address of a cell that stores the
                    190: \G length of the last string received by @code{expect}. OBSOLESCENT.
1.34      crook     191: 
                    192: : expect ( c-addr +n -- ) \ core-ext
1.36      crook     193:     \G Receive a string of at most @i{+n} characters, and store it
                    194:     \G in memory starting at @i{c-addr}. The string is
1.34      crook     195:     \G displayed. Input terminates when the <return> key is pressed or
1.36      crook     196:     \G @i{+n} characters have been received. The normal Gforth line
1.34      crook     197:     \G editing capabilites are available. The length of the string is
                    198:     \G stored in @code{span}; it does not include the <return>
                    199:     \G character. OBSOLESCENT: superceeded by @code{accept}.
1.9       anton     200:     0 rot over
                    201:     BEGIN ( maxlen span c-addr pos1 )
                    202:        key decode ( maxlen span c-addr pos2 flag )
                    203:        >r 2over = r> or
                    204:     UNTIL
1.17      pazsan    205:     2 pick swap /string type
                    206:     nip span ! ;
1.27      jwilke    207: 
                    208: \ marker                                               18dec94py
                    209: 
                    210: \ Marker creates a mark that is removed (including everything 
                    211: \ defined afterwards) when executing the mark.
                    212: 
1.31      anton     213: : included-files-mark ( -- u )
                    214:     included-files 2@ nip
                    215:     blk @ 0=
                    216:     if \ not input from blocks
                    217:        source-id 1 -1 within
                    218:        if \ input from file
                    219:            1- \ do not include the last file (hopefully this is the
                    220:               \ currently included file)
                    221:        then
                    222:     then ;  
                    223: 
                    224: \ hmm, most of the saving appears to be pretty unnecessary: we could
                    225: \ derive the wordlists and the words that have to be kept from the
                    226: \ saved value of dp value. - anton
                    227: 
                    228: : marker, ( -- mark )
                    229:     here
                    230:     included-files-mark ,
                    231:     dup A, \ here
                    232:     voclink @ A, \ vocabulary list start
                    233:     \ for all wordlists, remember wordlist-id (the linked list)
                    234:     voclink
                    235:     BEGIN
                    236:        @ dup
                    237:     WHILE
                    238:        dup 0 wordlist-link - wordlist-id @ A,
                    239:     REPEAT
                    240:     drop
                    241:     \ remember udp
                    242:     udp @ , ;
1.27      jwilke    243: 
                    244: : marker! ( mark -- )
1.31      anton     245:     \ reset included files count; resize will happen on next add-included-file
                    246:     included-files 2@ drop over @ included-files 2! cell+
                    247:     \ rest of marker!
                    248:     dup @ swap cell+ ( here rest-of-marker )
1.27      jwilke    249:     dup @ voclink ! cell+
1.31      anton     250:     \ restore wordlists to former words
1.27      jwilke    251:     voclink
                    252:     BEGIN
                    253:        @ dup 
                    254:     WHILE
1.31      anton     255:        over @ over 0 wordlist-link - wordlist-id !
1.27      jwilke    256:        swap cell+ swap
                    257:     REPEAT
1.31      anton     258:     drop
                    259:     \ rehash wordlists to remove forgotten words
                    260:     \ why don't we do this in a single step? - anton
                    261:     voclink
1.27      jwilke    262:     BEGIN
                    263:        @ dup
                    264:     WHILE
                    265:        dup 0 wordlist-link - rehash
                    266:     REPEAT
                    267:     drop
1.31      anton     268:     \ restore udp and dp
1.29      pazsan    269:     @ udp !  dp !
                    270:     \ clean up vocabulary stack
                    271:     0 vp @ 0
                    272:     ?DO
                    273:        vp cell+ I cells + @ dup here >
                    274:        IF  drop  ELSE  swap 1+  THEN
                    275:     LOOP
                    276:     dup 0= or set-order \ -1 set-order if order is empty
                    277:     get-current here > IF
                    278:        forth-wordlist set-current
                    279:     THEN ;
1.27      jwilke    280: 
1.32      crook     281: : marker ( "<spaces> name" -- ) \ core-ext
1.36      crook     282:     \G Create a definition, @i{name} (called a @i{mark}) whose
1.32      crook     283:     \G execution semantics are to remove itself and everything 
                    284:     \G defined after it.
1.27      jwilke    285:     marker, Create A,
                    286: DOES> ( -- )
                    287:     @ marker! ;
1.1       anton     288: 

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