Files
cflmy e029853e32 feat(lang): implement RFC-LANG-0002 unified semantics (breaking)
One Markdown-to-program mapping, no version selector or legacy mapper.

Frontend/runtime:
- resolve @exec/@knowledge heading declarations with nearest-ancestor
  inheritance; only an Exec H2 is callable; add UnitKind::Knowledge and
  sem.* diagnostics (mode marker, unit name, exec-without-function,
  payload nesting/empty, syntax, control stack, params position,
  version directive, inferred-signature lint)
- extract Strong/Emph/inline Code as one-statement carriers, refuse
  nested carriers, and honour heading segment boundaries so an if/while
  cannot be closed across a heading
- lower if/else/while/end to the existing Branch/Loop/Return IR with a
  compile-time control stack; keep payload-relative source spans through
  expression parsing and rewrite them onto the file
- accept params(...) only as the first direct Exec H2 payload; keep the
  existing free-variable inference and named-only rule for inferred
  signatures; fix duplicate-mode/else/end and Knowledge-body inertness
- reject marqdo_semantics/syntax front matter directives

Data and tests:
- migrate stdlib/**/*.mq.md, regenerate stdlib.json digest
- migrate examples and the full semantics/negative/runtime corpus to
  the only current syntax; expected values/runtime codes preserved
- migrate Rust-embedded .mq.md fixtures, init template, CLI help and
  security-lane fixtures

Verified: cargo fmt --check, cargo clippy -D warnings and
cargo test --workspace --locked (green).
2026-11-21 12:26:46 +08:00

417 lines
5.4 KiB
Markdown

# @exec table
## 选择列
`params(数据, 列)`
**列名 = table.columns(数据)**
**结果行 = []**
**行号 = 0**
`while 行号 < 长度(数据)`
**行 = table.row(数据, 行号)**
**结果行 = list.append(结果行, [行[列]])**
**行号 = 行号 + 1**
`end`
`return 表([列名[列]], 结果行)`
## 过滤数值大于
`params(数据, 列, 阈值)`
**列名 = table.columns(数据)**
**结果行 = []**
**行号 = 0**
`while 行号 < 长度(数据)`
**行 = table.row(数据, 行号)**
`if 行[列] > 阈值`
**结果行 = list.append(结果行, 行)**
`else`
`if true`
**忽略 = 0**
`end`
`end`
**行号 = 行号 + 1**
`end`
`return 表(列名, 结果行)`
## 插入有序
`params(已排序, 行, 列)`
**新列表 = []**
**已插入 = false**
**下标 = 0**
`while 下标 < 长度(已排序)`
**当前行 = 已排序[下标]**
`if not 已插入 and 当前行[列] > 行[列]`
**新列表 = list.append(新列表, 行)**
**新列表 = list.append(新列表, 当前行)**
**已插入 = true**
`else`
`if true`
**新列表 = list.append(新列表, 当前行)**
`end`
`end`
**下标 = 下标 + 1**
`end`
`if not 已插入`
**新列表 = list.append(新列表, 行)**
`else`
`if true`
**忽略 = 0**
`end`
`end`
`return 新列表`
## 排序
`params(数据, 列)`
**列名 = table.columns(数据)**
**行数 = 长度(数据)**
**已排序 = []**
**下标 = 0**
`while 下标 < 行数`
**已排序 = 插入有序(已排序, table.row(数据, 下标), 列)**
**下标 = 下标 + 1**
`end`
`return 表(列名, 已排序)`
## 追加行
`params(数据, 行)`
**列名 = table.columns(数据)**
**所有行 = []**
**行号 = 0**
`while 行号 < 长度(数据)`
**所有行 = list.append(所有行, table.row(数据, 行号))**
**行号 = 行号 + 1**
`end`
`return 表(列名, list.append(所有行, 行))`
## 设置单元格
`params(数据, 目标行, 目标列, 值)`
**列名 = table.columns(数据)**
**新行表 = []**
**行号 = 0**
`while 行号 < 长度(数据)`
**行 = table.row(数据, 行号)**
`if 行号 == 目标行`
**新行 = []**
**列号 = 0**
`while 列号 < 长度(行)`
`if 列号 == 目标列`
**新行 = list.append(新行, 值)**
`else`
`if true`
**新行 = list.append(新行, 行[列号])**
`end`
`end`
**列号 = 列号 + 1**
`end`
**新行表 = list.append(新行表, 新行)**
`else`
`if true`
**新行表 = list.append(新行表, 行)**
`end`
`end`
**行号 = 行号 + 1**
`end`
`return 表(列名, 新行表)`
## 连接
`params(左, 右, 左列, 右列)`
**表头 = list.concat(table.columns(左), table.columns(右))**
**结果行 = []**
**左行号 = 0**
`while 左行号 < 长度(左)`
**左行 = table.row(左, 左行号)**
**键 = 左行[左列]**
**右行号 = 0**
`while 右行号 < 长度(右)`
**右行 = table.row(右, 右行号)**
`if 右行[右列] == 键`
**结果行 = list.append(结果行, list.concat(左行, 右行))**
`else`
`if true`
**忽略 = 0**
`end`
`end`
**右行号 = 右行号 + 1**
`end`
**左行号 = 左行号 + 1**
`end`
`return 表(表头, 结果行)`
## 分组
`params(数据, 键列, 值列)`
**结果 = {}**
**行号 = 0**
`while 行号 < 长度(数据)`
**行 = table.row(数据, 行号)**
**键 = 行[键列]**
**当前 = map.get(结果, 键)**
**列表 = []**
`if map.has(结果, 键)`
**列表 = 当前**
`else`
`if true`
**忽略 = 0**
`end`
`end`
**列表 = list.append(列表, 行[值列])**
**结果 = map.set(结果, 键, 列表)**
**行号 = 行号 + 1**
`end`
`return 结果`
## 连接片段
`params(片段)`
**结果 = ""**
**下标 = 0**
`while 下标 < 长度(片段)`
`if 下标 > 0`
**结果 = 结果 + " | "**
`else`
`if true`
**忽略 = 0**
`end`
`end`
**结果 = 结果 + 片段[下标]**
**下标 = 下标 + 1**
`end`
`return 结果`
## 连接行
`params(行列表)`
**结果 = ""**
**下标 = 0**
`while 下标 < 长度(行列表)`
`if 下标 > 0`
**结果 = 结果 + "\n"**
`else`
`if true`
**忽略 = 0**
`end`
`end`
**结果 = 结果 + 行列表[下标]**
**下标 = 下标 + 1**
`end`
`return 结果`
## 转义单元格
`params(值)`
**文本值 = 文本(值)**
**点 = text.codepoints(文本值)**
**结果 = ""**
**下标 = 0**
`while 下标 < 长度(点)`
`if 点[下标] == 124`
**结果 = 结果 + text.from_codepoint(92) + "|"**
`else`
`if 点[下标] == 10 or 点[下标] == 13`
**结果 = 结果 + " "**
`else`
`if true`
**结果 = 结果 + text.from_codepoint(点[下标])**
`end`
`end`
`end`
**下标 = 下标 + 1**
`end`
`return 结果`
## 转GFM
`params(数据)`
**列名 = table.columns(数据)**
**表头 = []**
**下标 = 0**
`while 下标 < 长度(列名)`
**表头 = list.append(表头, 转义单元格(列名[下标]))**
**下标 = 下标 + 1**
`end`
**行列表 = []**
**行列表 = list.append(行列表, "| " + 连接片段(表头) + " |")**
**分隔 = []**
**下标 = 0**
`while 下标 < 长度(列名)`
**分隔 = list.append(分隔, "---")**
**下标 = 下标 + 1**
`end`
**行列表 = list.append(行列表, "| " + 连接片段(分隔) + " |")**
**行号 = 0**
`while 行号 < 长度(数据)`
**行 = table.row(数据, 行号)**
**单元格 = []**
**列号 = 0**
`while 列号 < 长度(行)`
**单元格 = list.append(单元格, 转义单元格(行[列号]))**
**列号 = 列号 + 1**
`end`
**行列表 = list.append(行列表, "| " + 连接片段(单元格) + " |")**
**行号 = 行号 + 1**
`end`
`return 连接行(行列表)`