Annotation of gforth/kernel.fs, revision 1.12

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
1.12    ! anton     575: : plain-( ( 'ccc<close-paren>' -- ; )
        !           576:     [char] ) parse 2drop ;
        !           577: 
        !           578: : file-( ( 'ccc<close-paren>' -- ; )
1.1       pazsan    579:     BEGIN
1.12    ! anton     580:        >in @
        !           581:        [char] ) parse nip
        !           582:        >in @ rot - = \ is there no delimter?
1.1       pazsan    583:     WHILE
1.12    ! anton     584:        refill 0=
        !           585:        IF
        !           586:            warnings @
        !           587:            IF
        !           588:                ." warning: ')' missing" cr
        !           589:            THEN
        !           590:            EXIT
1.1       pazsan    591:        THEN
1.12    ! anton     592:     REPEAT ;
        !           593: 
        !           594: : ( ( compilation 'ccc<close-paren>' -- ; run-time -- ) \ core,file    paren
        !           595:     loadfile @
        !           596:     IF
        !           597:        file-(
        !           598:     ELSE
        !           599:        plain-(
        !           600:     THEN ; immediate
        !           601: 
1.1       pazsan    602: : \ ( -- ) \ core-ext backslash
                    603:     blk @
                    604:     IF
                    605:        >in @ c/l / 1+ c/l * >in !
                    606:        EXIT
                    607:     THEN
                    608:     source >in ! drop ; immediate
                    609: 
                    610: : \G ( -- ) \ gforth backslash
                    611:     POSTPONE \ ; immediate
                    612: 
                    613: \ error handling                                       22feb93py
                    614: \ 'abort thrown out!                                   11may93jaw
                    615: 
                    616: : (abort")
                    617:     "lit >r
                    618:     IF
                    619:        r> "error ! -2 throw
                    620:     THEN
                    621:     rdrop ;
                    622: : abort" ( compilation 'ccc"' -- ; run-time f -- ) \ core,exception-ext        abort-quote
                    623:     postpone (abort") ," ;        immediate restrict
                    624: 
                    625: \ Header states                                        23feb93py
                    626: 
                    627: : cset ( bmask c-addr -- )
                    628:     tuck c@ or swap c! ; 
                    629: : creset ( bmask c-addr -- )
                    630:     tuck c@ swap invert and swap c! ; 
                    631: : ctoggle ( bmask c-addr -- )
                    632:     tuck c@ xor swap c! ; 
                    633: 
                    634: : lastflags ( -- c-addr )
                    635:     \ the address of the flags byte in the last header
                    636:     \ aborts if the last defined word was headerless
                    637:     last @ dup 0= abort" last word was headerless" cell+ ;
                    638: 
1.2       anton     639: : immediate ( -- ) \ core
                    640:     immediate-mask lastflags cset ;
                    641: : restrict ( -- ) \ gforth
                    642:     restrict-mask lastflags cset ;
                    643: ' restrict alias compile-only ( -- ) \ gforth
1.1       pazsan    644: 
                    645: \ Header                                               23feb93py
                    646: 
                    647: \ input-stream, nextname and noname are quite ugly (passing
                    648: \ information through global variables), but they are useful for dealing
                    649: \ with existing/independent defining words
                    650: 
                    651: defer (header)
                    652: defer header ( -- ) \ gforth
                    653: ' (header) IS header
                    654: 
                    655: : string, ( c-addr u -- ) \ gforth
1.3       pazsan    656:     \G puts down string as cstring
1.1       pazsan    657:     dup c, here swap chars dup allot move ;
                    658: 
                    659: : header, ( c-addr u -- ) \ gforth
                    660:     name-too-long?
                    661:     align here last !
                    662:     current @ 1 or A,  \ link field; before revealing, it contains the
                    663:                        \ tagged reveal-into wordlist
                    664:     string, cfalign
                    665:     alias-mask lastflags cset ;
                    666: 
                    667: : input-stream-header ( "name" -- )
                    668:     name name-too-short? header, ;
                    669: : input-stream ( -- )  \ general
1.3       pazsan    670:     \G switches back to getting the name from the input stream ;
1.1       pazsan    671:     ['] input-stream-header IS (header) ;
                    672: 
                    673: ' input-stream-header IS (header)
                    674: 
                    675: \ !! make that a 2variable
                    676: create nextname-buffer 32 chars allot
                    677: 
                    678: : nextname-header ( -- )
                    679:     nextname-buffer count header,
                    680:     input-stream ;
                    681: 
                    682: \ the next name is given in the string
                    683: : nextname ( c-addr u -- ) \ gforth
                    684:     name-too-long?
                    685:     nextname-buffer c! ( c-addr )
                    686:     nextname-buffer count move
                    687:     ['] nextname-header IS (header) ;
                    688: 
                    689: : noname-header ( -- )
                    690:     0 last ! cfalign
                    691:     input-stream ;
                    692: 
                    693: : noname ( -- ) \ gforth
                    694: \ the next defined word remains anonymous. The xt of that word is given by lastxt
                    695:     ['] noname-header IS (header) ;
                    696: 
                    697: : lastxt ( -- xt ) \ gforth
                    698: \ 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
                    699:     lastcfa @ ;
                    700: 
                    701: : Alias    ( cfa "name" -- ) \ gforth
                    702:     Header reveal
                    703:     alias-mask lastflags creset
                    704:     dup A, lastcfa ! ;
                    705: 
1.4       anton     706: : name>string ( nt -- addr count ) \ gforth    name-to-string
                    707:     \g @var{addr count} is the name of the word represented by @var{nt}.
                    708:     cell+ count $1F and ;
1.1       pazsan    709: 
                    710: Create ???  0 , 3 c, char ? c, char ? c, char ? c,
1.4       anton     711: : >name ( cfa -- nt ) \ gforth to-name
1.1       pazsan    712:  $21 cell do
                    713:    dup i - count $9F and + cfaligned over alias-mask + = if
                    714:      i - cell - unloop exit
                    715:    then
                    716:  cell +loop
                    717:  drop ??? ( wouldn't 0 be better? ) ;
                    718: 
                    719: \ threading                                   17mar93py
                    720: 
                    721: : cfa,     ( code-address -- )  \ gforth       cfa-comma
                    722:     here
                    723:     dup lastcfa !
                    724:     0 A, 0 ,  code-address! ;
                    725: : compile, ( xt -- )   \ core-ext      compile-comma
                    726:     A, ;
                    727: : !does    ( addr -- ) \ gforth        store-does
                    728:     lastxt does-code! ;
                    729: : (does>)  ( R: addr -- )
                    730:     r> /does-handler + !does ;
                    731: : dodoes,  ( -- )
                    732:   here /does-handler allot does-handler! ;
                    733: 
                    734: : Create ( "name" -- ) \ core
                    735:     Header reveal dovar: cfa, ;
                    736: 
                    737: \ Create Variable User Constant                        17mar93py
                    738: 
                    739: : Variable ( "name" -- ) \ core
                    740:     Create 0 , ;
                    741: : AVariable ( "name" -- ) \ gforth
                    742:     Create 0 A, ;
                    743: : 2VARIABLE ( "name" -- ) \ double
                    744:     create 0 , 0 , ;
                    745:     
                    746: : User ( "name" -- ) \ gforth
                    747:     Variable ;
                    748: : AUser ( "name" -- ) \ gforth
                    749:     AVariable ;
                    750: 
                    751: : (Constant)  Header reveal docon: cfa, ;
                    752: : Constant ( w "name" -- ) \ core
1.3       pazsan    753:     \G Defines constant @var{name}
                    754:     \G  
                    755:     \G @var{name} execution: @var{-- w}
1.1       pazsan    756:     (Constant) , ;
                    757: : AConstant ( addr "name" -- ) \ gforth
                    758:     (Constant) A, ;
                    759: 
                    760: : 2Constant ( w1 w2 "name" -- ) \ double
                    761:     Create ( w1 w2 "name" -- )
                    762:         2,
                    763:     DOES> ( -- w1 w2 )
                    764:         2@ ;
                    765:     
                    766: \ IS Defer What's Defers TO                            24feb93py
                    767: 
                    768: : Defer ( "name" -- ) \ gforth
                    769:     \ !! shouldn't it be initialized with abort or something similar?
                    770:     Header Reveal dodefer: cfa,
                    771:     ['] noop A, ;
                    772: \     Create ( -- ) 
                    773: \      ['] noop A,
                    774: \     DOES> ( ??? )
                    775: \      perform ;
                    776: 
                    777: : Defers ( "name" -- ) \ gforth
                    778:     ' >body @ compile, ; immediate
                    779: 
                    780: \ : ;                                                  24feb93py
                    781: 
                    782: defer :-hook ( sys1 -- sys2 )
                    783: defer ;-hook ( sys2 -- sys1 )
                    784: 
                    785: : : ( "name" -- colon-sys ) \ core     colon
                    786:     Header docol: cfa, defstart ] :-hook ;
                    787: : ; ( compilation colon-sys -- ; run-time nest-sys ) \ core    semicolon
                    788:     ;-hook ?struc postpone exit reveal postpone [ ; immediate restrict
                    789: 
                    790: : :noname ( -- xt colon-sys ) \ core-ext       colon-no-name
                    791:     0 last !
                    792:     cfalign here docol: cfa, 0 ] :-hook ;
                    793: 
                    794: \ Search list handling                                 23feb93py
                    795: 
                    796: AVariable current ( -- addr ) \ gforth
                    797: 
                    798: : last?   ( -- false / nfa nfa )
                    799:     last @ ?dup ;
1.4       anton     800: : (reveal) ( nt wid -- )
1.1       pazsan    801:     ( wid>wordlist-id ) dup >r
                    802:     @ over ( name>link ) ! 
                    803:     r> ! ;
                    804: 
                    805: \ object oriented search list                          17mar93py
                    806: 
                    807: \ word list structure:
                    808: 
                    809: struct
1.4       anton     810:   1 cells: field find-method   \ xt: ( c_addr u wid -- nt )
                    811:   1 cells: field reveal-method \ xt: ( nt wid -- ) \ used by dofield:, must be field
1.1       pazsan    812:   1 cells: field rehash-method \ xt: ( wid -- )
                    813: \   \ !! what else
                    814: end-struct wordlist-map-struct
                    815: 
                    816: struct
                    817:   1 cells: field wordlist-id \ not the same as wid; representation depends on implementation
                    818:   1 cells: field wordlist-map \ pointer to a wordlist-map-struct
                    819:   1 cells: field wordlist-link \ link field to other wordlists
                    820:   1 cells: field wordlist-extend \ points to wordlist extensions (eg hash)
                    821: end-struct wordlist-struct
                    822: 
1.4       anton     823: : f83find      ( addr len wordlist -- nt / false )
1.1       pazsan    824:     ( wid>wordlist-id ) @ (f83find) ;
                    825: 
                    826: \ Search list table: find reveal
                    827: Create f83search ( -- wordlist-map )
                    828:     ' f83find A,  ' (reveal) A,  ' drop A,
                    829: 
                    830: Create forth-wordlist  NIL A, G f83search T A, NIL A, NIL A,
                    831: AVariable lookup       G forth-wordlist lookup T !
                    832: G forth-wordlist current T !
                    833: 
                    834: \ higher level parts of find
                    835: 
                    836: ( struct )
                    837: 0 >body cell
                    838:   1 cells: field interpret/compile-int
                    839:   1 cells: field interpret/compile-comp
                    840: end-struct interpret/compile-struct
                    841: 
                    842: : interpret/compile? ( xt -- flag )
                    843:     >does-code ['] S" >does-code = ;
                    844: 
                    845: : (cfa>int) ( cfa -- xt )
                    846:     dup interpret/compile?
                    847:     if
                    848:        interpret/compile-int @
                    849:     then ;
                    850: 
                    851: : (x>int) ( cfa b -- xt )
                    852:     \ get interpretation semantics of name
                    853:     restrict-mask and
                    854:     if
                    855:        drop ['] compile-only-error
                    856:     else
                    857:        (cfa>int)
                    858:     then ;
                    859: 
1.4       anton     860: : name>int ( nt -- xt ) \ gforth
                    861:     \G @var{xt} represents the interpretation semantics of the word
                    862:     \G @var{nt}. Produces @code{' compile-only-error} if
                    863:     \G @var{nt} is compile-only.
1.1       pazsan    864:     (name>x) (x>int) ;
                    865: 
1.4       anton     866: : name?int ( nt -- xt ) \ gforth
                    867:     \G Like name>int, but throws an error if compile-only.
1.1       pazsan    868:     (name>x) restrict-mask and
                    869:     if
                    870:        compile-only-error \ does not return
                    871:     then
                    872:     (cfa>int) ;
                    873: 
1.4       anton     874: : name>comp ( nt -- w xt ) \ gforth
1.10      anton     875:     \G @var{w xt} is the compilation token for the word @var{nt}.
1.1       pazsan    876:     (name>x) >r dup interpret/compile?
                    877:     if
                    878:        interpret/compile-comp @
                    879:     then
                    880:     r> immediate-mask and if
                    881:        ['] execute
                    882:     else
                    883:        ['] compile,
                    884:     then ;
                    885: 
1.4       anton     886: : (search-wordlist)  ( addr count wid -- nt / false )
1.1       pazsan    887:     dup wordlist-map @ find-method perform ;
                    888: 
                    889: : flag-sign ( f -- 1|-1 )
                    890:     \ true becomes 1, false -1
                    891:     0= 2* 1+ ;
                    892: 
                    893: : (name>intn) ( nfa -- xt +-1 )
                    894:     (name>x) tuck (x>int) ( b xt )
                    895:     swap immediate-mask and flag-sign ;
                    896: 
                    897: : search-wordlist ( addr count wid -- 0 / xt +-1 ) \ search
                    898:     \ xt is the interpretation semantics
                    899:     (search-wordlist) dup if
                    900:        (name>intn)
                    901:     then ;
                    902: 
1.4       anton     903: : find-name ( c-addr u -- nt/0 ) \ gforth
                    904:     \g Find the name @var{c-addr u} in the current search
                    905:     \g order. Return its nt, if found, otherwise 0.
1.1       pazsan    906:     lookup @ (search-wordlist) ;
                    907: 
                    908: : sfind ( c-addr u -- 0 / xt +-1  ) \ gforth-obsolete
                    909:     find-name dup
1.4       anton     910:     if ( nt )
1.1       pazsan    911:        state @
                    912:        if
                    913:            name>comp ['] execute = flag-sign
                    914:        else
                    915:            (name>intn)
                    916:        then
                    917:     then ;
                    918: 
                    919: : find ( c-addr -- xt +-1 / c-addr 0 ) \ core
                    920:     dup count sfind dup
                    921:     if
                    922:        rot drop
                    923:     then ;
                    924: 
1.4       anton     925: : (') ( "name" -- nt ) \ gforth
1.1       pazsan    926:     name find-name dup 0=
                    927:     IF
                    928:        drop -&13 bounce
                    929:     THEN  ;
                    930: 
1.4       anton     931: : [(')]  ( compilation "name" -- ; run-time -- nt ) \ gforth   bracket-paren-tick
1.1       pazsan    932:     (') postpone ALiteral ; immediate restrict
                    933: 
                    934: : '    ( "name" -- xt ) \ core tick
1.4       anton     935:     \g @var{xt} represents @var{name}'s interpretation
                    936:     \g semantics. Performs @code{-14 throw} if the word has no
                    937:     \g interpretation semantics.
1.1       pazsan    938:     (') name?int ;
1.4       anton     939: : [']  ( compilation. "name" -- ; run-time. -- xt ) \ core     bracket-tick
                    940:     \g @var{xt} represents @var{name}'s interpretation
                    941:     \g semantics. Performs @code{-14 throw} if the word has no
                    942:     \g interpretation semantics.
1.1       pazsan    943:     ' postpone ALiteral ; immediate restrict
                    944: 
                    945: : COMP'    ( "name" -- w xt ) \ gforth c-tick
1.4       anton     946:     \g @var{w xt} represents @var{name}'s compilation semantics.
1.1       pazsan    947:     (') name>comp ;
                    948: : [COMP']  ( compilation "name" -- ; run-time -- w xt ) \ gforth       bracket-comp-tick
1.4       anton     949:     \g @var{w xt} represents @var{name}'s compilation semantics.
1.1       pazsan    950:     COMP' swap POSTPONE Aliteral POSTPONE ALiteral ; immediate restrict
                    951: 
                    952: \ reveal words
                    953: 
                    954: Variable warnings ( -- addr ) \ gforth
                    955: G -1 warnings T !
                    956: 
                    957: : check-shadow  ( addr count wid -- )
1.3       pazsan    958: \G prints a warning if the string is already present in the wordlist
1.1       pazsan    959:  >r 2dup 2dup r> (search-wordlist) warnings @ and ?dup if
                    960:    ." redefined " name>string 2dup type
                    961:    compare 0<> if
                    962:      ."  with " type
                    963:    else
                    964:      2drop
                    965:    then
                    966:    space space EXIT
                    967:  then
                    968:  2drop 2drop ;
                    969: 
                    970: : reveal ( -- ) \ gforth
                    971:     last?
                    972:     if \ the last word has a header
                    973:        dup ( name>link ) @ 1 and
                    974:        if \ it is still hidden
1.4       anton     975:            dup ( name>link ) @ 1 xor           ( nt wid )
                    976:            2dup >r name>string r> check-shadow ( nt wid )
1.1       pazsan    977:            dup wordlist-map @ reveal-method perform
                    978:        then
                    979:     then ;
                    980: 
                    981: : rehash  ( wid -- )
                    982:     dup wordlist-map @ rehash-method perform ;
                    983: 
                    984: \ Input                                                13feb93py
                    985: 
                    986: 07 constant #bell ( -- c ) \ gforth
                    987: 08 constant #bs ( -- c ) \ gforth
                    988: 09 constant #tab ( -- c ) \ gforth
                    989: 7F constant #del ( -- c ) \ gforth
                    990: 0D constant #cr   ( -- c ) \ gforth
                    991: \ the newline key code
                    992: 0C constant #ff ( -- c ) \ gforth
                    993: 0A constant #lf ( -- c ) \ gforth
                    994: 
                    995: : bell  #bell emit ;
                    996: : cr ( -- ) \ core
                    997:     \ emit a newline
                    998:     #lf ( sic! ) emit ;
                    999: 
                   1000: \ : backspaces  0 ?DO  #bs emit  LOOP ;
                   1001: 
                   1002: : (ins) ( max span addr pos1 key -- max span addr pos2 )
                   1003:     >r 2dup + r@ swap c! r> emit 1+ rot 1+ -rot ;
                   1004: : (bs) ( max span addr pos1 -- max span addr pos2 flag )
                   1005:     dup IF
                   1006:        #bs emit bl emit #bs emit 1- rot 1- -rot
                   1007:     THEN false ;
                   1008: : (ret)  true space ;
                   1009: 
                   1010: Create ctrlkeys
                   1011:   ] false false false false  false false false false
                   1012:     (bs)  false (ret) false  false (ret) false false
                   1013:     false false false false  false false false false
                   1014:     false false false false  false false false false [
                   1015: 
                   1016: defer insert-char
                   1017: ' (ins) IS insert-char
                   1018: defer everychar
                   1019: ' noop IS everychar
                   1020: 
                   1021: : decode ( max span addr pos1 key -- max span addr pos2 flag )
                   1022:   everychar
                   1023:   dup #del = IF  drop #bs  THEN  \ del is rubout
                   1024:   dup bl <   IF  cells ctrlkeys + perform  EXIT  THEN
                   1025:   >r 2over = IF  rdrop bell 0 EXIT  THEN
                   1026:   r> insert-char 0 ;
                   1027: 
                   1028: : accept   ( addr len -- len ) \ core
                   1029:   dup 0< IF    abs over dup 1 chars - c@ tuck type 
                   1030: \ this allows to edit given strings
                   1031:          ELSE  0  THEN rot over
                   1032:   BEGIN  key decode  UNTIL
                   1033:   2drop nip ;
                   1034: 
                   1035: \ Output                                               13feb93py
                   1036: 
                   1037: : (type) ( c-addr u -- ) \ gforth
                   1038:     outfile-id write-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
                   1039: ;
                   1040: 
                   1041: Defer type ( c-addr u -- ) \ core
                   1042: \ defer type for a output buffer or fast
                   1043: \ screen write
                   1044: 
                   1045: ' (type) IS Type
                   1046: 
                   1047: : (emit) ( c -- ) \ gforth
                   1048:     outfile-id emit-file drop \ !! use ?DUP-IF THROW ENDIF instead of DROP ?
                   1049: ;
                   1050: 
                   1051: Defer emit ( c -- ) \ core
                   1052: ' (Emit) IS Emit
                   1053: 
                   1054: Defer key ( -- c ) \ core
                   1055: ' (key) IS key
                   1056: 
                   1057: \ Query                                                07apr93py
                   1058: 
                   1059: : refill ( -- flag ) \ core-ext,block-ext,file-ext
                   1060:   blk @  IF  1 blk +!  true  0 >in !  EXIT  THEN
                   1061:   tib /line
                   1062:   loadfile @ ?dup
                   1063:   IF    read-line throw
                   1064:   ELSE  sourceline# 0< IF 2drop false EXIT THEN
                   1065:         accept true
                   1066:   THEN
                   1067:   1 loadline +!
                   1068:   swap #tib ! 0 >in ! ;
                   1069: 
1.8       anton    1070: : query   ( -- ) \ core-ext
1.3       pazsan   1071:     \G obsolescent
1.8       anton    1072:     tib /line accept #tib ! 0 >in ! ;
1.1       pazsan   1073: 
                   1074: \ File specifiers                                       11jun93jaw
                   1075: 
                   1076: 
                   1077: \ 1 c, here char r c, 0 c,                0 c, 0 c, char b c, 0 c,
                   1078: \ 2 c, here char r c, char + c, 0 c,
                   1079: \ 2 c, here char w c, char + c, 0 c, align
                   1080: 4 Constant w/o ( -- fam ) \ file       w-o
                   1081: 2 Constant r/w ( -- fam ) \ file       r-w
                   1082: 0 Constant r/o ( -- fam ) \ file       r-o
                   1083: 
                   1084: \ BIN WRITE-LINE                                        11jun93jaw
                   1085: 
                   1086: \ : bin           dup 1 chars - c@
                   1087: \                 r/o 4 chars + over - dup >r swap move r> ;
                   1088: 
                   1089: : bin ( fam1 -- fam2 ) \ file
                   1090:     1 or ;
                   1091: 
                   1092: : write-line ( c-addr u fileid -- ior ) \ file
                   1093:     dup >r write-file
                   1094:     ?dup IF
                   1095:        r> drop EXIT
                   1096:     THEN
1.10      anton    1097:     #lf r> emit-file ;
1.1       pazsan   1098: 
                   1099: \ include-file                                         07apr93py
                   1100: 
                   1101: : push-file  ( -- )  r>
                   1102:   sourceline# >r  loadfile @ >r
                   1103:   blk @ >r  tibstack @ >r  >tib @ >r  #tib @ >r
                   1104:   >tib @ tibstack @ = IF  r@ tibstack +!  THEN
                   1105:   tibstack @ >tib ! >in @ >r  >r ;
                   1106: 
                   1107: : pop-file   ( throw-code -- throw-code )
                   1108:   dup IF
                   1109:          source >in @ sourceline# sourcefilename
                   1110:         error-stack dup @ dup 1+
                   1111:         max-errors 1- min error-stack !
                   1112:         6 * cells + cell+
                   1113:         5 cells bounds swap DO
                   1114:                            I !
                   1115:         -1 cells +LOOP
                   1116:   THEN
                   1117:   r>
                   1118:   r> >in !  r> #tib !  r> >tib !  r> tibstack !  r> blk !
                   1119:   r> loadfile ! r> loadline !  >r ;
                   1120: 
                   1121: : read-loop ( i*x -- j*x )
                   1122:   BEGIN  refill  WHILE  interpret  REPEAT ;
                   1123: 
                   1124: : include-file ( i*x fid -- j*x ) \ file
                   1125:   push-file  loadfile !
                   1126:   0 loadline ! blk off  ['] read-loop catch
                   1127:   loadfile @ close-file swap 2dup or
                   1128:   pop-file  drop throw throw ;
                   1129: 
                   1130: create pathfilenamebuf 256 chars allot \ !! make this grow on demand
                   1131: 
                   1132: \ : check-file-prefix  ( addr len -- addr' len' flag )
                   1133: \   dup 0=                    IF  true EXIT  THEN 
                   1134: \   over c@ '/ =              IF  true EXIT  THEN 
                   1135: \   over 2 S" ./" compare 0=  IF  true EXIT  THEN 
                   1136: \   over 3 S" ../" compare 0= IF  true EXIT  THEN
                   1137: \   over 2 S" ~/" compare 0=
                   1138: \   IF     1 /string
                   1139: \          S" HOME" getenv tuck pathfilenamebuf swap move
                   1140: \          2dup + >r pathfilenamebuf + swap move
                   1141: \          pathfilenamebuf r> true
                   1142: \   ELSE   false
                   1143: \   THEN ;
                   1144: 
                   1145: : absolut-path? ( addr u -- flag ) \ gforth
1.3       pazsan   1146:     \G a path is absolute, if it starts with a / or a ~ (~ expansion),
                   1147:     \G or if it is in the form ./* or ../*, extended regexp: ^[/~]|./|../
                   1148:     \G Pathes simply containing a / are not absolute!
1.1       pazsan   1149:     over c@ '/ = >r
                   1150:     over c@ '~ = >r
                   1151:     2dup 2 min S" ./" compare 0= >r
                   1152:          3 min S" ../" compare 0=
                   1153:     r> r> r> or or or ;
                   1154: \   [char] / scan nip 0<> ;    
                   1155: 
                   1156: : open-path-file ( c-addr1 u1 -- file-id c-addr2 u2 ) \ gforth
1.3       pazsan   1157:     \G opens a file for reading, searching in the path for it (unless
                   1158:     \G the filename contains a slash); c-addr2 u2 is the full filename
                   1159:     \G (valid until the next call); if the file is not found (or in
                   1160:     \G case of other errors for each try), -38 (non-existant file) is
                   1161:     \G thrown. Opening for other access modes makes little sense, as
                   1162:     \G the path will usually contain dirs that are only readable for
                   1163:     \G the user
1.1       pazsan   1164:     \ !! use file-status to determine access mode?
                   1165:     2dup absolut-path?
                   1166:     if \ the filename contains a slash
                   1167:        2dup r/o open-file throw ( c-addr1 u1 file-id )
                   1168:        -rot >r pathfilenamebuf r@ cmove ( file-id R: u1 )
                   1169:        pathfilenamebuf r> EXIT
                   1170:     then
                   1171:     pathdirs 2@ 0
                   1172: \    check-file-prefix 0= 
                   1173: \    IF  pathdirs 2@ 0
                   1174:     ?DO ( c-addr1 u1 dirnamep )
                   1175:        dup >r 2@ dup >r pathfilenamebuf swap cmove ( addr u )
                   1176:        2dup pathfilenamebuf r@ chars + swap cmove ( addr u )
                   1177:        pathfilenamebuf over r> + dup >r r/o open-file 0=
                   1178:        IF ( addr u file-id )
                   1179:            nip nip r> rdrop 0 LEAVE
                   1180:        THEN
                   1181:        rdrop drop r> cell+ cell+
                   1182:     LOOP
                   1183: \    ELSE   2dup open-file throw -rot  THEN 
                   1184:     0<> -&38 and throw ( file-id u2 )
                   1185:     pathfilenamebuf swap ;
                   1186: 
                   1187: create included-files 0 , 0 , ( pointer to and count of included files )
                   1188: here ," the terminal" dup c@ swap 1 + swap , A, here 2 cells -
                   1189: create image-included-files  1 , A, ( pointer to and count of included files )
                   1190: \ included-files points to ALLOCATEd space, while image-included-files
                   1191: \ points to ALLOTed objects, so it survives a save-system
                   1192: 
                   1193: : loadfilename ( -- a-addr )
1.3       pazsan   1194:     \G a-addr 2@ produces the current file name ( c-addr u )
1.1       pazsan   1195:     included-files 2@ drop loadfilename# @ 2* cells + ;
                   1196: 
                   1197: : sourcefilename ( -- c-addr u ) \ gforth
1.3       pazsan   1198:     \G the name of the source file which is currently the input
                   1199:     \G source.  The result is valid only while the file is being
                   1200:     \G loaded.  If the current input source is no (stream) file, the
                   1201:     \G result is undefined.
1.1       pazsan   1202:     loadfilename 2@ ;
                   1203: 
                   1204: : sourceline# ( -- u ) \ gforth                sourceline-number
1.3       pazsan   1205:     \G the line number of the line that is currently being interpreted
                   1206:     \G from a (stream) file. The first line has the number 1. If the
                   1207:     \G current input source is no (stream) file, the result is
                   1208:     \G undefined.
1.1       pazsan   1209:     loadline @ ;
                   1210: 
                   1211: : init-included-files ( -- )
                   1212:     image-included-files 2@ 2* cells save-mem drop ( addr )
                   1213:     image-included-files 2@ nip included-files 2! ;
                   1214: 
                   1215: : included? ( c-addr u -- f ) \ gforth
1.3       pazsan   1216:     \G true, iff filename c-addr u is in included-files
1.1       pazsan   1217:     included-files 2@ 0
                   1218:     ?do ( c-addr u addr )
                   1219:        dup >r 2@ 2over compare 0=
                   1220:        if
                   1221:            2drop rdrop unloop
                   1222:            true EXIT
                   1223:        then
                   1224:        r> cell+ cell+
                   1225:     loop
                   1226:     2drop drop false ;
                   1227: 
                   1228: : add-included-file ( c-addr u -- ) \ gforth
1.3       pazsan   1229:     \G add name c-addr u to included-files
1.1       pazsan   1230:     included-files 2@ 2* cells 2 cells extend-mem
                   1231:     2/ cell / included-files 2!
                   1232:     2! ;
                   1233: \    included-files 2@ tuck 1+ 2* cells resize throw
                   1234: \    swap 2dup 1+ included-files 2!
                   1235: \    2* cells + 2! ;
                   1236: 
                   1237: : included1 ( i*x file-id c-addr u -- j*x ) \ gforth
1.3       pazsan   1238:     \G include the file file-id with the name given by c-addr u
1.1       pazsan   1239:     loadfilename# @ >r
                   1240:     save-mem add-included-file ( file-id )
                   1241:     included-files 2@ nip 1- loadfilename# !
                   1242:     ['] include-file catch
                   1243:     r> loadfilename# !
                   1244:     throw ;
                   1245:     
                   1246: : included ( i*x addr u -- j*x ) \ file
                   1247:     open-path-file included1 ;
                   1248: 
                   1249: : required ( i*x addr u -- j*x ) \ gforth
1.3       pazsan   1250:     \G include the file with the name given by addr u, if it is not
                   1251:     \G included already. Currently this works by comparing the name of
                   1252:     \G the file (with path) against the names of earlier included
                   1253:     \G files; however, it would probably be better to fstat the file,
                   1254:     \G and compare the device and inode. The advantages would be: no
                   1255:     \G problems with several paths to the same file (e.g., due to
                   1256:     \G links) and we would catch files included with include-file and
                   1257:     \G write a require-file.
1.1       pazsan   1258:     open-path-file 2dup included?
                   1259:     if
                   1260:        2drop close-file throw
                   1261:     else
                   1262:        included1
                   1263:     then ;
                   1264: 
                   1265: \ HEX DECIMAL                                           2may93jaw
                   1266: 
                   1267: : decimal ( -- ) \ core
                   1268:     a base ! ;
                   1269: : hex ( -- ) \ core-ext
                   1270:     10 base ! ;
                   1271: 
                   1272: \ DEPTH                                                 9may93jaw
                   1273: 
                   1274: : depth ( -- +n ) \ core
                   1275:     sp@ s0 @ swap - cell / ;
                   1276: : clearstack ( ... -- )
                   1277:     s0 @ sp! ;
                   1278: 
                   1279: \ INCLUDE                                               9may93jaw
                   1280: 
                   1281: : include  ( "file" -- ) \ gforth
                   1282:   name included ;
                   1283: 
                   1284: : require  ( "file" -- ) \ gforth
                   1285:   name required ;
                   1286: 
                   1287: \ RECURSE                                               17may93jaw
                   1288: 
                   1289: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
                   1290:     lastxt compile, ; immediate restrict
                   1291: ' reveal alias recursive ( -- ) \ gforth
                   1292:        immediate
                   1293: 
                   1294: \ */MOD */                                              17may93jaw
                   1295: 
                   1296: \ !! I think */mod should have the same rounding behaviour as / - anton
                   1297: : */mod ( n1 n2 n3 -- n4 n5 ) \ core   star-slash-mod
                   1298:     >r m* r> sm/rem ;
                   1299: 
                   1300: : */ ( n1 n2 n3 -- n4 ) \ core star-slash
                   1301:     */mod nip ;
                   1302: 
                   1303: \ EVALUATE                                              17may93jaw
                   1304: 
                   1305: : evaluate ( c-addr len -- ) \ core,block
                   1306:   push-file  #tib ! >tib !
                   1307:   >in off blk off loadfile off -1 loadline !
                   1308:   ['] interpret catch
                   1309:   pop-file throw ;
                   1310: 
                   1311: : abort ( ?? -- ?? ) \ core,exception-ext
                   1312:     -1 throw ;
                   1313: 
                   1314: \+ environment? true ENV" CORE"
                   1315: \ core wordset is now complete!
                   1316: 
                   1317: \ Quit                                                 13feb93py
                   1318: 
                   1319: Defer 'quit
                   1320: Defer .status
                   1321: : prompt        state @ IF ."  compiled" EXIT THEN ."  ok" ;
1.8       anton    1322: : (Query)  ( -- )
                   1323:     loadfile off  blk off  refill drop ;
                   1324: : (quit)        BEGIN .status cr (query) interpret prompt AGAIN ;
1.1       pazsan   1325: ' (quit) IS 'quit
                   1326: 
                   1327: \ DOERROR (DOERROR)                                     13jun93jaw
                   1328: 
                   1329: 8 Constant max-errors
                   1330: Variable error-stack  0 error-stack !
                   1331: max-errors 6 * cells allot
                   1332: \ format of one cell:
                   1333: \ source ( addr u )
                   1334: \ >in
                   1335: \ line-number
                   1336: \ Loadfilename ( addr u )
                   1337: 
                   1338: : dec. ( n -- ) \ gforth
                   1339:     \ print value in decimal representation
                   1340:     base @ decimal swap . base ! ;
                   1341: 
                   1342: : hex. ( u -- ) \ gforth
                   1343:     \ print value as unsigned hex number
                   1344:     '$ emit base @ swap hex u. base ! ;
                   1345: 
                   1346: : typewhite ( addr u -- ) \ gforth
                   1347:     \ like type, but white space is printed instead of the characters
                   1348:     bounds ?do
1.10      anton    1349:        i c@ #tab = if \ check for tab
                   1350:            #tab
1.1       pazsan   1351:        else
                   1352:            bl
                   1353:        then
                   1354:        emit
                   1355:     loop ;
                   1356: 
                   1357: DEFER DOERROR
                   1358: 
                   1359: : .error-frame ( addr1 u1 n1 n2 addr2 u2 -- )
                   1360:   cr error-stack @
                   1361:   IF
                   1362:      ." in file included from "
                   1363:      type ." :" dec.  drop 2drop
                   1364:   ELSE
                   1365:      type ." :" dec.
                   1366:      cr dup 2over type cr drop
                   1367:      nip -trailing 1- ( line-start index2 )
                   1368:      0 >r  BEGIN
                   1369:                   2dup + c@ bl >  WHILE
                   1370:                  r> 1+ >r  1- dup 0<  UNTIL  THEN  1+
                   1371:      ( line-start index1 )
                   1372:      typewhite
                   1373:      r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
                   1374:                   [char] ^ emit
                   1375:      loop
                   1376:   THEN
                   1377: ;
                   1378: 
                   1379: : (DoError) ( throw-code -- )
                   1380:   sourceline# IF
                   1381:                source >in @ sourceline# 0 0 .error-frame
                   1382:   THEN
                   1383:   error-stack @ 0 ?DO
                   1384:     -1 error-stack +!
                   1385:     error-stack dup @ 6 * cells + cell+
                   1386:     6 cells bounds DO
                   1387:       I @
                   1388:     cell +LOOP
                   1389:     .error-frame
                   1390:   LOOP
                   1391:   dup -2 =
                   1392:   IF 
                   1393:      "error @ ?dup
                   1394:      IF
                   1395:         cr count type 
                   1396:      THEN
                   1397:      drop
                   1398:   ELSE
                   1399:      .error
                   1400:   THEN
                   1401:   normal-dp dpp ! ;
                   1402: 
                   1403: ' (DoError) IS DoError
                   1404: 
                   1405: : quit ( ?? -- ?? ) \ core
                   1406:     r0 @ rp! handler off >tib @ >r
                   1407:     BEGIN
                   1408:        postpone [
                   1409:        ['] 'quit CATCH dup
                   1410:     WHILE
                   1411:        DoError r@ >tib ! r@ tibstack !
                   1412:     REPEAT
                   1413:     drop r> >tib ! ;
                   1414: 
                   1415: \ Cold                                                 13feb93py
                   1416: 
                   1417: \ : .name ( name -- ) name>string type space ;
                   1418: \ : words  listwords @
                   1419: \          BEGIN  @ dup  WHILE  dup .name  REPEAT drop ;
                   1420: 
                   1421: : cstring>sstring  ( cstring -- addr n ) \ gforth      cstring-to-sstring
                   1422:     -1 0 scan 0 swap 1+ /string ;
                   1423: : arg ( n -- addr count ) \ gforth
                   1424:     cells argv @ + @ cstring>sstring ;
                   1425: : #!       postpone \ ;  immediate
                   1426: 
                   1427: Create pathstring 2 cells allot \ string
                   1428: Create pathdirs   2 cells allot \ dir string array, pointer and count
                   1429: Variable argv
                   1430: Variable argc
                   1431: 
                   1432: 0 Value script? ( -- flag )
                   1433: 
                   1434: : process-path ( addr1 u1 -- addr2 u2 )
                   1435:     \ addr1 u1 is a path string, addr2 u2 is an array of dir strings
                   1436:     align here >r
                   1437:     BEGIN
1.6       anton    1438:        over >r 0 scan
1.1       pazsan   1439:        over r> tuck - ( rest-str this-str )
                   1440:        dup
                   1441:        IF
                   1442:            2dup 1- chars + c@ [char] / <>
                   1443:            IF
                   1444:                2dup chars + [char] / swap c!
                   1445:                1+
                   1446:            THEN
                   1447:            2,
                   1448:        ELSE
                   1449:            2drop
                   1450:        THEN
                   1451:        dup
                   1452:     WHILE
                   1453:        1 /string
                   1454:     REPEAT
                   1455:     2drop
                   1456:     here r> tuck - 2 cells / ;
                   1457: 
                   1458: : do-option ( addr1 len1 addr2 len2 -- n )
                   1459:     2swap
                   1460:     2dup s" -e"         compare  0= >r
                   1461:     2dup s" --evaluate" compare  0= r> or
                   1462:     IF  2drop dup >r ['] evaluate catch
                   1463:        ?dup IF  dup >r DoError r> negate (bye)  THEN
                   1464:        r> >tib +!  2 EXIT  THEN
                   1465:     ." Unknown option: " type cr 2drop 1 ;
                   1466: 
                   1467: : process-args ( -- )
                   1468:     >tib @ >r
                   1469:     argc @ 1
                   1470:     ?DO
                   1471:        I arg over c@ [char] - <>
                   1472:        IF
                   1473:            required 1
                   1474:        ELSE
                   1475:            I 1+ argc @ =  IF  s" "  ELSE  I 1+ arg  THEN
                   1476:            do-option
                   1477:        THEN
                   1478:     +LOOP
                   1479:     r> >tib ! ;
                   1480: 
                   1481: Defer 'cold ' noop IS 'cold
                   1482: 
                   1483: : cold ( -- ) \ gforth
                   1484:     stdout TO outfile-id
                   1485:     pathstring 2@ process-path pathdirs 2!
                   1486:     init-included-files
                   1487:     'cold
                   1488:     argc @ 1 >
                   1489:     IF
                   1490:        true to script?
                   1491:        ['] process-args catch ?dup
                   1492:        IF
                   1493:            dup >r DoError cr r> negate (bye)
                   1494:        THEN
                   1495:        cr
                   1496:     THEN
                   1497:     false to script?
                   1498:     ." GForth " version-string type ." , Copyright (C) 1994-1996 Free Software Foundation, Inc." cr
                   1499:     ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'" cr
                   1500:     ." Type `bye' to exit"
                   1501:     loadline off quit ;
                   1502: 
                   1503: : license ( -- ) \ gforth
                   1504:  cr
                   1505:  ." This program is free software; you can redistribute it and/or modify" cr
                   1506:  ." it under the terms of the GNU General Public License as published by" cr
                   1507:  ." the Free Software Foundation; either version 2 of the License, or" cr
                   1508:  ." (at your option) any later version." cr cr
                   1509: 
                   1510:  ." This program is distributed in the hope that it will be useful," cr
                   1511:  ." but WITHOUT ANY WARRANTY; without even the implied warranty of" cr
                   1512:  ." MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" cr
                   1513:  ." GNU General Public License for more details." cr cr
                   1514: 
                   1515:  ." You should have received a copy of the GNU General Public License" cr
                   1516:  ." along with this program; if not, write to the Free Software" cr
                   1517:  ." Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA." cr ;
                   1518: 
                   1519: : boot ( path **argv argc -- )
1.7       anton    1520:   argc ! argv ! pathstring 2!  main-task up!
1.9       anton    1521:   sp@ s0 !
                   1522:   lp@ forthstart 7 cells + @ - dup >tib ! tibstack ! #tib off >in off
                   1523:   rp@ r0 !
                   1524:   fp@ f0 !
                   1525:   ['] cold catch DoError
                   1526:   bye ;
1.1       pazsan   1527: 
                   1528: : bye ( -- ) \ tools-ext
                   1529:     script? 0= IF  cr  THEN  0 (bye) ;
                   1530: 
                   1531: \ **argv may be scanned by the C starter to get some important
                   1532: \ information, as -display and -geometry for an X client FORTH
                   1533: \ or space and stackspace overrides
                   1534: 
                   1535: \ 0 arg contains, however, the name of the program.
                   1536: 
                   1537: 

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