crftng-intrprtrs/src/main.rs

52 lines
1.1 KiB
Rust
Raw Normal View History

2022-04-03 21:54:26 +02:00
use crftng_intrprtrs::run;
2022-03-21 15:47:35 +01:00
use std::{fs, io, path};
use tracing::Level;
use tracing_subscriber::{EnvFilter, FmtSubscriber};
fn run_file(file: &path::Path) -> Result<(), io::Error> {
let src = fs::read_to_string(file)?;
println!("{:?}", run(&src).unwrap());
2022-03-21 15:47:35 +01:00
Ok(())
}
fn run_repl() {
2022-03-21 18:42:47 +01:00
let mut line_buf = String::new();
2022-03-21 15:47:35 +01:00
loop {
match io::stdin().read_line(&mut line_buf) {
2022-04-02 21:36:06 +02:00
Ok(n) => {
// Check for EOF
if n == 0 {
break;
}
2022-03-21 15:47:35 +01:00
let line = line_buf.trim();
2022-04-02 21:36:06 +02:00
if !line.is_empty() {
2022-04-03 21:54:26 +02:00
match run(line) {
Ok(v) => println!("{:?}", v),
Err(e) => println!("{}", e),
}
2022-04-02 21:36:06 +02:00
line_buf.clear();
}
2022-03-21 15:47:35 +01:00
}
2022-04-02 21:36:06 +02:00
Err(e) => panic!("{:?}", e),
2022-03-21 15:47:35 +01:00
}
}
}
fn main() {
dotenv::dotenv().ok();
2022-04-02 21:23:47 +02:00
color_eyre::install().expect("Failed to install color-eyre");
2022-03-21 15:47:35 +01:00
let subscriber = FmtSubscriber::builder()
.with_max_level(Level::TRACE)
.with_env_filter(EnvFilter::from_env("LOG"))
.finish();
2022-04-02 21:23:47 +02:00
2022-03-21 15:47:35 +01:00
tracing::subscriber::set_global_default(subscriber).unwrap();
if let Some(file) = std::env::args_os().nth(1) {
run_file(file.as_ref()).unwrap();
} else {
run_repl()
}
}