feat(debug): dump v0.3 prose bits, inferred params, and dead binds

--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 <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-09-14 15:33:45 +08:00
1 parent 6994acb91d
commit 7554e45f60
12 files changed
+173 -58

No files matched your search

+29 -4
View File
@@ -59,6 +59,8 @@ pub struct Function {
pub children: Vec<Function>,
/// Base type name for `# Child = > Parent` (objects only).
pub base: Option<String>,
/// Prose `` `名` `` never read/assigned on the executable surface (not params).
pub dead_binds: Vec<String>,
}
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<String> = 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(", "))
}
+6 -6
View File
@@ -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:?}");
+1 -1
View File
@@ -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]));
+80
View File
@@ -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::<String>())
} 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::<Vec<_>>()
.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::<String>())
}
}
#[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}");
}
}
+5 -5
View File
@@ -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<!-- marqdo-out\n42\n-->\n\n> print text=tail\n";
let src = "# main\n\n**x = 1**\n<!-- marqdo-out\n42\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<!-- marqdo-out\nold\n-->\n\n> print text=tail\n";
let src = "# main\n\n**x = 1**\n<!-- marqdo-out\nold\n-->\n\n> print text=tail\n";
let line = 3u32;
let strip_insert = |s: &str, body: &str| {
let stripped = find_adjacent_block(s, line)
+1 -1
View File
@@ -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");
+3 -2
View File
@@ -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<i32> {
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}) ===");
+22 -1
View File
@@ -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<String>)> = 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::<Vec<_>>(), ["题干"]);
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]
+1 -1
View File
@@ -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));
}
}