File:  [gforth] / gforth / Attic / kernal.fs
Revision 1.31: download - view: text, annotated - select for diffs
Thu Feb 23 20:17:20 1995 UTC (29 years, 1 month ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added structure support in kernal
fixed bug on dictionary expand (512 wordlist limit)

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

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