Files
marqdo-cli/tests/conformance/semantics/63-memory-bytecode-vm.mq.md
cflmy 92bfbf2568 feat(rfc-cm): baseline P0-P3 compute machine, stdlib and resource slices
Implements the first vertical slices of RFC-CM-0001/0002 on top of a113d89:

- P1: opaque Memory regions (64 KiB pages, budgets, grow-only), byte
  read/write, Memory-index place assignment, wire/host-boundary rejection.
- CM-A04/A05: value-semantics List/Map/Text/Table primitives and the
  recoverable Result map convention.
- CM-A06: opaque Resource handles, resource.open/read/write/close, stdout
  write, granted project-file read and buffered write, strict fail-closed.
- CM-A07 Compat-A: explicit .mq.md format/utf8/io chain matching builtin
  print for the covered value types; builtin remains default.
- P2 fixtures: allocators, list/tree/graph/hash, integer codecs, UTF-8
  encode/decode, CSV/JSON subsets, table filter/sort/join, bytecode VM.
- ADRs 0022-0027, spec updates, evidence reports and 68 semantic goldens.

Gate: cargo fmt --check, clippy -D warnings, 407 tests passed.
2026-11-20 21:16:54 +08:00

2.3 KiB
Raw Permalink Blame History

成功

(值)

{ok: true, value: 值}

失败

(代码, 消息)

{ok: false, code: 代码, message: 消息}

写字节

(内存, 基址, 字节)

下标 = 0

  • 下标 < 长度(字节)

    内存[基址 + 下标] = 字节[下标]

    下标 = 下标 + 1

None

运行

(内存, 程序长度, 栈基址)

程序从偏移 0 开始,栈从 栈基址 开始。指令集: 1 = PUSH 立即数,2 = ADD,3 = MUL,5 = JMP,6 = JZ,7 = HALT。

指令指针 = 0

栈指针 = 0

运行中 = true

错误 = ""

  • 指令指针 < 程序长度 and 运行中 and 错误 == ""

    操作码 = 内存[指令指针]

    1. 操作码 == 1

      内存[栈基址 + 栈指针] = 内存[指令指针 + 1]

      栈指针 = 栈指针 + 1

      指令指针 = 指令指针 + 2

    2. 操作码 == 2

      右 = 内存[栈基址 + 栈指针 - 1]

      左 = 内存[栈基址 + 栈指针 - 2]

      内存[栈基址 + 栈指针 - 2] = 左 + 右

      栈指针 = 栈指针 - 1

      指令指针 = 指令指针 + 1

    3. 操作码 == 3

      右 = 内存[栈基址 + 栈指针 - 1]

      左 = 内存[栈基址 + 栈指针 - 2]

      内存[栈基址 + 栈指针 - 2] = 左 * 右

      栈指针 = 栈指针 - 1

      指令指针 = 指令指针 + 1

    4. 操作码 == 5

      指令指针 = 内存[指令指针 + 1]

    5. 操作码 == 6

      条件 = 内存[栈基址 + 栈指针 - 1]

      栈指针 = 栈指针 - 1

      1. 条件 == 0

        指令指针 = 内存[指令指针 + 1]

      2. true

        指令指针 = 指令指针 + 2

    6. 操作码 == 7

      运行中 = false

    7. true

      错误 = "vm.invalid_opcode"

  1. 错误 != ""

    失败("vm.invalid_opcode", "未知指令")

  2. 栈指针 == 0

    失败("vm.empty_stack", "没有结果")

成功(内存[栈基址 + 栈指针 - 1])

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])

分支结果 = 运行(内存, 11, 2048)

[算术结果, 分支结果]