示例工程 23-bytecode-dis-ast

23-bytecode-dis-ast/02-constant-folding/README.md

markdown · 0.6 KB

# 02 · constant-folding
 
**演示**:`1 + 2 * 3` 在编译期折叠成常量 `7`,函数里没有 `BINARY_OP`;`value + 2 * 3` 只能折叠 `2*3`,保留一次加法。
**运行**:
 
```sh
uv run python examples/23-bytecode-dis-ast/02-constant-folding/constant_folding.py
```
 
**预期输出**(3.12.9):
 
```text
folded_consts: [7]
folded_binary: False
not_folded_consts: [6]
not_folded_binary: True
```
 
**依赖**:仅标准库。
**注意**:3.12 用 `RETURN_CONST` 返回折叠结果;3.13/3.14 可能用 `LOAD_SMALL_INT`。示例同时识别 `LOAD_CONST`/`RETURN_CONST`/`LOAD_SMALL_INT`,保证三版本都能看到常量。