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

183 lines
2.6 KiB
Markdown

## @exec 写32
`params(内存: Memory, 基址: Int, 值: Int)`
`字节3 = 值 % 256`
`字节2 = 值 // 256 % 256`
`字节1 = 值 // 65536 % 256`
`字节0 = 值 // 16777216 % 256`
`内存[基址] = 字节0`
`内存[基址 + 1] = 字节1`
`内存[基址 + 2] = 字节2`
`内存[基址 + 3] = 字节3`
`return None`
## @exec 读32
`params(内存: Memory, 基址: Int)`
`return 内存[基址] * 16777216 + 内存[基址 + 1] * 65536 + 内存[基址 + 2] * 256 + 内存[基址 + 3]`
## @exec 成功
`params(值)`
`return {ok: true, value: 值}`
## @exec 失败
`params(代码, 消息)`
`return {ok: false, code: 代码, message: 消息}`
## @exec 初始化
`params(内存: Memory, 容量: Int)`
头部 8 字节:0..3 是元素数,4..7 是槽数。槽从偏移 8 开始,每槽 8 字节:
键 u32 + 值 u32。键 0 表示空槽,因此业务键必须从 1 开始。
`写32(内存, 0, 0)`
`写32(内存, 4, 容量)`
`return 成功(None)`
## @exec 插入
`params(内存: Memory, 键: Int, 值: Int)`
线性探测;键已存在时只更新值,不增加元素数。
`容量 = 读32(内存, 4)`
`位置 = 键 % 容量`
`尝试 = 0`
`完成 = false`
`while 尝试 < 容量 and not 完成`
`槽 = 8 + (位置 + 尝试) % 容量 * 8`
`槽键 = 读32(内存, 槽)`
`if 槽键 == 0`
`写32(内存, 槽, 键)`
`写32(内存, 槽 + 4, 值)`
`写32(内存, 0, 读32(内存, 0) + 1)`
`完成 = true`
`else`
`if 槽键 == 键`
`写32(内存, 槽 + 4, 值)`
`完成 = true`
`else`
`尝试 = 尝试 + 1`
`end`
`end`
`end`
`if 完成`
`return 成功(None)`
`else`
`return 失败("hash.full", "哈希表已满")`
`end`
## @exec 查找
`params(内存: Memory, 键: Int)`
遇到空槽即停止;找不到返回 None。
`容量 = 读32(内存, 4)`
`位置 = 键 % 容量`
`尝试 = 0`
`结果 = None`
`while 尝试 < 容量`
`槽 = 8 + (位置 + 尝试) % 容量 * 8`
`槽键 = 读32(内存, 槽)`
`if 槽键 == 键`
`结果 = 读32(内存, 槽 + 4)`
`尝试 = 容量`
`else`
`if 槽键 == 0`
`尝试 = 容量`
`else`
`尝试 = 尝试 + 1`
`end`
`end`
`end`
`return 结果`
## @exec main
业务键从 1 开始。37 与 5 在 32 个槽中同余,用来验证线性探测。
`内存 = memory.new(1, 1)`
`初始化(内存, 32)`
`插入5 = 插入(内存, 5, 50)`
`插入(内存, 37, 370)`
`插入(内存, 12, 120)`
`插入(内存, 5, 55)`
`内存2 = memory.new(1, 1)`
`初始化(内存2, 2)`
`插入(内存2, 1, 10)`
`插入(内存2, 2, 20)`
`表满 = 插入(内存2, 4, 40)`
`return [插入5, 查找(内存, 5), 查找(内存, 37), 查找(内存, 12), 查找(内存, 99), 读32(内存, 0), 表满, 查找(内存2, 1), 查找(内存2, 2)]`