Annotation of gforth/kernal.fs, revision 1.49

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.48      anton    1341:   ELSE  sourceline# 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.48      anton    1383:   sourceline# >r  loadfile @ >r
1.47      pazsan   1384:   blk @ >r  tibstack @ >r  >tib @ >r  #tib @ >r
                   1385:   >tib @ tibstack @ = IF  r@ tibstack +!  THEN
                   1386:   tibstack @ >tib ! >in @ >r  >r ;
1.12      pazsan   1387: 
1.18      pazsan   1388: : pop-file   ( throw-code -- throw-code )
                   1389:   dup IF
1.48      anton    1390:          source >in @ sourceline# sourcefilename
1.18      pazsan   1391:         error-stack dup @ dup 1+
                   1392:         max-errors 1- min error-stack !
                   1393:         6 * cells + cell+
                   1394:         5 cells bounds swap DO
                   1395:                            I !
                   1396:         -1 cells +LOOP
                   1397:   THEN
                   1398:   r>
1.47      pazsan   1399:   r> >in !  r> #tib !  r> >tib !  r> tibstack !  r> blk !
1.17      pazsan   1400:   r> loadfile ! r> loadline !  >r ;
                   1401: 
                   1402: : read-loop ( i*x -- j*x )
                   1403:   BEGIN  refill  WHILE  interpret  REPEAT ;
1.12      pazsan   1404: 
1.42      anton    1405: : include-file ( i*x fid -- j*x ) \ file
1.12      pazsan   1406:   push-file  loadfile !
1.17      pazsan   1407:   0 loadline ! blk off  ['] read-loop catch
1.20      pazsan   1408:   loadfile @ close-file swap 2dup or
                   1409:   pop-file  drop throw throw ;
1.1       anton    1410: 
1.19      anton    1411: create pathfilenamebuf 256 chars allot \ !! make this grow on demand
                   1412: 
1.37      anton    1413: \ : check-file-prefix  ( addr len -- addr' len' flag )
                   1414: \   dup 0=                    IF  true EXIT  THEN 
                   1415: \   over c@ '/ =              IF  true EXIT  THEN 
                   1416: \   over 2 S" ./" compare 0=  IF  true EXIT  THEN 
                   1417: \   over 3 S" ../" compare 0= IF  true EXIT  THEN
                   1418: \   over 2 S" ~/" compare 0=
                   1419: \   IF     1 /string
                   1420: \          S" HOME" getenv tuck pathfilenamebuf swap move
                   1421: \          2dup + >r pathfilenamebuf + swap move
                   1422: \          pathfilenamebuf r> true
                   1423: \   ELSE   false
                   1424: \   THEN ;
1.36      pazsan   1425: 
1.42      anton    1426: : open-path-file ( c-addr1 u1 -- file-id c-addr2 u2 ) \ gforth
1.37      anton    1427:     \ opens a file for reading, searching in the path for it (unless
                   1428:     \ the filename contains a slash); c-addr2 u2 is the full filename
                   1429:     \ (valid until the next call); if the file is not found (or in
                   1430:     \ case of other errors for each try), -38 (non-existant file) is
                   1431:     \ thrown. Opening for other access modes makes little sense, as
                   1432:     \ the path will usually contain dirs that are only readable for
                   1433:     \ the user
                   1434:     \ !! use file-status to determine access mode?
                   1435:     2dup [char] / scan nip ( 0<> )
                   1436:     if \ the filename contains a slash
                   1437:        2dup r/o open-file throw ( c-addr1 u1 file-id )
                   1438:        -rot >r pathfilenamebuf r@ cmove ( file-id R: u1 )
                   1439:        pathfilenamebuf r> EXIT
                   1440:     then
                   1441:     pathdirs 2@ 0
                   1442: \    check-file-prefix 0= 
                   1443: \    IF  pathdirs 2@ 0
1.19      anton    1444:     ?DO ( c-addr1 u1 dirnamep )
                   1445:        dup >r 2@ dup >r pathfilenamebuf swap cmove ( addr u )
                   1446:        2dup pathfilenamebuf r@ chars + swap cmove ( addr u )
                   1447:        pathfilenamebuf over r> + dup >r r/o open-file 0=
1.36      pazsan   1448:        IF ( addr u file-id )
1.37      anton    1449:            nip nip r> rdrop 0 LEAVE
1.36      pazsan   1450:        THEN
1.19      anton    1451:        rdrop drop r> cell+ cell+
                   1452:     LOOP
1.37      anton    1453: \    ELSE   2dup open-file throw -rot  THEN 
1.19      anton    1454:     0<> -&38 and throw ( file-id u2 )
                   1455:     pathfilenamebuf swap ;
                   1456: 
1.27      anton    1457: create included-files 0 , 0 , ( pointer to and count of included files )
1.49    ! pazsan   1458: here ," the terminal" dup c@ swap 1 + swap , A, here 2 cells -
        !          1459: create image-included-files  1 , A, ( pointer to and count of included files )
1.42      anton    1460: \ included-files points to ALLOCATEd space, while image-included-files
                   1461: \ points to ALLOTed objects, so it survives a save-system
                   1462: 
1.46      anton    1463: : loadfilename ( -- a-addr )
                   1464:     \ a-addr 2@ produces the current file name ( c-addr u )
                   1465:     included-files 2@ drop loadfilename# @ 2* cells + ;
                   1466: 
1.48      anton    1467: : sourcefilename ( -- c-addr u ) \ gforth
                   1468:     \ the name of the source file which is currently the input
                   1469:     \ source.  The result is valid only while the file is being
                   1470:     \ loaded.  If the current input source is no (stream) file, the
                   1471:     \ result is undefined.
                   1472:     loadfilename 2@ ;
                   1473: 
                   1474: : sourceline# ( -- u ) \ gforth                sourceline-number
                   1475:     \ the line number of the line that is currently being interpreted
                   1476:     \ from a (stream) file. The first line has the number 1. If the
                   1477:     \ current input source is no (stream) file, the result is
                   1478:     \ undefined.
                   1479:     loadline @ ;
                   1480: 
1.42      anton    1481: : init-included-files ( -- )
                   1482:     image-included-files 2@ 2* cells save-string drop ( addr )
                   1483:     image-included-files 2@ nip included-files 2! ;
1.27      anton    1484: 
1.42      anton    1485: : included? ( c-addr u -- f ) \ gforth
1.27      anton    1486:     \ true, iff filename c-addr u is in included-files
                   1487:     included-files 2@ 0
                   1488:     ?do ( c-addr u addr )
                   1489:        dup >r 2@ 2over compare 0=
                   1490:        if
                   1491:            2drop rdrop unloop
                   1492:            true EXIT
                   1493:        then
                   1494:        r> cell+ cell+
                   1495:     loop
                   1496:     2drop drop false ;
                   1497: 
1.42      anton    1498: : add-included-file ( c-addr u -- ) \ gforth
1.27      anton    1499:     \ add name c-addr u to included-files
                   1500:     included-files 2@ tuck 1+ 2* cells resize throw
                   1501:     swap 2dup 1+ included-files 2!
                   1502:     2* cells + 2! ;
                   1503: 
1.42      anton    1504: : save-string          ( addr1 u -- addr2 u ) \ gforth
                   1505:     \ !! not a string, but a memblock word
1.27      anton    1506:     swap >r
                   1507:     dup allocate throw
                   1508:     swap 2dup r> -rot move ;
                   1509: 
1.42      anton    1510: : included1 ( i*x file-id c-addr u -- j*x ) \ gforth
1.27      anton    1511:     \ include the file file-id with the name given by c-addr u
1.46      anton    1512:     loadfilename# @ >r
                   1513:     save-string add-included-file ( file-id )
                   1514:     included-files 2@ nip 1- loadfilename# !
1.20      pazsan   1515:     ['] include-file catch
1.46      anton    1516:     r> loadfilename# !
                   1517:     throw ;
1.27      anton    1518:     
1.43      anton    1519: : included ( i*x addr u -- j*x ) \ file
1.27      anton    1520:     open-path-file included1 ;
                   1521: 
1.42      anton    1522: : required ( i*x addr u -- j*x ) \ gforth
1.27      anton    1523:     \ include the file with the name given by addr u, if it is not
                   1524:     \ included already. Currently this works by comparing the name of
                   1525:     \ the file (with path) against the names of earlier included
                   1526:     \ files; however, it would probably be better to fstat the file,
                   1527:     \ and compare the device and inode. The advantages would be: no
                   1528:     \ problems with several paths to the same file (e.g., due to
                   1529:     \ links) and we would catch files included with include-file and
                   1530:     \ write a require-file.
                   1531:     open-path-file 2dup included?
                   1532:     if
                   1533:        2drop close-file throw
                   1534:     else
                   1535:        included1
                   1536:     then ;
1.1       anton    1537: 
                   1538: \ HEX DECIMAL                                           2may93jaw
                   1539: 
1.42      anton    1540: : decimal ( -- ) \ core
                   1541:     a base ! ;
                   1542: : hex ( -- ) \ core-ext
                   1543:     10 base ! ;
1.1       anton    1544: 
                   1545: \ DEPTH                                                 9may93jaw
                   1546: 
1.42      anton    1547: : depth ( -- +n ) \ core
                   1548:     sp@ s0 @ swap - cell / ;
                   1549: : clearstack ( ... -- )
                   1550:     s0 @ sp! ;
1.1       anton    1551: 
                   1552: \ INCLUDE                                               9may93jaw
                   1553: 
1.42      anton    1554: : include  ( "file" -- ) \ gforth
1.20      pazsan   1555:   name included ;
1.27      anton    1556: 
1.42      anton    1557: : require  ( "file" -- ) \ gforth
1.27      anton    1558:   name required ;
1.1       anton    1559: 
                   1560: \ RECURSE                                               17may93jaw
                   1561: 
1.43      anton    1562: : recurse ( compilation -- ; run-time ?? -- ?? ) \ core
1.10      anton    1563:     lastxt compile, ; immediate restrict
1.42      anton    1564: : recursive ( -- ) \ gforth
1.40      pazsan   1565:     reveal last off ; immediate
1.1       anton    1566: 
                   1567: \ */MOD */                                              17may93jaw
                   1568: 
1.13      anton    1569: \ !! I think */mod should have the same rounding behaviour as / - anton
1.42      anton    1570: : */mod ( n1 n2 n3 -- n4 n5 ) \ core   star-slash-mod
                   1571:     >r m* r> sm/rem ;
1.1       anton    1572: 
1.42      anton    1573: : */ ( n1 n2 n3 -- n4 ) \ core star-slash
                   1574:     */mod nip ;
1.1       anton    1575: 
                   1576: \ EVALUATE                                              17may93jaw
                   1577: 
1.42      anton    1578: : evaluate ( c-addr len -- ) \ core,block
1.47      pazsan   1579:   push-file  #tib ! >tib !
1.12      pazsan   1580:   >in off blk off loadfile off -1 loadline !
1.17      pazsan   1581:   ['] interpret catch
                   1582:   pop-file throw ;
1.1       anton    1583: 
1.42      anton    1584: : abort ( ?? -- ?? ) \ core,exception-ext
                   1585:     -1 throw ;
1.1       anton    1586: 
                   1587: \+ environment? true ENV" CORE"
                   1588: \ core wordset is now complete!
                   1589: 
                   1590: \ Quit                                                 13feb93py
                   1591: 
                   1592: Defer 'quit
                   1593: Defer .status
                   1594: : prompt        state @ IF ."  compiled" EXIT THEN ."  ok" ;
                   1595: : (quit)        BEGIN .status cr query interpret prompt AGAIN ;
                   1596: ' (quit) IS 'quit
                   1597: 
                   1598: \ DOERROR (DOERROR)                                     13jun93jaw
                   1599: 
1.18      pazsan   1600: 8 Constant max-errors
                   1601: Variable error-stack  0 error-stack !
                   1602: max-errors 6 * cells allot
                   1603: \ format of one cell:
                   1604: \ source ( addr u )
                   1605: \ >in
                   1606: \ line-number
                   1607: \ Loadfilename ( addr u )
                   1608: 
1.42      anton    1609: : dec. ( n -- ) \ gforth
1.10      anton    1610:     \ print value in decimal representation
                   1611:     base @ decimal swap . base ! ;
                   1612: 
1.42      anton    1613: : typewhite ( addr u -- ) \ gforth
1.10      anton    1614:     \ like type, but white space is printed instead of the characters
1.18      pazsan   1615:     bounds ?do
                   1616:        i c@ 9 = if \ check for tab
1.10      anton    1617:            9
                   1618:        else
                   1619:            bl
                   1620:        then
                   1621:        emit
1.42      anton    1622:     loop ;
1.10      anton    1623: 
1.1       anton    1624: DEFER DOERROR
                   1625: 
1.18      pazsan   1626: : .error-frame ( addr1 u1 n1 n2 addr2 u2 -- )
                   1627:   cr error-stack @
                   1628:   IF
                   1629:      ." in file included from "
                   1630:      type ." :" dec.  drop 2drop
                   1631:   ELSE
                   1632:      type ." :" dec.
                   1633:      cr dup 2over type cr drop
1.20      pazsan   1634:      nip -trailing 1- ( line-start index2 )
1.18      pazsan   1635:      0 >r  BEGIN
1.20      pazsan   1636:                   2dup + c@ bl >  WHILE
                   1637:                  r> 1+ >r  1- dup 0<  UNTIL  THEN  1+
1.18      pazsan   1638:      ( line-start index1 )
                   1639:      typewhite
                   1640:      r> 1 max 0 ?do \ we want at least one "^", even if the length is 0
                   1641:                   [char] ^ emit
                   1642:      loop
                   1643:   THEN
                   1644: ;
                   1645: 
1.1       anton    1646: : (DoError) ( throw-code -- )
1.48      anton    1647:   sourceline# IF
                   1648:                source >in @ sourceline# 0 0 .error-frame
1.18      pazsan   1649:   THEN
                   1650:   error-stack @ 0 ?DO
                   1651:     -1 error-stack +!
                   1652:     error-stack dup @ 6 * cells + cell+
                   1653:     6 cells bounds DO
                   1654:       I @
                   1655:     cell +LOOP
                   1656:     .error-frame
                   1657:   LOOP
                   1658:   dup -2 =
                   1659:   IF 
                   1660:      "error @ ?dup
                   1661:      IF
                   1662:         cr count type 
                   1663:      THEN
                   1664:      drop
                   1665:   ELSE
                   1666:      .error
                   1667:   THEN
                   1668:   normal-dp dpp ! ;
1.1       anton    1669: 
                   1670: ' (DoError) IS DoError
                   1671: 
1.42      anton    1672: : quit ( ?? -- ?? ) \ core
                   1673:     r0 @ rp! handler off >tib @ >r
                   1674:     BEGIN
                   1675:        postpone [
                   1676:        ['] 'quit CATCH dup
                   1677:     WHILE
1.47      pazsan   1678:        DoError r@ >tib ! r@ tibstack !
1.42      anton    1679:     REPEAT
                   1680:     drop r> >tib ! ;
1.1       anton    1681: 
                   1682: \ Cold                                                 13feb93py
                   1683: 
                   1684: \ : .name ( name -- ) cell+ count $1F and type space ;
                   1685: \ : words  listwords @
                   1686: \          BEGIN  @ dup  WHILE  dup .name  REPEAT drop ;
                   1687: 
1.42      anton    1688: : cstring>sstring  ( cstring -- addr n ) \ gforth      cstring-to-sstring
                   1689:     -1 0 scan 0 swap 1+ /string ;
                   1690: : arg ( n -- addr count ) \ gforth
                   1691:     cells argv @ + @ cstring>sstring ;
1.1       anton    1692: : #!       postpone \ ;  immediate
                   1693: 
1.19      anton    1694: Create pathstring 2 cells allot \ string
                   1695: Create pathdirs   2 cells allot \ dir string array, pointer and count
1.1       anton    1696: Variable argv
                   1697: Variable argc
                   1698: 
1.12      pazsan   1699: 0 Value script? ( -- flag )
                   1700: 
1.19      anton    1701: : process-path ( addr1 u1 -- addr2 u2 )
                   1702:     \ addr1 u1 is a path string, addr2 u2 is an array of dir strings
1.44      pazsan   1703:     align here >r
1.19      anton    1704:     BEGIN
                   1705:        over >r [char] : scan
                   1706:        over r> tuck - ( rest-str this-str )
                   1707:        dup
                   1708:        IF
                   1709:            2dup 1- chars + c@ [char] / <>
                   1710:            IF
                   1711:                2dup chars + [char] / swap c!
                   1712:                1+
                   1713:            THEN
                   1714:            2,
                   1715:        ELSE
                   1716:            2drop
                   1717:        THEN
                   1718:        dup
                   1719:     WHILE
                   1720:        1 /string
                   1721:     REPEAT
                   1722:     2drop
                   1723:     here r> tuck - 2 cells / ;
                   1724: 
1.32      anton    1725: : do-option ( addr1 len1 addr2 len2 -- n )
                   1726:     2swap
                   1727:     2dup s" -e"         compare  0= >r
                   1728:     2dup s" --evaluate" compare  0= r> or
                   1729:     IF  2drop dup >r ['] evaluate catch
                   1730:        ?dup IF  dup >r DoError r> negate (bye)  THEN
                   1731:        r> >tib +!  2 EXIT  THEN
                   1732:     ." Unknown option: " type cr 2drop 1 ;
1.12      pazsan   1733: 
1.34      anton    1734: : process-args ( -- )
                   1735:     >tib @ >r
1.19      anton    1736:     argc @ 1
                   1737:     ?DO
                   1738:        I arg over c@ [char] - <>
                   1739:        IF
1.35      anton    1740:            required 1
1.19      anton    1741:        ELSE
1.40      pazsan   1742:            I 1+ argc @ =  IF  s" "  ELSE  I 1+ arg  THEN
                   1743:            do-option
1.19      anton    1744:        THEN
1.25      pazsan   1745:     +LOOP
                   1746:     r> >tib ! ;
1.1       anton    1747: 
1.24      pazsan   1748: Defer 'cold ' noop IS 'cold
                   1749: 
1.42      anton    1750: : cold ( -- ) \ gforth
1.25      pazsan   1751:     pathstring 2@ process-path pathdirs 2!
1.42      anton    1752:     init-included-files
1.24      pazsan   1753:     'cold
1.10      anton    1754:     argc @ 1 >
1.12      pazsan   1755:     IF
1.40      pazsan   1756:        true to script?
1.12      pazsan   1757:        ['] process-args catch ?dup
1.10      anton    1758:        IF
1.12      pazsan   1759:            dup >r DoError cr r> negate (bye)
1.10      anton    1760:        THEN
1.40      pazsan   1761:        cr
1.10      anton    1762:     THEN
1.40      pazsan   1763:     false to script?
1.43      anton    1764:     ." GForth " version-string type ." , Copyright (C) 1994 Free Software Foundation, Inc." cr
                   1765:     ." GForth comes with ABSOLUTELY NO WARRANTY; for details type `license'" cr
1.13      anton    1766:     ." Type `bye' to exit"
1.26      pazsan   1767:     loadline off quit ;
1.1       anton    1768: 
1.42      anton    1769: : license ( -- ) \ gforth
                   1770:  cr
1.12      pazsan   1771:  ." This program is free software; you can redistribute it and/or modify" cr
                   1772:  ." it under the terms of the GNU General Public License as published by" cr
1.13      anton    1773:  ." the Free Software Foundation; either version 2 of the License, or" cr
                   1774:  ." (at your option) any later version." cr cr
1.12      pazsan   1775: 
                   1776:  ." This program is distributed in the hope that it will be useful," cr
                   1777:  ." but WITHOUT ANY WARRANTY; without even the implied warranty of" cr
                   1778:  ." MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the" cr
                   1779:  ." GNU General Public License for more details." cr cr
                   1780: 
                   1781:  ." You should have received a copy of the GNU General Public License" cr
                   1782:  ." along with this program; if not, write to the Free Software" cr
                   1783:  ." Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA." cr ;
                   1784: 
1.19      anton    1785: : boot ( path **argv argc -- )
                   1786:   argc ! argv ! cstring>sstring pathstring 2!  main-task up!
1.47      pazsan   1787:   sp@ dup s0 ! $10 + dup >tib ! tibstack ! #tib off >in off
1.49    ! pazsan   1788:   rp@ r0 !  fp@ f0 !  ['] cold catch DoError bye ;
1.1       anton    1789: 
1.42      anton    1790: : bye ( -- ) \ tools-ext
                   1791:     script? 0= IF  cr  THEN  0 (bye) ;
1.1       anton    1792: 
                   1793: \ **argv may be scanned by the C starter to get some important
                   1794: \ information, as -display and -geometry for an X client FORTH
                   1795: \ or space and stackspace overrides
                   1796: 
                   1797: \ 0 arg contains, however, the name of the program.

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