2022-07-05 16:59:03 +02:00
|
|
|
#![cfg(not(feature = "no_custom_syntax"))]
|
|
|
|
|
2021-07-10 09:50:31 +02:00
|
|
|
use rhai::{
|
2021-08-13 13:23:20 +02:00
|
|
|
Dynamic, Engine, EvalAltResult, ImmutableString, LexError, ParseErrorType, Position, Scope, INT,
|
2021-07-10 09:50:31 +02:00
|
|
|
};
|
2020-07-09 13:54:28 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_syntax() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2021-08-06 08:46:27 +02:00
|
|
|
engine.run("while false {}")?;
|
2020-07-17 08:50:23 +02:00
|
|
|
|
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!(
|
2022-07-29 03:42:30 +02:00
|
|
|
engine.compile("while false {}").expect_err("should error").err_type(),
|
2020-07-17 08:50:23 +02:00
|
|
|
ParseErrorType::Reserved(err) if err == "while"
|
|
|
|
));
|
|
|
|
assert!(matches!(
|
2022-07-29 03:42:30 +02:00
|
|
|
engine.compile("let while = 0").expect_err("should error").err_type(),
|
2020-07-17 08:50:23 +02:00
|
|
|
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-07-10 09:50:31 +02:00
|
|
|
"exec", "[", "$ident$", "$symbol$", "$int$", "]", "->", "$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| {
|
2021-12-16 11:01:49 +01:00
|
|
|
let var_name = inputs[0].get_string_value().unwrap();
|
2021-07-10 09:50:31 +02:00
|
|
|
let op = inputs[1].get_literal_value::<ImmutableString>().unwrap();
|
|
|
|
let max = inputs[2].get_literal_value::<INT>().unwrap();
|
|
|
|
let stmt = &inputs[3];
|
|
|
|
let condition = &inputs[4];
|
2020-07-09 13:54:28 +02:00
|
|
|
|
2021-12-16 11:01:49 +01:00
|
|
|
context.scope_mut().push(var_name.to_string(), 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 {
|
2021-07-10 09:50:31 +02:00
|
|
|
let done = match op.as_str() {
|
|
|
|
"<" => count >= max,
|
|
|
|
"<=" => count > max,
|
|
|
|
">" => count <= max,
|
|
|
|
">=" => count < max,
|
|
|
|
"==" => count != max,
|
|
|
|
"!=" => count == max,
|
2022-08-11 13:01:23 +02:00
|
|
|
_ => return Err(format!("Unsupported operator: {op}").into()),
|
2021-07-10 09:50:31 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if done {
|
2021-06-10 04:16:39 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2022-07-06 06:56:15 +02:00
|
|
|
// Do not rewind if the variable is upper-case
|
|
|
|
if var_name.to_uppercase() == var_name {
|
|
|
|
context.eval_expression_tree_raw(stmt, false)?;
|
|
|
|
} else {
|
|
|
|
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()
|
2022-08-11 13:01:23 +02:00
|
|
|
.push(format!("{var_name}{count}"), count);
|
2021-05-11 04:58:28 +02:00
|
|
|
|
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
|
|
|
|
2021-07-10 09:50:31 +02:00
|
|
|
assert!(matches!(
|
|
|
|
*engine
|
2021-08-06 08:46:27 +02:00
|
|
|
.run("let foo = (exec [x<<15] -> { x += 2 } while x < 42) * 10;")
|
2021-07-10 09:50:31 +02:00
|
|
|
.expect_err("should error"),
|
2022-02-08 02:02:15 +01:00
|
|
|
EvalAltResult::ErrorRuntime(..)
|
2021-07-10 09:50:31 +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-07-10 09:50:31 +02:00
|
|
|
let foo = (exec [x<15] -> { x += 2 } while x < 42) * 10;
|
2020-12-17 03:42:20 +01:00
|
|
|
foo
|
|
|
|
"
|
|
|
|
)?,
|
2021-06-10 04:16:39 +02:00
|
|
|
150
|
2020-12-17 03:42:20 +01:00
|
|
|
);
|
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-07-10 09:50:31 +02:00
|
|
|
exec [x<100] -> { 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-07-10 09:50:31 +02:00
|
|
|
exec [x<100] -> { 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;
|
2021-07-10 09:50:31 +02:00
|
|
|
exec [x<15] -> { x += 1 } while x < 42;
|
2021-05-11 04:58:28 +02:00
|
|
|
foo + x + x1 + x2 + x3
|
|
|
|
"
|
|
|
|
)?,
|
2021-06-10 04:16:39 +02:00
|
|
|
144
|
2021-05-11 04:58:28 +02:00
|
|
|
);
|
2022-07-06 06:56:15 +02:00
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let foo = 123;
|
|
|
|
exec [x<15] -> { let foo = x; x += 1; } while x < 42;
|
|
|
|
foo
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
123
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval::<INT>(
|
|
|
|
"
|
|
|
|
let foo = 123;
|
|
|
|
exec [ABC<15] -> { let foo = ABC; ABC += 1; } while ABC < 42;
|
|
|
|
foo
|
|
|
|
"
|
|
|
|
)?,
|
|
|
|
14
|
|
|
|
);
|
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")
|
2022-07-29 03:42:30 +02:00
|
|
|
.err_type(),
|
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
|
|
|
);
|
|
|
|
|
2021-08-02 04:24:03 +02:00
|
|
|
// Check self-termination
|
|
|
|
engine
|
|
|
|
.register_custom_syntax(&["test1", "$block$"], true, |_, _| Ok(Dynamic::UNIT))?
|
|
|
|
.register_custom_syntax(&["test2", "}"], true, |_, _| Ok(Dynamic::UNIT))?
|
|
|
|
.register_custom_syntax(&["test3", ";"], true, |_, _| Ok(Dynamic::UNIT))?;
|
|
|
|
|
|
|
|
assert_eq!(engine.eval::<INT>("test1 { x = y + z; } 42")?, 42);
|
|
|
|
assert_eq!(engine.eval::<INT>("test2 } 42")?, 42);
|
|
|
|
assert_eq!(engine.eval::<INT>("test3; 42")?, 42);
|
|
|
|
|
2021-08-13 13:23:20 +02:00
|
|
|
// Register the custom syntax: var x = ???
|
|
|
|
engine.register_custom_syntax(
|
|
|
|
&["var", "$ident$", "=", "$expr$"],
|
|
|
|
true,
|
|
|
|
|context, inputs| {
|
2021-12-16 11:01:49 +01:00
|
|
|
let var_name = inputs[0].get_string_value().unwrap();
|
2021-08-13 13:23:20 +02:00
|
|
|
let expr = &inputs[1];
|
|
|
|
|
|
|
|
// Evaluate the expression
|
|
|
|
let value = context.eval_expression_tree(expr)?;
|
|
|
|
|
2021-12-16 11:01:49 +01:00
|
|
|
if !context.scope().is_constant(var_name).unwrap_or(false) {
|
|
|
|
context.scope_mut().set_value(var_name.to_string(), value);
|
2021-08-13 16:47:03 +02:00
|
|
|
Ok(Dynamic::UNIT)
|
|
|
|
} else {
|
2022-08-11 13:01:23 +02:00
|
|
|
Err(format!("variable {var_name} is constant").into())
|
2021-08-13 16:47:03 +02:00
|
|
|
}
|
2021-08-13 13:23:20 +02:00
|
|
|
},
|
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut scope = Scope::new();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_with_scope::<INT>(&mut scope, "var foo = 42; foo")?,
|
|
|
|
42
|
|
|
|
);
|
|
|
|
assert_eq!(scope.get_value::<INT>("foo"), Some(42));
|
|
|
|
assert_eq!(scope.len(), 1);
|
|
|
|
assert_eq!(
|
|
|
|
engine.eval_with_scope::<INT>(&mut scope, "var foo = 123; foo")?,
|
|
|
|
123
|
|
|
|
);
|
|
|
|
assert_eq!(scope.get_value::<INT>("foo"), Some(123));
|
|
|
|
assert_eq!(scope.len(), 1);
|
|
|
|
|
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() {
|
2021-10-25 16:41:42 +02:00
|
|
|
"world" => Ok(Some("$$hello".into())),
|
|
|
|
"kitty" => Ok(None),
|
2021-11-07 11:12:37 +01:00
|
|
|
s => Err(LexError::ImproperSymbol(s.to_string(), String::new())
|
2021-06-29 15:58:05 +02:00
|
|
|
.into_err(Position::NONE)
|
|
|
|
.into()),
|
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
|
|
|
|
2021-12-16 11:01:49 +01:00
|
|
|
Ok(match inputs[0].get_string_value().unwrap() {
|
2021-10-25 16:41:42 +02:00
|
|
|
"world"
|
|
|
|
if inputs
|
|
|
|
.last()
|
|
|
|
.unwrap()
|
|
|
|
.get_literal_value::<ImmutableString>()
|
|
|
|
.map_or(false, |s| s == "$$hello") =>
|
|
|
|
{
|
|
|
|
0 as INT
|
|
|
|
}
|
2020-10-25 14:57:18 +01:00
|
|
|
"world" => 123 as INT,
|
2021-10-25 16:41:42 +02:00
|
|
|
"kitty" if inputs.len() > 1 => 999 as INT,
|
2020-10-25 14:57:18 +01:00
|
|
|
"kitty" => 42 as INT,
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
.into())
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2021-10-25 16:41:42 +02:00
|
|
|
assert_eq!(engine.eval::<INT>("hello world")?, 0);
|
2020-10-25 14:57:18 +01:00
|
|
|
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!(
|
2022-07-29 03:42:30 +02:00
|
|
|
*engine
|
|
|
|
.compile("hello hey")
|
|
|
|
.expect_err("should error")
|
|
|
|
.err_type(),
|
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(())
|
|
|
|
}
|
2021-12-16 09:06:44 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_custom_syntax_raw2() -> Result<(), Box<EvalAltResult>> {
|
|
|
|
let mut engine = Engine::new();
|
|
|
|
|
2021-12-16 11:01:49 +01:00
|
|
|
engine.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())
|
|
|
|
},
|
|
|
|
);
|
2021-12-16 09:06:44 +01:00
|
|
|
|
2021-12-16 11:01:49 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("#-1")?, -1);
|
2021-12-16 09:06:44 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("let x = 41; x + #1")?, 42);
|
2021-12-17 09:32:34 +01:00
|
|
|
#[cfg(not(feature = "no_object"))]
|
2021-12-16 15:40:10 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("#-42.abs()")?, 42);
|
2021-12-16 09:06:44 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("#42/2")?, 21);
|
2021-12-16 11:01:49 +01:00
|
|
|
assert_eq!(engine.eval::<INT>("sign(#1)")?, 1);
|
2021-12-16 09:06:44 +01:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|