home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 224b.lha / metavocs.txt < prev    next >
Text File  |  1989-04-08  |  12KB  |  288 lines

  1.  
  2. **this file explains how I implemented the multiple vocabularies with the
  3. **copy of L&P Meta.compiler. It was posted on GEnie, I have included it
  4. **here for information.
  5.  
  6.  
  7.  
  8.   How to change Laxen&Perry F83 Metacompiler to use multiple vocabularies
  9.                           when compiling the kernel.
  10.  
  11.  
  12.  First, the version I used this on is the Amiga F83 a derivative of L&P
  13.  V2.1.0 for CP/M.  The Amiga F83 was posted to GEnie by me some time ago.
  14.  It is a 32 bit model.  The differences are obvious, if you have your
  15.  version at hand and compare the listings.  Most are simple substitutions
  16.  of 4* for 2* and 4+ for 2+, since cell size in my system is 32 bits, the
  17.  L&P model uses 16 bits.
  18.  
  19.  This is not a new Metacompiler. I set out to make changes to the system,
  20.  instead.  This is supposed to be a feature of Forth.  Maintainability.
  21.  And as far as I'm concerned it is.  It took very few changes to make
  22.  multiple vocabularies possible.
  23.  What that implies is that any word defined to be used interpretively from
  24.  within Meta is visible at all times.  This includes defined VOCABULARIES.
  25.  Others which will be visible at all times (much as before) are VARIABLES
  26.  and CONSTANTS.  In my opinion that is a small sacrifice.  And the major
  27.  advantage is that the original Kernel.blk and Meta.blk need no changes
  28.  except for the ones given below.  The Metacompiler feels and looks
  29.  identical to the original from L&P, with the added feature of hiding CODE
  30.  and COLON definitions.
  31.  
  32.  
  33.  This is a 2 step procedure:
  34.     1).  Change the kernel, Metacompile
  35.     2).  Change Meta and some minor changes in the kernel, Metacompile.
  36.  
  37.  Some terminology:
  38.  target-image  The data that will be the new kernel, once saved.
  39.  dictionary    A set of vocabularies, with a CONTEXT like array, a variable
  40.                behaving like CURRENT, and a vocabulary link like VOC-LINK.
  41.  symbol-dict.  A set of vocabularies into which all symbols are entered.
  42.                It is here that the Metacompiler finds the address to be
  43.                compiled. In the original version this was the TARGET
  44.                vocabulary. The symbol-dict will grow as each new target
  45.                vocabulary is defined.
  46.  
  47.  
  48.  STEP 1  Altering the way the system FINDs words
  49.  -----------------------------------------------
  50.  
  51.  When Metacompiling, the host system is used to find the word in CONTEXT.
  52.  Meta controls the search order by using TARGET, the vocabulary where all
  53.  the symbols are defined.  This is only one vocabulary, and what we want is
  54.  a multiple set of vocabularies, controlled by us in the kernel.blk source
  55.  file.
  56.  
  57.  If the whole idea of FINDing and CONTEXT was designed to be used with
  58.  multiple dictionaries, that would have been a trivial change.
  59.  
  60.  We can still simulate it by altering the way FIND works. If FIND
  61.  encounters a vocabulary address that we mark as a special vocabulary, it
  62.  can go and find the word in question in an extra CONTEXT.
  63.  
  64.   CONTEXT:  ....  .....  TARGET .....  0000
  65.            ------------>  +      + ------>
  66.                           |      |
  67.                    +------+      +-------+
  68.                    |                     |
  69.                    |                     |
  70.                    +->     ------------->+
  71.                  <CONTEXT>: .... .... .... 0000
  72.  
  73.  In the above diagram TARGET would not receive any words. They are stuck
  74.  into another current; <CURRENT>.
  75.  This allows Meta to be used, almost unchanged. TARGET is still a
  76.  vocabulary in Meta, empty, and used as a flag.
  77.  
  78.  The actual changes:  (in Kernel.blk )
  79.  
  80.  I inserted a blank screen between the screens that define (FIND) and FIND,
  81.  by moving screens around.  In the blank screen I entered:
  82.  
  83.   0 \  Meta multiple voc support.                         02Jun88pJa
  84.   1 VARIABLE <CURRENT>
  85.   2 VARIABLE <CONTEXT>   HERE THERE #VOCS 4* DUP ALLOT ERASE
  86.   3 VARIABLE <TARGET>    -1 <TARGET> !-T
  87.   4 : TARGET?   (S addr -- fl )   <TARGET> @ =  ;
  88.   5 : <DEFINITIONS>   <CONTEXT> @  <CURRENT> !  ;
  89.   6 : <FIND>   (S addr -- addr false | cfa flag )
  90.   7    DUP C@  IF  PRIOR OFF  FALSE  #VOCS 0
  91.   8       DO   DROP <CONTEXT> I 4* + @  DUP
  92.   9          IF  DUP PRIOR @ OVER PRIOR ! =
  93.  10             IF  DROP FALSE
  94.  11             ELSE  OVER SWAP HASH @ (FIND)  DUP ?LEAVE
  95.  12          THEN  THEN  LOOP
  96.  13       DUP  IF  R> DROP  LEAVE  ELSE  DUP  THEN
  97.  14    ELSE  DROP END? ON  ['] NOOP 1  THEN  ;
  98.  15
  99.  
  100.  <CURRENT> and <CONTEXT> are alternate copies of CONTEXT and CURRENT.
  101.  <TARGET> is a flag, initialized to -1.  Here Meta can put the address of
  102.  the vocabulary, which causes FIND to execute <FIND>.  The word TARGET?
  103.  returns a flag for that purpose.
  104.  <DEFINITIONS> is the same as DEFINITIONS, for the alternate
  105.  CONTEXT/CURRENT.  <FIND> is a copy of FIND, with a small difference. It
  106.  uses <CONTEXT> and it exists a little different. First of all the C@ will
  107.  always be true, or execution wouldn't even get to here, since <FIND>
  108.  should only be called from FIND. The C@ test is done in FIND, and it will
  109.  return NOOP and 1 in that case. I'm a little lazy, could have removed
  110.  that. (You can). If <FIND> finds a match in <CONTEXT> it will exit the
  111.  DO..LOOP via ?LEAVE.  It will end up on line 13, test to see if it was a
  112.  match and exit the caller: FIND.  The R> DROP pops the return address and
  113.  the LEAVE exits the DO..LOOP that was being executed in FIND. If no match
  114.  it duplicates the flag, to be used in FIND, to indicate no vocabulary in
  115.  that position of CONTEXT.
  116.  
  117.  ( I actually use all lower case, but that's my choice. Watch out for the
  118.  4* that should in 16 bit system be 2* )
  119.  
  120.  In the next screen, the original FIND was changed:
  121.  
  122.       .....................
  123.         DO DROP CONTEXT I 4* + @  DUP
  124.            TARGET?  IF  DROP <FIND>  ELSE  DUP  THEN    (  <--inserted )
  125.            IF  DUP PRIOR @ OVER PRIOR !  =
  126.        ..................
  127.  
  128.  The added line will cause <FIND> to be used if TARGET? returns true, that
  129.  is, the vocabulary is e.g. TARGET.
  130.  
  131.  In "CREATE, the sequence:  CURRENT @
  132.  was replaced by:
  133.      CURRENT @ TARGET?  IF  <CURRENT> @  ELSE  CURRENT @  THEN
  134.  
  135.  Those are all the changes required for the kernel, next metacompile.
  136.  The new kernel must be used to Metacompile again, after the next
  137.  steps have been made.
  138.  
  139.  
  140.  STEP 2. Making the changes in Meta.blk and the last changes in Kernel.blk
  141.  -------------------------------------------------------------------------
  142.  
  143.  The only changes required in Meta.blk are in the way Meta defines a
  144.  vocabulary.  We want this vocabulary to:
  145.  1- Create a header and vocabulary links in the target-image.
  146.  2- Save the address of (1) in a symbol, which is kept in the <CURRENT>
  147.     vocabulary, part of the symbol dictionary.
  148.  3- Create a word in vocabulary META to save pointers into the symbol
  149.     dictionary; the vocabulary corresponding to the target-image one.
  150.     When this word executes, it must set <CONTEXT> and also CONTEXT-T,
  151.     which points to a set of links in the target-image.
  152.     The vocabulary defined in meta is linked into SYMBOL-LINK, not VOC-LINK
  153.     of the host system.
  154.  To make life easier a set of words to manipulate and take a look at the
  155.  symbol table are added. (.SYMBOLS doesn't work)
  156.  
  157.  The changes in Meta.blk:
  158.  
  159.  After the vocabulary TARGET is defined the variable <TARGET> must be set:
  160.  
  161.  VOCABULARY TARGET    ' TARGET >BODY  <TARGET> !
  162.  
  163.  After that I added a variable:
  164.  
  165.  VARIABLE SYMBOL-LINK
  166.  
  167.  It links all the symbol vocabularies together, see below.
  168.  
  169.  The word VOCABULARY now is:
  170.  
  171.  : VOCABULARY  (S -- )
  172.     RECREATE  [FORWARD] <VOCABULARY>
  173.     HERE-T  #THREADS 0  DO  0 ,-T  LOOP
  174.     HERE-T  VOC-LINK-T @ ,-T  VOC-LINK-T !
  175.     [FORTH] CREATE [META]
  176.        #TRHEADS 0  DO   0 , LOOP
  177.        HERE SYMBOL-LINK @ ,  SYMBOL-LINK !
  178.        ,
  179.        DOES>  DUP <CONTEXT> !  #THREADS 1+ 4* + @  CONTEXT-T !  ;
  180.  
  181.  The first three lines are the same as before. ( In the Amiga version the
  182.  second ,-T is ,-tr )  On the fourth line, ( forth ) CREATE makes a header
  183.  in the host system, META vocabulary. It then allocates link pointers which
  184.  will point into the symbol dict. Links itself into SYMBOL-LINK. Stores the
  185.  target-image pointer to the target-image version of the defined
  186.  vocabulary.  When the defined vocabulary is used, it will put the saved
  187.  target-image pointer into CONTEXT-T and set <CONTEXT> to the link
  188.  pointers.
  189.  
  190.  Next I WIPED the screen defining .SYMBOLS, it will not work with the above
  191.  vocabulary definition.  I used that screen to make some help words:
  192.                                                                    |
  193.   0 \ META <CONTEXT> MANIPULATORS.                        03Jun88pJa
  194.   1 : <EMPTY>   (S -- ) \ clears out <CONTEXT>.
  195.   2    <CONTEXT #VOCS 4* ERASE  ;
  196.   3 : <ALSO>    (S -- ) \ duplicate <CONTEXT>
  197.   4    <CONTEXT> DUP 4+ #VOCS 1- 4* CMOVE>  ;
  198.   5 : <ORDER>   (S -- )  \ show what <CONTEXT> is.
  199.   6    CR ." <Context>: " <CONTEXT> #VOCS 0  DO  DUP @ ?DUP IF
  200.   7       BODY> >NAME .ID  THEN  4+  LOOP  DROP
  201.   8    CR ." <Current>: " <CURRENT> @ BODY> >NAME .ID  ;
  202.   9 : <VOCS>    (S -- )  \ display symbol vocabularies.
  203.  10    SYMBOL-LINK @  BEGIN  DUP #THREADS 4* - BODY> >NAME .ID
  204.  11       @ DUP 0=  UNTIL  DROP ;
  205.  12 : <WORDS>   (S -- )  \ show words (symbols) in <CONTEXT>.
  206.  13    CONTEXT @  <CONTEXT> @ CONTEXT !  WORDS  CONTEXT !  ;
  207.  14
  208.  15
  209.  
  210.  Again the 4+ and 4* should be 2+ and 2* for 16 bit versions. The words
  211.  work the same as the standard ONLY/ALSO extension. ONLY is replaced by
  212.  <EMPTY>. Since there is no ROOT vocabulary, it simply empties <CONTEXT>.
  213.  Make sure you invoke a vocabulary after <EMPTY>.
  214.  
  215.  The last change is in the word DEFINITIONS on the last screen of Meta.blk
  216.  It should be:
  217.  H: DEFINITIONS  DEFINITIONS <DEFINITIONS> CONTEXT-T @ CURRENT-T ! ;
  218.  
  219.  So much for the changes in Meta.blk.
  220.  
  221.  
  222.  Now for the changes in Kernel.blk..
  223.  
  224.  The problem is with the first word to be defined. FORTH is the first word
  225.  and is a vocabulary. It must be properly linked into the system. The
  226.  original version of L&P took care of linking the first word into
  227.  CURRENT-T.  It also initialized that first word to point to it's own link.
  228.  
  229.  I will repeat here the way I did it in the Amiga version, and then I will
  230.  guess at what it would be in a 16 bit version.
  231.  
  232.  Amiga:
  233.  
  234.  here-t dup 100 + current-t !   dup 116 + there <current> !
  235.  vocabulary forth  forth definitions
  236.   dup 108 +  -relocate
  237.    dup 124 + there @   dup last @ name> >body 8+ !  off
  238.  0 over 2+ !-t
  239.  dup  2+  swap 24 +  dup relocate  !-t
  240.  in-meta
  241.  
  242.  
  243.  And for 16 bits (guessing)
  244.  
  245.  HERE-T DUP 100 + CURRENT-T !   DUP 108 + THERE <CURRENT> !
  246.  VOCABULARY FORTH  FORTH DEFINITIONS
  247.    DUP 112 + THERE @   DUP LAST @ NAME> >BODY 4 + !  OFF
  248.  0 OVER 2+ !-T
  249.  DUP 2+ SWAP 16 + !-T
  250.  IN-META
  251.  
  252.  
  253.  The last change to Kernel.blk is not a problem, it is the result of the
  254.  feature of having multiple vocabularies. When another vocabulary is
  255.  created and words are to be defined in it, <CONTEXT> must now be
  256.  manipulated. For instance:
  257.  
  258.  VOCABULARY DOS DOS DEFINITIONS
  259.  
  260.  would hide all the FORTH defined symbols, you won't know, but at the end
  261.  of Metacompiling you will see a bunch of unresolved forward references.
  262.  You must take care in properly specifying the symbol search order.
  263.  
  264.  <EMPTY> FORTH DEFINITIONS  <ALSO>
  265.  VOCABULARY DOS  DOS DEFINITIONS
  266.  
  267.  A final note, when the Metacompiler defines a new colon word, the
  268.  <CURRENT> and <CONTEXT>  are not altered.  This is in contrast to the host
  269.  system, which sets CONTEXT to CURRENT at the start of a colon definition.
  270.  This way you can do the following:
  271.  
  272.  <EMPTY> FORTH DEFINITIONS <ALSO> DOS
  273.  ( <ORDER> = )  <Context>: DOS FORTH
  274.                 <Current>: FORTH
  275.  
  276.  And <CURRENT> will stay like that until the next DEFINITIONS.
  277.  
  278.  
  279.  At this point you can Metacompile, using the kernel you compiled in
  280.  step 1.
  281.  
  282.  
  283.  I hope I didn't forget anything.
  284.  
  285.  Peter Appelman.
  286.  
  287.  
  288.