From f467df06dcfa8383c3bd36e7877b8cf900e973c9 Mon Sep 17 00:00:00 2001 From: Vincent Ambo Date: Fri, 9 Dec 2022 13:49:36 +0300 Subject: [PATCH] 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 --- tvix/cli/src/main.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/tvix/cli/src/main.rs b/tvix/cli/src/main.rs index 897c31570..8374ec42e 100644 --- a/tvix/cli/src/main.rs +++ b/tvix/cli/src/main.rs @@ -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, + /// 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, @@ -29,7 +42,26 @@ fn interpret(code: &str, path: Option, 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);