main.rs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. use matzo::interp::State;
  2. fn matzo_version() -> String {
  3. format!("matzo (git {})", env!("VERGEN_GIT_SHA"))
  4. }
  5. fn run_repl() -> Result<(), Box<dyn std::error::Error>> {
  6. let mut rl = rustyline::Editor::<matzo::repl::Repl>::new();
  7. let state = std::rc::Rc::new(std::cell::RefCell::new(State::new()));
  8. rl.set_helper(Some(matzo::repl::Repl::new(state.clone())));
  9. println!("{}", ansi_term::Colour::Blue.bold().paint(matzo_version()),);
  10. println!("{}", ansi_term::Colour::Blue.paint("(work-in-progress)"),);
  11. loop {
  12. let line = match rl.readline(">>> ") {
  13. Ok(ln) => ln,
  14. Err(rustyline::error::ReadlineError::Eof)
  15. | Err(rustyline::error::ReadlineError::Interrupted) => return Ok(()),
  16. err => err?,
  17. };
  18. if let Err(err) = state.borrow_mut().run_repl(&line) {
  19. eprintln!(
  20. "{} {}",
  21. ansi_term::Colour::Red.bold().paint("error:"),
  22. ansi_term::Colour::Red.paint(format!("{}", err)),
  23. );
  24. }
  25. }
  26. }
  27. fn main() {
  28. let args = std::env::args().skip(1).collect::<Vec<String>>();
  29. if args.is_empty() {
  30. run_repl().unwrap();
  31. return;
  32. }
  33. for arg in args {
  34. let buf = std::fs::read_to_string(arg).unwrap();
  35. if let Err(err) = State::new().run(&buf) {
  36. eprintln!(
  37. "{} {}",
  38. ansi_term::Colour::Red.bold().paint("error:"),
  39. ansi_term::Colour::Red.paint(format!("{}", err)),
  40. );
  41. }
  42. }
  43. }