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