Lowering: AST to Stack Bytecode
- Derive postfix emission from recursive tree shape.
- Preserve operand and argument order for noncommutative operations.
- Distinguish the tiny Lower API from v2 program bytecode.
1 / 6
Lower 20 - 3 * 4 by recursively finishing children before their parent:
| Instruction | Hand-execution stack |
|---|---|
PUSH_NUM 20 |
[20] |
PUSH_NUM 3 |
[20,3] |
PUSH_NUM 4 |
[20,3,4] |
MUL |
[20,12] |
SUB |
[8] |
The compiler visits left then right; the consumer pops right then left. Those orders agree because the stack is LIFO. Reversing either subtraction operand would change 8 into -8 even though addition-based tests might still pass.
For mix(7, 2+3), the sequence is PUSH_NUM 7; PUSH_NUM 2; PUSH_NUM 3; ADD; CALL mix 2. The arity is two expressions, not three literal pushes. mix is
held in the call instruction's name field at this stage, not pushed as a value.
A lowerer emits PUSH_NUM 20; PUSH_NUM 3; SUB; PUSH_NUM 4; MUL for the worked source. Is the arithmetic opcode implementation the first place to fix? Identify the required instruction movement.
Without the trace open, lower -f(8,3-1) / 2. Annotate each instruction with its stack effect and the call arity. Give a second expression that would expose reversing argument order.
Carry the postfix invariant into the v2 compiler. The expression artifact is complete when you can derive and diagnose its instruction sequence, not merely copy an opcode mapping.