2020-03-08 12:54:02 +01:00
|
|
|
//! Module that defines the `Scope` type representing a function call-stack scope.
|
|
|
|
|
2020-03-14 07:30:44 +01:00
|
|
|
use crate::any::{Any, AnyExt, Dynamic};
|
|
|
|
use crate::parser::{Expr, Position, INT};
|
|
|
|
|
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
use crate::parser::FLOAT;
|
2020-03-11 04:03:18 +01:00
|
|
|
|
2020-03-07 03:15:42 +01:00
|
|
|
use std::borrow::Cow;
|
2020-03-03 08:20:20 +01:00
|
|
|
|
2020-03-14 07:57:59 +01:00
|
|
|
/// Type of a variable in the Scope.
|
2020-03-13 11:12:41 +01:00
|
|
|
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
|
|
|
|
pub enum VariableType {
|
2020-03-14 07:57:59 +01:00
|
|
|
/// Normal variable.
|
2020-03-13 11:12:41 +01:00
|
|
|
Normal,
|
2020-03-14 07:57:59 +01:00
|
|
|
/// Immutable constant value.
|
2020-03-13 11:12:41 +01:00
|
|
|
Constant,
|
|
|
|
}
|
|
|
|
|
2020-03-14 07:57:59 +01:00
|
|
|
/// An entry in the Scope.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub struct ScopeEntry<'a> {
|
2020-03-14 07:57:59 +01:00
|
|
|
/// Name of the variable.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub name: Cow<'a, str>,
|
2020-03-14 07:57:59 +01:00
|
|
|
/// Type of the variable.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub var_type: VariableType,
|
2020-03-14 07:57:59 +01:00
|
|
|
/// Current value of the variable.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub value: Dynamic,
|
2020-03-14 07:57:59 +01:00
|
|
|
/// A constant expression if the initial value matches one of the recognized types.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub expr: Option<Expr>,
|
|
|
|
}
|
|
|
|
|
2020-03-14 07:57:59 +01:00
|
|
|
/// A type containing information about the current scope.
|
|
|
|
/// Useful for keeping state between `Engine` evaluation runs.
|
2020-03-03 08:20:20 +01:00
|
|
|
///
|
2020-03-04 15:00:01 +01:00
|
|
|
/// # Example
|
|
|
|
///
|
2020-03-03 08:20:20 +01:00
|
|
|
/// ```rust
|
2020-03-09 14:09:53 +01:00
|
|
|
/// # fn main() -> Result<(), rhai::EvalAltResult> {
|
2020-03-03 08:20:20 +01:00
|
|
|
/// use rhai::{Engine, Scope};
|
|
|
|
///
|
|
|
|
/// let mut engine = Engine::new();
|
|
|
|
/// let mut my_scope = Scope::new();
|
|
|
|
///
|
2020-03-12 07:54:14 +01:00
|
|
|
/// engine.eval_with_scope::<()>(&mut my_scope, "let x = 5;")?;
|
2020-03-09 14:09:53 +01:00
|
|
|
///
|
2020-03-12 07:54:14 +01:00
|
|
|
/// assert_eq!(engine.eval_with_scope::<i64>(&mut my_scope, "x + 1")?, 6);
|
2020-03-09 14:09:53 +01:00
|
|
|
/// # Ok(())
|
|
|
|
/// # }
|
2020-03-03 08:20:20 +01:00
|
|
|
/// ```
|
|
|
|
///
|
2020-03-04 15:00:01 +01:00
|
|
|
/// When searching for variables, newly-added variables are found before similarly-named but older variables,
|
|
|
|
/// allowing for automatic _shadowing_ of variables.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub struct Scope<'a>(Vec<ScopeEntry<'a>>);
|
2020-03-03 08:20:20 +01:00
|
|
|
|
2020-03-07 03:15:42 +01:00
|
|
|
impl<'a> Scope<'a> {
|
2020-03-03 08:20:20 +01:00
|
|
|
/// Create a new Scope.
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self(Vec::new())
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Empty the Scope.
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.0.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the number of variables inside the Scope.
|
|
|
|
pub fn len(&self) -> usize {
|
|
|
|
self.0.len()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add (push) a new variable to the Scope.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub fn push<K: Into<Cow<'a, str>>, T: Any>(&mut self, name: K, value: T) {
|
|
|
|
let value = value.into_dynamic();
|
|
|
|
|
|
|
|
// Map into constant expressions
|
2020-03-14 13:08:18 +01:00
|
|
|
//let (expr, value) = map_dynamic_to_expr(value);
|
2020-03-14 07:30:44 +01:00
|
|
|
|
|
|
|
self.0.push(ScopeEntry {
|
|
|
|
name: name.into(),
|
|
|
|
var_type: VariableType::Normal,
|
|
|
|
value,
|
2020-03-14 13:08:18 +01:00
|
|
|
expr: None,
|
2020-03-14 07:30:44 +01:00
|
|
|
});
|
2020-03-03 08:20:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-13 11:12:41 +01:00
|
|
|
/// Add (push) a new constant to the Scope.
|
2020-03-14 07:57:59 +01:00
|
|
|
///
|
|
|
|
/// Constants are immutable and cannot be assigned to. Their values never change.
|
|
|
|
/// Constants propagation is a technique used to optimize an AST.
|
|
|
|
/// However, in order to be used for optimization, constants must be in one of the recognized types:
|
|
|
|
/// `INT` (default to `i64`, `i32` if `only_i32`), `f64`, `String`, `char` and `bool`.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub fn push_constant<K: Into<Cow<'a, str>>, T: Any>(&mut self, name: K, value: T) {
|
|
|
|
let value = value.into_dynamic();
|
|
|
|
|
|
|
|
// Map into constant expressions
|
|
|
|
let (expr, value) = map_dynamic_to_expr(value);
|
|
|
|
|
|
|
|
self.0.push(ScopeEntry {
|
|
|
|
name: name.into(),
|
|
|
|
var_type: VariableType::Constant,
|
|
|
|
value,
|
|
|
|
expr,
|
|
|
|
});
|
2020-03-13 11:12:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Add (push) a new variable with a `Dynamic` value to the Scope.
|
|
|
|
pub(crate) fn push_dynamic<K: Into<Cow<'a, str>>>(
|
|
|
|
&mut self,
|
2020-03-14 07:30:44 +01:00
|
|
|
name: K,
|
|
|
|
var_type: VariableType,
|
2020-03-13 11:12:41 +01:00
|
|
|
value: Dynamic,
|
|
|
|
) {
|
2020-03-14 13:08:18 +01:00
|
|
|
//let (expr, value) = map_dynamic_to_expr(value);
|
2020-03-14 07:30:44 +01:00
|
|
|
|
|
|
|
self.0.push(ScopeEntry {
|
|
|
|
name: name.into(),
|
|
|
|
var_type,
|
|
|
|
value,
|
2020-03-14 13:08:18 +01:00
|
|
|
expr: None,
|
2020-03-14 07:30:44 +01:00
|
|
|
});
|
2020-03-03 08:20:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Remove (pop) the last variable from the Scope.
|
2020-03-13 11:12:41 +01:00
|
|
|
pub fn pop(&mut self) -> Option<(String, VariableType, Dynamic)> {
|
2020-03-14 07:30:44 +01:00
|
|
|
self.0.pop().map(
|
|
|
|
|ScopeEntry {
|
|
|
|
name,
|
|
|
|
var_type,
|
|
|
|
value,
|
|
|
|
..
|
|
|
|
}| (name.to_string(), var_type, value),
|
|
|
|
)
|
2020-03-03 08:20:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Truncate (rewind) the Scope to a previous size.
|
|
|
|
pub fn rewind(&mut self, size: usize) {
|
|
|
|
self.0.truncate(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Find a variable in the Scope, starting from the last.
|
2020-03-13 11:12:41 +01:00
|
|
|
pub fn get(&self, key: &str) -> Option<(usize, &str, VariableType, Dynamic)> {
|
2020-03-03 08:20:20 +01:00
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2020-03-04 15:00:01 +01:00
|
|
|
.rev() // Always search a Scope in reverse order
|
2020-03-14 07:30:44 +01:00
|
|
|
.find(|(_, ScopeEntry { name, .. })| name == key)
|
|
|
|
.map(
|
|
|
|
|(
|
|
|
|
i,
|
|
|
|
ScopeEntry {
|
|
|
|
name,
|
|
|
|
var_type,
|
|
|
|
value,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
)| (i, name.as_ref(), *var_type, value.clone()),
|
|
|
|
)
|
2020-03-03 08:20:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the value of a variable in the Scope, starting from the last.
|
|
|
|
pub fn get_value<T: Any + Clone>(&self, key: &str) -> Option<T> {
|
|
|
|
self.0
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2020-03-04 15:00:01 +01:00
|
|
|
.rev() // Always search a Scope in reverse order
|
2020-03-14 07:30:44 +01:00
|
|
|
.find(|(_, ScopeEntry { name, .. })| name == key)
|
|
|
|
.and_then(|(_, ScopeEntry { value, .. })| value.downcast_ref::<T>())
|
|
|
|
.map(T::clone)
|
2020-03-03 08:20:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Get a mutable reference to a variable in the Scope.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub(crate) fn get_mut(&mut self, name: &str, index: usize) -> &mut Dynamic {
|
2020-03-03 08:20:20 +01:00
|
|
|
let entry = self.0.get_mut(index).expect("invalid index in Scope");
|
|
|
|
|
2020-03-13 11:12:41 +01:00
|
|
|
assert_ne!(
|
2020-03-14 07:30:44 +01:00
|
|
|
entry.var_type,
|
2020-03-13 11:12:41 +01:00
|
|
|
VariableType::Constant,
|
|
|
|
"get mut of constant variable"
|
|
|
|
);
|
2020-03-14 07:30:44 +01:00
|
|
|
assert_eq!(entry.name, name, "incorrect key at Scope entry");
|
2020-03-03 08:20:20 +01:00
|
|
|
|
2020-03-14 07:30:44 +01:00
|
|
|
&mut entry.value
|
2020-03-03 08:20:20 +01:00
|
|
|
}
|
|
|
|
|
2020-03-05 18:05:02 +01:00
|
|
|
/// Get a mutable reference to a variable in the Scope and downcast it to a specific type
|
2020-03-10 10:10:33 +01:00
|
|
|
#[cfg(not(feature = "no_index"))]
|
2020-03-14 07:30:44 +01:00
|
|
|
pub(crate) fn get_mut_by_type<T: Any + Clone>(&mut self, name: &str, index: usize) -> &mut T {
|
|
|
|
self.get_mut(name, index)
|
2020-03-05 18:05:02 +01:00
|
|
|
.downcast_mut::<T>()
|
|
|
|
.expect("wrong type cast")
|
|
|
|
}
|
|
|
|
|
2020-03-03 08:20:20 +01:00
|
|
|
/// Get an iterator to variables in the Scope.
|
2020-03-14 07:30:44 +01:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = &ScopeEntry> {
|
|
|
|
self.0.iter().rev() // Always search a Scope in reverse order
|
2020-03-03 08:20:20 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 11:12:41 +01:00
|
|
|
impl<'a, K> std::iter::Extend<(K, VariableType, Dynamic)> for Scope<'a>
|
2020-03-07 03:15:42 +01:00
|
|
|
where
|
|
|
|
K: Into<Cow<'a, str>>,
|
|
|
|
{
|
2020-03-13 11:12:41 +01:00
|
|
|
fn extend<T: IntoIterator<Item = (K, VariableType, Dynamic)>>(&mut self, iter: T) {
|
2020-03-14 07:30:44 +01:00
|
|
|
self.0
|
|
|
|
.extend(iter.into_iter().map(|(name, var_type, value)| ScopeEntry {
|
|
|
|
name: name.into(),
|
|
|
|
var_type,
|
|
|
|
value,
|
|
|
|
expr: None,
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn map_dynamic_to_expr(value: Dynamic) -> (Option<Expr>, Dynamic) {
|
|
|
|
if value.is::<INT>() {
|
|
|
|
let value2 = value.clone();
|
|
|
|
(
|
|
|
|
Some(Expr::IntegerConstant(
|
|
|
|
*value.downcast::<INT>().expect("value should be INT"),
|
|
|
|
Position::none(),
|
|
|
|
)),
|
|
|
|
value2,
|
|
|
|
)
|
|
|
|
} else if value.is::<char>() {
|
|
|
|
let value2 = value.clone();
|
|
|
|
(
|
|
|
|
Some(Expr::CharConstant(
|
|
|
|
*value.downcast::<char>().expect("value should be char"),
|
|
|
|
Position::none(),
|
|
|
|
)),
|
|
|
|
value2,
|
|
|
|
)
|
|
|
|
} else if value.is::<String>() {
|
|
|
|
let value2 = value.clone();
|
|
|
|
(
|
|
|
|
Some(Expr::StringConstant(
|
|
|
|
*value.downcast::<String>().expect("value should be String"),
|
|
|
|
Position::none(),
|
|
|
|
)),
|
|
|
|
value2,
|
|
|
|
)
|
|
|
|
} else if value.is::<bool>() {
|
|
|
|
let value2 = value.clone();
|
|
|
|
(
|
|
|
|
Some(
|
|
|
|
if *value.downcast::<bool>().expect("value should be bool") {
|
|
|
|
Expr::True(Position::none())
|
|
|
|
} else {
|
|
|
|
Expr::False(Position::none())
|
|
|
|
},
|
|
|
|
),
|
|
|
|
value2,
|
|
|
|
)
|
|
|
|
} else {
|
2020-03-14 13:06:40 +01:00
|
|
|
#[cfg(not(feature = "no_float"))]
|
|
|
|
{
|
|
|
|
if value.is::<FLOAT>() {
|
|
|
|
let value2 = value.clone();
|
|
|
|
return (
|
|
|
|
Some(Expr::FloatConstant(
|
|
|
|
*value.downcast::<FLOAT>().expect("value should be FLOAT"),
|
|
|
|
Position::none(),
|
|
|
|
)),
|
|
|
|
value2,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-14 07:30:44 +01:00
|
|
|
(None, value)
|
2020-03-03 08:20:20 +01:00
|
|
|
}
|
|
|
|
}
|