about summary refs log tree commit diff
path: root/tvix/eval/src
diff options
context:
space:
mode:
authorVincent Ambo <mail@tazj.in>2022-08-04T13·43+0300
committertazjin <tazjin@tvl.su>2022-08-12T12·39+0000
commit892168833423a084a06f2be535080e61aa8ca134 (patch)
treefbb590f2ed16a93d7afe35e2e0598fb919507369 /tvix/eval/src
parenta9b2157fba18e9672e08c90d870cd1a5dfd8d95d (diff)
chore(tvix/eval): bootstrap some evaluator boilerplate r/4401
Change-Id: I7770a20948d18a8506c2418dea21202aa21a6ddc
Reviewed-on: https://cl.tvl.fyi/c/depot/+/6064
Tested-by: BuildkiteCI
Reviewed-by: grfn <grfn@gws.fyi>
Diffstat (limited to 'tvix/eval/src')
-rw-r--r--tvix/eval/src/errors.rs12
-rw-r--r--tvix/eval/src/eval.rs5
-rw-r--r--tvix/eval/src/main.rs48
3 files changed, 64 insertions, 1 deletions
diff --git a/tvix/eval/src/errors.rs b/tvix/eval/src/errors.rs
new file mode 100644
index 000000000000..14f6819862b1
--- /dev/null
+++ b/tvix/eval/src/errors.rs
@@ -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>;
diff --git a/tvix/eval/src/eval.rs b/tvix/eval/src/eval.rs
new file mode 100644
index 000000000000..89a375b69a32
--- /dev/null
+++ b/tvix/eval/src/eval.rs
@@ -0,0 +1,5 @@
+use crate::errors::{Error, EvalResult};
+
+pub fn interpret(code: String) -> EvalResult<()> {
+    Ok(())
+}
diff --git a/tvix/eval/src/main.rs b/tvix/eval/src/main.rs
index e7a11a969c03..8192965a8b61 100644
--- a/tvix/eval/src/main.rs
+++ b/tvix/eval/src/main.rs
@@ -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),
+    }
 }