Fronds tables
OK, here's my latest greatest idea on how to do tables:
: -my-table [ foo bar baz qux
: my-table [ `-my-table exec-nth;
Now [ 2 my-table will execute baz.
Note: there is no "compile time" for -my-table because the ` prefix tells the recursive compiler not to compile that word. exec-nth-table makes sure that foo, bar, baz and qux are compiled at the end with compile-deps.
Notes:
exec-nth; might be implemented something like this:
: exec-nth; ,2* dict->def dup 2+ lit ,+ ,h@ ,tok-do ,; compile-deps ;
Another example:
: -ed-key-dispatch [ key-r ed-go-to-def key-g ed-go-back key-x bye
: ed-key-dispatch [ `-ed-key-dispatch case;
Advantages
1) definition of -my-table is totally clean (just a list of words/numbers with no extra syntax inbetween)
2) supports numbers just as well. Can even be a mix of numbers, constants and words
3) supports immediates seamlessly.
4) bounds-checking table lookup words can be easily implemented because the length of the source for -my-table is known.
Disadvantages
1) you have to create two definitions
2) you must be careful that the contents of -my-table are yellow/execute and that you use backtic (`) instead of tic (') in my-table
It would be cool if it could look like this:
: my-table [ exec-nth-table: foo bar baz qux
but that would require hacking the compiler, which I'm not ready to do yet.
Key
: defined
compiled
[ executed (at compile time)
` prefix is like tic (it puts an xt on the stack) but doesn't cause the word to get compiled
, prefix is like postpone