chore(tvix/cli): re-add observer flags
Users can again pass flags for dumping the AST, bytecode, and runtime trace. With this commit the CLI is at feature-parity with what we had before, but entirely through the new API. Change-Id: I30fe26f243224b25d1e4f828fec607325ef88306 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7550 Tested-by: BuildkiteCI Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
parent
60d9fba56d
commit
f467df06dc
1 changed files with 34 additions and 2 deletions
|
@ -2,7 +2,8 @@ use std::{fs, path::PathBuf};
|
|||
|
||||
use clap::Parser;
|
||||
use rustyline::{error::ReadlineError, Editor};
|
||||
use tvix_eval::Value; //{Error, EvalWarning, Evaluation, Value};
|
||||
use tvix_eval::observer::{DisassemblingObserver, TracingObserver};
|
||||
use tvix_eval::Value;
|
||||
|
||||
#[derive(Parser)]
|
||||
struct Args {
|
||||
|
@ -12,6 +13,18 @@ struct Args {
|
|||
#[clap(long, short = 'E')]
|
||||
expr: Option<String>,
|
||||
|
||||
/// Dump the raw AST to stdout before interpreting
|
||||
#[clap(long, env = "TVIX_DISPLAY_AST")]
|
||||
display_ast: bool,
|
||||
|
||||
/// Dump the bytecode to stdout before evaluating
|
||||
#[clap(long, env = "TVIX_DUMP_BYTECODE")]
|
||||
dump_bytecode: bool,
|
||||
|
||||
/// Trace the runtime of the VM
|
||||
#[clap(long, env = "TVIX_TRACE_RUNTIME")]
|
||||
trace_runtime: bool,
|
||||
|
||||
/// A colon-separated list of directories to use to resolve `<...>`-style paths
|
||||
#[clap(long, short = 'I', env = "NIX_PATH")]
|
||||
nix_search_path: Option<String>,
|
||||
|
@ -29,7 +42,26 @@ fn interpret(code: &str, path: Option<PathBuf>, args: &Args) -> bool {
|
|||
eval.nix_path = args.nix_search_path.clone();
|
||||
|
||||
let source_map = eval.source_map();
|
||||
let result = eval.evaluate();
|
||||
let result = {
|
||||
let mut compiler_observer =
|
||||
DisassemblingObserver::new(source_map.clone(), std::io::stderr());
|
||||
if args.dump_bytecode {
|
||||
eval.compiler_observer = Some(&mut compiler_observer);
|
||||
}
|
||||
|
||||
let mut runtime_observer = TracingObserver::new(std::io::stderr());
|
||||
if args.trace_runtime {
|
||||
eval.runtime_observer = Some(&mut runtime_observer);
|
||||
}
|
||||
|
||||
eval.evaluate()
|
||||
};
|
||||
|
||||
if args.display_ast {
|
||||
if let Some(ref expr) = result.expr {
|
||||
eprintln!("AST: {}", tvix_eval::pretty_print_expr(expr));
|
||||
}
|
||||
}
|
||||
|
||||
for error in &result.errors {
|
||||
error.fancy_format_stderr(&source_map);
|
||||
|
|
Loading…
Reference in a new issue