Annotation of gforth/kernal.fs, revision 1.51

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

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