Files
marqdo-cli/tests/conformance/semantics/64-memory-integer-codecs.mq.md
T
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

119 lines
1.8 KiB
Markdown

## @exec 写大端
`params(内存: Memory, 基址: Int, 值: Int, 宽度: Int)`
从最低字节开始写,放到 基址 + 宽度 - 1 - 下标,因此最终是大端序。
调用方保证 值 能放进 宽度 字节。
`当前 = 值`
`下标 = 0`
`while 下标 < 宽度`
`内存[基址 + 宽度 - 1 - 下标] = 当前 % 256`
`当前 = 当前 // 256`
`下标 = 下标 + 1`
`end`
`return None`
## @exec 读大端
`params(内存: Memory, 基址: Int, 宽度: Int)`
`值 = 0`
`下标 = 0`
`while 下标 < 宽度`
`值 = 值 * 256 + 内存[基址 + 下标]`
`下标 = 下标 + 1`
`end`
`return 值`
## @exec 写变长
`params(内存: Memory, 基址: Int, 值: Int)`
LEB128:每字节低 7 位,最高位表示是否还有后续;返回写入字节数。
`当前 = 值`
`偏移 = 0`
`while 当前 >= 128`
`内存[基址 + 偏移] = 当前 % 128 + 128`
`当前 = 当前 // 128`
`偏移 = 偏移 + 1`
`end`
`内存[基址 + 偏移] = 当前`
`return 偏移 + 1`
## @exec 读变长
`params(内存: Memory, 基址: Int)`
返回 [值, 消耗字节数]。
`值 = 0`
`倍数 = 1`
`偏移 = 0`
`继续 = true`
`while 继续`
`字节 = 内存[基址 + 偏移]`
`值 = 值 + 字节 % 128 * 倍数`
`if 字节 < 128`
`继续 = false`
`else`
`倍数 = 倍数 * 128`
`偏移 = 偏移 + 1`
`end`
`end`
`return [值, 偏移 + 1]`
## @exec main
`内存 = memory.new(1, 1)`
`写大端(内存, 0, 4660, 2)`
`写大端(内存, 16, 305419896, 4)`
`写大端(内存, 32, 1311768467463790320, 8)`
`写大端(内存, 48, 18446744073709551615, 8)`
`变长长度 = 写变长(内存, 128, 123456789)`
`变长 = 读变长(内存, 128)`
`return [读大端(内存, 0, 2), 读大端(内存, 16, 4), 读大端(内存, 32, 8), 读大端(内存, 48, 8), 变长长度, 变长[0], 变长[1], 内存[48], 内存[55]]`