about summary refs log tree commit diff
path: root/tvix/cli/src/main.rs
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-12-09T10·49+0300
committertazjin <tazjin@tvl.su>2022-12-21T13·23+0000
commitf467df06dcfa8383c3bd36e7877b8cf900e973c9 (patch)
tree887b4f09f5a119bb0fec01a1b16f3cc3a7cc6a5b /tvix/cli/src/main.rs
parent60d9fba56d12279157e62c6df60b421ec116c8ee (diff)
chore(tvix/cli): re-add observer flags r/5449
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>
Diffstat (limited to '')
-rw-r--r--tvix/cli/src/main.rs36
1 files changed, 34 insertions, 2 deletions
diff --git a/tvix/cli/src/main.rs b/tvix/cli/src/main.rs
index 897c31570a..8374ec42e6 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<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);