From 7554e45f6090fc078e75b734aa1486e3ee5c92b1 Mon Sep 17 00:00:00 2001 From: cflmy Date: Mon, 14 Sep 2026 15:33:45 +0800 Subject: [PATCH] feat(debug): dump v0.3 prose bits, inferred params, and dead binds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --dump-lines tags [decl]/[bold]/[italic]; --dump-ast prints `inferred` params and `dead=[…]`. Migrate leftover italic-assign unit fixtures to bold/return. Co-authored-by: Cursor --- doc/design/code-vs-comment.md | 44 ++++++---------- doc/design/markdown-mapping-v0.3.md | 14 ++--- doc/design/pipeline-debug.md | 4 +- src/ast.rs | 33 ++++++++++-- src/browser_session.rs | 12 ++--- src/debug/ctrl.rs | 2 +- src/debug/mod.rs | 80 +++++++++++++++++++++++++++++ src/host/writeback.rs | 10 ++-- src/input_feed.rs | 2 +- src/lib.rs | 5 +- src/parse/mod.rs | 23 ++++++++- src/view/mod.rs | 2 +- 12 files changed, 173 insertions(+), 58 deletions(-) diff --git a/doc/design/code-vs-comment.md b/doc/design/code-vs-comment.md index 841558d..523269d 100644 --- a/doc/design/code-vs-comment.md +++ b/doc/design/code-vs-comment.md @@ -2,9 +2,12 @@ | | | |---|---| -| 状态 | 与 mapping **v0.2** 对齐(成段注释);**v0.3 草案**见 [markdown-mapping-v0.3.md](markdown-mapping-v0.3.md) §4 | -| 日期 | 2026-08-04 | -| 修订 | 2026-09-13:注明 v0.3 叙述可含声明与行内代码段 | +| 状态 | 与 mapping **v0.3** 对齐(现行运行时) | +| 日期 | 2026-09-14 | +| 取代 | 本文曾按 v0.2(`*语句*` / `**返回**`)叙述;旧对照见 [markdown-mapping.md](markdown-mapping.md) | +| 相关 | [markdown-mapping-v0.3.md](markdown-mapping-v0.3.md) §4 | + +「Markup」在此仓库 = Marqdo **把 Markdown 标记映射成语法** 的那一层,不是另一个语言。 --- @@ -12,19 +15,18 @@ | 层 | 形态 | 角色 | |----|------|------| -| **叙述面** | 成段注释(空行分段) | 注释;段内任意标记无语义 | -| **Marqdo 可执行面** | 结构标记 + `*语句*` + `**返回**` | 本语言程序 | +| **叙述面** | 普通段落 | 注释;行内 `` `名` `` 可声明/引用;装饰性 `**强调**` / `*斜体*` 不执行 | +| **Marqdo 可执行面** | 结构标记 + **`**代码**`** + **`*返回*`** | 本语言程序 | | **外联面** | ` ```lang ` | 其它语言(FFI) | -输出属于可执行面里的**函数调用**(`> print …`),见 [keywords.md](keywords.md)。 +输出属于可执行面里的**函数调用**(`> print …` / `**print …**`),见 [keywords.md](keywords.md)。 ### 成段注释 - 以**空行**划分段落。 -- 段首行:首字符非标记 → 注释。 -- 段内续行:一律注释(即使以 `` ` `` / `-` / `**` 等开头)。 -- **例外**:整行仅为 `**expr**`(或 `****`)→ Marqdo **返回**;`**强调**` 后还有文字 → 仍是叙述注释。 -- 叙述段与可执行代码之间**必须空一行**,否则 `> print` 等会被吞进注释段。 +- 段首行:首字符非标记 → 注释段;段内续行一律叙述(即使以 `` ` `` / `-` 开头)。 +- 段内仍扫描行内可执行位:`` `名` ``、代码形 `**…**`、返回形 `*…*`。 +- 叙述段与独立结构行(`>` `#`)之间**建议空一行**,否则 `> print` 可能被吞进注释段。 --- @@ -32,22 +34,8 @@ | 标记/形式 | 角色 | 是否架构级 | |-----------|------|------------| -| `*…*` | 程序语句 | 是 | -| `**…**` | 返回值 | 是 | -| `****` / 空粗体 | 空返回 + 结束函数体 | 是 | +| `**…**` | 代码(赋值 / 调用 / 表达式语句) | 是 | +| `*…*` | 返回值(整行、`返回*n*`、或表达式形) | 是 | +| `****` / 整行 `**` / `*None*` | 空返回 + 结束函数体 | 是 | | `> print …` | 打印副作用 | 否(内置函数) | -| 函数体 `---` / `***` | 结束函数体 | 是(边界) | - ---- - -## v0.3 草案预告(未实现) - -[mapping v0.3](markdown-mapping-v0.3.md) 拟改为: - -| 层 | 形态 | -|----|------| -| 叙述面 | 普通段落;`` `名` `` 可声明/引用 | -| 可执行面 | 结构标记 + **`**代码**`** + **`*返回*`**;调用可无 `>` | -| 外联面 | 不变 | - -现行实现与上表(v0.2)一致,直至路线图默认切换。 +| 函数体 `---` / `***` | 叙述分隔(跳过,**不**结束函数体) | 否 | diff --git a/doc/design/markdown-mapping-v0.3.md b/doc/design/markdown-mapping-v0.3.md index 99fafb3..2623403 100644 --- a/doc/design/markdown-mapping-v0.3.md +++ b/doc/design/markdown-mapping-v0.3.md @@ -570,7 +570,7 @@ FnDef.params: | 3 | 粗体内调用 vs 赋值 | **已落地** | §6.2:可赋值左值 → 赋值;否则表达式优先、再裸调用;`**n + 1**` = ExprStmt | | 4 | `` `n`+1 `` 误声明 | **已落地** | Decl 名须为标识符;`` `n+1` `` 不升 Decl | | 5 | 叙述强调斜体 vs 返回 | **已落地** | 整行 / `返回*…*` / 表达式形才当返回;`*only*` `*very important*` `*foo.bar*` 为叙述 | -| 6 | 一行多 `**…**` | **建议采纳** | 允许,按出现序执行(M0 钉死为是) | +| 6 | 一行多 `**…**` | **已落地** | 允许,按出现序执行(`tests/markup-v03/multi-bold.mq.md`) | | 7 | view 展示 prose Decl | **已落地** | Structure / Functions 大纲:`inferred` 形参标注(`chip inferred`) | --- @@ -598,13 +598,13 @@ FnDef.params: - [x] **空返回**对照其它语言并钉死词法 - [x] **提及/未使用变量**政策钉死 -实现验收(后续): +实现验收: -- [x] 金样例夹具 `tests/markup-v03/`(11 个 · `gold.rs` 暂 ignore) -- 动机示例金样例通过(去掉 ignore) -- `--dump-ast` 可见 inferred params 与死绑定(若 dump) -- 空返回四种形式金样例通过 -- 旧 v0.2 套件在旗标下行为不变,直到默认切换 +- [x] 金样例夹具 `tests/markup-v03/`(`gold.rs` `markup_v03_*`) +- [x] 动机示例金样例通过(`inc-prose.mq.md`) +- [x] `--dump-ast` 可见 inferred params 与死绑定;`--dump-lines` 标注 `[decl]` / `[bold]` / `[italic]` +- [x] 空返回四种形式金样例通过 +- [x] 无 v0.2 双模式(默认即 v0.3) --- diff --git a/doc/design/pipeline-debug.md b/doc/design/pipeline-debug.md index 3cdb3a1..b2fa25f 100644 --- a/doc/design/pipeline-debug.md +++ b/doc/design/pipeline-debug.md @@ -23,9 +23,9 @@ marqdo dump FILE --stage lines|tokens|ast|sema|all | 标志 | 阶段 | 输出内容 | |------|------|----------| -| `--dump-lines` | 行分类 | 每行:`LINE kind=Comment\|Code\|Blank` + 原文预览 | +| `--dump-lines` | 行分类 | 每行:`kind=Comment\|Code\|Blank` + 原文;v0.3 行内位 `[decl n]` / `[bold …]` / `[italic …]` | | `--dump-tokens` | 词法 | Token 流:`type lexeme line:col` | -| `--dump-ast` | 语法 | S 表达式或缩进树(模块/函数/语句) | +| `--dump-ast` | 语法 | 模块/函数/语句;形参带 `inferred`;叙述死绑定 `dead=[…]` | | `--dump-sema` | 语义 | 作用域、导入图、提升后的符号表 | | `--trace-eval` | 求值 | **JSON 行**(stderr):语句级事件,含 `path` / `span`(`line:col`) | | `--dump-bytecode` | 字节码 | 反汇编 `Program`(指令旁带 `@line:col`;见 [bytecode.md](bytecode.md)) | diff --git a/src/ast.rs b/src/ast.rs index 50212f3..d298ddc 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -59,6 +59,8 @@ pub struct Function { pub children: Vec, /// Base type name for `# Child = > Parent` (objects only). pub base: Option, + /// Prose `` `名` `` never read/assigned on the executable surface (not params). + pub dead_binds: Vec, } impl Function { @@ -273,14 +275,20 @@ pub fn format_ast_dump(path: &str, module: &Module) -> String { fn dump_fun(out: &mut String, fun: &Function, depth: usize) { let pad = " ".repeat(depth); let kind = if fun.is_object() { "object" } else { "fun" }; + let params = format_params(&fun.params); + let dead = if fun.dead_binds.is_empty() { + String::new() + } else { + format!(" dead={:?}", fun.dead_binds) + }; match &fun.base { Some(base) => out.push_str(&format!( - "{pad}({kind} level={} {:?} extends={base:?} params={:?} @{}\n", - fun.level, fun.name, fun.params, fun.span + "{pad}({kind} level={} {:?} extends={base:?} params={params}{dead} @{}\n", + fun.level, fun.name, fun.span )), None => out.push_str(&format!( - "{pad}({kind} level={} {:?} params={:?} @{}\n", - fun.level, fun.name, fun.params, fun.span + "{pad}({kind} level={} {:?} params={params}{dead} @{}\n", + fun.level, fun.name, fun.span )), } for stmt in &fun.body { @@ -355,3 +363,20 @@ fn dump_stmt(out: &mut String, stmt: &Stmt, depth: usize) { } } } + +fn format_params(params: &[Param]) -> String { + let parts: Vec = params + .iter() + .map(|p| { + let mut s = p.name.clone(); + if p.default.is_some() { + s.push('?'); + } + if p.inferred { + s.push_str(" inferred"); + } + s + }) + .collect(); + format!("[{}]", parts.join(", ")) +} diff --git a/src/browser_session.rs b/src/browser_session.rs index ed4f3ad..656a247 100644 --- a/src/browser_session.rs +++ b/src/browser_session.rs @@ -97,7 +97,7 @@ import json:lib/json.mq.md # main -*`count` = 0* +**count = 0** `wire` = @@ -105,13 +105,13 @@ import json:lib/json.mq.md |---|--------|------|------| | 1 | "#btn" | click | bump | -**`wire`** +*wire* ## bump -*`count` = count + 1* -*`label` = > str count* -*`patch` = > json.set map=None key="#out" value=label* -**> json.set map=None key="set_text" value=patch** +**count = count + 1** +**label = > str count** +**patch = > json.set map=None key="#out" value=label** +*> json.set map=None key="set_text" value=patch* "##; let (mut sess, wire) = BrowserSession::boot(src).expect("boot"); assert!(matches!(wire, Value::List(_)), "wire={wire:?}"); diff --git a/src/debug/ctrl.rs b/src/debug/ctrl.rs index 4d0a756..ff5782b 100644 --- a/src/debug/ctrl.rs +++ b/src/debug/ctrl.rs @@ -308,7 +308,7 @@ mod tests { use crate::interp::Interpreter; use crate::parse::parse_source; - let src = "# main\n\n*`x` = 1*\n> print text=`x`\n*`x` = 2*\n> print text=`x`\n"; + let src = "# main\n\n**x = 1**\n> print text=`x`\n**x = 2**\n> print text=`x`\n"; let module = parse_source(src).expect("parse"); // Line 4 is first `> print` let ctrl = DebugController::new(HashSet::from([4u32])); diff --git a/src/debug/mod.rs b/src/debug/mod.rs index 3c313b1..8cd0c5c 100644 --- a/src/debug/mod.rs +++ b/src/debug/mod.rs @@ -8,3 +8,83 @@ pub use ctrl::{ snapshot_json, DebugAction, DebugController, DebugPause, DebugSnapshot, }; pub use trace::emit_trace; + +use crate::lex::{ClassifiedLine, LineKind}; +use crate::parse::{scan_prose_line, ProseBit}; + +/// Line dump with v0.3 prose bits (decl / bold / italic) on each line. +pub fn format_lines_dump_v03(path: &str, lines: &[ClassifiedLine]) -> String { + let mut out = String::new(); + out.push_str(&format!("=== marqdo: lines ({path}) ===\n")); + for line in lines { + let preview = if line.text.len() > 80 { + format!("{}…", &line.text.chars().take(80).collect::()) + } else { + line.text.clone() + }; + match line.kind { + LineKind::Blank => { + out.push_str(&format!("{:4} {:7}\n", line.line_no, line.kind)); + } + _ => { + let tags = prose_tags(&preview); + if tags.is_empty() { + out.push_str(&format!( + "{:4} {:7} | {}\n", + line.line_no, line.kind, preview + )); + } else { + out.push_str(&format!( + "{:4} {:7} | {} {}\n", + line.line_no, line.kind, preview, tags + )); + } + } + } + } + out.push_str("=== marqdo: end lines ===\n"); + out +} + +fn prose_tags(text: &str) -> String { + let bits = scan_prose_line(text.trim_end_matches(['\r', '\n'])); + if bits.is_empty() { + return String::new(); + } + bits.iter() + .map(|b| match b { + ProseBit::Decl { name, default, .. } => match default { + Some(d) => format!("[decl {name}={d}]"), + None => format!("[decl {name}]"), + }, + ProseBit::BoldCode { inner, .. } => format!("[bold {}]", compact(inner)), + ProseBit::ItalicReturn { inner, .. } => format!("[italic {}]", compact(inner)), + }) + .collect::>() + .join(" ") +} + +fn compact(s: &str) -> String { + let t = s.trim(); + if t.chars().count() <= 40 { + t.to_string() + } else { + format!("{}…", t.chars().take(40).collect::()) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::lex::classify_source; + + #[test] + fn dump_lines_tags_decl_bold_italic() { + let src = "对于输入变量`n`,我们执行操作**n=n+1**接着返回*n*。\n"; + let lines = classify_source(src); + let dump = format_lines_dump_v03("t.mq.md", &lines); + assert!(dump.contains("[decl n]"), "{dump}"); + assert!(dump.contains("[bold n=n+1]"), "{dump}"); + assert!(dump.contains("[italic n]"), "{dump}"); + } +} diff --git a/src/host/writeback.rs b/src/host/writeback.rs index 0e43cb1..a93b6ec 100644 --- a/src/host/writeback.rs +++ b/src/host/writeback.rs @@ -613,7 +613,7 @@ mod tests { #[test] fn parse_and_insert_adjacent() { - let src = "# main\n\n*`x` = 1*\n"; + let src = "# main\n\n**x = 1**\n"; let block = format_block("1", None); let next = insert_after_line(src, 3, &block).unwrap(); assert!(next.contains("marqdo-out")); @@ -627,17 +627,17 @@ mod tests { #[test] fn writeback_map_uses_line_above() { - let src = "# main\n\n*`x` = 1*\n\n\n> print text=tail\n"; + let src = "# main\n\n**x = 1**\n\n\n> print text=tail\n"; let map = writeback_map(src); assert_eq!(map.get(&3), Some(&"42".to_string())); } #[test] fn keyed_slots_sit_under_anchor_and_do_not_clobber() { - let src = "# main\n\n*`r` = > step *\n\n> print text=after\n"; + let src = "# main\n\n**r = > step**\n\n> print text=after\n"; let with_ok = replace_or_insert_keyed_at(src, 3, "ok", "success-1").unwrap(); let with_both = replace_or_insert_keyed_at(&with_ok, 3, "error", "fail-1").unwrap(); - let step_at = with_both.find("`r` = > step").unwrap(); + let step_at = with_both.find("r = > step").unwrap(); let ok_at = with_both.find("marqdo-out ok").unwrap(); let err_at = with_both.find("marqdo-out error").unwrap(); let after_at = with_both.find("> print text=after").unwrap(); @@ -665,7 +665,7 @@ mod tests { #[test] fn unkeyed_replace_does_not_accumulate_blank_lines() { - let src = "# main\n\n*`x` = 1*\n\n\n> print text=tail\n"; + let src = "# main\n\n**x = 1**\n\n\n> print text=tail\n"; let line = 3u32; let strip_insert = |s: &str, body: &str| { let stripped = find_adjacent_block(s, line) diff --git a/src/input_feed.rs b/src/input_feed.rs index e93ebee..d4c4228 100644 --- a/src/input_feed.rs +++ b/src/input_feed.rs @@ -366,7 +366,7 @@ mod tests { #[test] fn capture_uses_frontmatter_stdin() { - let src = "---\ntitle: t\nstdin: Ada\n---\n\n# main\n\n*`n` = > input*\n\n> print text=`n`\n"; + let src = "---\ntitle: t\nstdin: Ada\n---\n\n# main\n\n**n = > input**\n\n> print text=`n`\n"; let dir = std::env::temp_dir().join("marqdo-fm-stdin-test"); let _ = fs::create_dir_all(&dir); let path = dir.join("fm.mq.md"); diff --git a/src/lib.rs b/src/lib.rs index d6df028..068c6af 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,10 +42,11 @@ use anyhow::{bail, Result}; use crate::host::{flush_auto_plots, HostCaps, HostContext}; use crate::ast::format_ast_dump; +use crate::debug::format_lines_dump_v03; use crate::bytecode::{compile_module, Vm}; use crate::capture::RunCapture; use crate::interp::Interpreter; -use crate::lex::{classify_source, format_lines_dump}; +use crate::lex::classify_source; use crate::load::{load_module, load_module_from_source}; pub use browser_session::{value_as_json, BrowserSession}; @@ -160,7 +161,7 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result { if opts.dump_lines { let lines = classify_source(&source); - print!("{}", format_lines_dump(&path_label, &lines)); + print!("{}", format_lines_dump_v03(&path_label, &lines)); } if opts.dump_tokens { println!("=== marqdo: tokens ({path_label}) ==="); diff --git a/src/parse/mod.rs b/src/parse/mod.rs index f0fe500..d972eed 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -211,6 +211,7 @@ impl<'a> Cursor<'a> { body: Vec::new(), children: Vec::new(), base, + dead_binds: Vec::new(), }; // Prose decls pending promotion (name → optional default text). let mut prose_decls: Vec<(String, Option)> = Vec::new(); @@ -1289,7 +1290,8 @@ fn merge_inferred_params( use std::collections::HashSet; let mut needs_input = HashSet::new(); - collect_names_needing_input(&fun.body, &mut HashSet::new(), &mut needs_input); + let mut bound = HashSet::new(); + collect_names_needing_input(&fun.body, &mut bound, &mut needs_input); for (name, default_txt) in prose_decls { if name == "self" || name == "自" { @@ -1301,6 +1303,9 @@ fn merge_inferred_params( let has_default = default_txt.is_some(); // Elevate: defaulted always; or executable read that needs an external input. if !has_default && !needs_input.contains(name) { + if !bound.contains(name) { + fun.dead_binds.push(name.clone()); + } continue; // dead bind or pure local } let default = if let Some(txt) = default_txt { @@ -1702,6 +1707,22 @@ Candidates may include lexical `score`. assert_eq!(add.params.len(), 1); assert_eq!(add.params[0].name, "n"); assert!(add.params[0].inferred); + let dump = crate::ast::format_ast_dump("t.mq.md", &m); + assert!(dump.contains("params=[n inferred]"), "{dump}"); + assert!(!dump.contains("dead="), "{dump}"); + } + + #[test] + fn parse_dead_binds_visible_in_ast_dump() { + let src = include_str!("../../tests/markup-v03/dead-bind.mq.md"); + let m = parse_source(src).unwrap(); + let echo = m.functions.iter().find(|f| f.name == "回声").unwrap(); + assert_eq!(echo.params.iter().map(|p| p.name.as_str()).collect::>(), ["题干"]); + assert!(echo.dead_binds.contains(&"Markdown".to_string()), "{:?}", echo.dead_binds); + assert!(echo.dead_binds.contains(&"GFM".to_string()), "{:?}", echo.dead_binds); + let dump = crate::ast::format_ast_dump("t.mq.md", &m); + assert!(dump.contains("dead="), "{dump}"); + assert!(dump.contains("Markdown"), "{dump}"); } #[test] diff --git a/src/view/mod.rs b/src/view/mod.rs index e482244..bbb570c 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -833,7 +833,7 @@ mod tests { #[test] fn not_awaiting_when_frontmatter_stdin() { - let src = "---\nstdin: Ada\n---\n\n# main\n\n*`n` = > input*\n"; + let src = "---\nstdin: Ada\n---\n\n# main\n\n**n = > input**\n"; assert!(!awaiting_preset_input(&["p".into()], &[], src)); } }