Annotation of gforth/cross.fs, revision 1.10

1.1       anton       1: \ CROSS.FS     The Cross-Compiler                      06oct92py
1.10    ! anton       2: \ $Id: cross.fs,v 1.9 1994/07/21 10:52:37 pazsan Exp $
1.1       anton       3: \ Idea and implementation: Bernd Paysan (py)
                      4: \ Copyright 1992 by the ANSI figForth Development Group
                      5: 
                      6: \ Log:
                      7: \       changed in ; [ to state off           12may93jaw
                      8: \       included place +place                 12may93jaw
                      9: \       for a created word (variable, constant...)
                     10: \       is now an alias in the target voabulary.
                     11: \       this means it is no longer necessary to
                     12: \       switch between vocabularies for variable
                     13: \       initialization                        12may93jaw
                     14: \       discovered error in DOES>
                     15: \       replaced !does with (;code)           16may93jaw
                     16: \       made complete redesign and
                     17: \       introduced two vocs method
                     18: \       to be asure that the right words
                     19: \       are found                             08jun93jaw
                     20: \       btw:  ! works not with 16 bit
                     21: \             targets                         09jun93jaw
                     22: \       added: 2user and value                11jun93jaw
                     23: 
1.9       pazsan     24: \ include other.fs       \ ansforth extentions for cross
1.1       anton      25: 
1.5       pazsan     26: : comment? ( c-addr u -- c-addr u )
                     27:         2dup s" (" compare 0=
                     28:         IF    postpone (
                     29:         ELSE  2dup s" \" compare 0= IF postpone \ THEN
                     30:         THEN ;
                     31: 
1.1       anton      32: decimal
                     33: 
                     34: \ Begin CROSS COMPILER:
                     35: 
                     36: \ GhostNames                                            9may93jaw
                     37: \ second name source to search trough list
                     38: 
                     39: VARIABLE GhostNames
                     40: 0 GhostNames !
                     41: : GhostName ( -- addr )
                     42:         here GhostNames @ , GhostNames ! here 0 ,
                     43:         name count
                     44: \        2dup type space
                     45:         dup c, here over chars allot swap move align ;
                     46: 
                     47: hex
                     48: 
                     49: 
                     50: Vocabulary Cross
                     51: Vocabulary Target
                     52: Vocabulary Ghosts
                     53: VOCABULARY Minimal
                     54: only Forth also Target also also
                     55: definitions Forth
                     56: 
                     57: : T  previous Cross also Target ; immediate
                     58: : G  Ghosts ; immediate
                     59: : H  previous Forth also Cross ; immediate
                     60: 
                     61: forth definitions
                     62: 
                     63: : T  previous Cross also Target ; immediate
                     64: : G  Ghosts ; immediate
                     65: 
                     66: : >cross  also Cross definitions previous ;
                     67: : >target also Target definitions previous ;
                     68: : >minimal also Minimal definitions previous ;
                     69: 
                     70: H
                     71: 
                     72: >CROSS
                     73: 
                     74: \ Variables                                            06oct92py
                     75: 
                     76: -1 Constant NIL
                     77: Variable image
                     78: Variable tlast    NIL tlast !  \ Last name field
                     79: Variable tlastcfa \ Last code field
                     80: Variable tdoes    \ Resolve does> calls
                     81: Variable bit$
                     82: Variable tdp
                     83: : there  tdp @ ;
                     84: 
                     85: \ Parameter for target systems                         06oct92py
                     86: 
1.10    ! anton      87: include-file
1.1       anton      88: 
                     89: >TARGET
                     90: 
                     91: \ Byte ordering and cell size                          06oct92py
                     92: 
                     93: : cell+         cell + ;
                     94: : cells         cell<< lshift ;
                     95: : chars         ;
1.6       anton      96: : floats       float * ;
                     97:     
1.1       anton      98: >CROSS
                     99: : cell/         cell<< rshift ;
                    100: >TARGET
                    101: 20 CONSTANT bl
                    102: -1 Constant NIL
                    103: -2 Constant :docol
                    104: -3 Constant :docon
                    105: -4 Constant :dovar
1.3       pazsan    106: -5 Constant :douser
1.10    ! anton     107: -6 Constant :dodefer
        !           108: -7 Constant :dodoes
        !           109: -8 Constant :doesjump
1.1       anton     110: 
                    111: >CROSS
                    112: 
                    113: endian  0 pad ! -1 pad c! pad @ 0<
                    114: = [IF]   : bswap ; immediate 
                    115: [ELSE]   : bswap ( big / little -- little / big )  0
                    116:            cell 1- FOR  bits/byte lshift over
                    117:                         [ 1 bits/byte lshift 1- ] Literal and or
                    118:                         swap bits/byte rshift swap  NEXT  nip ;
                    119: [THEN]
                    120: 
                    121: \ Memory initialisation                                05dec92py
                    122: \ Fixed bug in else part                               11may93jaw
                    123: 
                    124: [IFDEF] Memory \ Memory is a bigFORTH feature
1.5       pazsan    125:    also Memory
1.1       anton     126:    : initmem ( var len -- )
                    127:      2dup swap handle! >r @ r> erase ;
1.5       pazsan    128:    toss
1.1       anton     129: [ELSE]
                    130:    : initmem ( var len -- )
                    131:      tuck allocate abort" CROSS: No memory for target"
                    132:      ( len var adr ) dup rot !
                    133:      ( len adr ) swap erase ;
                    134: [THEN]
                    135: 
                    136: \ MakeKernal                                           12dec92py
                    137: 
                    138: >MINIMAL
                    139: : makekernal ( targetsize -- targetsize )
                    140:   bit$  over 1- cell>bit rshift 1+ initmem
                    141:   image over initmem tdp off ;
                    142: 
                    143: >CROSS
                    144: \ Bit string manipulation                               06oct92py
                    145: \                                                       9may93jaw
                    146: CREATE Bittable 80 c, 40 c, 20 c, 10 c, 8 c, 4 c, 2 c, 1 c,
                    147: : bits ( n -- n ) chars Bittable + c@ ;
                    148: 
                    149: : >bit ( addr n -- c-addr mask ) 8 /mod rot + swap bits ;
                    150: : +bit ( addr n -- )  >bit over c@ or swap c! ;
1.4       pazsan    151: : -bit ( addr n -- )  >bit invert over c@ and swap c! ;
1.1       anton     152: : relon ( taddr -- )  bit$ @ swap cell/ +bit ;
1.4       pazsan    153: : reloff ( taddr -- )  bit$ @ swap cell/ -bit ;
1.1       anton     154: 
                    155: \ Target memory access                                 06oct92py
                    156: 
                    157: : align+  ( taddr -- rest )
                    158:     cell tuck 1- and - [ cell 1- ] Literal and ;
                    159: 
                    160: >TARGET
                    161: : aligned ( taddr -- ta-addr )  dup align+ + ;
                    162: \ assumes cell alignment granularity (as GNU C)
                    163: 
                    164: >CROSS
                    165: : >image ( taddr -- absaddr )  image @ + ;
                    166: >TARGET
                    167: : @  ( taddr -- w )     >image @ bswap ;
                    168: : !  ( w taddr -- )     >r bswap r> >image ! ;
                    169: : c@ ( taddr -- char )  >image c@ ;
                    170: : c! ( char taddr -- )  >image c! ;
1.7       anton     171: : 2@ ( taddr -- x1 x2 ) T dup cell+ @ swap @ H ;
                    172: : 2! ( x1 x2 taddr -- ) T swap over ! cell+ ! H ;
1.1       anton     173: 
                    174: \ Target compilation primitives                        06oct92py
                    175: \ included A!                                          16may93jaw
                    176: 
                    177: : here  ( -- there )    there ;
                    178: : allot ( n -- )        tdp +! ;
                    179: : ,     ( w -- )        T here H cell T allot  ! H ;
                    180: : c,    ( char -- )     T here    1 allot c! H ;
                    181: : align ( -- )          T here H align+ 0 ?DO  bl T c, H LOOP ;
                    182: 
                    183: : A!                    dup relon T ! H ;
                    184: : A,    ( w -- )        T here H relon T , H ;
                    185: 
                    186: >CROSS
                    187: 
                    188: \ threading modell                                     13dec92py
                    189: 
                    190: \ generic threading modell
                    191: : docol,  ( -- ) :docol T A, 0 , H ;
                    192: 
                    193: >TARGET
                    194: : >body   ( cfa -- pfa ) T cell+ cell+ H ;
                    195: >CROSS
                    196: 
1.3       pazsan    197: : dodoes, ( -- ) T :doesjump A, 0 , H ;
1.1       anton     198: 
                    199: \ Ghost Builder                                        06oct92py
                    200: 
                    201: \ <T T> new version with temp variable                 10may93jaw
                    202: 
                    203: VARIABLE VocTemp
                    204: 
                    205: : <T  get-current VocTemp ! also Ghosts definitions ;
                    206: : T>  previous VocTemp @ set-current ;
                    207: 
                    208: 4711 Constant <fwd>             4712 Constant <res>
                    209: 4713 Constant <imm>
                    210: 
                    211: \ iForth makes only immediate directly after create
                    212: \ make atonce trick! ?
                    213: 
                    214: Variable atonce atonce off
                    215: 
                    216: : NoExec true ABORT" CROSS: Don't execute ghost" ;
                    217: 
                    218: : GhostHeader <fwd> , 0 , ['] NoExec , ;
                    219: 
                    220: : >magic ; : >link cell+ ; : >exec cell+ cell+ ;
                    221: : >end 3 cells + ;
                    222: 
                    223: : Make-Ghost ( "name" -- ghost )
                    224:   >in @ GhostName swap >in !
                    225:   <T Create atonce @ IF immediate atonce off THEN
                    226:   here tuck swap ! ghostheader T>
                    227:   DOES>  >exec @ execute ;
                    228: 
                    229: \ ghost words                                          14oct92py
                    230: \                                          changed:    10may93py/jaw
                    231: 
                    232: : gfind   ( string -- ghost true/1 / string false )
                    233: \ searches for string in word-list ghosts
                    234: \ !! wouldn't it be simpler to just use search-wordlist ? ae
1.5       pazsan    235:   dup count [ ' ghosts >body ] ALiteral search-wordlist
                    236: \ >r get-order  0 set-order also ghosts  r> find >r >r
                    237:   >r r@ IF  >body nip  THEN  r> ;
                    238: \ set-order  r> r@  IF  >body  THEN  r> ;
1.1       anton     239: 
                    240: VARIABLE Already
                    241: 
                    242: : ghost   ( "name" -- ghost )
                    243:   Already off
                    244:   >in @  name gfind   IF  Already on nip EXIT  THEN
                    245:   drop  >in !  Make-Ghost ;
                    246: 
                    247: \ resolve                                              14oct92py
                    248: 
                    249: : resolve-loop ( ghost tcfa -- ghost tcfa )
                    250:   >r dup >link @
                    251:   BEGIN  dup  WHILE  dup T @ H r@ rot T ! H REPEAT  drop r> ;
                    252: 
                    253: \ exists                                                9may93jaw
                    254: 
                    255: : exists ( ghost tcfa -- )
                    256:   over GhostNames
                    257:   BEGIN @ dup
                    258:   WHILE 2dup cell+ @ =
                    259:   UNTIL
                    260:         nip 2 cells + count cr ." CROSS: Exists: " type 4 spaces
                    261:         swap cell+ !
                    262:   ELSE true ABORT" CROSS: Ghostnames inconsistent"
                    263:   THEN ;
                    264: 
                    265: : resolve  ( ghost tcfa -- )
                    266:   over >magic @ <fwd> <>  IF  exists EXIT THEN
                    267:   resolve-loop  over >link ! <res> swap >magic ! ;
                    268: 
                    269: \ gexecute ghost,                                      01nov92py
                    270: 
                    271: : do-forward   ( ghost -- )
                    272:   >link dup @  there rot !  T  A,  H ;
                    273: : do-resolve   ( ghost -- )
                    274:   >link @                   T  A,  H ;
                    275: 
                    276: : gexecute   ( ghost -- )   dup @
                    277:              <fwd> = IF  do-forward  ELSE  do-resolve  THEN ;
                    278: : ghost,     ghost  gexecute ;
                    279: 
                    280: \ .unresolved                                          11may93jaw
                    281: 
                    282: variable ResolveFlag
                    283: 
                    284: \ ?touched                                             11may93jaw
                    285: 
                    286: : ?touched ( ghost -- flag ) dup >magic @ <fwd> = swap >link @
                    287:                                0 <> and ;
                    288: 
                    289: : ?resolved  ( ghostname -- )
                    290:   dup cell+ @ ?touched
                    291:   IF  cell+ cell+ count cr type ResolveFlag on ELSE drop THEN ;
                    292: 
                    293: >MINIMAL
                    294: : .unresolved  ( -- )
                    295:   ResolveFlag off cr ." Unresolved: "
                    296:   Ghostnames
                    297:   BEGIN @ dup
                    298:   WHILE dup ?resolved
1.10    ! anton     299:   REPEAT drop ResolveFlag @
        !           300:   IF
        !           301:       1 (bye)
        !           302:   ELSE
        !           303:       ." Nothing!"
        !           304:   THEN
        !           305:   cr ;
1.1       anton     306: 
                    307: >CROSS
                    308: \ Header states                                        12dec92py
                    309: 
                    310: : flag! ( 8b -- )   tlast @ dup >r T c@ xor r> c! H ;
                    311: 
                    312: VARIABLE ^imm
                    313: 
                    314: >TARGET
                    315: : immediate     20 flag!
                    316:                 ^imm @ @ dup <imm> = ?EXIT
                    317:                 <res> <> ABORT" CROSS: Cannot immediate a unresolved word"
                    318:                 <imm> ^imm @ ! ;
1.8       pazsan    319: : restrict      40 flag! ;
1.1       anton     320: >CROSS
                    321: 
                    322: \ ALIAS2 ansforth conform alias                          9may93jaw
                    323: 
                    324: : ALIAS2 create here 0 , DOES> @ execute ;
                    325: \ usage:
                    326: \ ' alias2 bla !
                    327: 
                    328: \ Target Header Creation                               01nov92py
                    329: 
                    330: : string,  ( addr count -- )
                    331:   dup T c, H bounds  DO  I c@ T c, H  LOOP ; 
                    332: : name,  ( "name" -- )  name count string, T align H ;
                    333: : view,   ( -- ) ( dummy ) ;
                    334: 
                    335: VARIABLE CreateFlag CreateFlag off
                    336: 
                    337: : (Theader ( "name" -- ghost ) T align H view,
                    338:   tlast @ dup 0> IF  T 1 cells - THEN  A, H  there tlast !
                    339:   >in @ name, >in ! T here H tlastcfa !
                    340:   CreateFlag @ IF
                    341:   >in @ alias2 swap >in !         \ create alias in target
                    342:   >in @ ghost swap >in !
                    343:   swap also ghosts ' previous swap !        \ tick ghost and store in alias
                    344:   CreateFlag off
                    345:   ELSE ghost THEN
                    346:   dup >magic ^imm !     \ a pointer for immediate
                    347:   Already @ IF  dup >end tdoes !
                    348:   ELSE 0 tdoes ! THEN
                    349:   80 flag! ;
                    350: 
                    351: VARIABLE ;Resolve 1 cells allot
                    352: 
                    353: : Theader  ( "name" -- )     (THeader there resolve 0 ;Resolve ! ;
                    354: 
                    355: >TARGET
                    356: : Alias    ( cfa -- ) \ name
                    357:   (THeader over resolve T A, H 80 flag! ;
                    358: >CROSS
                    359: 
                    360: \ Conditionals and Comments                            11may93jaw
                    361: 
                    362: : ;Cond
                    363:   postpone ;
                    364:   swap ! ;  immediate
                    365: 
                    366: : Cond: ( -- ) \ name {code } ;
                    367:   atonce on
                    368:   ghost
                    369:   >exec
                    370:   :NONAME ;
                    371: 
                    372: : restrict? ( -- )
                    373: \ aborts on interprete state - ae
                    374:   state @ 0= ABORT" CROSS: Restricted" ;
                    375: 
                    376: : Comment ( -- )
                    377:   >in @ atonce on ghost swap >in ! ' swap >exec ! ;
                    378: 
                    379: Comment (       Comment \
                    380: 
                    381: \ Predefined ghosts                                    12dec92py
                    382: 
                    383: ghost 0=                                        drop
                    384: ghost branch    ghost ?branch                   2drop
                    385: ghost (do)      ghost (?do)                     2drop
                    386: ghost (for)                                     drop
                    387: ghost (loop)    ghost (+loop)                   2drop
                    388: ghost (next)                                    drop
1.2       pazsan    389: ghost unloop    ghost ;S                        2drop
1.1       anton     390: ghost lit       ghost (compile) ghost !         2drop drop
                    391: ghost (;code)   ghost noop                      2drop
                    392: ghost (.")      ghost (S")      ghost (ABORT")  2drop drop
1.9       pazsan    393: ghost '
1.1       anton     394: 
                    395: \ compile                                              10may93jaw
                    396: 
                    397: : compile  ( -- ) \ name
                    398:   restrict?
                    399:   name gfind dup 0= ABORT" CROSS: Can't compile "
                    400:   0> ( immediate? )
                    401:   IF    >exec @ compile,
                    402:   ELSE  postpone literal postpone gexecute  THEN ;
                    403:                                         immediate
                    404: 
                    405: >TARGET
                    406: : '  ( -- cfa ) name gfind 0= ABORT" CROSS: undefined "
                    407:   dup >magic @ <fwd> = ABORT" CROSS: forward " >link @ ;
                    408: 
                    409: Cond: [']  compile lit ghost gexecute ;Cond
                    410: 
                    411: >CROSS
                    412: \ tLiteral                                             12dec92py
                    413: 
                    414: : lit, ( n -- )   compile lit T  ,  H ;
                    415: : alit, ( n -- )  compile lit T A,  H ;
                    416: 
                    417: >TARGET
                    418: Cond:  Literal ( n -- )   restrict? lit, ;Cond
                    419: Cond: ALiteral ( n -- )   restrict? alit, ;Cond
                    420: 
                    421: : Char ( "<char>" -- )  bl word char+ c@ ;
                    422: Cond: [Char]   ( "<char>" -- )  restrict? Char  lit, ;Cond
                    423: 
                    424: >CROSS
                    425: \ Target compiling loop                                12dec92py
                    426: \ ">tib trick thrown out                               10may93jaw
                    427: \ number? defined at the top                           11may93jaw
                    428: 
                    429: \ compiled word might leave items on stack!
                    430: : tcom ( in name -- )
                    431:   gfind  ?dup  IF    0> IF    nip >exec @ execute
                    432:                         ELSE  nip gexecute  THEN EXIT THEN
                    433:   number? dup  IF    0> IF swap lit,  THEN  lit,  drop
                    434:                ELSE  2drop >in !
                    435:                ghost gexecute THEN  ;
                    436: 
                    437: >TARGET
                    438: \ : ; DOES>                                            13dec92py
                    439: \ ]                                                     9may93py/jaw
                    440: 
                    441: : ] state on
                    442:     BEGIN
                    443:         BEGIN >in @ name
                    444:               dup c@ 0= WHILE 2drop refill 0=
                    445:               ABORT" CROSS: End of file while target compiling"
                    446:         REPEAT
                    447:         tcom
                    448:         state @
                    449:         0=
                    450:     UNTIL ;
                    451: 
                    452: \ by the way: defining a second interpreter (a compiler-)loop
                    453: \             is not allowed if a system should be ans conform
                    454: 
                    455: : : ( -- colon-sys ) \ Name
                    456:   (THeader ;Resolve ! there ;Resolve cell+ !
                    457:   docol, depth T ] H ;
                    458: 
1.2       pazsan    459: Cond: EXIT ( -- )  restrict?  compile ;S  ;Cond
1.6       anton     460: 
                    461: Cond: ?EXIT ( -- ) 1 abort" CROSS: using ?exit" ;Cond
1.2       pazsan    462: 
1.1       anton     463: Cond: ; ( -- ) restrict?
                    464:                depth ?dup IF   1- <> ABORT" CROSS: Stack changed"
                    465:                           ELSE true ABORT" CROSS: Stack empty" THEN
1.2       pazsan    466:                compile ;S state off
1.1       anton     467:                ;Resolve @
                    468:                IF ;Resolve @ ;Resolve cell+ @ resolve THEN
                    469:                ;Cond
                    470: Cond: [  restrict? state off ;Cond
                    471: 
                    472: >CROSS
                    473: : !does  :dodoes tlastcfa @ tuck T ! cell+ ! H ;
                    474: 
                    475: >TARGET
                    476: Cond: DOES> restrict?
                    477:         compile (;code) dodoes, tdoes @ ?dup IF  @ T here H resolve THEN
                    478:         ;Cond
                    479: : DOES> dodoes, T here H !does depth T ] H ;
                    480: 
                    481: >CROSS
                    482: \ Creation                                             01nov92py
                    483: 
                    484: \ Builder                                               11may93jaw
                    485: 
                    486: : Builder    ( Create do: "name" -- )
                    487:   >in @ alias2 swap dup >in ! >r >r
                    488:   Make-Ghost rot swap >exec ! ,
                    489:   r> r> >in !
                    490:   also ghosts ' previous swap !
                    491:   DOES> dup >exec @ execute ;
                    492: 
                    493: : gdoes,  ( ghost -- )  >end @ dup >magic @ <fwd> <>
                    494:   IF dup >link @ dup 0< IF T A, 0 , H drop EXIT THEN drop THEN
1.4       pazsan    495:   :dodoes T A, H gexecute T here H cell - reloff ;
1.1       anton     496: 
                    497: : TCreate ( ghost -- )
                    498:   CreateFlag on
                    499:   Theader dup gdoes,
                    500:   >end @ >exec @ execute ;
                    501: 
                    502: : Build:  ( -- [xt] [colon-sys] )
                    503:   :noname  postpone TCreate ;
                    504: 
                    505: : gdoes>  ( ghost -- addr flag )
                    506:   state @ IF  gexecute true EXIT  THEN
                    507:   cell+ @ T >body H false ;
                    508: 
                    509: \ DO: ;DO                                               11may93jaw
                    510: \ changed to ?EXIT                                      10may93jaw
                    511: 
                    512: : (does>)        postpone does> ; immediate \ second level does>
                    513: 
                    514: : DO:     ( -- addr [xt] [colon-sys] )
                    515:   here ghostheader
                    516:   :noname
                    517:   postpone (does>) postpone gdoes> postpone ?EXIT ;
                    518: 
                    519: : ;DO ( addr [xt] [colon-sys] -- )
                    520:   postpone ;    ( S addr xt )
                    521:   over >exec ! ; immediate
                    522: 
                    523: : by      ( -- addr ) \ Name
                    524:   ghost >end @ ;
                    525: 
                    526: >TARGET
                    527: \ Variables and Constants                              05dec92py
                    528: 
                    529: Build:  ;
                    530: DO: ( ghost -- addr ) ;DO
                    531: Builder Create
                    532: by Create :dovar resolve
                    533: 
                    534: Build: T 0 , H ;
                    535: by Create
                    536: Builder Variable
                    537: 
                    538: Build: T 0 A, H ;
                    539: by Create
                    540: Builder AVariable
                    541: 
1.3       pazsan    542: \ User variables                                       04may94py
                    543: 
                    544: >CROSS
                    545: Variable tup  0 tup !
                    546: Variable tudp 0 tudp !
                    547: : u,  ( n -- udp )
                    548:   tup @ tudp @ + T  ! H
                    549:   tudp @ dup cell+ tudp ! ;
                    550: : au, ( n -- udp )
                    551:   tup @ tudp @ + T A! H
                    552:   tudp @ dup cell+ tudp ! ;
                    553: >TARGET
                    554: 
                    555: Build: T 0 u, , H ;
                    556: DO: ( ghost -- up-addr )  T @ H tup @ + ;DO
1.1       anton     557: Builder User
1.3       pazsan    558: by User :douser resolve
1.1       anton     559: 
1.3       pazsan    560: Build: T 0 u, , 0 u, drop H ;
                    561: by User
1.1       anton     562: Builder 2User
                    563: 
1.3       pazsan    564: Build: T 0 au, , H ;
                    565: by User
1.1       anton     566: Builder AUser
                    567: 
                    568: Build:  ( n -- ) T , H ;
                    569: DO: ( ghost -- n ) T @ H ;DO
                    570: Builder Constant
                    571: by Constant :docon resolve
                    572: 
                    573: Build:  ( n -- ) T A, H ;
                    574: by Constant
                    575: Builder AConstant
                    576: 
                    577: Build: T 0 , H ;
                    578: by Constant
                    579: Builder Value
                    580: 
                    581: Build:  ( -- ) compile noop ;
                    582: DO: ( ghost -- ) ABORT" CROSS: Don't execute" ;DO
                    583: Builder Defer
1.10    ! anton     584: by Defer :dodefer resolve
1.1       anton     585: 
                    586: \ structural conditionals                              17dec92py
                    587: 
                    588: >CROSS
                    589: : ?struc      ( flag -- )       ABORT" CROSS: unstructured " ;
                    590: : sys?        ( sys -- sys )    dup 0= ?struc ;
                    591: : >mark       ( -- sys )        T here  0 , H ;
                    592: : >resolve    ( sys -- )        T here over - swap ! H ;
                    593: : <resolve    ( sys -- )        T here - , H ;
                    594: >TARGET
                    595: 
                    596: \ Structural Conditionals                              12dec92py
                    597: 
                    598: Cond: BUT       restrict? sys? swap ;Cond
                    599: Cond: YET       restrict? sys? dup ;Cond
                    600: 
                    601: >CROSS
                    602: Variable tleavings
                    603: >TARGET
                    604: 
                    605: Cond: DONE   ( addr -- )  restrict? tleavings @
                    606:       BEGIN  2dup u> 0=  WHILE  dup T @ H swap >resolve REPEAT
                    607:       tleavings ! drop ;Cond
                    608: 
                    609: >CROSS
                    610: : (leave  T here H tleavings @ T , H  tleavings ! ;
                    611: >TARGET
                    612: 
                    613: Cond: LEAVE     restrict? compile branch (leave ;Cond
                    614: Cond: ?LEAVE    restrict? compile 0=  compile ?branch (leave  ;Cond
                    615: 
                    616: \ Structural Conditionals                              12dec92py
                    617: 
                    618: Cond: AHEAD     restrict? compile branch >mark ;Cond
                    619: Cond: IF        restrict? compile ?branch >mark ;Cond
                    620: Cond: THEN      restrict? sys? dup T @ H ?struc >resolve ;Cond
                    621: Cond: ELSE      restrict? sys? compile AHEAD swap compile THEN ;Cond
                    622: 
                    623: Cond: BEGIN     restrict? T here H ;Cond
                    624: Cond: WHILE     restrict? sys? compile IF swap ;Cond
                    625: Cond: AGAIN     restrict? sys? compile branch <resolve ;Cond
                    626: Cond: UNTIL     restrict? sys? compile ?branch <resolve ;Cond
                    627: Cond: REPEAT    restrict? over 0= ?struc compile AGAIN compile THEN ;Cond
                    628: 
                    629: \ Structural Conditionals                              12dec92py
                    630: 
                    631: Cond: DO        restrict? compile (do)   T here H ;Cond
                    632: Cond: ?DO       restrict? compile (?do)  (leave T here H ;Cond
                    633: Cond: FOR       restrict? compile (for)  T here H ;Cond
                    634: 
                    635: >CROSS
                    636: : loop]   dup <resolve cell - compile DONE compile unloop ;
                    637: >TARGET
                    638: 
                    639: Cond: LOOP      restrict? sys? compile (loop)  loop] ;Cond
                    640: Cond: +LOOP     restrict? sys? compile (+loop) loop] ;Cond
                    641: Cond: NEXT      restrict? sys? compile (next)  loop] ;Cond
                    642: 
                    643: \ String words                                         23feb93py
                    644: 
                    645: : ,"            [char] " parse string, T align H ;
                    646: 
                    647: Cond: ."        restrict? compile (.")     T ," H ;Cond
                    648: Cond: S"        restrict? compile (S")     T ," H ;Cond
                    649: Cond: ABORT"    restrict? compile (ABORT") T ," H ;Cond
                    650: 
                    651: Cond: IS        T ' >body H compile ALiteral compile ! ;Cond
                    652: : IS            T ' >body ! H ;
1.9       pazsan    653: Cond: TO        T ' >body H compile ALiteral compile ! ;Cond
                    654: : TO            T ' >body ! H ;
1.1       anton     655: 
                    656: \ LINKED ERR" ENV" 2ENV"                                18may93jaw
                    657: 
                    658: \ linked list primitive
                    659: : linked        T here over @ A, swap ! H ;
                    660: 
                    661: : err"   s" ErrLink linked" evaluate T , H
                    662:          [char] " parse string, T align H ;
                    663: 
                    664: : env"  [char] " parse s" EnvLink linked" evaluate
                    665:         string, T align , H ;
                    666: 
                    667: : 2env" [char] " parse s" EnvLink linked" evaluate
                    668:         here >r string, T align , , H
                    669:         r> dup T c@ H 80 and swap T c! H ;
                    670: 
                    671: \ compile must be last                                 22feb93py
                    672: 
                    673: Cond: compile ( -- ) restrict? \ name
                    674:       name gfind dup 0= ABORT" CROSS: Can't compile"
                    675:       0> IF    gexecute
                    676:          ELSE  dup >magic @ <imm> =
                    677:                IF   gexecute
                    678:                ELSE compile (compile) gexecute THEN THEN ;Cond
                    679: 
                    680: Cond: postpone ( -- ) restrict? \ name
                    681:       name gfind dup 0= ABORT" CROSS: Can't compile"
                    682:       0> IF    gexecute
                    683:          ELSE  dup >magic @ <imm> =
                    684:                IF   gexecute
                    685:                ELSE compile (compile) gexecute THEN THEN ;Cond
                    686: 
                    687: >MINIMAL
                    688: also minimal
                    689: \ Usefull words                                        13feb93py
                    690: 
                    691: : KB  400 * ;
                    692: 
                    693: \ define new [IFDEF] and [IFUNDEF]                      20may93jaw
                    694: 
                    695: : there? name gfind IF >magic @ <fwd> <> ELSE drop false THEN ;
                    696: 
                    697: : [IFDEF] there? postpone [IF] ;
                    698: : [IFUNDEF] there? 0= postpone [IF] ;
                    699: 
                    700: \ C: \- \+ Conditional Compiling                         09jun93jaw
                    701: 
                    702: : C: >in @ there? 0=
                    703:      IF    >in ! T : H
                    704:      ELSE drop
                    705:         BEGIN bl word dup c@
                    706:               IF   count comment? s" ;" compare 0= ?EXIT
                    707:               ELSE refill 0= ABORT" CROSS: Out of Input while C:"
                    708:               THEN
                    709:         AGAIN
                    710:      THEN ;
                    711: 
                    712: also minimal
                    713: 
                    714: : \- there? IF postpone \ THEN ;
                    715: : \+ there? 0= IF postpone \ THEN ;
                    716: 
                    717: : [IF]   postpone [IF] ;
                    718: : [THEN] postpone [THEN] ;
                    719: : [ELSE] postpone [ELSE] ;
                    720: 
                    721: Cond: [IF]      [IF] ;Cond
                    722: Cond: [IFDEF]   [IFDEF] ;Cond
                    723: Cond: [IFUNDEF] [IFUNDEF] ;Cond
                    724: Cond: [THEN]    [THEN] ;Cond
                    725: Cond: [ELSE]    [ELSE] ;Cond
                    726: 
                    727: \ save-cross                                           17mar93py
                    728: 
                    729: \ i'm not interested in bigforth features this time    10may93jaw
                    730: \ [IFDEF] file
                    731: \ also file
                    732: \ [THEN]
                    733: \ included throw after create-file                     11may93jaw
                    734: 
                    735: endian Constant endian
                    736: 
                    737: : save-cross ( "name" -- )
                    738:   bl parse ." Saving to " 2dup type
                    739:   w/o bin create-file throw >r
                    740:   image @ there r@ write-file throw
                    741:   bit$  @ there 1- cell>bit rshift 1+ r@ write-file throw
                    742:   r> close-file throw ;
                    743: 
                    744: \ words that should be in minimal
                    745: 
                    746: : + + ;         : 1- 1- ;
                    747: : - - ;         : 2* 2* ;
                    748: : dup dup ;     : over over ;
                    749: : swap swap ;   : rot rot ;
                    750: 
                    751: \ include bug5.fs
                    752: \ only forth also minimal definitions
                    753: 
                    754: : \ postpone \ ;
                    755: : ( postpone ( ;
                    756: : include bl word count included ;
                    757: : .( [char] ) parse type ;
                    758: : cr cr ;
                    759: 
                    760: : times 0 ?DO dup T c, H LOOP drop ; \ used for space table creation
                    761: only forth also minimal definitions
                    762: 
                    763: \ cross-compiler words
                    764: 
                    765: : decimal       decimal ;
                    766: : hex           hex ;
                    767: 
1.3       pazsan    768: : tudp          T tudp H ;
                    769: : tup           T tup H ;  minimal
1.1       anton     770: 
                    771: \ for debugging...
                    772: : order         order ;
                    773: : words         words ;
                    774: : .s            .s ;
                    775: 
                    776: : bye           bye ;
                    777: 
                    778: \ turnkey direction
                    779: : H forth ; immediate
                    780: : T minimal ; immediate
                    781: : G ghosts ; immediate
                    782: 
                    783: : turnkey  0 set-order also Target definitions
                    784:            also Minimal also ;
                    785: 
                    786: \ these ones are pefered:
                    787: 
                    788: : lock   turnkey ;
                    789: : unlock forth also cross ;
                    790: 
                    791: unlock definitions also minimal
                    792: : lock   lock ;
                    793: lock

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