File:  [gforth] / gforth / extend.fs
Revision 1.32: download - view: text, annotated - select for diffs
Wed Feb 3 00:10:20 1999 UTC (21 years ago) by crook
Branches: MAIN
CVS tags: HEAD
New "docclean" target for makefile (removes glossary dependencies when
rebuilding documentation). Changes to .fs files and prim are restricted
to glossary (\G) additions for the documentation; this has necessitated
the addition of new white-space in places to stop the \G stuff from
obscuring the code. Many additions to doc/gforth.ds - new sections
added, a few things moved and some sections re-written slightly. There
are a set of things to tidy up before this rev. is suitable for
release, and those will be my highest priority. I have also used
"@comment TODO" to highlight other sections I plan to work on, and
added a set of comments at the start to indicate other things I plan
to modify in the medium-term.

    1: \ EXTEND.FS    CORE-EXT Word not fully tested!         12may93jaw
    2: 
    3: \ Copyright (C) 1995,1998 Free Software Foundation, Inc.
    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: 
   22: \ May be cross-compiled
   23: 
   24: decimal
   25: 
   26: \ .(                                                    12may93jaw
   27: 
   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
   33: 
   34: \ VALUE 2>R 2R> 2R@                                     17may93jaw
   35: 
   36: \ !! 2value
   37: 
   38: : 2Literal ( compilation w1 w2 -- ; run-time  -- w1 w2 ) \ double two-literal
   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.
   41:     swap postpone Literal  postpone Literal ; immediate restrict
   42: 
   43: ' drop alias d>s ( d -- n ) \ double		d_to_s
   44: 
   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 ;
   50: 
   51: \ CASE OF ENDOF ENDCASE                                 17may93jaw
   52: 
   53: \ just as described in dpANS5
   54: 
   55: 0 CONSTANT case ( compilation  -- case-sys ; run-time  -- ) \ core-ext
   56:     immediate
   57: 
   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
   70: 
   71: \ C"                                                    17may93jaw
   72: 
   73: : (c")     "lit ;
   74: 
   75: : CLiteral
   76:     postpone (c") here over char+ allot  place align ; immediate restrict
   77: 
   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.
   82:     [char] " parse postpone CLiteral ; immediate restrict
   83: 
   84: \ [COMPILE]                                             17may93jaw
   85: 
   86: : [compile] ( compilation "name" -- ; run-time ? -- ? ) \ core-ext bracket-compile
   87:     comp' drop compile, ; immediate
   88: 
   89: \ CONVERT                                               17may93jaw
   90: 
   91: : convert ( ud1 c-addr1 -- ud2 c-addr2 ) \ core-ext
   92:     \G obsolescent; superseded by @code{>number}.
   93:     char+ true >number drop ;
   94: 
   95: \ ERASE                                                 17may93jaw
   96: 
   97: : erase ( addr len -- ) \ core-ext
   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.
  100:     \ !! dependence on "1 chars 1 ="
  101:     ( 0 1 chars um/mod nip )  0 fill ;
  102: : blank ( addr len -- ) \ string
  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.
  106:     bl fill ;
  107: 
  108: \ SEARCH                                                02sep94py
  109: 
  110: : search ( c-addr1 u1 c-addr2 u2 -- c-addr3 u3 flag ) \ string
  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.
  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 ;
  126: 
  127: \ SOURCE-ID SAVE-INPUT RESTORE-INPUT                    11jun93jaw
  128: 
  129: : source-id ( -- 0 | -1 | fileid ) \ core-ext,file source-i-d
  130:   loadfile @ dup 0= IF  drop sourceline# 0 min  THEN ;
  131: 
  132: : save-input ( -- x1 .. xn n ) \ core-ext
  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 ;
  145: 
  146: : restore-input ( x1 .. xn n -- flag ) \ core-ext
  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: 
  166: \ This things we don't need, but for being complete... jaw
  167: 
  168: \ EXPECT SPAN                                           17may93jaw
  169: 
  170: variable span ( -- a-addr ) \ core-ext
  171: \ obsolescent
  172: 
  173: : expect ( c-addr +len -- ) \ core-ext
  174:     \ obsolescent; use accept
  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
  180:     2 pick swap /string type
  181:     nip span ! ;
  182: 
  183: \ marker                                               18dec94py
  184: 
  185: \ Marker creates a mark that is removed (including everything 
  186: \ defined afterwards) when executing the mark.
  187: 
  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 @ , ;
  218: 
  219: : marker! ( mark -- )
  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 )
  224:     dup @ voclink ! cell+
  225:     \ restore wordlists to former words
  226:     voclink
  227:     BEGIN
  228: 	@ dup 
  229:     WHILE
  230: 	over @ over 0 wordlist-link - wordlist-id !
  231: 	swap cell+ swap
  232:     REPEAT
  233:     drop
  234:     \ rehash wordlists to remove forgotten words
  235:     \ why don't we do this in a single step? - anton
  236:     voclink
  237:     BEGIN
  238: 	@ dup
  239:     WHILE
  240: 	dup 0 wordlist-link - rehash
  241:     REPEAT
  242:     drop
  243:     \ restore udp and dp
  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 ;
  255: 
  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.
  260:     marker, Create A,
  261: DOES> ( -- )
  262:     @ marker! ;
  263: 

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