2020-11-20 09:52:28 +01:00
|
|
|
use rhai::{Dynamic, Engine, EvalAltResult, LexError, ParseError, ParseErrorType, Position, INT};
|
2020-07-09 13:54:28 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2020-07-17 08:50:23 +02:00
|
|
|
engine.consume("while false {}")?;
|
|
|
|
|
2020-07-10 16:01:47 +02:00
|
|
|
// Disable 'while' and make sure it still works with custom syntax
|
|
|
|
engine.disable_symbol("while");
|
2020-07-17 08:50:23 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine.compile("while false {}").expect_err("should error").0,
|
|
|
|
ParseErrorType::Reserved(err) if err == "while"
|
|
|
|
));
|
|
|
|
assert!(matches!(
|
|
|
|
*engine.compile("let while = 0").expect_err("should error").0,
|
|
|
|
ParseErrorType::Reserved(err) if err == "while"
|
|
|
|
));
|
2020-07-10 16:01:47 +02:00
|
|
|
|
2020-08-05 11:02:11 +02:00
|
|
|
engine.register_custom_syntax(
|
|
|
|
&[
|
2021-05-11 04:58:28 +02:00
|
|
|
"exec", "[", "$ident$", "]", "->", "$block$", "while", "$expr$",
|
2020-08-05 11:02:11 +02:00
|
|
|
],
|
2021-05-11 04:58:28 +02:00
|
|
|
true,
|
2020-10-25 14:57:18 +01:00
|
|
|
|context, inputs| {
|
2020-08-05 11:02:11 +02:00
|
|
|
let var_name = inputs[0].get_variable_name().unwrap().to_string();
|
|
|
|
let stmt = inputs.get(1).unwrap();
|
2020-10-11 15:58:11 +02:00
|
|
|
let condition = inputs.get(2).unwrap();
|
2020-07-09 13:54:28 +02:00
|
|
|
|
2021-05-11 04:58:28 +02:00
|
|
|
context.scope_mut().push(var_name.clone(), 0 as INT);
|
2020-07-09 13:54:28 +02:00
|
|
|
|
2020-12-17 03:42:20 +01:00
|
|
|
let mut count: INT = 0;
|
|
|
|
|
2020-08-05 11:02:11 +02:00
|
|
|
loop {
|
2020-10-19 13:11:55 +02:00
|
|
|
context.eval_expression_tree(stmt)?;
|
2020-12-17 03:42:20 +01:00
|
|
|
count += 1;
|
2020-07-09 13:54:28 +02:00
|
|
|
|
2021-05-11 04:58:28 +02:00
|
|
|
context
|
|
|
|
.scope_mut()
|
|
|
|
.push(format!("{}{}", var_name, count), count);
|
|
|
|
|
2020-10-11 15:58:11 +02:00
|
|
|
let stop = !context
|
2020-10-19 13:11:55 +02:00
|
|
|
.eval_expression_tree(condition)?
|
2020-08-05 11:02:11 +02:00
|
|
|
.as_bool()
|
2020-10-11 15:58:11 +02:00
|
|
|
.map_err(|err| {
|
|
|
|
Box::new(EvalAltResult::ErrorMismatchDataType(
|
|
|
|
"bool".to_string(),
|
|
|
|
err.to_string(),
|
|
|
|
condition.position(),
|
|
|
|
))
|
|
|
|
})?;
|
|
|
|
|
|
|
|
if stop {
|
2020-08-05 11:02:11 +02:00
|
|
|
break;
|
2020-07-09 13:54:28 +02:00
|
|
|
}
|
2020-08-05 11:02:11 +02:00
|
|
|
}
|
2020-07-09 13:54:28 +02:00
|
|
|
|
2020-12-17 03:42:20 +01:00
|
|
|
Ok(count.into())
|
2020-08-05 11:02:11 +02:00
|
|
|
},
|
|
|
|
)?;
|
2020-07-09 13:54:28 +02:00
|
|
|
|
2020-12-17 03:42:20 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-12-17 03:42:20 +01:00
|
|
|
let x = 0;
|
2021-05-11 04:58:28 +02:00
|
|
|
let foo = (exec [x] -> { x += 2 } while x < 42) * 10;
|
2020-12-17 03:42:20 +01:00
|
|
|
foo
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
210
|
|
|
|
);
|
2020-11-21 06:05:57 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2020-11-21 06:05:57 +01:00
|
|
|
let x = 0;
|
2021-05-11 04:58:28 +02:00
|
|
|
exec [x] -> { x += 1 } while x < 42;
|
2020-11-21 06:05:57 +01:00
|
|
|
x
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
2020-07-09 13:54:28 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
2021-04-20 06:01:35 +02:00
|
|
|
"
|
2021-05-11 04:58:28 +02:00
|
|
|
exec [x] -> { x += 1 } while x < 42;
|
2020-07-09 13:54:28 +02:00
|
|
|
x
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
42
|
|
|
|
);
|
2021-05-11 04:58:28 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let foo = 123;
|
|
|
|
exec [x] -> { x += 1 } while x < 42;
|
|
|
|
foo + x + x1 + x2 + x3
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
171
|
|
|
|
);
|
2020-07-09 13:54:28 +02:00
|
|
|
|
2020-07-10 16:01:47 +02:00
|
|
|
// The first symbol must be an identifier
|
2020-08-05 11:02:11 +02:00
|
|
|
assert_eq!(
|
|
|
|
*engine
|
2021-05-11 04:58:28 +02:00
|
|
|
.register_custom_syntax(&["!"], false, |_, _| Ok(Dynamic::UNIT))
|
2020-08-05 11:02:11 +02:00
|
|
|
.expect_err("should error")
|
|
|
|
.0,
|
2020-11-02 05:50:27 +01:00
|
|
|
ParseErrorType::BadInput(LexError::ImproperSymbol(
|
2020-11-21 08:15:14 +01:00
|
|
|
"!".to_string(),
|
2020-10-25 14:57:18 +01:00
|
|
|
"Improper symbol for custom syntax at position #1: '!'".to_string()
|
2020-11-02 05:50:27 +01:00
|
|
|
))
|
2020-10-25 14:57:18 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_syntax_raw() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
|
|
|
engine.register_custom_syntax_raw(
|
|
|
|
"hello",
|
2020-12-15 12:23:30 +01:00
|
|
|
|stream, _| match stream.len() {
|
2020-10-25 14:57:18 +01:00
|
|
|
0 => unreachable!(),
|
2020-12-13 07:31:24 +01:00
|
|
|
1 => Ok(Some("$ident$".into())),
|
2020-10-25 14:57:18 +01:00
|
|
|
2 => match stream[1].as_str() {
|
|
|
|
"world" | "kitty" => Ok(None),
|
|
|
|
s => Err(ParseError(
|
2020-11-02 05:50:27 +01:00
|
|
|
Box::new(ParseErrorType::BadInput(LexError::ImproperSymbol(
|
|
|
|
s.to_string(),
|
2021-05-15 05:41:42 +02:00
|
|
|
Default::default(),
|
2020-11-02 05:50:27 +01:00
|
|
|
))),
|
2020-11-20 09:52:28 +01:00
|
|
|
Position::NONE,
|
2020-10-25 14:57:18 +01:00
|
|
|
)),
|
|
|
|
},
|
|
|
|
_ => unreachable!(),
|
|
|
|
},
|
2021-05-11 04:58:28 +02:00
|
|
|
true,
|
2020-11-21 06:05:57 +01:00
|
|
|
|context, inputs| {
|
2020-12-14 16:05:13 +01:00
|
|
|
context.scope_mut().push("foo", 999 as INT);
|
2020-11-21 06:05:57 +01:00
|
|
|
|
2020-10-25 14:57:18 +01:00
|
|
|
Ok(match inputs[0].get_variable_name().unwrap() {
|
|
|
|
"world" => 123 as INT,
|
|
|
|
"kitty" => 42 as INT,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>("hello world")?, 123);
|
|
|
|
assert_eq!(engine.eval::<INT>("hello kitty")?, 42);
|
2020-11-21 06:05:57 +01:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>("let foo = 0; (hello kitty) + foo")?,
|
|
|
|
1041
|
|
|
|
);
|
|
|
|
assert_eq!(engine.eval::<INT>("(hello kitty) + foo")?, 1041);
|
2020-10-25 14:57:18 +01:00
|
|
|
assert_eq!(
|
|
|
|
*engine.compile("hello hey").expect_err("should error").0,
|
2020-11-21 08:15:14 +01:00
|
|
|
ParseErrorType::BadInput(LexError::ImproperSymbol("hey".to_string(), "".to_string()))
|
2020-08-05 11:02:11 +02:00
|
|
|
);
|
2020-07-10 16:01:47 +02:00
|
|
|
|
2020-07-09 13:54:28 +02:00
|
|
|
Ok(())
|
|
|
|
}
|