From 098137082e08cc5978f61780fd3252dfde2d2a78 Mon Sep 17 00:00:00 2001 From: Alex Bethel Date: Wed, 16 Jun 2021 10:25:55 -0500 Subject: [PATCH] Better error messages, remove debug AST printing --- src/main.rs | 42 +++++++++++++++++++++--------------------- src/repl.rs | 2 +- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/main.rs b/src/main.rs index 847fa2ef..eb1e2c57 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,8 @@ mod parser; mod repl; mod variables; +use std::process::exit; + use clap::{App, Arg}; use interpret::ExecEnv; use logos::Source; @@ -34,32 +36,30 @@ fn main() { match matches.value_of("file") { Some(file_path) => { // Read file - let source = std::fs::read_to_string(file_path).unwrap(); - - // Parse - let mut parser = Parser::new(&source); - let ast = parser.init(); - match ast { - Ok(ast) => { - println!("{:#?}", ast); - let mut env = ExecEnv::new(); - println!("{:?}", env.eval_stmts(&ast)); - } + let source = match std::fs::read_to_string(file_path) { + Ok(s) => s, Err(e) => { - println!( - "Error `{:?}` occurred at span: {:?} = `{:?}`", - e.kind, - e.span.clone(), - source.slice(e.span) - ); + println!("Failed to read file \"{}\": {}", file_path, e); + exit(1) } + }; + + // Parse & evaluate + let mut parser = Parser::new(&source); + if let Err(e) = parser + .init() + .and_then(|ast| ExecEnv::new().eval_stmts(&ast)) + { + println!( + "Error `{:?}` occurred at span: {:?} = `{:?}`", + e.kind, + e.span.clone(), + source.slice(e.span) + ); } } None => { - println!( - "Hi [AbleScript {}] - AST Printer & Interpreter", - env!("CARGO_PKG_VERSION") - ); + println!("Hi [AbleScript {}]", env!("CARGO_PKG_VERSION")); repl::repl(); } } diff --git a/src/repl.rs b/src/repl.rs index e5d4a151..55395c6d 100644 --- a/src/repl.rs +++ b/src/repl.rs @@ -16,7 +16,7 @@ pub fn repl() { } let mut parser = Parser::new(&line); let value = parser.init().and_then(|ast| { - println!("{:?}", &ast); + // println!("{:?}", &ast); env.eval_stmts(&ast) });