File:  [gforth] / gforth / Attic / kernal.fs
Revision 1.36: download - view: text, annotated - select for diffs
Sun Apr 30 23:18:23 1995 UTC (24 years, 9 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added ~/ expansion (uses env HOME)

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

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