Annotation of gforth/cross.fs, revision 1.9

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

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