原计划的"翻转默认 + 旧形硬报错"不可行:`[键](集合)` 与 `[描述](实现)` 外形相同, 翻转会让 506 处取元静默变义,硬报错又拿不到证据。改为**按文件声明**。 一、per-file markup - frontmatter `markup: occlusion`(或 `标记: 遮蔽`)决定该文件模式 - load.rs 先取模式,用 parse::expr::with_markup(线程局部压栈)包住解析 - 同一进程可同时加载两种模式的文件(imports);MARQDO_MARKUP 仍作进程默认 - 好处:迁移逐文件、可增量、可回滚,不存在半迁移的静默错义 二、`marqdo migrate labels [--write|--json]` 改写规则: [键](集合) → [键](access(集合, "键")) [`名`](集合) → [名](access(集合, `名`)) - [项](集合) → - [项](遍历 集合 为 项) 安全模型:改写 → 重解析 → 证明等价 → 才写 - 原文件按 classic 解析得 AST-A;改写后按 occlusion 解析得 AST-B - 反归一化 B(标签与键同名且 payload 为 access 的遮蔽还原成 Index) - Debug(A) == Debug(反归一化(B))(抹掉行号)才写入,否则原文件一字不动 只改代码行(LineKind::Code);表格行只改 **…** 单元格,散文 markdown 链接不碰; 链式索引由内向外改写,已迁移的有守卫不二次改。 三、access 提升为宿主函数 HostFn::Access(u16 id 212) 与旧取元逐字一致(Map 按键 / List 1-based / 缺键 missing map key); 进 HostFn 是为了两个后端共用同一实现,interp 里的特例已删除。 四、顺带修的两处真实语义缺陷 1. payload 继承外层"文本模式":`f text=[k](access(c,k))` 中 access 不被当调用 → `()` 内永远允许 callee(args) 2. 遮蔽形状判定过宽:`[问候] ([格式化] "Bo")` 误判为遮蔽 → 要求 `]` 后紧跟 `(`,否则落回方括号调用 五、全仓迁移结果 migrate labels: 256 index, 23 loop, 80 file written;0 文件被等价性拒绝; 迁移后 cargo test --release → 604 passed / 0 failed;再跑迁移幂等。 等价性检查先后抓出 4 类改写 bug(表格散文链接、payload 文本模式、链式索引、 遮蔽与方括号调用混淆),每类都是先拦下再修工具,未污染文件。 测试:6 个单测 + 3 个集成(干跑不写盘 / 写入后运行输出不变且幂等 / 等价失败文件不动);全量 604 passed / 0 failed(M3 后 595 + 9);双后端一致。 文档:doc/design/change-m7-migration.md
154 lines
4.0 KiB
Markdown
154 lines
4.0 KiB
Markdown
---
|
|
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
|
|
import foreign:lib/foreign.mq.md
|
|
---
|
|
|
|
# main
|
|
|
|
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.
|
|
|
|
---
|
|
|
|
Probe the interpreter first. The result is ordinary Marqdo data.
|
|
|
|
**python = > foreign.probe lang="python"**
|
|
|
|
> print text=[ok](access(`python`, "ok"))
|
|
> print text=[version](access(`python`, "version"))
|
|
|
|
---
|
|
|
|
Marqdo values into a kernel.
|
|
|
|
The `订单` table is a normal Marqdo value. `foreign.session_set` copies it into the Python kernel as JSON; no shell and no stdin.
|
|
|
|
`订单` =
|
|
|
|
| @ | 区域 | 金额 |
|
|
|---|------|------|
|
|
| 1 | 华东 | 120 |
|
|
| 2 | 华东 | 300 |
|
|
| 3 | 华南 | 90 |
|
|
| 4 | 华南 | 140 |
|
|
| 5 | 华北 | 220 |
|
|
| 6 | 西部 | 80 |
|
|
|
|
**内核会话 = > foreign.session_open lang="python"**
|
|
|
|
**> 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 `分析单元`.
|
|
|
|
`分析内核` =
|
|
|
|
| @ | 名称 | 语言 |
|
|
|---|------|------|
|
|
| 1 | 分析 | python |
|
|
| 2 | 复核 | python |
|
|
|
|
`分析单元` =
|
|
|
|
| @ | 单元 | 内核 |
|
|
|---|------|------|
|
|
| 1 | 计算 | 分析 |
|
|
| 2 | 复核 | 复核 |
|
|
|
|
`初始值` =
|
|
|
|
| 内核 | 名称 | 值 |
|
|
|------|------|----|
|
|
| 分析 | orders | `订单` |
|
|
| 复核 | orders | `订单` |
|
|
|
|
`会话` = > foreign.session kernels=`分析内核` cells=`分析单元` values=`初始值`
|
|
|
|
`计算` =
|
|
```python
|
|
import json
|
|
import statistics
|
|
|
|
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))
|
|
```
|
|
|
|
`复核` =
|
|
```python
|
|
import json
|
|
|
|
print(json.dumps({
|
|
"kernel": "复核",
|
|
"count": len(orders),
|
|
"total": round(sum(float(row["金额"]) for row in orders), 2),
|
|
}, ensure_ascii=False))
|
|
```
|
|
|
|
`查看` 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.
|
|
|
|
The important language point:
|
|
|
|
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.
|
|
|
|
There is no separate notebook CLI, no stdin requirement in this example, and no hidden language switch.
|