[comp.lang.forth] Redefining numbers

hawley@adobe.COM (Steve Hawley) (03/23/91)

--------------------------------------------------------------------------
Dave Lowry sez:
With all this discussion of the merits of Forth, I wonder if somebody
could comment on the usefulness of a language that merrily accepts
things like:

: 1 2 ;

--------------------------------------------------------------------------
Charles Eaker sez:
Few languages can protect programmers from nonsense.  But very few even
allow you to express the kind of nonsense found in : 1 2 ; The
alternative is to introduce restrictions on what characters can appear
in a word's name and where they may appear in the name. If : 1 2 ; is
ruled out, so is : 1 1 ; Many of us would argue that the cure is
clearly worse than the disease. (Although the disease can be close
to fatal when FEED is accepted by the interpreter when the base is
hex, and the word named FEED is not, contrary to your expectations,
in the search order.)

--------------------------------------------------------------------------
I say:
Actually, you don't have to place restrictions on what character can or can
not appear in a words name to prevent the previous definition.

Remember that : really doesn't do a whole lot.  It turns on the compile
flag, and builds a dictionary header from the next token in the input stream.
That's it.

The real work is in the interpretter itself.

There is going to be code like this:

WORD DUP SEARCH
	IF \ The word is in the dictionary.
		SWAP POP STATE
		IF \ We're compiling
			DUP ?IMMEDIATE IF EXECUTE ELSE ,
		ELSE EXECUTE THEN
	ELSE \ The word is not in the dictionary
		DUP ?NUMBER
		IF \ It's a number
			BASE SWAP CONVERT \ Convert to binary
			STATE
			IF \ We're compiling
				LIT
			THEN \ Not compiling - leave on stack
		ELSE \ Not a number
			LENGTH TYPE ABORT" -- Unknown token."
		THEN
	THEN

Which will handle both interpretation and compilation of code.  If you swap
the order of dictionary searching vs number checking, you could still define
words like
: 1 2 ;
But you couldn't execute them normally.  You'd have to do:
' 1 EXECUTE
instead.

BUT

Here's why
: 1 2 ;
is a reasonable thing to be able to do.

Suppose my target machine has the ability to clear words and has a quick
version of MOVE for number less than 16.

Wouldn't you like to be able to do this:
CODE 0 -(PSP) CLR.W END-CODE
CODE 1 1 -(PSP) MOVE.Q END-CODE
CODE 2 2 -(PSP) MOVE.Q END-CODE
CODE 3 3 -(PSP) MOVE.Q END-CODE
CODE 4 4 -(PSP) MOVE.Q END-CODE
...

Because you've suddenly superceded the definitions of the integers that you
can tailor your forth to the target machine, and don't have to hope that your
compiler writer did that for you (in your favorite compiler-based language).

Of course you'd actually want to make these defnitions state-smart to compile
in the instruction if STATE is true, instead of simply executing it, or if you
have the MACRO facility, that'd be cool too.

Steve Hawley
hawley@adobe.com
-- 
"Did you know that a cow was *MURDERED* to make that jacket?"
"Yes.    I didn't think there were any witnesses, so I guess I'll have to kill
 you too." -Jake Johansen