Opcode 6: Conditional#
Syntax#
*[a 6 b c d] *[a *[[c d] 0 *[[2 3] 0 *[a 4 4 b]]]]
Explanation#
Opcode 6 implements a conditional branch. Evaluate test formula b; if true (0), evaluate and return c; if false (1), evaluate and return d. Crashes on non-boolean test results.
Evaluate
bagainst the subject, which must produce 0 or 1.If 0 (true): evaluate
cagainst subject and return.If 1 (false): evaluate
dagainst subject and return.
Any other value crashes (as enforced by the macro expansion).
In fact, much of the weirdness of the macro expression can be explained by its parsimonious use of the boolean result. From right to left:
*[a 4 4 b]evaluates b, then adds 2 (so 0 → 2, 1 → 3).*[[2 3] 0 ...]uses the result as an address into[2 3].*[[c d] 0 ...]uses 2 or 3 as address into[c d].
This cleverly selects c (at address 2) for true, d (at address 3) for false. (It does not pass through directly to avoid other results selecting other possible slots.)
:subject 42
Subject set to: 42
You need an expression that results in 0 or 1 to use as the test. For example, to test whether the subject is equal to a value such as 42, use opcode 5:
[6 [5 [1 42] [0 1]] [1 100] [1 0]]
100
This is how to implement a NOT operator on a loobean value:
:subject 0
Subject set to: 0
[6 [5 [1 0] [0 1]] [1 1] [1 0]]
1