Created
February 12, 2010 09:20
-
-
Save ELLIOTTCABLE/302430 to your computer and use it in GitHub Desktop.
Paws operator handling
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
“First off, we create a new list to screw with.” | |
my.thing ↼ (~) | |
“Now, we want to define an infix binary operator on it. Since left- | |
associativity is easy, we’re going to call this right-associative. We’ll | |
define a `foo` infix operator, which simply prints the two arguments given | |
it.” | |
my.thing foo ↼ routine | |
I.U. stdout put(@1, “ foo ”, @2) | |
“Time for the crucial part: telling the interpreter that this routine is an | |
operator, that it is right-associative, and that it is binary.” | |
my.thing foo [~is.operator, associativity, adicity] ↼ ~true, right, 2 | |
“Now that the interpreter’s parser knows about the operator (see below), we | |
can use it here: This should print something like “2 foo ()”.” | |
2 foo my.thing | |
“### What? | |
Okay, so what just happened? Let’s look at how the interpreter handles this: | |
- First off, although it’s unrelated to the execution, the interpreter is | |
unaware of ‘foo’ in the operator tables. It doesn’t care about it. | |
- During the parsing stage (which exists regardless of whether this is a | |
‘compiler’ or ‘interpreter’), the code is interpreted in a special | |
intermediate ‘compilation interpretation’ stage, wherein (usually) only | |
static code paths are executed. | |
- As the code is executed in this stage, if, after an expression is executed, | |
a `routine` has had one of its ‘magic operator-slots’ set or modified, | |
interpretation of the relevant context is ‘trashed’ and started from | |
scratch, with that new information in the operator tables. | |
- If the interpretation completes without such a change, then the state of | |
the operator tables are stored, and used (in static form) for the final | |
parsing stage, in preparation for actual interpretation (or compilation) | |
stage. | |
It’s important to note that by the time your code reaches runtime | |
(interpretation, or post-compilation), the parse tree is static, and the | |
operator tables are irrelevant (although also static), because they are not | |
going to be used to parse anything else. Thus, although `my.thing foo adicity` | |
in the above example, at runtime, will still be set to `2` (and be readable, | |
as per normal), that slot will no longer be ‘magical’—that is, it will have | |
no effect on anything, beyond the effect that any other slot has. | |
That means you can’t change those ‘magical fields’ at runtime, and expect | |
that change to have any meaningful effect. | |
” |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment