File:  [gforth] / gforth / Attic / kernal.fs
Revision 1.61: download - view: text, annotated - select for diffs
Wed Aug 21 14:58:42 1996 UTC (27 years, 7 months ago) by anton
Branches: MAIN
CVS tags: HEAD
a little cleanup in 386.h
renamed special: to interpret/compile:
renamed save-string to save-mem
added extend-mem
replaced (name>) with ((name>))
replaced name> with name>int and name>comp
renamed compile-only to compile-only-error
replaced xt>i with name>int
replaced xt>c with name>comp
removed xt>s
removed found
search-wordlist now delivers interpretation-xt
replaced (sfind) with find-name
replaced C' with COMP' and [C'] with [COMP']
removed S' and [S']
added hex.
added some helper words
adapted other words to the changes
started documenting the intergration of Gforth in applications

    1: \ KERNAL.FS    GForth kernal                        17dec92py
    2: 
    3: \ Copyright (C) 1995 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: \ Idea and implementation: Bernd Paysan (py)
   22: 
   23: \ Log:  ', '- usw. durch [char] ... ersetzt
   24: \       man sollte die unterschiedlichen zahlensysteme
   25: \       mit $ und & zumindest im interpreter weglassen
   26: \       schon erledigt!
   27: \       11may93jaw
   28: \ name>         0= nicht vorhanden              17may93jaw
   29: \               nfa can be lfa or nfa!
   30: \ find          splited into find and (find)
   31: \               (find) for later use            17may93jaw
   32: \ search        replaced by lookup because
   33: \               it is a word of the string wordset
   34: \                                               20may93jaw
   35: \ postpone      added immediate                 21may93jaw
   36: \ to            added immediate                 07jun93jaw
   37: \ cfa, header   put "here lastcfa !" in
   38: \               cfa, this is more logical
   39: \               and noname: works wothout
   40: \               extra "here lastcfa !"          08jun93jaw
   41: \ (parse-white) thrown out
   42: \ refill        added outer trick
   43: \               to show there is something
   44: \               going on                        09jun93jaw
   45: \ leave ?leave  somebody forgot UNLOOP!!!       09jun93jaw
   46: \ leave ?leave  unloop thrown out
   47: \               unloop after loop is used       10jun93jaw
   48: 
   49: HEX
   50: 
   51: \ labels for some code addresses
   52: 
   53: : docon: ( -- addr )	\ gforth
   54:     \ the code address of a @code{CONSTANT}
   55:     ['] bl >code-address ;
   56: 
   57: : docol: ( -- addr )	\ gforth
   58:     \ the code address of a colon definition
   59:     ['] docon: >code-address ;
   60: 
   61: : dovar: ( -- addr )	\ gforth
   62:     \ the code address of a @code{CREATE}d word
   63:     ['] udp >code-address ;
   64: 
   65: : douser: ( -- addr )	\ gforth
   66:     \ the code address of a @code{USER} variable
   67:     ['] s0 >code-address ;
   68: 
   69: : dodefer: ( -- addr )	\ gforth
   70:     \ the code address of a @code{defer}ed word
   71:     ['] source >code-address ;
   72: 
   73: : dofield: ( -- addr )	\ gforth
   74:     \ the code address of a @code{field}
   75:     ['] reveal-method >code-address ;
   76: 
   77: NIL AConstant NIL \ gforth
   78: 
   79: \ Bit string manipulation                              06oct92py
   80: 
   81: \ Create bits  80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
   82: \ DOES> ( n -- )  + c@ ;
   83: 
   84: \ : >bit  ( addr n -- c-addr mask )  8 /mod rot + swap bits ;
   85: \ : +bit  ( addr n -- )  >bit over c@ or swap c! ;
   86: 
   87: \ : relinfo ( -- addr )  forthstart dup @ + !!bug!! ;
   88: \ : >rel  ( addr -- n )  forthstart - ;
   89: \ : relon ( addr -- )  relinfo swap >rel cell / +bit ;
   90: 
   91: \ here allot , c, A,                                   17dec92py
   92: 
   93: : dp	( -- addr ) \ gforth
   94:     dpp @ ;
   95: : here  ( -- here ) \ core
   96:     dp @ ;
   97: : allot ( n -- ) \ core
   98:     dp +! ;
   99: : c,    ( c -- ) \ core
  100:     here 1 chars allot c! ;
  101: : ,     ( x -- ) \ core
  102:     here cell allot  ! ;
  103: : 2,	( w1 w2 -- ) \ gforth
  104:     here 2 cells allot 2! ;
  105: 
  106: \ : aligned ( addr -- addr' ) \ core
  107: \     [ cell 1- ] Literal + [ -1 cells ] Literal and ;
  108: : align ( -- ) \ core
  109:     here dup aligned swap ?DO  bl c,  LOOP ;
  110: 
  111: \ : faligned ( addr -- f-addr ) \ float
  112: \     [ 1 floats 1- ] Literal + [ -1 floats ] Literal and ;
  113: 
  114: : falign ( -- ) \ float
  115:     here dup faligned swap
  116:     ?DO
  117: 	bl c,
  118:     LOOP ;
  119: 
  120: \ !! this is machine-dependent, but works on all but the strangest machines
  121: ' faligned Alias maxaligned ( addr1 -- addr2 ) \ gforth
  122: ' falign Alias maxalign ( -- ) \ gforth
  123: 
  124: \ !! machine-dependent and won't work if "0 >body" <> "0 >body maxaligned"
  125: ' maxaligned Alias cfaligned ( addr1 -- addr2 ) \ gforth
  126: \ the code field is aligned if its body is maxaligned
  127: ' maxalign Alias cfalign ( -- ) \ gforth
  128: 
  129: : chars ( n1 -- n2 ) \ core
  130: ; immediate
  131: 
  132: 
  133: \ : A!    ( addr1 addr2 -- ) \ gforth
  134: \    dup relon ! ;
  135: \ : A,    ( addr -- ) \ gforth
  136: \    here cell allot A! ;
  137: ' ! alias A! ( addr1 addr2 -- ) \ gforth
  138: ' , alias A, ( addr -- ) \ gforth 
  139: 
  140: 
  141: \ on off                                               23feb93py
  142: 
  143: : on  ( addr -- ) \ gforth
  144:     true  swap ! ;
  145: : off ( addr -- ) \ gforth
  146:     false swap ! ;
  147: 
  148: \ dabs roll                                           17may93jaw
  149: 
  150: : dabs ( d1 -- d2 ) \ double
  151:     dup 0< IF dnegate THEN ;
  152: 
  153: : roll  ( x0 x1 .. xn n -- x1 .. xn x0 ) \ core-ext
  154:   dup 1+ pick >r
  155:   cells sp@ cell+ dup cell+ rot move drop r> ;
  156: 
  157: \ name> found                                          17dec92py
  158: 
  159: $80 constant alias-mask \ set when the word is not an alias!
  160: $40 constant immediate-mask
  161: $20 constant restrict-mask
  162: 
  163: : ((name>))  ( nfa -- cfa )
  164:     name>string +  cfaligned ;
  165: 
  166: : (name>x) ( nfa -- cfa b )
  167:     \ cfa is an intermediate cfa and b is the flags byte of nfa
  168:     dup ((name>))
  169:     swap cell+ c@ dup alias-mask and 0=
  170:     IF
  171: 	swap @ swap
  172:     THEN ;
  173: 
  174: \ place bounds                                         13feb93py
  175: 
  176: : place  ( addr len to -- ) \ gforth
  177:     over >r  rot over 1+  r> move c! ;
  178: : bounds ( beg count -- end beg ) \ gforth
  179:     over + swap ;
  180: 
  181: : save-mem	( addr1 u -- addr2 u ) \ gforth
  182:     \ copy a memory block into a newly allocated region in the heap
  183:     swap >r
  184:     dup allocate throw
  185:     swap 2dup r> -rot move ;
  186: 
  187: : extend-mem	( addr1 u1 u -- addr addr2 u2 )
  188:     \ extend memory block allocated from the heap by u aus
  189:     \ the (possibly reallocated piece is addr2 u2, the extension is at addr
  190:     over >r + dup >r resize throw
  191:     r> over r> + -rot ;
  192: 
  193: \ input stream primitives                              23feb93py
  194: 
  195: : tib ( -- c-addr ) \ core-ext
  196:     \ obsolescent
  197:     >tib @ ;
  198: Defer source ( -- addr count ) \ core
  199: \ used by dodefer:, must be defer
  200: : (source) ( -- addr count )
  201:     tib #tib @ ;
  202: ' (source) IS source
  203: 
  204: \ (word)                                               22feb93py
  205: 
  206: : scan   ( addr1 n1 char -- addr2 n2 ) \ gforth
  207:     \ skip all characters not equal to char
  208:     >r
  209:     BEGIN
  210: 	dup
  211:     WHILE
  212: 	over c@ r@ <>
  213:     WHILE
  214: 	1 /string
  215:     REPEAT  THEN
  216:     rdrop ;
  217: : skip   ( addr1 n1 char -- addr2 n2 ) \ gforth
  218:     \ skip all characters equal to char
  219:     >r
  220:     BEGIN
  221: 	dup
  222:     WHILE
  223: 	over c@ r@  =
  224:     WHILE
  225: 	1 /string
  226:     REPEAT  THEN
  227:     rdrop ;
  228: 
  229: : (word) ( addr1 n1 char -- addr2 n2 )
  230:   dup >r skip 2dup r> scan  nip - ;
  231: 
  232: \ (word) should fold white spaces
  233: \ this is what (parse-white) does
  234: 
  235: \ word parse                                           23feb93py
  236: 
  237: : parse-word  ( char -- addr len ) \ gforth
  238:   source 2dup >r >r >in @ over min /string
  239:   rot dup bl = IF  drop (parse-white)  ELSE  (word)  THEN
  240:   2dup + r> - 1+ r> min >in ! ;
  241: : word   ( char -- addr ) \ core
  242:   parse-word here place  bl here count + c!  here ;
  243: 
  244: : parse    ( char -- addr len ) \ core-ext
  245:   >r  source  >in @ over min /string  over  swap r>  scan >r
  246:   over - dup r> IF 1+ THEN  >in +! ;
  247: 
  248: \ name                                                 13feb93py
  249: 
  250: : capitalize ( addr len -- addr len ) \ gforth
  251:   2dup chars chars bounds
  252:   ?DO  I c@ toupper I c! 1 chars +LOOP ;
  253: : (name) ( -- c-addr count )
  254:     source 2dup >r >r >in @ /string (parse-white)
  255:     2dup + r> - 1+ r> min >in ! ;
  256: \    name count ;
  257: 
  258: : name-too-short? ( c-addr u -- c-addr u )
  259:     dup 0= -&16 and throw ;
  260: 
  261: : name-too-long? ( c-addr u -- c-addr u )
  262:     dup $1F u> -&19 and throw ;
  263: 
  264: \ Literal                                              17dec92py
  265: 
  266: : Literal  ( compilation n -- ; run-time -- n ) \ core
  267:     postpone lit  , ; immediate restrict
  268: : ALiteral ( compilation addr -- ; run-time -- addr ) \ gforth
  269:     postpone lit A, ; immediate restrict
  270: 
  271: : char   ( 'char' -- n ) \ core
  272:     bl word char+ c@ ;
  273: : [char] ( compilation 'char' -- ; run-time -- n )
  274:     char postpone Literal ; immediate restrict
  275: 
  276: : (compile) ( -- ) \ gforth
  277:     r> dup cell+ >r @ compile, ;
  278: 
  279: \ not the most efficient implementation of POSTPONE, but simple
  280: : POSTPONE ( -- ) \ core
  281:     COMP' swap POSTPONE aliteral compile, ; immediate restrict
  282: 
  283: : interpret/compile: ( interp-xt comp-xt "name" -- )
  284:     Create immediate swap A, A,
  285: DOES>
  286:     abort" executed primary cfa of an interpret/compile: word" ;
  287: \    state @ IF  cell+  THEN  perform ;
  288: 
  289: \ Use (compile) for the old behavior of compile!
  290: 
  291: \ digit?                                               17dec92py
  292: 
  293: : digit?   ( char -- digit true/ false ) \ gforth
  294:   base @ $100 =
  295:   IF
  296:     true EXIT
  297:   THEN
  298:   toupper [char] 0 - dup 9 u> IF
  299:     [ 'A '9 1 + -  ] literal -
  300:     dup 9 u<= IF
  301:       drop false EXIT
  302:     THEN
  303:   THEN
  304:   dup base @ u>= IF
  305:     drop false EXIT
  306:   THEN
  307:   true ;
  308: 
  309: : accumulate ( +d0 addr digit - +d1 addr )
  310:   swap >r swap  base @  um* drop rot  base @  um* d+ r> ;
  311: 
  312: : >number ( d addr count -- d addr count ) \ core
  313:     0
  314:     ?DO
  315: 	count digit?
  316:     WHILE
  317: 	accumulate
  318:     LOOP
  319:         0
  320:     ELSE
  321: 	1- I' I -
  322: 	UNLOOP
  323:     THEN ;
  324: 
  325: \ number? number                                       23feb93py
  326: 
  327: Create bases   10 ,   2 ,   A , 100 ,
  328: \              16     2    10   Zeichen
  329: \ !! this saving and restoring base is an abomination! - anton
  330: : getbase ( addr u -- addr' u' )
  331:     over c@ [char] $ - dup 4 u<
  332:     IF
  333: 	cells bases + @ base ! 1 /string
  334:     ELSE
  335: 	drop
  336:     THEN ;
  337: : s>number ( addr len -- d )
  338:     base @ >r  dpl on
  339:     over c@ '- =  dup >r
  340:     IF
  341: 	1 /string
  342:     THEN
  343:     getbase  dpl on  0 0 2swap
  344:     BEGIN
  345: 	dup >r >number dup
  346:     WHILE
  347: 	dup r> -
  348:     WHILE
  349: 	dup dpl ! over c@ [char] . =
  350:     WHILE
  351: 	1 /string
  352:     REPEAT  THEN
  353:         2drop rdrop dpl off
  354:     ELSE
  355: 	2drop rdrop r>
  356: 	IF
  357: 	    dnegate
  358: 	THEN
  359:     THEN
  360:     r> base ! ;
  361: 
  362: : snumber? ( c-addr u -- 0 / n -1 / d 0> )
  363:     s>number dpl @ 0=
  364:     IF
  365: 	2drop false  EXIT
  366:     THEN
  367:     dpl @ dup 0> 0= IF
  368: 	nip
  369:     THEN ;
  370: : number? ( string -- string 0 / n -1 / d 0> )
  371:     dup >r count snumber? dup if
  372: 	rdrop
  373:     else
  374: 	r> swap
  375:     then ;
  376: : s>d ( n -- d ) \ core		s-to-d
  377:     dup 0< ;
  378: : number ( string -- d )
  379:     number? ?dup 0= abort" ?"  0<
  380:     IF
  381: 	s>d
  382:     THEN ;
  383: 
  384: \ space spaces ud/mod                                  21mar93py
  385: decimal
  386: Create spaces ( u -- ) \ core
  387: bl 80 times \ times from target compiler! 11may93jaw
  388: DOES>   ( u -- )
  389:     swap
  390:     0 max 0 ?DO  I' I - &80 min 2dup type  +LOOP  drop ;
  391: Create backspaces
  392: 08 80 times \ times from target compiler! 11may93jaw
  393: DOES>   ( u -- )
  394:     swap
  395:     0 max 0 ?DO  I' I - &80 min 2dup type  +LOOP  drop ;
  396: hex
  397: : space ( -- ) \ core
  398:     1 spaces ;
  399: 
  400: : ud/mod ( ud1 u2 -- urem udquot ) \ gforth
  401:     >r 0 r@ um/mod r> swap >r
  402:     um/mod r> ;
  403: 
  404: : pad    ( -- addr ) \ core-ext
  405:   here [ $20 8 2* cells + 2 + cell+ ] Literal + aligned ;
  406: 
  407: \ hold <# #> sign # #s                                 25jan92py
  408: 
  409: : hold    ( char -- ) \ core
  410:     pad cell - -1 chars over +! @ c! ;
  411: 
  412: : <# ( -- ) \ core	less-number-sign
  413:     pad cell - dup ! ;
  414: 
  415: : #>      ( xd -- addr u ) \ core	number-sign-greater
  416:     2drop pad cell - dup @ tuck - ;
  417: 
  418: : sign    ( n -- ) \ core
  419:     0< IF  [char] - hold  THEN ;
  420: 
  421: : #       ( ud1 -- ud2 ) \ core		number-sign
  422:     base @ 2 max ud/mod rot 9 over <
  423:     IF
  424: 	[ char A char 9 - 1- ] Literal +
  425:     THEN
  426:     [char] 0 + hold ;
  427: 
  428: : #s      ( +d -- 0 0 ) \ core	number-sign-s
  429:     BEGIN
  430: 	# 2dup d0=
  431:     UNTIL ;
  432: 
  433: \ print numbers                                        07jun92py
  434: 
  435: : d.r ( d n -- ) \ double	d-dot-r
  436:     >r tuck  dabs  <# #s  rot sign #>
  437:     r> over - spaces  type ;
  438: 
  439: : ud.r ( ud n -- ) \ gforth	u-d-dot-r
  440:     >r <# #s #> r> over - spaces type ;
  441: 
  442: : .r ( n1 n2 -- ) \ core-ext	dot-r
  443:     >r s>d r> d.r ;
  444: : u.r ( u n -- )  \ core-ext	u-dot-r
  445:     0 swap ud.r ;
  446: 
  447: : d. ( d -- ) \ double	d-dot
  448:     0 d.r space ;
  449: : ud. ( ud -- ) \ gforth	u-d-dot
  450:     0 ud.r space ;
  451: 
  452: : . ( n -- ) \ core	dot
  453:     s>d d. ;
  454: : u. ( u -- ) \ core	u-dot
  455:     0 ud. ;
  456: 
  457: \ catch throw                                          23feb93py
  458: \ bounce                                                08jun93jaw
  459: 
  460: \ !! allow the user to add rollback actions    anton
  461: \ !! use a separate exception stack?           anton
  462: 
  463: : lp@ ( -- addr ) \ gforth	l-p-fetch
  464:  laddr# [ 0 , ] ;
  465: 
  466: : catch ( x1 .. xn xt -- y1 .. ym 0 / z1 .. zn error ) \ exception
  467:   >r sp@ r> swap >r       \ don't count xt! jaw
  468:   fp@ >r
  469:   lp@ >r
  470:   handler @ >r
  471:   rp@ handler !
  472:   execute
  473:   r> handler ! rdrop rdrop rdrop 0 ;
  474: 
  475: : throw ( y1 .. ym error/0 -- y1 .. ym / z1 .. zn error ) \ exception
  476:     ?DUP IF
  477: 	[ here 9 cells ! ]
  478: 	handler @ rp!
  479: 	r> handler !
  480: 	r> lp!
  481: 	r> fp!
  482: 	r> swap >r sp! r>
  483:     THEN ;
  484: 
  485: \ Bouncing is very fine,
  486: \ programming without wasting time...   jaw
  487: : bounce ( y1 .. ym error/0 -- y1 .. ym error / y1 .. ym ) \ gforth
  488: \ a throw without data or fp stack restauration
  489:   ?DUP IF
  490:     handler @ rp!
  491:     r> handler !
  492:     r> lp!
  493:     rdrop
  494:     rdrop
  495:   THEN ;
  496: 
  497: \ ?stack                                               23feb93py
  498: 
  499: : ?stack ( ?? -- ?? ) \ gforth
  500:     sp@ s0 @ > IF    -4 throw  THEN
  501:     fp@ f0 @ > IF  -&45 throw  THEN  ;
  502: \ ?stack should be code -- it touches an empty stack!
  503: 
  504: \ interpret                                            10mar92py
  505: 
  506: Defer parser
  507: Defer name ( -- c-addr count ) \ gforth
  508: \ get the next word from the input buffer
  509: ' (name) IS name
  510: Defer compiler-notfound ( c-addr count -- )
  511: Defer interpreter-notfound ( c-addr count -- )
  512: 
  513: : no.extensions  ( addr u -- )
  514:     2drop -&13 bounce ;
  515: ' no.extensions IS compiler-notfound
  516: ' no.extensions IS interpreter-notfound
  517: 
  518: : compile-only-error ( ... -- )
  519:     -&14 throw ;
  520: Defer interpret-special ( c-addr u xt -- ) \ !! use nfa instead of xt?
  521: ' compile-only-error IS interpret-special
  522: 
  523: : interpret ( ?? -- ?? ) \ gforth
  524:     \ interpret/compile the (rest of the) input buffer
  525:     BEGIN
  526: 	?stack name dup
  527:     WHILE
  528: 	parser
  529:     REPEAT
  530:     2drop ;
  531: 
  532: \ interpreter compiler                                 30apr92py
  533: 
  534: \ not the most efficient implementations of interpreter and compiler
  535: : interpreter ( c-addr u -- ) 
  536:     2dup find-name dup
  537:     if
  538: 	nip nip name>int execute
  539:     else
  540: 	drop
  541: 	2dup 2>r snumber?
  542: 	IF
  543: 	    2rdrop
  544: 	ELSE
  545: 	    2r> interpreter-notfound
  546: 	THEN
  547:     then ;
  548: 
  549: : compiler ( c-addr u -- )
  550:     2dup find-name dup
  551:     if ( c-addr u nfa )
  552: 	nip nip name>comp execute
  553:     else
  554: 	drop
  555: 	2dup snumber? dup
  556: 	IF
  557: 	    0>
  558: 	    IF
  559: 		swap postpone Literal
  560: 	    THEN
  561: 	    postpone Literal
  562: 	    2drop
  563: 	ELSE
  564: 	    drop compiler-notfound
  565: 	THEN
  566:     then ;
  567: 
  568: ' interpreter  IS  parser
  569: 
  570: : [ ( -- ) \ core	left-bracket
  571:     ['] interpreter  IS parser state off ; immediate
  572: : ] ( -- ) \ core	right-bracket
  573:     ['] compiler     IS parser state on  ;
  574: 
  575: \ locals stuff needed for control structures
  576: 
  577: : compile-lp+! ( n -- ) \ gforth	compile-l-p-plus-store
  578:     dup negate locals-size +!
  579:     0 over = if
  580:     else -1 cells  over = if postpone lp-
  581:     else  1 floats over = if postpone lp+
  582:     else  2 floats over = if postpone lp+2
  583:     else postpone lp+!# dup ,
  584:     then then then then drop ;
  585: 
  586: : adjust-locals-size ( n -- ) \ gforth
  587:     \ sets locals-size to n and generates an appropriate lp+!
  588:     locals-size @ swap - compile-lp+! ;
  589: 
  590: 
  591: here 0 , \ just a dummy, the real value of locals-list is patched into it in glocals.fs
  592: AConstant locals-list \ acts like a variable that contains
  593: 		      \ a linear list of locals names
  594: 
  595: 
  596: variable dead-code \ true if normal code at "here" would be dead
  597: variable backedge-locals
  598:     \ contains the locals list that BEGIN will assume to be live on
  599:     \ the back edge if the BEGIN is unreachable from above. Set by
  600:     \ ASSUME-LIVE, reset by UNREACHABLE.
  601: 
  602: : UNREACHABLE ( -- ) \ gforth
  603:     \ declares the current point of execution as unreachable
  604:     dead-code on
  605:     0 backedge-locals ! ; immediate
  606: 
  607: : ASSUME-LIVE ( orig -- orig ) \ gforth
  608:     \ used immediateliy before a BEGIN that is not reachable from
  609:     \ above.  causes the BEGIN to assume that the same locals are live
  610:     \ as at the orig point
  611:     dup orig?
  612:     2 pick backedge-locals ! ; immediate
  613:     
  614: \ locals list operations
  615: 
  616: : common-list ( list1 list2 -- list3 ) \ gforth-internal
  617: \ list1 and list2 are lists, where the heads are at higher addresses than
  618: \ the tail. list3 is the largest sublist of both lists.
  619:  begin
  620:    2dup u<>
  621:  while
  622:    2dup u>
  623:    if
  624:      swap
  625:    then
  626:    @
  627:  repeat
  628:  drop ;
  629: 
  630: : sub-list? ( list1 list2 -- f ) \ gforth-internal
  631: \ true iff list1 is a sublist of list2
  632:  begin
  633:    2dup u<
  634:  while
  635:    @
  636:  repeat
  637:  = ;
  638: 
  639: : list-size ( list -- u ) \ gforth-internal
  640: \ size of the locals frame represented by list
  641:  0 ( list n )
  642:  begin
  643:    over 0<>
  644:  while
  645:    over
  646:    ((name>)) >body @ max
  647:    swap @ swap ( get next )
  648:  repeat
  649:  faligned nip ;
  650: 
  651: : set-locals-size-list ( list -- )
  652:  dup locals-list !
  653:  list-size locals-size ! ;
  654: 
  655: : check-begin ( list -- )
  656: \ warn if list is not a sublist of locals-list
  657:  locals-list @ sub-list? 0= if
  658:    \ !! print current position
  659:    ." compiler was overly optimistic about locals at a BEGIN" cr
  660:    \ !! print assumption and reality
  661:  then ;
  662: 
  663: \ Control Flow Stack
  664: \ orig, etc. have the following structure:
  665: \ type ( defstart, live-orig, dead-orig, dest, do-dest, scopestart) ( TOS )
  666: \ address (of the branch or the instruction to be branched to) (second)
  667: \ locals-list (valid at address) (third)
  668: 
  669: \ types
  670: 0 constant defstart
  671: 1 constant live-orig
  672: 2 constant dead-orig
  673: 3 constant dest \ the loopback branch is always assumed live
  674: 4 constant do-dest
  675: 5 constant scopestart
  676: 
  677: : def? ( n -- )
  678:     defstart <> abort" unstructured " ;
  679: 
  680: : orig? ( n -- )
  681:  dup live-orig <> swap dead-orig <> and abort" expected orig " ;
  682: 
  683: : dest? ( n -- )
  684:  dest <> abort" expected dest " ;
  685: 
  686: : do-dest? ( n -- )
  687:  do-dest <> abort" expected do-dest " ;
  688: 
  689: : scope? ( n -- )
  690:  scopestart <> abort" expected scope " ;
  691: 
  692: : non-orig? ( n -- )
  693:  dest scopestart 1+ within 0= abort" expected dest, do-dest or scope" ;
  694: 
  695: : cs-item? ( n -- )
  696:  live-orig scopestart 1+ within 0= abort" expected control flow stack item" ;
  697: 
  698: 3 constant cs-item-size
  699: 
  700: : CS-PICK ( ... u -- ... destu ) \ tools-ext
  701:  1+ cs-item-size * 1- >r
  702:  r@ pick  r@ pick  r@ pick
  703:  rdrop
  704:  dup non-orig? ;
  705: 
  706: : CS-ROLL ( destu/origu .. dest0/orig0 u -- .. dest0/orig0 destu/origu ) \ tools-ext
  707:  1+ cs-item-size * 1- >r
  708:  r@ roll r@ roll r@ roll
  709:  rdrop
  710:  dup cs-item? ; 
  711: 
  712: : cs-push-part ( -- list addr )
  713:  locals-list @ here ;
  714: 
  715: : cs-push-orig ( -- orig )
  716:  cs-push-part dead-code @
  717:  if
  718:    dead-orig
  719:  else
  720:    live-orig
  721:  then ;   
  722: 
  723: \ Structural Conditionals                              12dec92py
  724: 
  725: : ?struc      ( flag -- )       abort" unstructured " ;
  726: : sys?        ( sys -- )        dup 0= ?struc ;
  727: : >mark ( -- orig )
  728:  cs-push-orig 0 , ;
  729: : >resolve    ( addr -- )        here over - swap ! ;
  730: : <resolve    ( addr -- )        here - , ;
  731: 
  732: : BUT
  733:     1 cs-roll ;                      immediate restrict
  734: : YET
  735:     0 cs-pick ;                       immediate restrict
  736: 
  737: \ Structural Conditionals                              12dec92py
  738: 
  739: : AHEAD ( compilation -- orig ; run-time -- ) \ tools-ext
  740:     POSTPONE branch  >mark  POSTPONE unreachable ; immediate restrict
  741: 
  742: : IF ( compilation -- orig ; run-time f -- ) \ core
  743:  POSTPONE ?branch >mark ; immediate restrict
  744: 
  745: : ?DUP-IF ( compilation -- orig ; run-time n -- n| ) \ gforth	question-dupe-if
  746: \ This is the preferred alternative to the idiom "?DUP IF", since it can be
  747: \ better handled by tools like stack checkers. Besides, it's faster.
  748:     POSTPONE ?dup-?branch >mark ;       immediate restrict
  749: 
  750: : ?DUP-0=-IF ( compilation -- orig ; run-time n -- n| ) \ gforth	question-dupe-zero-equals-if
  751:     POSTPONE ?dup-0=-?branch >mark ;       immediate restrict
  752: 
  753: : then-like ( orig -- addr )
  754:     swap -rot dead-orig =
  755:     if
  756:         drop
  757:     else
  758:         dead-code @
  759:         if
  760: 	    set-locals-size-list dead-code off
  761: 	else \ both live
  762: 	    dup list-size adjust-locals-size
  763: 	    locals-list @ common-list dup list-size adjust-locals-size
  764: 	    locals-list !
  765: 	then
  766:     then ;
  767: 
  768: : THEN ( compilation orig -- ; run-time -- ) \ core
  769:     dup orig?  then-like  >resolve ; immediate restrict
  770: 
  771: ' THEN alias ENDIF ( compilation orig -- ; run-time -- ) \ gforth
  772: immediate restrict
  773: \ Same as "THEN". This is what you use if your program will be seen by
  774: \ people who have not been brought up with Forth (or who have been
  775: \ brought up with fig-Forth).
  776: 
  777: : ELSE ( compilation orig1 -- orig2 ; run-time f -- ) \ core
  778:     POSTPONE ahead
  779:     1 cs-roll
  780:     POSTPONE then ; immediate restrict
  781: 
  782: 
  783: : BEGIN ( compilation -- dest ; run-time -- ) \ core
  784:     dead-code @ if
  785: 	\ set up an assumption of the locals visible here.  if the
  786: 	\ users want something to be visible, they have to declare
  787: 	\ that using ASSUME-LIVE
  788: 	backedge-locals @ set-locals-size-list
  789:     then
  790:     cs-push-part dest
  791:     dead-code off ; immediate restrict
  792: 
  793: \ AGAIN (the current control flow joins another, earlier one):
  794: \ If the dest-locals-list is not a subset of the current locals-list,
  795: \ issue a warning (see below). The following code is generated:
  796: \ lp+!# (current-local-size - dest-locals-size)
  797: \ branch <begin>
  798: 
  799: : again-like ( dest -- addr )
  800:     over list-size adjust-locals-size
  801:     swap check-begin  POSTPONE unreachable ;
  802: 
  803: : AGAIN ( compilation dest -- ; run-time -- ) \ core-ext
  804:     dest? again-like  POSTPONE branch  <resolve ; immediate restrict
  805: 
  806: \ UNTIL (the current control flow may join an earlier one or continue):
  807: \ Similar to AGAIN. The new locals-list and locals-size are the current
  808: \ ones. The following code is generated:
  809: \ ?branch-lp+!# <begin> (current-local-size - dest-locals-size)
  810: : until-like ( list addr xt1 xt2 -- )
  811:     \ list and addr are a fragment of a cs-item
  812:     \ xt1 is the conditional branch without lp adjustment, xt2 is with
  813:     >r >r
  814:     locals-size @ 2 pick list-size - dup if ( list dest-addr adjustment )
  815: 	r> drop r> compile,
  816: 	swap <resolve ( list adjustment ) ,
  817:     else ( list dest-addr adjustment )
  818: 	drop
  819: 	r> compile, <resolve
  820: 	r> drop
  821:     then ( list )
  822:     check-begin ;
  823: 
  824: : UNTIL ( compilation dest -- ; run-time f -- ) \ core
  825:     dest? ['] ?branch ['] ?branch-lp+!# until-like ; immediate restrict
  826: 
  827: : WHILE ( compilation dest -- orig dest ; run-time f -- ) \ core
  828:     POSTPONE if
  829:     1 cs-roll ; immediate restrict
  830: 
  831: : REPEAT ( compilation orig dest -- ; run-time -- ) \ core
  832:     POSTPONE again
  833:     POSTPONE then ; immediate restrict
  834: 
  835: 
  836: \ counted loops
  837: 
  838: \ leave poses a little problem here
  839: \ we have to store more than just the address of the branch, so the
  840: \ traditional linked list approach is no longer viable.
  841: \ This is solved by storing the information about the leavings in a
  842: \ special stack.
  843: 
  844: \ !! remove the fixed size limit. 'Tis not hard.
  845: 20 constant leave-stack-size
  846: create leave-stack  60 cells allot
  847: Avariable leave-sp  leave-stack 3 cells + leave-sp !
  848: 
  849: : clear-leave-stack ( -- )
  850:     leave-stack leave-sp ! ;
  851: 
  852: \ : leave-empty? ( -- f )
  853: \  leave-sp @ leave-stack = ;
  854: 
  855: : >leave ( orig -- )
  856:     \ push on leave-stack
  857:     leave-sp @
  858:     dup [ leave-stack 60 cells + ] Aliteral
  859:     >= abort" leave-stack full"
  860:     tuck ! cell+
  861:     tuck ! cell+
  862:     tuck ! cell+
  863:     leave-sp ! ;
  864: 
  865: : leave> ( -- orig )
  866:     \ pop from leave-stack
  867:     leave-sp @
  868:     dup leave-stack <= IF
  869:        drop 0 0 0  EXIT  THEN
  870:     cell - dup @ swap
  871:     cell - dup @ swap
  872:     cell - dup @ swap
  873:     leave-sp ! ;
  874: 
  875: : DONE ( compilation orig -- ; run-time -- ) \ gforth
  876:     \ !! the original done had ( addr -- )
  877:     drop >r drop
  878:     begin
  879: 	leave>
  880: 	over r@ u>=
  881:     while
  882: 	POSTPONE then
  883:     repeat
  884:     >leave rdrop ; immediate restrict
  885: 
  886: : LEAVE ( compilation -- ; run-time loop-sys -- ) \ core
  887:     POSTPONE ahead
  888:     >leave ; immediate restrict
  889: 
  890: : ?LEAVE ( compilation -- ; run-time f | f loop-sys -- ) \ gforth	question-leave
  891:     POSTPONE 0= POSTPONE if
  892:     >leave ; immediate restrict
  893: 
  894: : DO ( compilation -- do-sys ; run-time w1 w2 -- loop-sys ) \ core
  895:     POSTPONE (do)
  896:     POSTPONE begin drop do-dest
  897:     ( 0 0 0 >leave ) ; immediate restrict
  898: 
  899: : ?do-like ( -- do-sys )
  900:     ( 0 0 0 >leave )
  901:     >mark >leave
  902:     POSTPONE begin drop do-dest ;
  903: 
  904: : ?DO ( compilation -- do-sys ; run-time w1 w2 -- | loop-sys )	\ core-ext	question-do
  905:     POSTPONE (?do) ?do-like ; immediate restrict
  906: 
  907: : +DO ( compilation -- do-sys ; run-time n1 n2 -- | loop-sys )	\ gforth	plus-do
  908:     POSTPONE (+do) ?do-like ; immediate restrict
  909: 
  910: : U+DO ( compilation -- do-sys ; run-time u1 u2 -- | loop-sys )	\ gforth	u-plus-do
  911:     POSTPONE (u+do) ?do-like ; immediate restrict
  912: 
  913: : -DO ( compilation -- do-sys ; run-time n1 n2 -- | loop-sys )	\ gforth	minus-do
  914:     POSTPONE (-do) ?do-like ; immediate restrict
  915: 
  916: : U-DO ( compilation -- do-sys ; run-time u1 u2 -- | loop-sys )	\ gforth	u-minus-do
  917:     POSTPONE (u-do) ?do-like ; immediate restrict
  918: 
  919: : FOR ( compilation -- do-sys ; run-time u -- loop-sys )	\ gforth
  920:     POSTPONE (for)
  921:     POSTPONE begin drop do-dest
  922:     ( 0 0 0 >leave ) ; immediate restrict
  923: 
  924: \ LOOP etc. are just like UNTIL
  925: 
  926: : loop-like ( do-sys xt1 xt2 -- )
  927:     >r >r 0 cs-pick swap cell - swap 1 cs-roll r> r> rot do-dest?
  928:     until-like  POSTPONE done  POSTPONE unloop ;
  929: 
  930: : LOOP ( compilation do-sys -- ; run-time loop-sys1 -- | loop-sys2 )	\ core
  931:  ['] (loop) ['] (loop)-lp+!# loop-like ; immediate restrict
  932: 
  933: : +LOOP ( compilation do-sys -- ; run-time loop-sys1 n -- | loop-sys2 )	\ core	plus-loop
  934:  ['] (+loop) ['] (+loop)-lp+!# loop-like ; immediate restrict
  935: 
  936: \ !! should the compiler warn about +DO..-LOOP?
  937: : -LOOP ( compilation do-sys -- ; run-time loop-sys1 u -- | loop-sys2 )	\ gforth	minus-loop
  938:  ['] (-loop) ['] (-loop)-lp+!# loop-like ; immediate restrict
  939: 
  940: \ A symmetric version of "+LOOP". I.e., "-high -low ?DO -inc S+LOOP"
  941: \ will iterate as often as "high low ?DO inc S+LOOP". For positive
  942: \ increments it behaves like "+LOOP". Use S+LOOP instead of +LOOP for
  943: \ negative increments.
  944: : S+LOOP ( compilation do-sys -- ; run-time loop-sys1 n -- | loop-sys2 )	\ gforth	s-plus-loop
  945:  ['] (s+loop) ['] (s+loop)-lp+!# loop-like ; immediate restrict
  946: 
  947: : NEXT ( compilation do-sys -- ; run-time loop-sys1 -- | loop-sys2 ) \ gforth
  948:  ['] (next) ['] (next)-lp+!# loop-like ; immediate restrict
  949: 
  950: \ Structural Conditionals                              12dec92py
  951: 
  952: : EXIT ( compilation -- ; run-time nest-sys -- ) \ core
  953:     0 adjust-locals-size
  954:     POSTPONE ;s
  955:     POSTPONE unreachable ; immediate restrict
  956: 
  957: : ?EXIT ( -- ) ( compilation -- ; run-time nest-sys f -- | nest-sys ) \ gforth
  958:      POSTPONE if POSTPONE exit POSTPONE then ; immediate restrict
  959: 
  960: \ Strings                                              22feb93py
  961: 
  962: : ," ( "string"<"> -- ) [char] " parse
  963:   here over char+ allot  place align ;
  964: : "lit ( -- addr )
  965:   r> r> dup count + aligned >r swap >r ;               restrict
  966: : (.")     "lit count type ;                           restrict
  967: : (S")     "lit count ;                                restrict
  968: : SLiteral ( Compilation c-addr1 u ; run-time -- c-addr2 u ) \ string
  969:     postpone (S") here over char+ allot  place align ;
  970:                                              immediate restrict
  971: : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ core,file	paren
  972:     BEGIN
  973: 	>in @ [char] ) parse nip >in @ rot - =
  974:     WHILE
  975: 	loadfile @ IF
  976: 	    refill 0= abort" missing ')' in paren comment"
  977: 	THEN
  978:     REPEAT ;                       immediate
  979: : \ ( -- ) \ core-ext backslash
  980:     blk @
  981:     IF
  982: 	>in @ c/l / 1+ c/l * >in !
  983: 	EXIT
  984:     THEN
  985:     source >in ! drop ; immediate
  986: 
  987: : \G ( -- ) \ gforth backslash
  988:     POSTPONE \ ; immediate
  989: 
  990: \ error handling                                       22feb93py
  991: \ 'abort thrown out!                                   11may93jaw
  992: 
  993: : (abort")
  994:     "lit >r
  995:     IF
  996: 	r> "error ! -2 throw
  997:     THEN
  998:     rdrop ;
  999: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext	abort-quote
 1000:     postpone (abort") ," ;        immediate restrict
 1001: 
 1002: \ Header states                                        23feb93py
 1003: 
 1004: : cset ( bmask c-addr -- )
 1005:     tuck c@ or swap c! ; 
 1006: : creset ( bmask c-addr -- )
 1007:     tuck c@ swap invert and swap c! ; 
 1008: : ctoggle ( bmask c-addr -- )
 1009:     tuck c@ xor swap c! ; 
 1010: 
 1011: : lastflags ( -- c-addr )
 1012:     \ the address of the flags byte in the last header
 1013:     \ aborts if the last defined word was headerless
 1014:     last @ dup 0= abort" last word was headerless" cell+ ;
 1015: 
 1016: : immediate     immediate-mask lastflags cset ;
 1017: : restrict      restrict-mask lastflags cset ;
 1018: 
 1019: \ Header                                               23feb93py
 1020: 
 1021: \ input-stream, nextname and noname are quite ugly (passing
 1022: \ information through global variables), but they are useful for dealing
 1023: \ with existing/independent defining words
 1024: 
 1025: defer (header)
 1026: defer header ( -- ) \ gforth
 1027: ' (header) IS header
 1028: 
 1029: : string, ( c-addr u -- ) \ gforth
 1030:     \ puts down string as cstring
 1031:     dup c, here swap chars dup allot move ;
 1032: 
 1033: : header, ( c-addr u -- ) \ gforth
 1034:     name-too-long?
 1035:     align here last !
 1036:     current @ 1 or A,	\ link field; before revealing, it contains the
 1037: 			\ tagged reveal-into wordlist
 1038:     string, cfalign
 1039:     alias-mask lastflags cset ;
 1040: 
 1041: : input-stream-header ( "name" -- )
 1042:     name name-too-short? header, ;
 1043: : input-stream ( -- )  \ general
 1044: \ switches back to getting the name from the input stream ;
 1045:     ['] input-stream-header IS (header) ;
 1046: 
 1047: ' input-stream-header IS (header)
 1048: 
 1049: \ !! make that a 2variable
 1050: create nextname-buffer 32 chars allot
 1051: 
 1052: : nextname-header ( -- )
 1053:     nextname-buffer count header,
 1054:     input-stream ;
 1055: 
 1056: \ the next name is given in the string
 1057: : nextname ( c-addr u -- ) \ gforth
 1058:     name-too-long?
 1059:     nextname-buffer c! ( c-addr )
 1060:     nextname-buffer count move
 1061:     ['] nextname-header IS (header) ;
 1062: 
 1063: : noname-header ( -- )
 1064:     0 last ! cfalign
 1065:     input-stream ;
 1066: 
 1067: : noname ( -- ) \ gforth
 1068: \ the next defined word remains anonymous. The xt of that word is given by lastxt
 1069:     ['] noname-header IS (header) ;
 1070: 
 1071: : lastxt ( -- xt ) \ gforth
 1072: \ xt is the execution token of the last word defined. The main purpose of this word is to get the xt of words defined using noname
 1073:     lastcfa @ ;
 1074: 
 1075: : Alias    ( cfa "name" -- ) \ gforth
 1076:     Header reveal
 1077:     alias-mask lastflags creset
 1078:     dup A, lastcfa ! ;
 1079: 
 1080: : name>string ( nfa -- addr count ) \ gforth	name-to-string
 1081:  cell+ count $1F and ;
 1082: 
 1083: Create ???  0 , 3 c, char ? c, char ? c, char ? c,
 1084: : >name ( cfa -- nfa ) \ gforth	to-name
 1085:  $21 cell do
 1086:    dup i - count $9F and + cfaligned over alias-mask + = if
 1087:      i - cell - unloop exit
 1088:    then
 1089:  cell +loop
 1090:  drop ??? ( wouldn't 0 be better? ) ;
 1091: 
 1092: \ threading                                   17mar93py
 1093: 
 1094: : cfa,     ( code-address -- )  \ gforth	cfa-comma
 1095:     here
 1096:     dup lastcfa !
 1097:     0 A, 0 ,  code-address! ;
 1098: : compile, ( xt -- )	\ core-ext	compile-comma
 1099:     A, ;
 1100: : !does    ( addr -- ) \ gforth	store-does
 1101:     lastxt does-code! ;
 1102: : (does>)  ( R: addr -- )
 1103:     r> /does-handler + !does ;
 1104: : dodoes,  ( -- )
 1105:   here /does-handler allot does-handler! ;
 1106: 
 1107: : Create ( -- ) \ core
 1108:     Header reveal dovar: cfa, ;
 1109: 
 1110: \ Create Variable User Constant                        17mar93py
 1111: 
 1112: : Variable ( -- ) \ core
 1113:     Create 0 , ;
 1114: : AVariable ( -- ) \ gforth
 1115:     Create 0 A, ;
 1116: : 2VARIABLE ( "name" -- ) \ double
 1117:     create 0 , 0 , ;
 1118:     
 1119: : User
 1120:     Variable ;
 1121: : AUser
 1122:     AVariable ;
 1123: 
 1124: : (Constant)  Header reveal docon: cfa, ;
 1125: : Constant ( w -- ) \ core
 1126:     (Constant) , ;
 1127: : AConstant ( addr -- ) \ gforth
 1128:     (Constant) A, ;
 1129: 
 1130: : 2Constant ( d -- ) \ double
 1131:     Create ( w1 w2 "name" -- )
 1132:         2,
 1133:     DOES> ( -- w1 w2 )
 1134:         2@ ;
 1135:     
 1136: \ IS Defer What's Defers TO                            24feb93py
 1137: 
 1138: : Defer ( -- ) \ gforth
 1139:     \ !! shouldn't it be initialized with abort or something similar?
 1140:     Header Reveal dodefer: cfa,
 1141:     ['] noop A, ;
 1142: \     Create ( -- ) 
 1143: \ 	['] noop A,
 1144: \     DOES> ( ??? )
 1145: \ 	perform ;
 1146: 
 1147: : Defers ( "name" -- ) \ gforth
 1148:     ' >body @ compile, ; immediate
 1149: 
 1150: \ : ;                                                  24feb93py
 1151: 
 1152: defer :-hook ( sys1 -- sys2 )
 1153: defer ;-hook ( sys2 -- sys1 )
 1154: 
 1155: : : ( -- colon-sys ) \ core	colon
 1156:     Header docol: cfa, defstart ] :-hook ;
 1157: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core	semicolon
 1158:     ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
 1159: 
 1160: : :noname ( -- xt colon-sys ) \ core-ext	colon-no-name
 1161:     0 last !
 1162:     here docol: cfa, 0 ] :-hook ;
 1163: 
 1164: \ Search list handling                                 23feb93py
 1165: 
 1166: AVariable current ( -- addr ) \ gforth
 1167: 
 1168: : last?   ( -- false / nfa nfa )
 1169:     last @ ?dup ;
 1170: : (reveal) ( nfa wid -- )
 1171:     ( wid>wordlist-id ) dup >r
 1172:     @ over ( name>link ) ! 
 1173:     r> ! ;
 1174: 
 1175: \ object oriented search list                          17mar93py
 1176: 
 1177: \ word list structure:
 1178: 
 1179: struct
 1180:   1 cells: field find-method   \ xt: ( c_addr u wid -- name-id )
 1181:   1 cells: field reveal-method \ xt: ( nfa wid -- ) \ used by dofield:, must be field
 1182:   1 cells: field rehash-method \ xt: ( wid -- )
 1183: \   \ !! what else
 1184: end-struct wordlist-map-struct
 1185: 
 1186: struct
 1187:   1 cells: field wordlist-id \ not the same as wid; representation depends on implementation
 1188:   1 cells: field wordlist-map \ pointer to a wordlist-map-struct
 1189:   1 cells: field wordlist-link \ link field to other wordlists
 1190:   1 cells: field wordlist-extend \ points to wordlist extensions (eg hash)
 1191: end-struct wordlist-struct
 1192: 
 1193: : f83find      ( addr len wordlist -- nfa / false )
 1194:     ( wid>wordlist-id ) @ (f83find) ;
 1195: 
 1196: \ Search list table: find reveal
 1197: Create f83search ( -- wordlist-map )
 1198:     ' f83find A,  ' (reveal) A,  ' drop A,
 1199: 
 1200: Create forth-wordlist  NIL A, G f83search T A, NIL A, NIL A,
 1201: AVariable lookup       G forth-wordlist lookup T !
 1202: G forth-wordlist current T !
 1203: 
 1204: \ higher level parts of find
 1205: 
 1206: ( struct )
 1207: 0 >body cell
 1208:   1 cells: field special-interpretation
 1209:   1 cells: field special-compilation
 1210: end-struct special-struct
 1211: 
 1212: : interpret/compile? ( xt -- flag )
 1213:     >does-code ['] S" >does-code = ;
 1214: 
 1215: : (x>int) ( cfa b -- xt )
 1216:     \ get interpretation semantics of name
 1217:     restrict-mask and
 1218:     if
 1219: 	drop ['] compile-only-error
 1220:     else
 1221: 	dup interpret/compile?
 1222: 	if
 1223: 	    special-interpretation @
 1224: 	then
 1225:     then ;
 1226: 
 1227: : name>int ( nfa -- xt ) \ gforth
 1228:     (name>x) (x>int) ;
 1229: 
 1230: : name>comp ( nfa -- w xt ) \ gforth
 1231:     \ get compilation semantics of name
 1232:     (name>x) >r dup interpret/compile?
 1233:     if
 1234: 	special-compilation @
 1235:     then
 1236:     r> immediate-mask and if
 1237: 	['] execute
 1238:     else
 1239: 	['] compile,
 1240:     then ;
 1241: 
 1242: : (search-wordlist)  ( addr count wid -- nfa / false )
 1243:     dup wordlist-map @ find-method perform ;
 1244: 
 1245: : flag-sign ( f -- 1|-1 )
 1246:     \ true becomes 1, false -1
 1247:     0= 2* 1+ ;
 1248: 
 1249: : search-wordlist ( addr count wid -- 0 / xt +-1 ) \ search
 1250:    \ xt is the interpretation semantics
 1251:     (search-wordlist) dup if
 1252: 	(name>x) tuck (x>int) ( b xt )
 1253: 	swap immediate-mask and flag-sign
 1254:     then ;
 1255: 
 1256: : find-name ( c-addr u -- nfa/0 )
 1257:     lookup @ (search-wordlist) ;
 1258: 
 1259: : sfind ( c-addr u -- 0 / xt +-1  ) \ gforth-obsolete
 1260:     find-name dup
 1261:     if ( nfa )
 1262: 	state @
 1263: 	if
 1264: 	    name>comp ['] execute =
 1265: 	else
 1266: 	    (name>x) tuck (x>int)
 1267: 	    swap immediate-mask and
 1268: 	then
 1269: 	flag-sign
 1270:     then ;
 1271: 
 1272: : find ( c-addr -- xt +-1 / c-addr 0 ) \ core
 1273:     dup count sfind dup
 1274:     if
 1275: 	rot drop
 1276:     then ;
 1277: 
 1278: : (') ( "name" -- nfa ) \ gforth
 1279:     name find-name dup 0=
 1280:     IF
 1281: 	drop -&13 bounce
 1282:     THEN  ;
 1283: 
 1284: : [(')]  ( compilation "name" -- ; run-time -- nfa ) \ gforth	bracket-paren-tick
 1285:     (') postpone ALiteral ; immediate restrict
 1286: 
 1287: : '    ( "name" -- xt ) \ core	tick
 1288:     (') name>int ;
 1289: : [']  ( compilation "name" -- ; run-time -- xt ) \ core	bracket-tick
 1290:     ' postpone ALiteral ; immediate restrict
 1291: 
 1292: : COMP'    ( "name" -- w xt ) \ gforth	c-tick
 1293:     (') name>comp ;
 1294: : [COMP']  ( compilation "name" -- ; run-time -- w xt ) \ gforth	bracket-comp-tick
 1295:     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
 1296: 
 1297: \ reveal words
 1298: 
 1299: Variable warnings ( -- addr ) \ gforth
 1300: G -1 warnings T !
 1301: 
 1302: : check-shadow  ( addr count wid -- )
 1303: \ prints a warning if the string is already present in the wordlist
 1304:  >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
 1305:    ." redefined " name>string 2dup type
 1306:    compare 0<> if
 1307:      ."  with " type
 1308:    else
 1309:      2drop
 1310:    then
 1311:    space space EXIT
 1312:  then
 1313:  2drop 2drop ;
 1314: 
 1315: : reveal ( -- ) \ gforth
 1316:     last?
 1317:     if \ the last word has a header
 1318: 	dup ( name>link ) @ 1 and
 1319: 	if \ it is still hidden
 1320: 	    dup ( name>link ) @ 1 xor		( nfa wid )
 1321: 	    2dup >r name>string r> check-shadow ( nfa wid )
 1322: 	    dup wordlist-map @ reveal-method perform
 1323: 	then
 1324:     then ;
 1325: 
 1326: : rehash  ( wid -- )
 1327:     dup wordlist-map @ rehash-method perform ;
 1328: 
 1329: \ Input                                                13feb93py
 1330: 
 1331: 07 constant #bell ( -- c ) \ gforth
 1332: 08 constant #bs ( -- c ) \ gforth
 1333: 09 constant #tab ( -- c ) \ gforth
 1334: 7F constant #del ( -- c ) \ gforth
 1335: 0D constant #cr   ( -- c ) \ gforth
 1336: \ the newline key code
 1337: 0C constant #ff ( -- c ) \ gforth
 1338: 0A constant #lf ( -- c ) \ gforth
 1339: 
 1340: : bell  #bell emit ;
 1341: : cr ( -- ) \ core
 1342:     \ emit a newline
 1343:     #lf ( sic! ) emit ;
 1344: 
 1345: \ : backspaces  0 ?DO  #bs emit  LOOP ;
 1346: 
 1347: : (ins) ( max span addr pos1 key -- max span addr pos2 )
 1348:     >r 2dup + r@ swap c! r> emit 1+ rot 1+ -rot ;
 1349: : (bs) ( max span addr pos1 -- max span addr pos2 flag )
 1350:     dup IF
 1351: 	#bs emit bl emit #bs emit 1- rot 1- -rot
 1352:     THEN false ;
 1353: : (ret)  true space ;
 1354: 
 1355: Create ctrlkeys
 1356:   ] false false false false  false false false false
 1357:     (bs)  false (ret) false  false (ret) false false
 1358:     false false false false  false false false false
 1359:     false false false false  false false false false [
 1360: 
 1361: defer insert-char
 1362: ' (ins) IS insert-char
 1363: defer everychar
 1364: ' noop IS everychar
 1365: 
 1366: : decode ( max span addr pos1 key -- max span addr pos2 flag )
 1367:   everychar
 1368:   dup #del = IF  drop #bs  THEN  \ del is rubout
 1369:   dup bl <   IF  cells ctrlkeys + perform  EXIT  THEN
 1370:   >r 2over = IF  rdrop bell 0 EXIT  THEN
 1371:   r> insert-char 0 ;
 1372: 
 1373: : accept   ( addr len -- len ) \ core
 1374:   dup 0< IF    abs over dup 1 chars - c@ tuck type 
 1375: \ this allows to edit given strings
 1376:          ELSE  0  THEN rot over
 1377:   BEGIN  key decode  UNTIL
 1378:   2drop nip ;
 1379: 
 1380: \ Output                                               13feb93py
 1381: 
 1382: : (type) ( c-addr u -- ) \ gforth
 1383:     outfile-id write-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
 1384: ;
 1385: 
 1386: Defer type ( c-addr u -- ) \ core
 1387: \ defer type for a output buffer or fast
 1388: \ screen write
 1389: 
 1390: ' (type) IS Type
 1391: 
 1392: : (emit) ( c -- ) \ gforth
 1393:     outfile-id emit-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
 1394: ;
 1395: 
 1396: Defer emit ( c -- ) \ core
 1397: ' (Emit) IS Emit
 1398: 
 1399: Defer key ( -- c ) \ core
 1400: ' (key) IS key
 1401: 
 1402: \ Query                                                07apr93py
 1403: 
 1404: : refill ( -- flag ) \ core-ext,block-ext,file-ext
 1405:   blk @  IF  1 blk +!  true  0 >in !  EXIT  THEN
 1406:   tib /line
 1407:   loadfile @ ?dup
 1408:   IF    read-line throw
 1409:   ELSE  sourceline# 0< IF 2drop false EXIT THEN
 1410:         accept true
 1411:   THEN
 1412:   1 loadline +!
 1413:   swap #tib ! 0 >in ! ;
 1414: 
 1415: : Query  ( -- ) \ core-ext
 1416:     \ obsolescent
 1417:     loadfile off  blk off  refill drop ;
 1418: 
 1419: \ File specifiers                                       11jun93jaw
 1420: 
 1421: 
 1422: \ 1 c, here char r c, 0 c,                0 c, 0 c, char b c, 0 c,
 1423: \ 2 c, here char r c, char + c, 0 c,
 1424: \ 2 c, here char w c, char + c, 0 c, align
 1425: 4 Constant w/o ( -- fam ) \ file	w-o
 1426: 2 Constant r/w ( -- fam ) \ file	r-w
 1427: 0 Constant r/o ( -- fam ) \ file	r-o
 1428: 
 1429: \ BIN WRITE-LINE                                        11jun93jaw
 1430: 
 1431: \ : bin           dup 1 chars - c@
 1432: \                 r/o 4 chars + over - dup >r swap move r> ;
 1433: 
 1434: : bin ( fam1 -- fam2 ) \ file
 1435:     1 or ;
 1436: 
 1437: create nl$ 1 c, A c, 0 c, \ gnu includes usually a cr in dos
 1438:                            \ or not unix environments if
 1439:                            \ bin is not selected
 1440: 
 1441: : write-line ( c-addr u fileid -- ior ) \ file
 1442:     dup >r write-file
 1443:     ?dup IF
 1444: 	r> drop EXIT
 1445:     THEN
 1446:     nl$ count r> write-file ;
 1447: 
 1448: \ include-file                                         07apr93py
 1449: 
 1450: : push-file  ( -- )  r>
 1451:   sourceline# >r  loadfile @ >r
 1452:   blk @ >r  tibstack @ >r  >tib @ >r  #tib @ >r
 1453:   >tib @ tibstack @ = IF  r@ tibstack +!  THEN
 1454:   tibstack @ >tib ! >in @ >r  >r ;
 1455: 
 1456: : pop-file   ( throw-code -- throw-code )
 1457:   dup IF
 1458:          source >in @ sourceline# sourcefilename
 1459: 	 error-stack dup @ dup 1+
 1460: 	 max-errors 1- min error-stack !
 1461: 	 6 * cells + cell+
 1462: 	 5 cells bounds swap DO
 1463: 	                    I !
 1464: 	 -1 cells +LOOP
 1465:   THEN
 1466:   r>
 1467:   r> >in !  r> #tib !  r> >tib !  r> tibstack !  r> blk !
 1468:   r> loadfile ! r> loadline !  >r ;
 1469: 
 1470: : read-loop ( i*x -- j*x )
 1471:   BEGIN  refill  WHILE  interpret  REPEAT ;
 1472: 
 1473: : include-file ( i*x fid -- j*x ) \ file
 1474:   push-file  loadfile !
 1475:   0 loadline ! blk off  ['] read-loop catch
 1476:   loadfile @ close-file swap 2dup or
 1477:   pop-file  drop throw throw ;
 1478: 
 1479: create pathfilenamebuf 256 chars allot \ !! make this grow on demand
 1480: 
 1481: \ : check-file-prefix  ( addr len -- addr' len' flag )
 1482: \   dup 0=                    IF  true EXIT  THEN 
 1483: \   over c@ '/ =              IF  true EXIT  THEN 
 1484: \   over 2 S" ./" compare 0=  IF  true EXIT  THEN 
 1485: \   over 3 S" ../" compare 0= IF  true EXIT  THEN
 1486: \   over 2 S" ~/" compare 0=
 1487: \   IF     1 /string
 1488: \          S" HOME" getenv tuck pathfilenamebuf swap move
 1489: \          2dup + >r pathfilenamebuf + swap move
 1490: \          pathfilenamebuf r> true
 1491: \   ELSE   false
 1492: \   THEN ;
 1493: 
 1494: : absolut-path? ( addr u -- flag ) \ gforth
 1495:     \ a path is absolute, if it starts with a / or a ~ (~ expansion),
 1496:     \ or if it is in the form ./* or ../*, extended regexp: [/~]|./|../
 1497:     \ Pathes simply containing a / are not absolute!
 1498:     over c@ '/ = >r
 1499:     over c@ '~ = >r
 1500:     2dup 2 min S" ./" compare 0= >r
 1501:          3 min S" ../" compare 0=
 1502:     r> r> r> or or or ;
 1503: \   [char] / scan nip 0<> ;    
 1504: 
 1505: : open-path-file ( c-addr1 u1 -- file-id c-addr2 u2 ) \ gforth
 1506:     \ opens a file for reading, searching in the path for it (unless
 1507:     \ the filename contains a slash); c-addr2 u2 is the full filename
 1508:     \ (valid until the next call); if the file is not found (or in
 1509:     \ case of other errors for each try), -38 (non-existant file) is
 1510:     \ thrown. Opening for other access modes makes little sense, as
 1511:     \ the path will usually contain dirs that are only readable for
 1512:     \ the user
 1513:     \ !! use file-status to determine access mode?
 1514:     2dup absolut-path?
 1515:     if \ the filename contains a slash
 1516: 	2dup r/o open-file throw ( c-addr1 u1 file-id )
 1517: 	-rot >r pathfilenamebuf r@ cmove ( file-id R: u1 )
 1518: 	pathfilenamebuf r> EXIT
 1519:     then
 1520:     pathdirs 2@ 0
 1521: \    check-file-prefix 0= 
 1522: \    IF  pathdirs 2@ 0
 1523:     ?DO ( c-addr1 u1 dirnamep )
 1524: 	dup >r 2@ dup >r pathfilenamebuf swap cmove ( addr u )
 1525: 	2dup pathfilenamebuf r@ chars + swap cmove ( addr u )
 1526: 	pathfilenamebuf over r> + dup >r r/o open-file 0=
 1527: 	IF ( addr u file-id )
 1528: 	    nip nip r> rdrop 0 LEAVE
 1529: 	THEN
 1530: 	rdrop drop r> cell+ cell+
 1531:     LOOP
 1532: \    ELSE   2dup open-file throw -rot  THEN 
 1533:     0<> -&38 and throw ( file-id u2 )
 1534:     pathfilenamebuf swap ;
 1535: 
 1536: create included-files 0 , 0 , ( pointer to and count of included files )
 1537: here ," the terminal" dup c@ swap 1 + swap , A, here 2 cells -
 1538: create image-included-files  1 , A, ( pointer to and count of included files )
 1539: \ included-files points to ALLOCATEd space, while image-included-files
 1540: \ points to ALLOTed objects, so it survives a save-system
 1541: 
 1542: : loadfilename ( -- a-addr )
 1543:     \ a-addr 2@ produces the current file name ( c-addr u )
 1544:     included-files 2@ drop loadfilename# @ 2* cells + ;
 1545: 
 1546: : sourcefilename ( -- c-addr u ) \ gforth
 1547:     \ the name of the source file which is currently the input
 1548:     \ source.  The result is valid only while the file is being
 1549:     \ loaded.  If the current input source is no (stream) file, the
 1550:     \ result is undefined.
 1551:     loadfilename 2@ ;
 1552: 
 1553: : sourceline# ( -- u ) \ gforth		sourceline-number
 1554:     \ the line number of the line that is currently being interpreted
 1555:     \ from a (stream) file. The first line has the number 1. If the
 1556:     \ current input source is no (stream) file, the result is
 1557:     \ undefined.
 1558:     loadline @ ;
 1559: 
 1560: : init-included-files ( -- )
 1561:     image-included-files 2@ 2* cells save-mem drop ( addr )
 1562:     image-included-files 2@ nip included-files 2! ;
 1563: 
 1564: : included? ( c-addr u -- f ) \ gforth
 1565:     \ true, iff filename c-addr u is in included-files
 1566:     included-files 2@ 0
 1567:     ?do ( c-addr u addr )
 1568: 	dup >r 2@ 2over compare 0=
 1569: 	if
 1570: 	    2drop rdrop unloop
 1571: 	    true EXIT
 1572: 	then
 1573: 	r> cell+ cell+
 1574:     loop
 1575:     2drop drop false ;
 1576: 
 1577: : add-included-file ( c-addr u -- ) \ gforth
 1578:     \ add name c-addr u to included-files
 1579:     included-files 2@ 2* cells 2 cells extend-mem
 1580:     2/ cell / included-files 2!
 1581:     2! ;
 1582: \    included-files 2@ tuck 1+ 2* cells resize throw
 1583: \    swap 2dup 1+ included-files 2!
 1584: \    2* cells + 2! ;
 1585: 
 1586: : included1 ( i*x file-id c-addr u -- j*x ) \ gforth
 1587:     \ include the file file-id with the name given by c-addr u
 1588:     loadfilename# @ >r
 1589:     save-mem add-included-file ( file-id )
 1590:     included-files 2@ nip 1- loadfilename# !
 1591:     ['] include-file catch
 1592:     r> loadfilename# !
 1593:     throw ;
 1594:     
 1595: : included ( i*x addr u -- j*x ) \ file
 1596:     open-path-file included1 ;
 1597: 
 1598: : required ( i*x addr u -- j*x ) \ gforth
 1599:     \ include the file with the name given by addr u, if it is not
 1600:     \ included already. Currently this works by comparing the name of
 1601:     \ the file (with path) against the names of earlier included
 1602:     \ files; however, it would probably be better to fstat the file,
 1603:     \ and compare the device and inode. The advantages would be: no
 1604:     \ problems with several paths to the same file (e.g., due to
 1605:     \ links) and we would catch files included with include-file and
 1606:     \ write a require-file.
 1607:     open-path-file 2dup included?
 1608:     if
 1609: 	2drop close-file throw
 1610:     else
 1611: 	included1
 1612:     then ;
 1613: 
 1614: \ HEX DECIMAL                                           2may93jaw
 1615: 
 1616: : decimal ( -- ) \ core
 1617:     a base ! ;
 1618: : hex ( -- ) \ core-ext
 1619:     10 base ! ;
 1620: 
 1621: \ DEPTH                                                 9may93jaw
 1622: 
 1623: : depth ( -- +n ) \ core
 1624:     sp@ s0 @ swap - cell / ;
 1625: : clearstack ( ... -- )
 1626:     s0 @ sp! ;
 1627: 
 1628: \ INCLUDE                                               9may93jaw
 1629: 
 1630: : include  ( "file" -- ) \ gforth
 1631:   name included ;
 1632: 
 1633: : require  ( "file" -- ) \ gforth
 1634:   name required ;
 1635: 
 1636: \ RECURSE                                               17may93jaw
 1637: 
 1638: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
 1639:     lastxt compile, ; immediate restrict
 1640: ' reveal alias recursive ( -- ) \ gforth
 1641: 	immediate
 1642: 
 1643: \ */MOD */                                              17may93jaw
 1644: 
 1645: \ !! I think */mod should have the same rounding behaviour as / - anton
 1646: : */mod ( n1 n2 n3 -- n4 n5 ) \ core	star-slash-mod
 1647:     >r m* r> sm/rem ;
 1648: 
 1649: : */ ( n1 n2 n3 -- n4 ) \ core	star-slash
 1650:     */mod nip ;
 1651: 
 1652: \ EVALUATE                                              17may93jaw
 1653: 
 1654: : evaluate ( c-addr len -- ) \ core,block
 1655:   push-file  #tib ! >tib !
 1656:   >in off blk off loadfile off -1 loadline !
 1657:   ['] interpret catch
 1658:   pop-file throw ;
 1659: 
 1660: : abort ( ?? -- ?? ) \ core,exception-ext
 1661:     -1 throw ;
 1662: 
 1663: \+ environment? true ENV" CORE"
 1664: \ core wordset is now complete!
 1665: 
 1666: \ Quit                                                 13feb93py
 1667: 
 1668: Defer 'quit
 1669: Defer .status
 1670: : prompt        state @ IF ."  compiled" EXIT THEN ."  ok" ;
 1671: : (quit)        BEGIN .status cr query interpret prompt AGAIN ;
 1672: ' (quit) IS 'quit
 1673: 
 1674: \ DOERROR (DOERROR)                                     13jun93jaw
 1675: 
 1676: 8 Constant max-errors
 1677: Variable error-stack  0 error-stack !
 1678: max-errors 6 * cells allot
 1679: \ format of one cell:
 1680: \ source ( addr u )
 1681: \ >in
 1682: \ line-number
 1683: \ Loadfilename ( addr u )
 1684: 
 1685: : dec. ( n -- ) \ gforth
 1686:     \ print value in decimal representation
 1687:     base @ decimal swap . base ! ;
 1688: 
 1689: : hex. ( u -- ) \ gforth
 1690:     \ print value as unsigned hex number
 1691:     '$ emit base @ swap hex u. base ! ;
 1692: 
 1693: : typewhite ( addr u -- ) \ gforth
 1694:     \ like type, but white space is printed instead of the characters
 1695:     bounds ?do
 1696: 	i c@ 9 = if \ check for tab
 1697: 	    9
 1698: 	else
 1699: 	    bl
 1700: 	then
 1701: 	emit
 1702:     loop ;
 1703: 
 1704: DEFER DOERROR
 1705: 
 1706: : .error-frame ( addr1 u1 n1 n2 addr2 u2 -- )
 1707:   cr error-stack @
 1708:   IF
 1709:      ." in file included from "
 1710:      type ." :" dec.  drop 2drop
 1711:   ELSE
 1712:      type ." :" dec.
 1713:      cr dup 2over type cr drop
 1714:      nip -trailing 1- ( line-start index2 )
 1715:      0 >r  BEGIN
 1716:                   2dup + c@ bl >  WHILE
 1717: 		  r> 1+ >r  1- dup 0<  UNTIL  THEN  1+
 1718:      ( line-start index1 )
 1719:      typewhite
 1720:      r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
 1721:                   [char] ^ emit
 1722:      loop
 1723:   THEN
 1724: ;
 1725: 
 1726: : (DoError) ( throw-code -- )
 1727:   sourceline# IF
 1728:                source >in @ sourceline# 0 0 .error-frame
 1729:   THEN
 1730:   error-stack @ 0 ?DO
 1731:     -1 error-stack +!
 1732:     error-stack dup @ 6 * cells + cell+
 1733:     6 cells bounds DO
 1734:       I @
 1735:     cell +LOOP
 1736:     .error-frame
 1737:   LOOP
 1738:   dup -2 =
 1739:   IF 
 1740:      "error @ ?dup
 1741:      IF
 1742:         cr count type 
 1743:      THEN
 1744:      drop
 1745:   ELSE
 1746:      .error
 1747:   THEN
 1748:   normal-dp dpp ! ;
 1749: 
 1750: ' (DoError) IS DoError
 1751: 
 1752: : quit ( ?? -- ?? ) \ core
 1753:     r0 @ rp! handler off >tib @ >r
 1754:     BEGIN
 1755: 	postpone [
 1756: 	['] 'quit CATCH dup
 1757:     WHILE
 1758: 	DoError r@ >tib ! r@ tibstack !
 1759:     REPEAT
 1760:     drop r> >tib ! ;
 1761: 
 1762: \ Cold                                                 13feb93py
 1763: 
 1764: \ : .name ( name -- ) name>string type space ;
 1765: \ : words  listwords @
 1766: \          BEGIN  @ dup  WHILE  dup .name  REPEAT drop ;
 1767: 
 1768: : cstring>sstring  ( cstring -- addr n ) \ gforth	cstring-to-sstring
 1769:     -1 0 scan 0 swap 1+ /string ;
 1770: : arg ( n -- addr count ) \ gforth
 1771:     cells argv @ + @ cstring>sstring ;
 1772: : #!       postpone \ ;  immediate
 1773: 
 1774: Create pathstring 2 cells allot \ string
 1775: Create pathdirs   2 cells allot \ dir string array, pointer and count
 1776: Variable argv
 1777: Variable argc
 1778: 
 1779: 0 Value script? ( -- flag )
 1780: 
 1781: : process-path ( addr1 u1 -- addr2 u2 )
 1782:     \ addr1 u1 is a path string, addr2 u2 is an array of dir strings
 1783:     align here >r
 1784:     BEGIN
 1785: 	over >r [char] : scan
 1786: 	over r> tuck - ( rest-str this-str )
 1787: 	dup
 1788: 	IF
 1789: 	    2dup 1- chars + c@ [char] / <>
 1790: 	    IF
 1791: 		2dup chars + [char] / swap c!
 1792: 		1+
 1793: 	    THEN
 1794: 	    2,
 1795: 	ELSE
 1796: 	    2drop
 1797: 	THEN
 1798: 	dup
 1799:     WHILE
 1800: 	1 /string
 1801:     REPEAT
 1802:     2drop
 1803:     here r> tuck - 2 cells / ;
 1804: 
 1805: : do-option ( addr1 len1 addr2 len2 -- n )
 1806:     2swap
 1807:     2dup s" -e"         compare  0= >r
 1808:     2dup s" --evaluate" compare  0= r> or
 1809:     IF  2drop dup >r ['] evaluate catch
 1810: 	?dup IF  dup >r DoError r> negate (bye)  THEN
 1811: 	r> >tib +!  2 EXIT  THEN
 1812:     ." Unknown option: " type cr 2drop 1 ;
 1813: 
 1814: : process-args ( -- )
 1815:     >tib @ >r
 1816:     argc @ 1
 1817:     ?DO
 1818: 	I arg over c@ [char] - <>
 1819: 	IF
 1820: 	    required 1
 1821: 	ELSE
 1822: 	    I 1+ argc @ =  IF  s" "  ELSE  I 1+ arg  THEN
 1823: 	    do-option
 1824: 	THEN
 1825:     +LOOP
 1826:     r> >tib ! ;
 1827: 
 1828: Defer 'cold ' noop IS 'cold
 1829: 
 1830: : cold ( -- ) \ gforth
 1831:     stdout TO outfile-id
 1832:     pathstring 2@ process-path pathdirs 2!
 1833:     init-included-files
 1834:     'cold
 1835:     argc @ 1 >
 1836:     IF
 1837: 	true to script?
 1838: 	['] process-args catch ?dup
 1839: 	IF
 1840: 	    dup >r DoError cr r> negate (bye)
 1841: 	THEN
 1842: 	cr
 1843:     THEN
 1844:     false to script?
 1845:     ." GForth " version-string type ." , Copyright (C) 1994-1996 Free Software Foundation, Inc." cr
 1846:     ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'" cr
 1847:     ." Type `bye' to exit"
 1848:     loadline off quit ;
 1849: 
 1850: : license ( -- ) \ gforth
 1851:  cr
 1852:  ." This program is free software; you can redistribute it and/or modify" cr
 1853:  ." it under the terms of the GNU General Public License as published by" cr
 1854:  ." the Free Software Foundation; either version 2 of the License, or" cr
 1855:  ." (at your option) any later version." cr cr
 1856: 
 1857:  ." This program is distributed in the hope that it will be useful," cr
 1858:  ." but WITHOUT ANY WARRANTY; without even the implied warranty of" cr
 1859:  ." MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" cr
 1860:  ." GNU General Public License for more details." cr cr
 1861: 
 1862:  ." You should have received a copy of the GNU General Public License" cr
 1863:  ." along with this program; if not, write to the Free Software" cr
 1864:  ." Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA." cr ;
 1865: 
 1866: : boot ( path **argv argc -- )
 1867:   argc ! argv ! cstring>sstring pathstring 2!  main-task up!
 1868:   sp@ dup s0 ! $10 + dup >tib ! tibstack ! #tib off >in off
 1869:   rp@ r0 !  fp@ f0 !  ['] cold catch DoError bye ;
 1870: 
 1871: : bye ( -- ) \ tools-ext
 1872:     script? 0= IF  cr  THEN  0 (bye) ;
 1873: 
 1874: \ **argv may be scanned by the C starter to get some important
 1875: \ information, as -display and -geometry for an X client FORTH
 1876: \ or space and stackspace overrides
 1877: 
 1878: \ 0 arg contains, however, the name of the program.
 1879: 
 1880: 

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