Annotation of gforth/kernel.fs, revision 1.11

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

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