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).
165 lines
2.3 KiB
Markdown
165 lines
2.3 KiB
Markdown
## @exec 成功
|
||
|
||
`params(值)`
|
||
|
||
`return {ok: true, value: 值}`
|
||
|
||
## @exec 失败
|
||
|
||
`params(代码, 消息)`
|
||
|
||
`return {ok: false, code: 代码, message: 消息}`
|
||
|
||
## @exec 写字节
|
||
|
||
`params(内存: Memory, 基址: Int, 字节: List)`
|
||
|
||
`下标 = 0`
|
||
|
||
`while 下标 < 长度(字节)`
|
||
|
||
`内存[基址 + 下标] = 字节[下标]`
|
||
|
||
`下标 = 下标 + 1`
|
||
|
||
`end`
|
||
|
||
`return None`
|
||
|
||
## @exec 运行
|
||
|
||
`params(内存: Memory, 程序长度: Int, 栈基址: Int)`
|
||
|
||
程序从偏移 0 开始,栈从 栈基址 开始。指令集:
|
||
1 = PUSH 立即数,2 = ADD,3 = MUL,5 = JMP,6 = JZ,7 = HALT。
|
||
|
||
`指令指针 = 0`
|
||
|
||
`栈指针 = 0`
|
||
|
||
`运行中 = true`
|
||
|
||
`错误 = ""`
|
||
|
||
`while 指令指针 < 程序长度 and 运行中 and 错误 == ""`
|
||
|
||
`操作码 = 内存[指令指针]`
|
||
|
||
`if 操作码 == 1`
|
||
|
||
`内存[栈基址 + 栈指针] = 内存[指令指针 + 1]`
|
||
|
||
`栈指针 = 栈指针 + 1`
|
||
|
||
`指令指针 = 指令指针 + 2`
|
||
|
||
`else`
|
||
|
||
`if 操作码 == 2`
|
||
|
||
`右 = 内存[栈基址 + 栈指针 - 1]`
|
||
|
||
`左 = 内存[栈基址 + 栈指针 - 2]`
|
||
|
||
`内存[栈基址 + 栈指针 - 2] = 左 + 右`
|
||
|
||
`栈指针 = 栈指针 - 1`
|
||
|
||
`指令指针 = 指令指针 + 1`
|
||
|
||
`else`
|
||
|
||
`if 操作码 == 3`
|
||
|
||
`右 = 内存[栈基址 + 栈指针 - 1]`
|
||
|
||
`左 = 内存[栈基址 + 栈指针 - 2]`
|
||
|
||
`内存[栈基址 + 栈指针 - 2] = 左 * 右`
|
||
|
||
`栈指针 = 栈指针 - 1`
|
||
|
||
`指令指针 = 指令指针 + 1`
|
||
|
||
`else`
|
||
|
||
`if 操作码 == 5`
|
||
|
||
`指令指针 = 内存[指令指针 + 1]`
|
||
|
||
`else`
|
||
|
||
`if 操作码 == 6`
|
||
|
||
`条件 = 内存[栈基址 + 栈指针 - 1]`
|
||
|
||
`栈指针 = 栈指针 - 1`
|
||
|
||
`if 条件 == 0`
|
||
|
||
`指令指针 = 内存[指令指针 + 1]`
|
||
|
||
`else`
|
||
|
||
`指令指针 = 指令指针 + 2`
|
||
|
||
`end`
|
||
|
||
`else`
|
||
|
||
`if 操作码 == 7`
|
||
|
||
`运行中 = false`
|
||
|
||
`else`
|
||
|
||
`错误 = "vm.invalid_opcode"`
|
||
|
||
`end`
|
||
|
||
`end`
|
||
|
||
`end`
|
||
|
||
`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])`
|
||
|
||
`分支结果 = 运行(内存, 11, 2048)`
|
||
|
||
`return [算术结果, 分支结果]`
|