home *** CD-ROM | disk | FTP | other *** search
- Multiple Code Field Word Set
-
- These words can be used to create "inteligent" data types. The
- standard Forth word VARIABLE is very simple in its operation, its sole
- action is to return the address of its data, what is done with the
- address afterwords, is up to the following code. This action can be
- adjusted by adding a DOES> or ;CODE clause to the VARIABLE (or word
- with CREATE), but this still offers only a single action to be
- performed on the specified data. This word set allows multiple actions
- to be specified. The particular action to be preformed is specified by
- a prefix, used in front of the decalared word.
-
- For example, the word VALUE is proposed for ANSI Forth. It is
- essentially a self-fetching VARIABLE. A number is stored into a VALUE
- by using TO (and perhaps +TO to add into it). The following is an
- implementation of VALUE using the MCFA words:
-
- 3 CODEFIELDS
- : VALUE BUILD ( like CREATE ) 0 , ( room for data ) ;
- DO> @ ; ( fetch action - no prefix used )
- PREFIX TO
- DO> ! ; ( store action - using prefix TO )
- PREFIX +TO
- DO> +! ; ( plus-store action - with +TO )
-
- VALUE #trees
- : CUT-DOWN NEGATE +TO #trees ; ( #ToCut -- )
- 56 TO #trees
- 26 CUT-DOWN #trees . ( would print:)
- 30 Ok
-
- This word set is based on the article "Multiple Code Field
- Data Types and Prefix Operators" by Klaus Schleisiek (Journal of Forth
- Application and Research Volume 1 No. 2 December 1983). This
- implementation has a cleaner syntax. The RPN assembler must be loaded
- in order to compile this word set.
-
- Declaring words
-
- First the number of actions (ie. number of code fields or
- number of prefixes plus one) is specified with the word CODEFIELDS.
- The compile time action is then specified in a colon definition
- containing BUILD (works like CREATE). Following are the
- various actions, each begun with DO> or CODE>. A prefix for the first
- action is optional. This is simply the action taken when no prefix is
- used (ie. first or usual code field). A prefix must be declared for
- each subsequent action. This is simply specified with PREFIX
- <wordname>. The order is important, the prefix must appear before the
- DO> or CODE> clause to which it refers.
-
- When execution begins after a DO> the address on the stack is
- whatever data field was defined in the BUILD clause. This is identical
- to the action of DOES> . Words begun with CODE> are a little more
- complex. The BX register always contains the code field address on
- entry to a word. This is no different here, except that it points to
- one of the several declared code fields. In order to access the data
- pointer for example, a constant must be added to it. This value is
- the number of bytes to pass by the rest of the code fields. This can
- be determined from the position of the particular code fields in the
- list. If for example, there are four code fields in total, and CODE>
- is used for the second, 6 bytes must be added.
-
- Size Penalties
-
- Any reference to or usage of a word created with this word set,
- takes two bytes. The PREFIXes are IMMEDIATE words and force the
- compilation of a two byte threaded address just like any other word.
- There is a size penalty in that every created word has the specified
- number of code field addresses. If there are many uses of the declared
- words, than this is probably not of concern. In the example above,
- every declared VALUE takes 8 bytes in the thread segment (6 for cfas
- and two for a data pointer) and two bytes in the data segment.
-
- Summary of syntax
- n CODEFIELDS
- : <creatingwordname> BUILD .... ;
-
- PREFIX <prefixname> ( optional first prefix)
- DO> ... ( actions for first or no prefix) ;
-
- PREFIX <prefixname>
- DO> ... ;
-
- PREFIX <prefixname>
- CODE> ... ( rpn assembler code) ... C;
-
- Ancillary Words
-
- USE-PREFIX
- Allows a prefix defined to be used with a previous word set, to
- be re-used. There is one restriction: the number of CFAs in both
- word sets must be the same. For example:
-
- 3 CODEFIELDS
- : DVALUE BUILD ( like CREATE ) 0 , ( room for data ) ;
- DO> D@ ; ( fetch action - no prefix used )
- USE-PREFIX TO
- DO> D! ; ( store action - using prefix TO )
- USE-PREFIX +TO
- DO> DUP >R D@ D+ R> D! ; ( plus-store action - with +TO )
-
- Examples
-
- INTEGER and DOUBLE
- These are formed using multiple cfa words. Both are sometimes
- called: self-fetching variables. DOUBLE is simply the 32 bit counterpart
- of INTEGER (16 bits). To fetch the value from an INTEGER, simply
- name it (like a CONSTANT). To store a value use: ->
- Example:
- INTEGER #of-flower-pots
- 74 -> #of-flower-pots
- : PRINT-IT ." Number of pots:" #of-flower-pots . ;
-
- The word: +> is like -> but adds into the integer, analogous to +! ,
- used with a VARIABLE. The double precision counterparts of -> and +> are
- D-> and D+> . INTEGER is like the ANSI proposed word VALUE, except that
- -> is used instead of TO .