Emit structured JSON --trace-eval events with source spans.
Tree walk logs enter_fn/stmt/leave_fn; bytecode logs op events for print/input/call/return, paving the way for view debug pause hooks. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
30cd39fd8f
commit
fd36b92224
8 files changed
+190
-14
No files matched your search
@@ -6,6 +6,7 @@ use std::path::{Path, PathBuf};
|
||||
|
||||
use crate::builtin::{builtin_int, builtin_len, builtin_str};
|
||||
use crate::bytecode::{Op, Program};
|
||||
use crate::debug::emit_trace;
|
||||
use crate::diagnostics::{bail_at, Span};
|
||||
use crate::input_feed::InputFeed;
|
||||
use crate::value::Value;
|
||||
@@ -15,6 +16,7 @@ pub struct Vm {
|
||||
capture: bool,
|
||||
pub captured_stdout: String,
|
||||
input: InputFeed,
|
||||
trace: bool,
|
||||
}
|
||||
|
||||
struct Frame {
|
||||
@@ -30,6 +32,7 @@ impl Vm {
|
||||
capture: false,
|
||||
captured_stdout: String::new(),
|
||||
input: InputFeed::new(false, Vec::new()),
|
||||
trace: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,6 +42,7 @@ impl Vm {
|
||||
capture: true,
|
||||
captured_stdout: String::new(),
|
||||
input: InputFeed::new(true, Vec::new()),
|
||||
trace: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,6 +51,11 @@ impl Vm {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_trace(mut self, trace: bool) -> Self {
|
||||
self.trace = trace;
|
||||
self
|
||||
}
|
||||
|
||||
fn emit_line(&mut self, text: &str) {
|
||||
if self.capture {
|
||||
self.captured_stdout.push_str(text);
|
||||
@@ -102,6 +111,32 @@ impl Vm {
|
||||
let span = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1));
|
||||
frame.ip += 1;
|
||||
|
||||
if self.trace {
|
||||
match op {
|
||||
Op::Print | Op::Input | Op::Call(_, _) | Op::Return => {
|
||||
let kind = match op {
|
||||
Op::Print => "print",
|
||||
Op::Input => "input",
|
||||
Op::Call(_, _) => "call",
|
||||
Op::Return => "return",
|
||||
_ => unreachable!(),
|
||||
};
|
||||
let ip_s = ip.to_string();
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(span),
|
||||
"op",
|
||||
&[
|
||||
("kind", kind),
|
||||
("fn", fun.name.as_str()),
|
||||
("ip", ip_s.as_str()),
|
||||
],
|
||||
);
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
match op {
|
||||
Op::Constant(i) => stack.push(fun.constants[i as usize].clone()),
|
||||
Op::True => stack.push(Value::Bool(true)),
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
//! Pipeline dump helpers (see doc/design/pipeline-debug.md).
|
||||
|
||||
mod trace;
|
||||
|
||||
pub use crate::lex::format_lines_dump;
|
||||
pub use trace::emit_trace;
|
||||
@@ -0,0 +1,49 @@
|
||||
//! Structured eval trace (P3-D1): JSON lines on stderr.
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
use crate::diagnostics::{display_path, Span};
|
||||
|
||||
/// Emit one JSON object line to stderr when tracing.
|
||||
pub fn emit_trace(path: Option<&Path>, span: Option<Span>, event: &str, fields: &[(&str, &str)]) {
|
||||
let mut out = String::from("{");
|
||||
out.push_str(&format!("\"event\":{}", json_str(event)));
|
||||
if let Some(p) = path {
|
||||
out.push_str(&format!(",\"path\":{}", json_str(&display_path(p))));
|
||||
}
|
||||
if let Some(sp) = span {
|
||||
out.push_str(&format!(",\"span\":{}", json_str(&sp.to_string())));
|
||||
}
|
||||
for (k, v) in fields {
|
||||
out.push_str(&format!(",\"{}\":{}", k, json_str(v)));
|
||||
}
|
||||
out.push('}');
|
||||
eprintln!("{out}");
|
||||
}
|
||||
|
||||
fn json_str(s: &str) -> String {
|
||||
let mut out = String::from("\"");
|
||||
for c in s.chars() {
|
||||
match c {
|
||||
'"' => out.push_str("\\\""),
|
||||
'\\' => out.push_str("\\\\"),
|
||||
'\n' => out.push_str("\\n"),
|
||||
'\r' => out.push_str("\\r"),
|
||||
'\t' => out.push_str("\\t"),
|
||||
c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)),
|
||||
c => out.push(c),
|
||||
}
|
||||
}
|
||||
out.push('"');
|
||||
out
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn json_str_escapes() {
|
||||
assert_eq!(json_str("a\"b"), r#""a\"b""#);
|
||||
}
|
||||
}
|
||||
+60
-8
@@ -8,6 +8,7 @@ use crate::ast::{
|
||||
Arg, BinaryOp, CallExpr, Expr, Function, InterpPart, Literal, Module, Stmt, UnaryOp,
|
||||
};
|
||||
use crate::builtin::{builtin_int, builtin_len, builtin_str};
|
||||
use crate::debug::emit_trace;
|
||||
use crate::diagnostics::{bail_at, Span};
|
||||
use crate::input_feed::InputFeed;
|
||||
use crate::value::Value;
|
||||
@@ -125,7 +126,12 @@ impl Interpreter {
|
||||
args: &[(String, Value)],
|
||||
) -> Result<Value> {
|
||||
if self.trace {
|
||||
eprintln!("enter fun {:?}", fun.name);
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(fun.span),
|
||||
"enter_fn",
|
||||
&[("fn", fun.name.as_str())],
|
||||
);
|
||||
}
|
||||
for (k, v) in args {
|
||||
env.set(k.clone(), v.clone());
|
||||
@@ -144,7 +150,13 @@ impl Interpreter {
|
||||
}
|
||||
}
|
||||
if self.trace {
|
||||
eprintln!("leave fun {:?} => {ret}", fun.name);
|
||||
let display = ret.as_display();
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(fun.span),
|
||||
"leave_fn",
|
||||
&[("fn", fun.name.as_str()), ("value", display.as_str())],
|
||||
);
|
||||
}
|
||||
Ok(ret)
|
||||
}
|
||||
@@ -161,7 +173,17 @@ impl Interpreter {
|
||||
self.current_span = *span;
|
||||
let v = self.eval_expr(module, fun, env, value)?;
|
||||
if self.trace {
|
||||
eprintln!(" assign {name} = {v:?} @{span}");
|
||||
let display = v.as_display();
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(*span),
|
||||
"stmt",
|
||||
&[
|
||||
("kind", "assign"),
|
||||
("name", name.as_str()),
|
||||
("value", display.as_str()),
|
||||
],
|
||||
);
|
||||
}
|
||||
env.set(name.clone(), v);
|
||||
Ok(None)
|
||||
@@ -170,14 +192,25 @@ impl Interpreter {
|
||||
self.current_span = *span;
|
||||
let v = self.eval_expr(module, fun, env, value)?;
|
||||
if self.trace {
|
||||
eprintln!(" return {v:?} @{span}");
|
||||
let display = v.as_display();
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(*span),
|
||||
"stmt",
|
||||
&[("kind", "return"), ("value", display.as_str())],
|
||||
);
|
||||
}
|
||||
Ok(Some(v))
|
||||
}
|
||||
Stmt::Call { call, span } => {
|
||||
self.current_span = *span;
|
||||
if self.trace {
|
||||
eprintln!(" call-stmt {} @{span}", call.callee);
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(*span),
|
||||
"stmt",
|
||||
&[("kind", "call"), ("callee", call.callee.as_str())],
|
||||
);
|
||||
}
|
||||
let _ = self.eval_call(module, fun, env, call)?;
|
||||
Ok(None)
|
||||
@@ -185,7 +218,12 @@ impl Interpreter {
|
||||
Stmt::Branch { arms, span } => {
|
||||
self.current_span = *span;
|
||||
if self.trace {
|
||||
eprintln!(" branch @{span}");
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(*span),
|
||||
"stmt",
|
||||
&[("kind", "branch")],
|
||||
);
|
||||
}
|
||||
for arm in arms {
|
||||
let take = match &arm.condition {
|
||||
@@ -210,7 +248,12 @@ impl Interpreter {
|
||||
} => {
|
||||
self.current_span = *span;
|
||||
if self.trace {
|
||||
eprintln!(" while @{span}");
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(*span),
|
||||
"stmt",
|
||||
&[("kind", "while")],
|
||||
);
|
||||
}
|
||||
let mut guard = 0u32;
|
||||
while self.eval_expr(module, fun, env, condition)?.truthy() {
|
||||
@@ -234,7 +277,16 @@ impl Interpreter {
|
||||
} => {
|
||||
self.current_span = *span;
|
||||
if self.trace {
|
||||
eprintln!(" foreach {item} in {collection} @{span}");
|
||||
emit_trace(
|
||||
self.path.as_deref(),
|
||||
Some(*span),
|
||||
"stmt",
|
||||
&[
|
||||
("kind", "foreach"),
|
||||
("item", item.as_str()),
|
||||
("collection", collection.as_str()),
|
||||
],
|
||||
);
|
||||
}
|
||||
let coll = env.get(collection).cloned().ok_or_else(|| {
|
||||
self.err(format!("undefined collection `{collection}`"))
|
||||
|
||||
+9
-1
@@ -141,8 +141,16 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result<i32> {
|
||||
if opts.dump_bytecode {
|
||||
print!("{}", program.disassemble());
|
||||
}
|
||||
let mut vm = Vm::new(Some(path)).with_stdin(opts.stdin_lines.clone());
|
||||
let mut vm = Vm::new(Some(path))
|
||||
.with_stdin(opts.stdin_lines.clone())
|
||||
.with_trace(opts.trace_eval);
|
||||
if opts.trace_eval {
|
||||
eprintln!("=== marqdo: trace-eval ({path_label}) ===");
|
||||
}
|
||||
vm.run(&program)?;
|
||||
if opts.trace_eval {
|
||||
eprintln!("=== marqdo: end trace-eval ===");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user