feat(lang): M3 access 内建与遮蔽循环 — 为 M7 迁移准备等价语义

access(集合, 键)(别名 取/取值):
- 复用 builtin_footnote_get,与旧 [键](集合) 语义完全一致
  (Map 按键 / List 1-based 下标 / 缺键报 missing map key,不静默 None)
- 不新增平行实现,避免两套索引语义漂移
- 用途:M7 把 506 处 [k](c) 机械改写成 access(c, k),语义零漂移

遮蔽循环:- [描述](遍历 集合 为 变量)
- 仅 occlusion 模式识别;接受 遍历/for 与 为/as/in
- 描述存入 Stmt::ForEach.label(Option<String>),供 M8 渲染/诊断

为什么先 M3 后 M7:迁移目标必须先存在且等价,否则就是"边迁移边发明语义"。

实测:
  access(`表`,"键") → 值;access(`列`,2) → 二;
  缺键 → missing map key(与旧取元同一错误)
  - [篮子里的每个果子](遍历 `列` 为 `果`) → 果=一 / 果=二

测试:2 个集成(含缺键错误保真);全量 595 passed / 0 failed(M5 后 593 + 2)

文档:doc/design/change-m3-access-and-loop.md
This commit is contained in:
cflmy committed 2026-10-22 15:23:36 +08:00
1 parent 409b8df2cb
commit 0405a60d68
7 files changed
+205

No files matched your search

+23
View File
@@ -883,6 +883,7 @@ impl Interpreter {
Stmt::ForEach {
item,
collection,
label: _,
body,
span,
} => {
@@ -1184,6 +1185,28 @@ impl Interpreter {
}
match call.callee.as_str() {
// `<TAB>` M3: `access(集合, 键)` — the exact polymorphism of the
// legacy `[键](集合)` index (map key / 1-based list index, error on
// missing). Migration rewrites legacy indexes to this call, so the
// two must stay semantically identical.
"access" | "取" | "取值" => {
let bound = bind_args(&["value".into(), "key".into()], &ev_args, false)
.map_err(|m| self.err(m))?;
let base = bound
.get("value")
.cloned()
.ok_or_else(|| self.err("access requires value (collection)"))?;
let key = bound
.get("key")
.map(|v| match v {
Value::Text(t) => t.clone(),
Value::Int(i) => i.to_string(),
other => other.as_display(),
})
.ok_or_else(|| self.err("access requires key"))?;
return crate::builtin::builtin_footnote_get(&base, &key)
.map_err(|m| self.err(m));
}
"print" => {
let bound = bind_args(&["text".into()], &ev_args, false)
.map_err(|m| self.err(m))?;