File:  [gforth] / gforth / Attic / kernal.fs
Revision 1.52: download - view: text, annotated - select for diffs
Fri Feb 9 17:34:11 1996 UTC (28 years, 1 month ago) by anton
Branches: MAIN
CVS tags: HEAD
?DUP-IF and ?DUP-0=-IF are now supported by primitives
added primitives EMIT-FILE, STDOUT, STDERR
EMIT and TYPE now work through file words
added some code for the BUGGY_LONG_LONG case (not yet complete)
eliminated D! and D@
made DMIN, DMAX, DABS high-level
added compat/control.fs (?DUP-IF etc.)

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

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