ir230@sdcc6.ucsd.edu (john wavrik) (09/14/90)
John Hayes recently posted information on the new word START: and
asked for comments. I would like to show how two fairly common and
substantial problems are solved with it.
For those using traditional Forth systems, the definition of
START: is
: START: HERE DOCOL , ;
Where DOCOL is the address of the inner interpreter. It can be
defined by ' : @ CONSTANT DOCOL.
I. [: and ;]
Late last year I posted code for this pair of words and gave
examples of their use. They work like : and ;. They compile the
code between them but they do not generate a header -- instead they
return the CFA of the generated code. They are state smart.
The original use of these words was to make it easy to generate a
family of special words which are obtained from a general word by
changing the runtime environment (which may depend on executable as
well as numerical parameters).
: SPECIAL ( * Runtime Environment *)
[: ..special code 1 ;]
[: ..special code 2 ;]
.....
n1 n2 ... ( numerical parameters )
( * End of Runtime Environment * )
GENERAL-WORD ;
(Imagine, for example, a collection of sorting programs all derived
from a general template by changing parameters describing the
comparison function, how data items are exchange, how they are
stored, etc.)
The code posted last year was this:
Blk # 1 File: environs.blk
0. \ Headerless code fragments 04Nov88jjw
1. \ Use like : and ; -- they return the cfa of the code
2.
3. VARIABLE OSTATE ' : @ CONSTANT DOCOL
4. : <[:> R> DUP @ >R 2+ ;
5.
6. : [: STATE @ DUP OSTATE !
7. IF COMPILE <[:> ?>MARK DOCOL ,
8. ELSE HERE DOCOL , !CSP ] THEN ; IMMEDIATE
9.
10. : ;] OSTATE @
11. IF COMPILE EXIT ?>RESOLVE
12. ELSE ?CSP COMPILE EXIT
13. [COMPILE] [ THEN ; IMMEDIATE
14.
15.
This code assumes a traditional Forth (indirect threaded code, IP sent
to the return stack, HERE marks the start of code, etc.). Using START:
these words can be coded this.
: [: STATE @ DUP OSTATE !
IF POSTPONE FALSE POSTPONE IF START:
ELSE START: ] THEN ; IMMEDIATE
: ;] POSTPONE EXIT OSTATE @
IF >R POSTPONE THEN R> POSTPONE LITERAL
ELSE POSTPONE [ THEN ; IMMEDIATE
Therefore this structure will be portable in the proposed ANSI
Standards. (John Hayes gets the credit for suggesting the replacement
of the inline handler <[:> by an IF .. THEN control structure).
II. Inline Data Handlers
Some are disturbed by what they regard as the dual pre/postfix
nature of Forth. There is a very simple rule for making sense of
this:
RULE: You know what the interpreter is going to do with a string.
If that's not what you want done, you must precede the
string by a handler.
(Simiarly numbers in a dictionary body which are not
addresses to be executed are preceded by a handler.)
Here is an example from a system designed to handle very large
integers and rationals (BIGNUMS).
v% 123456789
dup v* v. 15241578750190521
q% 1/2 q% 1/3 q+ q. 5/6
q% 1/2 q% 2/3 q* q. 1/3
: pi q% 355/113 ;
pi q. 355/113
v1 v.
vnum 20!
: factorials v1 21 1 do i . 2 spaces
i s>v v*
dup v. cr
loop 20! vmove ;
factorials
20! v. 2432902008176640000
20! dup v* v. 5919012181389927685417441689600000000
In this language integer operations are prefaced by v (v+, v. etc)
and rational number operations by q. v% and q% are handlers for
these data types. The handler converts a string to a big integer.
When used in interpretation, a pointer to the data (in its
converted form) is return to the stack. If used during compilation
the internal representation is stored in the dictionary preceded by
a handler VLIT. This is precisely how traditional Forth handles its
standard numerical types. Here are the definitions in tradition
Forth.
4 : VLIT R> DUP VSIZE + >R ;
5 : V% STATE @ IF COMPILE VLIT BL WORD VNUMBER
6 HERE VSIZE ALLOT VMOVE
7 ELSE BL WORD VNUMBER THEN ; IMMEDIATE
The same assumptions are made about how Forth works as are made
above.
In this case, too, START: allows the handler V% to be defined
portably in the proposed ANSI Standard.
2 : V% STATE @
3 IF POSTPONE FALSE POSTPONE IF START: >BODY
4 BL WORD VNUMBER VSIZE ALLOT
5 OVER VMOVE >R POSTPONE THEN R>
6 POSTPONE LITERAL
7 ELSE BL WORD VNUMBER THEN ; IMMEDIATE
I think that there is some reason to be enthusiastic about John
Hayes' efforts in getting this word accepted.
It would be excessive to claim that this solves all the major
problems of designing application oriented programming languages.
But it is fair to say that it provides ANSI Forth with a little more
of the power that traditional Forth has.
John J Wavrik
jjwavrik@ucsd.edu Dept of Math C-012
Univ of Calif - San Diego
La Jolla, CA 92093 dwp@willett.pgh.pa.us (Doug Philips) (09/19/90)
In <12658@sdcc6.ucsd.edu>, ir230@sdcc6.ucsd.edu (john wavrik) writes: > II. Inline Data Handlers > > Some are disturbed by what they regard as the dual pre/postfix > nature of Forth. There is a very simple rule for making sense of > this: > > RULE: You know what the interpreter is going to do with a string. > If that's not what you want done, you must precede the > string by a handler. > > (Simiarly numbers in a dictionary body which are not > addresses to be executed are preceded by a handler.) The RULE you state is good for deciding how to write more prefix words. However, the reason your RULE fails to quell the disquiet is because of an equivocation about "the dual pre/postfix nature of Forth." As you have pointed out extremely effectively in previous messages, one of Forth's strong points is in being a "language lab". I have no qualms with an increasing proliferation of prefix words in the derived application. My concern is with the duality in the core of Forth itself. -Doug --- Preferred: dwp@willett.pgh.pa.us Daily: {uunet,nfsun}!willett!dwp