chore(tvix/eval): bootstrap some evaluator boilerplate

Change-Id: I7770a20948d18a8506c2418dea21202aa21a6ddc
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6064
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
This commit is contained in:
Vincent Ambo 2022-08-04 16:43:51 +03:00 committed by tazjin
parent a9b2157fba
commit 8921688334
3 changed files with 64 additions and 1 deletions

12
tvix/eval/src/errors.rs Normal file
View file

@ -0,0 +1,12 @@
use std::fmt::Display;
#[derive(Debug)]
pub struct Error {}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "error")
}
}
pub type EvalResult<T> = Result<T, Error>;

5
tvix/eval/src/eval.rs Normal file
View file

@ -0,0 +1,5 @@
use crate::errors::{Error, EvalResult};
pub fn interpret(code: String) -> EvalResult<()> {
Ok(())
}

View file

@ -1,3 +1,49 @@
use std::{
env, fs,
io::{self, Write},
mem, process,
};
mod errors;
mod eval;
fn main() {
println!("Hello, world!");
let mut args = env::args();
if args.len() > 2 {
println!("Usage: tvix-eval [script]");
process::exit(1);
}
if let Some(file) = args.nth(1) {
run_file(&file);
} else {
run_prompt();
}
}
fn run_file(file: &str) {
let contents = fs::read_to_string(file).expect("failed to read the input file");
run(contents);
}
fn run_prompt() {
let mut line = String::new();
loop {
print!("> ");
io::stdout().flush().unwrap();
io::stdin()
.read_line(&mut line)
.expect("failed to read user input");
run(mem::take(&mut line));
line.clear();
}
}
fn run(code: String) {
match eval::interpret(code) {
Ok(result) => println!("=> {:?}", result),
Err(err) => eprintln!("{}", err),
}
}