Better error messages, remove debug AST printing

This commit is contained in:
Alex Bethel 2021-06-16 10:25:55 -05:00
parent 2e5799d9e6
commit 098137082e
2 changed files with 22 additions and 22 deletions

View file

@ -10,6 +10,8 @@ mod parser;
mod repl; mod repl;
mod variables; mod variables;
use std::process::exit;
use clap::{App, Arg}; use clap::{App, Arg};
use interpret::ExecEnv; use interpret::ExecEnv;
use logos::Source; use logos::Source;
@ -34,18 +36,20 @@ fn main() {
match matches.value_of("file") { match matches.value_of("file") {
Some(file_path) => { Some(file_path) => {
// Read file // Read file
let source = std::fs::read_to_string(file_path).unwrap(); let source = match std::fs::read_to_string(file_path) {
Ok(s) => s,
// 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));
}
Err(e) => { Err(e) => {
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!( println!(
"Error `{:?}` occurred at span: {:?} = `{:?}`", "Error `{:?}` occurred at span: {:?} = `{:?}`",
e.kind, e.kind,
@ -54,12 +58,8 @@ fn main() {
); );
} }
} }
}
None => { None => {
println!( println!("Hi [AbleScript {}]", env!("CARGO_PKG_VERSION"));
"Hi [AbleScript {}] - AST Printer & Interpreter",
env!("CARGO_PKG_VERSION")
);
repl::repl(); repl::repl();
} }
} }

View file

@ -16,7 +16,7 @@ pub fn repl() {
} }
let mut parser = Parser::new(&line); let mut parser = Parser::new(&line);
let value = parser.init().and_then(|ast| { let value = parser.init().and_then(|ast| {
println!("{:?}", &ast); // println!("{:?}", &ast);
env.eval_stmts(&ast) env.eval_stmts(&ast)
}); });