Annotation of gforth/kernal.fs, revision 1.56

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

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