|
@@ -1,31 +1,14 @@
|
|
|
-use matzo::grammar::StmtsParser;
|
|
|
use matzo::interp::State;
|
|
|
-use matzo::lexer::tokens;
|
|
|
|
|
|
fn matzo_version() -> String {
|
|
|
format!("matzo (git {})", env!("VERGEN_GIT_SHA"))
|
|
|
}
|
|
|
|
|
|
-fn run(src: &str) {
|
|
|
- let lexed = tokens(src);
|
|
|
- let stmts = StmtsParser::new().parse(lexed).unwrap();
|
|
|
- let mut state = State::new();
|
|
|
- for stmt in stmts {
|
|
|
- if let Err(err) = state.execute(&stmt) {
|
|
|
- eprintln!("error: {}", err);
|
|
|
- }
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
fn run_repl() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
let mut rl = rustyline::Editor::<matzo::repl::Repl>::new();
|
|
|
let state = std::rc::Rc::new(std::cell::RefCell::new(State::new()));
|
|
|
rl.set_helper(Some(matzo::repl::Repl::new(state.clone())));
|
|
|
- let parser = StmtsParser::new();
|
|
|
- println!(
|
|
|
- "{}",
|
|
|
- ansi_term::Colour::Blue.bold().paint(matzo_version()),
|
|
|
- );
|
|
|
+ println!("{}", ansi_term::Colour::Blue.bold().paint(matzo_version()),);
|
|
|
println!("{}", ansi_term::Colour::Blue.paint("(work-in-progress)"),);
|
|
|
|
|
|
loop {
|
|
@@ -35,35 +18,13 @@ fn run_repl() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
| Err(rustyline::error::ReadlineError::Interrupted) => return Ok(()),
|
|
|
err => err?,
|
|
|
};
|
|
|
- let lexed = tokens(&line);
|
|
|
|
|
|
- let stmts = match parser.parse(lexed) {
|
|
|
- Ok(stmts) => stmts,
|
|
|
- Err(err) => {
|
|
|
- // for the REPL specifically, let's try adding a
|
|
|
- // `puts` to see if that works
|
|
|
- let added_puts = format!("puts {}", line);
|
|
|
- let lexed = tokens(&added_puts);
|
|
|
- match parser.parse(lexed) {
|
|
|
- Ok(stmts) => stmts,
|
|
|
- Err(_) => {
|
|
|
- // that didn't fix it, so report the
|
|
|
- // _original_ parse error, not the new one
|
|
|
- eprintln!("{}", ansi_term::Colour::Red.paint(format!("{:?}", err)),);
|
|
|
- continue;
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- };
|
|
|
-
|
|
|
- for stmt in stmts {
|
|
|
- if let Err(err) = state.borrow_mut().execute(&stmt) {
|
|
|
- eprintln!(
|
|
|
- "{} {}",
|
|
|
- ansi_term::Colour::Red.bold().paint("error:"),
|
|
|
- ansi_term::Colour::Red.paint(format!("{}", err)),
|
|
|
- );
|
|
|
- }
|
|
|
+ if let Err(err) = state.borrow_mut().run_repl(&line) {
|
|
|
+ eprintln!(
|
|
|
+ "{} {}",
|
|
|
+ ansi_term::Colour::Red.bold().paint("error:"),
|
|
|
+ ansi_term::Colour::Red.paint(format!("{}", err)),
|
|
|
+ );
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -77,6 +38,12 @@ fn main() {
|
|
|
|
|
|
for arg in args {
|
|
|
let buf = std::fs::read_to_string(arg).unwrap();
|
|
|
- run(&buf);
|
|
|
+ if let Err(err) = State::new().run(&buf) {
|
|
|
+ eprintln!(
|
|
|
+ "{} {}",
|
|
|
+ ansi_term::Colour::Red.bold().paint("error:"),
|
|
|
+ ansi_term::Colour::Red.paint(format!("{}", err)),
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
}
|