Handle break and return better.

This commit is contained in:
Stephen Chung
2020-03-17 17:33:37 +08:00
parent 706e0a0c4c
commit 6ca39a019b
9 changed files with 244 additions and 142 deletions

View File

@@ -36,10 +36,18 @@ fn print_error(input: &str, err: EvalAltResult) {
);
println!("{}", lines[p.line().unwrap() - 1]);
let err_text = match err {
EvalAltResult::ErrorRuntime(err, _) if !err.is_empty() => {
format!("Runtime error: {}", err)
}
_ => err.to_string(),
};
println!(
"{}^ {}",
padding(" ", p.position().unwrap() - 1),
err.to_string().replace(&pos_text, "")
err_text.replace(&pos_text, "")
);
}
}
@@ -86,7 +94,12 @@ fn main() {
.map_err(EvalAltResult::ErrorParsing)
.and_then(|r| {
ast = Some(r);
engine.consume_ast_with_scope(&mut scope, true, ast.as_ref().unwrap())
engine
.consume_ast_with_scope(&mut scope, true, ast.as_ref().unwrap())
.or_else(|err| match err {
EvalAltResult::Return(_, _) => Ok(()),
err => Err(err),
})
})
{
println!("");

View File

@@ -31,7 +31,13 @@ fn eprint_error(input: &str, err: EvalAltResult) {
// EOF
let line = lines.len() - 1;
let pos = lines[line - 1].len();
eprint_line(&lines, line, pos, &err.to_string());
let err_text = match err {
EvalAltResult::ErrorRuntime(err, _) if !err.is_empty() => {
format!("Runtime error: {}", err)
}
_ => err.to_string(),
};
eprint_line(&lines, line, pos, &err_text);
}
p if p.is_none() => {
// No position