Annotation of gforth/kernel.fs, revision 1.4

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
1.4     ! anton     182:     \g copy a memory block into a newly allocated region in the heap
1.1       pazsan    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
1.4     ! anton     552:     if ( c-addr u nt )
1.1       pazsan    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: 
1.4     ! anton     984: : name>string ( nt -- addr count ) \ gforth    name-to-string
        !           985:     \g @var{addr count} is the name of the word represented by @var{nt}.
        !           986:     cell+ count $1F and ;
1.1       pazsan    987: 
                    988: Create ???  0 , 3 c, char ? c, char ? c, char ? c,
1.4     ! anton     989: : >name ( cfa -- nt ) \ gforth to-name
1.1       pazsan    990:  $21 cell do
                    991:    dup i - count $9F and + cfaligned over alias-mask + = if
                    992:      i - cell - unloop exit
                    993:    then
                    994:  cell +loop
                    995:  drop ??? ( wouldn't 0 be better? ) ;
                    996: 
                    997: \ threading                                   17mar93py
                    998: 
                    999: : cfa,     ( code-address -- )  \ gforth       cfa-comma
                   1000:     here
                   1001:     dup lastcfa !
                   1002:     0 A, 0 ,  code-address! ;
                   1003: : compile, ( xt -- )   \ core-ext      compile-comma
                   1004:     A, ;
                   1005: : !does    ( addr -- ) \ gforth        store-does
                   1006:     lastxt does-code! ;
                   1007: : (does>)  ( R: addr -- )
                   1008:     r> /does-handler + !does ;
                   1009: : dodoes,  ( -- )
                   1010:   here /does-handler allot does-handler! ;
                   1011: 
                   1012: : Create ( "name" -- ) \ core
                   1013:     Header reveal dovar: cfa, ;
                   1014: 
                   1015: \ Create Variable User Constant                        17mar93py
                   1016: 
                   1017: : Variable ( "name" -- ) \ core
                   1018:     Create 0 , ;
                   1019: : AVariable ( "name" -- ) \ gforth
                   1020:     Create 0 A, ;
                   1021: : 2VARIABLE ( "name" -- ) \ double
                   1022:     create 0 , 0 , ;
                   1023:     
                   1024: : User ( "name" -- ) \ gforth
                   1025:     Variable ;
                   1026: : AUser ( "name" -- ) \ gforth
                   1027:     AVariable ;
                   1028: 
                   1029: : (Constant)  Header reveal docon: cfa, ;
                   1030: : Constant ( w "name" -- ) \ core
1.3       pazsan   1031:     \G Defines constant @var{name}
                   1032:     \G  
                   1033:     \G @var{name} execution: @var{-- w}
1.1       pazsan   1034:     (Constant) , ;
                   1035: : AConstant ( addr "name" -- ) \ gforth
                   1036:     (Constant) A, ;
                   1037: 
                   1038: : 2Constant ( w1 w2 "name" -- ) \ double
                   1039:     Create ( w1 w2 "name" -- )
                   1040:         2,
                   1041:     DOES> ( -- w1 w2 )
                   1042:         2@ ;
                   1043:     
                   1044: \ IS Defer What's Defers TO                            24feb93py
                   1045: 
                   1046: : Defer ( "name" -- ) \ gforth
                   1047:     \ !! shouldn't it be initialized with abort or something similar?
                   1048:     Header Reveal dodefer: cfa,
                   1049:     ['] noop A, ;
                   1050: \     Create ( -- ) 
                   1051: \      ['] noop A,
                   1052: \     DOES> ( ??? )
                   1053: \      perform ;
                   1054: 
                   1055: : Defers ( "name" -- ) \ gforth
                   1056:     ' >body @ compile, ; immediate
                   1057: 
                   1058: \ : ;                                                  24feb93py
                   1059: 
                   1060: defer :-hook ( sys1 -- sys2 )
                   1061: defer ;-hook ( sys2 -- sys1 )
                   1062: 
                   1063: : : ( "name" -- colon-sys ) \ core     colon
                   1064:     Header docol: cfa, defstart ] :-hook ;
                   1065: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core    semicolon
                   1066:     ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
                   1067: 
                   1068: : :noname ( -- xt colon-sys ) \ core-ext       colon-no-name
                   1069:     0 last !
                   1070:     cfalign here docol: cfa, 0 ] :-hook ;
                   1071: 
                   1072: \ Search list handling                                 23feb93py
                   1073: 
                   1074: AVariable current ( -- addr ) \ gforth
                   1075: 
                   1076: : last?   ( -- false / nfa nfa )
                   1077:     last @ ?dup ;
1.4     ! anton    1078: : (reveal) ( nt wid -- )
1.1       pazsan   1079:     ( wid>wordlist-id ) dup >r
                   1080:     @ over ( name>link ) ! 
                   1081:     r> ! ;
                   1082: 
                   1083: \ object oriented search list                          17mar93py
                   1084: 
                   1085: \ word list structure:
                   1086: 
                   1087: struct
1.4     ! anton    1088:   1 cells: field find-method   \ xt: ( c_addr u wid -- nt )
        !          1089:   1 cells: field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
1.1       pazsan   1090:   1 cells: field rehash-method \ xt: ( wid -- )
                   1091: \   \ !! what else
                   1092: end-struct wordlist-map-struct
                   1093: 
                   1094: struct
                   1095:   1 cells: field wordlist-id \ not the same as wid; representation depends on implementation
                   1096:   1 cells: field wordlist-map \ pointer to a wordlist-map-struct
                   1097:   1 cells: field wordlist-link \ link field to other wordlists
                   1098:   1 cells: field wordlist-extend \ points to wordlist extensions (eg hash)
                   1099: end-struct wordlist-struct
                   1100: 
1.4     ! anton    1101: : f83find      ( addr len wordlist -- nt / false )
1.1       pazsan   1102:     ( wid>wordlist-id ) @ (f83find) ;
                   1103: 
                   1104: \ Search list table: find reveal
                   1105: Create f83search ( -- wordlist-map )
                   1106:     ' f83find A,  ' (reveal) A,  ' drop A,
                   1107: 
                   1108: Create forth-wordlist  NIL A, G f83search T A, NIL A, NIL A,
                   1109: AVariable lookup       G forth-wordlist lookup T !
                   1110: G forth-wordlist current T !
                   1111: 
                   1112: \ higher level parts of find
                   1113: 
                   1114: ( struct )
                   1115: 0 >body cell
                   1116:   1 cells: field interpret/compile-int
                   1117:   1 cells: field interpret/compile-comp
                   1118: end-struct interpret/compile-struct
                   1119: 
                   1120: : interpret/compile? ( xt -- flag )
                   1121:     >does-code ['] S" >does-code = ;
                   1122: 
                   1123: : (cfa>int) ( cfa -- xt )
                   1124:     dup interpret/compile?
                   1125:     if
                   1126:        interpret/compile-int @
                   1127:     then ;
                   1128: 
                   1129: : (x>int) ( cfa b -- xt )
                   1130:     \ get interpretation semantics of name
                   1131:     restrict-mask and
                   1132:     if
                   1133:        drop ['] compile-only-error
                   1134:     else
                   1135:        (cfa>int)
                   1136:     then ;
                   1137: 
1.4     ! anton    1138: : name>int ( nt -- xt ) \ gforth
        !          1139:     \G @var{xt} represents the interpretation semantics of the word
        !          1140:     \G @var{nt}. Produces @code{' compile-only-error} if
        !          1141:     \G @var{nt} is compile-only.
1.1       pazsan   1142:     (name>x) (x>int) ;
                   1143: 
1.4     ! anton    1144: : name?int ( nt -- xt ) \ gforth
        !          1145:     \G Like name>int, but throws an error if compile-only.
1.1       pazsan   1146:     (name>x) restrict-mask and
                   1147:     if
                   1148:        compile-only-error \ does not return
                   1149:     then
                   1150:     (cfa>int) ;
                   1151: 
1.4     ! anton    1152: : name>comp ( nt -- w xt ) \ gforth
        !          1153:     \G @var{w xt} is the compilation token wor the word @var{nt}.
1.1       pazsan   1154:     (name>x) >r dup interpret/compile?
                   1155:     if
                   1156:        interpret/compile-comp @
                   1157:     then
                   1158:     r> immediate-mask and if
                   1159:        ['] execute
                   1160:     else
                   1161:        ['] compile,
                   1162:     then ;
                   1163: 
1.4     ! anton    1164: : (search-wordlist)  ( addr count wid -- nt / false )
1.1       pazsan   1165:     dup wordlist-map @ find-method perform ;
                   1166: 
                   1167: : flag-sign ( f -- 1|-1 )
                   1168:     \ true becomes 1, false -1
                   1169:     0= 2* 1+ ;
                   1170: 
                   1171: : (name>intn) ( nfa -- xt +-1 )
                   1172:     (name>x) tuck (x>int) ( b xt )
                   1173:     swap immediate-mask and flag-sign ;
                   1174: 
                   1175: : search-wordlist ( addr count wid -- 0 / xt +-1 ) \ search
                   1176:     \ xt is the interpretation semantics
                   1177:     (search-wordlist) dup if
                   1178:        (name>intn)
                   1179:     then ;
                   1180: 
1.4     ! anton    1181: : find-name ( c-addr u -- nt/0 ) \ gforth
        !          1182:     \g Find the name @var{c-addr u} in the current search
        !          1183:     \g order. Return its nt, if found, otherwise 0.
1.1       pazsan   1184:     lookup @ (search-wordlist) ;
                   1185: 
                   1186: : sfind ( c-addr u -- 0 / xt +-1  ) \ gforth-obsolete
                   1187:     find-name dup
1.4     ! anton    1188:     if ( nt )
1.1       pazsan   1189:        state @
                   1190:        if
                   1191:            name>comp ['] execute = flag-sign
                   1192:        else
                   1193:            (name>intn)
                   1194:        then
                   1195:     then ;
                   1196: 
                   1197: : find ( c-addr -- xt +-1 / c-addr 0 ) \ core
                   1198:     dup count sfind dup
                   1199:     if
                   1200:        rot drop
                   1201:     then ;
                   1202: 
1.4     ! anton    1203: : (') ( "name" -- nt ) \ gforth
1.1       pazsan   1204:     name find-name dup 0=
                   1205:     IF
                   1206:        drop -&13 bounce
                   1207:     THEN  ;
                   1208: 
1.4     ! anton    1209: : [(')]  ( compilation "name" -- ; run-time -- nt ) \ gforth   bracket-paren-tick
1.1       pazsan   1210:     (') postpone ALiteral ; immediate restrict
                   1211: 
                   1212: : '    ( "name" -- xt ) \ core tick
1.4     ! anton    1213:     \g @var{xt} represents @var{name}'s interpretation
        !          1214:     \g semantics. Performs @code{-14 throw} if the word has no
        !          1215:     \g interpretation semantics.
1.1       pazsan   1216:     (') name?int ;
1.4     ! anton    1217: : [']  ( compilation. "name" -- ; run-time. -- xt ) \ core     bracket-tick
        !          1218:     \g @var{xt} represents @var{name}'s interpretation
        !          1219:     \g semantics. Performs @code{-14 throw} if the word has no
        !          1220:     \g interpretation semantics.
1.1       pazsan   1221:     ' postpone ALiteral ; immediate restrict
                   1222: 
                   1223: : COMP'    ( "name" -- w xt ) \ gforth c-tick
1.4     ! anton    1224:     \g @var{w xt} represents @var{name}'s compilation semantics.
1.1       pazsan   1225:     (') name>comp ;
                   1226: : [COMP']  ( compilation "name" -- ; run-time -- w xt ) \ gforth       bracket-comp-tick
1.4     ! anton    1227:     \g @var{w xt} represents @var{name}'s compilation semantics.
1.1       pazsan   1228:     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
                   1229: 
                   1230: \ reveal words
                   1231: 
                   1232: Variable warnings ( -- addr ) \ gforth
                   1233: G -1 warnings T !
                   1234: 
                   1235: : check-shadow  ( addr count wid -- )
1.3       pazsan   1236: \G prints a warning if the string is already present in the wordlist
1.1       pazsan   1237:  >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
                   1238:    ." redefined " name>string 2dup type
                   1239:    compare 0<> if
                   1240:      ."  with " type
                   1241:    else
                   1242:      2drop
                   1243:    then
                   1244:    space space EXIT
                   1245:  then
                   1246:  2drop 2drop ;
                   1247: 
                   1248: : reveal ( -- ) \ gforth
                   1249:     last?
                   1250:     if \ the last word has a header
                   1251:        dup ( name>link ) @ 1 and
                   1252:        if \ it is still hidden
1.4     ! anton    1253:            dup ( name>link ) @ 1 xor           ( nt wid )
        !          1254:            2dup >r name>string r> check-shadow ( nt wid )
1.1       pazsan   1255:            dup wordlist-map @ reveal-method perform
                   1256:        then
                   1257:     then ;
                   1258: 
                   1259: : rehash  ( wid -- )
                   1260:     dup wordlist-map @ rehash-method perform ;
                   1261: 
                   1262: \ Input                                                13feb93py
                   1263: 
                   1264: 07 constant #bell ( -- c ) \ gforth
                   1265: 08 constant #bs ( -- c ) \ gforth
                   1266: 09 constant #tab ( -- c ) \ gforth
                   1267: 7F constant #del ( -- c ) \ gforth
                   1268: 0D constant #cr   ( -- c ) \ gforth
                   1269: \ the newline key code
                   1270: 0C constant #ff ( -- c ) \ gforth
                   1271: 0A constant #lf ( -- c ) \ gforth
                   1272: 
                   1273: : bell  #bell emit ;
                   1274: : cr ( -- ) \ core
                   1275:     \ emit a newline
                   1276:     #lf ( sic! ) emit ;
                   1277: 
                   1278: \ : backspaces  0 ?DO  #bs emit  LOOP ;
                   1279: 
                   1280: : (ins) ( max span addr pos1 key -- max span addr pos2 )
                   1281:     >r 2dup + r@ swap c! r> emit 1+ rot 1+ -rot ;
                   1282: : (bs) ( max span addr pos1 -- max span addr pos2 flag )
                   1283:     dup IF
                   1284:        #bs emit bl emit #bs emit 1- rot 1- -rot
                   1285:     THEN false ;
                   1286: : (ret)  true space ;
                   1287: 
                   1288: Create ctrlkeys
                   1289:   ] false false false false  false false false false
                   1290:     (bs)  false (ret) false  false (ret) false false
                   1291:     false false false false  false false false false
                   1292:     false false false false  false false false false [
                   1293: 
                   1294: defer insert-char
                   1295: ' (ins) IS insert-char
                   1296: defer everychar
                   1297: ' noop IS everychar
                   1298: 
                   1299: : decode ( max span addr pos1 key -- max span addr pos2 flag )
                   1300:   everychar
                   1301:   dup #del = IF  drop #bs  THEN  \ del is rubout
                   1302:   dup bl <   IF  cells ctrlkeys + perform  EXIT  THEN
                   1303:   >r 2over = IF  rdrop bell 0 EXIT  THEN
                   1304:   r> insert-char 0 ;
                   1305: 
                   1306: : accept   ( addr len -- len ) \ core
                   1307:   dup 0< IF    abs over dup 1 chars - c@ tuck type 
                   1308: \ this allows to edit given strings
                   1309:          ELSE  0  THEN rot over
                   1310:   BEGIN  key decode  UNTIL
                   1311:   2drop nip ;
                   1312: 
                   1313: \ Output                                               13feb93py
                   1314: 
                   1315: : (type) ( c-addr u -- ) \ gforth
                   1316:     outfile-id write-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
                   1317: ;
                   1318: 
                   1319: Defer type ( c-addr u -- ) \ core
                   1320: \ defer type for a output buffer or fast
                   1321: \ screen write
                   1322: 
                   1323: ' (type) IS Type
                   1324: 
                   1325: : (emit) ( c -- ) \ gforth
                   1326:     outfile-id emit-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
                   1327: ;
                   1328: 
                   1329: Defer emit ( c -- ) \ core
                   1330: ' (Emit) IS Emit
                   1331: 
                   1332: Defer key ( -- c ) \ core
                   1333: ' (key) IS key
                   1334: 
                   1335: \ Query                                                07apr93py
                   1336: 
                   1337: : refill ( -- flag ) \ core-ext,block-ext,file-ext
                   1338:   blk @  IF  1 blk +!  true  0 >in !  EXIT  THEN
                   1339:   tib /line
                   1340:   loadfile @ ?dup
                   1341:   IF    read-line throw
                   1342:   ELSE  sourceline# 0< IF 2drop false EXIT THEN
                   1343:         accept true
                   1344:   THEN
                   1345:   1 loadline +!
                   1346:   swap #tib ! 0 >in ! ;
                   1347: 
                   1348: : Query  ( -- ) \ core-ext
1.3       pazsan   1349:     \G obsolescent
1.1       pazsan   1350:     loadfile off  blk off  refill drop ;
                   1351: 
                   1352: \ File specifiers                                       11jun93jaw
                   1353: 
                   1354: 
                   1355: \ 1 c, here char r c, 0 c,                0 c, 0 c, char b c, 0 c,
                   1356: \ 2 c, here char r c, char + c, 0 c,
                   1357: \ 2 c, here char w c, char + c, 0 c, align
                   1358: 4 Constant w/o ( -- fam ) \ file       w-o
                   1359: 2 Constant r/w ( -- fam ) \ file       r-w
                   1360: 0 Constant r/o ( -- fam ) \ file       r-o
                   1361: 
                   1362: \ BIN WRITE-LINE                                        11jun93jaw
                   1363: 
                   1364: \ : bin           dup 1 chars - c@
                   1365: \                 r/o 4 chars + over - dup >r swap move r> ;
                   1366: 
                   1367: : bin ( fam1 -- fam2 ) \ file
                   1368:     1 or ;
                   1369: 
                   1370: create nl$ 1 c, A c, 0 c, \ gnu includes usually a cr in dos
                   1371:                            \ or not unix environments if
                   1372:                            \ bin is not selected
                   1373: 
                   1374: : write-line ( c-addr u fileid -- ior ) \ file
                   1375:     dup >r write-file
                   1376:     ?dup IF
                   1377:        r> drop EXIT
                   1378:     THEN
                   1379:     nl$ count r> write-file ;
                   1380: 
                   1381: \ include-file                                         07apr93py
                   1382: 
                   1383: : push-file  ( -- )  r>
                   1384:   sourceline# >r  loadfile @ >r
                   1385:   blk @ >r  tibstack @ >r  >tib @ >r  #tib @ >r
                   1386:   >tib @ tibstack @ = IF  r@ tibstack +!  THEN
                   1387:   tibstack @ >tib ! >in @ >r  >r ;
                   1388: 
                   1389: : pop-file   ( throw-code -- throw-code )
                   1390:   dup IF
                   1391:          source >in @ sourceline# sourcefilename
                   1392:         error-stack dup @ dup 1+
                   1393:         max-errors 1- min error-stack !
                   1394:         6 * cells + cell+
                   1395:         5 cells bounds swap DO
                   1396:                            I !
                   1397:         -1 cells +LOOP
                   1398:   THEN
                   1399:   r>
                   1400:   r> >in !  r> #tib !  r> >tib !  r> tibstack !  r> blk !
                   1401:   r> loadfile ! r> loadline !  >r ;
                   1402: 
                   1403: : read-loop ( i*x -- j*x )
                   1404:   BEGIN  refill  WHILE  interpret  REPEAT ;
                   1405: 
                   1406: : include-file ( i*x fid -- j*x ) \ file
                   1407:   push-file  loadfile !
                   1408:   0 loadline ! blk off  ['] read-loop catch
                   1409:   loadfile @ close-file swap 2dup or
                   1410:   pop-file  drop throw throw ;
                   1411: 
                   1412: create pathfilenamebuf 256 chars allot \ !! make this grow on demand
                   1413: 
                   1414: \ : check-file-prefix  ( addr len -- addr' len' flag )
                   1415: \   dup 0=                    IF  true EXIT  THEN 
                   1416: \   over c@ '/ =              IF  true EXIT  THEN 
                   1417: \   over 2 S" ./" compare 0=  IF  true EXIT  THEN 
                   1418: \   over 3 S" ../" compare 0= IF  true EXIT  THEN
                   1419: \   over 2 S" ~/" compare 0=
                   1420: \   IF     1 /string
                   1421: \          S" HOME" getenv tuck pathfilenamebuf swap move
                   1422: \          2dup + >r pathfilenamebuf + swap move
                   1423: \          pathfilenamebuf r> true
                   1424: \   ELSE   false
                   1425: \   THEN ;
                   1426: 
                   1427: : absolut-path? ( addr u -- flag ) \ gforth
1.3       pazsan   1428:     \G a path is absolute, if it starts with a / or a ~ (~ expansion),
                   1429:     \G or if it is in the form ./* or ../*, extended regexp: ^[/~]|./|../
                   1430:     \G Pathes simply containing a / are not absolute!
1.1       pazsan   1431:     over c@ '/ = >r
                   1432:     over c@ '~ = >r
                   1433:     2dup 2 min S" ./" compare 0= >r
                   1434:          3 min S" ../" compare 0=
                   1435:     r> r> r> or or or ;
                   1436: \   [char] / scan nip 0<> ;    
                   1437: 
                   1438: : open-path-file ( c-addr1 u1 -- file-id c-addr2 u2 ) \ gforth
1.3       pazsan   1439:     \G opens a file for reading, searching in the path for it (unless
                   1440:     \G the filename contains a slash); c-addr2 u2 is the full filename
                   1441:     \G (valid until the next call); if the file is not found (or in
                   1442:     \G case of other errors for each try), -38 (non-existant file) is
                   1443:     \G thrown. Opening for other access modes makes little sense, as
                   1444:     \G the path will usually contain dirs that are only readable for
                   1445:     \G the user
1.1       pazsan   1446:     \ !! use file-status to determine access mode?
                   1447:     2dup absolut-path?
                   1448:     if \ the filename contains a slash
                   1449:        2dup r/o open-file throw ( c-addr1 u1 file-id )
                   1450:        -rot >r pathfilenamebuf r@ cmove ( file-id R: u1 )
                   1451:        pathfilenamebuf r> EXIT
                   1452:     then
                   1453:     pathdirs 2@ 0
                   1454: \    check-file-prefix 0= 
                   1455: \    IF  pathdirs 2@ 0
                   1456:     ?DO ( c-addr1 u1 dirnamep )
                   1457:        dup >r 2@ dup >r pathfilenamebuf swap cmove ( addr u )
                   1458:        2dup pathfilenamebuf r@ chars + swap cmove ( addr u )
                   1459:        pathfilenamebuf over r> + dup >r r/o open-file 0=
                   1460:        IF ( addr u file-id )
                   1461:            nip nip r> rdrop 0 LEAVE
                   1462:        THEN
                   1463:        rdrop drop r> cell+ cell+
                   1464:     LOOP
                   1465: \    ELSE   2dup open-file throw -rot  THEN 
                   1466:     0<> -&38 and throw ( file-id u2 )
                   1467:     pathfilenamebuf swap ;
                   1468: 
                   1469: create included-files 0 , 0 , ( pointer to and count of included files )
                   1470: here ," the terminal" dup c@ swap 1 + swap , A, here 2 cells -
                   1471: create image-included-files  1 , A, ( pointer to and count of included files )
                   1472: \ included-files points to ALLOCATEd space, while image-included-files
                   1473: \ points to ALLOTed objects, so it survives a save-system
                   1474: 
                   1475: : loadfilename ( -- a-addr )
1.3       pazsan   1476:     \G a-addr 2@ produces the current file name ( c-addr u )
1.1       pazsan   1477:     included-files 2@ drop loadfilename# @ 2* cells + ;
                   1478: 
                   1479: : sourcefilename ( -- c-addr u ) \ gforth
1.3       pazsan   1480:     \G the name of the source file which is currently the input
                   1481:     \G source.  The result is valid only while the file is being
                   1482:     \G loaded.  If the current input source is no (stream) file, the
                   1483:     \G result is undefined.
1.1       pazsan   1484:     loadfilename 2@ ;
                   1485: 
                   1486: : sourceline# ( -- u ) \ gforth                sourceline-number
1.3       pazsan   1487:     \G the line number of the line that is currently being interpreted
                   1488:     \G from a (stream) file. The first line has the number 1. If the
                   1489:     \G current input source is no (stream) file, the result is
                   1490:     \G undefined.
1.1       pazsan   1491:     loadline @ ;
                   1492: 
                   1493: : init-included-files ( -- )
                   1494:     image-included-files 2@ 2* cells save-mem drop ( addr )
                   1495:     image-included-files 2@ nip included-files 2! ;
                   1496: 
                   1497: : included? ( c-addr u -- f ) \ gforth
1.3       pazsan   1498:     \G true, iff filename c-addr u is in included-files
1.1       pazsan   1499:     included-files 2@ 0
                   1500:     ?do ( c-addr u addr )
                   1501:        dup >r 2@ 2over compare 0=
                   1502:        if
                   1503:            2drop rdrop unloop
                   1504:            true EXIT
                   1505:        then
                   1506:        r> cell+ cell+
                   1507:     loop
                   1508:     2drop drop false ;
                   1509: 
                   1510: : add-included-file ( c-addr u -- ) \ gforth
1.3       pazsan   1511:     \G add name c-addr u to included-files
1.1       pazsan   1512:     included-files 2@ 2* cells 2 cells extend-mem
                   1513:     2/ cell / included-files 2!
                   1514:     2! ;
                   1515: \    included-files 2@ tuck 1+ 2* cells resize throw
                   1516: \    swap 2dup 1+ included-files 2!
                   1517: \    2* cells + 2! ;
                   1518: 
                   1519: : included1 ( i*x file-id c-addr u -- j*x ) \ gforth
1.3       pazsan   1520:     \G include the file file-id with the name given by c-addr u
1.1       pazsan   1521:     loadfilename# @ >r
                   1522:     save-mem add-included-file ( file-id )
                   1523:     included-files 2@ nip 1- loadfilename# !
                   1524:     ['] include-file catch
                   1525:     r> loadfilename# !
                   1526:     throw ;
                   1527:     
                   1528: : included ( i*x addr u -- j*x ) \ file
                   1529:     open-path-file included1 ;
                   1530: 
                   1531: : required ( i*x addr u -- j*x ) \ gforth
1.3       pazsan   1532:     \G include the file with the name given by addr u, if it is not
                   1533:     \G included already. Currently this works by comparing the name of
                   1534:     \G the file (with path) against the names of earlier included
                   1535:     \G files; however, it would probably be better to fstat the file,
                   1536:     \G and compare the device and inode. The advantages would be: no
                   1537:     \G problems with several paths to the same file (e.g., due to
                   1538:     \G links) and we would catch files included with include-file and
                   1539:     \G write a require-file.
1.1       pazsan   1540:     open-path-file 2dup included?
                   1541:     if
                   1542:        2drop close-file throw
                   1543:     else
                   1544:        included1
                   1545:     then ;
                   1546: 
                   1547: \ HEX DECIMAL                                           2may93jaw
                   1548: 
                   1549: : decimal ( -- ) \ core
                   1550:     a base ! ;
                   1551: : hex ( -- ) \ core-ext
                   1552:     10 base ! ;
                   1553: 
                   1554: \ DEPTH                                                 9may93jaw
                   1555: 
                   1556: : depth ( -- +n ) \ core
                   1557:     sp@ s0 @ swap - cell / ;
                   1558: : clearstack ( ... -- )
                   1559:     s0 @ sp! ;
                   1560: 
                   1561: \ INCLUDE                                               9may93jaw
                   1562: 
                   1563: : include  ( "file" -- ) \ gforth
                   1564:   name included ;
                   1565: 
                   1566: : require  ( "file" -- ) \ gforth
                   1567:   name required ;
                   1568: 
                   1569: \ RECURSE                                               17may93jaw
                   1570: 
                   1571: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
                   1572:     lastxt compile, ; immediate restrict
                   1573: ' reveal alias recursive ( -- ) \ gforth
                   1574:        immediate
                   1575: 
                   1576: \ */MOD */                                              17may93jaw
                   1577: 
                   1578: \ !! I think */mod should have the same rounding behaviour as / - anton
                   1579: : */mod ( n1 n2 n3 -- n4 n5 ) \ core   star-slash-mod
                   1580:     >r m* r> sm/rem ;
                   1581: 
                   1582: : */ ( n1 n2 n3 -- n4 ) \ core star-slash
                   1583:     */mod nip ;
                   1584: 
                   1585: \ EVALUATE                                              17may93jaw
                   1586: 
                   1587: : evaluate ( c-addr len -- ) \ core,block
                   1588:   push-file  #tib ! >tib !
                   1589:   >in off blk off loadfile off -1 loadline !
                   1590:   ['] interpret catch
                   1591:   pop-file throw ;
                   1592: 
                   1593: : abort ( ?? -- ?? ) \ core,exception-ext
                   1594:     -1 throw ;
                   1595: 
                   1596: \+ environment? true ENV" CORE"
                   1597: \ core wordset is now complete!
                   1598: 
                   1599: \ Quit                                                 13feb93py
                   1600: 
                   1601: Defer 'quit
                   1602: Defer .status
                   1603: : prompt        state @ IF ."  compiled" EXIT THEN ."  ok" ;
                   1604: : (quit)        BEGIN .status cr query interpret prompt AGAIN ;
                   1605: ' (quit) IS 'quit
                   1606: 
                   1607: \ DOERROR (DOERROR)                                     13jun93jaw
                   1608: 
                   1609: 8 Constant max-errors
                   1610: Variable error-stack  0 error-stack !
                   1611: max-errors 6 * cells allot
                   1612: \ format of one cell:
                   1613: \ source ( addr u )
                   1614: \ >in
                   1615: \ line-number
                   1616: \ Loadfilename ( addr u )
                   1617: 
                   1618: : dec. ( n -- ) \ gforth
                   1619:     \ print value in decimal representation
                   1620:     base @ decimal swap . base ! ;
                   1621: 
                   1622: : hex. ( u -- ) \ gforth
                   1623:     \ print value as unsigned hex number
                   1624:     '$ emit base @ swap hex u. base ! ;
                   1625: 
                   1626: : typewhite ( addr u -- ) \ gforth
                   1627:     \ like type, but white space is printed instead of the characters
                   1628:     bounds ?do
                   1629:        i c@ 9 = if \ check for tab
                   1630:            9
                   1631:        else
                   1632:            bl
                   1633:        then
                   1634:        emit
                   1635:     loop ;
                   1636: 
                   1637: DEFER DOERROR
                   1638: 
                   1639: : .error-frame ( addr1 u1 n1 n2 addr2 u2 -- )
                   1640:   cr error-stack @
                   1641:   IF
                   1642:      ." in file included from "
                   1643:      type ." :" dec.  drop 2drop
                   1644:   ELSE
                   1645:      type ." :" dec.
                   1646:      cr dup 2over type cr drop
                   1647:      nip -trailing 1- ( line-start index2 )
                   1648:      0 >r  BEGIN
                   1649:                   2dup + c@ bl >  WHILE
                   1650:                  r> 1+ >r  1- dup 0<  UNTIL  THEN  1+
                   1651:      ( line-start index1 )
                   1652:      typewhite
                   1653:      r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
                   1654:                   [char] ^ emit
                   1655:      loop
                   1656:   THEN
                   1657: ;
                   1658: 
                   1659: : (DoError) ( throw-code -- )
                   1660:   sourceline# IF
                   1661:                source >in @ sourceline# 0 0 .error-frame
                   1662:   THEN
                   1663:   error-stack @ 0 ?DO
                   1664:     -1 error-stack +!
                   1665:     error-stack dup @ 6 * cells + cell+
                   1666:     6 cells bounds DO
                   1667:       I @
                   1668:     cell +LOOP
                   1669:     .error-frame
                   1670:   LOOP
                   1671:   dup -2 =
                   1672:   IF 
                   1673:      "error @ ?dup
                   1674:      IF
                   1675:         cr count type 
                   1676:      THEN
                   1677:      drop
                   1678:   ELSE
                   1679:      .error
                   1680:   THEN
                   1681:   normal-dp dpp ! ;
                   1682: 
                   1683: ' (DoError) IS DoError
                   1684: 
                   1685: : quit ( ?? -- ?? ) \ core
                   1686:     r0 @ rp! handler off >tib @ >r
                   1687:     BEGIN
                   1688:        postpone [
                   1689:        ['] 'quit CATCH dup
                   1690:     WHILE
                   1691:        DoError r@ >tib ! r@ tibstack !
                   1692:     REPEAT
                   1693:     drop r> >tib ! ;
                   1694: 
                   1695: \ Cold                                                 13feb93py
                   1696: 
                   1697: \ : .name ( name -- ) name>string type space ;
                   1698: \ : words  listwords @
                   1699: \          BEGIN  @ dup  WHILE  dup .name  REPEAT drop ;
                   1700: 
                   1701: : cstring>sstring  ( cstring -- addr n ) \ gforth      cstring-to-sstring
                   1702:     -1 0 scan 0 swap 1+ /string ;
                   1703: : arg ( n -- addr count ) \ gforth
                   1704:     cells argv @ + @ cstring>sstring ;
                   1705: : #!       postpone \ ;  immediate
                   1706: 
                   1707: Create pathstring 2 cells allot \ string
                   1708: Create pathdirs   2 cells allot \ dir string array, pointer and count
                   1709: Variable argv
                   1710: Variable argc
                   1711: 
                   1712: 0 Value script? ( -- flag )
                   1713: 
                   1714: : process-path ( addr1 u1 -- addr2 u2 )
                   1715:     \ addr1 u1 is a path string, addr2 u2 is an array of dir strings
                   1716:     align here >r
                   1717:     BEGIN
                   1718:        over >r [char] : scan
                   1719:        over r> tuck - ( rest-str this-str )
                   1720:        dup
                   1721:        IF
                   1722:            2dup 1- chars + c@ [char] / <>
                   1723:            IF
                   1724:                2dup chars + [char] / swap c!
                   1725:                1+
                   1726:            THEN
                   1727:            2,
                   1728:        ELSE
                   1729:            2drop
                   1730:        THEN
                   1731:        dup
                   1732:     WHILE
                   1733:        1 /string
                   1734:     REPEAT
                   1735:     2drop
                   1736:     here r> tuck - 2 cells / ;
                   1737: 
                   1738: : do-option ( addr1 len1 addr2 len2 -- n )
                   1739:     2swap
                   1740:     2dup s" -e"         compare  0= >r
                   1741:     2dup s" --evaluate" compare  0= r> or
                   1742:     IF  2drop dup >r ['] evaluate catch
                   1743:        ?dup IF  dup >r DoError r> negate (bye)  THEN
                   1744:        r> >tib +!  2 EXIT  THEN
                   1745:     ." Unknown option: " type cr 2drop 1 ;
                   1746: 
                   1747: : process-args ( -- )
                   1748:     >tib @ >r
                   1749:     argc @ 1
                   1750:     ?DO
                   1751:        I arg over c@ [char] - <>
                   1752:        IF
                   1753:            required 1
                   1754:        ELSE
                   1755:            I 1+ argc @ =  IF  s" "  ELSE  I 1+ arg  THEN
                   1756:            do-option
                   1757:        THEN
                   1758:     +LOOP
                   1759:     r> >tib ! ;
                   1760: 
                   1761: Defer 'cold ' noop IS 'cold
                   1762: 
                   1763: : cold ( -- ) \ gforth
                   1764:     stdout TO outfile-id
                   1765:     pathstring 2@ process-path pathdirs 2!
                   1766:     init-included-files
                   1767:     'cold
                   1768:     argc @ 1 >
                   1769:     IF
                   1770:        true to script?
                   1771:        ['] process-args catch ?dup
                   1772:        IF
                   1773:            dup >r DoError cr r> negate (bye)
                   1774:        THEN
                   1775:        cr
                   1776:     THEN
                   1777:     false to script?
                   1778:     ." GForth " version-string type ." , Copyright (C) 1994-1996 Free Software Foundation, Inc." cr
                   1779:     ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'" cr
                   1780:     ." Type `bye' to exit"
                   1781:     loadline off quit ;
                   1782: 
                   1783: : license ( -- ) \ gforth
                   1784:  cr
                   1785:  ." This program is free software; you can redistribute it and/or modify" cr
                   1786:  ." it under the terms of the GNU General Public License as published by" cr
                   1787:  ." the Free Software Foundation; either version 2 of the License, or" cr
                   1788:  ." (at your option) any later version." cr cr
                   1789: 
                   1790:  ." This program is distributed in the hope that it will be useful," cr
                   1791:  ." but WITHOUT ANY WARRANTY; without even the implied warranty of" cr
                   1792:  ." MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" cr
                   1793:  ." GNU General Public License for more details." cr cr
                   1794: 
                   1795:  ." You should have received a copy of the GNU General Public License" cr
                   1796:  ." along with this program; if not, write to the Free Software" cr
                   1797:  ." Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA." cr ;
                   1798: 
                   1799: : boot ( path **argv argc -- )
1.4     ! anton    1800:   argc ! argv ! cstring>sstring save-mem pathstring 2!  main-task up!
1.1       pazsan   1801:   sp@ dup s0 ! $10 + dup >tib ! tibstack ! #tib off >in off
                   1802:   rp@ r0 !  fp@ f0 !  ['] cold catch DoError bye ;
                   1803: 
                   1804: : bye ( -- ) \ tools-ext
                   1805:     script? 0= IF  cr  THEN  0 (bye) ;
                   1806: 
                   1807: \ **argv may be scanned by the C starter to get some important
                   1808: \ information, as -display and -geometry for an X client FORTH
                   1809: \ or space and stackspace overrides
                   1810: 
                   1811: \ 0 arg contains, however, the name of the program.
                   1812: 
                   1813: 

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