3 ms·
It's just a case of a pattern definition including references to itself, which are then linked to its definition before use. In the same way function definition
by Nevermark 3y ago
It's just a case of a pattern definition including references to itself, which are then linked to its definition before use. In the same way function definitions often contain reference themselves, which are linked to complete the definition.
uint64_t alignBitsLeft(uint64_t x) {
return ((x >> 63) == 1)) ? x : alignBitsLeft(x << 1);
}
The pattern "alignBitLeft(uint64_t)" definition starts in line 1, but is not completed until line 3. Yet line 2 references that pattern, before the definition is complete.
This works, because of a linking step at definition completion, where the "alignBitsLeft" reference gets linked to the now complete "alignBitsLeft" definition.
Similarly for:
> loop Body is { Body; loop Body }
The "loop Body" definition beings, including a reference to itself. Once the whole definition has been built up, the "loop Body" reference gets linked back to the "loop Body" definition. Ready for use.
--
Another way to look at this is memoization (at compile or run time). When you evaluate the second line of this:
n is 1;
loop { print n; n is n + 1; }
You get this:
{ print n; n is n + 1; loop { print n; n is n + 1; }}
If we cache that definition before evaluating it, when we get to the embedded loop clause, we don't need to compute it again. We have already computed that exact loop pattern before and can reuse the result (over and over).