IF / ELIF / ELSE Conditional Chains
AmritaSense’s conditional branch instructions fully replicate Python’s elif chain syntax and provide a flexible, powerful conditional control mechanism. All conditional branches complete static address jump calculation at compile time, so runtime only uses pointer vector arithmetic.
Underlying implementation mechanism
Core class structure
Conditional branching is implemented through these core classes:
IFClause: base IF statement implementationELIFClause: ELIF branch implementationELSEClause: ELSE branch implementationConditionJumpNode: conditional evaluation and jump node
Compile-time address calculation
All jump addresses are computed during the render() stage:
- Simple IF-ELSE: uses relative addressing to point to a NOP placeholder.
- Complex IF-ELIF-ELSE: uses absolute addressing to point to the final NOP in the entire scope.
Runtime execution flow
Although the expansion is a compile-time layout, understanding runtime flow is still important.
Simple IF execution flow
- Execute
ConditionJumpNode. - Run the
conditionnode to obtain a boolean value. - If the result is
False, jump toNOP(skippingdo). - If the result is
True, continue to execute thedonode.
IF-ELSE execution flow
- Execute
ConditionJumpNode. - Run the
conditionnode. - If the result is
False, jump toELSEJumpNode. ELSEJumpNodejumps directly toelse_do.- If the result is
True, executedoand then skipelse_dotoNOP.
IF-ELIF-ELSE execution flow
For nested conditional chains, absolute addressing ensures correct jumps:
- Execute each
ConditionJumpNodein order. - Execute the first branch whose condition is
True. - If all conditions are
False, execute theELSEbranch if present. - After any branch completes, jump to the final
NOP.
Expanded layout structure
Simple IF expanded space
During workflow rendering, the IF instruction expands to this structure:
[ConditionJumpNode, condition, do, NOP]Corresponding Mermaid diagram:
IF-ELSE expanded space
[ConditionJumpNode, condition, do, ELSEJumpNode, else_do, NOP]IF-ELIF-ELSE expanded space
[ConditionJumpNode(condition1), condition1, do1,
ConditionJumpNode(condition2), condition2, do2,
ELSEJumpNode, else_do, NOP]Expansion illustration
Simple IF expansion
IF-ELSE expansion
IF-ELIF-ELSE expansion
Syntax features
Flexible syntax composition
# basic IF (ELSE not required)
IF(condition, do)
# IF-ELSE branch
IF(condition, do).ELSE(else_do)
# IF-ELIF chain
IF(condition, do).ELIF(condition2, do2)
# full IF-ELIF-ELSE chain
IF(condition, do).ELIF(condition2, do2).ELSE(else_do)Condition node features
- Unified underlying type: all conditions are
Node[bool]. - Sync/async mixing: conditions can be synchronous functions or asynchronous coroutines, and the engine normalizes them automatically.
- Complex condition support: condition nodes can contain dependency injection and complex logic.
Best practices
Condition optimization
- Short-circuit evaluation: put the most likely true conditions first.
- Simple conditions first: place complex conditions after simpler ones.
- Avoid side effects: condition nodes should not have side effects.
Performance considerations
- Compile-time optimization: all jump addresses are determined during rendering, so runtime overhead is zero.
- Memory efficiency: no extra data structures are created; execution uses pointer arithmetic only.
- Thread safety: conditional execution is fully thread-safe.
Error handling
- Type safety: conditions must return a boolean, or a type error is thrown.
- Exception propagation: exceptions thrown by condition nodes propagate normally to the outer flow.
- Resource cleanup: use TRY/FINALLY to ensure cleanup during condition execution.
With this design, AmritaSense conditionals maintain strong alignment with Python syntax while delivering compile-time optimized high-performance execution.
