Refactor execute_trace to use run_debug
This commit refactors the `execute_trace` function to utilize the `run_debug` method on the `Environment`. This simplifies the execution flow and leverages existing functionality for tracing and error handling.
This commit is contained in:
+15
-22
@@ -71,7 +71,7 @@ fn main() {
|
||||
Err(e) => eprintln!("Error dumping AST: {}", e),
|
||||
}
|
||||
} else if cli.trace {
|
||||
execute_trace(&mut env, &script_str);
|
||||
execute_trace(&env, &script_str);
|
||||
} else {
|
||||
execute(&env, &script_str);
|
||||
}
|
||||
@@ -84,7 +84,7 @@ fn main() {
|
||||
Err(e) => eprintln!("Error dumping AST: {}", e),
|
||||
}
|
||||
} else if cli.trace {
|
||||
execute_trace(&mut env, &content);
|
||||
execute_trace(&env, &content);
|
||||
} else {
|
||||
execute(&env, &content);
|
||||
}
|
||||
@@ -109,27 +109,20 @@ 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);
|
||||
}
|
||||
fn execute_trace(env: &Environment, source: &str) {
|
||||
match env.run_debug(source) {
|
||||
Ok((Ok(result), logs)) => {
|
||||
for line in logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
println!("Result: {}", result);
|
||||
}
|
||||
Ok((Err(e), logs)) => {
|
||||
for line in logs {
|
||||
println!("{}", line);
|
||||
}
|
||||
eprintln!("Runtime Error: {}", e);
|
||||
std::process::exit(1);
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("Compilation Error: {}", e);
|
||||
|
||||
Reference in New Issue
Block a user