diff --git a/Cargo.lock b/Cargo.lock index 2a544f9..035cd97 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,6 +67,12 @@ version = "1.0.104" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470" +[[package]] +name = "ascii" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d92bec98840b8f03a5ff5413de5293bfcd8bf96467cf5452609f939ec6f5de16" + [[package]] name = "assert_cmd" version = "2.0.14" @@ -99,6 +105,12 @@ dependencies = [ "serde_core", ] +[[package]] +name = "chunked_transfer" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6e4de3bc4ea267985becf712dc6d9eed8b04c953b3fcfb339ebc87acd9804901" + [[package]] name = "clap" version = "4.4.18" @@ -172,6 +184,12 @@ version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -184,6 +202,12 @@ version = "0.2.189" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3eaf3ede3fee6db1a4c2ee091bf8a8b4dccdc6d17f656fb07896ee72867612f2" +[[package]] +name = "log" +version = "0.4.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad" + [[package]] name = "marqdo" version = "0.1.0" @@ -193,6 +217,7 @@ dependencies = [ "clap", "predicates", "thiserror", + "tiny_http", ] [[package]] @@ -373,6 +398,18 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "tiny_http" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "389915df6413a2e74fb181895f933386023c71110878cd0825588928e64cdc82" +dependencies = [ + "ascii", + "chunked_transfer", + "httpdate", + "log", +] + [[package]] name = "unicode-ident" version = "1.0.24" diff --git a/Cargo.toml b/Cargo.toml index 3f6f4f1..d748a18 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ path = "src/main.rs" anyhow = "1" clap = { version = "=4.4.18", features = ["derive"] } thiserror = "1" +tiny_http = "0.12" [dev-dependencies] assert_cmd = "=2.0.14" diff --git a/README.md b/README.md index 4c43816..048da30 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ Marqdo:Markdown **标记即语法**。源文件 `.mq.md` 既是文稿也是程序。 -语法宪法:[doc/design/markdown-mapping.md](doc/design/markdown-mapping.md) · 关键字:[keywords.md](doc/design/keywords.md) · 示例:[examples/](examples/) +语法宪法:[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) · 浏览:[view.md](doc/design/view.md) · 示例:[examples/](examples/) --- @@ -48,16 +48,18 @@ Marqdo:Markdown **标记即语法**。源文件 `.mq.md` 既是文稿也是程 - 映射 v0.1:[markdown-mapping.md](doc/design/markdown-mapping.md) - 解释器路线图:[interpreter.md](doc/roadmap/interpreter.md) -- **Phase I(树遍历)已通**:`examples/` 下 hello / index / branch / loop / collection / with-import 均可 `marqdo run` +- **Phase I(树遍历)已通**:`examples/structure/` 与 `examples/keywords/` 金样例均可 `marqdo run` +- **view**:`marqdo view examples` 按 AST 浏览结构与输出 - 依赖(Rust,无 Flex/Bison):[dependencies.md](doc/design/dependencies.md) - 选型:[ADR 0001 — Rust](doc/adr/0001-implementation-language.md) ```bash -cargo run -- run examples/index.mq.md -# Hello World! +cargo run -- run examples/structure/hello.mq.md +cargo run -- run examples/structure/hello.mq.md --backend bytecode +cargo run -- view examples --no-open ``` -下一步:按需加强语义诊断与表达式;Phase II 字节码见路线图 M5。 +M5 字节码见 [bytecode.md](doc/design/bytecode.md);errors 见 `examples/errors/`。 --- diff --git a/doc/README.md b/doc/README.md index 0a29b62..75c90ce 100644 --- a/doc/README.md +++ b/doc/README.md @@ -23,6 +23,10 @@ | [adr/0001-implementation-language.md](adr/0001-implementation-language.md) | **Accepted:Rust 参考解释器;不用 Flex/Bison** | | [design/dependencies.md](design/dependencies.md) | 依赖详单(无 Flex/Bison) | | [design/keywords.md](design/keywords.md) | **英文最小关键字 / 内置 `print` `input`** | +| [design/call-arguments.md](design/call-arguments.md) | **调用:具名 + 位置实参** | +| [design/examples-and-tests.md](design/examples-and-tests.md) | **示例即金样例目录布局** | +| [design/view.md](design/view.md) | **`marqdo view` 源结构浏览器** | +| [design/bytecode.md](design/bytecode.md) | **M5 字节码 / 双后端** | | [design/pipeline-debug.md](design/pipeline-debug.md) | **流水线 `--dump-*` 可视调试** | | [dev-setup.md](dev-setup.md) | 开发环境配置(Rust M0) | | [roadmap/interpreter.md](roadmap/interpreter.md) | 正经解释器路线图 | diff --git a/doc/design/bytecode.md b/doc/design/bytecode.md new file mode 100644 index 0000000..c78825a --- /dev/null +++ b/doc/design/bytecode.md @@ -0,0 +1,89 @@ +# Marqdo 字节码(M5 原型) + +| | | +|---|---| +| 状态 | 进行中(原型) | +| 日期 | 2026-08-04 | +| 相关 | [roadmap/interpreter.md](../roadmap/interpreter.md) · Phase I 树遍历 | + +--- + +## 1. 目标 + +在保留树遍历后端(`--backend tree`,默认)的同时,提供栈式字节码 VM(`--backend bytecode`),使**核心金样例** stdout 与树遍历 bit 一致。 + +``` +.mq.md → load/parse → Module AST + ├─ tree-walk + └─ compile → Chunk → VM +``` + +--- + +## 2. 指令集(草案 / 原型已实现子集) + +| Op | 栈效应 | 说明 | +|----|--------|------| +| `Constant i` | → v | 常量池 | +| `True` / `False` / `None` | → b | | +| `Pop` | v → | | +| `GetLocal i` / `SetLocal i` | | 当前帧槽 | +| `GetGlobal i` / `SetGlobal i` | | 名字在常量池(文本) | +| `Add` `Sub` `Mul` `Div` `Negate` `Not` | | | +| `Equal` `NotEqual` `Greater` `GreaterEqual` `Less` `LessEqual` | | | +| `Jump a` / `JumpIfFalse a` / `JumpBack a` | | 相对/绝对跳转(原型用绝对 u16) | +| `Print` | v → | 弹出并写 stdout | +| `BuildList n` | v… → list | | +| `GetIndex` | list, i → v | foreach 用 | +| `Call f argc` | args… → ret | 用户函数(按形参序压栈) | +| `Return` | | 返回 / 结束帧 | +| `Interp n` | parts… → text | n 个交错 Lit/Var 标记后的拼接(简化:编译期展开为常量+拼接) | + +原型实现以 `src/bytecode/` 为准;未列出的 Op 不存在于当前构建。 + +--- + +## 3. 编译约定 + +- 每个 Marqdo `Function` → 一个 `FnChunk`(含 `name`、`params`、`code`、`constants`)。 +- 模块级 `Program`:函数表 + 入口名(`main` 或唯一无参 level-1)。 +- 局部变量:函数体内首次赋值/形参分配槽位。 +- 嵌套 `##`:编入同一 `Program`,按名 `Call`;查找规则对齐树遍历(子树优先,再顶层)。 +- `and` / `or`:短接跳转。 +- 分支 / while:`JumpIfFalse` + `Jump` / `JumpBack`。 +- foreach:`BuildList` 或已有 list 全局/局部 → 索引循环。 + +--- + +## 4. 覆盖表(M5 原型) + +| 金样例 | tree | bytecode | +|--------|------|----------| +| `structure/hello` | ✅ | ✅ | +| `structure/branch` | ✅ | ✅ | +| `structure/loop` | ✅ | ✅ | +| `structure/collection` | ✅ | ✅ | +| `keywords/print` | ✅ | ✅ | +| `keywords/bool-logic` | ✅ | ✅ | +| `structure/nested-call` | ✅ | ✅ | +| `structure/positional-call` | ✅ | ✅ | +| `structure/import/*` | ✅ | ✅(load 合并 AST 后编译) | +| `examples/errors/*` | ✅(非零退出) | 同 tree 语义(编译期/运行期诊断) | + +核心金样例双后端已对齐;后续可扩展 Op、优化与更好的调试信息。 + +--- + +## 5. CLI + +```bash +marqdo run FILE --backend tree # 默认 +marqdo run FILE --backend bytecode +marqdo run FILE --dump-bytecode # 反汇编后仍可按 backend 执行 +``` + +--- + +## 6. 非目标(本原型) + +优化、寄存器分配、GC、与树遍历指令级调试对齐、独立 sema IR。 diff --git a/doc/design/call-arguments.md b/doc/design/call-arguments.md new file mode 100644 index 0000000..a9e01c7 --- /dev/null +++ b/doc/design/call-arguments.md @@ -0,0 +1,50 @@ +# 调用实参:具名与位置 + +| | | +|---|---| +| 状态 | 已定(v0.1 增补) | +| 日期 | 2026-08-04 | +| 相关 | [markdown-mapping.md](markdown-mapping.md) · [keywords.md](keywords.md) | + +--- + +## 1. 目标 + +`>` 调用同时支持: + +| 形式 | 示例 | 说明 | +|------|------|------| +| **具名**(保留) | `> 问候 谁=Marqdo` | `名=值`;值可含空格(至下一 `名=`) | +| **位置**(新增) | `> 问候 Marqdo` | 按形参声明顺序绑定 | + +二者可混用:位置实参按「尚未被具名绑定的形参」从左到右填充。具名不可绑定同一形参两次;位置实参不得出现在「已出现具名实参」之后(对齐常见「位置在前、关键字在后」习惯)。 + +--- + +## 2. 文法(调用尾部) + +``` +call_tail ::= callee arg* +arg ::= named | positional +named ::= ident '=' value +positional ::= value_token +``` + +- `value`(具名右侧):可含空格与 `` `var` `` 插值,直到下一个 `ident=` 边界。 +- `value_token`(位置):空白分隔的一个词法单元——数字、`` `名` ``、标识/文本词;**不含空格**。多词字符串请用具名,例如 `> print text=Hello World!`。 +- 内置 `print` 形参名为 `text`:`> print Hi` 与 `> print text=Hi` 等价。 + +--- + +## 3. 绑定算法 + +1. 收集具名 → 映射;冲突则报错。 +2. 按函数形参表顺序:若具名已提供则用之,否则取下一个位置实参。 +3. 位置实参有余、或必填形参仍缺 → 报错(内置 `input` 的 `prompt` 可缺省)。 +4. 用户函数:额外具名键仍写入调用环境(便于扩展);位置实参不可超过形参数目。 + +--- + +## 4. 金样例 + +见 `examples/structure/positional-call.mq.md` 与更新后的 `examples/structure/import/`。 diff --git a/doc/design/dependencies.md b/doc/design/dependencies.md index 578c95b..eec8374 100644 --- a/doc/design/dependencies.md +++ b/doc/design/dependencies.md @@ -152,7 +152,7 @@ spike/ # Python 风险探测存档(可保留,非运行 ```bash cargo build cargo test -cargo run -- run examples/index.mq.md +cargo run -- run examples/structure/nested-call.mq.md ``` --- diff --git a/doc/design/examples-and-tests.md b/doc/design/examples-and-tests.md new file mode 100644 index 0000000..7ee4b59 --- /dev/null +++ b/doc/design/examples-and-tests.md @@ -0,0 +1,76 @@ +# 示例与测试布局 + +| | | +|---|---| +| 状态 | 已定 | +| 日期 | 2026-08-04 | +| 原则 | **示例即金样例**:`examples/**/*.mq.md` 由 `cargo test` / CI 跑通;`marqdo view` 也可直接浏览 | + +--- + +## 1. 目录 + +``` +examples/ + README.md # 本树说明 + structure/ # 代码基础结构(Markup 架构) + hello.mq.md + nested-call.mq.md # 原 index:嵌套函数 + 调用 + positional-call.mq.md # 位置实参 + branch.mq.md + loop.mq.md + collection.mq.md + import/ + main.mq.md # 原 with-import + utils.mq.md + keywords/ # 关键字与内置名 + print.mq.md + bool-logic.mq.md # True/False/None、and/or/not + errors/ # 期望失败(诊断文案稳定后再加金样例) + .gitkeep +``` + +Rust 集成测试:`tests/gold.rs` 扫描约定路径,比对 stdout。 + +--- + +## 2. 分类职责 + +| 类别 | 测什么 | 不测什么 | +|------|--------|----------| +| **structure** | `#`/`##`、形参、`>` 调用、`*`/`**`、`+`/`-`、表、导入、位置/具名实参 | 关键字边角 | +| **keywords** | `True`/`False`/`None`、`and`/`or`/`not`、`print`/`input` 约定 | 复杂控制流(可极简) | +| **errors** | 未定义名、实参错误、语法错误的诊断 | 成功路径 | + +未单独建夹、但应覆盖的其它测试(放 `tests/` 单测或日后 `examples/runtime/`): + +- **流水线调试**:`--dump-lines` / `--dump-ast` / `--trace-eval` +- **CLI**:默认 `index.mq.md`、退出码 +- **view**:目录扫描、单文件页、执行区(见 [view.md](view.md)) +- **模块图**:循环导入拒绝 + +--- + +## 3. 金样例约定 + +每个可执行 `.mq.md` 在 `tests/gold.rs`(或旁路 `.out` 文件)登记期望 stdout。 +`errors/` 内文件登记期望 stderr 子串与非零退出码。 + +Frontmatter 可写: + +```yaml +--- +title: … +expect_stdout: | + line1 + line2 +--- +``` + +(可选;v0 先以测试代码内嵌期望为主。) + +--- + +## 4. 与 `view` 的关系 + +`marqdo view examples` 按上述文件夹生成侧栏索引;`structure` / `keywords` 为一级分组。 diff --git a/doc/design/keywords.md b/doc/design/keywords.md index e4043a7..b771d13 100644 --- a/doc/design/keywords.md +++ b/doc/design/keywords.md @@ -38,7 +38,7 @@ | 名称 | 作用 | 调用示例 | |------|------|----------| -| `print` | 写到 stdout(副作用) | `> print text=Hello World!` | +| `print` | 写到 stdout(副作用) | `> print text=Hello` 或 `> print Hello`(位置实参,见 [call-arguments.md](call-arguments.md)) | | `input` | 从 stdin 读一行(返回文本) | `*`name` = > input prompt=Name: *` | 参数约定(v0): diff --git a/doc/design/markdown-mapping.md b/doc/design/markdown-mapping.md index 8062604..228e6ff 100644 --- a/doc/design/markdown-mapping.md +++ b/doc/design/markdown-mapping.md @@ -42,7 +42,7 @@ Markdown **标记即语法**。输出**不是**语言架构原语,而是内置 | 体内 `-` | 循环 | | 体内 `+` 或 `1.` | 分支 | | 分支臂头整行恰为 `*` | else | -| `> 函数 形参=实参` | 调用(含内置 `print` / `input`) | +| `> 函数 形参=实参` 或 `> 函数 实参` | 调用(具名 / 位置,见 [call-arguments.md](call-arguments.md)) | | frontmatter `> path.mq.md` | 跨文件导入 | | `*…*` | **程序语句**(绑定、表达式句、可写的调用句等) | | `**…**` | **返回值**(架构级;函数结果) | diff --git a/doc/design/pipeline-debug.md b/doc/design/pipeline-debug.md index b94245e..cc1c78f 100644 --- a/doc/design/pipeline-debug.md +++ b/doc/design/pipeline-debug.md @@ -28,6 +28,8 @@ marqdo dump FILE --stage lines|tokens|ast|sema|all | `--dump-ast` | 语法 | S 表达式或缩进树(模块/函数/语句) | | `--dump-sema` | 语义 | 作用域、导入图、提升后的符号表 | | `--trace-eval` | 求值 | 进入/离开函数、绑定、分支臂选择、`print` 调用 | +| `--dump-bytecode` | 字节码 | 反汇编 `Program`(见 [bytecode.md](bytecode.md)) | +| `--backend tree\|bytecode` | 执行后端 | 默认 `tree`;`bytecode` 走栈式 VM | | `--dump-all` | 以上全部 | 按管道顺序打印,段之间用横幅分隔 | 格式约定: diff --git a/doc/design/view.md b/doc/design/view.md new file mode 100644 index 0000000..6ab5acd --- /dev/null +++ b/doc/design/view.md @@ -0,0 +1,119 @@ +# `marqdo view` — 源结构浏览器 + +| | | +|---|---| +| 状态 | 已定(实现中) | +| 日期 | 2026-08-04 | +| 相关 | [examples-and-tests.md](examples-and-tests.md) · [pipeline-debug.md](pipeline-debug.md) · AST(`src/ast.rs`) | + +--- + +## 1. 动机 + +`.mq.md` 既是文档也是程序。需要一种**可读渲染**,而不仅是纯文本或 `run` 的 stdout: + +- 按语法树展示函数、分支、循环、调用、返回; +- 同页展示**执行结果**; +- 支持打开**单个文件**或**文件夹**(扫描全部 `.mq.md` + 结构索引)。 + +--- + +## 2. CLI + +```bash +marqdo view [PATH] [--port PORT] [--host HOST] [--no-open] +``` + +| 参数 | 默认 | 说明 | +|------|------|------| +| `PATH` | `.` | `.mq.md` 文件,或目录(递归扫描 `*.mq.md`) | +| `--port` | `7429` | HTTP 端口 | +| `--host` | `127.0.0.1` | 仅本地 | +| `--no-open` | 关 | 不尝试打开系统浏览器 | + +示例: + +```bash +marqdo view examples/structure/hello.mq.md +marqdo view examples +marqdo view examples/keywords --port 8080 +``` + +启动后打印 `http://127.0.0.1:7429/`,阻塞直至 Ctrl+C。 + +--- + +## 3. 信息架构 + +### 3.1 目录模式 + +``` +┌─────────────┬──────────────────────────────────┐ +│ 索引 │ 选中文件 / 欢迎页 │ +│ structure/ │ │ +│ hello │ [源码结构] [执行结果] [原始文本] │ +│ branch │ │ +│ keywords/ │ │ +└─────────────┴──────────────────────────────────┘ +``` + +- 侧栏:相对 `PATH` 的目录树;只列出 `.mq.md`。 +- 点击文件:主区切换为该文件的三栏/三节视图。 + +### 3.2 单文件模式 + +无侧栏列表(或仅当前文件);主区同「选中文件」视图。仍提供「执行」结果。 + +--- + +## 4. 主区三节 + +### 4.1 源码结构(AST 渲染) + +不依赖浏览器 Markdown 猜测语义,而用服务端已解析的 AST: + +| AST 节点 | 视觉 | +|----------|------| +| `Function` | 卡片:标题层级、形参芯片、子函数嵌套;**中间的注释行仍展示**(来自源行分类,不在 AST 内) | +| `Stmt::*` | 绑定 / 返回 / 调用 / 分支 / 循环;表达式用**表面语法**(如 `` `x` > 0 ``),不用 Debug | + +样式:清晰分区,避免「通用仪表盘」堆砌;结构层级一眼可读。 + +### 4.2 执行结果 + +服务端对该文件跑与 `marqdo run` 相同的管线,**捕获 stdout/stderr** 与退出状态,展示在「输出」区。 +含 `input` 的程序:view 中标注「需交互,未在 view 执行」或提供只读说明(v0:跳过/失败信息)。 + +### 4.3 原始文本 + +等宽展示源文件;可选行分类着色(Code / Comment / Blank),与 `--dump-lines` 一致。 + +--- + +## 5. HTTP API(实现细节) + +| 方法 | 路径 | 含义 | +|------|------|------| +| `GET` | `/` | 索引 HTML | +| `GET` | `/file?path=…` | 单文件页(path 相对根,防 `..` 逃逸) | +| `GET` | `/api/tree` | JSON 文件树 | +| `GET` | `/api/file?path=…` | JSON:`{ source, ast_html 或 ast_json, stdout, stderr, ok }` | + +v0 可全部服务端渲染 HTML,API 可选。 + +安全:根目录锁死为 CLI 给定的 `PATH`(若为文件则其父目录为根、仅一文件);拒绝根外路径。 + +--- + +## 6. 依赖 + +本地同步 HTTP 即可(如 `tiny_http`),无强制前端构建;HTML/CSS/少量 JS 内嵌于 Rust。 + +--- + +## 7. 验收 + +1. `marqdo view examples/structure/hello.mq.md` 显示结构 + `Hello World!` 输出。 +2. `marqdo view examples` 侧栏含 `structure/` 与 `keywords/` 分组。 +3. 分支/循环样例在结构区可区分臂与循环体。 +4. 路径逃逸被拒绝。 diff --git a/doc/dev-setup.md b/doc/dev-setup.md index 002a528..f747d3e 100644 --- a/doc/dev-setup.md +++ b/doc/dev-setup.md @@ -33,7 +33,7 @@ cd marqdo cargo build cargo test -cargo run -- run examples/hello.mq.md +cargo run -- run examples/structure/hello.mq.md ``` 当前 M0:`run` 会读入文件并明确报错「evaluation not implemented」(脚手架,非静默假成功)。 diff --git a/doc/roadmap/interpreter.md b/doc/roadmap/interpreter.md index 1c23763..5664bcd 100644 --- a/doc/roadmap/interpreter.md +++ b/doc/roadmap/interpreter.md @@ -96,7 +96,7 @@ Phase I 对齐 Crafting Interpreters 的 **jlox(树遍历)**;Phase II 对 2. **语法 → AST**:函数、形参、`*` 语句、`**` 返回、`>` 调用、`+` 分支、`-` 循环、表格集合、绑定与 `==`。 3. **语义**:提升、嵌套作用域、`##` 私有、frontmatter 导入公有顶层。 4. **运行时**:环境、调用栈、值(文/数/表/记录)、内置 `print`。 -5. **验收**:`examples/hello` … `with-import` 全部 `marqdo run` 通过。 +5. **验收**:`examples/structure/*` 与 `examples/keywords/*` 全部 `marqdo run` 通过。 ### Phase II — 字节码 VM(正经化) @@ -158,7 +158,7 @@ spike/ # Python 存档,非运行时依赖 | M2 | 解析:函数、输出调用、返回 | `hello` + `index` 金样例绿 | ✅ | | M3 | 分支、循环、表格、绑定 | `branch`/`loop`/`collection` 绿 | ✅ | | M4 | 导入与提升 | `with-import` 绿;**宣称 Phase I 达标** | ✅ | -| M5 | 字节码设计文档 + 原型 opcode | 同金样例双后端一致 | 未开始 | +| M5 | 字节码设计文档 + 原型 opcode | 核心金样例双后端一致;覆盖表见 [bytecode.md](../design/bytecode.md) | 进行中 | **Phase I 完成前,不宣称「语言已实现」。** **仅 hello 特判通过,不算完成。** @@ -170,9 +170,9 @@ spike/ # Python 存档,非运行时依赖 在仓库根或任意含 `index.mq.md` 的目录: ```bash -marqdo run examples/index.mq.md +marqdo run examples/structure/nested-call.mq.md # 或 -cd examples && marqdo run +marqdo run examples/structure/import/main.mq.md ``` 输出与金样例一致;错误带位置;新增语法必须加金样例与单测。这才是「一门解释型语言」的最低合格线。 diff --git a/examples/README.md b/examples/README.md new file mode 100644 index 0000000..2b53165 --- /dev/null +++ b/examples/README.md @@ -0,0 +1,15 @@ +# Marqdo examples(金样例) + +本目录即测试夹具,见 [doc/design/examples-and-tests.md](../doc/design/examples-and-tests.md)。 + +| 目录 | 内容 | +|------|------| +| [`structure/`](structure/) | 函数、调用、分支、循环、表、导入、位置实参 | +| [`keywords/`](keywords/) | `print`、布尔与 `and`/`or`/`not` | +| [`errors/`](errors/) | 期望失败(未定义名 / 未知函数 / 实参 / 语法) | + +```bash +cargo test +marqdo run examples/structure/hello.mq.md +marqdo view examples +``` diff --git a/examples/errors/.gitkeep b/examples/errors/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/examples/errors/bad-arity.mq.md b/examples/errors/bad-arity.mq.md new file mode 100644 index 0000000..74c74c4 --- /dev/null +++ b/examples/errors/bad-arity.mq.md @@ -0,0 +1,12 @@ +--- +title: bad arity +--- + +# main + +> needs_one + +## needs_one + - x + +> print text=`x` diff --git a/examples/errors/syntax-bad-line.mq.md b/examples/errors/syntax-bad-line.mq.md new file mode 100644 index 0000000..cf11ab2 --- /dev/null +++ b/examples/errors/syntax-bad-line.mq.md @@ -0,0 +1,7 @@ +--- +title: unrecognized code line +--- + +# main + +~not-valid-syntax diff --git a/examples/errors/undefined-var.mq.md b/examples/errors/undefined-var.mq.md new file mode 100644 index 0000000..8b88621 --- /dev/null +++ b/examples/errors/undefined-var.mq.md @@ -0,0 +1,7 @@ +--- +title: undefined variable +--- + +# main + +> print text=`missing` diff --git a/examples/errors/unknown-fn.mq.md b/examples/errors/unknown-fn.mq.md new file mode 100644 index 0000000..7edb799 --- /dev/null +++ b/examples/errors/unknown-fn.mq.md @@ -0,0 +1,7 @@ +--- +title: unknown function +--- + +# main + +> no_such_fn x=1 diff --git a/examples/keywords/bool-logic.mq.md b/examples/keywords/bool-logic.mq.md new file mode 100644 index 0000000..154737b --- /dev/null +++ b/examples/keywords/bool-logic.mq.md @@ -0,0 +1,20 @@ +--- +title: 布尔与逻辑关键字 +--- + +# main + +*`a` = True* +*`b` = False* + ++ `a` and not `b` + > print text=ok ++ * + > print text=fail + +*`probe` = 1* + ++ None + > print text=none-truthy ++ * + > print text=none-falsy diff --git a/examples/keywords/print.mq.md b/examples/keywords/print.mq.md new file mode 100644 index 0000000..978ffa7 --- /dev/null +++ b/examples/keywords/print.mq.md @@ -0,0 +1,9 @@ +--- +title: 内置 print +--- + +# main + +> print text=named form + +> print positional diff --git a/examples/branch.mq.md b/examples/structure/branch.mq.md similarity index 100% rename from examples/branch.mq.md rename to examples/structure/branch.mq.md diff --git a/examples/collection.mq.md b/examples/structure/collection.mq.md similarity index 100% rename from examples/collection.mq.md rename to examples/structure/collection.mq.md diff --git a/examples/hello.mq.md b/examples/structure/hello.mq.md similarity index 100% rename from examples/hello.mq.md rename to examples/structure/hello.mq.md diff --git a/examples/with-import.mq.md b/examples/structure/import/main.mq.md similarity index 100% rename from examples/with-import.mq.md rename to examples/structure/import/main.mq.md diff --git a/examples/utils.mq.md b/examples/structure/import/utils.mq.md similarity index 100% rename from examples/utils.mq.md rename to examples/structure/import/utils.mq.md diff --git a/examples/loop.mq.md b/examples/structure/loop.mq.md similarity index 100% rename from examples/loop.mq.md rename to examples/structure/loop.mq.md diff --git a/examples/index.mq.md b/examples/structure/nested-call.mq.md similarity index 100% rename from examples/index.mq.md rename to examples/structure/nested-call.mq.md diff --git a/examples/structure/positional-call.mq.md b/examples/structure/positional-call.mq.md new file mode 100644 index 0000000..7da7848 --- /dev/null +++ b/examples/structure/positional-call.mq.md @@ -0,0 +1,19 @@ +--- +title: 位置实参 +description: `> fn value` binds by parameter order; named form still works +--- + +# main + +位置实参: + +> 问候 Marqdo + +具名实参(保留): + +> 问候 谁=World + +## 问候 + - 谁 + +> print text=Hello `谁`! diff --git a/src/ast.rs b/src/ast.rs index 875e890..9b533ae 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -60,7 +60,13 @@ pub struct BranchArm { #[derive(Debug, Clone)] pub struct CallExpr { pub callee: String, - pub args: Vec<(String, Expr)>, + pub args: Vec, +} + +#[derive(Debug, Clone)] +pub enum Arg { + Positional(Expr), + Named { name: String, value: Expr }, } #[derive(Debug, Clone)] diff --git a/src/bytecode/compile.rs b/src/bytecode/compile.rs new file mode 100644 index 0000000..d1d7578 --- /dev/null +++ b/src/bytecode/compile.rs @@ -0,0 +1,453 @@ +//! AST → bytecode Program. + +use anyhow::{bail, Result}; +use std::collections::HashMap; + +use crate::ast::{ + Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp, +}; +use crate::bytecode::{FnChunk, Op, Program}; +use crate::value::Value; + +struct FlatFun { + name: String, + parent: Option, + params: Vec, + body: Vec, + children: Vec, +} + +pub fn compile_module(module: &Module) -> Result { + let mut flat = Vec::new(); + for fun in &module.functions { + collect_fun(fun, None, &mut flat); + } + if flat.is_empty() { + bail!("bytecode: no functions to compile"); + } + + let entry = flat + .iter() + .position(|f| f.name == "main" && f.parent.is_none()) + .or_else(|| { + flat.iter() + .position(|f| f.parent.is_none() && f.params.is_empty()) + }) + .ok_or_else(|| anyhow::anyhow!("bytecode: no entry function"))?; + + let mut functions = Vec::new(); + for i in 0..flat.len() { + functions.push(compile_function(i, &flat)?); + } + + Ok(Program { functions, entry }) +} + +fn collect_fun(fun: &Function, parent: Option, flat: &mut Vec) -> usize { + let id = flat.len(); + flat.push(FlatFun { + name: fun.name.clone(), + parent, + params: fun.params.clone(), + body: fun.body.clone(), + children: Vec::new(), + }); + let mut child_ids = Vec::new(); + for child in &fun.children { + child_ids.push(collect_fun(child, Some(id), flat)); + } + flat[id].children = child_ids; + id +} + +struct FnCompiler<'a> { + flat: &'a [FlatFun], + fn_id: usize, + chunk: FnChunk, + locals: HashMap, +} + +fn compile_function(fn_id: usize, flat: &[FlatFun]) -> 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 { + flat, + fn_id, + chunk: FnChunk { + name: fun.name.clone(), + params: fun.params.clone(), + code: Vec::new(), + constants: Vec::new(), + locals: fun.params.clone(), + }, + locals, + }; + for stmt in &fun.body { + c.compile_stmt(stmt)?; + } + c.emit(Op::None_); + c.emit(Op::Return); + let mut names = vec![String::new(); c.locals.len()]; + for (name, slot) in &c.locals { + names[*slot as usize] = name.clone(); + } + c.chunk.locals = names; + Ok(c.chunk) +} + +impl<'a> FnCompiler<'a> { + fn emit(&mut self, op: Op) { + self.chunk.code.push(op); + } + + fn here(&self) -> u16 { + self.chunk.code.len() as u16 + } + + fn patch(&mut self, at: u16, op: Op) { + self.chunk.code[at as usize] = op; + } + + fn add_const(&mut self, v: Value) -> u16 { + if let Some(i) = self.chunk.constants.iter().position(|c| c == &v) { + return i as u16; + } + let i = self.chunk.constants.len() as u16; + self.chunk.constants.push(v); + i + } + + fn local_slot(&mut self, name: &str) -> u8 { + if let Some(&s) = self.locals.get(name) { + return s; + } + let s = self.locals.len() as u8; + self.locals.insert(name.to_string(), s); + s + } + + fn resolve_call(&self, name: &str) -> Result { + let fun = &self.flat[self.fn_id]; + for &cid in &fun.children { + if self.flat[cid].name == name { + return Ok(cid); + } + } + let mut parent = fun.parent; + while let Some(pid) = parent { + for &cid in &self.flat[pid].children { + if self.flat[cid].name == name { + return Ok(cid); + } + } + parent = self.flat[pid].parent; + } + for (i, f) in self.flat.iter().enumerate() { + if f.parent.is_none() && f.name == name { + return Ok(i); + } + } + bail!("bytecode: unknown function `{name}`") + } + + fn compile_stmt(&mut self, stmt: &Stmt) -> Result<()> { + match stmt { + Stmt::Assign { name, value, .. } => { + self.compile_expr(value)?; + let slot = self.local_slot(name); + self.emit(Op::SetLocal(slot)); + self.emit(Op::Pop); + } + Stmt::Return { value, .. } => { + self.compile_expr(value)?; + self.emit(Op::Return); + } + Stmt::Call { call, .. } => self.compile_call(call, true)?, + Stmt::Branch { arms, .. } => { + let mut end_jumps = Vec::new(); + for (i, arm) in arms.iter().enumerate() { + let is_last = i + 1 == arms.len(); + let mut else_jump = None; + if let Some(cond) = &arm.condition { + self.compile_expr(cond)?; + let jf = self.here(); + self.emit(Op::JumpIfFalse(0)); + else_jump = Some(jf); + } + for st in &arm.body { + self.compile_stmt(st)?; + } + if !is_last { + let j = self.here(); + self.emit(Op::Jump(0)); + end_jumps.push(j); + } + if let Some(jf) = else_jump { + self.patch(jf, Op::JumpIfFalse(self.here())); + } + } + let end = self.here(); + for j in end_jumps { + self.patch(j, Op::Jump(end)); + } + } + Stmt::While { + condition, body, .. + } => { + let loop_start = self.here(); + self.compile_expr(condition)?; + let exit_j = self.here(); + self.emit(Op::JumpIfFalse(0)); + for st in body { + self.compile_stmt(st)?; + } + self.emit(Op::Jump(loop_start)); + self.patch(exit_j, Op::JumpIfFalse(self.here())); + } + Stmt::ForEach { + item, + collection, + body, + .. + } => { + self.compile_expr(&Expr::Var(collection.clone()))?; + let coll_slot = self.local_slot(&format!("__coll_{collection}")); + self.emit(Op::SetLocal(coll_slot)); + self.emit(Op::Pop); + let i_slot = self.local_slot(&format!("__i_{collection}")); + let zero = self.add_const(Value::Int(0)); + self.emit(Op::Constant(zero)); + self.emit(Op::SetLocal(i_slot)); + self.emit(Op::Pop); + let item_slot = self.local_slot(item); + + let loop_start = self.here(); + self.emit(Op::GetLocal(i_slot)); + self.emit(Op::GetLocal(coll_slot)); + self.emit(Op::Len); + self.emit(Op::Less); + let exit_j = self.here(); + self.emit(Op::JumpIfFalse(0)); + + self.emit(Op::GetLocal(coll_slot)); + self.emit(Op::GetLocal(i_slot)); + self.emit(Op::GetIndex); + self.emit(Op::SetLocal(item_slot)); + self.emit(Op::Pop); + + for st in body { + self.compile_stmt(st)?; + } + + self.emit(Op::GetLocal(i_slot)); + let one = self.add_const(Value::Int(1)); + self.emit(Op::Constant(one)); + self.emit(Op::Add); + self.emit(Op::SetLocal(i_slot)); + self.emit(Op::Pop); + self.emit(Op::Jump(loop_start)); + self.patch(exit_j, Op::JumpIfFalse(self.here())); + } + } + Ok(()) + } + + fn compile_call(&mut self, call: &CallExpr, as_stmt: bool) -> Result<()> { + match call.callee.as_str() { + "print" => { + let text_expr = call + .args + .iter() + .find_map(|a| match a { + Arg::Named { name, value } if name == "text" => Some(value), + _ => None, + }) + .or_else(|| { + call.args.iter().find_map(|a| match a { + Arg::Positional(e) => Some(e), + _ => None, + }) + }) + .ok_or_else(|| anyhow::anyhow!("bytecode: print needs text"))?; + self.compile_expr(text_expr)?; + self.emit(Op::Print); + if !as_stmt { + self.emit(Op::None_); + } + return Ok(()); + } + "input" => bail!("bytecode: input not supported"), + _ => {} + } + + let fid = self.resolve_call(&call.callee)?; + let params = self.flat[fid].params.clone(); + let mut named: HashMap = HashMap::new(); + let mut positionals = Vec::new(); + for a in &call.args { + match a { + Arg::Positional(e) => positionals.push(e), + Arg::Named { name, value } => { + named.insert(name.clone(), value); + } + } + } + let mut pos_i = 0; + for p in ¶ms { + if let Some(e) = named.get(p) { + self.compile_expr(e)?; + } else if pos_i < positionals.len() { + self.compile_expr(positionals[pos_i])?; + pos_i += 1; + } else { + bail!("bytecode: missing argument for parameter `{p}`"); + } + } + if pos_i < positionals.len() { + bail!("bytecode: too many positional arguments"); + } + self.emit(Op::Call(fid as u16, params.len() as u8)); + if as_stmt { + self.emit(Op::Pop); + } + Ok(()) + } + + fn compile_expr(&mut self, expr: &Expr) -> Result<()> { + match expr { + Expr::Literal(lit) => match lit { + Literal::None => self.emit(Op::None_), + Literal::Bool(true) => self.emit(Op::True), + Literal::Bool(false) => self.emit(Op::False), + Literal::Int(n) => { + let i = self.add_const(Value::Int(*n)); + self.emit(Op::Constant(i)); + } + Literal::Text(t) => { + let i = self.add_const(Value::Text(t.clone())); + self.emit(Op::Constant(i)); + } + }, + Expr::Var(name) => { + let slot = self + .locals + .get(name) + .copied() + .ok_or_else(|| anyhow::anyhow!("bytecode: undefined variable `{name}`"))?; + self.emit(Op::GetLocal(slot)); + } + Expr::Interp(parts) => { + if parts.is_empty() { + let i = self.add_const(Value::Text(String::new())); + self.emit(Op::Constant(i)); + return Ok(()); + } + for (idx, part) in parts.iter().enumerate() { + match part { + InterpPart::Lit(t) => { + let i = self.add_const(Value::Text(t.clone())); + self.emit(Op::Constant(i)); + } + InterpPart::Var(n) => { + let slot = self.locals.get(n).copied().ok_or_else(|| { + anyhow::anyhow!("bytecode: undefined variable `{n}`") + })?; + self.emit(Op::GetLocal(slot)); + } + } + if idx > 0 { + self.emit(Op::Add); + } + } + } + Expr::Unary { op, expr } => { + self.compile_expr(expr)?; + match op { + UnaryOp::Not => self.emit(Op::Not), + UnaryOp::Neg => self.emit(Op::Negate), + } + } + Expr::Binary { + op: BinaryOp::And, + left, + right, + } => { + // JumpIfFalse always pops the tested value. + self.compile_expr(left)?; + let j_false = self.here(); + self.emit(Op::JumpIfFalse(0)); + self.compile_expr(right)?; + let j_false2 = self.here(); + self.emit(Op::JumpIfFalse(0)); + self.emit(Op::True); + let j_end = self.here(); + self.emit(Op::Jump(0)); + let f2 = self.here(); + self.patch(j_false2, Op::JumpIfFalse(f2)); + self.emit(Op::False); + let j_end2 = self.here(); + self.emit(Op::Jump(0)); + let f1 = self.here(); + self.patch(j_false, Op::JumpIfFalse(f1)); + self.emit(Op::False); + let end = self.here(); + self.patch(j_end, Op::Jump(end)); + self.patch(j_end2, Op::Jump(end)); + } + Expr::Binary { + op: BinaryOp::Or, + left, + right, + } => { + self.compile_expr(left)?; + let j_else = self.here(); + self.emit(Op::JumpIfFalse(0)); + self.emit(Op::True); + let j_end = self.here(); + self.emit(Op::Jump(0)); + let else_ip = self.here(); + self.patch(j_else, Op::JumpIfFalse(else_ip)); + self.compile_expr(right)?; + let j_f = self.here(); + self.emit(Op::JumpIfFalse(0)); + self.emit(Op::True); + let j_end2 = self.here(); + self.emit(Op::Jump(0)); + let f = self.here(); + self.patch(j_f, Op::JumpIfFalse(f)); + self.emit(Op::False); + let end = self.here(); + self.patch(j_end, Op::Jump(end)); + self.patch(j_end2, Op::Jump(end)); + } + Expr::Binary { op, left, right } => { + self.compile_expr(left)?; + self.compile_expr(right)?; + self.emit(match op { + BinaryOp::Add => Op::Add, + BinaryOp::Sub => Op::Sub, + BinaryOp::Mul => Op::Mul, + BinaryOp::Div => Op::Div, + BinaryOp::Eq => Op::Equal, + BinaryOp::Ne => Op::NotEqual, + BinaryOp::Lt => Op::Less, + BinaryOp::Le => Op::Lessequal, + BinaryOp::Gt => Op::Greater, + BinaryOp::Ge => Op::GreaterEqual, + BinaryOp::And | BinaryOp::Or => unreachable!(), + }); + } + Expr::Call(call) => self.compile_call(call, false)?, + Expr::List(items) => { + for it in items { + self.compile_expr(it)?; + } + self.emit(Op::BuildList(items.len() as u16)); + } + } + Ok(()) + } +} diff --git a/src/bytecode/mod.rs b/src/bytecode/mod.rs new file mode 100644 index 0000000..7ca666c --- /dev/null +++ b/src/bytecode/mod.rs @@ -0,0 +1,80 @@ +//! Bytecode chunk + opcodes (M5 prototype). + +mod compile; +mod vm; + +pub use compile::compile_module; +pub use vm::Vm; + +use crate::value::Value; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Op { + Constant(u16), + True, + False, + None_, + Pop, + GetLocal(u8), + SetLocal(u8), + Add, + Sub, + Mul, + Div, + Negate, + Not, + Equal, + NotEqual, + Greater, + GreaterEqual, + Less, + Lessequal, + /// Absolute jump to instruction index. + Jump(u16), + JumpIfFalse(u16), + Print, + BuildList(u16), + Len, + GetIndex, + /// Call function by index; argc values are on the stack (param order). + Call(u16, u8), + Return, +} + +#[derive(Debug, Clone)] +pub struct FnChunk { + pub name: String, + pub params: Vec, + pub code: Vec, + pub constants: Vec, + /// Local slot names (params first). + pub locals: Vec, +} + +#[derive(Debug, Clone)] +pub struct Program { + pub functions: Vec, + pub entry: usize, +} + +impl Program { + pub fn disassemble(&self) -> String { + let mut out = String::new(); + out.push_str("=== marqdo: bytecode ===\n"); + for (fi, fun) in self.functions.iter().enumerate() { + let entry = if fi == self.entry { " " } else { "" }; + out.push_str(&format!( + "fn[{fi}] {} params={:?} locals={:?}{entry}\n", + fun.name, fun.params, fun.locals + )); + for (i, c) in fun.constants.iter().enumerate() { + out.push_str(&format!(" const[{i}] = {c:?}\n")); + } + for (ip, op) in fun.code.iter().enumerate() { + out.push_str(&format!(" {ip:04} {op:?}\n")); + } + } + out.push_str("=== marqdo: end bytecode ===\n"); + out + } +} diff --git a/src/bytecode/vm.rs b/src/bytecode/vm.rs new file mode 100644 index 0000000..5381167 --- /dev/null +++ b/src/bytecode/vm.rs @@ -0,0 +1,236 @@ +//! Stack VM for Marqdo bytecode. + +use anyhow::{bail, Result}; + +use crate::bytecode::{Op, Program}; +use crate::value::Value; + +pub struct Vm { + capture: bool, + pub captured_stdout: String, +} + +struct Frame { + fn_idx: usize, + ip: usize, + slots: Vec, +} + +impl Vm { + pub fn new() -> Self { + Self { + capture: false, + captured_stdout: String::new(), + } + } + + pub fn with_capture() -> Self { + Self { + capture: true, + captured_stdout: String::new(), + } + } + + fn emit_line(&mut self, text: &str) { + if self.capture { + self.captured_stdout.push_str(text); + self.captured_stdout.push('\n'); + } else { + println!("{text}"); + } + } + + 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 mut frames = vec![Frame { + fn_idx: entry, + ip: 0, + slots: vec![Value::None; program.functions[entry].locals.len().max(1)], + }]; + + loop { + 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 op = fun.code[frame.ip]; + frame.ip += 1; + + match op { + Op::Constant(i) => stack.push(fun.constants[i as usize].clone()), + Op::True => stack.push(Value::Bool(true)), + Op::False => stack.push(Value::Bool(false)), + Op::None_ => stack.push(Value::None), + Op::Pop => { + stack.pop(); + } + Op::GetLocal(i) => { + let v = frames.last().unwrap().slots[i as usize].clone(); + stack.push(v); + } + Op::SetLocal(i) => { + let v = stack.last().cloned().unwrap_or(Value::None); + let slot = i as usize; + let fr = frames.last_mut().unwrap(); + if fr.slots.len() <= slot { + fr.slots.resize(slot + 1, Value::None); + } + fr.slots[slot] = v; + } + Op::Add => { + let b = pop(&mut stack)?; + let a = pop(&mut stack)?; + stack.push(add(a, b)?); + } + Op::Sub => bin_int(&mut stack, |a, b| a - b)?, + Op::Mul => bin_int(&mut stack, |a, b| a * b)?, + Op::Div => { + let b = pop(&mut stack)?; + let a = pop(&mut stack)?; + match (a, b) { + (Value::Int(_), Value::Int(0)) => bail!("division by zero"), + (Value::Int(x), Value::Int(y)) => stack.push(Value::Int(x / y)), + _ => bail!("`/` needs ints"), + } + } + Op::Negate => match pop(&mut stack)? { + Value::Int(n) => stack.push(Value::Int(-n)), + _ => bail!("unary `-` needs int"), + }, + Op::Not => { + let v = pop(&mut stack)?; + stack.push(Value::Bool(!v.truthy())); + } + Op::Equal => { + let b = pop(&mut stack)?; + let a = pop(&mut stack)?; + stack.push(Value::Bool(a == b)); + } + Op::NotEqual => { + let b = pop(&mut stack)?; + let a = pop(&mut stack)?; + 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::Jump(t) => { + frames.last_mut().unwrap().ip = t as usize; + } + Op::JumpIfFalse(t) => { + let v = pop(&mut stack)?; + if !v.truthy() { + frames.last_mut().unwrap().ip = t as usize; + } + } + Op::Print => { + let v = pop(&mut stack)?; + self.emit_line(&v.as_display()); + } + 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.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::GetIndex => { + let idx = pop(&mut stack)?; + let list = pop(&mut stack)?; + 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"), + } + } + Op::Call(fid, argc) => { + let fid = fid as usize; + let argc = argc as usize; + let callee = &program.functions[fid]; + if callee.params.len() != argc { + bail!("bytecode: call argc 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)?; + } + // 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, + slots, + }); + } + Op::Return => { + let ret = pop(&mut stack).unwrap_or(Value::None); + frames.pop(); + if frames.is_empty() { + return Ok(ret); + } + stack.push(ret); + } + } + } + } +} + +fn pop(stack: &mut Vec) -> Result { + stack + .pop() + .ok_or_else(|| anyhow::anyhow!("bytecode: stack underflow")) +} + +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"), + } +} + +fn bin_int(stack: &mut Vec, f: impl Fn(i64, i64) -> i64) -> Result<()> { + let b = pop(stack)?; + let a = pop(stack)?; + match (a, b) { + (Value::Int(x), Value::Int(y)) => { + stack.push(Value::Int(f(x, y))); + Ok(()) + } + _ => bail!("binary op needs ints"), + } +} + +fn cmp(stack: &mut Vec, pred: impl Fn(std::cmp::Ordering) -> bool) -> Result<()> { + 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"), + }; + stack.push(Value::Bool(pred(ord))); + Ok(()) +} diff --git a/src/capture.rs b/src/capture.rs new file mode 100644 index 0000000..1849b3d --- /dev/null +++ b/src/capture.rs @@ -0,0 +1,9 @@ +//! Captured program run (for tests and `view`). + +use crate::value::Value; + +#[derive(Debug, Clone)] +pub struct RunCapture { + pub stdout: String, + pub value: Value, +} diff --git a/src/diagnostics.rs b/src/diagnostics.rs index 3836fe6..31ed45d 100644 --- a/src/diagnostics.rs +++ b/src/diagnostics.rs @@ -1,16 +1,59 @@ //! Diagnostics with file/line/column. -//! Expanded in M1+. use std::fmt; +use std::path::{Path, PathBuf}; -#[derive(Debug, Clone, Copy)] +use thiserror::Error; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Span { pub line: u32, pub col: u32, } +impl Span { + pub fn new(line: u32, col: u32) -> Self { + Self { line, col } + } +} + impl fmt::Display for Span { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{}:{}", self.line, self.col) } } + +/// Unified diagnostic: `path:line:col: message` (path omitted if unknown). +#[derive(Debug, Error, Clone)] +#[error("{}", self.format_message())] +pub struct Diagnostic { + pub path: Option, + pub span: Span, + pub message: String, +} + +impl Diagnostic { + pub fn new(path: Option<&Path>, span: Span, message: impl Into) -> Self { + Self { + path: path.map(|p| p.to_path_buf()), + span, + message: message.into(), + } + } + + pub fn at(path: &Path, span: Span, message: impl Into) -> Self { + Self::new(Some(path), span, message) + } + + pub fn format_message(&self) -> String { + match &self.path { + Some(p) => format!("{}:{}: {}", p.display(), self.span, self.message), + None => format!("{}: {}", self.span, self.message), + } + } +} + +/// Convenience for `anyhow` sites that already have a span. +pub fn bail_at(path: Option<&Path>, span: Span, message: impl Into) -> anyhow::Error { + anyhow::Error::new(Diagnostic::new(path, span, message)) +} diff --git a/src/interp.rs b/src/interp.rs index 3fe7544..446315b 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -1,15 +1,21 @@ //! Tree-walk interpreter (Phase I). use anyhow::{bail, Result}; -use std::collections::HashMap; +use std::collections::{HashMap, HashSet}; +use std::path::{Path, PathBuf}; use crate::ast::{ - BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp, + Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp, }; +use crate::diagnostics::{bail_at, Span}; use crate::value::Value; pub struct Interpreter { + pub path: Option, pub trace: bool, + capture: bool, + pub captured_stdout: String, + current_span: Span, } struct Env { @@ -32,16 +38,49 @@ impl Env { } } +enum EvArg { + Positional(Value), + Named(String, Value), +} + impl Interpreter { - pub fn new(trace: bool) -> Self { - Self { trace } + pub fn new(path: Option<&Path>, trace: bool) -> Self { + Self { + path: path.map(|p| p.to_path_buf()), + trace, + capture: false, + captured_stdout: String::new(), + current_span: Span::new(1, 1), + } + } + + pub fn with_capture(path: Option<&Path>, trace: bool) -> Self { + Self { + path: path.map(|p| p.to_path_buf()), + trace, + capture: true, + captured_stdout: String::new(), + current_span: Span::new(1, 1), + } + } + + fn err(&self, message: impl Into) -> anyhow::Error { + bail_at(self.path.as_deref(), self.current_span, message) + } + + fn emit_line(&mut self, text: &str) { + if self.capture { + self.captured_stdout.push_str(text); + self.captured_stdout.push('\n'); + } else { + println!("{text}"); + } } 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(), &[]); } - // No main: run each level-1 function with empty args (hello-style demos). let tops: Vec<&Function> = module.functions.iter().filter(|f| f.level == 1).collect(); if tops.is_empty() { bail!("no `# main` and no level-1 function to run"); @@ -68,7 +107,6 @@ impl Interpreter { for (k, v) in args { env.set(k.clone(), v.clone()); } - // Bind missing params to None for p in &fun.params { if env.get(p).is_none() { env.set(p.clone(), Value::None); @@ -88,7 +126,6 @@ impl Interpreter { Ok(ret) } - /// Returns Some if function should return. fn exec_stmt( &mut self, module: &Module, @@ -98,6 +135,7 @@ impl Interpreter { ) -> Result> { match stmt { Stmt::Assign { name, value, span } => { + self.current_span = *span; let v = self.eval_expr(module, fun, env, value)?; if self.trace { eprintln!(" assign {name} = {v:?} @{span}"); @@ -106,6 +144,7 @@ impl Interpreter { Ok(None) } Stmt::Return { value, span } => { + self.current_span = *span; let v = self.eval_expr(module, fun, env, value)?; if self.trace { eprintln!(" return {v:?} @{span}"); @@ -113,6 +152,7 @@ impl Interpreter { Ok(Some(v)) } Stmt::Call { call, span } => { + self.current_span = *span; if self.trace { eprintln!(" call-stmt {} @{span}", call.callee); } @@ -120,6 +160,7 @@ impl Interpreter { Ok(None) } Stmt::Branch { arms, span } => { + self.current_span = *span; if self.trace { eprintln!(" branch @{span}"); } @@ -144,15 +185,15 @@ impl Interpreter { body, span, } => { + self.current_span = *span; if self.trace { eprintln!(" while @{span}"); } - // Guard against infinite loops in buggy programs. let mut guard = 0u32; while self.eval_expr(module, fun, env, condition)?.truthy() { guard += 1; if guard > 1_000_000 { - bail!("{span}: while loop exceeded iteration limit"); + return Err(self.err("while loop exceeded iteration limit")); } for s in body { if let Some(v) = self.exec_stmt(module, fun, env, s)? { @@ -168,16 +209,18 @@ impl Interpreter { body, span, } => { + self.current_span = *span; if self.trace { eprintln!(" foreach {item} in {collection} @{span}"); } - let coll = env - .get(collection) - .cloned() - .ok_or_else(|| anyhow::anyhow!("{span}: undefined collection `{collection}`"))?; + let coll = env.get(collection).cloned().ok_or_else(|| { + self.err(format!("undefined collection `{collection}`")) + })?; let items = match coll { Value::List(xs) => xs, - other => bail!("{span}: foreach needs a list, got {other:?}"), + other => { + return Err(self.err(format!("foreach needs a list, got {other:?}"))); + } }; for val in items { env.set(item.clone(), val); @@ -204,7 +247,7 @@ impl Interpreter { Expr::Var(name) => env .get(name) .cloned() - .ok_or_else(|| anyhow::anyhow!("undefined variable `{name}`")), + .ok_or_else(|| self.err(format!("undefined variable `{name}`"))), Expr::Interp(parts) => { let mut s = String::new(); for part in parts { @@ -213,7 +256,7 @@ impl Interpreter { InterpPart::Var(n) => { let v = env .get(n) - .ok_or_else(|| anyhow::anyhow!("undefined variable `{n}`"))?; + .ok_or_else(|| self.err(format!("undefined variable `{n}`")))?; s.push_str(&v.as_display()); } } @@ -226,13 +269,12 @@ impl Interpreter { UnaryOp::Not => Ok(Value::Bool(!v.truthy())), UnaryOp::Neg => match v { Value::Int(n) => Ok(Value::Int(-n)), - _ => bail!("unary `-` needs int"), + _ => Err(self.err("unary `-` needs int")), }, } } Expr::Binary { op, left, right } => { let l = self.eval_expr(module, fun, env, left)?; - // Short-circuit and/or match op { BinaryOp::And => { if !l.truthy() { @@ -251,7 +293,7 @@ impl Interpreter { _ => {} } let r = self.eval_expr(module, fun, env, right)?; - eval_binary(*op, &l, &r) + eval_binary(*op, &l, &r).map_err(|e| self.err(e.to_string())) } Expr::Call(call) => self.eval_call(module, fun, env, call), Expr::List(items) => { @@ -271,26 +313,41 @@ impl Interpreter { env: &mut Env, call: &CallExpr, ) -> Result { - let mut arg_vals = Vec::new(); - for (k, e) in &call.args { - arg_vals.push((k.clone(), self.eval_expr(module, fun, env, e)?)); + let mut ev_args = Vec::new(); + for arg in &call.args { + match arg { + Arg::Positional(e) => { + ev_args.push(EvArg::Positional(self.eval_expr(module, fun, env, e)?)); + } + Arg::Named { name, value } => { + ev_args.push(EvArg::Named( + name.clone(), + self.eval_expr(module, fun, env, value)?, + )); + } + } } match call.callee.as_str() { "print" => { - let text = arg_vals - .iter() - .find(|(k, _)| k == "text") - .map(|(_, v)| v.as_display()) - .ok_or_else(|| anyhow::anyhow!("print requires text=..."))?; - println!("{text}"); + let bound = bind_args(&["text".into()], &ev_args, false) + .map_err(|m| self.err(m))?; + let text = bound + .get("text") + .map(|v| v.as_display()) + .ok_or_else(|| self.err("print requires text (named or positional)"))?; + self.emit_line(&text); return Ok(Value::None); } "input" => { - let prompt = arg_vals - .iter() - .find(|(k, _)| k == "prompt") - .map(|(_, v)| v.as_display()) + 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}"); @@ -310,27 +367,71 @@ impl Interpreter { _ => {} } - let target = lookup_function(module, fun, &call.callee).ok_or_else(|| { - anyhow::anyhow!("unknown function `{}`", call.callee) - })?; + let target = lookup_function(module, fun, &call.callee) + .ok_or_else(|| self.err(format!("unknown function `{}`", call.callee)))?; - // Bind by param names: prefer named args matching params; also allow arg keys as env names. + let bound = bind_args(&target.params, &ev_args, false).map_err(|m| self.err(m))?; let mut call_env = Env::new(); - for p in &target.params { - if let Some((_, v)) = arg_vals.iter().find(|(k, _)| k == p) { - call_env.set(p.clone(), v.clone()); - } - } - for (k, v) in &arg_vals { - if call_env.get(k).is_none() { - call_env.set(k.clone(), v.clone()); - } + for (k, v) in bound { + call_env.set(k, v); } self.run_function(module, target, call_env, &[]) } } +/// Bind evaluated args onto parameter names. Err is a plain message (caller adds span/path). +fn bind_args( + params: &[String], + args: &[EvArg], + allow_missing: bool, +) -> std::result::Result, String> { + let mut named: HashMap = HashMap::new(); + let mut positionals = Vec::new(); + for a in args { + match a { + EvArg::Positional(v) => positionals.push(v.clone()), + EvArg::Named(k, v) => { + if named.contains_key(k) { + return Err(format!("duplicate named argument `{k}`")); + } + named.insert(k.clone(), v.clone()); + } + } + } + + let mut out: HashMap = HashMap::new(); + let mut used_named = HashSet::new(); + let mut pos_i = 0usize; + + for p in params { + if let Some(v) = named.get(p) { + out.insert(p.clone(), v.clone()); + used_named.insert(p.clone()); + } else if pos_i < positionals.len() { + out.insert(p.clone(), positionals[pos_i].clone()); + pos_i += 1; + } else if !allow_missing { + return Err(format!("missing argument for parameter `{p}`")); + } + } + + if pos_i < positionals.len() { + return Err(format!( + "too many positional arguments ({} extra)", + positionals.len() - pos_i + )); + } + + for (k, v) in named { + if !used_named.contains(&k) && !out.contains_key(&k) { + out.insert(k, v); + } + } + + Ok(out) +} + fn find_top<'a>(module: &'a Module, name: &str) -> Option<&'a Function> { module.functions.iter().find(|f| f.name == name) } @@ -340,11 +441,9 @@ fn lookup_function<'a>( current: &'a Function, name: &str, ) -> Option<&'a Function> { - // Nested children of current (hoisted). if let Some(f) = find_in_tree(current, name) { return Some(f); } - // Top-level / imported. module.functions.iter().find(|f| f.name == name) } @@ -376,7 +475,7 @@ fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result { (Value::Text(a), Value::Text(b)) => Ok(Value::Text(format!("{a}{b}"))), (Value::Text(a), b) => Ok(Value::Text(format!("{a}{}", b.as_display()))), (a, Value::Text(b)) => Ok(Value::Text(format!("{}{b}", a.as_display()))), - _ => bail!("`+` needs ints or text, got {l:?} and {r:?}"), + _ => bail!("`+` needs ints or text"), }, BinaryOp::Sub => match (l, r) { (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a - b)), @@ -391,8 +490,8 @@ fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result { (Value::Int(a), Value::Int(b)) => Ok(Value::Int(a / b)), _ => bail!("`/` needs ints"), }, - BinaryOp::Eq => Ok(Value::Bool(values_eq(l, r))), - BinaryOp::Ne => Ok(Value::Bool(!values_eq(l, r))), + BinaryOp::Eq => Ok(Value::Bool(l == r)), + BinaryOp::Ne => Ok(Value::Bool(l != r)), BinaryOp::Lt => cmp_ord(l, r, |o| o.is_lt()), BinaryOp::Le => cmp_ord(l, r, |o| o.is_le()), BinaryOp::Gt => cmp_ord(l, r, |o| o.is_gt()), @@ -401,10 +500,6 @@ fn eval_binary(op: BinaryOp, l: &Value, r: &Value) -> Result { } } -fn values_eq(l: &Value, r: &Value) -> bool { - l == r -} - fn cmp_ord(l: &Value, r: &Value, f: impl Fn(std::cmp::Ordering) -> bool) -> Result { match (l, r) { (Value::Int(a), Value::Int(b)) => Ok(Value::Bool(f(a.cmp(b)))), diff --git a/src/lex/mod.rs b/src/lex/mod.rs index da8c140..a937812 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -122,7 +122,7 @@ mod tests { #[test] fn classify_hello_fixture() { - let src = include_str!("../../examples/hello.mq.md"); + let src = include_str!("../../examples/structure/hello.mq.md"); let lines = classify_source(src); assert!(lines.iter().any(|l| l.kind == LineKind::Code && l.text.contains("print"))); assert!(lines.iter().any(|l| l.kind == LineKind::Comment)); diff --git a/src/lib.rs b/src/lib.rs index b431f41..bfbc477 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,11 @@ -//! Marqdo reference interpreter (Phase I). +//! Marqdo reference interpreter (Phase I + M5 bytecode prototype). //! -//! Pipeline: load → line classify → parse → tree-walk eval. +//! Pipeline: load → line classify → parse → tree-walk eval | bytecode VM. +//! CLI also offers `view` for AST-backed browsing. pub mod ast; +pub mod bytecode; +pub mod capture; pub mod debug; pub mod diagnostics; pub mod interp; @@ -10,16 +13,36 @@ pub mod lex; pub mod load; pub mod parse; pub mod value; +pub mod view; use std::path::Path; use anyhow::{bail, Result}; use crate::ast::format_ast_dump; +use crate::bytecode::{compile_module, Vm}; +use crate::capture::RunCapture; use crate::interp::Interpreter; use crate::lex::{classify_source, format_lines_dump}; use crate::load::load_module; +#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] +pub enum Backend { + #[default] + Tree, + Bytecode, +} + +impl Backend { + pub fn parse(s: &str) -> Result { + match s { + "tree" => Ok(Self::Tree), + "bytecode" | "bc" => Ok(Self::Bytecode), + other => bail!("unknown backend `{other}` (expected tree|bytecode)"), + } + } +} + /// Options for a single `run` / dump invocation. #[derive(Debug, Clone, Default)] pub struct RunOptions { @@ -27,7 +50,9 @@ pub struct RunOptions { pub dump_tokens: bool, pub dump_ast: bool, pub dump_sema: bool, + pub dump_bytecode: bool, pub trace_eval: bool, + pub backend: Backend, } impl RunOptions { @@ -37,16 +62,23 @@ impl RunOptions { dump_tokens: true, dump_ast: true, dump_sema: true, + dump_bytecode: true, trace_eval: true, + backend: Backend::Tree, } } pub fn any_dump(&self) -> bool { - self.dump_lines || self.dump_tokens || self.dump_ast || self.dump_sema || self.trace_eval + self.dump_lines + || self.dump_tokens + || self.dump_ast + || self.dump_sema + || self.dump_bytecode + || self.trace_eval } } -/// Run a `.mq.md` program. +/// Run a `.mq.md` program (prints to real stdout). pub fn run_file(path: &Path, opts: &RunOptions) -> Result { let source = std::fs::read_to_string(path) .map_err(|e| anyhow::anyhow!("failed to read {}: {e}", path.display()))?; @@ -86,13 +118,56 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result { println!("=== marqdo: end sema ==="); } - let mut interp = Interpreter::new(opts.trace_eval); - if opts.trace_eval { - eprintln!("=== marqdo: trace-eval ({path_label}) ==="); + match opts.backend { + Backend::Tree => { + let mut interp = Interpreter::new(Some(path), opts.trace_eval); + if opts.trace_eval { + eprintln!("=== marqdo: trace-eval ({path_label}) ==="); + } + interp.run_module(&module)?; + if opts.trace_eval { + eprintln!("=== marqdo: end trace-eval ==="); + } + } + Backend::Bytecode => { + let program = compile_module(&module)?; + if opts.dump_bytecode { + print!("{}", program.disassemble()); + } + let mut vm = Vm::new(); + vm.run(&program)?; + } } - interp.run_module(&module)?; - if opts.trace_eval { - eprintln!("=== marqdo: end trace-eval ==="); + + // dump bytecode even on tree backend if requested + if opts.backend == Backend::Tree && opts.dump_bytecode { + let program = compile_module(&module)?; + print!("{}", program.disassemble()); } + Ok(0) } + +/// Run and capture stdout (used by `view` and tests). +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 value = interp.run_module(&module)?; + Ok(RunCapture { + stdout: interp.captured_stdout, + value, + }) + } + Backend::Bytecode => { + let program = compile_module(&module)?; + let mut vm = Vm::with_capture(); + let value = vm.run(&program)?; + Ok(RunCapture { + stdout: vm.captured_stdout, + value, + }) + } + } +} diff --git a/src/load.rs b/src/load.rs index fb895b4..ef40be4 100644 --- a/src/load.rs +++ b/src/load.rs @@ -6,6 +6,7 @@ use std::path::{Path, PathBuf}; use anyhow::{bail, Context, Result}; use crate::ast::{Function, Module}; +use crate::diagnostics::Diagnostic; use crate::parse::parse_source; /// Load `path` and recursively merge imported modules' top-level functions. @@ -24,15 +25,13 @@ fn load_module_inner(path: &Path, visited: &mut HashSet) -> Result) -> Result anyhow::Error { + if let Some(d) = err.downcast_ref::() { + if d.path.is_none() { + return Diagnostic::at(path, d.span, d.message.clone()).into(); + } + return err; + } + // Legacy `line:col: message` from parse bail! + let msg = err.to_string(); + if let Some((loc, rest)) = msg.split_once(": ") { + if let Some((line_s, col_s)) = loc.split_once(':') { + if let (Ok(line), Ok(col)) = (line_s.parse::(), col_s.parse::()) { + return Diagnostic::at(path, crate::diagnostics::Span::new(line, col), rest).into(); + } + } + } + err.context(format!("in {}", path.display())) +} + fn merge_top_level(into: &mut Vec, fun: Function) { if let Some(existing) = into.iter_mut().find(|f| f.name == fun.name) { - // Later definition wins (entry file overrides import). *existing = fun; } else { into.push(fun); diff --git a/src/main.rs b/src/main.rs index 7228b98..5425a7d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,49 +2,72 @@ use std::path::PathBuf; use std::process::ExitCode; use anyhow::Result; -use clap::{Parser, Subcommand}; +use clap::{Parser, Subcommand, ValueEnum}; -use marqdo::RunOptions; +use marqdo::view::{serve, ViewOptions}; +use marqdo::{Backend, RunOptions}; #[derive(Parser, Debug)] -#[command(name = "marqdo", version, about = "Marqdo interpreter — run .mq.md programs")] +#[command(name = "marqdo", version, about = "Marqdo interpreter — run and view .mq.md programs")] struct Cli { #[command(subcommand)] command: Commands, } +#[derive(Clone, Copy, Debug, ValueEnum)] +enum BackendCli { + Tree, + Bytecode, +} + +impl From for Backend { + fn from(b: BackendCli) -> Self { + match b { + BackendCli::Tree => Backend::Tree, + BackendCli::Bytecode => Backend::Bytecode, + } + } +} + #[derive(Subcommand, Debug)] enum Commands { /// Execute a Marqdo source file (defaults to ./index.mq.md) Run { - /// Path to a `.mq.md` file #[arg(value_name = "FILE")] file: Option, - /// Dump line classification + #[arg(long, value_enum, default_value_t = BackendCli::Tree)] + backend: BackendCli, + #[arg(long)] dump_lines: bool, - - /// Dump tokens (placeholder until lexer fills in) #[arg(long)] dump_tokens: bool, - - /// Dump AST (placeholder until parser exists) #[arg(long)] dump_ast: bool, - - /// Dump semantic info (placeholder) #[arg(long)] dump_sema: bool, - - /// Trace evaluation (placeholder) + #[arg(long)] + dump_bytecode: bool, #[arg(long)] trace_eval: bool, - - /// Enable all dumps #[arg(long)] dump_all: bool, }, + /// Browse `.mq.md` structure + execution in a local webpage + View { + #[arg(value_name = "PATH")] + path: Option, + + #[arg(long, default_value = "127.0.0.1")] + host: String, + + #[arg(long, default_value_t = 7429)] + port: u16, + + #[arg(long)] + no_open: bool, + }, } fn main() -> ExitCode { @@ -62,10 +85,12 @@ fn try_main() -> Result { match cli.command { Commands::Run { file, + backend, dump_lines, dump_tokens, dump_ast, dump_sema, + dump_bytecode, trace_eval, dump_all, } => { @@ -78,19 +103,30 @@ fn try_main() -> Result { dump_tokens, dump_ast, dump_sema, + dump_bytecode, trace_eval, + backend: backend.into(), } }; - // dump_all already set; individual flags merge if not dump_all - if !dump_all { - opts.dump_lines = dump_lines; - opts.dump_tokens = dump_tokens; - opts.dump_ast = dump_ast; - opts.dump_sema = dump_sema; - opts.trace_eval = trace_eval; + if dump_all { + opts.backend = backend.into(); } marqdo::run_file(&path, &opts)?; Ok(0) } + Commands::View { + path, + host, + port, + no_open, + } => { + serve(ViewOptions { + path: path.unwrap_or_else(|| PathBuf::from(".")), + host, + port, + open_browser: !no_open, + })?; + Ok(0) + } } } diff --git a/src/parse/expr.rs b/src/parse/expr.rs index 38601b4..1dc8b58 100644 --- a/src/parse/expr.rs +++ b/src/parse/expr.rs @@ -15,17 +15,41 @@ pub fn parse_expr(input: &str) -> Result { } /// Argument / template RHS: prefer a full expression; else interpolated text. +/// Hyphenated prose (`none-falsy`) is not parsed as subtraction. pub fn parse_value_or_interp(input: &str) -> Result { let s = input.trim(); if s.is_empty() { return Ok(Expr::Literal(Literal::Text(String::new()))); } + if has_hyphenated_prose(s) { + return Ok(parse_interp(s)); + } match parse_expr(s) { Ok(e) => Ok(e), Err(_) => Ok(parse_interp(s)), } } +fn has_hyphenated_prose(s: &str) -> bool { + let chars: Vec = s.chars().collect(); + for i in 1..chars.len().saturating_sub(1) { + if chars[i] != '-' { + continue; + } + let left = chars[i - 1]; + let right = chars[i + 1]; + if left.is_whitespace() || right.is_whitespace() || left == '`' || right == '`' { + continue; + } + // `none-falsy` / `Hello-World` — not `` `n` - 1 `` + if left.is_alphanumeric() || right.is_alphanumeric() || !left.is_ascii() || !right.is_ascii() + { + return true; + } + } + false +} + pub fn parse_interp(s: &str) -> Expr { let mut parts = Vec::new(); let mut rest = s; @@ -329,18 +353,20 @@ fn is_word_end(s: &str, len: usize) -> bool { } } -/// Parse `name key=val key2=val2` from a string; returns call + bytes consumed. +/// Parse `name key=val …` / positional args; returns call + bytes consumed. pub fn parse_call_tail(s: &str) -> Result<(CallExpr, usize)> { - let s = s.trim_start(); - let trimmed_lead = s.len(); // not used — work on s - let _ = trimmed_lead; - let original_len = s.len(); // we'll compute from full — caller passes rest after > + use crate::ast::Arg; - let mut parts = s.split_whitespace(); - let callee = parts - .next() - .ok_or_else(|| anyhow::anyhow!("missing callee"))? - .to_string(); + let s = s.trim_start(); + let original_len = s.len(); + + let callee = { + let mut parts = s.split_whitespace(); + parts + .next() + .ok_or_else(|| anyhow::anyhow!("missing callee"))? + .to_string() + }; let after_callee_offset = { let idx = s.find(&callee).unwrap(); @@ -348,26 +374,68 @@ pub fn parse_call_tail(s: &str) -> Result<(CallExpr, usize)> { }; let mut args_str = s[after_callee_offset..].trim_start(); let mut args = Vec::new(); + let mut seen_named = false; - // Support one or more `key=value`; value may contain spaces until next ` key=` pattern. while !args_str.is_empty() { - let eq = args_str - .find('=') - .ok_or_else(|| anyhow::anyhow!("expected key=value in call args, got {args_str:?}"))?; - let key = args_str[..eq].trim().to_string(); - if key.is_empty() { - bail!("empty argument name"); + if let Some((key, after_eq)) = try_named_arg(args_str) { + seen_named = true; + let val_end = find_next_arg_boundary(after_eq); + let val_raw = after_eq[..val_end].trim_end(); + args.push(Arg::Named { + name: key, + value: parse_value_or_interp(val_raw)?, + }); + args_str = after_eq[val_end..].trim_start(); + } else { + if seen_named { + bail!("positional argument after named argument is not allowed"); + } + let (tok, rest) = split_first_token(args_str); + if tok.is_empty() { + break; + } + args.push(Arg::Positional(parse_value_or_interp(tok)?)); + args_str = rest.trim_start(); } - let after_eq = &args_str[eq + 1..]; - // Value runs until ` word=` where word is next key, or end. - let val_end = find_next_arg_boundary(after_eq); - let val_raw = after_eq[..val_end].trim_end(); - args.push((key, parse_value_or_interp(val_raw)?)); - args_str = after_eq[val_end..].trim_start(); } - let consumed = original_len; // entire rest consumed for stmt-form calls - Ok((CallExpr { callee, args }, consumed)) + Ok((CallExpr { callee, args }, original_len)) +} + +/// If `s` begins with `ident=`, return (ident, rest_after_eq). +fn try_named_arg(s: &str) -> Option<(String, &str)> { + let s = s.trim_start(); + for (i, c) in s.char_indices() { + if c == '=' { + if i == 0 { + return None; + } + let key = &s[..i]; + if key.chars().any(|ch| ch.is_whitespace()) { + return None; + } + return Some((key.to_string(), &s[i + 1..])); + } + if c.is_whitespace() { + return None; + } + } + None +} + +fn split_first_token(s: &str) -> (&str, &str) { + let s = s.trim_start(); + if s.starts_with('`') { + // `` `name` `` as one token + if let Some(end) = s[1..].find('`') { + let end = end + 2; // include closing ` + return (&s[..end], &s[end..]); + } + } + match s.find(char::is_whitespace) { + Some(i) => (&s[..i], &s[i..]), + None => (s, ""), + } } fn find_next_arg_boundary(after_eq: &str) -> usize { diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 76ef14a..8751a61 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -9,7 +9,7 @@ use anyhow::{bail, Result}; use crate::ast::{ BranchArm, Expr, Function, Literal, Module, Stmt, }; -use crate::diagnostics::Span; +use crate::diagnostics::{Diagnostic, Span}; use crate::lex::{classify_source, ClassifiedLine, LineKind}; use crate::parse::expr::parse_call_after_gt as call_after_gt; use crate::parse::expr::parse_expr as parse_expr_str; @@ -217,7 +217,7 @@ impl<'a> Cursor<'a> { } } - bail!("{span}: unrecognized statement: {trimmed}") + Err(Diagnostic::new(None, span, format!("unrecognized statement: {trimmed}")).into()) } fn parse_backtick_assign(&mut self, trimmed: &str, span: Span) -> Result> { @@ -525,7 +525,7 @@ mod tests { #[test] fn parse_hello() { - let src = include_str!("../../examples/hello.mq.md"); + let src = include_str!("../../examples/structure/hello.mq.md"); let m = parse_source(src).unwrap(); assert_eq!(m.functions.len(), 1); assert_eq!(m.functions[0].name, "Hello World"); @@ -535,7 +535,7 @@ mod tests { #[test] fn parse_index() { - let src = include_str!("../../examples/index.mq.md"); + let src = include_str!("../../examples/structure/nested-call.mq.md"); let m = parse_source(src).unwrap(); assert!(m.imports.is_empty()); let main = m.functions.iter().find(|f| f.name == "main").unwrap(); @@ -546,7 +546,7 @@ mod tests { #[test] fn parse_branch() { - let src = include_str!("../../examples/branch.mq.md"); + let src = include_str!("../../examples/structure/branch.mq.md"); let m = parse_source(src).unwrap(); let main = &m.functions[0]; assert!(main.body.iter().any(|s| matches!(s, Stmt::Branch { .. }))); diff --git a/src/view/html.rs b/src/view/html.rs new file mode 100644 index 0000000..ac5267f --- /dev/null +++ b/src/view/html.rs @@ -0,0 +1,339 @@ +//! HTML shell for `marqdo view`. + +use std::path::Path; + +use crate::view::render::FileViewModel; + +pub fn escape(s: &str) -> String { + s.chars() + .map(|c| match c { + '&' => "&".into(), + '<' => "<".into(), + '>' => ">".into(), + '"' => """.into(), + '\'' => "'".into(), + _ => c.to_string(), + }) + .collect() +} + +pub fn layout(title: &str, nav: &str, main: &str) -> String { + format!( + r##" + + + + +{title} · marqdo view + + + + + +
+ +
+ {main} +
+
+ + +"##, + title = escape(title), + nav = nav, + main = main + ) +} + +pub fn nav_html(files: &[std::path::PathBuf], active: Option<&str>) -> String { + let mut out = String::from("

Files

    "); + let mut last_folder = String::new(); + for f in files { + let rel = f.to_string_lossy().replace('\\', "/"); + let folder = Path::new(&rel) + .parent() + .map(|p| p.to_string_lossy().to_string()) + .unwrap_or_default(); + if folder != last_folder { + if !folder.is_empty() { + out.push_str(&format!( + "
{}
    ", + escape(&folder) + )); + } + last_folder = folder; + } + let name = Path::new(&rel) + .file_name() + .and_then(|s| s.to_str()) + .unwrap_or(&rel); + let active_cls = if active == Some(rel.as_str()) { + " active" + } else { + "" + }; + let href = format!("/file?path={}", urlencoding_encode(&rel)); + out.push_str(&format!( + "
  • {}
  • ", + escape(name) + )); + } + out.push_str("
"); + out +} + +fn urlencoding_encode(s: &str) -> String { + let mut out = String::new(); + for b in s.bytes() { + match b { + b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' => { + out.push(b as char); + } + _ => out.push_str(&format!("%{b:02X}")), + } + } + out +} + +pub fn page_index(files: &[std::path::PathBuf], only: Option<&Path>) -> String { + let nav = nav_html(files, None); + let main = if let Some(p) = only { + let rel = p.to_string_lossy().replace('\\', "/"); + format!( + "

Single file

Open {}

", + urlencoding_encode(&rel), + escape(&rel) + ) + } else { + format!( + "

Browse

Select a .mq.md file in the index. Structure is rendered from the AST; output comes from the same pipeline as marqdo run.

{} file(s)

", + files.len() + ) + }; + layout("index", &nav, &main) +} + +pub fn page_file(files: &[std::path::PathBuf], rel: &str, vm: &FileViewModel) -> String { + let nav = nav_html(files, Some(rel)); + let status = if vm.ok { "ok" } else { "fail" }; + let out_text = if vm.ok { + if vm.stdout.is_empty() { + "(no stdout)".to_string() + } else { + escape(&vm.stdout) + } + } else { + escape(&vm.stderr) + }; + let main = format!( + r#" +

{title}

+

{rel}

+
+

Structure

+ {structure} +
+
+

Execution

+
{out}
+
+
+

Source

+
{source}
+
+"#, + title = escape( + Path::new(rel) + .file_stem() + .and_then(|s| s.to_str()) + .unwrap_or(rel) + ), + rel = escape(rel), + structure = vm.structure_html, + status = status, + out = out_text, + source = escape(&vm.source), + ); + layout(rel, &nav, &main) +} diff --git a/src/view/mod.rs b/src/view/mod.rs new file mode 100644 index 0000000..871de0d --- /dev/null +++ b/src/view/mod.rs @@ -0,0 +1,307 @@ +//! `marqdo view` — local HTML browser for `.mq.md` structure + output. + +mod html; +mod render; + +use std::fs; +use std::net::SocketAddr; +use std::path::{Path, PathBuf}; +use std::process::Command; + +use anyhow::{bail, Context, Result}; +use tiny_http::{Header, Method, Request, Response, Server, StatusCode}; + +use crate::view::html::{escape, page_file, page_index}; +use crate::view::render::{render_module_structure, FileViewModel}; +use crate::{run_file_capture, RunOptions}; + +pub struct ViewOptions { + pub path: PathBuf, + pub host: String, + pub port: u16, + pub open_browser: bool, +} + +struct ViewRoot { + /// Directory that paths are resolved against. + root: PathBuf, + /// If Some, only this relative file is in scope (single-file mode). + only_file: Option, + files: Vec, +} + +/// Block serving until Ctrl+C (process kill). +pub fn serve(opts: ViewOptions) -> Result<()> { + let root_info = build_root(&opts.path)?; + let addr: SocketAddr = format!("{}:{}", opts.host, opts.port) + .parse() + .context("invalid host/port")?; + let server = Server::http(addr).map_err(|e| anyhow::anyhow!("listen {addr}: {e}"))?; + let url = format!("http://{}:{}/", opts.host, opts.port); + eprintln!("marqdo view: {url}"); + eprintln!("root: {}", root_info.root.display()); + eprintln!("{} .mq.md file(s). Ctrl+C to stop.", root_info.files.len()); + + if opts.open_browser { + let _ = open_url(&url); + } + + for request in server.incoming_requests() { + if let Err(err) = handle(&root_info, request) { + eprintln!("view request error: {err:#}"); + } + } + Ok(()) +} + +fn build_root(path: &Path) -> Result { + let path = if path.exists() { + path.canonicalize().unwrap_or_else(|_| path.to_path_buf()) + } else { + bail!("path not found: {}", path.display()); + }; + + if path.is_file() { + if !is_mq_md(&path) { + bail!("not a .mq.md file: {}", path.display()); + } + let root = path + .parent() + .map(|p| p.to_path_buf()) + .unwrap_or_else(|| PathBuf::from(".")); + let rel = PathBuf::from(path.file_name().unwrap()); + return Ok(ViewRoot { + root, + only_file: Some(rel.clone()), + files: vec![rel], + }); + } + + if !path.is_dir() { + bail!("not a file or directory: {}", path.display()); + } + + let mut files = Vec::new(); + collect_mq_md(&path, &path, &mut files)?; + files.sort(); + Ok(ViewRoot { + root: path, + only_file: None, + files, + }) +} + +fn collect_mq_md(root: &Path, dir: &Path, out: &mut Vec) -> Result<()> { + for entry in fs::read_dir(dir).with_context(|| format!("read_dir {}", dir.display()))? { + let entry = entry?; + let p = entry.path(); + if p.is_dir() { + collect_mq_md(root, &p, out)?; + } else if is_mq_md(&p) { + let rel = p.strip_prefix(root).unwrap_or(&p).to_path_buf(); + out.push(rel); + } + } + Ok(()) +} + +fn is_mq_md(path: &Path) -> bool { + path.file_name() + .and_then(|s| s.to_str()) + .map(|s| s.ends_with(".mq.md")) + .unwrap_or(false) +} + +fn handle(root: &ViewRoot, request: Request) -> Result<()> { + let url = request.url().to_string(); + let method = request.method().clone(); + if method != Method::Get { + let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405)); + let _ = request.respond(resp); + return Ok(()); + } + + let (path_part, query) = split_url(&url); + match path_part { + "/" | "/index.html" => { + let body = page_index(&root.files, root.only_file.as_deref()); + respond_html(request, body) + } + "/file" => { + let rel = query_param(query, "path").unwrap_or_default(); + let resolved = resolve_rel(root, &rel)?; + let vm = build_file_view(&resolved, &rel)?; + let body = page_file(&root.files, &rel, &vm); + respond_html(request, body) + } + "/api/tree" => { + let json = files_json(&root.files); + respond_json(request, json) + } + _ => { + let resp = Response::from_string("not found").with_status_code(StatusCode(404)); + let _ = request.respond(resp); + Ok(()) + } + } +} + +fn build_file_view(abs: &Path, rel: &str) -> 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), + Err(e) => format!( + "
parse/load error: {}
", + escape(&format!("{e:#}")) + ), + }; + let (stdout, stderr, ok) = match run_file_capture(abs, &RunOptions::default()) { + Ok(cap) => (cap.stdout, String::new(), true), + Err(e) => (String::new(), format!("{e:#}"), false), + }; + Ok(FileViewModel { + rel_path: rel.to_string(), + source, + structure_html: structure, + stdout, + stderr, + ok, + }) +} + +fn resolve_rel(root: &ViewRoot, rel: &str) -> Result { + if rel.is_empty() { + bail!("missing path"); + } + let rel_path = PathBuf::from(rel.replace('\\', "/")); + if rel_path + .components() + .any(|c| matches!(c, std::path::Component::ParentDir)) + { + bail!("path escapes root"); + } + if let Some(only) = &root.only_file { + if &rel_path != only { + bail!("file not in view scope"); + } + } else if !root.files.iter().any(|f| f == &rel_path) { + // also allow forward-slash normalized compare + let norm = rel_path.to_string_lossy().replace('\\', "/"); + if !root + .files + .iter() + .any(|f| f.to_string_lossy().replace('\\', "/") == norm) + { + bail!("unknown file: {rel}"); + } + } + let abs = root.root.join(&rel_path); + let canon = abs.canonicalize().unwrap_or(abs.clone()); + let root_canon = root.root.canonicalize().unwrap_or_else(|_| root.root.clone()); + if !canon.starts_with(&root_canon) { + bail!("path escapes root"); + } + if !is_mq_md(&canon) { + bail!("not a .mq.md file"); + } + Ok(canon) +} + +fn split_url(url: &str) -> (&str, &str) { + match url.split_once('?') { + Some((p, q)) => (p, q), + None => (url, ""), + } +} + +fn query_param<'a>(query: &'a str, key: &str) -> Option { + for pair in query.split('&') { + let mut kv = pair.splitn(2, '='); + let k = kv.next()?; + let v = kv.next().unwrap_or(""); + if k == key { + return Some(urlencoding_decode(v)); + } + } + None +} + +fn urlencoding_decode(s: &str) -> String { + // Minimal decode for %XX and + + let mut out = String::new(); + let bytes = s.as_bytes(); + let mut i = 0; + while i < bytes.len() { + match bytes[i] { + b'+' => { + out.push(' '); + i += 1; + } + b'%' if i + 2 < bytes.len() => { + let hex = &s[i + 1..i + 3]; + if let Ok(v) = u8::from_str_radix(hex, 16) { + out.push(v as char); + i += 3; + } else { + out.push('%'); + i += 1; + } + } + c => { + out.push(c as char); + i += 1; + } + } + } + out +} + +fn files_json(files: &[PathBuf]) -> String { + let list: Vec = files + .iter() + .map(|f| format!("\"{}\"", escape_json(&f.to_string_lossy().replace('\\', "/")))) + .collect(); + format!("{{\"files\":[{}]}}", list.join(",")) +} + +fn escape_json(s: &str) -> String { + s.replace('\\', "\\\\").replace('"', "\\\"") +} + +fn respond_html(request: Request, body: String) -> Result<()> { + let header = Header::from_bytes(&b"Content-Type"[..], &b"text/html; charset=utf-8"[..]) + .map_err(|_| anyhow::anyhow!("header"))?; + let resp = Response::from_string(body).with_header(header); + request.respond(resp).context("respond")?; + Ok(()) +} + +fn respond_json(request: Request, body: String) -> Result<()> { + let header = Header::from_bytes(&b"Content-Type"[..], &b"application/json; charset=utf-8"[..]) + .map_err(|_| anyhow::anyhow!("header"))?; + let resp = Response::from_string(body).with_header(header); + request.respond(resp).context("respond")?; + Ok(()) +} + +fn open_url(url: &str) -> Result<()> { + #[cfg(target_os = "windows")] + { + Command::new("cmd") + .args(["/C", "start", "", url]) + .spawn() + .context("open browser")?; + } + #[cfg(target_os = "macos")] + { + Command::new("open").arg(url).spawn().context("open browser")?; + } + #[cfg(all(unix, not(target_os = "macos")))] + { + Command::new("xdg-open") + .arg(url) + .spawn() + .context("open browser")?; + } + Ok(()) +} diff --git a/src/view/render.rs b/src/view/render.rs new file mode 100644 index 0000000..b34085b --- /dev/null +++ b/src/view/render.rs @@ -0,0 +1,424 @@ +//! Render Marqdo AST as HTML fragments for `view`. +//! +//! Comments are not in the AST; they are interleaved from classified source lines. +//! Expressions are shown in surface syntax, never Rust `Debug`. + +use crate::ast::{ + Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp, +}; +use crate::lex::{classify_source, ClassifiedLine, LineKind}; +use crate::view::html::escape; + +pub struct FileViewModel { + #[allow(dead_code)] + pub rel_path: String, + pub source: String, + pub structure_html: String, + pub stdout: String, + pub stderr: String, + pub ok: bool, +} + +pub fn render_module_structure(module: &Module, source: &str) -> String { + let lines = classify_source(source); + let mut out = String::new(); + + if !module.imports.is_empty() { + out.push_str("
import"); + for imp in &module.imports { + out.push_str(&format!("{}", escape(imp))); + } + out.push_str("
"); + } + + let body_start = skip_frontmatter_end(&lines).unwrap_or(1); + let mut cursor = body_start; + + if module.functions.is_empty() { + out.push_str(&emit_comments(&lines, cursor, u32::MAX)); + if out.is_empty() { + out.push_str("

(empty module)

"); + } + return out; + } + + // Leading comments before first function + let first = module.functions[0].span.line; + out.push_str(&emit_comments(&lines, cursor, first)); + cursor = first; + + for (i, fun) in module.functions.iter().enumerate() { + if i > 0 { + out.push_str(&emit_comments(&lines, cursor, fun.span.line)); + } + let (html, next) = render_fun(fun, &lines, 0); + out.push_str(&html); + cursor = next; + } + + // Trailing comments after last function tree + out.push_str(&emit_comments(&lines, cursor, u32::MAX)); + out +} + +/// Line after closing frontmatter `---`, or 1 if none. +fn skip_frontmatter_end(lines: &[ClassifiedLine]) -> Option { + let first_code = lines.iter().find(|l| l.kind == LineKind::Code)?; + if first_code.text.trim() != "---" { + return Some(1); + } + for l in lines.iter().skip_while(|l| l.line_no <= first_code.line_no) { + if l.kind == LineKind::Code && l.text.trim() == "---" { + return Some(l.line_no + 1); + } + } + Some(1) +} + +fn emit_comments(lines: &[ClassifiedLine], from_line: u32, before_line: u32) -> String { + let mut s = String::new(); + for l in lines { + if l.line_no < from_line { + continue; + } + if l.line_no >= before_line { + break; + } + if l.kind == LineKind::Comment { + let text = l.text.trim(); + if text.is_empty() { + continue; + } + s.push_str(&format!( + "
comment{}
", + escape(text) + )); + } + } + s +} + +fn render_fun(fun: &Function, lines: &[ClassifiedLine], depth: usize) -> (String, u32) { + let mut s = String::new(); + let nest = if depth > 0 { " nested" } else { "" }; + s.push_str(&format!("
")); + s.push_str(&format!( + "
fn · h{}{}
", + fun.level, + escape(&fun.name) + )); + if !fun.params.is_empty() { + s.push_str("
"); + for p in &fun.params { + s.push_str(&format!("{}", escape(p))); + } + s.push_str("
"); + } + + let mut cursor = fun.span.line + 1; + + // Comments / gap before first body stmt or child + let first_body = fun.body.first().map(stmt_start); + let first_child = fun.children.first().map(|c| c.span.line); + let first_content = match (first_body, first_child) { + (Some(a), Some(b)) => Some(a.min(b)), + (Some(a), None) => Some(a), + (None, Some(b)) => Some(b), + (None, None) => None, + }; + if let Some(fc) = first_content { + s.push_str(&emit_comments(lines, cursor, fc)); + cursor = fc; + } + + // Interleave body stmts and nested functions by source line + let mut bi = 0usize; + let mut ci = 0usize; + while bi < fun.body.len() || ci < fun.children.len() { + let stmt_line = fun.body.get(bi).map(stmt_start); + let child_line = fun.children.get(ci).map(|c| c.span.line); + let take_stmt = match (stmt_line, child_line) { + (Some(sl), Some(cl)) => sl <= cl, + (Some(_), None) => true, + (None, Some(_)) => false, + (None, None) => break, + }; + if take_stmt { + let stmt = &fun.body[bi]; + let start = stmt_start(stmt); + s.push_str(&emit_comments(lines, cursor, start)); + let (html, end) = render_stmt(stmt, lines); + s.push_str(&html); + cursor = end; + bi += 1; + } else { + let child = &fun.children[ci]; + s.push_str(&emit_comments(lines, cursor, child.span.line)); + let (html, end) = render_fun(child, lines, depth + 1); + s.push_str(&format!("
{html}
")); + cursor = end; + ci += 1; + } + } + + s.push_str("
"); + (s, cursor) +} + +fn stmt_start(stmt: &Stmt) -> u32 { + match stmt { + Stmt::Assign { span, .. } + | Stmt::Return { span, .. } + | Stmt::Call { span, .. } + | Stmt::Branch { span, .. } + | Stmt::While { span, .. } + | Stmt::ForEach { span, .. } => span.line, + } +} + +fn stmt_end_line(stmt: &Stmt) -> u32 { + match stmt { + Stmt::Assign { span, .. } | Stmt::Return { span, .. } | Stmt::Call { span, .. } => { + span.line + 1 + } + Stmt::Branch { arms, span, .. } => { + let mut end = span.line + 1; + for arm in arms { + for st in &arm.body { + end = end.max(stmt_end_line(st)); + } + } + end + } + Stmt::While { body, span, .. } | Stmt::ForEach { body, span, .. } => { + let mut end = span.line + 1; + for st in body { + end = end.max(stmt_end_line(st)); + } + end + } + } +} + +fn render_stmt(stmt: &Stmt, lines: &[ClassifiedLine]) -> (String, u32) { + let end = stmt_end_line(stmt); + let html = match stmt { + Stmt::Assign { name, value, .. } => format!( + "
bind`{}` = {}
", + escape(name), + escape(&expr_display(value)) + ), + Stmt::Return { value, .. } => format!( + "
return{}
", + escape(&expr_display(value)) + ), + Stmt::Call { call, .. } => format!( + "
call{}
", + escape(&call_display(call)) + ), + Stmt::Branch { arms, .. } => { + let mut inner = + String::from("
branch"); + let mut arm_cursor = stmt_start(stmt) + 1; + for arm in arms { + let label = match &arm.condition { + None => "else".to_string(), + Some(c) => expr_display(c), + }; + // comments before first stmt in arm (approx: between arms hard; use body starts) + let arm_first = arm.body.first().map(stmt_start); + inner.push_str(&format!( + "
arm{}", + escape(&label) + )); + if let Some(af) = arm_first { + inner.push_str(&emit_comments(lines, arm_cursor, af)); + arm_cursor = af; + } + inner.push_str("
"); + for st in &arm.body { + let start = stmt_start(st); + inner.push_str(&emit_comments(lines, arm_cursor, start)); + let (h, e) = render_stmt(st, lines); + inner.push_str(&h); + arm_cursor = e; + } + inner.push_str("
"); + } + inner.push_str("
"); + inner + } + Stmt::While { + condition, body, .. + } => { + let mut inner = format!( + "
while{}
", + escape(&expr_display(condition)) + ); + let mut cursor = stmt_start(stmt) + 1; + for st in body { + let start = stmt_start(st); + inner.push_str(&emit_comments(lines, cursor, start)); + let (h, e) = render_stmt(st, lines); + inner.push_str(&h); + cursor = e; + } + inner.push_str("
"); + inner + } + Stmt::ForEach { + item, + collection, + body, + .. + } => { + let mut inner = format!( + "
foreach[{}]({})
", + escape(item), + escape(collection) + ); + let mut cursor = stmt_start(stmt) + 1; + for st in body { + let start = stmt_start(st); + inner.push_str(&emit_comments(lines, cursor, start)); + let (h, e) = render_stmt(st, lines); + inner.push_str(&h); + cursor = e; + } + inner.push_str("
"); + inner + } + }; + (html, end) +} + +fn call_display(call: &CallExpr) -> String { + let mut s = format!("> {}", call.callee); + for a in &call.args { + match a { + Arg::Positional(e) => { + s.push(' '); + s.push_str(&expr_display(e)); + } + Arg::Named { name, value } => { + s.push(' '); + s.push_str(name); + s.push('='); + s.push_str(&expr_display(value)); + } + } + } + s +} + +/// Surface-syntax preview for view (not Debug). +pub fn expr_display(expr: &Expr) -> String { + expr_prec(expr, 0) +} + +fn expr_prec(expr: &Expr, parent_prec: u8) -> String { + match expr { + Expr::Literal(lit) => lit_display(lit), + Expr::Var(name) => format!("`{name}`"), + Expr::Interp(parts) => { + let mut s = String::new(); + for p in parts { + match p { + InterpPart::Lit(t) => s.push_str(t), + InterpPart::Var(n) => { + s.push('`'); + s.push_str(n); + s.push('`'); + } + } + } + s + } + Expr::Unary { op, expr } => { + let prec = 5; + let inner = match op { + UnaryOp::Not => format!("not {}", expr_prec(expr, prec)), + UnaryOp::Neg => format!("-{}", expr_prec(expr, prec)), + }; + wrap(inner, prec, parent_prec) + } + Expr::Binary { op, left, right } => { + let (sym, prec) = bin_sym_prec(*op); + let inner = format!( + "{} {} {}", + expr_prec(left, prec), + sym, + expr_prec(right, prec + 1) // left-assoc bias + ); + wrap(inner, prec, parent_prec) + } + Expr::Call(call) => call_display(call), + Expr::List(items) => { + let parts: Vec = items.iter().map(|e| expr_prec(e, 0)).collect(); + format!("[{}]", parts.join(", ")) + } + } +} + +fn wrap(inner: String, prec: u8, parent_prec: u8) -> String { + if prec < parent_prec { + format!("({inner})") + } else { + inner + } +} + +fn bin_sym_prec(op: BinaryOp) -> (&'static str, u8) { + match op { + BinaryOp::Or => ("or", 1), + BinaryOp::And => ("and", 2), + BinaryOp::Eq => ("==", 3), + BinaryOp::Ne => ("!=", 3), + BinaryOp::Lt => ("<", 3), + BinaryOp::Le => ("<=", 3), + BinaryOp::Gt => (">", 3), + BinaryOp::Ge => (">=", 3), + BinaryOp::Add => ("+", 4), + BinaryOp::Sub => ("-", 4), + BinaryOp::Mul => ("*", 5), + BinaryOp::Div => ("/", 5), + } +} + +fn lit_display(lit: &Literal) -> String { + match lit { + Literal::None => "None".into(), + Literal::Bool(true) => "True".into(), + Literal::Bool(false) => "False".into(), + Literal::Int(n) => n.to_string(), + Literal::Text(t) => t.clone(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::ast::{BinaryOp, Expr, Literal}; + + #[test] + fn expr_gt_is_surface() { + let e = Expr::Binary { + op: BinaryOp::Gt, + left: Box::new(Expr::Var("x".into())), + right: Box::new(Expr::Literal(Literal::Int(0))), + }; + assert_eq!(expr_display(&e), "`x` > 0"); + assert!(!expr_display(&e).contains("Binary")); + } + + #[test] + fn structure_keeps_comments_and_surface_expr() { + let src = include_str!("../../examples/structure/nested-call.mq.md"); + let module = crate::parse::parse_source(src).unwrap(); + let html = render_module_structure(&module, src); + assert!(html.contains("comment-card")); + assert!(html.contains("print")); + assert!(!html.contains("Binary {")); + assert!(!html.contains("Literal(Int")); + } +} diff --git a/tests/cli_m0.rs b/tests/cli_m0.rs deleted file mode 100644 index e32b01e..0000000 --- a/tests/cli_m0.rs +++ /dev/null @@ -1,65 +0,0 @@ -use std::process::Command; - -fn run(args: &[&str]) -> (i32, String, String) { - let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) - .args(args) - .output() - .expect("failed to run 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) -} - -#[test] -fn hello_prints_greeting() { - let (code, stdout, stderr) = run(&["run", "examples/hello.mq.md"]); - assert_eq!(code, 0, "stderr={stderr}"); - assert_eq!(stdout.trim_end(), "Hello World!"); -} - -#[test] -fn index_nested_call() { - let (code, stdout, stderr) = run(&["run", "examples/index.mq.md"]); - assert_eq!(code, 0, "stderr={stderr}"); - assert_eq!(stdout.trim_end(), "Hello World!"); -} - -#[test] -fn branch_else_arm() { - let (code, stdout, stderr) = run(&["run", "examples/branch.mq.md"]); - assert_eq!(code, 0, "stderr={stderr}"); - assert_eq!(stdout.trim_end(), "零"); -} - -#[test] -fn loop_while_and_foreach() { - let (code, stdout, stderr) = run(&["run", "examples/loop.mq.md"]); - assert_eq!(code, 0, "stderr={stderr}"); - assert_eq!( - stdout.trim_end(), - "3\n2\n1\n今天吃苹果\n今天吃梨" - ); -} - -#[test] -fn collection_foreach() { - let (code, stdout, stderr) = run(&["run", "examples/collection.mq.md"]); - assert_eq!(code, 0, "stderr={stderr}"); - assert_eq!(stdout.trim_end(), "苹果\n梨\n桃"); -} - -#[test] -fn with_import() { - let (code, stdout, stderr) = run(&["run", "examples/with-import.mq.md"]); - assert_eq!(code, 0, "stderr={stderr}"); - assert_eq!(stdout.trim_end(), "42\n你好,Marqdo!"); -} - -#[test] -fn hello_dump_lines_still_runs() { - let (code, stdout, stderr) = run(&["run", "examples/hello.mq.md", "--dump-lines"]); - assert_eq!(code, 0, "stderr={stderr}"); - assert!(stdout.contains("=== marqdo: lines")); - assert!(stdout.contains("Hello World!")); -} diff --git a/tests/gold.rs b/tests/gold.rs new file mode 100644 index 0000000..7a09212 --- /dev/null +++ b/tests/gold.rs @@ -0,0 +1,206 @@ +use std::process::Command; + +fn run(args: &[&str]) -> (i32, String, String) { + let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(args) + .output() + .expect("failed to run 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}" + ); +} + +#[test] +fn structure_hello() { + assert_out("examples/structure/hello.mq.md", "Hello World!"); +} + +#[test] +fn structure_nested_call() { + assert_out("examples/structure/nested-call.mq.md", "Hello World!"); +} + +#[test] +fn structure_positional_call() { + assert_out( + "examples/structure/positional-call.mq.md", + "Hello Marqdo!\nHello World!", + ); +} + +#[test] +fn structure_branch() { + assert_out("examples/structure/branch.mq.md", "零"); +} + +#[test] +fn structure_loop() { + assert_out( + "examples/structure/loop.mq.md", + "3\n2\n1\n今天吃苹果\n今天吃梨", + ); +} + +#[test] +fn structure_collection() { + assert_out("examples/structure/collection.mq.md", "苹果\n梨\n桃"); +} + +#[test] +fn structure_import() { + assert_out( + "examples/structure/import/main.mq.md", + "42\n你好,Marqdo!", + ); +} + +#[test] +fn keywords_print() { + assert_out( + "examples/keywords/print.mq.md", + "named form\npositional", + ); +} + +#[test] +fn keywords_bool_logic() { + assert_out( + "examples/keywords/bool-logic.mq.md", + "ok\nnone-falsy", + ); +} + +#[test] +fn error_undefined_var() { + assert_err( + "examples/errors/undefined-var.mq.md", + "undefined variable `missing`", + ); +} + +#[test] +fn error_unknown_fn() { + assert_err( + "examples/errors/unknown-fn.mq.md", + "unknown function `no_such_fn`", + ); +} + +#[test] +fn error_bad_arity() { + assert_err( + "examples/errors/bad-arity.mq.md", + "missing argument for parameter `x`", + ); +} + +#[test] +fn error_syntax_bad_line() { + assert_err( + "examples/errors/syntax-bad-line.mq.md", + "unrecognized statement", + ); +} + +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}"); + assert_eq!( + stdout.trim_end(), + expect.trim_end(), + "{path} backend={backend}" + ); +} + +#[test] +fn bytecode_hello() { + assert_out_backend( + "examples/structure/hello.mq.md", + "bytecode", + "Hello World!", + ); +} + +#[test] +fn bytecode_branch() { + assert_out_backend("examples/structure/branch.mq.md", "bytecode", "零"); +} + +#[test] +fn bytecode_loop() { + assert_out_backend( + "examples/structure/loop.mq.md", + "bytecode", + "3\n2\n1\n今天吃苹果\n今天吃梨", + ); +} + +#[test] +fn bytecode_collection() { + assert_out_backend( + "examples/structure/collection.mq.md", + "bytecode", + "苹果\n梨\n桃", + ); +} + +#[test] +fn bytecode_print_keyword() { + assert_out_backend( + "examples/keywords/print.mq.md", + "bytecode", + "named form\npositional", + ); +} + +#[test] +fn bytecode_bool_logic() { + assert_out_backend( + "examples/keywords/bool-logic.mq.md", + "bytecode", + "ok\nnone-falsy", + ); +} + +#[test] +fn bytecode_nested_call() { + assert_out_backend( + "examples/structure/nested-call.mq.md", + "bytecode", + "Hello World!", + ); +} + +#[test] +fn bytecode_positional_call() { + assert_out_backend( + "examples/structure/positional-call.mq.md", + "bytecode", + "Hello Marqdo!\nHello World!", + ); +} + +#[test] +fn bytecode_import() { + assert_out_backend( + "examples/structure/import/main.mq.md", + "bytecode", + "42\n你好,Marqdo!", + ); +}