Opcode 2: Evaluate#
Syntax#
Opcode 2 implements the * tar evaluate operator, which dynamically computes a new subject and formula, then evaluates the formula against the subject.
*[a 2 b c] *[*[a b] *[a c]]
Explanation#
The eval of Nock, opcode 2 computes a new subject from formula b, compute a new formula from formula c, then nock the results together. This enables dynamic code execution.
Evaluate
bagainst the subject to produce a noun (new subject).Evaluate
cagainst the subject to produce a noun (new formula).Evaluate the new formula against the new subject to yield the final product.
Opcode 2 is the foundation for function calls, code generation, and metaprogramming in Nock. However, it is not used directly by the Hoon and Jock compiler, which instead use opcode 2 via the opcode 9 macro.
:subject 42
Subject set to: 42
This simple formula computes the constant function that always returns 100, then returns its subject (now 100).
[2 [1 100] [1 [0 1]]]
100
This formula acts as a dynamic formula selection, which sets the subject to 5 and then calculates its increment.
:subject [5 [4 0 1]]
Subject set to: [5 4 0 1]
[2 [0 2] [0 3]]
6