feat: Add tracing support to AST tool

Introduce a `--trace` flag to the AST tool that enables the
TracingObserver.
This allows users to see the detailed execution steps of their scripts.
Also, refactor the main and AST tool to better handle tracing and
performance
statistics.
This commit is contained in:
Michael Schimmel
2026-02-21 16:02:44 +01:00
parent d645f36700
commit ef67a3d60d
2 changed files with 61 additions and 27 deletions
+38 -1
View File
@@ -26,11 +26,15 @@ struct Cli {
/// Dump the compiled AST
#[arg(short, long)]
dump: bool,
/// Run with TracingObserver enabled
#[arg(short, long)]
trace: bool,
}
fn main() {
let cli = Cli::parse();
let env = Environment::new();
let mut env = Environment::new();
if cli.bench || cli.update_bench {
if cfg!(debug_assertions) {
@@ -54,6 +58,8 @@ fn main() {
Ok(dump) => println!("{}", dump),
Err(e) => eprintln!("Error dumping AST: {}", e),
}
} else if cli.trace {
execute_trace(&mut env, &script_str);
} else {
execute(&env, &script_str);
}
@@ -65,6 +71,8 @@ fn main() {
Ok(dump) => println!("{}", dump),
Err(e) => eprintln!("Error dumping AST: {}", e),
}
} else if cli.trace {
execute_trace(&mut env, &content);
} else {
execute(&env, &content);
}
@@ -88,3 +96,32 @@ fn execute(env: &Environment, source: &str) {
}
}
}
fn execute_trace(env: &mut Environment, source: &str) {
match env.compile(source) {
Ok(compiled) => {
let linked = env.link(compiled);
let mut vm = myc::ast::vm::VM::new(env.global_values.clone());
let mut observer = myc::ast::vm::TracingObserver::new();
match vm.run_with_observer(&mut observer, &linked) {
Ok(result) => {
for line in observer.logs {
println!("{}", line);
}
println!("Result: {}", result);
}
Err(e) => {
for line in observer.logs {
println!("{}", line);
}
eprintln!("Runtime Error: {}", e);
std::process::exit(1);
}
}
}
Err(e) => {
eprintln!("Compilation Error: {}", e);
std::process::exit(1);
}
}
}