Annotation of gforth/kernel.fs, revision 1.3

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

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