Opcode 8: Extend#
Syntax#
*[a 8 b c] *[[*[a b] a] c]
Explanation#
Opcode 8 pins a new value to the head of the subject, then evaluate the body. This is how Nock implements variable binding—the new value becomes accessible at address 2.
Evaluate
bagainst subject to produce a new value.Construct
[new-value subject]as the extended subject.Evaluate
cagainst the extended subject.
After opcode 8, addresses shift:
Address 2 → the new pinned value
Address 3 → the original subject
Address 6 → what was address 2
Address 7 → what was address 3
Opcode 8 extends the subject, while opcode 7 replaces it:
*[a 7 b c] → *[*[a b] c] :: New subject is *[a b]
*[a 8 b c] → *[[*[a b] a] c] :: New subject is [*[a b] a]
There are two primary patterns for using opcode 8. In the first, a local variable is bound for use in a computation:
*[data [8 [compute-value] [body-using-value-at-+2]]]
:subject 42
Subject set to: 42
[8 [1 41] [[0 2] [0 3]]]
[41 42]
The other, more complex, pattern is to pin a core and its arms to the subject for invocation later. We’ll see this pattern in detail in opcode 9’s discussion.
*[context [8 [1 battery] [9 2 [0 1]]]]