From e720545e5b32683a3cdb135b6004a06304e025aa Mon Sep 17 00:00:00 2001 From: Griffin Smith Date: Sun, 18 Sep 2022 15:59:59 -0400 Subject: refactor(tvix/eval): use Clap for arg+env parsing Refactor the environment variable and argument parsing for the tvix repl to use Clap instead of doing things ad-hoc, and thread through options obtained from environment variables via explicit arguments rather than obtaining them from the environment as they're needed. This makes adding more flags more sustainable, and also makes the binary fully self-documenting, including supported env vars, via `--help`. Change-Id: Ib1f6a0cd20056e8c9196760ff755fa5729667760 Reviewed-on: https://cl.tvl.fyi/c/depot/+/6653 Autosubmit: grfn Tested-by: BuildkiteCI Reviewed-by: tazjin --- tvix/eval/src/eval.rs | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'tvix/eval/src/eval.rs') diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs index 6510ef0afb2b..21591802a443 100644 --- a/tvix/eval/src/eval.rs +++ b/tvix/eval/src/eval.rs @@ -7,7 +7,24 @@ use crate::{ value::Value, }; -pub fn interpret(code: &str, location: Option) -> EvalResult { +/// Runtime options for the Tvix interpreter +#[derive(Debug, Clone, Copy, Default)] +#[cfg_attr(feature = "repl", derive(clap::Parser))] +pub struct Options { + /// Dump the raw AST to stdout before interpreting + #[cfg_attr(feature = "repl", clap(long, env = "TVIX_DISPLAY_AST"))] + display_ast: bool, + + /// Dump the bytecode to stdout before evaluating + #[cfg_attr(feature = "repl", clap(long, env = "TVIX_DUMP_BYTECODE"))] + dump_bytecode: bool, + + /// Trace the runtime of the VM + #[cfg_attr(feature = "repl", clap(long, env = "TVIX_TRACE_RUNTIME"))] + trace_runtime: bool, +} + +pub fn interpret(code: &str, location: Option, options: Options) -> EvalResult { let mut codemap = codemap::CodeMap::new(); let file = codemap.add_file( location @@ -37,11 +54,11 @@ pub fn interpret(code: &str, location: Option) -> EvalResult { .expr() .expect("expression should exist if no errors occured"); - if std::env::var("TVIX_DISPLAY_AST").is_ok() { + if options.display_ast { println!("{:?}", root_expr); } - let result = if std::env::var("TVIX_DUMP_BYTECODE").is_ok() { + let result = if options.dump_bytecode { crate::compiler::compile( root_expr, location, @@ -76,7 +93,7 @@ pub fn interpret(code: &str, location: Option) -> EvalResult { return Err(err.clone()); } - if std::env::var("TVIX_TRACE_RUNTIME").is_ok() { + if options.trace_runtime { crate::vm::run_lambda(&mut TracingObserver::new(std::io::stderr()), result.lambda) } else { crate::vm::run_lambda(&mut NoOpObserver::default(), result.lambda) -- cgit 1.4.1