docs(spec): adopt RFC-LANG-0002 unified language semantics

Replace the Strong/Emph/List/Quote mapping in place with the single
@exec/@knowledge heading-mode model, unified payload carriers and keyword
control flow.

- add the authoritative RFC, ADR-0052 and a historical ADR index
- rewrite spec/mapping.md and spec/expression.ebnf as the normative
  mapping/statement grammar; update evaluation, references, math,
  foreign, package-format, plugin-abi, security and schema notes
- rewrite README, AGENTS, COMPATIBILITY and CHANGELOG for the breaking
  language change; update the architecture index/overview and active docs
- mark superseded planning docs, compute RFCs, evidence and syntax-role
  ADRs as historical/superseded without rewriting their decisions
- add migration guide, per-file inventory and dev-only audit scripts

The frontend/stdlib migration is completed in the following commits; this
commit contains documentation only.
This commit is contained in:
cflmy committed 2026-11-21 12:24:19 +08:00
1 parent 0d8ed44010
commit 49835424ee
47 files changed
+4941 -603

No files matched your search

+6 -1
View File
@@ -160,7 +160,7 @@ Diagnostics no longer embed absolute physical paths:
"line": 2,
"column": 3,
"severity": "warning",
"code": "markdown.inert_executable",
"code": "sem.inferred_signature",
"message": "...",
"hints": []
}
@@ -170,6 +170,11 @@ Diagnostics no longer embed absolute physical paths:
the owning module. `span`, `line` or `column` may be `null` when no precise
location exists.
The `code` value is an open, stable string: the current language revision adds
the `sem.*` family (`spec/mapping.md` §8) and removes the old
`markdown.inert_executable` warning. This is a diagnostic-value change, not an
Analysis schema change; `schema_version` stays 2.
## 6. Compatibility
* Writers emit `schema_version: 2`. There is no v1 writer and no
+68 -16
View File
@@ -1,23 +1,71 @@
(* Marqdo Expression Grammar v1
Status: frozen for M0.5, extended by ADR-0022 with Memory-index assignment
targets. This is the only grammar for executable payloads.
It is a small independent language: it is NOT Markdown and NOT translated
to Python. Markdown AST decides where a payload exists; this grammar
decides what it means.
(* Marqdo Statement and Expression Grammar
Status: current language contract, breaking rewrite (RFC-LANG-0002).
This replaces the earlier v1 expression-only grammar in place.
Terminals are described in prose where Unicode categories matter.
There is one grammar for every executable payload. Markdown AST decides
which closed carrier (Strong / Emph / inline Code) is a payload and where
it lives; this grammar decides what that payload means. No second parser
and no per-file syntax selector exists.
Implementation state: the Rust expression parser still implements the
previous, control-free grammar; this file is the target contract. See
docs/STATUS.md.
*)
program = [ statement ] , EOF ;
(* One payload is exactly one statement and must consume the whole payload. *)
payload = statement , EOF ;
statement = assignment | expression ;
statement = if_stmt
| else_stmt
| while_stmt
| end_stmt
| return_stmt
| params_stmt
| assignment
| expression ;
if_stmt = "if" , space_required , expression ;
else_stmt = "else" ;
while_stmt = "while" , space_required , expression ;
end_stmt = "end" ;
return_stmt = "return" , space_required , expression ;
params_stmt = "params" , "(" , [ parameter_list ] , ")" ;
(* `if`, `else`, `while`, `end`, `return` and `params` are reserved statement
words only when they are the complete first token of a payload.
`ifdef(x)` and `returning(x)` are ordinary identifiers; a bare `if` used as
an identifier is a reserved-word error. `else`/`end` take no additional
tokens: `else x` and `end if` are syntax errors. No trailing colon:
`if x > 0:` is a syntax error. No `elif`, `for`, `break`, `continue`,
`try`, empty `return`, `end if`, or semicolon-separated multi-statement
payload exists in this revision.
Control pairing is not part of this grammar; it is a separate compile-time
stack in the frontend:
- every `if` has exactly one closing `end`;
- `else` is optional and at most one per `if`;
- every `while` has exactly one closing `end`;
- `else` must bind the top-of-stack `if`; `end` closes the top frame;
- a frame must close inside the same heading's direct body.
*)
(* Function parameter declaration. It is legal only as the first direct
execution payload of an Exec H2 function, at most once, and never inside a
list or block quote. The declaration is compile-time metadata, not a
runtime call. *)
parameter_list = parameter , { "," , parameter } , [ "," ] ;
parameter = identifier , ":" , type_name , [ "=" , expression ] ;
type_name = identifier ;
(* Parameter names are unique; positional parameters precede defaulted ones.
Types and default expressions reuse the existing signature rules. *)
(* Assignment and place writes are unchanged from the previous revision. *)
assignment = assign_target , "=" , expression ;
assign_target = identifier
| postfix_expr , "[" , expression , "]" ;
(* v1 left-hand sides are a local identifier or one index place. The index
target writes one byte into an addressable Memory region; other base values
are rejected at run time. Member assignment (`a.b = 1`) is a parse error.
Chained assignment is a parse error. See ADR-0022. *)
(* The index target writes one byte into an addressable Memory region; other
base values are rejected at run time. Member assignment (`a.b = 1`) and
chained assignment are parse errors. See ADR-0022. *)
expression = or_expr ;
or_expr = and_expr , { "or" , and_expr } ;
@@ -60,8 +108,9 @@ record_entry = ( identifier | text_literal ) , ":" , expression ;
identifier = xid_start , { xid_continue } ;
(* Unicode XID per UAX #31: Chinese, Latin, etc. are valid.
Keywords are reserved and cannot be identifiers:
true, false, None, and, or, not. *)
Reserved words cannot be identifiers:
true, false, None, and, or, not,
if, else, while, end, return, params. *)
int_literal = digit , { digit } ;
(* arbitrary precision decimal integer; no sign here (unary
@@ -77,7 +126,8 @@ escape = "\\" , ( '"' | "\\" | "n" | "t" | "r" | "u{" , hexdigit , { hex
text_char = ? any Unicode scalar value except '"' and "\\" ? ;
(* Whitespace: any Unicode whitespace separates tokens. Newlines inside a
code-span payload are whitespace. There are no comments in v1. *)
code-span payload are whitespace. There are no comments. `space_required`
means at least one whitespace character after the keyword. *)
(* Notes on the Markdown boundary *)
(* - A "**" inside a payload is the power operator only when it survived
@@ -88,3 +138,5 @@ text_char = ? any Unicode scalar value except '"' and "\\" ? ;
payload; the frontend maps them back through the text/code segments to
file spans. *)
(* - Maximum nesting depth is 128; exceeding it is an expr.parse error. *)
(* - Every payload must be fully consumed. A trailing token is a syntax error,
never an ignored suffix. *)
+7 -5
View File
@@ -21,7 +21,9 @@ Access is by `ref("#heading")` (exactly one code/formula resource in that
heading range; `ref.ambiguous` otherwise — see `spec/references.md` §5):
````markdown
## Python 校验
# @knowledge 示例
## @knowledge Python 校验
```python
import json, math, sys
@@ -30,11 +32,11 @@ r = int(envelope["input"]["entries"]["r"]["decimal"])
print(json.dumps({"type": "float", "value": math.pi * r ** 2}))
```
## main
## @exec main
**结果 = foreign.call(ref("#python-校验"), {"r": 3})**
> 打印(结果)
`结果 = foreign.call(ref("#python-校验"), {"r": 3})`
`print(结果)`
`return 结果`
````
The anchor is the GitHub-style slug of the heading (`#python-校验`), not the
+355 -208
View File
@@ -1,255 +1,402 @@
# Marqdo AST → Program Mapping v1
# Marqdo AST → Program Mapping
Status: **frozen for M0.5**. This file is the normative mapping from Markdown
AST structure to program meaning. It is derived directly from
`docs/DEVELOPMENT_BLUEPRINT.md` sections 1.2–1.4 and 3.2.
Status: **current language contract, breaking rewrite (RFC-LANG-0002)**. This
file replaces the earlier Strong/Emph/List/Quote mapping in place; there is no
second active mapping and no per-file syntax selector.
## 1. The two rules that override everything
> **Implementation state:** this document is normative for the target language,
> but the Rust frontend in this checkout still implements the pre-RFC mapping.
> Tests that exercise executable Marqdo source are therefore expected to be red
> until the frontend migration lands. See `docs/STATUS.md` and
> `docs/migration/semantic-rewrite-inventory.tsv`. Do not read this document as
> evidence that the CLI already supports the syntax.
1. **Node type and structural position decide the role.** A `strong` node in an
executable position is an execution statement and its payload must parse as
a `Statement`. It is never "prose that happens to look like code".
2. **Invalid executable shapes are hard errors.** If a node occupies an
executable role but its shape is invalid, compilation fails with a
`markdown.invalid_construct` diagnostic. There is no fallback to prose.
Authority: `docs/architecture/RFC-LANG-0002-unified-semantics.md`.
Markdown parsing remains `marqdo.md.v1` (`spec/markdown-profile.md`); only the
Markdown→program mapping changes.
Prose never causes an action. Natural-language words (“if”, “return”, “若”,
“执行”) are never interpreted as keywords.
## 0. The mapping in one page
## 2. Document regions and heading scopes
1. **Headings declare the semantic mode.** `@exec` and `@knowledge` may prefix
any H1–H6 heading. Unmarked headings inherit the nearest ancestor heading's
mode. The document root is Knowledge.
2. **Only an Exec H2 is a callable function.** H1 is a namespace/top-level
unit, H2 Exec is a function, H2 Knowledge is an inert knowledge unit, and
H3–H6 are sections that never create callable functions.
3. **Closed carriers carry one statement each.** In an Exec scope, `Strong`,
`Emph` and inline `Code` are interchangeable carriers of one payload. They
no longer mean execute/return/lazy respectively.
4. **Keywords inside the payload decide the operation.** `if`, `else`, `while`,
`end`, `return`, `params(...)`, assignment and expression statements. Lists,
block quotes and paragraphs are document containers only.
5. **Errors never fall back to prose.** Once a payload is extracted from an
Exec scope it must parse fully; a failure is a diagnostic.
6. **Control blocks never cross a heading segment.** Every `if`/`while` must be
closed by `end` inside the same heading's direct body. A `return` unwinds
the whole H2 function.
A document root is a module. Heading nodes create addressable scopes:
Two rules override everything else:
| AST | Role | Requirements |
* **Node type and structural position decide whether a node is a payload
candidate**, never the success or failure of parsing and never natural
language.
* **Invalid payloads are hard errors**, never "ordinary Markdown".
## 1. Document regions and heading modes
### 1.1 Declaring a mode
A heading mode declaration is recognized only when it is the exact prefix of
the heading's **first plain Text child**, followed by whitespace and a non-empty
heading name:
```ebnf
heading_mode_prefix = "@exec" | "@knowledge" ;
heading_decl = heading_mode_prefix , whitespace , nonempty_title ;
```
The two legal words are case-sensitive and complete. The rules:
| Source | Result |
|---|---|
| `## @exec main` | Exec heading, semantic name `main` |
| `## @knowledge 原理` | Knowledge heading, semantic name `原理` |
| `## main` | no declaration; inherit |
| `## 关于 @exec 的讨论` | no declaration; the `@exec` is ordinary title text |
| `## @Exec main` / `## @knowledg main` | `sem.invalid_mode_marker` (no case/spelling repair) |
| `## @exec` | `sem.empty_unit_name` |
| `## @exec @knowledge main` | conflicting declarations; hard error |
| `## **@exec** main` | not a declaration; `@exec` is not the first plain Text child |
| prose containing `@exec` | ordinary text; never changes mode |
The heading's **display text** is unchanged for Markdown rendering. Its
**semantic name** has the declaration (and the whitespace after it) removed,
then applies the existing NFC/trim/whitespace-collapse normalization. All
identity, duplicate checks, function registration and `ref` lookup use the
semantic name; `@exec`/`@knowledge` are never part of a callable name.
ATX headings are the only accepted declaration form. A Setext heading has no
prefix declaration in the first version and simply inherits its mode; do not
invent an implicit declaration for it.
### 1.2 Inheritance
Using the Markdown heading stack (nearest lower-level ancestor heading, by
source order):
```text
Mode(document root) = Knowledge
Mode(H) = Explicit(H) if H has a valid declaration
Mode(H) = Mode(nearest ancestor) if H has no declaration and has an ancestor
Mode(H) = Knowledge if H has no declaration and no ancestor
```
Inheritance is a structural calculation over heading levels, not inference from
prose. A heading's scope ends at the next heading of the same or higher level
(same or smaller number); deeper headings are inside it. Siblings never share a
mode: an unmarked heading after a sibling that explicitly switched to Knowledge
still inherits from the nearest **ancestor**, not from the sibling.
Skipped levels (H1 directly followed by H3) still use the nearest valid
ancestor heading.
### 1.3 Heading roles
| Heading | Mode | Language object | Callable? | Effect |
|---|---|---|---|---|
| H1 | Knowledge/Exec | Namespace or top-level unit | No | Sets the default mode for descendants |
| H2 | Exec | Function + Unit | **Yes** | Opens the function body |
| H2 | Knowledge | Knowledge unit | No | Document section; no function |
| H3–H6 | Exec | Section of the owning H2 Exec function | No | Appends an execution segment to that function |
| H3–H6 | Knowledge | Section (knowledge or under a function) | No | Retained as document; no statements |
H3–H6 never create functions, never export callables and never create a new
call frame. An Exec payload under an H1 without an H2 Exec function, or under a
Knowledge H2, is `sem.exec_without_function`; it is never promoted to a function
and never executed as module-level code.
### 1.4 Version selection
There is no syntax-version switch. Every `.mq.md` file, imported module,
dependency, fixture and stdlib module uses this mapping. Introducing
`marqdo_semantics`, `syntax: v1`, `--legacy`, `--syntax` or any per-file mode
selection is an error (`sem.unsupported_version_directive` where the old
directive is seen); no implicit downgrade to an earlier mapper is permitted.
## 2. Inert regions
The following are never searched for payloads, even inside an Exec scope:
| Region | Treatment |
|---|---|
| every `@knowledge` subtree | zero executable statements |
| heading text (including a mode declaration) | unit identity only |
| link labels and URLs, image alt text | document/link data |
| GFM table cells | table data |
| math / math blocks | inert `Formula` resources |
| HTML blocks/inline HTML | document data |
| fenced and indented code blocks | inert `Code` resources |
| front matter | module configuration |
| strikethrough and other non-MVP extensions | inert document data |
Inline `Code` (a code span) is a payload carrier in Exec scope. A fenced code
block is a different AST node and stays inert; the two must not be conflated.
## 3. Executable payload carriers
### 3.1 Accepted carriers
Only these Comrak AST nodes are payload carriers in the first version:
| AST kind | Payload |
|---|---|
| `Strong` | concatenation of its **direct** Text and inline Code children, in source order |
| `Emph` | same as `Strong` |
| inline `Code` | the code span literal |
They are **interchangeable carriers of one statement**. For the same Exec
scope, owner function and payload text:
```text
Parse(Strong("return n")) == Parse(Emph("return n")) == Parse(Code("return n"))
```
The original AST kind is still recorded for rendering and source spans. Format
does not select the operation.
### 3.2 Extraction rules
1. Traverse an Exec heading's direct body in Markdown AST document order, and
recurse only through the allowed containers `Paragraph`, `List`, `ListItem`
and `BlockQuote`.
2. Extract only complete outer carrier nodes. A nested `Code` inside a
`Strong`/`Emph` is consumed by the outer carrier and must not execute again.
3. For `Strong`/`Emph`, only direct Text and inline Code children are allowed.
Any nested `Strong`, `Emph`, `Link`, `Image`, math, line break or other
complex child is `sem.payload_nested`. Never extract a partial statement or
silently concatenate multiple fragments.
4. A payload must be fully consumed by exactly one statement. `x = 1; return x`
in one carrier is `sem.syntax`; write two carriers instead.
5. Empty carriers in Exec scope are `sem.empty_payload`; they are never ignored.
6. Every payload keeps its original UTF-8 file span. For `Strong(Code(...))`
record the segment mapping so diagnostics point into the inner text, not at
the emphasis delimiters.
7. Two adjacent carriers produce two statements in source order. One carrier is
never split into multiple statements by newlines or semicolons.
### 3.3 Containers
Containers provide order and layout, not operation type:
| Container | Compiler behavior | Not allowed |
|---|---|---|
| `document` | module | exactly one root |
| `heading(level=1)` | namespace / object domain | non-empty canonical name |
| `heading(level=2)` | function | non-empty canonical name, unique in its scope |
| `heading(level>=3)` | section (source location) | does not create a function |
| `Paragraph` | extract permitted carriers in source order | prose does not execute |
| ordered `List` | recurse through items in source order | no implicit `Branch` |
| bullet `List` | recurse through items in source order | no implicit `Loop` |
| `BlockQuote` | recurse through nested blocks in source order | no implicit `Call` |
| `Heading` | switches/extends mode and segments | not a statement |
Scope rules:
A list's `ordered`/`start`/`tight` attributes, an item's checkbox, and quote
nesting depth never affect control flow. A plain ordered or bullet list with no
carriers has an empty execution projection (zero statements), which is not the
same as producing a `List([])` value.
* The range of a heading extends until the next heading of the same or a
lower level (i.e. same or smaller number), or the end of the document.
* A level-2 heading without a preceding level-1 heading belongs to the module
root. Nested headings inside a section remain part of the enclosing function.
* Headings inside container blocks (block quotes, list items) or inside fenced
code blocks never create functions.
* A function's canonical name is the normalised text of its heading children:
NFC, trimmed, internal whitespace collapsed to one space. Empty names are
errors. Duplicate canonical names in the same scope are errors. GitHub-style
anchor slugs are derived for navigation but are **not** the identity.
* `main` at module root is the default entry point for `marqdo run`; an
imported module's `main` is not run automatically.
### 3.4 The deliberate emphasis cost
## 3. Lazy (inert) regions
In an Exec scope, `**重要**` is a payload and is parsed as the expression
`重要`. If no such symbol exists, that is `symbol.unknown`. The language does
not guess that the author wanted emphasis. Put purely visual emphasis in a
`@knowledge` section (or another inert region). This is a deliberate
determinism trade-off, not an implementation gap.
The following nodes are inert regardless of where they appear. Their inline
content is never executed:
## 4. Statements
* heading text (headings scope, they do not run)
* `link` labels and `image` alt text
* `table` cells (tables are document values)
* `strikethrough` wrappers: their content is inert prose and is not traversed
for execution; do not rely on formatting to hide executable nodes
* `code_block` and inline `code` outside designated payload positions
* `math` / `math_block`
* `html_block` / `html_inline`
* front matter
* text and other inline nodes inside prose paragraphs
Each extracted payload is exactly one statement, defined by
`spec/expression.ebnf`:
Inline formatting inside those regions (e.g. `strong` inside a heading) does
not execute. The implementation must use a context-aware visitor, not a blind
whole-tree walk.
```text
if <expr> else while <expr> end
return <expr> params(...) assignment expression
```
## 4. Executable roles in function bodies
Lexical details:
Inside a function (`heading(level=2)` range) the following roles apply,
including inside branch and loop bodies:
* Keywords are recognized only as complete first tokens: `ifdef(...)` and
`returning` are ordinary identifiers; a bare `if`/`else` used as an
identifier is a reserved-keyword error.
* `if`, `while` and `return` require a non-empty expression that consumes the
whole payload (to EOF). Trailing characters are errors.
* `else` and `end` accept no additional tokens; `else x` and `end if` are
`sem.syntax`.
* No trailing colon: `if x > 0:` is `sem.syntax`. No `elif`, `for`, `break`,
`continue`, `try`, `end if` or semicolon-separated statements in this
version.
* `return` always requires an expression; falling off the end of a function
still returns `Unit`. Explicit `return None` is available when `None` is the
intended value.
* `params(...)` is a compile-time signature declaration, not a runtime
callable; see §6.
* Existing expression precedence, literals, calls, indexing, assignment and
`Memory` place writes are unchanged. Conditions must evaluate to `Bool` and
keep the existing purity rule.
| AST node | Role | Shape requirement | On invalid shape |
|---|---|---|---|
| `strong` | execution statement | strict payload parses as `Statement` | hard error |
| `emph` | return statement | strict payload parses as `Expression` | hard error |
| `list(ordered=true)` | branch | each item starts with a condition header | hard error |
| `list(ordered=false)` | loop | each item starts with a loop condition header | hard error |
| `block_quote` | call | one paragraph forming a complete call | hard error |
| `paragraph` | prose + inline execution | inline executable nodes run in order | n/a |
| everything else | inert document value | — | type/constraint errors only if used |
## 5. Control-stack lowering
Outside functions (module top-level prose) the same executable nodes are inert
document content (S-03 in the blueprint: executable lists/strong/emph only run
inside a function body). The single exception is `main` itself, which is a
function. To make this explicit rather than silent, the frontend emits a
`markdown.inert_executable` **warning** for every would-be execution node found
outside a function body; the document still compiles and the node does nothing.
Within one execution segment, payloads form a token stream. Lower it with one
explicit stack:
Headings inside container blocks (block quotes, list items) or fenced code never
create functions and never execute inline markup.
```text
open_if(cond) -> push IfFrame(cond, then=[], else=None)
statement -> append to the innermost active body
else -> require top is IfFrame with else=None; switch to else body
open_while(cond)-> push WhileFrame(cond, body=[])
end -> require a non-empty stack; pop the top frame and lower it
at_segment_end -> require the stack to be empty
```
### 4.1 Strict payload extraction
Rules:
For `strong`/`emph`, the payload is the concatenation of the direct `text` and
`code` child literals, exactly as written. Nested `strong`, `emph`, `link`,
`strikethrough`, line breaks, math, HTML or any other inline node is an error:
one AST node must never be executed twice or hide control content.
* `else` pairs with the top-of-stack `IfFrame` only. It may not skip an open
`while` or an inner `if`.
* At most one `else` per `if`; `else` with no open `if` is
`sem.unmatched_else`; a second `else` is `sem.duplicate_else`.
* `end` closes exactly the most recent open `if`/`while`; no match is
`sem.unmatched_end`.
* Reaching a segment boundary with an open frame is `sem.unclosed_control`,
with a related span on the opener. The compiler must not synthesize an `end`.
* `if` lower to the existing `Branch` IR; the optional `else` may lower to a
final unconditional arm while preserving left-to-right short-circuit
evaluation and executing at most one arm. `while` lowers to `Loop`; `return`
lowers to `Return` and unwinds the whole H2 function. `else`/`end` generate no
runtime instruction of their own.
`**x = 1**` and `**`x = 1`**` have the same payload `x = 1`. `**a * b**`
parses as text in most cases; when Markdown emphasis and the multiplication
operator conflict, the author should use a code span for the payload:
`` **`a * b`** ``. There is no text-based second parse.
## 6. Functions, signatures and calls
### 4.2 Paragraph and document order
* Only an Exec H2 creates a callable function. Its body is the concatenation,
in source order, of its direct Exec body segment and the Exec segments of its
H3–H6 descendants that are part of the same function. Knowledge segments
contribute nothing.
* H3–H6 Exec sections share the H2 function's frame; locals assigned in one
segment are visible in later segments. They do not create new functions.
* `params(name: Type, ...)` is legal only as the **first direct execution
payload of an Exec H2 function**, at most once, and not inside a list or
block quote. It is compile-time metadata and may be preceded by prose or a
Knowledge subsection but not by an executing statement. Violations are
`sem.signature_position`; a duplicate declaration is also rejected.
* A function without `params` keeps the existing free-variable inference for
the first version. Public functions should declare `params`; the optional
`sem.inferred_signature` lint may be enabled.
* Calls are ordinary expressions. `print(1)`, `f(n=5)` and `> **print(1)**`
all call because the payload contains a call; the block quote itself does
not create a call. A plain `> print(1)` is inert prose.
* `@knowledge` content is never scanned for identifiers, free variables or
calls.
* `main` must be an Exec H2 to be the default `run` entry; if none exists, `run`
reports `sem.no_entry_function`.
Executable inline nodes in a paragraph run in source order. Blocks run in AST
document order. A `return` short-circuits the remaining nodes of the enclosing
function, including nodes inside branches and loops.
## 7. Segments and the heading boundary
### 4.3 Branch (`ordered list`)
An **execution segment** is one heading's direct body, excluding descendant
headings. Segments are ordered by source position. For each Exec H2 function,
process its own segment followed by the Exec segments of its descendant
headings, in document order.
Each item must:
The control stack is reset and required to be empty at every heading boundary.
A control block opened in one heading's direct body must be closed in that same
body; `end` in a later H3/H4 or on the other side of a Knowledge heading is
`sem.unmatched_end` there, while the opener's segment reports
`sem.unclosed_control`. The two errors are reported independently and are never
"repaired" across the boundary.
1. contain at least one block;
2. have a `paragraph` as its **first** block;
3. have an inline `code` node as the **first inline node** of that paragraph;
its literal is the condition expression;
4. any remaining inline content in that first paragraph is **inert prose**
(`text`, inline `code`, links, images, math, HTML, strikethrough and line
breaks). An executable `Strong`/`Emph` there is a hard
`markdown.invalid_construct` error, not silently ignored description.
Complete segments are appended to the enclosing function body in source order;
earlier segments' assignments remain visible to later ones.
The condition literal is mapped from the bytes **inside** the code-span
backticks. Delimiters are part of the Code node's source span, but condition
expression spans and parse diagnostics point at the literal.
## 8. Diagnostics
The item's remaining blocks form the branch body. Items are tested in AST
order; the first condition evaluating to `true` is executed once. No match
means `Unit` and execution continues after the list. Conditions must be
`Bool`; they must be pure (side effects in conditions are a static error when
detectable and a runtime capability concern otherwise).
Stable machine-readable codes (the human `message`/`hint` text may change):
### 4.4 Loop (`bullet list`)
| Code | Trigger |
|---|---|
| `sem.invalid_mode_marker` | unknown/case-variant/conflicting `@...` declaration at heading start |
| `sem.empty_unit_name` | a declaration with no non-empty heading name |
| `sem.duplicate_unit_name` | duplicate canonical heading/unit name in one scope |
| `sem.exec_without_function` | an executable payload with no owning H2 Exec function |
| `sem.payload_nested` | a `Strong`/`Emph` with a disallowed nested node |
| `sem.empty_payload` | an empty carrier in an Exec scope |
| `sem.syntax` | malformed statement, missing condition/value, trailing tokens, colon, unsupported keyword |
| `sem.unmatched_else` | `else` with no open `if` at the top of the stack |
| `sem.duplicate_else` | a second `else` for the same `if` |
| `sem.unmatched_end` | `end` with no open control frame |
| `sem.unclosed_control` | an open `if`/`while` at a heading/function/file boundary |
| `sem.signature_position` | `params` not at the first direct payload, duplicated, or in a container |
| `sem.unsupported_version_directive` | legacy `marqdo_semantics`/syntax-selection front matter |
| `sem.no_entry_function` | `run` with no Exec H2 `main` |
| `sem.inferred_signature` | lint: public function without explicit `params` |
Each item is an independent `while` loop. Item structure matches the branch
header rule: the first paragraph's first inline node must be a `code` span
(condition), and the remaining blocks are the loop body. The condition is
re-evaluated before every iteration and must be `Bool`. Items execute in
order, so a bullet list is a sequence of loops, one per item.
Existing `expr.*`, `symbol.unknown`, `runtime.*`, `resource.*` and capability
diagnostics keep their meaning. Diagnostics are ordered by
`(source_file, start_byte, code)`, and each carries an exact UTF-8 span plus a
related span when an opener or conflicting declaration is involved. A file with
any hard error is never executed.
Iteration over collections is expressed with stdlib `map`/`filter`/`fold`;
a dedicated `forEach` header is intentionally not part of v1.
## 9. Security and the runtime boundary
### 4.5 Call (`block quote`)
`@exec` is a compile-time projection, not a capability grant. Resource, file,
network, stdin/stdout, memory and budget checks are unchanged. A payload in
`@knowledge` produces zero IR, so it can never cause a host effect, even if its
text looks like `resource.open(...)`. Permission flags never change the
AST→IR result; they are checked at runtime by the existing host boundary.
A block quote is exactly one `paragraph` and nothing else. The paragraph is
parsed with the call grammar:
The mapping introduces no `HostOp` for `if`/`else`/`while`/`end`/`return`, no
second interpreter and no runtime knowledge of Markdown emphasis. It lowers to
the existing `Assign`/`Eval`/`Branch`/`Loop`/`Return` IR.
* plain payload: `打印("你好")`, `math.eval(ref("#公式"), {"r": r}, target="A")`
— the strict payload must parse as a complete `CallExpr`;
* link form: `[计算面积](./math.mq.md#计算面积)(r=3)` — the first inline node
must be a `link`, its destination identifies the target function, and the
remaining `text`/`code` payload must be a parenthesised argument list.
## 10. Migration table (old mapping → current mapping)
Anything else (a second paragraph, plain prose, a nested quote, an expression
that is not a call) is a hard error. Two calls require two block quote nodes.
`> [说明](#目标)` without an argument list is not accepted in v1.
| Requirement | Old source | Current source |
|---|---|---|
| Namespace | `# 算法` | `# @knowledge 算法`, or `# @exec 算法` for an Exec default |
| Function | `## main` | `## @exec main` (or an unmarked H2 under an Exec H1) |
| Assignment | `**x = 1**` | `**x = 1**`, `*x = 1*` or `` `x = 1` `` |
| Return | `*x*` | `**return x**`, `*return x*` or `` `return x` `` |
| Branch | ordered list with a first code-span condition | `if cond ... else ... end`; containers are presentation only |
| Loop | bullet list with a first code-span condition | `while cond ... end` |
| Call | block quote containing a call, or a Strong expression | any carrier whose expression is a call |
| Signature | first standalone code span `(n: Int)` after the H2 | `params(n: Int)` as the first direct payload |
| Ordinary list | error inside a function | ordinary document structure, zero statements |
| Ordinary quote | error inside a function | ordinary document structure, zero statements |
| Knowledge | implied by position/inertness only | explicit `@knowledge` scope |
The call result is discarded by the statement form; use `strong` to bind it
(`**面积 = 计算面积(r=3)**`).
Old files are not compatible. Migration must be done by AST role, not by a
global regular expression, and a migrated program must preserve function names,
argument meaning, evaluation order, early-return behavior and side effects.
## 5. Calls, returns and values
* A call is `函数(...)`; zero-argument calls still write `()`.
* Positional arguments are only allowed for functions with an explicit
signature. Named arguments can be given in any order for any function.
* Arguments are evaluated in source order exactly once before binding.
* `emph` returns the value of its expression from the enclosing function
(propagating through branch/loop bodies), never from a block.
* `*表达式*` used in a branch is a return from the whole function.
* A function that ends without a return evaluates to `Unit`. `None` is an
explicit empty value and is distinct from `Unit`.
* An `emph` node outside any function body is inert document content and emits
a `markdown.inert_executable` warning (ADR-0002); it is not executed and is
not a hard error. Inside a function body, every `emph` is a return.
## 6. Function signatures and parameter inference
* Optional explicit signature: the first `paragraph` immediately after the
function heading, if and only if it consists solely of one inline `code`
span, and that span parses as `(name: Type = default, ...)`.
The declaration is inert Markdown otherwise.
* Without a signature, parameters are inferred from **free reads of executable
expressions** using data-flow analysis over the function's control-flow
graph (see `evaluation.md` §5). Natural language, heading words, code spans
outside executable positions and prose never introduce parameters.
* Inferred parameters are identified by name. Functions with inferred
signatures accept **named arguments only**. Explicit signatures allow
positional arguments in declaration order.
* A read that is neither a parameter, a local assignment, a module constant,
an imported binding nor a builtin is a `symbol.unknown` error with a hint;
the CLI can list inferred signatures with `inspect --signature`.
## 7. Example: ordered list branch
Source:
## 11. Canonical example
```markdown
## 分类
# @knowledge 数学演示
1. `n < 0`
**结果 = -1**
2. `n == 0`
**结果 = 0**
3. `n > 0`
**结果 = 1**
这是普通文档;**加粗**只显示为加粗。
*结果*
## @exec main
`x = 5`
`y = x * x`
`return y`
```
AST (abridged):
Normalized IR: `Assign(x, 5)`, `Assign(y, Mul(x, x))`, `Return(y)`. The H1 is
Knowledge; the H2 overrides to Exec and is the callable `main`.
```text
heading(2) 分类
list(ordered) start=1
item
paragraph -> code("n < 0"), text("")
strong -> text("结果 = -1")
item
paragraph -> code("n == 0")
strong -> text("结果 = 0")
...
paragraph
emph -> text("结果")
```
## 12. Non-negotiable implementation constraints
IR:
Do not:
```text
Function 分类(params=[n])
Branch [
(Eval(n < 0), [Assign(结果, -1)]),
(Eval(n == 0), [Assign(结果, 0)]),
(Eval(n > 0), [Assign(结果, 1)]),
]
Return(Ident(结果))
```
## 8. Hard-error checklist
The following are compile errors with source spans, never prose fallbacks:
* `strong`/`emph` in a function body whose payload does not parse;
* nested `strong`/`emph`/link inside an executable payload;
* ordered list item whose first inline node is not a code span condition;
* bullet list item without a condition header;
* executable `Strong`/`Emph` in the description remainder of a condition
paragraph (it would otherwise be swallowed);
* non-`Bool` condition (detected statically when the type is known, else at
runtime as `runtime.type`);
* block quote that is not exactly one complete call;
* duplicate function names in a scope;
* empty heading names;
* `emph`/`strong`/control structures outside any function body are inert and
produce a `markdown.inert_executable` warning (see §4); they never return
from or execute anything at module level;
* executable node inside a lazy region being interpreted (must not happen).
* try to parse a carrier and fall back to prose on failure;
* infer a list is a branch because an item looks like a condition;
* use emphasis type to choose execute vs return;
* use block-quote/list type to choose call/branch/loop;
* scan raw Markdown text for `@exec`/`if`/`return` instead of using the AST;
* recover a misspelled `@knowledge` by inheriting the ancestor mode;
* expose a per-file syntax switch, legacy mapper, `--legacy` flag or dual
active specification.
+8 -1
View File
@@ -7,6 +7,13 @@ Profile id: `marqdo.md.v1`
Parser: [comrak](https://comrak.ee/) `=1.0.0`, `default-features = false`
CommonMark baseline: 0.31.2
> **Semantic mapping notice (RFC-LANG-0002).** The Markdown profile is
> unchanged by the breaking syntax revision: the same CommonMark/GFM AST and
> byte spans are produced. What changed is the separate Markdown→program
> mapping in `spec/mapping.md` (heading `@exec`/`@knowledge` modes, unified
> `Strong`/`Emph`/inline `Code` payloads, keyword control flow). Do not bump
> this profile id for a semantic-mapping-only change.
## 1. Why a profile
Markdown is the syntax of the language. Any parser behaviour that can change
@@ -22,7 +29,7 @@ Enabled extensions:
| Option | Value | Consequence |
|---|---|---|
| `extension.table` | `true` | GFM tables produce `table`/`table_row`/`table_cell` nodes. |
| `extension.strikethrough` | `true` | `~~x~~` produces `strikethrough`. Inert unless it appears in an executable payload (where it is an error). |
| `extension.strikethrough` | `true` | `~~x~~` produces `strikethrough`. Strikethrough is never an executable carrier and its content is inert in both modes. |
| `extension.tasklist` | `true` | `- [x]`/`- [ ]` items carry a checkbox flag on the item node. |
| `extension.math_dollars` | `true` | `$...$` and inline `$$...$$` produce `math`; standalone `$$` fences with `math_dollars_block` produce `math_block`. |
| `extension.math_dollars_block` | `true` | Multi-line `$$` display math is a block node. |
+7 -4
View File
@@ -19,17 +19,20 @@ with two or more formulas the runtime raises `ref.ambiguous` and refuses to
pick one (`spec/references.md` §5, ADR-0003).
```markdown
## 圆面积公式
# @knowledge 数学
## @knowledge 圆面积公式
$$
A = \pi r^2
$$
## 计算圆面积
## @exec 计算圆面积
**面积 = math.eval(ref("#圆面积公式"), {"r": 4}, target="A")**
`params(r: Number)`
*面积*
`面积 = math.eval(ref("#圆面积公式"), {"r": r}, target="A")`
`return 面积`
```
Nothing is evaluated by reading or rendering the document. `math.*` must be
+6 -5
View File
@@ -257,16 +257,17 @@ after its file-set digest and cache marker verify.
```
(empty dependencies/plugins/capabilities are omitted);
* writes `index.mq.md`:
* writes `index.mq.md` (the target syntax; the template in a pre-RFC binary
still emits the old spelling and is part of the pending migration):
```markdown
## main
## @exec main
**问候 = "你好,Marqdo"**
`问候 = "你好,Marqdo"`
> 打印(问候)
`打印(问候)`
*问候*
`return 问候`
```
* writes `.gitignore` containing `.marqdo/`;
+4 -4
View File
@@ -323,11 +323,11 @@ namespace (`solver`).
Document syntax (`examples/plugin-demo/plugin-call.mq.md`):
```markdown
## main
## @exec main
**结果 = solver.add(a=20, b=22)**
> 打印(结果)
`结果 = solver.add(a=20, b=22)`
`print(结果)`
`return 结果`
```
Semantics:
+70 -41
View File
@@ -1,7 +1,9 @@
# Marqdo References, Imports and Calls v1
# Marqdo References, Imports and Calls
Status: **frozen for M0.5 except where marked "current limitation".** Language
changes require an ADR plus updated tests.
Status: **current contract; breaking syntax revision (RFC-LANG-0002)**.
Import/reference/call identity, lock and resolution rules are unchanged; the
source spelling of a function heading and a call statement changed. See
`spec/mapping.md` for the current AST→program mapping.
Normative implementation:
@@ -10,9 +12,9 @@ Normative implementation:
| Front matter parsing | `crates/marqdo-markdown/src/frontmatter.rs` |
| Module loading, project root, cycles | `crates/cli/src/loader.rs` |
| Module/Unit/Function identities | `crates/marqdo-ir/src/lib.rs` |
| Heading scopes, anchors | `crates/marqdo-frontend/src/units.rs` |
| Heading scopes, modes, anchors | `crates/marqdo-frontend/src/units.rs` |
| Resource collection and attribution | `crates/marqdo-frontend/src/resources.rs` |
| Link-call mapping | `crates/marqdo-frontend/src/map.rs` |
| Executable payload → statement mapping | `crates/marqdo-frontend/src/map.rs` |
| `ref(...)` runtime resolution | `crates/marqdo-runtime/src/builtins_impl.rs` |
| Static link graph (`inspect --refs`) | `crates/cli/src/commands.rs` |
| Cross-module call validation | `crates/marqdo-frontend/src/calls.rs` |
@@ -29,12 +31,14 @@ separate and **only the call edge can execute code**.
|---|---|---|---|
| **Import** | YAML front matter `import:` mapping | module load / static analysis | No. Imported modules only register definitions; their `main` never runs. |
| **Reference** | Markdown `[label](url)` | `inspect --refs` (static graph) | No. Rendered/read as documentation only. |
| **Call** | `> f(...)` block quote, link-form block quote `> [f](#f)(...)`, or a `CallExpr` in an executable payload | static validation, then execution | Yes, at runtime, through the interpreter. |
| **Call** | a `CallExpr` in an executable payload, e.g. `` `f(...)` ``, `**f(...)**`, `*f(...)*` or `> **f(...)**` | static validation, then execution | Yes, at runtime, through the interpreter. |
Consequences:
* A Markdown link never calls, imports or loads anything. `[计算作用力](#计算作用力)`
is a reference; `> [计算作用力](#计算作用力)()` is a call.
is a reference; `` `计算作用力()` `` in an Exec section is a call.
* The block quote itself never creates a call. `> print(1)` is inert prose;
`> **print(1)**` contains a call payload.
* `ref("#anchor")` is a **runtime builtin**, not an import; it only resolves an
inert resource or function reference in the running module.
* The reference graph is static metadata and creates no proof obligation.
@@ -81,10 +85,14 @@ pub struct UnitId { pub module: ModuleId, pub path: Vec<String> }
outermost heading to this unit (root unit has an empty path).
* Canonical heading names are NFC-normalized, trimmed, with internal
whitespace collapsed to one space (`marqdo_markdown::normalize_name`).
* A heading mode declaration (`@exec`/`@knowledge`) is stripped before
normalization. `## @exec main` has the semantic name `main`; the declaration
is never part of a `UnitId`, `FunctionId` or anchor.
* `Display` renders `<module>#<path joined by '/'>`; the root unit renders as
`<module>#`.
* `UnitKind` is `root`, `namespace` (`#`), `function` (`##`) or `section`
(`###`+). Sections are addressable but not callable.
* `UnitKind` is `root`, `namespace` (`#`), `function` (`##` Exec) or `section`
(`###`+). A Knowledge H2 is an addressable knowledge unit, not a `function`.
Sections are addressable but not callable.
### 2.3 `FunctionId`
@@ -101,8 +109,8 @@ pub struct FunctionId { pub module: ModuleId, pub name: String }
`local_name`; if several namespaces define the same local name the lookup
returns `None` rather than guessing.
* No overloads: duplicate canonical names in one scope are hard errors
(`markdown.invalid_construct`, "duplicate heading name ... in the same
scope") and are never auto-suffixed.
(`sem.duplicate_unit_name`, "duplicate heading name ... in the same scope") and
are never auto-suffixed.
### 2.4 Anchors are a separate namespace, not identity
@@ -129,7 +137,7 @@ import:
util: "./lib/util.mq.md"
---
# main
# @exec main
```
Parsing rules (`crates/marqdo-markdown/src/frontmatter.rs::imports`):
@@ -139,6 +147,9 @@ Parsing rules (`crates/marqdo-markdown/src/frontmatter.rs::imports`):
* any other front matter key is metadata for tools and is ignored by the
interpreter; invalid YAML is a `frontmatter.invalid` diagnostic, never
silently dropped;
* the removed `marqdo_semantics`/`syntax` keys are **not** metadata that may be
ignored: seeing them is `sem.unsupported_version_directive` because they
would otherwise look like a syntax selector;
* imports are never inferred: a link, a file name or a directory scan does not
create an import;
* the diagnostic span recorded for all imports of a file is the whole front
@@ -271,17 +282,20 @@ Algorithm:
Example (from `examples/math.mq.md`):
```markdown
## 圆面积公式
# @knowledge 数学
## @knowledge 圆面积公式
$$
A = \pi r^2
$$
## 计算圆面积
## @exec 计算圆面积
**面积 = math.eval(ref("#圆面积公式"), {"r": 4}, target="A")**
`params(r: Number)`
*面积*
`面积 = math.eval(ref("#圆面积公式"), {"r": r}, target="A")`
`return 面积`
```
`ref("#圆面积公式")` resolves the single formula under that heading; the
@@ -298,7 +312,9 @@ formula is still inert until `math.eval` consumes it.
| module not present in the program | `symbol.unknown` |
`ref.unresolved` (`crates/marqdo-value/src/diagnostic.rs`) is a **compile-time**
code used by link-form call mapping, not by the `ref` builtin.
code used by `check --refs` link validation (and by stale pre-RFC link-call
fixtures), not by the `ref` builtin. There is no link-form call in the current
language.
## 6. Call edges
@@ -316,25 +332,26 @@ Static validation (`crates/marqdo-frontend/src/calls.rs`) checks arity,
argument names and the named-only rule for inferred signatures. Type errors are
runtime concerns in v1.
### 6.2 Block-quote calls
### 6.2 Calls written in payloads
`> f(...)` is a call statement; the payload must parse as a complete
`CallExpr` (see `mapping.md` §4.5). Its result is discarded.
A call is a statement or expression inside an executable payload, for example
`` `f(...)` ``, `**f(...)**`, `*f(...)*` or `> **f(...)**`. Its result is
discarded unless it is used by an assignment or another expression.
Link form `> [label](#anchor)(args)` resolves the link target **within the
current module** using the anchor map, then the canonical unit-name fallback,
and requires the target to be a function unit. Notes:
The old link-form block-quote call (`> [label](#anchor)(args)`) is **removed**
with the previous syntax; a block quote is now an ordinary container and its
plain content is inert. Migrate such a call to an ordinary payload call. If the
target can only be expressed as a link in the old form and no stable function
reference exists, stop and require a manual rewrite; do not generate a call the
new grammar cannot express.
* a zero-argument link call must still write `()`; proxy `> [f](#f)` is a hard
error;
* `http(s)` link targets used as calls are a hard `ref.unresolved` error;
external links are never downloaded or called;
* a relative-path link target such as `> [f](./other.mq.md#f)(x=1)` is
**not supported** in this build: mapping reports `module.unresolved` with
the hint to import the module in front matter and call through its alias.
Cross-module calls therefore go through an alias expression
(`geometry.f(...)`), not through a Markdown link. Blueprint §2.4's
`> [计算面积](./math.mq.md#计算面积)(r=3)` example is a **known divergence**.
Notes:
* a call expression with no arguments still writes `()`; a bare function value
is not a call;
* `http(s)` link targets are never downloaded or called;
* cross-module calls go through an import alias expression
(`geometry.f(...)`), never through a Markdown link.
### 6.3 Imported alias calls
@@ -347,13 +364,22 @@ program is loaded, and emits `symbol.unknown` for an unknown function.
| Code | Severity | Where produced |
|---|---|---|
| `module.unresolved` | error | loader (bad import target), call mapping (relative-path link call) |
| `module.unresolved` | error | loader (bad import target) |
| `module.cycle` | error | loader |
| `ref.unresolved` | error | link-form call target mapping, external links used as calls, `check --refs` link validation |
| `ref.unresolved` | error | `check --refs` link validation, stale link-form call fixtures |
| `ref.ambiguous` | runtime error | `ref` builtin (≥ 2 resources) |
| `symbol.unknown` | error / runtime error | static call validation; `ref` anchor miss at runtime |
| `frontmatter.invalid` | error | analyser (bad YAML/import shape) |
| `markdown.invalid_construct` | error | empty/duplicate heading names, invalid link-call shape |
| `sem.invalid_mode_marker` | error | heading mode declaration (see `mapping.md` §8) |
| `sem.empty_unit_name` | error | heading mode declaration with no name |
| `sem.duplicate_unit_name` | error | duplicate canonical heading/unit name in one scope |
| `sem.exec_without_function` | error | Exec payload with no owning Exec H2 |
| `sem.unsupported_version_directive` | error | legacy `marqdo_semantics`/syntax-selection front matter |
The previous `markdown.invalid_construct` role errors (Strong/Emph/List/Quote
shape, empty or duplicate heading names) are replaced by the `sem.*` codes and
the heading-name validation in `mapping.md`; runtime shape errors keep their
existing `runtime.*` codes.
## 8. Current limitations (open ADR candidates)
@@ -361,10 +387,13 @@ program is loaded, and emits `symbol.unknown` for an unknown function.
documented syntax for referencing a resource in an imported module.
2. Ordinary Markdown link resolution is inspection-only; `check` does not
validate links and there is no `--refs` flag on `check`.
3. Link-form calls cannot cross modules (see §6.2).
3. Markdown links never call; the removed link-form block-quote call has no
replacement link syntax (use a payload call).
4. Headings inside container blocks or fenced code never create units, and a
heading whose name is empty is a hard error rather than an ignored heading.
heading whose semantic name is empty is a hard error rather than an ignored
heading.
5. Anchor lookup in `ref` does not fall back to canonical unit names, while
link-form calls and `inspect --refs` do. This asymmetry is intentional for
the frozen build but should be unified by a future ADR (it is easy to get
surprising results with headings containing punctuation).
`inspect --refs` does. This asymmetry is intentional for the frozen build
but should be unified by a future ADR.
6. A legacy front matter key such as `marqdo_semantics` is rejected
(`sem.unsupported_version_directive`); it is not a syntax selector.
+9
View File
@@ -48,6 +48,15 @@ On a host that cannot create the namespaces the same block is
for the Linux construction, L1 scope and non-goals; ADR-0019 still governs
every other platform.
### 1.1 `@exec` is not a grant
The `@exec` heading mode introduced by RFC-LANG-0002 is a **compile-time
projection**, not an authorization. A payload in `@exec` still passes the same
`CapabilityHost` checks, budgets and sandbox rules; a payload in `@knowledge`
produces no IR and therefore cannot cause a host effect at all. No heading
annotation, keyword or Markdown container can grant a file/network/process
capability, and permission flags never change the AST→IR result.
## 2. Capability grants
`Capabilities` (`crates/marqdo-host/src/capability.rs`) is the effective grant
+3 -3
View File
@@ -43,9 +43,9 @@ use UTF-8 byte offsets into the source identified by `source.root_id` and
|---|---|---|
| `call.enter` | null | function entry; `function_id` is canonical |
| `call.exit` | `ok` / `return` / error code | function exit |
| `return` | value type name | explicit `Emph` return; no value content |
| `branch.eval` | `true` / `false` | ordered-list branch condition |
| `loop.iteration` | `continue` / `exit` | bullet-list loop condition |
| `return` | value type name | explicit `return` statement; no value content |
| `branch.eval` | `true` / `false` | `if` condition for an `if/else/end` group |
| `loop.iteration` | `continue` / `exit` | `while` condition for a `while/end` group |
| `effect` | result summary | a `HostOp` effect; `operation` names it |
Future event kinds are additive. Consumers must ignore unknown kinds rather