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).
This commit is contained in:
cflmy committed 2026-11-21 12:26:46 +08:00
1 parent 49835424ee
commit e029853e32
375 files changed
+6591 -4790

No files matched your search

@@ -1,130 +1,164 @@
## 成功
## @exec 成功
`(值)`
`params(值)`
*{ok: true, value: 值}*
`return {ok: true, value: 值}`
## 失败
## @exec 失败
`(代码, 消息)`
`params(代码, 消息)`
*{ok: false, code: 代码, message: 消息}*
`return {ok: false, code: 代码, message: 消息}`
## 写字节
## @exec 写字节
`(内存, 基址, 字节)`
`params(内存: Memory, 基址: Int, 字节: List)`
**下标 = 0**
`下标 = 0`
- `下标 < 长度(字节)`
`while 下标 < 长度(字节)`
**`内存[基址 + 下标] = 字节[下标]`**
`内存[基址 + 下标] = 字节[下标]`
**下标 = 下标 + 1**
`下标 = 下标 + 1`
*None*
`end`
## 运行
`return None`
`(内存, 程序长度, 栈基址)`
## @exec 运行
程序从偏移 0 开始,栈从 `栈基址` 开始。指令集:
`params(内存: Memory, 程序长度: Int, 栈基址: Int)`
程序从偏移 0 开始,栈从 栈基址 开始。指令集:
1 = PUSH 立即数,2 = ADD,3 = MUL,5 = JMP,6 = JZ,7 = HALT。
**指令指针 = 0**
`指令指针 = 0`
**栈指针 = 0**
`栈指针 = 0`
**运行中 = true**
`运行中 = true`
**错误 = ""**
`错误 = ""`
- `指令指针 < 程序长度 and 运行中 and 错误 == ""`
`while 指令指针 < 程序长度 and 运行中 and 错误 == ""`
**操作码 = 内存[指令指针]**
`操作码 = 内存[指令指针]`
1. `操作码 == 1`
`if 操作码 == 1`
**`内存[栈基址 + 栈指针] = 内存[指令指针 + 1]`**
`内存[栈基址 + 栈指针] = 内存[指令指针 + 1]`
**栈指针 = 栈指针 + 1**
`栈指针 = 栈指针 + 1`
**指令指针 = 指令指针 + 2**
`指令指针 = 指令指针 + 2`
2. `操作码 == 2`
`else`
**右 = 内存[栈基址 + 栈指针 - 1]**
`if 操作码 == 2`
**左 = 内存[栈基址 + 栈指针 - 2]**
`右 = 内存[栈基址 + 栈指针 - 1]`
**`内存[栈基址 + 栈指针 - 2] = 左 + 右`**
`左 = 内存[栈基址 + 栈指针 - 2]`
**栈指针 = 栈指针 - 1**
`内存[栈基址 + 栈指针 - 2] = 左 + 右`
**指令指针 = 指令指针 + 1**
`栈指针 = 栈指针 - 1`
3. `操作码 == 3`
`指令指针 = 指令指针 + 1`
**右 = 内存[栈基址 + 栈指针 - 1]**
`else`
**左 = 内存[栈基址 + 栈指针 - 2]**
`if 操作码 == 3`
**`内存[栈基址 + 栈指针 - 2] = 左 * 右`**
`右 = 内存[栈基址 + 栈指针 - 1]`
**栈指针 = 栈指针 - 1**
`左 = 内存[栈基址 + 栈指针 - 2]`
**指令指针 = 指令指针 + 1**
`内存[栈基址 + 栈指针 - 2] = 左 * 右`
4. `操作码 == 5`
`栈指针 = 栈指针 - 1`
**指令指针 = 内存[指令指针 + 1]**
`指令指针 = 指令指针 + 1`
5. `操作码 == 6`
`else`
**条件 = 内存[栈基址 + 栈指针 - 1]**
`if 操作码 == 5`
**栈指针 = 栈指针 - 1**
`指令指针 = 内存[指令指针 + 1]`
1. `条件 == 0`
`else`
**指令指针 = 内存[指令指针 + 1]**
`if 操作码 == 6`
2. `true`
`条件 = 内存[栈基址 + 栈指针 - 1]`
**指令指针 = 指令指针 + 2**
`栈指针 = 栈指针 - 1`
6. `操作码 == 7`
`if 条件 == 0`
**运行中 = false**
`指令指针 = 内存[指令指针 + 1]`
7. `true`
`else`
**错误 = "vm.invalid_opcode"**
`指令指针 = 指令指针 + 2`
1. `错误 != ""`
`end`
*失败("vm.invalid_opcode", "未知指令")*
`else`
1. `栈指针 == 0`
`if 操作码 == 7`
*失败("vm.empty_stack", "没有结果")*
`运行中 = false`
*成功(内存[栈基址 + 栈指针 - 1])*
`else`
## main
`错误 = "vm.invalid_opcode"`
**内存 = memory.new(1, 1)**
`end`
第一段程序计算 `(5 + 7) * 3`。
`end`
> 写字节(内存, 0, [1, 5, 1, 7, 2, 1, 3, 3, 7])
`end`
**算术结果 = 运行(内存, 9, 1024)**
`end`
`end`
`end`
`end`
`if 错误 != ""`
`return 失败("vm.invalid_opcode", "未知指令")`
`else`
`if 栈指针 == 0`
`return 失败("vm.empty_stack", "没有结果")`
`end`
`end`
`return 成功(内存[栈基址 + 栈指针 - 1])`
## @exec main
`内存 = memory.new(1, 1)`
第一段程序计算 (5 + 7) * 3。
`写字节(内存, 0, [1, 5, 1, 7, 2, 1, 3, 3, 7])`
`算术结果 = 运行(内存, 9, 1024)`
第二段程序在条件为 0 时跳到偏移 8,压入 42 后停机。
> 写字节(内存, 0, [1, 0, 6, 8, 1, 99, 7, 0, 1, 42, 7])
`写字节(内存, 0, [1, 0, 6, 8, 1, 99, 7, 0, 1, 42, 7])`
**分支结果 = 运行(内存, 11, 2048)**
`分支结果 = 运行(内存, 11, 2048)`
*[算术结果, 分支结果]*
`return [算术结果, 分支结果]`