File:  [gforth] / gforth / Attic / kernal.fs
Revision 1.34: download - view: text, annotated - select for diffs
Fri Apr 14 18:56:55 1995 UTC (25 years, 6 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Put pow10 in a separate file (it is used by ecvt and engine) and
adjusted configure accordingly.
script? is now also set during processing --evaluate arguments. This
avoids getting a newline from "gforth -e bye".

    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: : \ ( -- ) \ core-ext backslash
  790:     blk @
  791:     IF
  792: 	>in @ c/l / 1+ c/l * >in !
  793: 	EXIT
  794:     THEN
  795:     source >in ! drop ; immediate
  796: 
  797: : \G ( -- ) \ new backslash
  798:     POSTPONE \ ; immediate
  799: 
  800: \ error handling                                       22feb93py
  801: \ 'abort thrown out!                                   11may93jaw
  802: 
  803: : (abort")      "lit >r IF  r> "error ! -2 throw  THEN
  804:                 rdrop ;
  805: : abort"        postpone (abort") ," ;        immediate restrict
  806: 
  807: \ Header states                                        23feb93py
  808: 
  809: : flag! ( 8b -- )
  810:     last @ dup 0= abort" last word was headerless"
  811:     cell+ tuck c@ xor swap c! ;
  812: : immediate     $20 flag! ;
  813: : restrict      $40 flag! ;
  814: \ ' noop alias restrict
  815: 
  816: \ Header                                               23feb93py
  817: 
  818: \ input-stream, nextname and noname are quite ugly (passing
  819: \ information through global variables), but they are useful for dealing
  820: \ with existing/independent defining words
  821: 
  822: defer (header)
  823: defer header     ' (header) IS header
  824: 
  825: : string, ( c-addr u -- )
  826:     \ puts down string as cstring
  827:     dup c, here swap chars dup allot move ;
  828: 
  829: : name,  ( "name" -- )
  830:     name
  831:     dup $1F u> -&19 and throw ( is name too long? )
  832:     string, cfalign ;
  833: : input-stream-header ( "name" -- )
  834:     \ !! this is f83-implementation-dependent
  835:     align here last !  -1 A,
  836:     name, $80 flag! ;
  837: 
  838: : input-stream ( -- )  \ general
  839: \ switches back to getting the name from the input stream ;
  840:     ['] input-stream-header IS (header) ;
  841: 
  842: ' input-stream-header IS (header)
  843: 
  844: \ !! make that a 2variable
  845: create nextname-buffer 32 chars allot
  846: 
  847: : nextname-header ( -- )
  848:     \ !! f83-implementation-dependent
  849:     nextname-buffer count
  850:     align here last ! -1 A,
  851:     string, cfalign
  852:     $80 flag!
  853:     input-stream ;
  854: 
  855: \ the next name is given in the string
  856: : nextname ( c-addr u -- ) \ general
  857:     dup $1F u> -&19 and throw ( is name too long? )
  858:     nextname-buffer c! ( c-addr )
  859:     nextname-buffer count move
  860:     ['] nextname-header IS (header) ;
  861: 
  862: : noname-header ( -- )
  863:     0 last ! cfalign
  864:     input-stream ;
  865: 
  866: : noname ( -- ) \ general
  867: \ the next defined word remains anonymous. The xt of that word is given by lastxt
  868:     ['] noname-header IS (header) ;
  869: 
  870: : lastxt ( -- xt ) \ general
  871: \ 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
  872:     lastcfa @ ;
  873: 
  874: : Alias    ( cfa "name" -- )
  875:   Header reveal , $80 flag! ;
  876: 
  877: : name>string ( nfa -- addr count )
  878:  cell+ count $1F and ;
  879: 
  880: Create ???  0 , 3 c, char ? c, char ? c, char ? c,
  881: : >name ( cfa -- nfa )
  882:  $21 cell do
  883:    dup i - count $9F and + cfaligned over $80 + = if
  884:      i - cell - unloop exit
  885:    then
  886:  cell +loop
  887:  drop ??? ( wouldn't 0 be better? ) ;
  888: 
  889: \ indirect threading                                   17mar93py
  890: 
  891: : cfa,     ( code-address -- )
  892:     here lastcfa !
  893:     here  0 A, 0 ,  code-address! ;
  894: : compile, ( xt -- )		A, ;
  895: : !does    ( addr -- )		lastcfa @ does-code! ;
  896: : (;code)  ( R: addr -- )	r> /does-handler + !does ;
  897: : dodoes,  ( -- )
  898:   here /does-handler allot does-handler! ;
  899: 
  900: \ direct threading is implementation dependent
  901: 
  902: : Create    Header reveal [ :dovar ] Literal cfa, ;
  903: 
  904: \ DOES>                                                17mar93py
  905: 
  906: : DOES>  ( compilation: -- )
  907:     state @
  908:     IF
  909: 	;-hook postpone (;code) dodoes,
  910:     ELSE
  911: 	dodoes, here !does 0 ]
  912:     THEN 
  913:     :-hook ; immediate
  914: 
  915: \ Create Variable User Constant                        17mar93py
  916: 
  917: : Variable  Create 0 , ;
  918: : AVariable Create 0 A, ;
  919: : 2VARIABLE ( "name" -- ) \ double
  920:     create 0 , 0 , ;
  921:     
  922: : User      Variable ;
  923: : AUser     AVariable ;
  924: 
  925: : (Constant)  Header reveal [ :docon ] Literal cfa, ;
  926: : Constant  (Constant) , ;
  927: : AConstant (Constant) A, ;
  928: 
  929: : 2Constant
  930:     Create ( w1 w2 "name" -- )
  931:         2,
  932:     DOES> ( -- w1 w2 )
  933:         2@ ;
  934:     
  935: \ IS Defer What's Defers TO                            24feb93py
  936: 
  937: : Defer ( -- )
  938:     \ !! shouldn't it be initialized with abort or something similar?
  939:     Header Reveal [ :dodefer ] Literal cfa,
  940:     ['] noop A, ;
  941: \     Create ( -- ) 
  942: \ 	['] noop A,
  943: \     DOES> ( ??? )
  944: \ 	@ execute ;
  945: 
  946: : IS ( addr "name" -- )
  947:     ' >body
  948:     state @
  949:     IF    postpone ALiteral postpone !  
  950:     ELSE  !
  951:     THEN ;  immediate
  952: ' IS Alias TO immediate
  953: 
  954: : What's ( "name" -- addr )  ' >body
  955:   state @ IF  postpone ALiteral postpone @  ELSE  @  THEN ;
  956:                                              immediate
  957: : Defers ( "name" -- )  ' >body @ compile, ;
  958:                                              immediate
  959: 
  960: \ : ;                                                  24feb93py
  961: 
  962: defer :-hook ( sys1 -- sys2 )
  963: defer ;-hook ( sys2 -- sys1 )
  964: 
  965: : : ( -- colon-sys )  Header [ :docol ] Literal cfa, defstart ] :-hook ;
  966: : ; ( colon-sys -- )  ;-hook ?struc postpone exit reveal postpone [ ;
  967:   immediate restrict
  968: 
  969: : :noname ( -- xt colon-sys )
  970:     0 last !
  971:     here [ :docol ] Literal cfa, 0 ] :-hook ;
  972: 
  973: \ Search list handling                                 23feb93py
  974: 
  975: AVariable current
  976: 
  977: : last?   ( -- false / nfa nfa )    last @ ?dup ;
  978: : (reveal) ( -- )
  979:   last?
  980:   IF
  981:       dup @ 0<
  982:       IF
  983: 	current @ @ over ! current @ !
  984:       ELSE
  985: 	drop
  986:       THEN
  987:   THEN ;
  988: 
  989: \ object oriented search list                          17mar93py
  990: 
  991: \ word list structure:
  992: 
  993: struct
  994:   1 cells: field find-method   \ xt: ( c_addr u wid -- name-id )
  995:   1 cells: field reveal-method \ xt: ( -- )
  996:   1 cells: field rehash-method \ xt: ( wid -- )
  997: \   \ !! what else
  998: end-struct wordlist-map-struct
  999: 
 1000: struct
 1001:   1 cells: field wordlist-id \ not the same as wid; representation depends on implementation
 1002:   1 cells: field wordlist-map \ pointer to a wordlist-map-struct
 1003:   1 cells: field wordlist-link \ link field to other wordlists
 1004:   1 cells: field wordlist-extend \ points to wordlist extensions (eg hash)
 1005: end-struct wordlist-struct
 1006: 
 1007: : f83find      ( addr len wordlist -- nfa / false )  @ (f83find) ;
 1008: 
 1009: \ Search list table: find reveal
 1010: Create f83search       ' f83find A,  ' (reveal) A,  ' drop A,
 1011: 
 1012: Create forth-wordlist  NIL A, G f83search T A, NIL A, NIL A,
 1013: AVariable lookup       G forth-wordlist lookup T !
 1014: G forth-wordlist current T !
 1015: 
 1016: : (search-wordlist)  ( addr count wid -- nfa / false )
 1017:   dup wordlist-map @ find-method @ execute ;
 1018: 
 1019: : search-wordlist  ( addr count wid -- 0 / xt +-1 )
 1020:     (search-wordlist) dup  IF  found  THEN ;
 1021: 
 1022: Variable warnings  G -1 warnings T !
 1023: 
 1024: : check-shadow  ( addr count wid -- )
 1025: \ prints a warning if the string is already present in the wordlist
 1026: \ !! should be refined so the user can suppress the warnings
 1027:  >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
 1028:    ." redefined " name>string 2dup type
 1029:    compare 0<> if
 1030:      ."  with " type
 1031:    else
 1032:      2drop
 1033:    then
 1034:    space space EXIT
 1035:  then
 1036:  2drop 2drop ;
 1037: 
 1038: : sfind ( c-addr u -- xt n / 0 )
 1039:     lookup @ search-wordlist ;
 1040: 
 1041: : find   ( addr -- cfa +-1 / string false )
 1042:     \ !! not ANS conformant: returns +-2 for restricted words
 1043:     dup count sfind dup if
 1044: 	rot drop
 1045:     then ;
 1046: 
 1047: : reveal ( -- )
 1048:  last? if
 1049:    name>string current @ check-shadow
 1050:  then
 1051:  current @ wordlist-map @ reveal-method @ execute ;
 1052: 
 1053: : rehash  ( wid -- )  dup wordlist-map @ rehash-method @ execute ;
 1054: 
 1055: : '    ( "name" -- addr )  name sfind 0= if -&13 bounce then ;
 1056: : [']  ( "name" -- addr )  ' postpone ALiteral ; immediate
 1057: \ Input                                                13feb93py
 1058: 
 1059: 07 constant #bell
 1060: 08 constant #bs
 1061: 09 constant #tab
 1062: 7F constant #del
 1063: 0D constant #cr                \ the newline key code
 1064: 0C constant #ff
 1065: 0A constant #lf
 1066: 
 1067: : bell  #bell emit ;
 1068: 
 1069: \ : backspaces  0 ?DO  #bs emit  LOOP ;
 1070: : >string  ( span addr pos1 -- span addr pos1 addr2 len )
 1071:   over 3 pick 2 pick chars /string ;
 1072: : type-rest ( span addr pos1 -- span addr pos1 back )
 1073:   >string tuck type ;
 1074: : (del)  ( max span addr pos1 -- max span addr pos2 )
 1075:   1- >string over 1+ -rot move
 1076:   rot 1- -rot  #bs emit  type-rest bl emit 1+ backspaces ;
 1077: : (ins)  ( max span addr pos1 char -- max span addr pos2 )
 1078:   >r >string over 1+ swap move 2dup chars + r> swap c!
 1079:   rot 1+ -rot type-rest 1- backspaces 1+ ;
 1080: : ?del ( max span addr pos1 -- max span addr pos2 0 )
 1081:   dup  IF  (del)  THEN  0 ;
 1082: : (ret)  type-rest drop true space ;
 1083: : back  dup  IF  1- #bs emit  ELSE  #bell emit  THEN 0 ;
 1084: : forw 2 pick over <> IF  2dup + c@ emit 1+  ELSE  #bell emit  THEN 0 ;
 1085: 
 1086: Create ctrlkeys
 1087:   ] false false back  false  false false forw  false
 1088:     ?del  false (ret) false  false (ret) false false
 1089:     false false false false  false false false false
 1090:     false false false false  false false false false [
 1091: 
 1092: defer everychar
 1093: ' noop IS everychar
 1094: 
 1095: : decode ( max span addr pos1 key -- max span addr pos2 flag )
 1096:   everychar
 1097:   dup #del = IF  drop #bs  THEN  \ del is rubout
 1098:   dup bl <   IF  cells ctrlkeys + @ execute  EXIT  THEN
 1099:   >r 2over = IF  rdrop bell 0 EXIT  THEN
 1100:   r> (ins) 0 ;
 1101: 
 1102: \ decode should better use a table for control key actions
 1103: \ to define keyboard bindings later
 1104: 
 1105: : accept   ( addr len -- len )
 1106:   dup 0< IF    abs over dup 1 chars - c@ tuck type 
 1107: \ this allows to edit given strings
 1108:          ELSE  0  THEN rot over
 1109:   BEGIN  key decode  UNTIL
 1110:   2drop nip ;
 1111: 
 1112: \ Output                                               13feb93py
 1113: 
 1114: Defer type      \ defer type for a output buffer or fast
 1115:                 \ screen write
 1116: 
 1117: \ : (type) ( addr len -- )
 1118: \   bounds ?DO  I c@ emit  LOOP ;
 1119: 
 1120: ' (type) IS Type
 1121: 
 1122: Defer emit
 1123: 
 1124: ' (Emit) IS Emit
 1125: 
 1126: Defer key
 1127: ' (key) IS key
 1128: 
 1129: \ : form  ( -- rows cols )  &24 &80 ;
 1130: \ form should be implemented using TERMCAPS or CURSES
 1131: \ : rows  form drop ;
 1132: \ : cols  form nip  ;
 1133: 
 1134: \ Query                                                07apr93py
 1135: 
 1136: : refill ( -- flag )
 1137:   blk @  IF  1 blk +!  true  EXIT  THEN
 1138:   tib /line
 1139:   loadfile @ ?dup
 1140:   IF    read-line throw
 1141:   ELSE  loadline @ 0< IF 2drop false EXIT THEN
 1142:         accept true
 1143:   THEN
 1144:   1 loadline +!
 1145:   swap #tib ! 0 >in ! ;
 1146: 
 1147: : Query  ( -- )  loadfile off  blk off  refill drop ;
 1148: 
 1149: \ File specifiers                                       11jun93jaw
 1150: 
 1151: 
 1152: \ 1 c, here char r c, 0 c,                0 c, 0 c, char b c, 0 c,
 1153: \ 2 c, here char r c, char + c, 0 c,
 1154: \ 2 c, here char w c, char + c, 0 c, align
 1155: 4 Constant w/o
 1156: 2 Constant r/w
 1157: 0 Constant r/o
 1158: 
 1159: \ BIN WRITE-LINE                                        11jun93jaw
 1160: 
 1161: \ : bin           dup 1 chars - c@
 1162: \                 r/o 4 chars + over - dup >r swap move r> ;
 1163: 
 1164: : bin  1+ ;
 1165: 
 1166: create nl$ 1 c, A c, 0 c, \ gnu includes usually a cr in dos
 1167:                            \ or not unix environments if
 1168:                            \ bin is not selected
 1169: 
 1170: : write-line    dup >r write-file ?dup IF r> drop EXIT THEN
 1171:                 nl$ count r> write-file ;
 1172: 
 1173: \ include-file                                         07apr93py
 1174: 
 1175: : push-file  ( -- )  r>
 1176:   loadline @ >r loadfile @ >r
 1177:   blk @ >r >tib @ >r  #tib @ dup >r  >tib +!  >in @ >r  >r ;
 1178: 
 1179: : pop-file   ( throw-code -- throw-code )
 1180:   dup IF
 1181:          source >in @ loadline @ loadfilename 2@
 1182: 	 error-stack dup @ dup 1+
 1183: 	 max-errors 1- min error-stack !
 1184: 	 6 * cells + cell+
 1185: 	 5 cells bounds swap DO
 1186: 	                    I !
 1187: 	 -1 cells +LOOP
 1188:   THEN
 1189:   r>
 1190:   r> >in !  r> #tib !  r> >tib !  r> blk !
 1191:   r> loadfile ! r> loadline !  >r ;
 1192: 
 1193: : read-loop ( i*x -- j*x )
 1194:   BEGIN  refill  WHILE  interpret  REPEAT ;
 1195: 
 1196: : include-file ( i*x fid -- j*x )
 1197:   push-file  loadfile !
 1198:   0 loadline ! blk off  ['] read-loop catch
 1199:   loadfile @ close-file swap 2dup or
 1200:   pop-file  drop throw throw ;
 1201: 
 1202: create pathfilenamebuf 256 chars allot \ !! make this grow on demand
 1203: 
 1204: : open-path-file ( c-addr1 u1 -- file-id c-addr2 u2 )
 1205:     \ opens a file for reading, searching in the path for it; c-addr2
 1206:     \ u2 is the full filename (valid until the next call); if the file
 1207:     \ is not found (or in case of other errors for each try), -38
 1208:     \ (non-existant file) is thrown. Opening for other access modes
 1209:     \ makes little sense, as the path will usually contain dirs that
 1210:     \ are only readable for the user
 1211:     \ !! check for "/", "./", "../" in original filename; check for "~/"?
 1212:     pathdirs 2@ 0
 1213:     ?DO ( c-addr1 u1 dirnamep )
 1214: 	dup >r 2@ dup >r pathfilenamebuf swap cmove ( addr u )
 1215: 	2dup pathfilenamebuf r@ chars + swap cmove ( addr u )
 1216: 	pathfilenamebuf over r> + dup >r r/o open-file 0=
 1217: 	if ( addr u file-id )
 1218: 	    nip nip r> rdrop 0 leave
 1219: 	then
 1220: 	rdrop drop r> cell+ cell+
 1221:     LOOP
 1222:     0<> -&38 and throw ( file-id u2 )
 1223:     pathfilenamebuf swap ;
 1224: 
 1225: create included-files 0 , 0 , ( pointer to and count of included files )
 1226: 
 1227: : included? ( c-addr u -- f )
 1228:     \ true, iff filename c-addr u is in included-files
 1229:     included-files 2@ 0
 1230:     ?do ( c-addr u addr )
 1231: 	dup >r 2@ 2over compare 0=
 1232: 	if
 1233: 	    2drop rdrop unloop
 1234: 	    true EXIT
 1235: 	then
 1236: 	r> cell+ cell+
 1237:     loop
 1238:     2drop drop false ;
 1239: 
 1240: : add-included-file ( c-addr u -- )
 1241:     \ add name c-addr u to included-files
 1242:     included-files 2@ tuck 1+ 2* cells resize throw
 1243:     swap 2dup 1+ included-files 2!
 1244:     2* cells + 2! ;
 1245: 
 1246: : save-string		( addr1 u -- addr2 u )
 1247:     swap >r
 1248:     dup allocate throw
 1249:     swap 2dup r> -rot move ;
 1250: 
 1251: : included1 ( i*x file-id c-addr u -- j*x )
 1252:     \ include the file file-id with the name given by c-addr u
 1253:     loadfilename 2@ >r >r
 1254:     save-string 2dup loadfilename 2! add-included-file ( file-id )
 1255:     ['] include-file catch
 1256:     r> r> loadfilename 2!  throw ;
 1257:     
 1258: : included ( i*x addr u -- j*x )
 1259:     open-path-file included1 ;
 1260: 
 1261: : required ( i*x addr u -- j*x )
 1262:     \ include the file with the name given by addr u, if it is not
 1263:     \ included already. Currently this works by comparing the name of
 1264:     \ the file (with path) against the names of earlier included
 1265:     \ files; however, it would probably be better to fstat the file,
 1266:     \ and compare the device and inode. The advantages would be: no
 1267:     \ problems with several paths to the same file (e.g., due to
 1268:     \ links) and we would catch files included with include-file and
 1269:     \ write a require-file.
 1270:     open-path-file 2dup included?
 1271:     if
 1272: 	2drop close-file throw
 1273:     else
 1274: 	included1
 1275:     then ;
 1276: 
 1277: \ HEX DECIMAL                                           2may93jaw
 1278: 
 1279: : decimal a base ! ;
 1280: : hex     10 base ! ;
 1281: 
 1282: \ DEPTH                                                 9may93jaw
 1283: 
 1284: : depth ( -- +n )  sp@ s0 @ swap - cell / ;
 1285: : clearstack ( ... -- )  s0 @ sp! ;
 1286: 
 1287: \ INCLUDE                                               9may93jaw
 1288: 
 1289: : include  ( "file" -- )
 1290:   name included ;
 1291: 
 1292: : require  ( "file" -- )
 1293:   name required ;
 1294: 
 1295: \ RECURSE                                               17may93jaw
 1296: 
 1297: : recurse ( -- )
 1298:     lastxt compile, ; immediate restrict
 1299: : recursive ( -- )
 1300:     reveal ; immediate
 1301: 
 1302: \ */MOD */                                              17may93jaw
 1303: 
 1304: \ !! I think */mod should have the same rounding behaviour as / - anton
 1305: : */mod >r m* r> sm/rem ;
 1306: 
 1307: : */ */mod nip ;
 1308: 
 1309: \ EVALUATE                                              17may93jaw
 1310: 
 1311: : evaluate ( c-addr len -- )
 1312:   push-file  dup #tib ! >tib @ swap move
 1313:   >in off blk off loadfile off -1 loadline !
 1314: 
 1315: \  BEGIN  interpret  >in @ #tib @ u>= UNTIL
 1316:   ['] interpret catch
 1317:   pop-file throw ;
 1318: 
 1319: 
 1320: : abort -1 throw ;
 1321: 
 1322: \+ environment? true ENV" CORE"
 1323: \ core wordset is now complete!
 1324: 
 1325: \ Quit                                                 13feb93py
 1326: 
 1327: Defer 'quit
 1328: Defer .status
 1329: : prompt        state @ IF ."  compiled" EXIT THEN ."  ok" ;
 1330: : (quit)        BEGIN .status cr query interpret prompt AGAIN ;
 1331: ' (quit) IS 'quit
 1332: 
 1333: \ DOERROR (DOERROR)                                     13jun93jaw
 1334: 
 1335: 8 Constant max-errors
 1336: Variable error-stack  0 error-stack !
 1337: max-errors 6 * cells allot
 1338: \ format of one cell:
 1339: \ source ( addr u )
 1340: \ >in
 1341: \ line-number
 1342: \ Loadfilename ( addr u )
 1343: 
 1344: : dec. ( n -- )
 1345:     \ print value in decimal representation
 1346:     base @ decimal swap . base ! ;
 1347: 
 1348: : typewhite ( addr u -- )
 1349:     \ like type, but white space is printed instead of the characters
 1350:     bounds ?do
 1351: 	i c@ 9 = if \ check for tab
 1352: 	    9
 1353: 	else
 1354: 	    bl
 1355: 	then
 1356: 	emit
 1357:     loop
 1358: ;
 1359: 
 1360: DEFER DOERROR
 1361: 
 1362: : .error-frame ( addr1 u1 n1 n2 addr2 u2 -- )
 1363:   cr error-stack @
 1364:   IF
 1365:      ." in file included from "
 1366:      type ." :" dec.  drop 2drop
 1367:   ELSE
 1368:      type ." :" dec.
 1369:      cr dup 2over type cr drop
 1370:      nip -trailing 1- ( line-start index2 )
 1371:      0 >r  BEGIN
 1372:                   2dup + c@ bl >  WHILE
 1373: 		  r> 1+ >r  1- dup 0<  UNTIL  THEN  1+
 1374:      ( line-start index1 )
 1375:      typewhite
 1376:      r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
 1377:                   [char] ^ emit
 1378:      loop
 1379:   THEN
 1380: ;
 1381: 
 1382: : (DoError) ( throw-code -- )
 1383:   loadline @ IF
 1384:                source >in @ loadline @ 0 0 .error-frame
 1385:   THEN
 1386:   error-stack @ 0 ?DO
 1387:     -1 error-stack +!
 1388:     error-stack dup @ 6 * cells + cell+
 1389:     6 cells bounds DO
 1390:       I @
 1391:     cell +LOOP
 1392:     .error-frame
 1393:   LOOP
 1394:   dup -2 =
 1395:   IF 
 1396:      "error @ ?dup
 1397:      IF
 1398:         cr count type 
 1399:      THEN
 1400:      drop
 1401:   ELSE
 1402:      .error
 1403:   THEN
 1404:   normal-dp dpp ! ;
 1405: 
 1406: ' (DoError) IS DoError
 1407: 
 1408: : quit   r0 @ rp! handler off >tib @ >r
 1409:   BEGIN
 1410:     postpone [
 1411:     ['] 'quit CATCH dup
 1412:   WHILE
 1413:     DoError r@ >tib !
 1414:   REPEAT
 1415:   drop r> >tib ! ;
 1416: 
 1417: \ Cold                                                 13feb93py
 1418: 
 1419: \ : .name ( name -- ) cell+ count $1F and type space ;
 1420: \ : words  listwords @
 1421: \          BEGIN  @ dup  WHILE  dup .name  REPEAT drop ;
 1422: 
 1423: : cstring>sstring  ( cstring -- addr n )  -1 0 scan 0 swap 1+ /string ;
 1424: : arg ( n -- addr count )  cells argv @ + @ cstring>sstring ;
 1425: : #!       postpone \ ;  immediate
 1426: 
 1427: Create pathstring 2 cells allot \ string
 1428: Create pathdirs   2 cells allot \ dir string array, pointer and count
 1429: Variable argv
 1430: Variable argc
 1431: 
 1432: 0 Value script? ( -- flag )
 1433: 
 1434: : process-path ( addr1 u1 -- addr2 u2 )
 1435:     \ addr1 u1 is a path string, addr2 u2 is an array of dir strings
 1436:     here >r
 1437:     BEGIN
 1438: 	over >r [char] : scan
 1439: 	over r> tuck - ( rest-str this-str )
 1440: 	dup
 1441: 	IF
 1442: 	    2dup 1- chars + c@ [char] / <>
 1443: 	    IF
 1444: 		2dup chars + [char] / swap c!
 1445: 		1+
 1446: 	    THEN
 1447: 	    2,
 1448: 	ELSE
 1449: 	    2drop
 1450: 	THEN
 1451: 	dup
 1452:     WHILE
 1453: 	1 /string
 1454:     REPEAT
 1455:     2drop
 1456:     here r> tuck - 2 cells / ;
 1457: 
 1458: : do-option ( addr1 len1 addr2 len2 -- n )
 1459:     2swap
 1460:     2dup s" -e"         compare  0= >r
 1461:     2dup s" --evaluate" compare  0= r> or
 1462:     IF  2drop dup >r ['] evaluate catch
 1463: 	?dup IF  dup >r DoError r> negate (bye)  THEN
 1464: 	r> >tib +!  2 EXIT  THEN
 1465:     ." Unknown option: " type cr 2drop 1 ;
 1466: 
 1467: : process-args ( -- )
 1468:     >tib @ >r
 1469:     true to script?
 1470:     argc @ 1
 1471:     ?DO
 1472: 	I arg over c@ [char] - <>
 1473: 	IF
 1474: 	    included 1
 1475: 	ELSE
 1476: 	    I 1+ arg  do-option
 1477: 	THEN
 1478:     +LOOP
 1479:     false to script?
 1480:     r> >tib ! ;
 1481: 
 1482: Defer 'cold ' noop IS 'cold
 1483: 
 1484: : cold ( -- )
 1485:     pathstring 2@ process-path pathdirs 2!
 1486:     0 0 included-files 2!
 1487:     'cold
 1488:     argc @ 1 >
 1489:     IF
 1490: 	['] process-args catch ?dup
 1491: 	IF
 1492: 	    dup >r DoError cr r> negate (bye)
 1493: 	THEN
 1494:     THEN
 1495:     cr
 1496:     ." GNU Forth 0.0alpha, Copyright (C) 1994 Free Software Foundation, Inc." cr
 1497:     ." GNU Forth comes with ABSOLUTELY NO WARRANTY; for details type `license'" cr
 1498:     ." Type `bye' to exit"
 1499:     loadline off quit ;
 1500: 
 1501: : license ( -- ) cr
 1502:  ." This program is free software; you can redistribute it and/or modify" cr
 1503:  ." it under the terms of the GNU General Public License as published by" cr
 1504:  ." the Free Software Foundation; either version 2 of the License, or" cr
 1505:  ." (at your option) any later version." cr cr
 1506: 
 1507:  ." This program is distributed in the hope that it will be useful," cr
 1508:  ." but WITHOUT ANY WARRANTY; without even the implied warranty of" cr
 1509:  ." MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" cr
 1510:  ." GNU General Public License for more details." cr cr
 1511: 
 1512:  ." You should have received a copy of the GNU General Public License" cr
 1513:  ." along with this program; if not, write to the Free Software" cr
 1514:  ." Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA." cr ;
 1515: 
 1516: : boot ( path **argv argc -- )
 1517:   argc ! argv ! cstring>sstring pathstring 2!  main-task up!
 1518:   sp@ dup s0 ! $10 + >tib ! #tib off >in off
 1519:   rp@ r0 !  fp@ f0 !  cold ;
 1520: 
 1521: : bye  script? 0= IF  cr  THEN  0 (bye) ;
 1522: 
 1523: \ **argv may be scanned by the C starter to get some important
 1524: \ information, as -display and -geometry for an X client FORTH
 1525: \ or space and stackspace overrides
 1526: 
 1527: \ 0 arg contains, however, the name of the program.

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