From dc5ef63fefb32b1629c55e8ff7b0ba2d6e663920 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Mon, 7 Jun 2021 20:08:38 -0500 Subject: [PATCH] Better error handling in REPL We have much better spanned expression support, so now we get to show it off! --- src/repl.rs | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/src/repl.rs b/src/repl.rs index 44cf33b..e5d4a15 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -15,27 +15,25 @@ pub fn repl() { break; } let mut parser = Parser::new(&line); - let ast = parser.init(); - match ast { - Ok(ast) => { - println!("{:?}", ast); - println!("{:?}", env.eval_stmts(&ast)); - } - Err(e) => { - println!( - "Error `{:?}` occurred at span: {:?} = `{:?}`", - e.kind, - e.span.clone(), - line.slice(e.span.clone()) - ); + let value = parser.init().and_then(|ast| { + println!("{:?}", &ast); + env.eval_stmts(&ast) + }); - println!(" | {}", line); - println!( - " {}{}-- Here", - " ".repeat(e.span.start), - "^".repeat((e.span.end - e.span.start).max(1)) - ); - } + if let Err(e) = value { + println!( + "Error `{:?}` occurred at span: {:?} = `{:?}`", + e.kind, + e.span.clone(), + line.slice(e.span.clone()) + ); + + println!(" | {}", line); + println!( + " {}{}-- Here", + " ".repeat(e.span.start), + "^".repeat((e.span.end - e.span.start).max(1)) + ); } } Err(rustyline::error::ReadlineError::Eof) => {