Release 2.7.0: executable handbooks and native user-site workflows

This commit is contained in:
cflmy committed 2026-11-10 17:03:06 +08:00
1 parent aa0c4d0f55
commit d9aef957f7
562 files changed
+21064 -20387

No files matched your search

+5 -1
View File
@@ -6,10 +6,14 @@ description: Lex · parse · check · tree-walk end to end
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
Milestone: Phase I (tree-walk).
Pipeline: load → line classify → recursive-descent parse → semantic check → tree-walk eval. No Flex/Bison.
This page runs as a program — documentation is executable:
> print text=Phase I: tree-walk interpreter is live.
We now **[Display this step's result](> print text="Phase I: tree-walk interpreter is live.")**.
+5 -1
View File
@@ -6,10 +6,14 @@ description: 词法 · 语法 · 语义 · 树遍历已贯通
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
里程碑:第一阶段(树遍历)。
流水线:加载 → 行分类 → 递归下降语法 → 语义检查 → 树遍历执行。实现不用 Flex/Bison。
本页本身即可运行,证明「文档即程序」已落地:
> 打印 内容=第一阶段:树遍历解释器已就绪。
随后我们**[显示当前步骤的结果](> 打印 内容="第一阶段:树遍历解释器已就绪。")**。
+5 -1
View File
@@ -6,6 +6,10 @@ description: `--backend bytecode` shares semantics with tree-walk
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
Milestone: bytecode prototype.
The same `.mq.md` runs on tree-walk or the bytecode VM:
@@ -16,4 +20,4 @@ The same `.mq.md` runs on tree-walk or the bytecode VM:
Output matches on both backends:
> print text=Bytecode backend: same program, alternate engine.
We now **[Display this step's result](> print text="Bytecode backend: same program, alternate engine.")**.
+5 -1
View File
@@ -6,6 +6,10 @@ description: `--backend bytecode` 与树遍历共用语义
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
里程碑:字节码原型。
同一套 `.mq.md` 可用树遍历或字节码 VM:
@@ -16,4 +20,4 @@ description: `--backend bytecode` 与树遍历共用语义
两种后端输出一致:
> 打印 内容=字节码后端:同一程序,另一引擎。
随后我们**[显示当前步骤的结果](> 打印 内容="字节码后端:同一程序,另一引擎。")**。
+5 -1
View File
@@ -6,6 +6,10 @@ description: Local browse and static export from the interpreter
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
Version 2.6.0 keeps action descriptions in the sentence and moves their implementation to side notes. Empty parentheses explicitly mark missing logic. Multiple actions and returns keep their original prose, while ordinary links remain navigation. On compact screens, native disclosures keep the implementation readable even without JavaScript.
Executable knowledge now presents inputs, returns, effects, dependencies and separate verification facts. Technical identities and digests stay folded. Source explanations are distinct from captured results; a pure effect summary or linked source is not proof that a program ran or its content was verified.
@@ -33,4 +37,4 @@ Browse Structure keeps narration, highlights executable markers, and attaches
run results (values, taken branch arms, call/loop frames). The same render path
builds this user docs site; CI publishes to `gh-pages`.
> print text=Docs site: generated by the Marqdo interpreter, not hand-written HTML.
We now **[Display this step's result](> print text="Docs site: generated by the Marqdo interpreter, not hand-written HTML.")**.
+5 -1
View File
@@ -6,6 +6,10 @@ description: 本地浏览与静态导出由解释器生成
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
版本 2.6.0 将动作说明留在句子里,实际实现引到侧注;空圆括号明确提示待补逻辑。同一句话可以保留多个调用与返回,普通链接仍是导航。窄屏使用原生可展开详情,没有 JavaScript 也能查看实现。
可执行知识先展示输入、返回、调用边界、依赖和分别列出的验证依据,技术身份与摘要按需展开。源码说明与运行捕获有不同含义;pure、来源关联或构造配置都不能被当作已运行或内容已验证的证据。
@@ -28,4 +32,4 @@ description: 本地浏览与静态导出由解释器生成
Browse Structure 保真叙述、高亮可执行标记,并挂载运行结果(变量值、分支命中臂、调用/循环帧)。
用户站与 live view 同源渲染;CI 发布到 `gh-pages`。
> 打印 内容=文档站:由 Marqdo 解释器生成,不是手写 HTML。
随后我们**[显示当前步骤的结果](> 打印 内容="文档站:由 Marqdo 解释器生成,不是手写 HTML。")**。
+5 -1
View File
@@ -6,8 +6,12 @@ description: Sharp heading is a type; deeper headings are functions or methods
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
In Marqdo, a single sharp (#) defines an object / type constructor. Double sharp and deeper (## …) define free functions or methods. Constructing an object yields a map handle tagged with _type. Call methods with backtick object, then dot, then method name.
See doc/design/objects.md. Example gold: tests/structure/object-handle.mq.md. Official ext/llm and ext/agent use this pattern.
> print text=objects-ok
We now **[Display this step's result](> print text="objects-ok")**.
+5 -1
View File
@@ -6,8 +6,12 @@ description: 一级标题是类型;更深标题是函数或方法
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
Marqdo 中,单个井号(#)定义对象 / 类型构造体;二级及以上(## …)定义自由函数或方法。构造得到带 _type 标记的 map 句柄。方法调用形如:反引号对象、点、方法名。
详见 doc/design/objects.md。金样例:tests/structure/object-handle.mq.md。官方 ext/llm 与 ext/agent 使用同一模式。
> 打印 内容=对象就绪
随后我们**[显示当前步骤的结果](> 打印 内容="对象就绪")**。
+6 -2
View File
@@ -6,6 +6,10 @@ description: Optional packages — not stdlib
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
ext/ holds official optional extensions. They are not part of lib/. Resolve via `~/.marqdo/ext`, MARQDO_EXT, ./ext, or ext/ next to the binary.
Install (v0.2+):
@@ -39,7 +43,7 @@ ext/llm — OpenAI-compatible chat. Design: doc/design/ext-llm.md
ext/agent — agent framework + native plugin. Design: doc/design/ext-agent.md
ext/web — HTTP / SQLite·Postgres site helpers + native plugin (default *Go libweb*, v0.4.0). W0–W7, P3, W8, and customization C0–C4; acceptance example `examples/anlian-mq/`. Browser embed (routes D/E/F, v0.3.4): `web.client_embed` auto-mounts WASM — zero author JS; `lib/browser` + GFM tables; build with `marqdo wasm build`; examples: browser-hello · browser-app · browser-media · web-client-site. Design: doc/design/ext-web.md · WASM: doc/adr/0002-browser-marqdo-wasm.md · roadmaps D/E/F.
ext/web provides Documents/Endpoints, recursive View, document-head and request resources. Decompose layout to Markdown or native-control leaves; data, authentication and transport belong to ext/data, ext/security and ext/net. Follow examples/web-site/index.mq.md and examples/web-net-site/index.mq.md for complete workflows. Browser interaction uses Marqdo clients and the official WASM bridge. Development, builds and real acceptance run through marqdo call in skills/marqdo-dev/index.mq.md; the current author contract starts at ext/web/index.mq.md.
ext/quantum — circuits, draw, noise, formula matrix custom gates, Q7 density/viz, Q8 themed SVG (`theme=dark|light|bw`). Design: doc/design/ext-quantum.md · doc/design/ext-quantum-viz-style.md
@@ -47,4 +51,4 @@ Installer: marqdo ext list / add / remove — doc/design/ext-cli.md
Native plugins: lib/plugin + include/marqdo_abi.h — doc/design/ext-abi.md
> print text=ext-ok
We now **[Display this step's result](> print text="ext-ok")**.
+6 -2
View File
@@ -6,6 +6,10 @@ description: 可选包 — 不是标准库
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
ext/ 存放官方可选扩展,不属于 lib/。解析顺序:`~/.marqdo/ext`、MARQDO_EXT、./ext、或可执行文件旁的 ext/。
安装(v0.2+):
@@ -39,7 +43,7 @@ ext/llm(大模型)— OpenAI 兼容聊天。设计:doc/design/ext-llm.md
ext/agent(智能体)— 智能体框架 + 原生插件。设计:doc/design/ext-agent.md
ext/web(网页)— HTTP / SQLite 站点助手 + 原生插件(默认 *Go libweb*,v0.4.0)。W0–W7、P3、W8 与定制 C0–C4;验收示例 `examples/anlian-mq/`。浏览器挂载(路线 D/E/F,v0.3.4):`网页.客户端挂载` / `web.client_embed` 自动 mount(作者零业务 JS);`lib/浏览器` + GFM;先 `marqdo wasm build`;示例 examples/browser-hello/ · examples/browser-app/ · examples/browser-media/ · examples/web-client-site/。设计:doc/design/ext-web.md · WASM:doc/adr/0002-browser-marqdo-wasm.md · 路线 D/E/F:doc/roadmap/browser-wasm-*.md
ext/web(网页)提供 Document/Endpoint、递归 View、页头和请求资源。布局拆到 Markdown 或原生控件叶子,数据、鉴权与运输分别归 ext/data、ext/security、ext/net。完整实践见 examples/web-site-zh/index.mq.md 与 examples/web-net-site-zh/index.mq.md;浏览器交互通过 Marqdo 客户端及官方 WASM 桥完成。开发、构建和真实验收入口为 skills/marqdo-dev/index.mq.md,操作使用 marqdo call;当前作者契约见 ext/web/网页/索引.mq.md。
ext/quantum(量子)— 电路、绘图、噪声、公式矩阵自定义门、Q7 密度/高级图、Q8 主题 SVG(`主题=dark|light|bw`)。设计:doc/design/ext-quantum.md · doc/design/ext-quantum-viz-style.md
@@ -47,4 +51,4 @@ ext/quantum(量子)— 电路、绘图、噪声、公式矩阵自定义门
通用原生插件:lib/plugin + include/marqdo_abi.h — doc/design/ext-abi.md
> 打印 内容=扩展就绪
随后我们**[显示当前步骤的结果](> 打印 内容="扩展就绪")**。
+11 -7
View File
@@ -9,6 +9,10 @@ import:
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
Optional package (not stdlib). Install: `marqdo ext add quantum` (or build `marqdo_plugin_quantum`).
GHZ (3 qubits): H on q0, then CX 0→1 and 0→2. Ideal |000⟩ / |111⟩ each ≈ 1/2.
@@ -25,11 +29,11 @@ Q8: `draw theme="dark"|"light"|"bw"` (default dark lab look) on circuit / probs
| 2 | CX | 0,1 |
| 3 | CX | 0,2 |
**`qc` = > quantum.circuit qubits=3 steps=`steps`**
**`p` = > `qc`.probabilities**
**`_` = > `qc`.draw kind="probs" theme="dark"**
**`H` = > gate.gate name="H"**
**`__` = > `H`.draw kind="matrix"**
We first **qc = [Compute qc](quantum.circuit(qubits=3, steps=`steps`))** for the next step.
We first **p = [Compute p](> `qc`.probabilities)** for the next step.
We first **_ = [Compute _](> `qc`.draw kind="probs" theme="dark")** for the next step.
We first **H = [Compute H](gate.gate(name="H"))** for the next step.
We first **__ = [Compute __](> `H`.draw kind="matrix")** for the next step.
> print text=`p`
> print text=quantum-feature-ok
We now **[Display this step's result](> print text=`p`)**.
We now **[Display this step's result](> print text="quantum-feature-ok")**.
+11 -7
View File
@@ -9,6 +9,10 @@ description: 官方量子模拟 — 表、门、Q7 密度与高级图、Q8 主
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
可选扩展(非 stdlib)。安装:`marqdo ext add quantum`(或编译 `marqdo_plugin_quantum`)。
中文面在同包 `ext/quantum/量子/`(根 skill:`索引.mq.md`),与英文模块树并列、不混排 API。
@@ -27,11 +31,11 @@ Q8:`绘图 主题="dark"|"light"|"bw"`(默认 dark 科技感)作用于电
| 2 | CX | 0,1 |
| 3 | CX | 0,2 |
**`qc` = > 量子.电路 比特数=3 步骤=`步骤`**
**`p` = > `qc`.概率**
**`_` = > `qc`.绘图 种类="probs" 主题="dark"**
**`H` = > 门.门 名="H"**
**`__` = > `H`.绘图 种类="matrix"**
我们先**qc = [计算并保存 qc](量子.电路(比特数=3, 步骤=`步骤`))**,供下一步使用。
我们先**p = [计算并保存 p](> `qc`.概率)**,供下一步使用。
我们先**_ = [计算并保存 _](> `qc`.绘图 种类="probs" 主题="dark")**,供下一步使用。
我们先**H = [计算并保存 H](门.门(名="H"))**,供下一步使用。
我们先**__ = [计算并保存 __](> `H`.绘图 种类="matrix")**,供下一步使用。
> 打印 内容=`p`
> 打印 内容=量子特性-ok
随后我们**[显示当前步骤的结果](> 打印 内容=`p`)**。
随后我们**[显示当前步骤的结果](> 打印 内容="量子特性-ok")**。
+14 -9
View File
@@ -8,12 +8,17 @@ import:
# Formula documents with ext/linalg
Not stdlib. Install: `marqdo ext add linalg` (or build `marqdo_plugin_linalg`).
*Idea:* if the `.mq.md` runs and the asserted form matches, the identity holds under the library rules. Algebra reads like a lecture; `factorize` / `lstsq` / `draw` stay explicit calculation steps.
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
Declare symbols in a table, multiply, transpose — `T_ascii` is one-step display (auto-simplify).
`shapes` =
@@ -23,15 +28,15 @@ Declare symbols in a table, multiply, transpose — `T_ascii` is one-step displa
| 1 | A | 2 | 3 |
| 2 | B | 3 | 2 |
**`env` = > la.declare table=`shapes`**
**`A` = [A](access(env, "A"))**
**`B` = [B](access(env, "B"))**
**`P` = `A` * `B`**
**`t` = > `P`.T_ascii**
> print text=`t`
We first **env = [Compute env](la.declare(table=`shapes`))** for the next step.
We first **A = [A](access(`env`, "A"))** for the next step.
We first **B = [B](access(`env`, "B"))** for the next step.
We first **P = (`A` * `B`)** for the next step.
We first **t = [Compute t](> `P`.T_ascii)** for the next step.
We now **[Display this step's result](> print text=`t`)**.
1. `t` == "B^T*A^T"
> print text=formula-doc-ok
We now **[Display this step's result](> print text="formula-doc-ok")**.
2. *
> print text=formula-doc-fail
We now **[Display this step's result](> print text="formula-doc-fail")**.
> print text=see-examples-linalg-transpose
We now **[Display this step's result](> print text="see-examples-linalg-transpose")**.
+17 -9
View File
@@ -14,6 +14,14 @@ description: ext/linalg 公式文档面 — 声明、中缀、跑通即正确
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
这里讨论符号表达式的形状,而不是对矩阵中的每个数值做计算。先声明 A 为两行三列、B 为三行两列,乘积才具有合法的二行二列形状;转置时两个因子的顺序交换,得到 B^T*A^T。输出相符只证明本例在库定义的规范化规则下成立,不意味着任意数值近似或未声明符号也会被接受。
阅读时先看形状表,再看乘法和转置的动作说明。公式库需要对应的原生插件,缺少依赖时应先安装 linalg;不要把插件加载错误当成恒等式不成立。此例不读取或写入业务文件,也不把输出字符串重新执行成代码。需要实际矩阵分解时,沿文件开头的导入路径阅读分解函数的独立契约。
用表声明符号,再乘与转置 — `转置文本` 一步展示(默认化简)。
`形状` =
@@ -23,15 +31,15 @@ description: ext/linalg 公式文档面 — 声明、中缀、跑通即正确
| 1 | A | 2 | 3 |
| 2 | B | 3 | 2 |
**`环境` = > la.声明 表=`形状`**
**`A` = [A](access(环境, "A"))**
**`B` = [B](access(环境, "B"))**
**`P` = `A` * `B`**
**`t` = > `P`.转置文本**
> 打印 内容=`t`
我们先**环境 = [声明带有行列约束的符号环境](la.声明(表=`形状`))**,供下一步使用。
我们先**A = [A](access(`环境`, "A"))**,供下一步使用。
我们先**B = [B](access(`环境`, "B"))**,供下一步使用。
我们先**P = (`A` * `B`)**,供下一步使用。
我们先**t = [取得当前步骤的转置公式或随机令牌](> `P`.转置文本)**,供下一步使用。
随后我们**[显示当前步骤的结果](> 打印 内容=`t`)**。
1. `t` == "B^T*A^T"
> 打印 内容=formula-doc-ok
随后我们**[显示当前步骤的结果](> 打印 内容="formula-doc-ok")**。
2. *
> 打印 内容=formula-doc-fail
随后我们**[显示当前步骤的结果](> 打印 内容="formula-doc-fail")**。
> 打印 内容=see-examples-linalg-transpose
随后我们**[显示当前步骤的结果](> 打印 内容="see-examples-linalg-transpose")**。
+13 -8
View File
@@ -9,17 +9,22 @@ import:
# Regex and encoding in lib/
Mid-tier stdlib: everyday primitives stay in `lib/`, not ABI plugins.
# main
**`ok` = > re.is_match text="user@example.com" pattern="^[^@]+@[^@]+$"**
**`tok` = > enc.base64_encode text="marqdo"**
**`hex` = > enc.hex_encode text="ok"**
> print text=`ok`
> print text=`tok`
> print text=`hex`
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
We first **ok = [Compute ok](re.is_match(text="user@example.com", pattern="^[^@]+@[^@]+$"))** for the next step.
We first **tok = [Compute tok](enc.base64_encode(text="marqdo"))** for the next step.
We first **hex = [Compute hex](enc.hex_encode(text="ok"))** for the next step.
We now **[Display this step's result](> print text=`ok`)**.
We now **[Display this step's result](> print text=`tok`)**.
We now **[Display this step's result](> print text=`hex`)**.
1. `ok`
> print text=mid-m1-ok
We now **[Display this step's result](> print text="mid-m1-ok")**.
2. *
> print text=mid-m1-fail
We now **[Display this step's result](> print text="mid-m1-fail")**.
+16 -8
View File
@@ -13,13 +13,21 @@ description: lib/正则 + lib/编码,无需插件(中层 M1)
# main
**`ok` = > 正则.匹配 内容="user@example.com" 模式="^[^@]+@[^@]+$"**
**`tok` = > 编码.基64编码 内容="marqdo"**
**`hex` = > 编码.十六进制编码 内容="ok"**
> 打印 内容=`ok`
> 打印 内容=`tok`
> 打印 内容=`hex`
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
本例从固定的邮箱样式文本开始:正则只检查是否符合给定模式,并不是邮件地址真实性验证。随后把普通 UTF-8 文本分别编码成 Base64 和十六进制,以展示三种返回值的区别:匹配结果是布尔值,两种编码结果是文本。编码后的文本可以继续传给解码函数,还原原始字节或文字。
编码不具有保密作用,也不会替调用方校验邮箱能否接收消息。这里没有网络请求、文件读写或插件安装;所有输入都来自正文中的字面值。实际接入用户输入时要保留模式与编码函数的错误诊断,不能用空字符串代替失败。最后的标记只检查本例的匹配分支,不能代替编码往返和非法输入的验收。
我们先**ok = [检查给定模式是否匹配邮箱样式](正则.匹配(内容="user@example.com", 模式="^[^@]+@[^@]+$"))**,供下一步使用。
我们先**tok = [把文本编码为 Base64](编码.基64编码(内容="marqdo"))**,供下一步使用。
我们先**hex = [把文本编码为十六进制](编码.十六进制编码(内容="ok"))**,供下一步使用。
随后我们**[显示当前步骤的结果](> 打印 内容=`ok`)**。
随后我们**[显示当前步骤的结果](> 打印 内容=`tok`)**。
随后我们**[显示当前步骤的结果](> 打印 内容=`hex`)**。
1. `ok`
> 打印 内容=mid-m1-ok
随后我们**[显示当前步骤的结果](> 打印 内容="mid-m1-ok")**。
2. *
> 打印 内容=mid-m1-fail
随后我们**[显示当前步骤的结果](> 打印 内容="mid-m1-fail")**。
+24 -19
View File
@@ -1,29 +1,34 @@
---
markup: occlusion
title: Mid stdlib — path and fs
description: lib/path + fs copy/move/temp (Mid M2)
title: Path algebra and isolated native file copying
import:
path: lib/path.mq.md
fs: lib/fs.mq.md
---
# Path algebra and file mid-layer
This page composes native path and filesystem libraries. Call main with fs.write and a disposable --fs-root; the return contains actual observations for a caller or View.
Join and normalize without plugins; copy/move stay sandboxed under the program directory.
## main
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | Joined/normalized path text and actual copied content |
**`p` = > path.join a="out" b="demo.txt"**
**`n` = > path.normalize path="out/./x/../demo.txt"**
> fs.write_text path=`p` text="path-m2"
> fs.copy_file src=`p` dest="out/demo-copy.txt"
**`body` = > fs.read_text path="out/demo-copy.txt"**
> print text=`p`
> print text=`n`
> print text=`body`
1. `body` == "path-m2"
> print text=mid-m2-ok
2. *
> print text=mid-m2-fail
> fs.remove path=`p`
> fs.remove path="out/demo-copy.txt"
Joining and normalizing transform path text without granting filesystem access. The illustrative out/demo.txt is not an actual write destination. All real IO uses a unique native temporary directory, so this page neither requires a pre-existing out directory nor overwrites a caller's same-named files.
Grant fs.write and choose a disposable root. Native encoding, path and root checks still apply. Read the copied content before cleaning this run's directory; the returned value should be path-m2. The result contains observations rather than live file paths. An intermediate failure retains its native diagnostic and can leave this run's directory for inspection and caller cleanup.
The complete flow demonstrates text-only path algebra, actual writing, copying, reading and scoped cleanup. A normalized string is not a filesystem permission, and a printed marker alone would not prove what bytes were copied.
We **requested = [Join the illustrative output path](path.join(a="out", b="demo.txt"))** and **normalized = [Remove redundant path segments without filesystem IO](path.normalize(path="out/./x/../demo.txt"))** before the file demonstration.
For actual IO we **work = [Create a unique directory inside the granted root](fs.temp_dir(prefix="path-demo"))** and **original = [Name this run own source file](path.join(a=`work`, b="demo.txt"))** and **copied = [Name its isolated copy](path.join(a=`work`, b="demo-copy.txt"))**.
We **[Write the actual text to copy](fs.write_text(path=`original`, text="path-m2"))** and **[Copy only this run own file](fs.copy_file(src=`original`, dest=`copied`))**.
We **copied_text = [Observe the actual copied content](fs.read_text(path=`copied`))** and then **[Clean only this run unique directory](fs.remove_tree(path=`work`))**.
`observed` =
| requested | normalized | copied_text |
|---|---|---|
| `requested` | `normalized` | `copied_text` |
After the isolated file operation we return *observed*. The temporary files have been removed.
+25 -20
View File
@@ -1,29 +1,34 @@
---
markup: occlusion
title: 中层标准库 — 路径与文件
description: lib/路径 + 文件复制/移动/临时(中层 M2)
导入:
title: 路径代数与隔离的原生文件复制
import:
路径: lib/路径.mq.md
文件: lib/文件.mq.md
---
# 路径代数与文件中层
本页直接使用路径库和文件库。调用 main 时授权 fs.write,并将 --fs-root 指向可丢弃的工作目录。函数返回实际观察记录,View 可以显示这些值;无需生成外部脚本。
无插件拼接/规范化;复制/移动仍落在程序目录沙箱内。
## main
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | 路径拼接文本、规范化文本与实际复制正文 |
**`p` = > 路径.拼接 甲="out" 乙="demo.txt"**
**`n` = > 路径.规范化 路径="out/./x/../demo.txt"**
> 文件.写文本 路径=`p` 内容="path-m2"
> 文件.复制文件 源=`p` 目标="out/demo-copy.txt"
**`body` = > 文件.读文本 路径="out/demo-copy.txt"**
> 打印 内容=`p`
> 打印 内容=`n`
> 打印 内容=`body`
1. `body` == "path-m2"
> 打印 内容=mid-m2-ok
2. *
> 打印 内容=mid-m2-fail
> 文件.删除 路径=`p`
> 文件.删除 路径="out/demo-copy.txt"
路径拼接和规范化先处理文本,不隐含文件访问授权。out/demo.txt 只是这个代数示例的输入,不会成为实际写入目的地。真正的文件操作全部放在通过固有库创建的唯一临时目录里,因此不要求已有 out 目录,也不覆盖用户的同名文件。
调用需要 fs.write,文件库仍会检查授权根目录、编码与路径边界。复制后的正文应为 path-m2。保存这个实际值后清理本轮目录,再返回观察记录;临时路径不会成为可继续使用的持久产物。失败保留原生诊断,中途错误可能留下本轮临时目录供检查,调用方负责识别并清理它。
本函数既展示无 IO 的路径代数,也展示真实写入、复制、读取和清理。业务中不要根据一个规范化后的字符串就认为已经获得文件权限,更不要把打印成功标记当成已读回复制内容的证明。
先**requested = [组合演示用的相对路径文本](路径.拼接(甲="out", 乙="demo.txt"))**和**normalized = [消除相对路径中的冗余片段](路径.规范化(路径="out/./x/../demo.txt"))**,核对路径代数本身。
为实际读写创建**work = [只使用本轮独有的授权临时目录](文件.临时目录(前缀="路径演示"))**,再准备**original = [在临时目录中命名原文件](路径.拼接(甲=`work`, 乙="demo.txt"))**和**copied = [在同一目录中命名副本](路径.拼接(甲=`work`, 乙="demo-copy.txt"))**。
随后**[写入本轮需要复制的正文](文件.写文本(路径=`original`, 内容="path-m2"))**,并**[复制本轮文件而不触及已有用户文件](文件.复制文件(源=`original`, 目标=`copied`))**。
读取阶段保存**copied_text = [读取副本的真实正文](文件.读文本(路径=`copied`))**,完成观察后**[清理本轮独有的临时目录](文件.删目录树(路径=`work`))**。
`observed` =
| requested | normalized | copied_text |
|---|---|---|
| `requested` | `normalized` | `copied_text` |
完成路径处理与隔离读写后,我们返回*observed*,其中保留的是已观察到的值,临时文件已经清理。
+12 -7
View File
@@ -9,16 +9,21 @@ import:
# Digests and secure tokens
Integrity digests and cryptographic tokens stay in `lib/` — do not use `math.random` for secrets.
# main
**`d` = > hash.sha256 text="marqdo"**
**`t` = > sec.token_hex n=8**
**`L` = > len `t`**
> print text=`d`
> print text=`L`
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
We first **d = [Compute d](hash.sha256(text="marqdo"))** for the next step.
We first **t = [Compute t](sec.token_hex(n=8))** for the next step.
We first **L = [Compute L](len(`t`))** for the next step.
We now **[Display this step's result](> print text=`d`)**.
We now **[Display this step's result](> print text=`L`)**.
1. `L` == 16
> print text=mid-m3-ok
We now **[Display this step's result](> print text="mid-m3-ok")**.
2. *
> print text=mid-m3-fail
We now **[Display this step's result](> print text="mid-m3-fail")**.
+15 -7
View File
@@ -13,12 +13,20 @@ description: lib/哈希 + lib/机密,无需插件(中层 M3)
# main
**`d` = > 哈希.哈希256 内容="marqdo"**
**`t` = > 机密.十六进制令牌 字节数=8**
**`L` = > 长度 `t`**
> 打印 内容=`d`
> 打印 内容=`L`
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
摘要和随机令牌解决不同的问题。固定文本的 SHA-256 摘要可用于比对内容是否变化;随机令牌从安全随机源获取字节,再编码为十六进制文本。八个随机字节会显示为十六个十六进制字符,因此这里检查输出长度,不把令牌与某个固定值比较。
本例不会把令牌写到磁盘或发送到网络。实际产品需要依据用途选择令牌字节数,并在使用后按应用规则保管或撤销;这里的短值只演示参数与编码长度的关系。哈希不提供加密,也不能单靠未认证的摘要证明内容来自谁。随机源失败时应保留诊断,不能改用普通伪随机数或固定文本充当安全令牌。
我们先**d = [计算固定文本的 SHA-256](哈希.哈希256(内容="marqdo"))**,供下一步使用。
我们先**t = [取得当前步骤的转置公式或随机令牌](机密.十六进制令牌(字节数=8))**,供下一步使用。
我们先**L = [读取十六进制令牌的字符数](长度(`t`))**,供下一步使用。
随后我们**[显示当前步骤的结果](> 打印 内容=`d`)**。
随后我们**[显示当前步骤的结果](> 打印 内容=`L`)**。
1. `L` == 16
> 打印 内容=mid-m3-ok
随后我们**[显示当前步骤的结果](> 打印 内容="mid-m3-ok")**。
2. *
> 打印 内容=mid-m3-fail
随后我们**[显示当前步骤的结果](> 打印 内容="mid-m3-fail")**。
+14 -9
View File
@@ -9,18 +9,23 @@ import:
# CSV cleaning with text helpers
Parse a tiny CSV, normalize a field, print — no plugins.
# main
**raw = "name,city\nAda,london\nBob,PARIS\n"**
**rows = > csv.parse text=`raw`**
**r0 = > at value=`rows` index=0**
**city = > text.to_upper text=[city](access(`r0`, "city"))**
> print text=[name](access(`r0`, "name"))
> print text=`city`
**n = > len `rows`**
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
We first **raw = "name,city\nAda,london\nBob,PARIS\n"** for the next step.
We first **rows = [Compute rows](csv.parse(text=`raw`))** for the next step.
We first **r0 = [Compute r0](at(value=`rows`, index=0))** for the next step.
We first **city = [Compute city](text.to_upper(text=[city](access(`r0`, "city"))))** for the next step.
We now **[Display this step's result](> print text=[name](access(`r0`, "name")))**.
We now **[Display this step's result](> print text=`city`)**.
We first **n = [Compute n](len(`rows`))** for the next step.
1. `n` == 2
> print text=mid-m4-ok
We now **[Display this step's result](> print text="mid-m4-ok")**.
2. *
> print text=mid-m4-fail
We now **[Display this step's result](> print text="mid-m4-fail")**.
+17 -9
View File
@@ -13,14 +13,22 @@ description: lib/逗号表 + 加厚 lib/文本(中层 M4)
# main
**raw = "name,city\nAda,london\nBob,PARIS\n"**
**rows = > 逗号表.解析 内容=`raw`**
**r0 = > at value=`rows` index=0**
**city = > 文本.大写 内容=[city](access(`r0`, "city"))**
> 打印 内容=[name](access(`r0`, "name"))
> 打印 内容=`city`
**n = > 长度 `rows`**
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
CSV 解析先保留每个单元格的文本语义;下面选择第一条记录,再把城市字段规范为大写,得到 LONDON。姓名字段仍是 Ada,另一条记录仍保留在原集合中。调用文本变换不会自动替换原记录,因此这里只展示规范化值,不宣称整个表已经被清洗。
这里的 at 使用从零开始的位置,access 则按字段名读取记录;不要把二者的索引规则混用。输入正文直接写在本页,没有文件或进程访问。接入真实文件时先用原生文件库读取文本,再交给同一个解析函数;需要金额等数值运算时必须另外做明确的类型转换,不能根据 CSV 中的数字外观推断类型。两行计数只验证这份固定样本的解析结果。
我们先**raw = "name,city\nAda,london\nBob,PARIS\n"**,供下一步使用。
我们先**rows = [解析或排序当前行记录](逗号表.解析(内容=`raw`))**,供下一步使用。
我们先**r0 = [选择列表中的第一条记录](at(value=`rows`, index=0))**,供下一步使用。
我们先**city = [将第一条记录的城市改为大写文本](文本.大写(内容=[city](access(`r0`, "city"))))**,供下一步使用。
随后我们**[显示当前步骤的结果](> 打印 内容=[name](access(`r0`, "name")))**。
随后我们**[显示当前步骤的结果](> 打印 内容=`city`)**。
我们先**n = [取得本步骤需要的路径或计数](长度(`rows`))**,供下一步使用。
1. `n` == 2
> 打印 内容=mid-m4-ok
随后我们**[显示当前步骤的结果](> 打印 内容="mid-m4-ok")**。
2. *
> 打印 内容=mid-m4-fail
随后我们**[显示当前步骤的结果](> 打印 内容="mid-m4-fail")**。
+15 -10
View File
@@ -10,20 +10,25 @@ import:
# Sort CSV rows and parse flags
Zero plugins: parse CSV, sort by key, parse `--input`.
# main
**raw = "name,score\nbob,2\nalice,10\n"**
**rows = > csv.parse text=`raw`**
**rows = > table.sort_by list=`rows` key="name"**
**r0 = > at value=`rows` index=0**
> print text=[name](access(`r0`, "name"))
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
**args = > split value="--input,scores.csv,--pretty" sep=","**
**flags = > cli.parse args=`args`**
> print text=[input](access(`flags`, "input"))
We first **raw = "name,score\nbob,2\nalice,10\n"** for the next step.
We first **rows = [Compute rows](csv.parse(text=`raw`))** for the next step.
We first **rows = [Compute rows](table.sort_by(list=`rows`, key="name"))** for the next step.
We first **r0 = [Compute r0](at(value=`rows`, index=0))** for the next step.
We now **[Display this step's result](> print text=[name](access(`r0`, "name")))**.
We first **args = [Compute args](split(value="--input,scores.csv,--pretty", sep=","))** for the next step.
We first **flags = [Compute flags](cli.parse(args=`args`))** for the next step.
We now **[Display this step's result](> print text=[input](access(`flags`, "input")))**.
1. [pretty](access(`flags`, "pretty"))
> print text=mid-m5-ok
We now **[Display this step's result](> print text="mid-m5-ok")**.
2. *
> print text=mid-m5-fail
We now **[Display this step's result](> print text="mid-m5-fail")**.
+18 -10
View File
@@ -14,16 +14,24 @@ description: 表排序/去重 + 命令行解析(中层 M5)
# main
**raw = "name,score\nbob,2\nalice,10\n"**
**rows = > 逗号表.解析 内容=`raw`**
**rows = > 表.按键排序 list=`rows` key="name"**
**r0 = > at value=`rows` index=0**
> 打印 内容=[name](access(`r0`, "name"))
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
**args = > split value="--input,scores.csv,--pretty" sep=","**
**flags = > 命令行.解析 参数=`args`**
> 打印 内容=[input](access(`flags`, "input"))
这里先按 name 字段对 CSV 记录排序,所以第一条姓名是 alice;score 在本例中仍是文本,尚未参与数值排序。排序函数返回新集合,重新保存 rows 才能让后续取值使用排序结果。随后构造三个命令参数并交给命令行库,读取 input 的文件名以及 pretty 的开关状态。
参数数组中的元素始终是数据:本例解析 scores.csv 这个名字,不会读取该文件,也不会启动命令解释器。用于真实进程调用时应继续保持逐个参数传递,选项文本要加引号,不要拼成 shell 源码。没有插件、网络和文件授权需求;缺少必须的业务参数时,应由上层根据解析结果决定是否继续,而不是仅看到 pretty 为真就认定所有输入有效。
我们先**raw = "name,score\nbob,2\nalice,10\n"**,供下一步使用。
我们先**rows = [解析或排序当前行记录](逗号表.解析(内容=`raw`))**,供下一步使用。
我们先**rows = [解析或排序当前行记录](表.按键排序(list=`rows`, key="name"))**,供下一步使用。
我们先**r0 = [选择列表中的第一条记录](at(value=`rows`, index=0))**,供下一步使用。
随后我们**[显示当前步骤的结果](> 打印 内容=[name](access(`r0`, "name")))**。
我们先**args = [构造逐项传递的命令参数](split(value="--input,scores.csv,--pretty", sep=","))**,供下一步使用。
我们先**flags = [解析选项值与布尔开关](命令行.解析(参数=`args`))**,供下一步使用。
随后我们**[显示当前步骤的结果](> 打印 内容=[input](access(`flags`, "input")))**。
1. [pretty](access(`flags`, "pretty"))
> 打印 内容=mid-m5-ok
随后我们**[显示当前步骤的结果](> 打印 内容="mid-m5-ok")**。
2. *
> 打印 内容=mid-m5-fail
随后我们**[显示当前步骤的结果](> 打印 内容="mid-m5-fail")**。
+12 -7
View File
@@ -9,18 +9,23 @@ import:
# Observable scripts without plugins
Default log level is info; raise to debug when you need more. Mint ids with uuid.v4.
# main
> log.info text="mid-m6-start"
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
> log.debug text="skipped-by-default"
We now **[Call log.info](> log.info text="mid-m6-start")**.
> log.set_level level="debug"
We now **[Call log.debug](> log.debug text="skipped-by-default")**.
> log.debug text="now-visible"
We now **[Call log.set level](> log.set_level level="debug")**.
**id = > uuid.v4**
**n = > len `id`**
> print text=`n`
We now **[Call log.debug](> log.debug text="now-visible")**.
We first **id = [Compute id](uuid.v4())** for the next step.
We first **n = [Compute n](len(`id`))** for the next step.
We now **[Display this step's result](> print text=`n`)**.
+15 -7
View File
@@ -9,18 +9,26 @@ description: 按级别过滤的日志 + UUID 版本4(中层 M6)
# 无插件的可观测脚本
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;本段完成下述操作,不额外返回业务数据。 |
默认日志级别为 info;需要细节时再调到 debug。用 标识.版本4 生成 id。
# main
> 日志.信息 内容="中层-m6-开始"
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
> 日志.调试 内容="默认会跳过"
随后我们**[执行 日志.信息](> 日志.信息 内容="中层-m6-开始")**。
> 日志.设级别 级别="debug"
随后我们**[执行 日志.调试](> 日志.调试 内容="默认会跳过")**。
> 日志.调试 内容="现在可见"
随后我们**[执行 日志.设级别](> 日志.设级别 级别="debug")**。
**id = > 标识.版本4**
**n = > 长度 `id`**
> 打印 内容=`n`
随后我们**[执行 日志.调试](> 日志.调试 内容="现在可见")**。
我们先**id = [计算并保存 id](标识.版本4())**,供下一步使用。
我们先**n = [计算并保存 n](长度(`id`))**,供下一步使用。
随后我们**[显示当前步骤的结果](> 打印 内容=`n`)**。
+10 -5
View File
@@ -9,13 +9,18 @@ import:
# Time and addresses without plugins
Moments are maps `{unix, zone, iso}`. Parse HTTPS URLs and round-trip query strings in `lib/url` (segment encoding stays in `net.url_encode`).
# main
**epoch = > dt.from_unix unix=0 zone="Asia/Shanghai"**
> print text=[iso](access(`epoch`, "iso"))
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
**u = > url.parse text="https://example.com/path?q=1#top"**
> print text=[host](access(`u`, "host"))
> print text=[query](access(`u`, "query"))
We first **epoch = [Compute epoch](dt.from_unix(unix=0, zone="Asia/Shanghai"))** for the next step.
We now **[Display this step's result](> print text=[iso](access(`epoch`, "iso")))**.
We first **u = [Compute u](url.parse(text="https://example.com/path?q=1#top"))** for the next step.
We now **[Display this step's result](> print text=[host](access(`u`, "host")))**.
We now **[Display this step's result](> print text=[query](access(`u`, "query")))**.
+13 -5
View File
@@ -13,9 +13,17 @@ description: 日历时刻 + URL/查询串(Mid2 M7)
# main
**epoch = > 日时.自戳 秒=0 区="Asia/Shanghai"**
> print text=[iso](access(`epoch`, "iso"))
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
**u = > 址.解析 内容="https://example.com/path?q=1"**
> print text=[host](access(`u`, "host"))
> print text=[query](access(`u`, "query"))
时间戳零表示同一个时间点;指定 Asia/Shanghai 后,ISO 展示采用该时区的当地时间。改变时区不应改变 unix 字段所代表的瞬间。固定时间戳使本例可以重复验证,不依赖机器当前时钟,也不要求读取时间服务。
地址解析只拆分给定的 HTTPS 文本,host 应为 example.com,query 保留 q=1。它不会访问服务器、验证网站可用性或把查询字符串当作本地文件名。后续需要查询参数时应使用地址库对应的查询解析能力,并保留转义和重复键的边界。时间与 URL 都返回有明确字段的记录;先按契约读取字段,再决定展示格式,避免把整份记录误当成最终用户需要的单段文本。
我们先**epoch = [构造固定时区的零时刻](日时.自戳(秒=0, 区="Asia/Shanghai"))**,供下一步使用。
随后我们**[显示当前步骤的结果](> print text=[iso](access(`epoch`, "iso")))**。
我们先**u = [解析当前日期或 URL 输入](址.解析(内容="https://example.com/path?q=1"))**,供下一步使用。
随后我们**[显示当前步骤的结果](> print text=[host](access(`u`, "host")))**。
随后我们**[显示当前步骤的结果](> print text=[query](access(`u`, "query")))**。
+13 -8
View File
@@ -10,17 +10,22 @@ import:
# Config and safe text without plugins
Parse a TOML subset for tools, escape HTML before assembly, encode with base32.
# main
**cfg = > toml.parse text="app = \"demo\"\n[server]\nport = 8080\n"**
> print text=[app](access(`cfg`, "app"))
**srv = [server](access(`cfg`, "server"))**
> print text=[port](access(`srv`, "port"))
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
**safe = > html.escape text="<tag>"**
> print text=`safe`
We first **cfg = [Compute cfg](toml.parse(text="app = \"demo\"\n[server]\nport = 8080\n"))** for the next step.
We now **[Display this step's result](> print text=[app](access(`cfg`, "app")))**.
We first **srv = [server](access(`cfg`, "server"))** for the next step.
We now **[Display this step's result](> print text=[port](access(`srv`, "port")))**.
**b = > enc.base32_encode text="ok"**
> print text=`b`
We first **safe = [Compute safe](html.escape(text="<tag>"))** for the next step.
We now **[Display this step's result](> print text=`safe`)**.
We first **b = [Compute b](enc.base32_encode(text="ok"))** for the next step.
We now **[Display this step's result](> print text=`b`)**.
@@ -12,11 +12,19 @@ description: TOML 配置 + HTML 转义 + base32(Mid2 M8)
# main
**cfg = > 配置.解析 内容="app = \"演示\"\n"**
> print text=[app](access(`cfg`, "app"))
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
**safe = > 超.转义 内容="<tag>"**
> print text=`safe`
三个步骤处理三种文本边界:TOML 解析得到 app 字段的记录,HTML 转义把尖括号变成普通可展示文本,Base32 把 UTF-8 输入编码为另一种文本表示。它们彼此独立,不能把配置解析、页面布局和编码混成一个隐藏的字符串处理流程。
**b = > 编.基32编码 内容="ok"**
> print text=`b`
TOML 文本直接来自本页,读取真实配置文件时应先使用原生文件库;本例不会隐式寻找系统配置。HTML 转义适用于普通文本展示,不能代替 URL、脚本或样式上下文各自需要的处理,更不应因此拼接大量业务 HTML。Base32 只改变表示方法,不加密数据。所有结果都可继续传递给其他 Marqdo 函数,当前步骤不创建文件、不发送网络请求,也不需要外联解释器。
我们先**cfg = [解析显式提供的 TOML 配置](配置.解析(内容="app = \"演示\"\n"))**,供下一步使用。
随后我们**[显示当前步骤的结果](> print text=[app](access(`cfg`, "app")))**。
我们先**safe = [将尖括号转义为普通展示文本](超.转义(内容="<tag>"))**,供下一步使用。
随后我们**[显示当前步骤的结果](> print text=`safe`)**。
我们先**b = [把输入文本编码为 Base32](编.基32编码(内容="ok"))**,供下一步使用。
随后我们**[显示当前步骤的结果](> print text=`b`)**。
+20 -13
View File
@@ -1,26 +1,33 @@
---
markup: occlusion
title: Mid stdlib — fs make_dirs, walk, stat
description: Recursive dirs, walk, and file metadata (Mid2 M9)
title: Native directory trees, enumeration and file metadata
import:
fs: lib/fs.mq.md
---
# Practical filesystem without plugins
This complete demonstration uses native filesystem tools. Call main with fs.write and a disposable --fs-root. It returns actual metadata and relative paths after cleaning its own temporary directory.
`make_dirs` / `remove_tree` / `stat` / `walk` thicken `lib/fs` for scripts and tools.
## main
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | Actual byte size, directory entry count and relative paths |
> fs.make_dirs path="m9-demo/sub"
Create a unique temporary directory, then one child directory and one text file. Every write remains under the granted root, avoiding collisions with a pre-existing demo folder. Paths are built explicitly; no generated reader/writer or foreign process participates.
> fs.write_text path="m9-demo/readme.txt" text="ok"
The content ok occupies two UTF-8 bytes. File size measures stored bytes rather than text character count. Enumeration returns relative paths for the child directory and file; counting those paths is another distinct observation. The example does not depend on operating-system enumeration order. Sort explicitly if a later workflow requires stable ordering.
**st = > fs.stat path="m9-demo/readme.txt"**
> print text=[size](access(`st`, "size"))
Collect the real metadata and paths before removing this run's unique tree. Returned paths describe an observation, not still-live artifacts. A failure can retain its native diagnostic and this run's directory for caller inspection. Cleanup must stay scoped to the directory created by this invocation.
**w = > fs.walk path="m9-demo"**
**n = > len `w`**
> print text=`n`
We **work = [Create a unique directory inside the granted root](fs.temp_dir(prefix="fs-tree-demo"))** and **child = work + "/sub"** and **note = work + "/readme.txt"**.
We **[Create this run own child directory](fs.make_dirs(path=`child`))** and **[Write the exact UTF-8 example content](fs.write_text(path=`note`, text="ok"))**.
We **metadata = [Read the actual file metadata](fs.stat(path=`note`))** and **paths = [Enumerate this run directory entries](fs.walk(path=`work`))** and **size = [Retain storage byte size](access(metadata, "size"))** and **entries = [Count the actual enumerated paths](len(`paths`))**.
We **[Remove only this run unique directory tree](fs.remove_tree(path=`work`))** after collecting the observations.
> fs.remove_tree path="m9-demo"
`observed` =
| size | entries | paths |
|---|---|---|
| `size` | `entries` | `paths` |
After observing and cleaning this run we return *observed*.
+22 -13
View File
@@ -1,24 +1,33 @@
---
markup: occlusion
title: 中层标准库 — 文件目录树与状态
description: 递归目录、遍历与元数据(Mid2 M9)
导入:
title: 原生目录树、遍历与文件状态
import:
文件: lib/文件.mq.md
---
# 无需插件的实用文件系统
使用固有文件库完成整个演示。调用 main 时授予 fs.write,并指定独立的 --fs-root。函数返回实际元数据与相对路径列表,正常结束后清理自己的临时目录。
# main
## main
> 文件.建目录树 路径="m9-演示/子"
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | 真实文件字节数、目录条目数与相对路径列表 |
> 文件.写文本 路径="m9-演示/说明.txt" 内容="好"
本轮先创建唯一临时目录,再在其中建立子目录和文本文件,避免固定的演示名称与已有目录冲突。创建和写入都受 --fs-root 约束,目录参数并不授予访问其他位置的权限。所有路径由当前流程明确构造,没有外部脚本代为读取或修改正文。
**st = > 文件.状态 路径="m9-演示/说明.txt"**
> print text=[size](access(`st`, "size"))
内容“好”占三个 UTF-8 字节,size 描述存储大小而不是字符数。遍历结果包含文件和子目录的相对路径,条目数与字符串长度也是不同的量。本例只观察枚举内容,不依赖操作系统给出的顺序;需要稳定排序时应继续组合表库。
**w = > 文件.遍历 路径="m9-演示"**
**n = > 长度 `w`**
> print text=`n`
实际观察值保存后删除本轮独有的临时树。返回的路径列表描述已经观察过的内容,不是仍然存在的文件句柄。中途失败保留原生诊断与可能留下的本轮目录,调用方可以检查后清理,不应递归删除与演示无关的用户目录。
> 文件.删目录树 路径="m9-演示"
先**work = [创建本轮独有的授权临时目录](文件.临时目录(前缀="文件树演示"))**和**child = work + "/子"**及**note = work + "/说明.txt"**。
接着**[递归创建本轮子目录](文件.建目录树(路径=`child`))**,并**[写入一个明确的 UTF-8 字符](文件.写文本(路径=`note`, 内容="好"))**。
观察时保存**metadata = [读取真实文件元数据](文件.状态(路径=`note`))**和**paths = [枚举本轮目录里的实际条目](文件.遍历(路径=`work`))**及**size = [保留存储字节数](access(metadata, "size"))**和**entries = [保留遍历条目数](长度(`paths`))**。
最后**[只清理本轮独有的目录树](文件.删目录树(路径=`work`))**,再组织实际观察值。
`observed` =
| size | entries | paths |
|---|---|---|
| `size` | `entries` | `paths` |
完成观察与清理后,我们返回*observed*。
+8 -3
View File
@@ -9,10 +9,15 @@ import:
# Closing Mid2 sugar
Light statistics and optional `fields=` on log lines — no remote sinks, no dataframes.
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
`xs` =
| |
@@ -21,8 +26,8 @@ Light statistics and optional `fields=` on log lines — no remote sinks, no dat
| 20 |
| 30 |
**m = > stats.mean list=`xs`**
> print text=`m`
We first **m = [Compute m](stats.mean(list=`xs`))** for the next step.
We now **[Display this step's result](> print text=`m`)**.
`fld` =
@@ -30,4 +35,4 @@ Light statistics and optional `fields=` on log lines — no remote sinks, no dat
|------|--------|
| done | ok |
> log.info text="mid2-m10" fields=`fld`
We now **[Call log.info](> log.info text="mid2-m10" fields=`fld`)**.
+11 -3
View File
@@ -11,6 +11,14 @@ description: 薄均值/中位数/标准差 + 日志 fields(Mid2 M10)
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
数值列表包含十、二十和三十,平均值应为二十。这里把数据写成一列表格,让读者能直接核对输入,而不是在外部解释器中再构造同一份列表。统计函数不修改原集合;需要中位数、标准差或空集合策略时,应继续阅读统计库对应函数的独立契约。
结构化日志同时携带消息与步骤、状态字段,用于后续过滤或诊断。日志输出不是函数的业务返回值,也不能作为已经完成某项发布或文件写入的证据。本例没有文件和网络操作;实际日志目的地与权限沿用日志库的运行环境规则。若后续任务依赖统计结果,就应直接传递 m 的数值,而不是重新解析打印文本或把最后一条日志当作成功标志。
`xs` =
| |
@@ -19,8 +27,8 @@ description: 薄均值/中位数/标准差 + 日志 fields(Mid2 M10)
| 20 |
| 30 |
**m = > 统计.均值 列表=`xs`**
> print text=`m`
我们先**m = [计算列表的算术平均值](统计.均值(列表=`xs`))**,供下一步使用。
随后我们**[显示当前步骤的结果](> print text=`m`)**。
`fld` =
@@ -28,4 +36,4 @@ description: 薄均值/中位数/标准差 + 日志 fields(Mid2 M10)
|------|------|
| 完成 | ok |
> 日志.信息 内容="mid2-m10" 字段=`fld`
随后我们**[执行 日志.信息](> 日志.信息 内容="mid2-m10" 字段=`fld`)**。
+7 -3
View File
@@ -6,6 +6,10 @@ description: 渐进式契约、结构化诊断与面向 AI 的 Marqdo 语言服
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
里程碑(v1.1.0):AI 原生闭环——让 AI 不仅能写 Marqdo,还能验证 Marqdo。
三件套。核心表面守卫(`doc/design/core-surface.md`)钉住 19 个核心构造,任一侧(清单 / 文档 / 金样)单边改动,CI 必红。渐进式契约是可选的 `参数` / `返回` / `字段` 表,直接嵌在叙述里,四边界校验:调用实参、返回值、表绑定、键访问。MLSP for AI(`marqdo mlsp`)是行分隔 JSON 的语言服务,AI 按需查询语法,而不是背诵整部语法。
@@ -30,10 +34,10 @@ printf '%s\n' '{"id":1,"method":"syntax","params":{"query":"返回"}}' | marqdo
MLSP 方法:`locate`(查符号 / 构造卡片)、`syntax`(戒律原文 + 样例 + 文档锚点)、`validate`(结构化诊断)、`repair_targets`(有界靶点)、`repair_apply`(有界行编辑,越界必拒)、`schema`(单元契约)。
> print text=契约是可选的:不写契约表,文件保持全动态。
随后我们**[显示当前步骤的结果](> print text="契约是可选的:不写契约表,文件保持全动态。")**。
> print text=诊断自带 code / severity / suggestion / doc_anchor / doc_quote。
随后我们**[显示当前步骤的结果](> print text="诊断自带 code / severity / suggestion / doc_anchor / doc_quote。")**。
> print text=MLSP 语法卡片内嵌戒律原文与样例,AI 无需预学语法。
随后我们**[显示当前步骤的结果](> print text="MLSP 语法卡片内嵌戒律原文与样例,AI 无需预学语法。")**。
验证结果见:`doc/roadmap/perf-validation-report-2026-09-23.md`。
+7 -3
View File
@@ -6,6 +6,10 @@ description: Progressive contracts, structured diagnostics, and the Marqdo Langu
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
Milestone (v1.1.0): the AI-native loop — so an AI can verify Marqdo, not just write it.
Three pieces. A core surface guard (`doc/design/core-surface.md`) pins the 19 core constructs, so a single-sided change (list / doc / golden sample) turns CI red. Progressive contracts are optional `参数` / `返回` / `字段` tables embedded in the narrative, checked at four boundaries: call argument, return, table bind, key access. MLSP for AI (`marqdo mlsp`) is a line-delimited JSON language service; the AI queries syntax on demand instead of memorizing the whole grammar.
@@ -30,10 +34,10 @@ printf '%s\n' '{"id":1,"method":"syntax","params":{"query":"返回"}}' | marqdo
MLSP methods: `locate` (find a symbol / construct card), `syntax` (rule text + sample + doc anchor), `validate` (structured diagnostics), `repair_targets` (bounded span), `repair_apply` (bounded line edits, out-of-scope refused), `schema` (contract of one unit).
> print text=Contracts are optional: with no contract table, a file stays fully dynamic.
We now **[Display this step's result](> print text="Contracts are optional: with no contract table, a file stays fully dynamic.")**.
> print text=Diagnostics carry code, severity, suggestion, doc_anchor and doc_quote.
We now **[Display this step's result](> print text="Diagnostics carry code, severity, suggestion, doc_anchor and doc_quote.")**.
> print text=MLSP syntax cards embed the rule text and a sample, so AI need not pre-learn the grammar.
We now **[Display this step's result](> print text="MLSP syntax cards embed the rule text and a sample, so AI need not pre-learn the grammar.")**.
See validation results: `doc/roadmap/perf-validation-report-2026-09-23.md`.
+74 -112
View File
@@ -1,34 +1,57 @@
---
markup: occlusion
title: Foreign kernels v2 — Python analysis in view
description: Kernel state, Marqdo-to-kernel values and view cells — without a stdin-only demo
title: Native analysis and an explicit Intl formatting boundary
import:
foreign: lib/foreign.mq.md
table: lib/table.mq.md
result: lib/result.mq.md
native: ../../examples/foreign-native-report/index.mq.md
---
# main
This page keeps order analysis in Marqdo and uses Node Intl only for locale-aware
currency formatting. Read [the actual native report tools](../../examples/foreign-native-report/index.mq.md)
for their independently callable input and return contracts. That single module
contains grouping, population statistics and the bounded persistent formatter.
This page shows the foreign runtime as a language feature: a persistent kernel, explicit state from Marqdo values, and view cells that share kernel state. It never asks the reader to type JSON into stdin.
Call this page's main with process.exec for a full workflow, or call analyze_orders
in that module with no effects for native analysis alone. View evaluates the same
cells with shared state and isolated kernels. No Python interpreter is needed.
---
## main
Probe the interpreter first. The result is ordinary Marqdo data.
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | View-session plan plus the native report and checked localized formatting |
**python = > foreign.probe lang="python"**
This complete page uses six ordinary orders: their native total is 950, count
is six, and the four regional rows retain count, total, mean and normalized span.
The pure analysis is independently callable in the linked module. Formatting is a separate
capability with an explicit process.exec grant and an installed Node runtime.
The successful return contains report and formatting alongside the view-session
plan; it contains no live kernel handle and can be returned by marqdo call.
> print text=[ok](access(`python`, "ok"))
> print text=[version](access(`python`, "version"))
The view gets the same native report copied into two named kernels. Its cells
perform presentation rather than reimplementing native grouping or arithmetic.
Run All first initializes the formatter in the analysis kernel, then a second
cell reuses it. The review kernel receives its own report copy and formatter.
This demonstrates both shared state and kernel isolation. Running the dependent
formatting cell before its initializer should show the real missing-state error.
---
Static View export evaluates those same cells and embeds actual stdout.
Interactive View retains their kernels for later runs. The low-level formatter
used to check the public return is opened and closed separately, so it cannot
secretly supply state to the view kernels. No stdin, notebook command, generated
file-reader program or package installation is hidden in this example.
If Node or formatting fails, the page returns that failure before declaring a
ready view. Native-only consumers can still call analyze_orders with no effects.
The table cells below are data, while the three bound node fences are explicit
foreign code. The distinction is visible without translating a Python workflow
into another ad hoc command sequence.
Marqdo values into a kernel.
`orders` =
The `订单` table is a normal Marqdo value. `foreign.session_set` copies it into the Python kernel as JSON; no shell and no stdin.
`订单` =
| @ | 区域 | 金额 |
|---|------|------|
| @ | region | amount |
|---|---|---|
| 1 | 华东 | 120 |
| 2 | 华东 | 300 |
| 3 | 华南 | 90 |
@@ -36,119 +59,58 @@ The `订单` table is a normal Marqdo value. `foreign.session_set` copies it int
| 5 | 华北 | 220 |
| 6 | 西部 | 80 |
**内核会话 = > foreign.session_open lang="python"**
We **report = [Perform all analysis in Marqdo](native.analyze_orders(orders=`orders`))** and **formatted = [Use the genuine external formatting boundary](native.format_summary(report=`report`))**.
1. not access(`formatted`, "ok")
Return *formatted* before declaring an executable view ready.
**> foreign.session_set id=`内核会话` name="orders" value=`订单`**
`total_code` =
```python
round(sum(float(row["金额"]) for row in orders), 2)
```
**total = > foreign.session_eval id=`内核会话` code=`total_code`**
> print text=[result](access(`total`, "result"))
The low-level kernel session is managed entirely by `foreign`; the table crossed the boundary as an explicit JSON copy.
---
Kernel state persists.
Two evaluations share the same kernel. The second sees the first variable.
`set_values_code` =
```python
values = [3, 4, 5]
```
**> foreign.session_eval id=`内核会话` code=`set_values_code`**
`square_sum_code` =
```python
sum(value * value for value in values)
```
**square_sum = > foreign.session_eval id=`内核会话` code=`square_sum_code`**
> print text=[result](access(`square_sum`, "result"))
**> foreign.session_close id=`内核会话`**
This is kernel state, not a one-shot process. It is created, queried and closed through `foreign`.
---
View cells share state.
`foreign.session` declares a view session from ordinary tables. The table names are free; this document uses `分析内核` and `分析单元`.
`分析内核` =
`kernels` =
| @ | 名称 | 语言 |
|---|------|------|
| 1 | 分析 | python |
| 2 | 复核 | python |
|---|---|---|
| 1 | 分析 | node |
| 2 | 复核 | node |
`分析单元` =
`cells` =
| @ | 单元 | 内核 |
|---|------|------|
|---|---|---|
| 1 | 计算 | 分析 |
| 2 | 复核 | 复核 |
| 2 | 格式化 | 分析 |
| 3 | 复核 | 复核 |
`初始值` =
`initial` =
| 内核 | 名称 | 值 |
|------|------|----|
| 分析 | orders | `订单` |
| 复核 | orders | `订单` |
| @ | 内核 | 名称 | 值 |
|---|---|---|---|
| 1 | 分析 | report | `report` |
| 2 | 复核 | report | `report` |
`会话` = > foreign.session kernels=`分析内核` cells=`分析单元` values=`初始值`
We **会话 = [Declare the view with native values and explicit kernels](foreign.session(kernels=`kernels`, cells=`cells`, values=`initial`))**.
`计算` =
```python
import json
import statistics
```node
var money = new Intl.NumberFormat("en-US", {style:"currency", currency:"CNY"});
console.log(JSON.stringify(report));
```
by_region = {}
for row in orders:
by_region.setdefault(row["区域"], []).append(float(row["金额"]))
summary = []
for region, values in sorted(by_region.items()):
mean = statistics.mean(values)
sd = statistics.pstdev(values) or 1.0
summary.append({
"region": region,
"orders": len(values),
"total": round(sum(values), 2),
"mean": round(mean, 2),
"z_span": round((max(values) - mean) / sd, 3),
})
print(json.dumps({
"regions": summary,
"total": round(sum(float(row["金额"]) for row in orders), 2),
"count": len(orders),
}, ensure_ascii=False))
`格式化` =
```node
console.log(money.format(report.total));
console.log(money.format(report.average));
```
`复核` =
```python
import json
print(json.dumps({
"kernel": "复核",
"count": len(orders),
"total": round(sum(float(row["金额"]) for row in orders), 2),
}, ensure_ascii=False))
```node
var reviewMoney = new Intl.NumberFormat("en-US", {style:"currency", currency:"CNY"});
console.log(JSON.stringify({count:report.count, total:reviewMoney.format(report.total)}));
```
`查看` consumes the `会话` handle. Run All runs `计算` in the `分析` kernel and `复核` in the `复核` kernel; the static export runs the same cells and embeds their real outputs.
We **formatting = [Retain the checked persistent-kernel result](access(formatted, "value"))**.
The important language point:
`evidence` =
The `初始值` table passes the same Marqdo `订单` value into both kernels. Kernel state lives in `foreign`. View only renders the handle and its cells. `run` still executes the same document as a program.
| report | formatting |
|---|---|
| `report` | `formatting` |
There is no separate notebook CLI, no stdin requirement in this example, and no hidden language switch.
Return *[The executable view plan with native analysis evidence](table.merge(a=`会话`, b=`evidence`))*.
+51 -108
View File
@@ -1,34 +1,38 @@
---
markup: occlusion
title: 外联内核 v2 — Python 分析(view 单元)
description: 内核态、Marqdo 值传入与 view 单元 —— 不依赖 stdin 的完整示例
导入:
title: 原生分析与明确的 Intl 格式化边界
import:
外联: lib/外联.mq.md
表: lib/table.mq.md
例程: ../../examples/foreign-native-report/index.mq.md
---
# main
先用固有能力,外联只补真实的能力缺口。本页复用 [原生报告例程](../../examples/foreign-native-report/index.mq.md),让统计在 Marqdo 中完成,再通过已支持的 Node 持久内核使用 Intl。阅读例程的源码即可找到可以独立调用的参数、返回值与失败约束。
这一页把外联运行时当作语言能力来展示:持久内核、从 Marqdo 值显式传入状态,以及共享内核状态的 view 单元。读者不需要向 stdin 输入 JSON。
在仓库根目录调用 marqdo call public/features/19-外联视图单元.mq.md main --effects process.exec --fs-root . --json 可以获得真实报告与视图计划。使用 view 导出同一份源码即可运行下面声明的单元。这里采用函数标题 main,便于作为工具直接调用;不需要 Python 解释器或另一套笔记本入口。
---
## main
先探测解释器。结果仍然是普通 Marqdo 数据。
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | 包含 report、formatting 及内核/单元/初始值的视图计划;失败时返回实际 Result |
**python = > 外联.探测 语言="python"**
格式化结果中的数字会显示为 950.00 与 158.33,货币标志由区域设置决定;原生 report 保留未格式化的数值。
> 打印 内容=[ok](access(`python`, "ok"))
> 打印 内容=[version](access(`python`, "version"))
本页的六笔订单全部由 Marqdo 固有能力处理,得到合计 950、数量六、四个区域的订单数、合计、均值和总体标准化跨度。分组、排序、统计与数值运算都不需要外部脚本;真正交给 Node 的只有 Intl 本地化货币格式。直接调用本页 main 需要 process.exec,Node 应预先安装。成功返回的计划包含 report 原始报告和 formatting 展示结果,调用者可以继续验证具体数值。
---
共享例程有独立的参数及返回表,区域键和金额键都由调用者明确指定,因此中文字段不需要重命名成另一套 JSON 拼接语言。本页和英文页共同调用一份原生算法,避免两个版本逐渐出现不同的统计规则。纯数据分析可以直接调用例程的 analyze_orders,不需要任何进程授权;空输入、单元素区域与相同金额的区域也有清楚的定义。金额只是示例数值,展示的两位小数由 Intl 决定,不宣称是金融十进制定点模型。
Marqdo 值进入内核。
视图的分析内核先输出已经算好的报告,并创建本地化格式器;同一内核中的第二个单元继续使用这个格式器。因此 Run All 的顺序是明确的执行依赖,直接先运行格式化单元应展示真实的未初始化状态。复核单元使用另一份内核和自己的格式器,体现状态隔离。两个内核收到的是报告的数据拷贝,谁都不会重新实现一份分组算法,也不会修改 Marqdo 的源订单。
`订单` 表是普通 Marqdo 值。`外联.会话设值` 把它作为 JSON 复制进 Python 内核;没有 shell,也没有 stdin。
用于验证工具返回值的低层格式化例程单独创建并关闭了一个持久内核:第一次求值创建格式器,第二次复用状态。它检查每次求值的 ok,遇到可恢复错误时关闭仍存活的内核,再返回真实诊断。用于视图的会话随后才被声明;工具验证的内核不能暗中给视图提供状态。视图的静态导出与交互模式运行同一批单元,输出来自实际执行,不用预写的通过文字冒充结果。
正文先交代数据来源和边界,再把调用放入相应的句子。下面的订单、内核、单元和初始值都是普通表格;三段绑定的 node 围栏才是明确外联的代码。整个操作不要求向终端输入 JSON,也不生成文件读取器。若解释器不可用或格式化失败,会在创建可用视图之前把失败交给调用者。
`订单` =
| @ | 区域 | 金额 |
|---|------|------|
|---|---|---|
| 1 | 华东 | 120 |
| 2 | 华东 | 300 |
| 3 | 华南 | 90 |
@@ -36,119 +40,58 @@ Marqdo 值进入内核。
| 5 | 华北 | 220 |
| 6 | 西部 | 80 |
**内核会话 = > 外联.会话开启 语言="python"**
我们先**报告 = [使用固有库完成分组和总体统计](例程.analyze_orders(orders=`订单`, region_key="区域", amount_key="金额"))**,再**格式结果 = [把真正缺失的本地化展示交给 Intl](例程.format_summary(report=`报告`, locale="zh-CN"))**。
1. not access(`格式结果`, "ok")
返回*格式结果*,保留外联解释器或格式化的真实失败。
**> 外联.会话设值 标识=`内核会话` 名称="orders" 值=`订单`**
`合计代码` =
```python
round(sum(float(row["金额"]) for row in orders), 2)
```
**合计 = > 外联.会话求值 标识=`内核会话` 代码=`合计代码`**
> 打印 内容=[result](access(`合计`, "result"))
低层内核会话完全由 `外联` 管理;表格以显式 JSON 拷贝跨过边界。
---
内核态是持续的。
两次求值共享同一个内核。第二次能看到第一次创建的变量。
`设值代码` =
```python
values = [3, 4, 5]
```
**> 外联.会话求值 标识=`内核会话` 代码=`设值代码`**
`平方和代码` =
```python
sum(value * value for value in values)
```
**平方和 = > 外联.会话求值 标识=`内核会话` 代码=`平方和代码`**
> 打印 内容=[result](access(`平方和`, "result"))
**> 外联.会话关闭 标识=`内核会话`**
这是内核态,不是一次性进程。它由 `外联` 创建、查询和关闭。
---
view 单元共享状态。
`外联.会话` 从普通表格声明一个 view 会话。表名完全自由;本页用 `分析内核` 和 `分析单元`。
`分析内核` =
`内核` =
| @ | 名称 | 语言 |
|---|------|------|
| 1 | 分析 | python |
| 2 | 复核 | python |
|---|---|---|
| 1 | 分析 | node |
| 2 | 复核 | node |
`分析单元` =
`单元` =
| @ | 单元 | 内核 |
|---|------|------|
|---|---|---|
| 1 | 计算 | 分析 |
| 2 | 复核 | 复核 |
| 2 | 格式化 | 分析 |
| 3 | 复核 | 复核 |
`初始值` =
| 内核 | 名称 | 值 |
|------|------|----|
| 分析 | orders | `订单` |
| 复核 | orders | `订单` |
| @ | 内核 | 名称 | 值 |
|---|---|---|---|
| 1 | 分析 | report | `报告` |
| 2 | 复核 | report | `报告` |
`会话` = > 外联.会话 内核表=`分析内核` 单元表=`分析单元` 值表=`初始值`
接着**会话 = [用普通表格声明显式的视图会话](外联.会话(内核表=`内核`, 单元表=`单元`, 值表=`初始值`))**,让两份内核获得同一个原生报告的独立 JSON 拷贝。
`计算` =
```python
import json
import statistics
```node
var money = new Intl.NumberFormat("zh-CN", {style:"currency", currency:"CNY"});
console.log(JSON.stringify(report));
```
by_region = {}
for row in orders:
by_region.setdefault(row["区域"], []).append(float(row["金额"]))
summary = []
for region, values in sorted(by_region.items()):
mean = statistics.mean(values)
sd = statistics.pstdev(values) or 1.0
summary.append({
"region": region,
"orders": len(values),
"total": round(sum(values), 2),
"mean": round(mean, 2),
"z_span": round((max(values) - mean) / sd, 3),
})
print(json.dumps({
"regions": summary,
"total": round(sum(float(row["金额"]) for row in orders), 2),
"count": len(orders),
}, ensure_ascii=False))
`格式化` =
```node
console.log(money.format(report.total));
console.log(money.format(report.average));
```
`复核` =
```python
import json
print(json.dumps({
"kernel": "复核",
"count": len(orders),
"total": round(sum(float(row["金额"]) for row in orders), 2),
}, ensure_ascii=False))
```node
var reviewMoney = new Intl.NumberFormat("zh-CN", {style:"currency", currency:"CNY"});
console.log(JSON.stringify({count:report.count, total:reviewMoney.format(report.total)}));
```
`查看` 消费 `会话` 句柄。Run All 在 `分析` 内核执行 `计算`,在 `复核` 内核执行 `复核`;静态导出走同一批单元,并把真实输出嵌入页面。
我们**展示 = [保留已经检查过的持久格式化结果](access(格式结果, "value"))**,把数据与展示来源一并交给调用者。
语言上的重点:
`依据` =
`初始值` 表把同一个 Marqdo `订单` 值传给两个内核。内核态归 `外联` 管理。view 只渲染句柄和单元。`run` 仍然把同一份文档当程序执行。
| report | formatting |
|---|---|
| `报告` | `展示` |
没有单独的 notebook CLI,这个示例不依赖 stdin,也没有隐藏的语言切换。
所有值准备好后返回*[带原生分析依据的可执行视图计划](表.merge(a=`会话`, b=`依据`))*。
+7 -3
View File
@@ -6,6 +6,10 @@ description: Marqdo 如何以一份可执行源为唯一权威,把它投影成
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
Marqdo 的宪法是「代码即文档,文档即知识库」。`.mq.md` 不是程序截图:它同时是可读源码、可执行单元和知识页。
这个承诺只有在知识始终从源派生时才成立。OKF 提供可移植知识包的形态;EKC 提供工程化投影。二者都不成为第二份权威。
@@ -21,7 +25,7 @@ OKF(Open Knowledge Format)用 Markdown + YAML frontmatter 表示知识:一
```yaml
type: Capability
title: 持久外联内核
description: 在命名内核中运行 Python 或 Node 并保留状态
description: 向显式声明的本地内核传递原生数据并保留状态
resource: public/features/19-外联视图单元.mq.md
status: stable
verified:
@@ -63,7 +67,7 @@ EKC 把仓库投影成工程知识系统,并在 Agent 写新代码前提供 pr
|---|---|---|
| L0 Repository | 语言、规模、根目录 | 仓库总览 |
| L1 Module | imports、exports、职责 | 外联内核模块 |
| L2 Symbol | 签名、调用、指纹 | Python 分析函数 |
| L2 Symbol | 签名、调用、指纹 | 外部源码中的函数 |
| L3 Capability | 工程能做什么 | 运行持久外联内核 |
| L4 Knowledge | 决策、约束、失败 | 为什么状态桥必须显式 |
@@ -119,7 +123,7 @@ marqdo knowledge verify
一个知识页说“这个工程能分析订单”,就必须指向一个真正能跑的 `.mq.md` 资源。这是 OKF 形态的笔记与可执行知识系统的区别。
在外联示例里,页面本身就是证明:Python 调用、持久内核和状态桥都从读者正在看的同一份 Markdown 源运行。没有另一个会漂移的演示项目。
外联示例先用 Marqdo 固有能力分析订单,再把本地化展示交给 Node Intl。显式导入指向共享的可调用报表手册,页面本身声明实际的视图内核、原生值与单元。验收仍要检查逐单元的真实结果,不能把一份可序列化计划当作单元已经运行成功的证明。
---
+7 -3
View File
@@ -6,6 +6,10 @@ description: How Marqdo keeps one executable source of truth while projecting it
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
Marqdo's constitution is code as documentation and documentation as knowledge. A `.mq.md` file is not a screenshot of a program: it is the readable source, the executable unit and the knowledge page at the same time.
That claim matters only if the knowledge stays derived from the source. OKF gives us the shape of a portable knowledge bundle; EKC gives us the engineering projection. Neither becomes a second source of truth.
@@ -21,7 +25,7 @@ A concept page can therefore point at the exact file that proves it:
```yaml
type: Capability
title: Persistent foreign kernels
description: Run Python or Node in a named, stateful kernel
description: Run an explicitly declared local kernel with native data
resource: public/features/19-foreign-view-cells.mq.md
status: stable
verified:
@@ -63,7 +67,7 @@ EKC projects the repository into an engineering knowledge system, then exposes i
|---|---|---|
| L0 Repository | language, size, roots | repository summary |
| L1 Module | imports, exports, responsibility | a foreign kernel module |
| L2 Symbol | signature, calls, fingerprint | a Python analysis function |
| L2 Symbol | signature, calls, fingerprint | a function in external source code |
| L3 Capability | what the project can do | run a persistent foreign kernel |
| L4 Knowledge | decisions, constraints, failures | why the bridge is explicit |
@@ -119,7 +123,7 @@ Every claimed capability has an executable resource.
A knowledge page that says “this project can analyze orders” must point to a `.mq.md` resource that actually runs. That is the difference between an OKF-shaped note and an executable knowledge system.
In the foreign example, the page itself is the proof: the Python call, the persistent kernel and the state bridge all run from the same Markdown source that a reader is looking at. There is no separate demo project to drift out of sync.
The foreign example computes its order report in native Marqdo and uses Node Intl for locale-aware presentation. Explicit imports point to the shared callable report handbook, while the page declares the actual view kernels, values and cells. Inspect the real per-cell results; a serialized plan alone does not establish that its cells ran successfully.
---
+40 -40
View File
@@ -1,58 +1,58 @@
---
markup: occlusion
title: Persisting foreign execution results
description: Static snapshots and writeback — how foreign results survive after the process exits
title: Persist an actual formatting result through native writeback
import:
foreign: lib/foreign.mq.md
guide: ../../doc/library-development.mq.md
formatter: ../../examples/foreign-native-report/index.mq.md
writeback: lib/writeback.mq.md
result: lib/result.mq.md
---
# main
The source remains the manual for this file operation. Its callable main computes
native data, crosses a genuine formatting boundary, checks the result and records
only the successful value. Invoke with process.exec,fs.write and --fs-root .;
static View uses the same source and embeds its actual observed output.
Foreign code has two useful lives: as a published snapshot, and as a living document whose latest result is kept in source. They answer different questions and should not be confused.
## main
---
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | Result with the actual string recorded in this source's total slot |
Static snapshot: publish the result.
A static View snapshot embeds executed output in HTML. Explicit writeback
instead keeps a selected value in this source file's named slot. A caller should
choose that file effect deliberately: this main needs process.exec and fs.write,
with --fs-root pointing at the repository. Reading the page alone does not write.
`marqdo view output` executes the document's cells and embeds their real output in the generated HTML. The `.mq.md` source is not modified. This is the default choice for user documentation, reviews and stable links: a reader sees the result without running anything, and the source stays clean.
The amount is computed by the native programming guide, then the shared example
uses Node Intl only for locale-aware currency formatting. Its persistent kernel
is closed before this page receives the result. No Python process, foreign file
writer or duplicated arithmetic participates. The exact returned string is then
passed to lib/writeback, so the file operation remains a Marqdo capability.
The static snapshot is a point-in-time artifact. If an external service changes, rebuild it; do not treat it as a live dashboard.
The total slot below is actual recorded output. Repeated execution replaces that
same key rather than appending another slot. An unavailable interpreter or failed
formatting returns before the write; native write-policy or IO faults retain
their diagnostics. Writeback persists this selected ordinary value, not a live
kernel or arbitrary process memory. Review source changes with the repository's
normal diff workflow. For published docs use a static snapshot unless a living
auditable source result is intended, as it explicitly is in this demonstration.
---
We **amount = [Compute the native amount](guide.计算金额(单价=190, 数量=5))**.
Writeback: keep the result in the source.
`report` =
For an exploratory or audit document, pair `foreign` with `lib/writeback`. `writeback.record` writes a keyed output slot next to the call. The slot is visible in the Markdown source, can be reviewed in a diff and can be committed with the document.
| total | average |
|---|---|
| `amount` | `amount` |
| Step | What it does |
|------|--------------|
| Foreign run / Foreign session | Execute the foreign code and return a Marqdo value |
| Writeback record | Persist that value in a named slot |
| git diff | Review the changed result like any other document change |
A real writeback in this page.
This page is itself the runnable example. The code fence below is ordinary Marqdo data; `foreign.run` executes it, then `writeback.record` writes the `total` slot into this source file.
`写回代码` =
```python
print(950)
```
**结果 = > foreign.run code=`写回代码`**
**> writeback.record value=`结果` key="total"**
We **formatted = [Use only the actual Intl presentation gap](formatter.format_summary(report=`report`))**.
1. not access(`formatted`, "ok")
Return *formatted* before writing a result slot.
We **text = [Select the actual formatter output](access(access(formatted, "value"), "total"))**.
We **written = [Persist this actual output through Marqdo's native writeback](writeback.record(value=`text`, key="total"))**.
<!-- marqdo-out total
950
CN¥950.00
-->
The generated slot is directly below that call. Re-running the page replaces only the `total` slot; it does not duplicate it.
Writeback is explicit. It is not hidden kernel serialization, does not persist arbitrary kernel memory, and does not touch a file unless the document calls it.
---
Choosing.
For published user docs, use the static snapshot. For a living report that must keep the latest run result in the `.mq.md` itself, use writeback. Both can coexist in one repository: static pages for readers, writeback examples for authors and audits.
After the explicit native file operation, return *[The persisted formatted value](result.ok(value=`text`))*.
+27 -41
View File
@@ -1,58 +1,44 @@
---
markup: occlusion
title: 外联执行结果的持久化
description: 静态快照与自写回 —— 外联结果在进程退出后如何保留
导入:
外联: lib/外联.mq.md
writeback: lib/writeback.mq.md
title: 用固有写回保存真实的格式化结果
import:
指南: ../../doc/library-development.mq.md
例程: ../../examples/foreign-native-report/index.mq.md
写回: lib/writeback.mq.md
结果: lib/result.mq.md
---
# main
调用 main 需要 process.exec,fs.write,并将 --fs-root 指向仓库根目录。这是本页明确声明的文件修改,普通阅读不会写文件。静态 View 与可调用函数使用同一份源码,输出来自实际执行。
外联代码有两种有用的形态:发布快照,以及把最新结果保留在源码里的活文档。两者回答不同的问题,不应混为一谈。
## main
---
| 返回 | 类型 | 说明 |
|---|---|---|
| result | map | Result:保存到本页 total 槽位的实际文本 |
静态快照:发布结果。
静态快照把执行输出嵌入 HTML;显式写回则把选定结果保存在源码的命名槽位里。本页正是需要后者的活文档示例,调用者应先知道会修改本页,再授予 fs.write。写回后的值可以在普通 Markdown 源码和差异中审阅,不依赖仍存活的外部进程。
`marqdo view output` 执行文档单元,并把真实输出嵌入生成的 HTML。`.mq.md` 源码不会被修改。这是用户文档、评审和稳定链接的默认选择:读者不需要运行任何东西就能看到结果,源码保持干净。
金额由原生开发指南计算,随后共享例程只调用 Node Intl 完成固有库尚未提供的本地化货币展示。例程检查每次求值并关闭其持久内核,调用者得到的已经是普通文本。没有 Python 编排,也没有外部文件写入器;保存动作继续使用 Marqdo 的 writeback。示例的金额为 950,当前区域设置下的文本为 ¥950.00,真实输出会成为下面的 total 槽位内容。
静态快照是某个时间点的产物。外部服务变化后重新构建即可,不要把它当成实时看板。
再次执行替换同一个键,不重复添加槽位。若解释器不可用或格式化失败,在写文件之前就返回真实失败;固有文件策略或 IO 错误也保留诊断。这里持久化的是已经选定的文本,不保存任意内核内存,也不能根据它重建活资源句柄。完成后可用正常的仓库差异审阅流程检查变更,再决定是否随文档提交。
---
面向读者的发布页面通常选择静态快照;明确需要在源码中保存最新运行结果的审计文档才使用写回。两者可以共存,但静态导出如果执行了本页,仍会尊重这个显式写回操作。权限不会因为文档外观而被隐式扩张,依赖步骤也不能在失败后继续伪造结果。
自写回:把结果留在源码里。
先**金额 = [计算需要展示的原生金额](指南.计算金额(单价=190, 数量=5))**.
对于探索性或审计型文档,把 `外联` 和 `lib/writeback` 搭配使用。`writeback.record` 会在调用点附近写入带 key 的输出槽位。槽位在 Markdown 源码中可见,可以在 diff 中审阅,也可以随文档提交。
`report` =
| 步骤 | 作用 |
|------|------|
| 外联执行 / 外联会话 | 执行外联代码并返回 Marqdo 值 |
| 写回记录 | 把该值持久化到命名槽位 |
| git diff | 像审阅普通文档改动一样审阅结果 |
| total | average |
|---|---|
| `金额` | `金额` |
这一页本身就是完整示例。
这一页就是可运行的示例。下面的代码围栏是普通 Marqdo 值;`外联.运行` 执行它,然后 `writeback.record` 把 `total` 槽位写进本页源码。
`写回代码` =
```python
print(950)
```
**结果 = > 外联.运行 代码=`写回代码`**
**> writeback.record value=`结果` key="total"**
再**格式结果 = [只把本地化展示交给 Intl](例程.format_summary(report=`report`, locale="zh-CN"))**。
1. not access(`格式结果`, "ok")
返回*格式结果*,失败时不进入文件写回。
我们**正文 = [读取实际格式化文本](access(access(格式结果, "value"), "total"))**。
随后**写入 = [使用固有写回能力保存真实文本](写回.record(value=`正文`, key="total"))**。
<!-- marqdo-out total
950
¥950.00
-->
生成的槽位就在这次调用下方。再次运行只替换 `total` 槽位,不会重复添加。
自写回是显式动作。它不是隐藏的内核序列化,不持久化任意内核内存,也不会在文档没有调用时修改文件。
---
如何选择。
面向用户的发布文档用静态快照;需要把最新执行结果留在 `.mq.md` 本身里的活文档用自写回。两者可以共存于同一个仓库:静态页面给读者,自写回示例给作者和审计。
写回完成后返回*[已经保存的格式化文本](结果.ok(value=`正文`))*。
+28 -2
View File
@@ -6,6 +6,10 @@ description: v2.2.0 marqdo call runs a function inside any .mq.md. The explanati
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | None; execute this page's documented steps in order and observe their output or stated effects |
v2.2.0 adds one command: `marqdo call`. It adds no Markdown marker and no skill system. An ordinary .mq.md file is already a document, a piece of knowledge and a program. What was missing is a way to call one of its functions from outside.
The usual command runs the whole file. Functions inside it run only when the entry calls them. Trying one calculation meant writing an entry, a print and a check. The smallest unit you can execute is now a function.
@@ -42,18 +46,40 @@ After editing a function, call it again with the inputs that failed. A passing c
# calculator
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | Object/type namespace for the child functions below; call the intended child directly |
This is an ordinary Marqdo document.
It holds a little reusable arithmetic.
## add
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| a | any | required | Left operand supplied to this calculation |
| b | any | required | Right operand supplied to this calculation |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | The value explicitly returned below; conditional returns follow the selected branch |
Add the numbers `a` and `b`.
*[result](a + b)*
After these steps, return *[result]((`a` + `b`))*.
## multiply
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| a | any | required | Left operand supplied to this calculation |
| b | any | required | Right operand supplied to this calculation |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | The value explicitly returned below; conditional returns follow the selected branch |
Multiply the numbers `a` and `b`.
*[result](a * b)*
After these steps, return *[result]((`a` * `b`))*.
+28 -2
View File
@@ -6,6 +6,10 @@ description: v2.2.0 的 marqdo call 让任意 .mq.md 里的函数可以带参数
# main
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 空值;按顺序执行本页的演示步骤,观察输出或已说明的副作用。 |
v2.2.0 增加一条命令:`marqdo call`。它不新增 Markdown 标记,也不另建一套技能系统。一个普通的 .mq.md 本来就是文档、知识和程序。缺的是从外部直接调用其中某个函数。
以前日常用法是运行整份文件。入口对象跑完,里面的函数要等文件自己去调用。要试验一个计算,往往得再写入口、打印和检查。现在最小可执行单位是函数。
@@ -42,18 +46,40 @@ marqdo call public/features/22-函数调用.mq.md 计算器.加法 a=2 b=3
# 计算器
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 供下述方法复用的对象或类型命名空间;直接调用所需的子函数。 |
这是一个普通的 Marqdo 文档。
其中包含一些可以复用的数学计算知识。
## 加法
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| a | any | 必填 | Left operand supplied to this calculation |
| b | any | 必填 | Right operand supplied to this calculation |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 下文显式返回的计算值或结构化结果;分支存在时遵循实际命中的分支。 |
计算两个数字`a`和`b`的和。
*[计算结果](a + b)*
完成上述步骤后,返回*[计算结果]((`a` + `b`))*。
## 乘法
| 参数 | 类型 | 默认 | 说明 |
|---|---|---|---|
| a | any | 必填 | Left operand supplied to this calculation |
| b | any | 必填 | Right operand supplied to this calculation |
| 返回 | 类型 | 说明 |
|---|---|---|
| result | any | 下文显式返回的计算值或结构化结果;分支存在时遵循实际命中的分支。 |
计算两个数字`a`和`b`的积。
*[计算结果](a * b)*
完成上述步骤后,返回*[计算结果]((`a` * `b`))*。