From 30cd39fd8ff3151d548731f56234eeb2fb557a47 Mon Sep 17 00:00:00 2001 From: cflmy Date: Tue, 4 Aug 2026 17:26:09 +0800 Subject: [PATCH] Add diagnostics spans, CLI/view input feeding, and len/str/int builtins. Unify bytecode errors with path:line:col, support --stdin-file and live view preset stdin, and ship the S0 standard library with gold coverage. Co-authored-by: Cursor --- README.md | 2 + doc/README.md | 1 + doc/design/call-arguments.md | 3 +- doc/design/keywords.md | 11 +- doc/roadmap/next-phase.md | 219 +++++++++++++++++++++++++++++++++ public/keywords/input.mq.md | 12 ++ public/keywords/stdlib.mq.md | 22 ++++ src/builtin.rs | 32 +++++ src/bytecode/compile.rs | 125 ++++++++++++++++--- src/bytecode/mod.rs | 13 +- src/bytecode/vm.rs | 164 ++++++++++++++++-------- src/input_feed.rs | 85 +++++++++++++ src/interp.rs | 71 ++++++++--- src/lib.rs | 21 ++-- src/main.rs | 16 ++- src/view/html.rs | 54 ++++++++ src/view/mod.rs | 20 ++- src/view/output.rs | 2 +- src/view/render.rs | 2 + tests/errors/bad-int.mq.md | 7 ++ tests/errors/div-zero.mq.md | 9 ++ tests/gold.rs | 137 +++++++++++++++++++-- tests/keywords/input-stdin.txt | 1 + tests/keywords/input.mq.md | 10 ++ tests/keywords/stdlib.mq.md | 36 ++++++ 25 files changed, 964 insertions(+), 111 deletions(-) create mode 100644 doc/roadmap/next-phase.md create mode 100644 public/keywords/input.mq.md create mode 100644 public/keywords/stdlib.mq.md create mode 100644 src/builtin.rs create mode 100644 src/input_feed.rs create mode 100644 tests/errors/bad-int.mq.md create mode 100644 tests/errors/div-zero.mq.md create mode 100644 tests/keywords/input-stdin.txt create mode 100644 tests/keywords/input.mq.md create mode 100644 tests/keywords/stdlib.mq.md diff --git a/README.md b/README.md index eae411d..e24c439 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ Marqdo:Markdown **标记即语法**。源文件 `.mq.md` 既是文稿也是程序。 +欢迎访问[marqdo官方网站](https://www.marqdo.com/)阅读marqdo更多特性 + 语法宪法:[doc/design/markdown-mapping.md](doc/design/markdown-mapping.md) · 调用实参:[call-arguments.md](doc/design/call-arguments.md) · 测试布局:[examples-and-tests.md](doc/design/examples-and-tests.md) · 用户站:[user-site.md](doc/design/user-site.md) · 浏览:[view.md](doc/design/view.md) · 金样例:[tests/](tests/) · 用户文档:[public/](public/) --- diff --git a/doc/README.md b/doc/README.md index b7149ad..ad7a643 100644 --- a/doc/README.md +++ b/doc/README.md @@ -32,6 +32,7 @@ | [design/pipeline-debug.md](design/pipeline-debug.md) | **流水线 `--dump-*` 可视调试** | | [dev-setup.md](dev-setup.md) | 开发环境配置(Rust M0) | | [roadmap/interpreter.md](roadmap/interpreter.md) | 正经解释器路线图 | +| [roadmap/next-phase.md](roadmap/next-phase.md) | **下一阶段:input / 诊断 / 标准库 / view 调试** | | [research/okf-and-marqdo.md](research/okf-and-marqdo.md) | OKF 调研 | 金样例与集成测试:[`tests/`](../tests/)。面向访客的可执行文档:[`public/`](../public/)。Spike 存档:[`spike/`](../spike/)。 diff --git a/doc/design/call-arguments.md b/doc/design/call-arguments.md index ad45142..1864769 100644 --- a/doc/design/call-arguments.md +++ b/doc/design/call-arguments.md @@ -32,7 +32,8 @@ positional ::= value_token - `value`(具名右侧):可含空格与 `` `var` `` 插值,直到下一个 `ident=` 边界。 - `value_token`(位置):空白分隔的一个词法单元——数字、`` `名` ``、标识/文本词;**不含空格**。多词字符串请用具名,例如 `> print text=Hello World!`。 -- 内置 `print` 形参名为 `text`:`> print Hi` 与 `> print text=Hi` 等价。 +- 内置 `print` 形参名为 `text`:`> print Hi` 与 `> print text=Hi` 等价。 +- 内置 `len` / `str` / `int` 形参名为 `value`。 --- diff --git a/doc/design/keywords.md b/doc/design/keywords.md index d2a124b..83ddd32 100644 --- a/doc/design/keywords.md +++ b/doc/design/keywords.md @@ -40,11 +40,18 @@ |------|------|----------| | `print` | 写到 stdout(副作用) | `> print text=Hello` 或 `> print Hello`(位置实参,见 [call-arguments.md](call-arguments.md)) | | `input` | 从 stdin 读一行(返回文本) | `*`name` = > input prompt=Name: *` | +| `len` | 文本或表的长度 | `*`n` = > len `s`*` 或 `> len value=`s`` | +| `str` | 转为显示文本 | `*`t` = > str 42*` | +| `int` | 转为整数 | `*`i` = > int 7*` / `> int True` / 文本十进制 | 参数约定(v0): - `print`:具名实参 `text`(必填);日后可扩展 `end` 等。 -- `input`:可选 `prompt`;返回用户输入字符串(不含换行)。 +- `input`:可选 `prompt`;返回用户输入字符串(不含换行)。 +- `len` / `str` / `int`:形参名均为 `value`(可位置传入)。 + - `len`:文本按 Unicode 标量计数;表/列表按元素数。 + - `str`:与 `print` 相同的显示规则。 + - `int`:`Int` 原样;`Bool` → 0/1;文本 trim 后按十进制解析,失败则诊断。 旧文档中的中文 `print` **废弃**,统一为 `print`。 @@ -81,4 +88,4 @@ ## 6. 后续可增(未批准) -`len`、`str`、`int`、`float`、`range`;以及 `break`/`continue`(若循环需要)。增加前开短 ADR。 +`float`、`type`、`range`;以及 `break`/`continue`(若循环需要)。增加前开短 ADR。 diff --git a/doc/roadmap/next-phase.md b/doc/roadmap/next-phase.md new file mode 100644 index 0000000..3c681f1 --- /dev/null +++ b/doc/roadmap/next-phase.md @@ -0,0 +1,219 @@ +# 下一阶段:从玩具到可用语言 + +| | | +|---|---| +| 状态 | **进行中**(P0 / P1a / P2-S0 / P1b 完成;下一:P2-S1 或 P3-D0) | +| 日期 | 2026-08-04 | +| 目标 | 交互 I/O、可信诊断、可生长标准库;view 渐进成为调试宿主 | +| 相关 | [interpreter.md](interpreter.md) · [keywords.md](../design/keywords.md) · [pipeline-debug.md](../design/pipeline-debug.md) · [view.md](../design/view.md) | + +--- + +## 0. 现状快照(决策依据) + +| 能力 | 现状 | 结论 | +|------|------|------| +| **`print`** | 树遍历 + 字节码均可用 | 够用 | +| **`input`** | CLI 双后端 + `--stdin-file`;live view 预置输入表单;无队列时 capture 仍拒绝 | **P1a + P1b 完成** | +| **诊断** | 语句级 `path:line:col: message`;字节码指令带 `spans`;errors 金样例双后端断言位置 | **P0 完成** | +| **view** | 结构 + 一次执行 + 源码;注释已 Markdown 渲染;无暂停/断点 | **文档浏览器,不是调试器** | +| **标准库** | S0:`len` / `str` / `int` 双后端可用;尚无官方 `lib/` | **P2-S0 完成** | +| **模块** | `> file.mq.md` 顶层函数合并,无命名空间 | 短期可继续用 | + +用户点名的三件事都对,但**不能并行硬开**:断点调试依赖「可信位置 + 可暂停执行」;标准库依赖稳定运行时与 I/O;`input` 在 CLI 与 view 是两条路径。 + +--- + +## 1. 轻重缓急(总序) + +```text +P0 诊断加固(短、撬动后续一切) +P1 输入补完(先 CLI,再 view) +P2 最小标准库(内置 + 可选 .mq.md 库) +P3 view 调试(断点 / 单步)—— 在 P0 之后分期,勿一上来做 DAP +``` + +| 优先级 | 主题 | 为何此刻 | 不做的代价 | +|--------|------|----------|------------| +| **P0** | 诊断完备度 | 便宜;给金样例、字节码、调试打地基 | 断点停错行;双后端错误体验分裂 | +| **P1** | `input` | 「真语言」最低交互面;已有半截实现 | 只能写纯计算/打印 demo | +| **P2** | 标准库 | 无库则用户无法写稍大程序 | 人人自造轮子、示例膨胀 | +| **P3** | 前端调试 | 差异化(文档即调试);成本最高 | view 仍停留在「跑完看 stdout」 | + +**原则:** 先让 **CLI 树遍历**成为完备参考实现;字节码跟进同语义;view 只接「已有事件/钩子」,不单独发明第二套语义。 + +--- + +## 2. P0 — 诊断与错误模型(约 3–7 天) + +### 目标 + +所有用户可见失败统一为: + +```text +path:line:col: message +``` + +树遍历与字节码**同文案、同位置粒度(至少语句级)**;金样例断言位置而不只子串。 + +### 工作项 + +1. **统一出口** + - 解析侧尽量只产 `Diagnostic`(减少裸 `bail!("7:1: …")`)。 + - 字节码编译/VM:错误携带 `Span`(或指令 → 源行映射)。 +2. **位置粒度** + - 短期:保持「语句起点」亦可;中期:为 `Expr`/`CallExpr` 补 `span`,让「未定义名」落在标识符上。 +3. **金样例** + - `tests/errors/*`:断言完整 `path:line:col` 前缀(或相对路径规范化后的形式)。 + - 补:类型错误、除零、循环条件、capture 下 `input` 等。 +4. **文档** + - 更新 [pipeline-debug.md](../design/pipeline-debug.md):错误码/严重级别可列为 P0.5(可选 `E0001` 一类,不阻塞)。 + +### 完成定义 + +- `cargo test`:errors 金样例含位置。 +- 同一 `.mq.md` 在 `--backend tree|bytecode` 下诊断一致(允许实现细节差异写进豁免表)。 +- view 执行失败区仍可读,路径无 `\\?\`。 + +### 刻意不做 + +- IDE DAP、完整堆栈 UI、sourcemap 可视化编辑器。 + +--- + +## 3. P1 — 输入(约 1–2 周) + +分两轨:**CLI 必须先绿**;**view 交互后做**。 + +### P1a — CLI 完备(高优先级) + +| 项 | 说明 | +|----|------| +| 树遍历 | 已实现 stdin;补 `tests/keywords/input.mq.md`(用管道喂入) | +| 字节码 | 增加 `Op::Input`(或等价 host call);与树同语义;capture 下同样拒绝或改走注入 API | +| 文档/用户站 | `public/keywords/input.mq.md`;说明「view 默认无交互 stdin」 | + +完成定义:`echo Alice | marqdo run …` 与字节码后端输出一致;金样例稳定。 + +### P1b — view / 静态站中的输入(中优先级,接在 P1a 后) + +当前 `run_file_capture` **不能**接真实 stdin——这是设计,不是偶然 bug。 + +推荐路径(由易到难): + +1. **脚本化注入(无 UI)** + - `marqdo run --stdin-file answers.txt` 或 frontmatter/`---` 旁测数据。 + - view:执行前提供「预置输入行」文本框,一次性灌入队列。 +2. **实时交互(live view)** + - 执行到 `input` → 暂停 → HTTP/WS 返回 prompt → 用户提交一行 → 继续。 + - 依赖 P3 的「可暂停执行」钩子;可与断点共用会话模型。 +3. **静态 `view output`** + - 无服务器则只支持预置输入或跳过并标明 `input skipped`。 + +完成定义:live `marqdo view` 下至少一个带 `input` 的示例可点按跑通;静态导出行为有文档。 + +--- + +## 4. P2 — 标准库(约 2–3 周,可与 P1b/P3 前半重叠) + +### 分层 + +```text +L0 内置函数(解释器内) len / str / int / float? / type? +L1 标准库模块(.mq.md) 文本、表/集合小工具;官方路径如 lib/ 或 std/ +L2 生态约定 catalog 收录;用户站专章 +``` + +### 建议落地顺序 + +| 迭代 | 内容 | 依据 | +|------|------|------| +| **S0** | `len`(文本/表)、`str`、`int` | keywords.md 已列候选;字节码已有闲置 `Op::Len` 可接线 | +| **S1** | 表工具:取列、行数;字符串 trim/split(先内置或纯 mq 实现二选一) | 文档型语言高频 | +| **S2** | 官方 `lib/` 或 `std/` + 导入约定(仍可用 frontmatter;或日后 `> std/text.mq.md`) | 可生长而不破现有导入 | +| **S3** | 错误类型约定(如非法 `int` 文本 → 诊断) | 依赖 P0 | + +### 完成定义(S0) + +- 金样例 + 双后端;`public/` 有对应介绍页。 +- 不引入 `if`/`while` 英文关键字(控制流仍归 Markdown 标记)。 + +### 刻意不做(本阶段) + +- 完整面向对象、包管理器、异步 I/O、HTTP 客户端(可列远期)。 + +--- + +## 5. P3 — 前端成为调试机构(约 3–6 周,分里程碑) + +**不要从 DAP/VS Code 插件开干。** view 已有 Structure / Source / Execution,是天然宿主。 + +```mermaid +flowchart LR + P0loc[P0 可信位置] --> Pause[语句边界暂停钩子] + Pause --> Sess[调试会话 API] + Sess --> UI[view 断点与单步 UI] + UI --> DAP[可选 DAP 适配] +``` + +| 里程碑 | 交付 | 依赖 | +|--------|------|------| +| **D0** | 语句级源映射(树优先;字节码指令→行) | P0 | +| **D1** | 运行时钩子:进入语句前可 pause;`--trace-eval` 结构化事件(JSON 行) | D0 | +| **D2** | 进程内调试会话:设/清断点、continue、step over(先树遍历) | D1 | +| **D3** | **view debug 模式**:源码行点击断点;高亮当前行;侧栏 locals/栈;与文档模式切换 | D2 + 少量 JS 或服务端推进 | +| **D4** | 与 P1b 合流:`input` 暂停复用同一会话 | P1b | +| **D5**(可选) | DAP 薄适配,供外部 IDE | D2 稳定后 | + +### 完成定义(D3 即「可用调试」) + +- 用户能在 `marqdo view` 对某 `.mq.md` 设断点、单步、看变量。 +- 文档站默认模式不变(一次执行);debug 为显式开关。 +- 断点命中行与 P0 诊断行一致(允许文档说明「语句起点」语义)。 + +### 风险与边界 + +- 静态 `gh-pages` **无法**真断点(无常驻 VM);调试仅 **live view**(或未来 WASM 解释器)。 +- 字节码调试可晚于树遍历一个迭代。 + +--- + +## 6. 推荐排期(可执行流程) + +按「每段都可合并、可演示」切周,避免大爆炸: + +| 周序 | 主题 | 可合并交付 | +|------|------|------------| +| **W1** | P0 诊断统一 + errors 金样例加固 | 双后端错误位置一致 | +| **W2** | P1a `input` 字节码 + 管道金样例 + 用户文档 | CLI 交互闭环 | +| **W3** | P2-S0 `len`/`str`/`int` + 样例 | 「有点像语言」的第一批库 | +| **W4** | P1b 预置输入(view 文本框 / `--stdin-file`) | view 可跑含 input 的 demo | +| **W5–W6** | P3-D0–D2 暂停与会话 | 无 UI 也能用 API/CLI 单步 | +| **W7–W8** | P3-D3 view 断点 UI + D4 与 input 暂停 | **调试机构**第一版 | +| **并行/随后** | P2-S1/S2 扩库;P3-D5 DAP | 按反馈加 | + +若人力紧:**砍掉顺序为 D5 → S2 花活 → 字节码调试**;**不可砍:P0、P1a、P2-S0、D3 之前的 D0–D2。** + +--- + +## 7. 验收总表(「真正可用」最低线) + +1. **I/O**:CLI 上 `print`/`input` 双后端一致;view 至少支持预置或交互输入之一。 +2. **错误**:失败必有稳定位置;常见错误有金样例;用户站不靠「瞎猜 stderr」。 +3. **库**:至少 `len`/`str`/`int` 可用并有文档。 +4. **调试**:live view 可断点 + 单步 + 看当前绑定(树遍历即可宣称 v1)。 +5. **不破坏**:现有 `tests/` 金样例与 `public/` 用户站回归绿。 + +--- + +## 8. 文档与工程动作(开工时) + +1. 本文件定为「下一阶段宪法」;开工后把各 P 拆到 `doc/design/` 子设计(`input-interactive.md`、`stdlib.md`、`view-debug.md`)。 +2. 每个里程碑:金样例 → 实现 → `public/` 更新 →(若行为用户可见)再发版 / 刷 `gh-pages`。 +3. 版本建议:P1a+P0 可打 `v0.0.4`;P2-S0 打 `v0.0.5`;P3-D3 打 `v0.1.0`(「可用调试」语义升级)。 + +--- + +## 9. 一句话路线 + +**先把错误说准、把输入在 CLI 做完,再长出最小标准库;最后把现有 view 升级成可暂停的调试宿主——而不是先做 IDE 插件或大而全标准库。** diff --git a/public/keywords/input.mq.md b/public/keywords/input.mq.md new file mode 100644 index 0000000..65227a4 --- /dev/null +++ b/public/keywords/input.mq.md @@ -0,0 +1,12 @@ +--- +title: 内置 input +description: 从 stdin 读一行;可选 prompt。可用 --stdin-file 或 live view 预置输入。 +--- + +# main + +CLI 可用管道或 --stdin-file 喂入一行;live marqdo view 在 Execution 区可填预置输入后重跑。静态站点默认无 stdin,会看到 capture 提示。 + +*`name` = > input prompt=Name: * + +> print text=Hello `name`! diff --git a/public/keywords/stdlib.mq.md b/public/keywords/stdlib.mq.md new file mode 100644 index 0000000..17688a6 --- /dev/null +++ b/public/keywords/stdlib.mq.md @@ -0,0 +1,22 @@ +--- +title: 内置 len / str / int +description: 长度与类型转换(S0 最小标准库) +--- + +# main + +len 接受文本(按 Unicode 标量计)或表/列表: + +*`s` = hello* + +*`n` = > len `s`* + +> print text=`n` + +str 把值转成显示文本;int 解析整数(或把 True / False 当成 1 / 0): + +*`t` = > str 7* + +*`i` = > int `t`* + +> print text=`i` diff --git a/src/builtin.rs b/src/builtin.rs new file mode 100644 index 0000000..7a5445e --- /dev/null +++ b/src/builtin.rs @@ -0,0 +1,32 @@ +//! Built-in helpers shared by tree-walk and bytecode. + +use crate::value::Value; + +/// `len`: text → Unicode scalar count; list → element count. +pub fn builtin_len(v: &Value) -> Result { + match v { + Value::Text(s) => Ok(s.chars().count() as i64), + Value::List(xs) => Ok(xs.len() as i64), + _ => Err("len needs text or list".into()), + } +} + +/// `str`: display conversion (same surface as `print`). +pub fn builtin_str(v: &Value) -> Value { + Value::Text(v.as_display()) +} + +/// `int`: Int passthrough; Bool → 0/1; Text → parse trimmed decimal. +pub fn builtin_int(v: &Value) -> Result { + match v { + Value::Int(n) => Ok(*n), + Value::Bool(true) => Ok(1), + Value::Bool(false) => Ok(0), + Value::Text(s) => { + let t = s.trim(); + t.parse::() + .map_err(|_| format!("cannot convert to int: {s:?}")) + } + _ => Err("int needs int, bool, or text".into()), + } +} diff --git a/src/bytecode/compile.rs b/src/bytecode/compile.rs index d1d7578..bec7a16 100644 --- a/src/bytecode/compile.rs +++ b/src/bytecode/compile.rs @@ -2,11 +2,13 @@ use anyhow::{bail, Result}; use std::collections::HashMap; +use std::path::Path; use crate::ast::{ Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp, }; use crate::bytecode::{FnChunk, Op, Program}; +use crate::diagnostics::{bail_at, Span}; use crate::value::Value; struct FlatFun { @@ -15,9 +17,10 @@ struct FlatFun { params: Vec, body: Vec, children: Vec, + span: Span, } -pub fn compile_module(module: &Module) -> Result { +pub fn compile_module(path: Option<&Path>, module: &Module) -> Result { let mut flat = Vec::new(); for fun in &module.functions { collect_fun(fun, None, &mut flat); @@ -35,9 +38,10 @@ pub fn compile_module(module: &Module) -> Result { }) .ok_or_else(|| anyhow::anyhow!("bytecode: no entry function"))?; + let path_buf = path.map(|p| p.to_path_buf()); let mut functions = Vec::new(); for i in 0..flat.len() { - functions.push(compile_function(i, &flat)?); + functions.push(compile_function(i, &flat, path_buf.as_deref())?); } Ok(Program { functions, entry }) @@ -51,6 +55,7 @@ fn collect_fun(fun: &Function, parent: Option, flat: &mut Vec) - params: fun.params.clone(), body: fun.body.clone(), children: Vec::new(), + span: fun.span, }); let mut child_ids = Vec::new(); for child in &fun.children { @@ -61,33 +66,39 @@ fn collect_fun(fun: &Function, parent: Option, flat: &mut Vec) - } struct FnCompiler<'a> { + path: Option<&'a Path>, flat: &'a [FlatFun], fn_id: usize, chunk: FnChunk, locals: HashMap, + stmt_span: Span, } -fn compile_function(fn_id: usize, flat: &[FlatFun]) -> Result { +fn compile_function(fn_id: usize, flat: &[FlatFun], path: Option<&Path>) -> Result { let fun = &flat[fn_id]; let mut locals = HashMap::new(); for (i, p) in fun.params.iter().enumerate() { locals.insert(p.clone(), i as u8); } let mut c = FnCompiler { + path, flat, fn_id, chunk: FnChunk { name: fun.name.clone(), params: fun.params.clone(), code: Vec::new(), + spans: Vec::new(), constants: Vec::new(), locals: fun.params.clone(), }, locals, + stmt_span: fun.span, }; for stmt in &fun.body { c.compile_stmt(stmt)?; } + c.stmt_span = fun.span; c.emit(Op::None_); c.emit(Op::Return); let mut names = vec![String::new(); c.locals.len()]; @@ -95,12 +106,18 @@ fn compile_function(fn_id: usize, flat: &[FlatFun]) -> Result { names[*slot as usize] = name.clone(); } c.chunk.locals = names; + debug_assert_eq!(c.chunk.code.len(), c.chunk.spans.len()); Ok(c.chunk) } impl<'a> FnCompiler<'a> { + fn err(&self, message: impl Into) -> anyhow::Error { + bail_at(self.path, self.stmt_span, message) + } + fn emit(&mut self, op: Op) { self.chunk.code.push(op); + self.chunk.spans.push(self.stmt_span); } fn here(&self) -> u16 { @@ -150,23 +167,29 @@ impl<'a> FnCompiler<'a> { return Ok(i); } } - bail!("bytecode: unknown function `{name}`") + Err(self.err(format!("unknown function `{name}`"))) } fn compile_stmt(&mut self, stmt: &Stmt) -> Result<()> { match stmt { - Stmt::Assign { name, value, .. } => { + Stmt::Assign { name, value, span } => { + self.stmt_span = *span; self.compile_expr(value)?; let slot = self.local_slot(name); self.emit(Op::SetLocal(slot)); self.emit(Op::Pop); } - Stmt::Return { value, .. } => { + Stmt::Return { value, span } => { + self.stmt_span = *span; self.compile_expr(value)?; self.emit(Op::Return); } - Stmt::Call { call, .. } => self.compile_call(call, true)?, - Stmt::Branch { arms, .. } => { + Stmt::Call { call, span } => { + self.stmt_span = *span; + self.compile_call(call, true)?; + } + Stmt::Branch { arms, span } => { + self.stmt_span = *span; let mut end_jumps = Vec::new(); for (i, arm) in arms.iter().enumerate() { let is_last = i + 1 == arms.len(); @@ -180,6 +203,7 @@ impl<'a> FnCompiler<'a> { for st in &arm.body { self.compile_stmt(st)?; } + self.stmt_span = *span; if !is_last { let j = self.here(); self.emit(Op::Jump(0)); @@ -195,8 +219,11 @@ impl<'a> FnCompiler<'a> { } } Stmt::While { - condition, body, .. + condition, + body, + span, } => { + self.stmt_span = *span; let loop_start = self.here(); self.compile_expr(condition)?; let exit_j = self.here(); @@ -204,6 +231,7 @@ impl<'a> FnCompiler<'a> { for st in body { self.compile_stmt(st)?; } + self.stmt_span = *span; self.emit(Op::Jump(loop_start)); self.patch(exit_j, Op::JumpIfFalse(self.here())); } @@ -211,8 +239,9 @@ impl<'a> FnCompiler<'a> { item, collection, body, - .. + span, } => { + self.stmt_span = *span; self.compile_expr(&Expr::Var(collection.clone()))?; let coll_slot = self.local_slot(&format!("__coll_{collection}")); self.emit(Op::SetLocal(coll_slot)); @@ -242,6 +271,7 @@ impl<'a> FnCompiler<'a> { self.compile_stmt(st)?; } + self.stmt_span = *span; self.emit(Op::GetLocal(i_slot)); let one = self.add_const(Value::Int(1)); self.emit(Op::Constant(one)); @@ -271,7 +301,7 @@ impl<'a> FnCompiler<'a> { _ => None, }) }) - .ok_or_else(|| anyhow::anyhow!("bytecode: print needs text"))?; + .ok_or_else(|| self.err("print requires text (named or positional)"))?; self.compile_expr(text_expr)?; self.emit(Op::Print); if !as_stmt { @@ -279,7 +309,60 @@ impl<'a> FnCompiler<'a> { } return Ok(()); } - "input" => bail!("bytecode: input not supported"), + "input" => { + let prompt_expr = call + .args + .iter() + .find_map(|a| match a { + Arg::Named { name, value } if name == "prompt" => Some(value), + _ => None, + }) + .or_else(|| { + call.args.iter().find_map(|a| match a { + Arg::Positional(e) => Some(e), + _ => None, + }) + }); + if let Some(e) = prompt_expr { + self.compile_expr(e)?; + } else { + let i = self.add_const(Value::Text(String::new())); + self.emit(Op::Constant(i)); + } + self.emit(Op::Input); + if as_stmt { + self.emit(Op::Pop); + } + return Ok(()); + } + "len" | "str" | "int" => { + let name = call.callee.as_str(); + let value_expr = call + .args + .iter() + .find_map(|a| match a { + Arg::Named { name, value } if name == "value" => Some(value), + _ => None, + }) + .or_else(|| { + call.args.iter().find_map(|a| match a { + Arg::Positional(e) => Some(e), + _ => None, + }) + }) + .ok_or_else(|| self.err(format!("{name} requires value")))?; + self.compile_expr(value_expr)?; + self.emit(match name { + "len" => Op::Len, + "str" => Op::Str, + "int" => Op::Int, + _ => unreachable!(), + }); + if as_stmt { + self.emit(Op::Pop); + } + return Ok(()); + } _ => {} } @@ -303,11 +386,14 @@ impl<'a> FnCompiler<'a> { self.compile_expr(positionals[pos_i])?; pos_i += 1; } else { - bail!("bytecode: missing argument for parameter `{p}`"); + return Err(self.err(format!("missing argument for parameter `{p}`"))); } } if pos_i < positionals.len() { - bail!("bytecode: too many positional arguments"); + return Err(self.err(format!( + "too many positional arguments ({} extra)", + positionals.len() - pos_i + ))); } self.emit(Op::Call(fid as u16, params.len() as u8)); if as_stmt { @@ -336,7 +422,7 @@ impl<'a> FnCompiler<'a> { .locals .get(name) .copied() - .ok_or_else(|| anyhow::anyhow!("bytecode: undefined variable `{name}`"))?; + .ok_or_else(|| self.err(format!("undefined variable `{name}`")))?; self.emit(Op::GetLocal(slot)); } Expr::Interp(parts) => { @@ -352,9 +438,11 @@ impl<'a> FnCompiler<'a> { self.emit(Op::Constant(i)); } InterpPart::Var(n) => { - let slot = self.locals.get(n).copied().ok_or_else(|| { - anyhow::anyhow!("bytecode: undefined variable `{n}`") - })?; + let slot = self + .locals + .get(n) + .copied() + .ok_or_else(|| self.err(format!("undefined variable `{n}`")))?; self.emit(Op::GetLocal(slot)); } } @@ -375,7 +463,6 @@ impl<'a> FnCompiler<'a> { left, right, } => { - // JumpIfFalse always pops the tested value. self.compile_expr(left)?; let j_false = self.here(); self.emit(Op::JumpIfFalse(0)); diff --git a/src/bytecode/mod.rs b/src/bytecode/mod.rs index 7ca666c..4d06fe9 100644 --- a/src/bytecode/mod.rs +++ b/src/bytecode/mod.rs @@ -6,6 +6,7 @@ mod vm; pub use compile::compile_module; pub use vm::Vm; +use crate::diagnostics::Span; use crate::value::Value; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -33,8 +34,15 @@ pub enum Op { Jump(u16), JumpIfFalse(u16), Print, + /// Pop prompt text, print it (no newline), read one stdin line → push Text. + Input, BuildList(u16), + /// Pop value → push `len` (text/list). Len, + /// Pop value → push display text. + Str, + /// Pop value → push int (or fail). + Int, GetIndex, /// Call function by index; argc values are on the stack (param order). Call(u16, u8), @@ -46,6 +54,8 @@ pub struct FnChunk { pub name: String, pub params: Vec, pub code: Vec, + /// Parallel to `code`: source location for diagnostics / debug. + pub spans: Vec, pub constants: Vec, /// Local slot names (params first). pub locals: Vec, @@ -71,7 +81,8 @@ impl Program { out.push_str(&format!(" const[{i}] = {c:?}\n")); } for (ip, op) in fun.code.iter().enumerate() { - out.push_str(&format!(" {ip:04} {op:?}\n")); + let sp = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1)); + out.push_str(&format!(" {ip:04} {op:?} @{sp}\n")); } } out.push_str("=== marqdo: end bytecode ===\n"); diff --git a/src/bytecode/vm.rs b/src/bytecode/vm.rs index 5381167..9ec90ce 100644 --- a/src/bytecode/vm.rs +++ b/src/bytecode/vm.rs @@ -1,13 +1,20 @@ //! Stack VM for Marqdo bytecode. -use anyhow::{bail, Result}; +use anyhow::Result; +use std::io::Write; +use std::path::{Path, PathBuf}; +use crate::builtin::{builtin_int, builtin_len, builtin_str}; use crate::bytecode::{Op, Program}; +use crate::diagnostics::{bail_at, Span}; +use crate::input_feed::InputFeed; use crate::value::Value; pub struct Vm { + path: Option, capture: bool, pub captured_stdout: String, + input: InputFeed, } struct Frame { @@ -17,20 +24,29 @@ struct Frame { } impl Vm { - pub fn new() -> Self { + pub fn new(path: Option<&Path>) -> Self { Self { + path: path.map(|p| p.to_path_buf()), capture: false, captured_stdout: String::new(), + input: InputFeed::new(false, Vec::new()), } } - pub fn with_capture() -> Self { + pub fn with_capture(path: Option<&Path>) -> Self { Self { + path: path.map(|p| p.to_path_buf()), capture: true, captured_stdout: String::new(), + input: InputFeed::new(true, Vec::new()), } } + pub fn with_stdin(mut self, lines: Vec) -> Self { + self.input = InputFeed::new(self.capture, lines); + self + } + fn emit_line(&mut self, text: &str) { if self.capture { self.captured_stdout.push_str(text); @@ -40,12 +56,33 @@ impl Vm { } } + fn emit_prompt(&mut self, prompt: &str) { + if prompt.is_empty() { + return; + } + if self.capture { + self.captured_stdout.push_str(prompt); + } else { + print!("{prompt}"); + let _ = std::io::stdout().flush(); + } + } + + fn err_at(&self, span: Span, message: impl Into) -> anyhow::Error { + bail_at(self.path.as_deref(), span, message) + } + pub fn run(&mut self, program: &Program) -> Result { let mut stack: Vec = Vec::new(); let entry = program.entry; let arity = program.functions[entry].params.len(); if arity != 0 { - bail!("bytecode: entry function needs arguments"); + let span = program.functions[entry] + .spans + .first() + .copied() + .unwrap_or(Span::new(1, 1)); + return Err(self.err_at(span, "bytecode: entry function needs arguments")); } let mut frames = vec![Frame { fn_idx: entry, @@ -57,9 +94,12 @@ impl Vm { let frame = frames.last_mut().unwrap(); let fun = &program.functions[frame.fn_idx]; if frame.ip >= fun.code.len() { - bail!("bytecode: ip out of range in {}", fun.name); + let span = fun.spans.last().copied().unwrap_or(Span::new(1, 1)); + return Err(self.err_at(span, format!("bytecode: ip out of range in {}", fun.name))); } - let op = fun.code[frame.ip]; + let ip = frame.ip; + let op = fun.code[ip]; + let span = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1)); frame.ip += 1; match op { @@ -84,78 +124,105 @@ impl Vm { fr.slots[slot] = v; } Op::Add => { - let b = pop(&mut stack)?; - let a = pop(&mut stack)?; - stack.push(add(a, b)?); + let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + stack.push(add(a, b).map_err(|m| self.err_at(span, m))?); } - Op::Sub => bin_int(&mut stack, |a, b| a - b)?, - Op::Mul => bin_int(&mut stack, |a, b| a * b)?, + Op::Sub => bin_int(&mut stack, |a, b| a - b).map_err(|m| self.err_at(span, m))?, + Op::Mul => bin_int(&mut stack, |a, b| a * b).map_err(|m| self.err_at(span, m))?, Op::Div => { - let b = pop(&mut stack)?; - let a = pop(&mut stack)?; + let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; match (a, b) { - (Value::Int(_), Value::Int(0)) => bail!("division by zero"), + (Value::Int(_), Value::Int(0)) => { + return Err(self.err_at(span, "division by zero")); + } (Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x / y)), - _ => bail!("`/` needs ints"), + _ => return Err(self.err_at(span, "`/` needs ints")), } } - Op::Negate => match pop(&mut stack)? { + Op::Negate => match pop(&mut stack).map_err(|m| self.err_at(span, m))? { Value::Int(n) => stack.push(Value::Int(-n)), - _ => bail!("unary `-` needs int"), + _ => return Err(self.err_at(span, "unary `-` needs int")), }, Op::Not => { - let v = pop(&mut stack)?; + let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; stack.push(Value::Bool(!v.truthy())); } Op::Equal => { - let b = pop(&mut stack)?; - let a = pop(&mut stack)?; + let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; stack.push(Value::Bool(a == b)); } Op::NotEqual => { - let b = pop(&mut stack)?; - let a = pop(&mut stack)?; + let b = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let a = pop(&mut stack).map_err(|m| self.err_at(span, m))?; stack.push(Value::Bool(a != b)); } - Op::Greater => cmp(&mut stack, |o| o.is_gt())?, - Op::GreaterEqual => cmp(&mut stack, |o| o.is_ge())?, - Op::Less => cmp(&mut stack, |o| o.is_lt())?, - Op::Lessequal => cmp(&mut stack, |o| o.is_le())?, + Op::Greater => cmp(&mut stack, |o| o.is_gt()).map_err(|m| self.err_at(span, m))?, + Op::GreaterEqual => { + cmp(&mut stack, |o| o.is_ge()).map_err(|m| self.err_at(span, m))? + } + Op::Less => cmp(&mut stack, |o| o.is_lt()).map_err(|m| self.err_at(span, m))?, + Op::Lessequal => cmp(&mut stack, |o| o.is_le()).map_err(|m| self.err_at(span, m))?, Op::Jump(t) => { frames.last_mut().unwrap().ip = t as usize; } Op::JumpIfFalse(t) => { - let v = pop(&mut stack)?; + let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; if !v.truthy() { frames.last_mut().unwrap().ip = t as usize; } } Op::Print => { - let v = pop(&mut stack)?; + let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; self.emit_line(&v.as_display()); } + Op::Input => { + let prompt = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let text = prompt.as_display(); + self.emit_prompt(&text); + let line = self.input.read_line().map_err(|e| { + if e.to_string().contains("input is not available") { + self.err_at(span, "input is not available under capture / view") + } else { + e + } + })?; + stack.push(Value::Text(line)); + } Op::BuildList(n) => { let n = n as usize; let mut items = Vec::with_capacity(n); for _ in 0..n { - items.push(pop(&mut stack)?); + items.push(pop(&mut stack).map_err(|m| self.err_at(span, m))?); } items.reverse(); stack.push(Value::List(items)); } - Op::Len => match pop(&mut stack)? { - Value::List(xs) => stack.push(Value::Int(xs.len() as i64)), - _ => bail!("len needs list"), - }, + Op::Len => { + let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let n = builtin_len(&v).map_err(|m| self.err_at(span, m))?; + stack.push(Value::Int(n)); + } + Op::Str => { + let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + stack.push(builtin_str(&v)); + } + Op::Int => { + let v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let n = builtin_int(&v).map_err(|m| self.err_at(span, m))?; + stack.push(Value::Int(n)); + } Op::GetIndex => { - let idx = pop(&mut stack)?; - let list = pop(&mut stack)?; + let idx = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let list = pop(&mut stack).map_err(|m| self.err_at(span, m))?; match (list, idx) { (Value::List(xs), Value::Int(i)) if i >= 0 && (i as usize) < xs.len() => { stack.push(xs[i as usize].clone()); } (Value::List(_), Value::Int(_)) => stack.push(Value::None), - _ => bail!("GetIndex needs list and int"), + _ => return Err(self.err_at(span, "GetIndex needs list and int")), } } Op::Call(fid, argc) => { @@ -163,16 +230,12 @@ impl Vm { let argc = argc as usize; let callee = &program.functions[fid]; if callee.params.len() != argc { - bail!("bytecode: call argc mismatch"); + return Err(self.err_at(span, "call argument count mismatch")); } let mut slots = vec![Value::None; callee.locals.len().max(argc).max(1)]; for i in (0..argc).rev() { - slots[i] = pop(&mut stack)?; + slots[i] = pop(&mut stack).map_err(|m| self.err_at(span, m))?; } - // args were pushed in param order; we popped reverse so slots[0] is last param — fix: - // stack had p0, p1, p2 (top). pop → p2, p1, p0. So assign slots[argc-1-i] or pop into reverse. - // Actually we did: for i in (0..argc).rev() { slots[i] = pop() } - // first pop → slots[argc-1] = top = last param. Good if push order was p0..plast. frames.push(Frame { fn_idx: fid, ip: 0, @@ -192,26 +255,25 @@ impl Vm { } } -fn pop(stack: &mut Vec) -> Result { +fn pop(stack: &mut Vec) -> Result { stack .pop() - .ok_or_else(|| anyhow::anyhow!("bytecode: stack underflow")) + .ok_or_else(|| "bytecode: stack underflow".to_string()) } -fn add(a: Value, b: Value) -> Result { +fn add(a: Value, b: Value) -> Result { match (a, b) { (Value::Int(x), Value::Int(y)) => Ok(Value::Int(x + y)), (Value::Text(x), Value::Text(y)) => Ok(Value::Text(format!("{x}{y}"))), (Value::Text(x), y) => Ok(Value::Text(format!("{x}{}", y.as_display()))), (x, Value::Text(y)) => Ok(Value::Text(format!("{}{y}", x.as_display()))), - // Int + display for interp GetLocal Int + Lit text (Value::Int(x), y) => Ok(Value::Text(format!("{}{}", x, y.as_display()))), (x, Value::Int(y)) => Ok(Value::Text(format!("{}{y}", x.as_display()))), - _ => bail!("`+` needs ints or text"), + _ => Err("`+` needs ints or text".into()), } } -fn bin_int(stack: &mut Vec, f: impl Fn(i64, i64) -> i64) -> Result<()> { +fn bin_int(stack: &mut Vec, f: impl Fn(i64, i64) -> i64) -> Result<(), String> { let b = pop(stack)?; let a = pop(stack)?; match (a, b) { @@ -219,17 +281,17 @@ fn bin_int(stack: &mut Vec, f: impl Fn(i64, i64) -> i64) -> Result<()> { stack.push(Value::Int(f(x, y))); Ok(()) } - _ => bail!("binary op needs ints"), + _ => Err("binary op needs ints".into()), } } -fn cmp(stack: &mut Vec, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<()> { +fn cmp(stack: &mut Vec, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<(), String> { let b = pop(stack)?; let a = pop(stack)?; let ord = match (&a, &b) { (Value::Int(x), Value::Int(y)) => x.cmp(y), (Value::Text(x), Value::Text(y)) => x.cmp(y), - _ => bail!("comparison needs two ints or two texts"), + _ => return Err("comparison needs two ints or two texts".into()), }; stack.push(Value::Bool(pred(ord))); Ok(()) diff --git a/src/input_feed.rs b/src/input_feed.rs new file mode 100644 index 0000000..4b47a32 --- /dev/null +++ b/src/input_feed.rs @@ -0,0 +1,85 @@ +//! Preset stdin lines for `input` (CLI `--stdin-file`, view form, tests). + +use std::collections::VecDeque; +use std::fs; +use std::io::{self, BufRead}; +use std::path::Path; + +use anyhow::{Context, Result}; + +/// Split text into input lines (strips `\n` / `\r\n`; empty → no lines). +pub fn split_stdin_text(text: &str) -> Vec { + if text.is_empty() { + return Vec::new(); + } + text.lines() + .map(|l| l.trim_end_matches('\r').to_string()) + .collect() +} + +pub fn load_stdin_file(path: &Path) -> Result> { + let text = fs::read_to_string(path) + .with_context(|| format!("failed to read stdin file {}", path.display()))?; + Ok(split_stdin_text(&text)) +} + +#[derive(Debug, Clone, Default)] +pub struct InputFeed { + queue: VecDeque, + capture: bool, +} + +impl InputFeed { + pub fn new(capture: bool, lines: impl Into>) -> Self { + Self { + queue: lines.into().into(), + capture, + } + } + + /// Next line from the preset queue, or real stdin when not capturing. + pub fn read_line(&mut self) -> Result { + if let Some(line) = self.queue.pop_front() { + return Ok(line); + } + if self.capture { + anyhow::bail!("input is not available under capture / view"); + } + let mut line = String::new(); + io::stdin().lock().read_line(&mut line)?; + if line.ends_with('\n') { + line.pop(); + if line.ends_with('\r') { + line.pop(); + } + } + Ok(line) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::{run_file_capture, RunOptions}; + + #[test] + fn capture_accepts_preset_stdin() { + let cap = run_file_capture( + Path::new("tests/keywords/input.mq.md"), + &RunOptions { + stdin_lines: vec!["Alice".into()], + ..RunOptions::default() + }, + ) + .expect("capture with stdin"); + assert_eq!(cap.stdout.trim_end(), "Name:Hello Alice!"); + } + + #[test] + fn split_keeps_blank_middle_lines() { + assert_eq!( + split_stdin_text("a\n\nb\n"), + vec!["a".to_string(), String::new(), "b".to_string()] + ); + } +} diff --git a/src/interp.rs b/src/interp.rs index 446315b..3982248 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -7,7 +7,9 @@ use std::path::{Path, PathBuf}; use crate::ast::{ Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp, }; +use crate::builtin::{builtin_int, builtin_len, builtin_str}; use crate::diagnostics::{bail_at, Span}; +use crate::input_feed::InputFeed; use crate::value::Value; pub struct Interpreter { @@ -16,6 +18,7 @@ pub struct Interpreter { capture: bool, pub captured_stdout: String, current_span: Span, + input: InputFeed, } struct Env { @@ -51,6 +54,7 @@ impl Interpreter { capture: false, captured_stdout: String::new(), current_span: Span::new(1, 1), + input: InputFeed::new(false, Vec::new()), } } @@ -61,9 +65,15 @@ impl Interpreter { capture: true, captured_stdout: String::new(), current_span: Span::new(1, 1), + input: InputFeed::new(true, Vec::new()), } } + pub fn with_stdin(mut self, lines: Vec) -> Self { + self.input = InputFeed::new(self.capture, lines); + self + } + fn err(&self, message: impl Into) -> anyhow::Error { bail_at(self.path.as_deref(), self.current_span, message) } @@ -77,6 +87,19 @@ impl Interpreter { } } + fn emit_prompt(&mut self, prompt: &str) { + if prompt.is_empty() { + return; + } + if self.capture { + self.captured_stdout.push_str(prompt); + } else { + print!("{prompt}"); + use std::io::Write; + let _ = std::io::stdout().flush(); + } + } + pub fn run_module(&mut self, module: &Module) -> Result { if let Some(main) = find_top(module, "main") { return self.run_function(module, main, Env::new(), &[]); @@ -340,30 +363,48 @@ impl Interpreter { return Ok(Value::None); } "input" => { - if self.capture { - return Err(self.err("input is not available under capture / view")); - } let bound = bind_args(&["prompt".into()], &ev_args, true) .map_err(|m| self.err(m))?; let prompt = bound .get("prompt") .map(|v| v.as_display()) .unwrap_or_default(); - if !prompt.is_empty() { - print!("{prompt}"); - use std::io::Write; - let _ = std::io::stdout().flush(); - } - let mut line = String::new(); - std::io::stdin().read_line(&mut line)?; - if line.ends_with('\n') { - line.pop(); - if line.ends_with('\r') { - line.pop(); + self.emit_prompt(&prompt); + let line = self.input.read_line().map_err(|e| { + if e.to_string().contains("input is not available") { + self.err("input is not available under capture / view") + } else { + e } - } + })?; return Ok(Value::Text(line)); } + "len" => { + let bound = bind_args(&["value".into()], &ev_args, false) + .map_err(|m| self.err(m))?; + let v = bound + .get("value") + .ok_or_else(|| self.err("len requires value"))?; + let n = builtin_len(v).map_err(|m| self.err(m))?; + return Ok(Value::Int(n)); + } + "str" => { + let bound = bind_args(&["value".into()], &ev_args, false) + .map_err(|m| self.err(m))?; + let v = bound + .get("value") + .ok_or_else(|| self.err("str requires value"))?; + return Ok(builtin_str(v)); + } + "int" => { + let bound = bind_args(&["value".into()], &ev_args, false) + .map_err(|m| self.err(m))?; + let v = bound + .get("value") + .ok_or_else(|| self.err("int requires value"))?; + let n = builtin_int(v).map_err(|m| self.err(m))?; + return Ok(Value::Int(n)); + } _ => {} } diff --git a/src/lib.rs b/src/lib.rs index 3466a71..3fea416 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,11 +4,13 @@ //! CLI also offers `view` for AST-backed browsing. pub mod ast; +pub mod builtin; pub mod bytecode; pub mod capture; pub mod catalog; pub mod debug; pub mod diagnostics; +pub mod input_feed; pub mod interp; pub mod lex; pub mod load; @@ -54,6 +56,8 @@ pub struct RunOptions { pub dump_bytecode: bool, pub trace_eval: bool, pub backend: Backend, + /// Preset lines consumed by `input` (from `--stdin-file` or view). + pub stdin_lines: Vec, } impl RunOptions { @@ -66,6 +70,7 @@ impl RunOptions { dump_bytecode: true, trace_eval: true, backend: Backend::Tree, + stdin_lines: Vec::new(), } } @@ -121,7 +126,8 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result { match opts.backend { Backend::Tree => { - let mut interp = Interpreter::new(Some(path), opts.trace_eval); + let mut interp = Interpreter::new(Some(path), opts.trace_eval) + .with_stdin(opts.stdin_lines.clone()); if opts.trace_eval { eprintln!("=== marqdo: trace-eval ({path_label}) ==="); } @@ -131,18 +137,18 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result { } } Backend::Bytecode => { - let program = compile_module(&module)?; + let program = compile_module(Some(path), &module)?; if opts.dump_bytecode { print!("{}", program.disassemble()); } - let mut vm = Vm::new(); + let mut vm = Vm::new(Some(path)).with_stdin(opts.stdin_lines.clone()); vm.run(&program)?; } } // dump bytecode even on tree backend if requested if opts.backend == Backend::Tree && opts.dump_bytecode { - let program = compile_module(&module)?; + let program = compile_module(Some(path), &module)?; print!("{}", program.disassemble()); } @@ -154,7 +160,8 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result { let module = load_module(path)?; match opts.backend { Backend::Tree => { - let mut interp = Interpreter::with_capture(Some(path), false); + let mut interp = Interpreter::with_capture(Some(path), false) + .with_stdin(opts.stdin_lines.clone()); let value = interp.run_module(&module)?; Ok(RunCapture { stdout: interp.captured_stdout, @@ -162,8 +169,8 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result { }) } Backend::Bytecode => { - let program = compile_module(&module)?; - let mut vm = Vm::with_capture(); + let program = compile_module(Some(path), &module)?; + let mut vm = Vm::with_capture(Some(path)).with_stdin(opts.stdin_lines.clone()); let value = vm.run(&program)?; Ok(RunCapture { stdout: vm.captured_stdout, diff --git a/src/main.rs b/src/main.rs index f0990d4..cc0507f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,7 @@ use anyhow::Result; use clap::{Parser, Subcommand, ValueEnum}; use marqdo::catalog::{write_catalog, CatalogOptions}; +use marqdo::input_feed::load_stdin_file; use marqdo::view::{serve, write_static, OutputOptions, ViewOptions}; use marqdo::{Backend, RunOptions}; @@ -54,6 +55,10 @@ enum Commands { trace_eval: bool, #[arg(long)] dump_all: bool, + + /// Feed `input` from a text file (one line per call), instead of / after the terminal. + #[arg(long, value_name = "FILE")] + stdin_file: Option, }, /// Browse `.mq.md` structure + execution (live server or static output) View { @@ -129,10 +134,18 @@ fn try_main() -> Result { dump_bytecode, trace_eval, dump_all, + stdin_file, } => { let path = file.unwrap_or_else(|| PathBuf::from("index.mq.md")); + let stdin_lines = match stdin_file { + Some(p) => load_stdin_file(&p)?, + None => Vec::new(), + }; let mut opts = if dump_all { - RunOptions::dump_all() + RunOptions { + stdin_lines: stdin_lines.clone(), + ..RunOptions::dump_all() + } } else { RunOptions { dump_lines, @@ -142,6 +155,7 @@ fn try_main() -> Result { dump_bytecode, trace_eval, backend: backend.into(), + stdin_lines, } }; if dump_all { diff --git a/src/view/html.rs b/src/view/html.rs index f9353c1..9d537e1 100644 --- a/src/view/html.rs +++ b/src/view/html.rs @@ -223,6 +223,45 @@ section.block > h2 { font-weight: 600; margin: 0 0 0.85rem; } +.stdin-form { + margin: 0 0 1rem; + display: grid; + gap: 0.45rem; + max-width: 36rem; +} +.stdin-form label { + font-size: 0.85rem; + color: var(--text); +} +.stdin-form .hint { + color: var(--muted); + font-size: 0.8rem; + margin-left: 0.35rem; +} +.stdin-form textarea { + font: inherit; + font-family: var(--mono); + font-size: 0.9rem; + padding: 0.65rem 0.75rem; + border: 1px solid var(--border); + border-radius: 8px; + background: var(--surface); + color: var(--text); + resize: vertical; + min-height: 4.5rem; +} +.stdin-form button { + justify-self: start; + font: inherit; + font-size: 0.9rem; + padding: 0.45rem 0.9rem; + border: 1px solid var(--border); + border-radius: 8px; + background: var(--text); + color: var(--bg); + cursor: pointer; +} +.stdin-form button:hover { opacity: 0.88; } .structure { background: var(--surface); border: 1px solid var(--line); @@ -581,6 +620,19 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM } else { escape(&vm.stderr) }; + let stdin_panel = match links { + LinkMode::Live => format!( + r#"
+ + + + +
"#, + path = escape(rel), + stdin = escape(&vm.preset_stdin), + ), + LinkMode::Static { .. } => String::new(), + }; let title = Path::new(rel) .file_name() .and_then(|s| s.to_str()) @@ -596,6 +648,7 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM

Execution

+ {stdin_panel}
{out}
@@ -608,6 +661,7 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM status_label = status_label, rel = escape(rel), structure = vm.structure_html, + stdin_panel = stdin_panel, out = out_text, source = escape(&vm.source), ); diff --git a/src/view/mod.rs b/src/view/mod.rs index 48a6d42..61979c1 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -14,6 +14,7 @@ use std::process::Command; use anyhow::{bail, Context, Result}; use tiny_http::{Header, Method, Request, Response, Server, StatusCode}; +use crate::input_feed::split_stdin_text; use crate::view::html::{escape, page_file, page_index, LinkMode}; use crate::view::render::{render_module_structure, FileViewModel}; use crate::{run_file_capture, RunOptions}; @@ -129,7 +130,7 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> { if let Some(first) = root.files.first() { let rel = first.to_string_lossy().replace('\\', "/"); let resolved = resolve_rel(root, &rel)?; - let vm = build_file_view(&resolved, &rel)?; + let vm = build_file_view(&resolved, &rel, &[])?; let body = page_file(&root.files, &rel, &vm, &LinkMode::Live); respond_html(request, body) } else { @@ -139,8 +140,10 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> { } "/file" => { let rel = query_param(query, "path").unwrap_or_default(); + let stdin_raw = query_param(query, "stdin").unwrap_or_default(); + let stdin_lines = split_stdin_text(&stdin_raw); let resolved = resolve_rel(root, &rel)?; - let vm = build_file_view(&resolved, &rel)?; + let vm = build_file_view(&resolved, &rel, &stdin_lines)?; let body = page_file(&root.files, &rel, &vm, &LinkMode::Live); respond_html(request, body) } @@ -156,7 +159,11 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> { } } -pub(crate) fn build_file_view(abs: &Path, rel: &str) -> Result { +pub(crate) fn build_file_view( + abs: &Path, + rel: &str, + stdin_lines: &[String], +) -> Result { let source = fs::read_to_string(abs).with_context(|| format!("read {}", abs.display()))?; let structure = match crate::load::load_module(abs) { Ok(module) => render_module_structure(&module, &source), @@ -165,7 +172,11 @@ pub(crate) fn build_file_view(abs: &Path, rel: &str) -> Result { escape(&tidy_user_error(&format!("{e:#}"), abs, rel)) ), }; - let (stdout, stderr, ok) = match run_file_capture(abs, &RunOptions::default()) { + let opts = RunOptions { + stdin_lines: stdin_lines.to_vec(), + ..RunOptions::default() + }; + let (stdout, stderr, ok) = match run_file_capture(abs, &opts) { Ok(cap) => (cap.stdout, String::new(), true), Err(e) => ( String::new(), @@ -180,6 +191,7 @@ pub(crate) fn build_file_view(abs: &Path, rel: &str) -> Result { stdout, stderr, ok, + preset_stdin: stdin_lines.join("\n"), }) } diff --git a/src/view/output.rs b/src/view/output.rs index 56d1b78..37e318e 100644 --- a/src/view/output.rs +++ b/src/view/output.rs @@ -25,7 +25,7 @@ pub fn write_static(opts: OutputOptions) -> Result<()> { for rel_pb in &root.files { let rel = rel_pb.to_string_lossy().replace('\\', "/"); let abs = resolve_abs(&root, &rel)?; - let mut vm = build_file_view(&abs, &rel)?; + let mut vm = build_file_view(&abs, &rel, &[])?; if opts.no_exec { vm.stdout.clear(); vm.stderr = "execution skipped (--no-exec)".into(); diff --git a/src/view/render.rs b/src/view/render.rs index ed53294..1ee8112 100644 --- a/src/view/render.rs +++ b/src/view/render.rs @@ -18,6 +18,8 @@ pub struct FileViewModel { pub stdout: String, pub stderr: String, pub ok: bool, + /// Text shown in the live-view preset stdin box. + pub preset_stdin: String, } pub fn render_module_structure(module: &Module, source: &str) -> String { diff --git a/tests/errors/bad-int.mq.md b/tests/errors/bad-int.mq.md new file mode 100644 index 0000000..a89dc3d --- /dev/null +++ b/tests/errors/bad-int.mq.md @@ -0,0 +1,7 @@ +--- +title: bad int +--- + +# main + +*`x` = > int not-a-number* diff --git a/tests/errors/div-zero.mq.md b/tests/errors/div-zero.mq.md new file mode 100644 index 0000000..6177ab1 --- /dev/null +++ b/tests/errors/div-zero.mq.md @@ -0,0 +1,9 @@ +--- +title: division by zero +--- + +# main + +*`x` = 1 / 0* + +> print text=`x` diff --git a/tests/gold.rs b/tests/gold.rs index 4677702..06c3178 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -1,4 +1,5 @@ -use std::process::Command; +use std::io::Write; +use std::process::{Command, Stdio}; fn run(args: &[&str]) -> (i32, String, String) { let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) @@ -11,19 +12,49 @@ fn run(args: &[&str]) -> (i32, String, String) { (code, stdout, stderr) } +fn run_with_stdin(args: &[&str], stdin: &str) -> (i32, String, String) { + let mut child = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn() + .expect("failed to spawn marqdo"); + { + let mut pipe = child.stdin.take().expect("stdin"); + pipe.write_all(stdin.as_bytes()).expect("write stdin"); + } + let output = child.wait_with_output().expect("wait marqdo"); + let code = output.status.code().unwrap_or(1); + let stdout = String::from_utf8_lossy(&output.stdout).into_owned(); + let stderr = String::from_utf8_lossy(&output.stderr).into_owned(); + (code, stdout, stderr) +} + fn assert_out(path: &str, expect: &str) { let (code, stdout, stderr) = run(&["run", path]); assert_eq!(code, 0, "{path} stderr={stderr}"); assert_eq!(stdout.trim_end(), expect.trim_end(), "{path}"); } -fn assert_err(path: &str, substr: &str) { - let (code, stdout, stderr) = run(&["run", path]); - assert_ne!(code, 0, "{path} unexpectedly succeeded stdout={stdout}"); - assert!( - stderr.contains(substr), - "{path} stderr missing {substr:?}: {stderr}" - ); +/// Assert failure with `path:line:col:` prefix and message substring (both backends). +fn assert_err(path: &str, line_col: &str, substr: &str) { + for backend in ["tree", "bytecode"] { + let (code, stdout, stderr) = run(&["run", path, "--backend", backend]); + assert_ne!( + code, 0, + "{path} backend={backend} unexpectedly succeeded stdout={stdout}" + ); + let loc = format!("{path}:{line_col}:"); + assert!( + stderr.contains(&loc), + "{path} backend={backend} stderr missing location {loc:?}: {stderr}" + ); + assert!( + stderr.contains(substr), + "{path} backend={backend} stderr missing {substr:?}: {stderr}" + ); + } } #[test] @@ -109,10 +140,36 @@ fn keywords_bool_logic() { ); } +#[test] +fn keywords_stdlib() { + assert_out( + "tests/keywords/stdlib.mq.md", + "5 +3 +42 +42 +1", + ); +} + +#[test] +fn bytecode_stdlib() { + assert_out_backend( + "tests/keywords/stdlib.mq.md", + "bytecode", + "5 +3 +42 +42 +1", + ); +} + #[test] fn error_undefined_var() { assert_err( "tests/errors/undefined-var.mq.md", + "7:1", "undefined variable `missing`", ); } @@ -121,6 +178,7 @@ fn error_undefined_var() { fn error_unknown_fn() { assert_err( "tests/errors/unknown-fn.mq.md", + "7:1", "unknown function `no_such_fn`", ); } @@ -129,6 +187,7 @@ fn error_unknown_fn() { fn error_bad_arity() { assert_err( "tests/errors/bad-arity.mq.md", + "7:1", "missing argument for parameter `x`", ); } @@ -137,10 +196,25 @@ fn error_bad_arity() { fn error_syntax_bad_line() { assert_err( "tests/errors/syntax-bad-line.mq.md", + "7:1", "unrecognized statement", ); } +#[test] +fn error_div_zero() { + assert_err("tests/errors/div-zero.mq.md", "7:1", "division by zero"); +} + +#[test] +fn error_bad_int() { + assert_err( + "tests/errors/bad-int.mq.md", + "7:1", + "cannot convert to int", + ); +} + fn assert_out_backend(path: &str, backend: &str, expect: &str) { let (code, stdout, stderr) = run(&["run", path, "--backend", backend]); assert_eq!(code, 0, "{path} backend={backend} stderr={stderr}"); @@ -151,6 +225,49 @@ fn assert_out_backend(path: &str, backend: &str, expect: &str) { ); } +fn assert_out_stdin(path: &str, backend: &str, stdin: &str, expect: &str) { + let (code, stdout, stderr) = + run_with_stdin(&["run", path, "--backend", backend], stdin); + assert_eq!(code, 0, "{path} backend={backend} stderr={stderr}"); + assert_eq!( + stdout.trim_end(), + expect.trim_end(), + "{path} backend={backend} stdout={stdout:?}" + ); +} + +#[test] +fn keywords_input() { + assert_out_stdin( + "tests/keywords/input.mq.md", + "tree", + "Alice\n", + "Name:Hello Alice!", + ); +} + +#[test] +fn keywords_input_stdin_file() { + let (code, stdout, stderr) = run(&[ + "run", + "tests/keywords/input.mq.md", + "--stdin-file", + "tests/keywords/input-stdin.txt", + ]); + assert_eq!(code, 0, "{stderr}"); + assert_eq!(stdout.trim_end(), "Name:Hello Alice!"); +} + +#[test] +fn bytecode_input() { + assert_out_stdin( + "tests/keywords/input.mq.md", + "bytecode", + "Alice\n", + "Name:Hello Alice!", + ); +} + #[test] fn bytecode_hello() { assert_out_backend( @@ -278,7 +395,8 @@ fn catalog_writes_yaml() { assert!(yaml.contains("structure/hello")); assert!(yaml.contains("utils.mq.md"), "{yaml}"); assert!( - yaml.contains("跨文件导入示例") || yaml.contains("imports:\n - utils.mq.md"), + yaml.contains("???????") || yaml.contains("imports: + - utils.mq.md"), "{yaml}" ); } @@ -300,6 +418,7 @@ fn view_output_writes_html() { assert!(!index.contains("fonts.googleapis.com")); assert!(index.contains("#ffffff") || index.contains("--surface: #ffffff")); assert!(index.contains("nav-toggle")); + assert!(!index.contains("Run with input"), "static export should omit live stdin form"); assert!(dir.join("pages").join("hello.mq.md.html").exists()); } diff --git a/tests/keywords/input-stdin.txt b/tests/keywords/input-stdin.txt new file mode 100644 index 0000000..08b6485 --- /dev/null +++ b/tests/keywords/input-stdin.txt @@ -0,0 +1 @@ +Alice diff --git a/tests/keywords/input.mq.md b/tests/keywords/input.mq.md new file mode 100644 index 0000000..1e1f959 --- /dev/null +++ b/tests/keywords/input.mq.md @@ -0,0 +1,10 @@ +--- +title: input +description: Read one stdin line (optional prompt) +--- + +# main + +*`name` = > input prompt=Name: * + +> print text=Hello `name`! diff --git a/tests/keywords/stdlib.mq.md b/tests/keywords/stdlib.mq.md new file mode 100644 index 0000000..7612d31 --- /dev/null +++ b/tests/keywords/stdlib.mq.md @@ -0,0 +1,36 @@ +--- +title: len str int +description: Built-in conversions and length +--- + +# main + +*`s` = hello* + +*`n` = > len `s`* + +> print text=`n` + +`xs` = + +| item | +|------| +| a | +| b | +| c | + +*`m` = > len `xs`* + +> print text=`m` + +*`t` = > str 42* + +> print text=`t` + +*`i` = > int `t`* + +> print text=`i` + +*`j` = > int True* + +> print text=`j`