Currently,
assert is a base-language directive that halts/errors assembly when a condition is not met, and it has no direct counterpart inside
calminstruction.
I’d like to propose adding an
assert command to
calminstruction with behavior orthogonal to the base directive:
if the asserted condition fails, it aborts the current instruction and walks down the symbol's definition stack to try the previous definition of the same name. If no prior definition exists on the stack, it fails with an assembly error just like the standard
assert directive.
Motivating Example
Because
fasmg already maintains a stack of definitions for every redefined symbol, a stack-walking
assert turns instruction redefinition into clean, declarative predicate dispatch (similar to pattern/guard clauses in functional languages):
calminstruction(result) PrimeQ number*
display 'not prime' bappend 10
end calminstruction
calminstruction(result) PrimeQ number*
assert number = 2
display 'prime' bappend 10
end calminstruction
calminstruction(result) PrimeQ number*
assert number and 1 ; odd
display '*to do*' bappend 10
end calminstruction
When
PrimeQ is invoked:
- The topmost definition checks if number is odd. If true, it executes; if false, assert unwinds and delegates to the previous PrimeQ on the stack.
- The middle definition asserts number = 2. If true, it displays "prime"; otherwise, it unwinds again.
- The base definition catches everything else. (And if a base catch-all isn't provided, a failed assert at the bottom of the stack naturally reports an assertion error.)
Why this fits fasmg and CALM- Eliminates Fallback Boilerplate: Currently, overriding an instruction in CALM to handle a special case requires check, a conditional jump, arrange, and assemble (or call). assert reduces this guard-and-fallback pattern to a single line.
- Solves Fallback for Labeled CALM Instructions (calminstruction(label)): In CALM, call can only invoke instruction-class symbols—it cannot directly invoke a previous labeled calminstruction (nor can it invoke a macro, struc, or built-in instruction). Today, falling back from a calminstruction(label) requires arrange + assemble, which forces re-tokenization and sacrifices CALM's speed advantage. Walking the definition stack at the engine level allows fast, direct tail-dispatch to the prior definition regardless of whether it is a labeled calminstruction, a regular macro, or a built-in instruction.
- Modular Instruction Set Extensions: Instruction sets or DSLs can be layered modularly. An extension package can push specialized instruction handlers onto existing mnemonics and rely on `assert` to fall back to the base architecture's handler whenever the extension's constraints aren't met, without modifying the base definitions.
- Unified Semantics: If a symbol only has one definition (or reaches the bottom of the stack), assert inside calminstruction behaves identically to the base language's assert: it signals an error when the condition is false.
Proposed Semantics
Syntax: assert <logical_expression> (evaluating pre-compiled logical expressions identically to `check`).
- Execution on True: Execution continues to the next CALM command.
- Execution on False:
- If a prior definition of the active instruction symbol exists on the symbol stack, the current CALM execution frame is abandoned, and the engine dispatches the original invocation (with its original label and arguments) to the previous definition.
- If no prior definition exists, the assembler signals an error (just as `assert` does outside CALM).
Alternately, it could trigger on flag - works in conjunction with check, et al.
The policy around side-effects is the biggest rejection for me - just leave it an open complexity.
