示例工程 40-type-checker-in-python
40-type-checker-in-python/01-expr-typechecker/README.md
# 01 · expr-typechecker
**演示**:`ast.parse` 后自己遍历:局变量表 `dict[str, str]`、字面量类型、`+` 的 int/str 规则、比较/布尔、未定义变量与重新赋值。错误码 E000–E003。
**运行**:
```sh
uv run python examples/40-type-checker-in-python/01-expr-typechecker/expr_typechecker.py
```
**预期输出**(3.12.9):
```text
types: {'x': 'str', 's': 'str', 'ok': 'bool', 'bad': 'unknown', 'missing': 'unknown'}
codes: ['E001', 'E002', 'E003']
messages: ['unsupported operands: int and str', "undefined variable 'nope'", "variable 'x' was int, now str"]
```
**依赖**:仅标准库(`ast`)。
**注意**:AST 本身**不含类型**,所有类型信息都得自己建表推导;`bool` 是 `int` 的子类,判断顺序必须先 `bool` 后 `int`。`unknown` 是错误抑制器,避免一处错误引发连环报错。