diff options
author | Vincent Ambo <mail@tazj.in> | 2022-12-08T21·19+0300 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2022-12-21T13·09+0000 |
commit | d9e2bec953880ecb5953b61b36a5beaec0565e22 (patch) | |
tree | 928d61f4bfd8d98dbe194284285c4f75186ee4f1 /tvix/eval/src/main.rs | |
parent | 1138fbcaad3a7d69f707c4f47d84f314bc95d45a (diff) |
refactor(tvix): split binary (REPL etc.) out from evaluator library r/5440
The tvix-eval project is independent from any *uses* of the evaluator, such as the tvix-repl. This functionality has been split out into a separate "tvix-cli" crate. Note that this doesn't have to mean that this CLI crate is the "final" CLI crate for tvix, the point of this is not "getting the CLI structure right" but rather "getting the evaluator structure right". This reshuffling is part of restructuring the way that functionality like store communication is injected into language evaluation. Note that at this commit the new CLI crate is not at feature-parity. Change-Id: Id0af03dc8e07ef09a9f882a89612ad555eca8f93 Reviewed-on: https://cl.tvl.fyi/c/depot/+/7541 Autosubmit: tazjin <tazjin@tvl.su> Reviewed-by: grfn <grfn@gws.fyi> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/eval/src/main.rs')
-rw-r--r-- | tvix/eval/src/main.rs | 106 |
1 files changed, 0 insertions, 106 deletions
diff --git a/tvix/eval/src/main.rs b/tvix/eval/src/main.rs deleted file mode 100644 index bef48d07a0c4..000000000000 --- a/tvix/eval/src/main.rs +++ /dev/null @@ -1,106 +0,0 @@ -use std::{fs, path::PathBuf}; - -use clap::Parser; -use rustyline::{error::ReadlineError, Editor}; -use tvix_eval::Value; - -#[derive(Parser)] -struct Args { - /// Path to a script to evaluate - script: Option<PathBuf>, - - #[clap(long, short = 'E')] - expr: Option<String>, - - #[clap(flatten)] - eval_options: tvix_eval::Options, -} - -fn main() { - let args = Args::parse(); - - if let Some(file) = args.script { - run_file(file, args.eval_options) - } else if let Some(expr) = args.expr { - let raw = args.eval_options.raw; - if let Ok(result) = tvix_eval::interpret(&expr, None, args.eval_options) { - println_result(&result, raw); - } - } else { - run_prompt(args.eval_options) - } -} - -fn run_file(mut path: PathBuf, eval_options: tvix_eval::Options) { - if path.is_dir() { - path.push("default.nix"); - } - let contents = fs::read_to_string(&path).expect("failed to read the input file"); - let raw = eval_options.raw; - match tvix_eval::interpret(&contents, Some(path), eval_options) { - Ok(result) => println_result(&result, raw), - Err(err) => eprintln!("{}", err), - } -} - -fn println_result(result: &Value, raw: bool) { - if raw { - println!("{}", result.to_str().unwrap().as_str()) - } else { - println!("=> {} :: {}", result, result.type_of()) - } -} - -fn state_dir() -> Option<PathBuf> { - let mut path = dirs::data_dir(); - if let Some(p) = path.as_mut() { - p.push("tvix") - } - path -} - -fn run_prompt(eval_options: tvix_eval::Options) { - let mut rl = Editor::<()>::new().expect("should be able to launch rustyline"); - - let history_path = match state_dir() { - // Attempt to set up these paths, but do not hard fail if it - // doesn't work. - Some(mut path) => { - let _ = std::fs::create_dir_all(&path); - path.push("history.txt"); - let _ = rl.load_history(&path); - Some(path) - } - - None => None, - }; - - loop { - let readline = rl.readline("tvix-repl> "); - match readline { - Ok(line) => { - if line.is_empty() { - continue; - } - - rl.add_history_entry(&line); - match tvix_eval::interpret(&line, None, eval_options.clone()) { - Ok(result) => { - println!("=> {} :: {}", result, result.type_of()); - } - Err(_) => { /* interpret takes care of error formatting */ } - } - } - Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => break, - - Err(err) => { - eprintln!("error: {}", err); - break; - } - } - } - - if let Some(path) = history_path { - rl.save_history(&path).unwrap(); - } -} |