Merge branch 'v1.3-fixes'

This commit is contained in:
Stephen Chung
2021-12-16 16:10:39 +08:00
7 changed files with 97 additions and 44 deletions

View File

@@ -239,3 +239,37 @@ fn test_custom_syntax_raw() -> Result<(), Box<EvalAltResult>> {
Ok(())
}
#[test]
fn test_custom_syntax_raw2() -> Result<(), Box<EvalAltResult>> {
let mut engine = Engine::new();
engine
.register_custom_operator("#", 255)?
.register_custom_syntax_raw(
"#",
|symbols, lookahead| match symbols.len() {
1 if lookahead == "-" => Ok(Some("$symbol$".into())),
1 => Ok(Some("$int$".into())),
2 if symbols[1] == "-" => Ok(Some("$int$".into())),
2 => Ok(None),
3 => Ok(None),
_ => unreachable!(),
},
false,
move |_, inputs| {
let id = if inputs.len() == 2 {
-inputs[1].get_literal_value::<INT>().unwrap()
} else {
inputs[0].get_literal_value::<INT>().unwrap()
};
Ok(id.into())
},
);
assert_eq!(engine.eval::<INT>("let x = 41; x + #1")?, 42);
assert_eq!(engine.eval::<INT>("#42/2")?, 21);
assert_eq!(engine.eval::<INT>("#-1")?, -1);
Ok(())
}

View File

@@ -12,6 +12,7 @@ use rhai::Array;
use rhai::Map;
#[cfg(not(feature = "no_float"))]
use rhai::FLOAT;
#[cfg(not(feature = "no_float"))]
#[cfg(feature = "decimal")]
use rust_decimal::Decimal;