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).
This commit is contained in:
cflmy committed 2026-11-21 12:26:46 +08:00
1 parent 49835424ee
commit e029853e32
375 files changed
+6591 -4790

No files matched your search

+184 -126
View File
@@ -3,117 +3,140 @@ import:
utf8: "../text/utf8.mq.md"
---
# @exec http
## 切片字节
`(字节, 起, 止)`
`params(字节, 起, 止)`
**结果 = []**
**下标 = 起**
- `下标 < 止 and 下标 < 长度(字节)`
`while 下标 < 止 and 下标 < 长度(字节)`
**结果 = list.append(结果, 字节[下标])**
**结果 = list.append(结果, 字节[下标])**
**下标 = 下标 + 1**
**下标 = 下标 + 1**
*结果*
`end`
`return 结果`
## 找子串
`(字节, 子, 起点)`
`params(字节, 子, 起点)`
**结果 = 0 - 1**
**下标 = 起点**
- `下标 + 长度(子) <= 长度(字节) and 结果 < 0`
`while 下标 + 长度(子) <= 长度(字节) and 结果 < 0`
**偏移 = 0**
**偏移 = 0**
**匹配 = true**
**匹配 = true**
- `偏移 < 长度(子) and 匹配`
`while 偏移 < 长度(子) and 匹配`
1. `字节[下标 + 偏移] != 子[偏移]`
`if 字节[下标 + 偏移] != 子[偏移]`
**匹配 = false**
**匹配 = false**
2. `true`
`else`
`if true`
**忽略 = 0**
**忽略 = 0**
**偏移 = 偏移 + 1**
`end`
`end`
**偏移 = 偏移 + 1**
1. `匹配`
`end`
`if 匹配`
**结果 = 下标**
**结果 = 下标**
2. `true`
`else`
`if true`
**忽略 = 0**
**忽略 = 0**
**下标 = 下标 + 1**
`end`
`end`
**下标 = 下标 + 1**
*结果*
`end`
`return 结果`
## 找字符
`(字节, 值, 起点)`
`params(字节, 值, 起点)`
**结果 = 0 - 1**
**下标 = 起点**
- `下标 < 长度(字节) and 结果 < 0`
`while 下标 < 长度(字节) and 结果 < 0`
1. `字节[下标] == 值`
`if 字节[下标] == 值`
**结果 = 下标**
**结果 = 下标**
2. `true`
`else`
`if true`
**忽略 = 0**
**忽略 = 0**
**下标 = 下标 + 1**
`end`
`end`
**下标 = 下标 + 1**
*结果*
`end`
`return 结果`
## 十六进制值
`(字节)`
`params(字节)`
**数值 = 0**
**下标 = 0**
- `下标 < 长度(字节)`
`while 下标 < 长度(字节)`
**值 = 字节[下标]**
**值 = 字节[下标]**
1. `值 >= 48 and 值 <= 57`
`if 值 >= 48 and 值 <= 57`
**数值 = 数值 * 16 + 值 - 48**
**数值 = 数值 * 16 + 值 - 48**
2. `值 >= 65 and 值 <= 70`
`else`
`if 值 >= 65 and 值 <= 70`
**数值 = 数值 * 16 + 值 - 55**
**数值 = 数值 * 16 + 值 - 55**
3. `值 >= 97 and 值 <= 102`
`else`
`if 值 >= 97 and 值 <= 102`
**数值 = 数值 * 16 + 值 - 87**
**数值 = 数值 * 16 + 值 - 87**
4. `true`
`else`
`if true`
**忽略 = 0**
**忽略 = 0**
**下标 = 下标 + 1**
`end`
`end`
`end`
`end`
**下标 = 下标 + 1**
*数值*
`end`
`return 数值`
## 文本字节
`(文本)`
`params(文本)`
把 UTF-8 文本编码为字节列表,上限 8 MiB。
@@ -125,43 +148,46 @@ import:
**下标 = 0**
- `下标 < 字节数`
`while 下标 < 字节数`
**结果 = list.append(结果, 内存[下标])**
**结果 = list.append(结果, 内存[下标])**
**下标 = 下标 + 1**
**下标 = 下标 + 1**
*结果*
`end`
`return 结果`
## 字节文本
`(字节)`
`params(字节)`
把字节列表按 UTF-8 解码为文本,上限 8 MiB。
**长度值 = 长度(字节)**
1. `长度值 == 0`
`if 长度值 == 0`
*{ok: true, value: ""}*
`return {ok: true, value: ""}`
`end`
**页 = (长度值 + 65535) // 65536**
**内存 = memory.new(页, 页)**
**下标 = 0**
- `下标 < 长度值`
`while 下标 < 长度值`
**内存[下标] = 字节[下标]**
**内存[下标] = 字节[下标]**
**下标 = 下标 + 1**
**下标 = 下标 + 1**
*utf8.解码(内存, 0, 长度值)*
`end`
`return utf8.解码(内存, 0, 长度值)`
## 解码分块字节
`(体)`
`params(体)`
**结果 = []**
@@ -169,51 +195,62 @@ import:
**错误 = ""**
- `位置 < 长度(体) and 错误 == ""`
`while 位置 < 长度(体) and 错误 == ""`
**行尾 = 找子串(体, [13, 10], 位置)**
**行尾 = 找子串(体, [13, 10], 位置)**
1. `行尾 < 0`
`if 行尾 < 0`
**错误 = "http.chunked"**
**错误 = "http.chunked"**
2. `true`
`else`
`if true`
**大小 = 十六进制值(切片字节(体, 位置, 行尾))**
**大小 = 十六进制值(切片字节(体, 位置, 行尾))**
1. `大小 == 0`
`if 大小 == 0`
**位置 = 长度(体)**
**位置 = 长度(体)**
2. `位置 + 大小 + 4 > 长度(体)`
`else`
`if 位置 + 大小 + 4 > 长度(体)`
**错误 = "http.chunked"**
**错误 = "http.chunked"**
3. `true`
`else`
`if true`
**结果 = list.concat(结果, 切片字节(体, 行尾 + 2, 行尾 + 2 + 大小))**
**结果 = list.concat(结果, 切片字节(体, 行尾 + 2, 行尾 + 2 + 大小))**
**位置 = 行尾 + 2 + 大小 + 2**
**位置 = 行尾 + 2 + 大小 + 2**
1. `错误 != ""`
`end`
`end`
`end`
`end`
`end`
`end`
`if 错误 != ""`
*{ok: false, code: 错误, message: "分块长度无效"}*
`return {ok: false, code: 错误, message: "分块长度无效"}`
*{ok: true, value: 结果}*
`end`
`return {ok: true, value: 结果}`
## 解析响应字节
`(字节)`
`params(字节)`
解析 HTTP/1.1 响应字节。头部按 ASCII 解析,正文保持原始字节;chunked
长度以字节计。成功返回 `{ok:true, value:{status, reason, headers, body}}`。
长度以字节计。成功返回 {ok:true, value:{status, reason, headers, body}}。
**头部结束 = 找子串(字节, [13, 10, 13, 10], 0)**
1. `头部结束 < 0`
`if 头部结束 < 0`
*{ok: false, code: "http.invalid", message: "缺少头部结束"}*
`return {ok: false, code: "http.invalid", message: "缺少头部结束"}`
`end`
**头部 = 切片字节(字节, 0, 头部结束)**
**体 = 切片字节(字节, 头部结束 + 4, 长度(字节))**
@@ -224,123 +261,142 @@ import:
**下一行 = 找子串(头部, [13, 10], 0)**
- `下一行 >= 0`
`while 下一行 >= 0`
**行列表 = list.append(行列表, 切片字节(头部, 行起, 下一行))**
**行列表 = list.append(行列表, 切片字节(头部, 行起, 下一行))**
**行起 = 下一行 + 2**
**行起 = 下一行 + 2**
**下一行 = 找子串(头部, [13, 10], 行起)**
**下一行 = 找子串(头部, [13, 10], 行起)**
`end`
**行列表 = list.append(行列表, 切片字节(头部, 行起, 长度(头部)))**
1. `长度(行列表) == 0`
`if 长度(行列表) == 0`
*{ok: false, code: "http.invalid", message: "缺少状态行"}*
`return {ok: false, code: "http.invalid", message: "缺少状态行"}`
`end`
**状态行 = 字节文本(行列表[0])**
1. `not 状态行.ok`
`if not 状态行.ok`
*{ok: false, code: "http.invalid", message: "状态行不是 ASCII"}*
`return {ok: false, code: "http.invalid", message: "状态行不是 ASCII"}`
`end`
**状态字节 = 行列表[0]**
**第一空格 = 找字符(状态字节, 32, 0)**
**第二空格 = 找字符(状态字节, 32, 第一空格 + 1)**
1. `第一空格 < 0 or 第二空格 < 0`
`if 第一空格 < 0 or 第二空格 < 0`
*{ok: false, code: "http.invalid", message: "状态行格式无效"}*
`return {ok: false, code: "http.invalid", message: "状态行格式无效"}`
`end`
**状态文本 = 字节文本(切片字节(状态字节, 第一空格 + 1, 第二空格))**
1. `not 状态文本.ok`
`if not 状态文本.ok`
*{ok: false, code: "http.invalid", message: "状态码不是 ASCII"}*
`return {ok: false, code: "http.invalid", message: "状态码不是 ASCII"}`
`end`
**状态 = 整数(状态文本.value)**
**原因 = 字节文本(切片字节(状态字节, 第二空格 + 1, 长度(状态字节)))**
1. `not 原因.ok`
`if not 原因.ok`
*{ok: false, code: "http.invalid", message: "原因短语不是 ASCII"}*
`return {ok: false, code: "http.invalid", message: "原因短语不是 ASCII"}`
`end`
**头 = {}**
**行号 = 1**
- `行号 < 长度(行列表)`
`while 行号 < 长度(行列表)`
**行 = 行列表[行号]**
**行 = 行列表[行号]**
**冒号 = 找字符(行, 58, 0)**
**冒号 = 找字符(行, 58, 0)**
1. `冒号 >= 0`
`if 冒号 >= 0`
**名 = 字节文本(切片字节(行, 0, 冒号))**
**名 = 字节文本(切片字节(行, 0, 冒号))**
**值 = 字节文本(切片字节(行, 冒号 + 2, 长度(行)))**
**值 = 字节文本(切片字节(行, 冒号 + 2, 长度(行)))**
1. `名.ok and 值.ok`
`if 名.ok and 值.ok`
**头 = map.set(头, 名.value, 值.value)**
**头 = map.set(头, 名.value, 值.value)**
2. `true`
`else`
`if true`
**忽略 = 0**
**忽略 = 0**
2. `true`
`end`
`end`
`else`
`if true`
**忽略 = 0**
**忽略 = 0**
**行号 = 行号 + 1**
`end`
`end`
**行号 = 行号 + 1**
`end`
**传输 = map.get(头, "Transfer-Encoding")**
**解码后 = 体**
1. `传输 == "chunked"`
`if 传输 == "chunked"`
**分块 = 解码分块字节(体)**
**分块 = 解码分块字节(体)**
1. `not 分块.ok`
`if not 分块.ok`
*分块*
`return 分块`
**解码后 = 分块.value**
`end`
**解码后 = 分块.value**
2. `true`
`else`
`if true`
**忽略 = 0**
**忽略 = 0**
*{ok: true, value: {status: 状态, reason: 原因.value, headers: 头, body: 解码后}}*
`end`
`end`
`return {ok: true, value: {status: 状态, reason: 原因.value, headers: 头, body: 解码后}}`
## 解析响应
`(文本)`
`params(文本)`
兼容文本 API:按字节解析后,把正文按 UTF-8 解码。
**响应 = 解析响应字节(文本字节(文本))**
1. `not 响应.ok`
`if not 响应.ok`
*{状态: 0, 原因: "", 头: {}, 体: ""}*
`return {状态: 0, 原因: "", 头: {}, 体: ""}`
`end`
**正文 = 字节文本(响应.value.body)**
1. `not 正文.ok`
`if not 正文.ok`
*{状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: ""}*
`return {状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: ""}`
*{状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: 正文.value}*
`end`
`return {状态: 响应.value.status, 原因: 响应.value.reason, 头: 响应.value.headers, 体: 正文.value}`
## 构建请求字节
`(方法, 路径, 头列表, 体字节)`
`params(方法, 路径, 头列表, 体字节)`
构建 HTTP/1.1 请求字节。方法、路径、头名/值按 UTF-8 编码;正文使用
传入的字节列表。
@@ -349,21 +405,22 @@ import:
**下标 = 0**
- `下标 < 长度(头列表)`
`while 下标 < 长度(头列表)`
**头 = 头列表[下标]**
**头 = 头列表[下标]**
**结果 = list.concat(结果, 文本字节(文本(头[0]) + ": " + 文本(头[1]) + "\r\n"))**
**结果 = list.concat(结果, 文本字节(文本(头[0]) + ": " + 文本(头[1]) + "\r\n"))**
**下标 = 下标 + 1**
**下标 = 下标 + 1**
`end`
**结果 = list.concat(结果, [13, 10])**
*list.concat(结果, 体字节)*
`return list.concat(结果, 体字节)`
## 构建请求
`(方法, 路径, 头列表, 体文本)`
`params(方法, 路径, 头列表, 体文本)`
兼容文本 API,正文按 UTF-8 编码。
@@ -371,8 +428,9 @@ import:
**文本结果 = 字节文本(请求)**
1. `not 文本结果.ok`
`if not 文本结果.ok`
*""*
`return ""`
*文本结果.value*
`end`
`return 文本结果.value`