File:  [gforth] / gforth / Attic / kernal.fs
Revision 1.20: download - view: text, annotated - select for diffs
Mon Sep 12 19:00:32 1994 UTC (29 years, 7 months ago) by pazsan
Branches: MAIN
CVS tags: HEAD
Added forth variants for primitives
Added a generator for forth primitives
Cleaned up some minor errors
Changed names of local access (was cell size dependent)
Where is "getopt.h"???!? Added tiny workaround. Where is getopt_long?

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

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