File:  [gforth] / gforth / cross.fs
Revision 1.6: download - view: text, annotated - select for diffs
Fri Jun 17 12:34:58 1994 UTC (29 years, 10 months ago) by anton
Branches: MAIN
CVS tags: HEAD
Integrated locals (in particular automatic scoping) into the system.

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

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