feat(stdlib): ship a verified bundled stdlib with std: imports

- Move the Compat-A print prototype into stdlib/ as the official offline
  bundle with stdlib.json (version 0.1.0 + sha256 content digest).
- Add std:<relative> import resolution in the loader with bundle path
  confinement, extension inference and fail-closed digest verification.
- Record the verified version/digest/instance_id in Analysis v2
  project.stdlib; stdlib modules get a std:name@version#digest instance id.
- Add CLI tests: std:io execution, digest-tamper rejection, missing manifest,
  Analysis identity, no foreign/plugin calls, and frozen binary hash equality.
- Update ADR-0028, specs, status, changelog and evidence.

Gate: fmt, clippy -D warnings, 415 tests passed.
This commit is contained in:
cflmy committed 2026-11-20 21:43:14 +08:00
1 parent 1a978c7a42
commit 1377af1601
28 files changed
+879 -23

No files matched your search

+34
View File
@@ -0,0 +1,34 @@
# marqdo-stdlib (bundled first slice)
This directory is the official, offline `marqdo-stdlib` bundle consumed by
`std:<path>` imports. The CLI verifies `stdlib.json.digest` before loading any
module; a mismatch is a hard error.
- `format.mq.md` — value formatting (`显示`, list/map/table display, escaping)
- `text.mq.md` — UTF-8 encoding into `Memory`
- `io.mq.md` — `写文本` / `写值` through `resource.write(stdout)`
- `demo.mq.md` — side-by-side comparison with builtin `打印`
- `tests/` — bundle self-tests
The digest is a SHA-256 over sorted `*.mq.md` relative paths, a NUL, file
bytes, and a NUL. It excludes `stdlib.json` and non-`.mq.md` files. After any
module change, regenerate it with:
```bash
python3 - stdlib <<'PY'
import hashlib, json, sys
from pathlib import Path
root=Path(sys.argv[1])
h=hashlib.sha256()
for p in sorted(root.rglob('*.mq.md')):
h.update(p.relative_to(root).as_posix().encode()); h.update(b'\0')
h.update(p.read_bytes()); h.update(b'\0')
manifest=json.loads((root/'stdlib.json').read_text())
manifest['digest']='sha256:'+h.hexdigest()
(root/'stdlib.json').write_text(json.dumps(manifest, indent=2)+'\n')
PY
```
The version/digest are part of each stdlib module's package instance id, so a
different bundle cannot silently masquerade as the one a project was checked
against.
+20
View File
@@ -0,0 +1,20 @@
---
import:
io: "./io.mq.md"
---
## main
**列表 = ["文本", 2, 1.5, [3, true], {a: 1, b: "x"}, None]**
> io.写值(列表)
> 打印(列表)
**表格 = 表(["名称", "分数"], [["甲", 87], ["乙", 92]])**
> io.写值(表格)
> 打印(表格)
*[类型(列表), 类型(表格)]*
+209
View File
@@ -0,0 +1,209 @@
## 显示
`(值)`
按语言值语义格式化,用于与内置 `打印` 的输出对照。
当前只覆盖 Unit/None/Bool/Int/Float/Text/List/Map/Table;Formula/Code/
FunctionRef/PluginHandle 仍缺少公开观察接口。
**类别 = 类型(值)**
1. `类别 == "Unit"`
*"Unit"*
2. `类别 == "None"`
*"None"*
3. `类别 == "Text"`
*值*
4. `类别 == "List"`
*列表显示(值)*
5. `类别 == "Map"`
*映射显示(值)*
6. `类别 == "Table"`
*表格显示(值)*
7. `true`
*文本(值)*
## 列表显示
`(列表)`
**结果 = "["**
**下标 = 0**
- `下标 < 长度(列表)`
1. `下标 > 0`
**结果 = 结果 + ", "**
2. `true`
**忽略 = 0**
**结果 = 结果 + 值显示(列表[下标])**
**下标 = 下标 + 1**
*结果 + "]"*
## 映射显示
`(映射)`
**键 = map.keys(映射)**
**结果 = "{"**
**下标 = 0**
- `下标 < 长度(键)`
1. `下标 > 0`
**结果 = 结果 + ", "**
2. `true`
**忽略 = 0**
**当前键 = 键[下标]**
**结果 = 结果 + 当前键 + ": " + 值显示(map.get(映射, 当前键))**
**下标 = 下标 + 1**
*结果 + "}"*
## 表格显示
`(表格)`
**列 = table.columns(表格)**
**结果 = "Table[" + 连接文本(列, " | ") + "]{"**
**行号 = 0**
- `行号 < 长度(表格)`
1. `行号 > 0`
**结果 = 结果 + "; "**
2. `true`
**忽略 = 0**
**行 = table.row(表格, 行号)**
**单元格 = []**
**列号 = 0**
- `列号 < 长度(行)`
**单元格 = list.append(单元格, 显示(行[列号]))**
**列号 = 列号 + 1**
**结果 = 结果 + 连接文本(单元格, " | ")**
**行号 = 行号 + 1**
*结果 + "}"*
## 连接文本
`(列表, 分隔)`
**结果 = ""**
**下标 = 0**
- `下标 < 长度(列表)`
1. `下标 > 0`
**结果 = 结果 + 分隔**
2. `true`
**忽略 = 0**
**结果 = 结果 + 列表[下标]**
**下标 = 下标 + 1**
*结果*
## 值显示
`(值)`
列表/映射/表格内部使用 repr 语义:Text 带引号并转义,其它值递归显示。
**类别 = 类型(值)**
1. `类别 == "Text"`
*转义文本(值)*
2. `true`
*显示(值)*
## 转义文本
`(文本)`
**点 = text.codepoints(文本)**
**`结果 = "\""`**
**下标 = 0**
- `下标 < 长度(点)`
**码点 = 点[下标]**
1. `码点 == 34`
**`结果 = 结果 + "\\\""`**
2. `码点 == 92`
**`结果 = 结果 + "\\\\"`**
3. `码点 == 10`
**`结果 = 结果 + "\\n"`**
4. `码点 == 9`
**`结果 = 结果 + "\\t"`**
5. `码点 == 13`
**`结果 = 结果 + "\\r"`**
6. `true`
**结果 = 结果 + text.from_codepoint(码点)**
**下标 = 下标 + 1**
*`结果 + "\""`*
+35
View File
@@ -0,0 +1,35 @@
---
import:
format: "./format.mq.md"
text: "./text.mq.md"
---
## 写文本
`(文本)`
把文本编码为 UTF-8 并写入已授权的 stdout 资源流。
**内存 = memory.new(1, 1)**
**长度 = text.utf8编码(文本, 内存, 0)**
**流 = resource.open("stdio", "stdout", ["write"])**
> resource.write(流, 内存, 0, 长度)
> resource.close(流)
*None*
## 写值
`(值)`
按语言值语义格式化后输出,并追加换行。
**文字 = format.显示(值) + "\n"**
> 写文本(文字)
*None*
+5
View File
@@ -0,0 +1,5 @@
{
"name": "marqdo-stdlib",
"version": "0.1.0",
"digest": "sha256:000f39b01b3a1a7a8c32fe13d82db7f361166d1d7e8564d124b558f9b56d4e48"
}
+10
View File
@@ -0,0 +1,10 @@
---
import:
io: "std:io"
---
## main
> io.写值("stdlib self-test")
*None*
+55
View File
@@ -0,0 +1,55 @@
## utf8编码
`(文本, 内存, 基址)`
把 Unicode 标量序列按 UTF-8 写入 `Memory`,返回字节长度。
**点 = text.codepoints(文本)**
**下标 = 0**
**偏移 = 0**
- `下标 < 长度(点)`
**码点 = 点[下标]**
1. `码点 < 128`
**`内存[基址 + 偏移] = 码点`**
**偏移 = 偏移 + 1**
2. `码点 < 2048`
**`内存[基址 + 偏移] = 192 + 码点 // 64`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 % 64`**
**偏移 = 偏移 + 2**
3. `码点 < 65536`
**`内存[基址 + 偏移] = 224 + 码点 // 4096`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 64 % 64`**
**`内存[基址 + 偏移 + 2] = 128 + 码点 % 64`**
**偏移 = 偏移 + 3**
4. `true`
**`内存[基址 + 偏移] = 240 + 码点 // 262144`**
**`内存[基址 + 偏移 + 1] = 128 + 码点 // 4096 % 64`**
**`内存[基址 + 偏移 + 2] = 128 + 码点 // 64 % 64`**
**`内存[基址 + 偏移 + 3] = 128 + 码点 % 64`**
**偏移 = 偏移 + 4**
**下标 = 下标 + 1**
*偏移*