flat assembler
Message board for the users of flat assembler.

Index > Compiler Internals > [fasmg] feature request: calm assert (clause-based dispatch)

Author
Thread Post new topic Reply to topic
bitRAKE



Joined: 21 Jul 2003
Posts: 4665
Location: vpcmpistri
bitRAKE 03 Nov 2026, 07:26
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):
Code:
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:
  1. 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.
  2. The middle definition asserts number = 2. If true, it displays "prime"; otherwise, it unwinds again.
  3. 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. Very Happy
Post 03 Nov 2026, 07:26
View user's profile Send private message Visit poster's website Reply with quote
Tomasz Grysztar



Joined: 16 Jun 2003
Posts: 8578
Location: Kraków, Poland
Tomasz Grysztar 03 Nov 2026, 11:46
This is an interesting suggestion, but not because of having ASSERT in CALM analogously to the standard directive, but because proposed implementation would add an actual new functionality (the callback mechanism). It neatly combines multiple uses into a single language addition, which has been my long tradition for fasm development (compare how MATCH was designed to solve multiple unrelated problems with a single new directive).

I'm going to think about it, that is certain.
Post 03 Nov 2026, 11:46
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2026, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.