Annotation of gforth/kernal.fs, revision 1.46

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

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