Handle break and return better.

This commit is contained in:
Stephen Chung
2020-03-17 17:33:37 +08:00
parent d2951bfb6b
commit 8efe080412
9 changed files with 242 additions and 142 deletions

View File

@@ -62,8 +62,8 @@ pub enum EvalAltResult {
ErrorArithmetic(String, Position),
/// Run-time error encountered. Wrapped value is the error message.
ErrorRuntime(String, Position),
/// Internal use: Breaking out of loops.
LoopBreak,
/// Breaking out of loops - not an error if within a loop.
ErrorLoopBreak(Position),
/// Not an error: Value returned from a script via the `return` keyword.
/// Wrapped value is the result value.
Return(Dynamic, Position),
@@ -106,7 +106,7 @@ impl EvalAltResult {
Self::ErrorDotExpr(_, _) => "Malformed dot expression",
Self::ErrorArithmetic(_, _) => "Arithmetic error",
Self::ErrorRuntime(_, _) => "Runtime error",
Self::LoopBreak => "[Not Error] Breaks out of loop",
Self::ErrorLoopBreak(_) => "Break statement not inside a loop",
Self::Return(_, _) => "[Not Error] Function returns value",
}
}
@@ -134,7 +134,7 @@ impl fmt::Display for EvalAltResult {
Self::ErrorRuntime(s, pos) => {
write!(f, "{} ({})", if s.is_empty() { desc } else { s }, pos)
}
Self::LoopBreak => write!(f, "{}", desc),
Self::ErrorLoopBreak(pos) => write!(f, "{} ({})", desc, pos),
Self::Return(_, pos) => write!(f, "{} ({})", desc, pos),
#[cfg(not(feature = "no_stdlib"))]
Self::ErrorReadingScriptFile(path, err) => {
@@ -211,7 +211,6 @@ impl EvalAltResult {
match self {
#[cfg(not(feature = "no_stdlib"))]
Self::ErrorReadingScriptFile(_, _) => Position::none(),
Self::LoopBreak => Position::none(),
Self::ErrorParsing(err) => err.position(),
@@ -232,6 +231,7 @@ impl EvalAltResult {
| Self::ErrorDotExpr(_, pos)
| Self::ErrorArithmetic(_, pos)
| Self::ErrorRuntime(_, pos)
| Self::ErrorLoopBreak(pos)
| Self::Return(_, pos) => *pos,
}
}
@@ -240,7 +240,6 @@ impl EvalAltResult {
match self {
#[cfg(not(feature = "no_stdlib"))]
Self::ErrorReadingScriptFile(_, _) => (),
Self::LoopBreak => (),
Self::ErrorParsing(ParseError(_, ref mut pos))
| Self::ErrorFunctionNotFound(_, ref mut pos)
@@ -260,6 +259,7 @@ impl EvalAltResult {
| Self::ErrorDotExpr(_, ref mut pos)
| Self::ErrorArithmetic(_, ref mut pos)
| Self::ErrorRuntime(_, ref mut pos)
| Self::ErrorLoopBreak(ref mut pos)
| Self::Return(_, ref mut pos) => *pos = new_position,
}
}