Verify control-flow invariants and document the development contract
This commit is contained in:
1 parent
71d29261a8
commit
1dc088b602
22 files changed
+269
-80
No files matched your search
@@ -264,6 +264,21 @@ digests. The external lane ran from a temporary fixture checkout with Cargo
|
||||
absent from PATH. Archive SHA-256 is
|
||||
`8934bfc25c7b5526071eae6f24143ca3805ad3d7cc484626a4345b1deed430b2`.
|
||||
|
||||
## 0.8 Structured control flow — RFC-CTRL-0005 (unreleased)
|
||||
|
||||
[ADR-0067](adr/0067-structured-control-flow.md) covers the phase A implementation.
|
||||
An `else if` carrier adds a condition arm to the existing branch IR.
|
||||
`break` and `continue` transfer to the nearest loop in the same heading
|
||||
execution segment. Binding inference joins only reachable control exits.
|
||||
The [runnable example](../examples/control-flow.mq.md) uses ordinary Markdown
|
||||
list containers.
|
||||
|
||||
[Analysis v3](../spec/analysis-v3.md) represents the loop transfer statements.
|
||||
Other documents continue to emit v2. Trace v2 and Execution Events v1 expose
|
||||
the new control events. The published beta evidence applies to its original
|
||||
SHA. Maintainers decide when to include phase A in a release. List iteration
|
||||
is a follow-up design.
|
||||
|
||||
## 1. Historical: pre-beta remote baseline and evidence
|
||||
|
||||
> **Historical record.** Everything in §1 onward was executed before the
|
||||
|
||||
+2
-1
@@ -64,4 +64,5 @@ Files: `0001`–`0050`, plus:
|
||||
* [0065](0065-resource-control-vnext.md) — proposed manifest-driven stateful
|
||||
operations and child resources;
|
||||
* [0066](0066-structured-tasks-v1.md) — proposed task isolation, bounded
|
||||
communication and cancellation.
|
||||
communication and cancellation;
|
||||
* [0067](0067-structured-control-flow.md) — multi-arm branches and nearest-loop transfers.
|
||||
@@ -55,6 +55,13 @@ replace `spec/` or a recorded test log.
|
||||
> [Task v1](../adr/0066-structured-tasks-v1.md) designs are Proposed.
|
||||
> See [STATUS §0.7](../STATUS.md#07-post-beta-stable-core-review--rfc-gpl-0004-unreleased).
|
||||
|
||||
> **Structured control flow (RFC-CTRL-0005).**
|
||||
> [ADR-0067](../adr/0067-structured-control-flow.md) extends the payload parser
|
||||
> and branch mapper, adds nearest-loop execution transfers and makes binding
|
||||
> inference path-sensitive. Analysis chooses v2 or v3 according to the
|
||||
> statements it emits. The interpreter owns the transfers and reports them
|
||||
> through the existing observation facade.
|
||||
|
||||
## 1. Four state dimensions
|
||||
|
||||
A claim is stated in exactly one of these dimensions. "Exists in the tree" is not
|
||||
|
||||
@@ -526,8 +526,11 @@ import:
|
||||
| 关键字 | 语法 | 含义 | 是否新增计算原语 |
|
||||
|---|---|---|---|
|
||||
| `if` | `if <Bool expression>` | 开始条件分支 | 否,映射已有 `Branch` |
|
||||
| `else if` | `else if <Bool expression>` | 添加同一分支组的条件臂 | 否,复用 `Branch` |
|
||||
| `else` | `else` | 切换至当前 if 的否则分支 | 否 |
|
||||
| `while` | `while <Bool expression>` | 开始循环 | 否,映射已有 `Loop` |
|
||||
| `break` | `break` | 退出最近一层循环 | 否,结构化控制转移 |
|
||||
| `continue` | `continue` | 重新检查最近一层循环的条件 | 否,结构化控制转移 |
|
||||
| `end` | `end` | 结束最近未关闭的 if/while | 否,仅用于编译期配对 |
|
||||
| `return` | `return <expression>` | 返回当前 H2 函数,终止后续执行 | 否,映射已有 `Return` |
|
||||
| `params` | `params(name: Type, ...)` | H2 Exec 函数的可选参数声明 | 否,编译期元数据 |
|
||||
@@ -823,14 +826,23 @@ TokenStream = [StatementToken(payload, span, carrier_kind, owner_unit), ...]
|
||||
|
||||
```text
|
||||
open_if(condition):
|
||||
push IfFrame(cond, then=[], else=None)
|
||||
push IfFrame(arms=[(condition, [])], has_else=false)
|
||||
|
||||
on_statement(stmt):
|
||||
append stmt to the current innermost active body
|
||||
|
||||
on_else_if(condition):
|
||||
require stack.top == IfFrame and not frame.has_else
|
||||
append and activate a new conditional arm
|
||||
|
||||
on_else:
|
||||
require stack.top == IfFrame and frame.else is None
|
||||
switch frame active branch from then to else
|
||||
require stack.top == IfFrame and not frame.has_else
|
||||
append and activate the final arm
|
||||
set frame.has_else
|
||||
|
||||
on_break_or_continue:
|
||||
require an open WhileFrame in the current segment
|
||||
append the transfer to the current innermost active body
|
||||
|
||||
open_while(condition):
|
||||
push WhileFrame(cond, body=[])
|
||||
@@ -848,10 +860,10 @@ at_segment_end:
|
||||
|
||||
该算法可以保证:
|
||||
|
||||
- `else` 配对最近尚未结束、且位于栈顶的 `if`;不得跳过未关闭的 `while` 寻找更外层 `if`。
|
||||
- `else if` 和 `else` 配对栈顶的 `if`,且只能出现在末尾 `else` 之前;不得跳过未关闭的 `while` 寻找更外层 `if`。
|
||||
- `end` 只关闭栈顶最近的一层 if/while,没有匹配结构时报错。
|
||||
- `return` 属于正在打开的 body,但运行时终止整个 H2 函数;降为现有 `Stmt::Return` IR。
|
||||
- `else` 不会成为单独一条 Runtime 指令;它只是 IR `Branch` 的分支边界。
|
||||
- `else if` 和 `else` 是 `Branch` 的臂边界。解释器将 `break` 和 `continue` 传递到最近的循环。
|
||||
- 相同载荷序列应产生相同的**归一化可执行 IR**,不因列表编号、引用层数、加粗/斜体/行内代码形态改变运算类别。
|
||||
|
||||
**一个实现注意点**:现有 `Branch` IR 的每个 arm 带 `condition`,没有专用可选 else;首版可以将兜底 else 降为最后一个 `true` arm,并明确保持既有“从左到右找第一匹配”的短路求值。如果未来 IR 引入更明确的 `else_body`,必须同步更改 IR schema、Trace 规范和**全部下游消费者及测试**;不能在本轮暗改行为。
|
||||
@@ -975,6 +987,10 @@ R(IR, imports) = ExecutableProgram or Diagnostics
|
||||
| `## @exec` | 缺少标题名称 | `sem.empty_unit_name` |
|
||||
| `**if x >**` | 语法错误,不降级文档 | `sem.syntax` |
|
||||
| `**else**`(无 if) | 无匹配分支 | `sem.unmatched_else` |
|
||||
| `**else if true**`(无 if) | 无匹配分支 | `sem.unmatched_else_if` |
|
||||
| `**else if true**`(位于末尾 else 后) | 条件臂位置非法 | `sem.else_if_after_else` |
|
||||
| `**break**`(循环外) | 无匹配循环 | `sem.break_outside_loop` |
|
||||
| `**continue**`(循环外) | 无匹配循环 | `sem.continue_outside_loop` |
|
||||
| `**else**`(重复 else) | 重复,否则分支只允许一个 | `sem.duplicate_else` |
|
||||
| `**end**`(无 if/while) | 不能独立结束 | `sem.unmatched_end` |
|
||||
| `**if true**` 到片段结束无 end | 未闭合控制 | `sem.unclosed_control` |
|
||||
|
||||
@@ -49,6 +49,7 @@ marqdo-cli/
|
||||
│ ├── foreign.md Python/Node kernel protocol
|
||||
│ ├── tool-v1.md generic external Tool Protocol v1
|
||||
│ ├── analysis-v2.md public Analysis v2 projection (v1 historical)
|
||||
│ ├── analysis-v3.md projection for projects with loop transfers
|
||||
│ ├── trace-v2.md public Trace v2 projection (v1 historical)
|
||||
│ └── trace-v1.md public Trace v1 event stream
|
||||
├── crates/
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
# Analysis v2 and Trace v2 consumers
|
||||
# Analysis and Trace v2 consumers
|
||||
|
||||
Both are public, versioned JSON artifacts. A consumer must read them as files,
|
||||
never link Marqdo's Rust internals or depend on `inspect --ir`.
|
||||
|
||||
## Analysis v2 (static)
|
||||
## Analysis v2 and v3 (static)
|
||||
|
||||
```bash
|
||||
marqdo check index.mq.md --emit ./analysis.json
|
||||
@@ -13,7 +13,10 @@ marqdo check index.mq.md --emit ./analysis.json
|
||||
independent Markdown-link check and can make `check` fail; on failure the emit
|
||||
target is removed. Do not generate a consumer artifact with
|
||||
`check --refs --emit` without checking the exit code.
|
||||
- `schema: "marqdo.analysis"`, `schema_version: 2`.
|
||||
- `schema: "marqdo.analysis"`. The writer emits `schema_version: 2` for
|
||||
projects without loop transfers and `3` when a function body contains
|
||||
`break` or `continue`. Check the version before walking function bodies.
|
||||
Both versions encode `else if` in the ordered `branch.arms` array.
|
||||
- Top level: `producer` (`name`, `version` = product version,
|
||||
`markdown_profile`), `project`, `modules`, `diagnostics`.
|
||||
- `project`: `root_id` (`root:<name>@<version>` for manifest projects),
|
||||
@@ -26,9 +29,10 @@ marqdo check index.mq.md --emit ./analysis.json
|
||||
instance ids, never absolute paths.
|
||||
- `check` does not load plugin libraries, start kernels/providers or execute the
|
||||
program.
|
||||
- Schemas: `spec/schemas/analysis-v2.schema.json`; contract:
|
||||
`spec/analysis-v2.md`; consumers: `tests/consumers/analysis_consumer.py`,
|
||||
`tests/consumers/validate_protocols.py`.
|
||||
- Contracts: [Analysis v2](../../spec/analysis-v2.md) and
|
||||
[Analysis v3](../../spec/analysis-v3.md). Each links to its schema.
|
||||
The independent consumers in `tests/consumers/` check the version and
|
||||
decode the public nodes.
|
||||
|
||||
View must render the **original Markdown** and use Analysis only to correlate
|
||||
source offsets/heading units/callable mapping. Knowledge must use heading
|
||||
@@ -50,7 +54,8 @@ marqdo run index.mq.md --trace-file ./trace.json
|
||||
stale; do not bind it to the new source. `run --trace-file` does not emit
|
||||
Analysis itself.
|
||||
- Event kinds: `call.enter`, `call.exit`, `branch.eval`, `loop.iteration`,
|
||||
`return`, `effect`, and possibly `trace.truncated`. Sourced events carry a
|
||||
`control.break`, `control.continue`, `return`, `effect` and possibly
|
||||
`trace.truncated`. Sourced events carry a
|
||||
module id and a UTF-8 span.
|
||||
- Defaults are 100,000 events / 32 MiB; `--trace-max-events` and
|
||||
`--trace-max-bytes` adjust them (`0` = unlimited). A consumer must tolerate
|
||||
@@ -80,10 +85,11 @@ marqdo run index.mq.md --event-stream jsonl --trace-file ./trace.json
|
||||
a complete stream. An EOF without a terminal event is `incomplete`
|
||||
(SIGKILL/crash/broken pipe); never synthesize success.
|
||||
- `run.started.source` contains `root_id` and the `sha256:` digest of the exact
|
||||
Analysis v2 bytes. `--trace-file` may be combined; both writers share one
|
||||
Analysis bytes. `--trace-file` may be combined; both writers share one
|
||||
opaque run id, and Trace v2 remains atomic and value-free.
|
||||
- Structural events: `call.enter`/`call.exit` (`frame_id`, `parent_frame_id`),
|
||||
`return`, `branch.eval`, `loop.iteration`, `effect`. Frames identify call
|
||||
`return`, `branch.eval`, `loop.iteration`, `control.break`,
|
||||
`control.continue` and `effect`. Frames identify call
|
||||
instances, not functions; a failed argument binding creates no frame.
|
||||
- Values are off by default (`value_state: not_requested`).
|
||||
`--observe-variable NAME` and `--observe-parameter NAME` are exact-name
|
||||
|
||||
@@ -58,9 +58,15 @@ stdout and diagnostics.
|
||||
documentation. Unmarked headings inherit the nearest ancestor mode.
|
||||
- `**strong**`, `*emph*` and inline `` `code` `` are interchangeable carriers of
|
||||
one statement each.
|
||||
- Control flow uses keywords: `if`/`else`/`while`/`end`, plus `params(...)` and
|
||||
`return`.
|
||||
- Lists, block quotes and paragraphs are presentation only.
|
||||
- A branch has an `if`, any number of `else if` arms and an optional
|
||||
final `else`, followed by one `end`. Each condition is evaluated in order.
|
||||
- A `while` loop ends at `end`. Inside its body, `break` exits the nearest
|
||||
loop and `continue` skips to its next condition check. `return` exits
|
||||
the function. See the [runnable example](../../examples/control-flow.mq.md).
|
||||
- Lists, block quotes and paragraphs organize the source view. For review or
|
||||
linting, put each branch head at the start of an ordered list item and
|
||||
place the loop head at the start of a bullet item. Nest the body visually
|
||||
and close each group with its own `end` carrier.
|
||||
- The normative contract is
|
||||
[`../architecture/RFC-LANG-0002-unified-semantics.md`](../architecture/RFC-LANG-0002-unified-semantics.md),
|
||||
[`../../spec/mapping.md`](../../spec/mapping.md) and
|
||||
|
||||
@@ -44,7 +44,8 @@ marqdo-view/
|
||||
rejects a mismatched range at install and dispatch time with a
|
||||
`requires marqdo …` diagnostic.
|
||||
- `analysis`/`trace` are declared only if the tool consumes them; the current
|
||||
core provides Analysis v2 and Trace v2.
|
||||
development branch provides Analysis v2/v3 and Trace v2. Declare
|
||||
`analysis: [2, 3]` when the reader handles both versions.
|
||||
- `command` must match `^[a-z][a-z0-9-]*$` and must not collide with a built-in
|
||||
command (`init`, `run`, `call`, `check`, `inspect`, `fmt`, `plugin`, `add`,
|
||||
`remove`, `lock`, `sync`, `tool`, `help`).
|
||||
@@ -72,7 +73,7 @@ The tool receives:
|
||||
|---|---|
|
||||
| `MARQDO_BIN` | absolute path of the running `marqdo` executable |
|
||||
| `MARQDO_TOOL_PROTOCOL` | `1` |
|
||||
| `MARQDO_PROTOCOL_ANALYSIS` | `2` when Analysis v2 was negotiated |
|
||||
| `MARQDO_PROTOCOL_ANALYSIS` | Highest shared Analysis version. Read the artifact envelope for its actual version. |
|
||||
| `MARQDO_PROTOCOL_TRACE` | `2` when Trace v2 was negotiated |
|
||||
|
||||
Call `$MARQDO_BIN` with an argv array; do not assume `marqdo` is on `PATH`.
|
||||
|
||||
Reference in new issue
Block a user