neo_solidity/ir/expressions/dispatch/
conditional.rs

1fn try_lower_expression_conditional(
2    expr: &Expression,
3    ctx: &mut LoweringContext,
4    instructions: &mut Vec<Instruction>,
5) -> Option<bool> {
6    match expr {
7        Expression::ConditionalOperator(_, condition, then_expr, else_expr) => {
8            // Ternary operator: condition ? then_expr : else_expr
9            // Semantics: evaluate condition, then evaluate ONLY one branch based on result
10
11            // Generate unique labels for this ternary expression
12            let else_label = ctx.next_label();
13            let end_label = ctx.next_label();
14
15            // Step 1: Evaluate condition
16            if !lower_expression(condition, ctx, instructions) {
17                return Some(false);
18            }
19
20            // Step 2: Jump to else branch if condition is false (zero).
21            instructions.push(Instruction::JumpIf { target: else_label });
22
23            // Step 3: Evaluate then branch (condition was true/non-zero)
24            if !lower_expression(then_expr, ctx, instructions) {
25                return Some(false);
26            }
27            instructions.push(Instruction::Jump { target: end_label });
28
29            // Step 4: Else branch label
30            instructions.push(Instruction::Label(else_label));
31
32            // Step 5: Evaluate else branch (condition was false/zero)
33            if !lower_expression(else_expr, ctx, instructions) {
34                return Some(false);
35            }
36
37            // Step 6: End label - result is on stack from whichever branch executed
38            instructions.push(Instruction::Label(end_label));
39
40            Some(true)
41        }
42        _ => None,
43    }
44}