Martin_H wrote:
It looks like you are indexing into a list of execution tokens based upon the remainder. The token determines the output, and most of them are the TOS, but the exceptions output text and drop TOS. It's extensible just by changing the execution token table.
Yes, that's the mechanism.
Martin_H wrote:
I called it forthy since it uses a number some of the more powerful features of Forth that a novice wouldn't know. I am tempted to rework it using Create Does> which should do the same thing, but bury the complexity.
I... fail to see how CREATE DOES> would help here? We're trying to make one word with a specific behaviour, not multiple words with similar (parameterizable) behaviour.
Martin_H wrote:
Code:
CREATE BEES
' .FIZZBUZZ , ' . , ' . , ' .FIZZ , ' . ,
' .BUZZ , ' .FIZZ , ' . , ' . , ' .FIZZ ,
' .BUZZ , ' . , ' .FIZZ , ' . , ' . ,
DOES> ( i addr --)
SWAP TUCK
15 MOD CELLS + @ EXECUTE ;
DOES> outside of a colon definition is not defined by the ANS standard. Easy enough to implement, now that I'm thinking about it, but not defined by the standard.
barrym95838 wrote:
To me it's obvious, in a kinky sort of way. :D You're using a custom-tailored constant array of execution tokens that works great for Fizz=3 and Buzz=5, but it could get a bit unwieldy if you were later tasked to revise it with more "buzz-words". It appears to be efficient in what it does.
Yes, it could get unwieldy very quickly, but since the sequence repeats every ( 3 5 * => 15 ) elements this was easy enough. If the sequence length was in the 17 - 24 range, it'd be a bit iffier, and if it were longer or MOD were expensive, then I might look for another solution.