Annotation of gforth/kernal.fs, revision 1.22

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

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