Annotation of gforth/kernal.fs, revision 1.61

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

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