Set allow_loop_expressions to true by default.

This commit is contained in:
Stephen Chung 2023-02-16 20:35:15 +08:00
parent 513a1ab435
commit 6b301b4e70
4 changed files with 47 additions and 2 deletions

View File

@ -18,12 +18,14 @@ Potentially breaking changes
* The trait method `ModuleResolver::resolve_raw` (which is a low-level API) now takes a `&mut Scope` parameter. This is a breaking change because the signature is modified, but this trait method has a default and is rarely called/implemented in practice. * The trait method `ModuleResolver::resolve_raw` (which is a low-level API) now takes a `&mut Scope` parameter. This is a breaking change because the signature is modified, but this trait method has a default and is rarely called/implemented in practice.
* `Module::eval_ast_as_new_raw` (a low-level API) now takes a `&mut Scope` instead of the `Scope` parameter. This is a breaking change because the `&mut` is now required. * `Module::eval_ast_as_new_raw` (a low-level API) now takes a `&mut Scope` instead of the `Scope` parameter. This is a breaking change because the `&mut` is now required.
* `Engine::allow_loop_expressions` now correctly defaults to `true` (was erroneously `false` by default).
Enhancements Enhancements
------------ ------------
* The functions `min` and `max` are added for numbers. * The functions `min` and `max` are added for numbers.
* Range cases in `switch` statements now also match floating-point and decimal values. In order to support this, however, small numeric ranges cases are no longer unrolled. * Range cases in `switch` statements now also match floating-point and decimal values. In order to support this, however, small numeric ranges cases are no longer unrolled.
* Loading a module via `import` now gives the module access to the current scope, including variables and constants defined inside.
Version 1.12.0 Version 1.12.0

View File

@ -41,6 +41,7 @@ impl LangOptions {
pub fn new() -> Self { pub fn new() -> Self {
Self::IF_EXPR Self::IF_EXPR
| Self::SWITCH_EXPR | Self::SWITCH_EXPR
| Self::LOOP_EXPR
| Self::STMT_EXPR | Self::STMT_EXPR
| Self::LOOPING | Self::LOOPING
| Self::SHADOWING | Self::SHADOWING

View File

@ -1254,7 +1254,9 @@ impl Engine {
}; };
if forbidden { if forbidden {
return Err(PERR::WrongSwitchIntegerCase.into_err(expr.start_position())); return Err(
PERR::WrongSwitchIntegerCase.into_err(expr.start_position())
);
} }
} }

View File

@ -20,7 +20,7 @@ fn test_loop() -> Result<(), Box<EvalAltResult>> {
} }
} }
return x; x
" "
)?, )?,
21 21
@ -53,3 +53,43 @@ fn test_loop() -> Result<(), Box<EvalAltResult>> {
Ok(()) Ok(())
} }
#[test]
fn test_loop_expression() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
assert_eq!(
engine.eval::<INT>(
"
let x = 0;
let value = while x < 10 {
if x % 5 == 0 { break 42; }
x += 1;
};
value
"
)?,
42
);
engine.set_allow_loop_expressions(false);
assert!(engine
.eval::<INT>(
"
let x = 0;
let value = while x < 10 {
if x % 5 == 0 { break 42; }
x += 1;
};
value
"
)
.is_err());
Ok(())
}