diff options
author | Griffin Smith <root@gws.fyi> | 2022-09-18T19·59-0400 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-09-18T22·08+0000 |
commit | e720545e5b32683a3cdb135b6004a06304e025aa (patch) | |
tree | 33414930f82266722a53a184604a694448bd6a2b /tvix/eval/src/eval.rs | |
parent | 6f70f325138b48f2c9b03a2103371663cb210d7c (diff) |
refactor(tvix/eval): use Clap for arg+env parsing r/4911
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 <grfn@gws.fyi> Tested-by: BuildkiteCI Reviewed-by: tazjin <tazjin@tvl.su>
Diffstat (limited to 'tvix/eval/src/eval.rs')
-rw-r--r-- | tvix/eval/src/eval.rs | 25 |
1 files changed, 21 insertions, 4 deletions
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<PathBuf>) -> EvalResult<Value> { +/// 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<PathBuf>, options: Options) -> EvalResult<Value> { let mut codemap = codemap::CodeMap::new(); let file = codemap.add_file( location @@ -37,11 +54,11 @@ pub fn interpret(code: &str, location: Option<PathBuf>) -> EvalResult<Value> { .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<PathBuf>) -> EvalResult<Value> { 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) |