Fail Better? Bah!
I fully expect to fail this morning: I don’t see how to do what I need to do. I’ll try to fail in a way that sets me up for a win sometime soon. Result: Bah!
Summary
Three hours and nothing. I’m trying to understand how to make a colon definition of constant work. Here’s what should work:
: CONSTANT CREATE , DOES> @ ;
This should define a new word, CONSTANT, such that When we say this:
2025 CONSTANT YEAR
We should get a new word, YEAR, and when we say
YEAR
The top of the stack should have 2025.
The glitch is that while defining CONSTANT, we are also defining YEAR. If we assume that
CREATE YEAR
Creates a word YEAR whose definition is to push the currently allocated heap location to the stack,
We want the definition above to create its word (YEAR) then push the current stack (2025) to the heap, which is the comma word in the definition. Suppose the stack address is 5. Then we want to compile a literal 5 into the definition of YEAR. And then, because we have the DOES> and the @, we want to compile @ into YEAR, resulting as if we had defined YEAR
: YEAR 5 @
With the magical side effect that 2025 went into the heap and allocated cell 5 for it.
We seem to be defining two words “at the same time”. The colon definition is defining CONSTANT, but CONSTANT, once it is defined, will define YEAR. But when CONSTANT does that, it will read the input to get YEAR, the name of the word to define. Then it will grab the stack top at that time, allocate its value into the heap, and THEN compile the *# 5 @
into YEAR.
I am not clear on who does what and how they know to do it. I am not clear on what combination of immediate and not immediate can make it happen.
I need a simpler test but this one is so simple! I must stop, read, and think. This is getting very frustrating.
Bah!