Add constants.

This commit is contained in:
Stephen Chung
2020-03-13 18:12:41 +08:00
parent 9bd66c7db3
commit 9844ae8665
9 changed files with 426 additions and 155 deletions

View File

@@ -4,6 +4,12 @@ use crate::any::{Any, Dynamic};
use std::borrow::Cow;
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
pub enum VariableType {
Normal,
Constant,
}
/// A type containing information about current scope.
/// Useful for keeping state between `Engine` runs.
///
@@ -25,7 +31,7 @@ use std::borrow::Cow;
///
/// When searching for variables, newly-added variables are found before similarly-named but older variables,
/// allowing for automatic _shadowing_ of variables.
pub struct Scope<'a>(Vec<(Cow<'a, str>, Dynamic)>);
pub struct Scope<'a>(Vec<(Cow<'a, str>, VariableType, Dynamic)>);
impl<'a> Scope<'a> {
/// Create a new Scope.
@@ -45,17 +51,31 @@ impl<'a> Scope<'a> {
/// Add (push) a new variable to the Scope.
pub fn push<K: Into<Cow<'a, str>>, T: Any>(&mut self, key: K, value: T) {
self.0.push((key.into(), Box::new(value)));
self.0
.push((key.into(), VariableType::Normal, Box::new(value)));
}
/// Add (push) a new variable to the Scope.
pub(crate) fn push_dynamic<K: Into<Cow<'a, str>>>(&mut self, key: K, value: Dynamic) {
self.0.push((key.into(), value));
/// Add (push) a new constant to the Scope.
pub fn push_constant<K: Into<Cow<'a, str>>, T: Any>(&mut self, key: K, value: T) {
self.0
.push((key.into(), VariableType::Constant, Box::new(value)));
}
/// Add (push) a new variable with a `Dynamic` value to the Scope.
pub(crate) fn push_dynamic<K: Into<Cow<'a, str>>>(
&mut self,
key: K,
val_type: VariableType,
value: Dynamic,
) {
self.0.push((key.into(), val_type, value));
}
/// Remove (pop) the last variable from the Scope.
pub fn pop(&mut self) -> Option<(String, Dynamic)> {
self.0.pop().map(|(key, value)| (key.to_string(), value))
pub fn pop(&mut self) -> Option<(String, VariableType, Dynamic)> {
self.0
.pop()
.map(|(key, var_type, value)| (key.to_string(), var_type, value))
}
/// Truncate (rewind) the Scope to a previous size.
@@ -64,13 +84,13 @@ impl<'a> Scope<'a> {
}
/// Find a variable in the Scope, starting from the last.
pub fn get(&self, key: &str) -> Option<(usize, &str, Dynamic)> {
pub fn get(&self, key: &str) -> Option<(usize, &str, VariableType, Dynamic)> {
self.0
.iter()
.enumerate()
.rev() // Always search a Scope in reverse order
.find(|(_, (name, _))| name == key)
.map(|(i, (name, value))| (i, name.as_ref(), value.clone()))
.find(|(_, (name, _, _))| name == key)
.map(|(i, (name, var_type, value))| (i, name.as_ref(), *var_type, value.clone()))
}
/// Get the value of a variable in the Scope, starting from the last.
@@ -79,8 +99,8 @@ impl<'a> Scope<'a> {
.iter()
.enumerate()
.rev() // Always search a Scope in reverse order
.find(|(_, (name, _))| name == key)
.and_then(|(_, (_, value))| value.downcast_ref::<T>())
.find(|(_, (name, _, _))| name == key)
.and_then(|(_, (_, _, value))| value.downcast_ref::<T>())
.map(|value| value.clone())
}
@@ -88,9 +108,14 @@ impl<'a> Scope<'a> {
pub(crate) fn get_mut(&mut self, key: &str, index: usize) -> &mut Dynamic {
let entry = self.0.get_mut(index).expect("invalid index in Scope");
assert_ne!(
entry.1,
VariableType::Constant,
"get mut of constant variable"
);
assert_eq!(entry.0, key, "incorrect key at Scope entry");
&mut entry.1
&mut entry.2
}
/// Get a mutable reference to a variable in the Scope and downcast it to a specific type
@@ -102,11 +127,11 @@ impl<'a> Scope<'a> {
}
/// Get an iterator to variables in the Scope.
pub fn iter(&self) -> impl Iterator<Item = (&str, &Dynamic)> {
pub fn iter(&self) -> impl Iterator<Item = (&str, VariableType, &Dynamic)> {
self.0
.iter()
.rev() // Always search a Scope in reverse order
.map(|(key, value)| (key.as_ref(), value))
.map(|(key, var_type, value)| (key.as_ref(), *var_type, value))
}
/*
@@ -120,12 +145,14 @@ impl<'a> Scope<'a> {
*/
}
impl<'a, K> std::iter::Extend<(K, Dynamic)> for Scope<'a>
impl<'a, K> std::iter::Extend<(K, VariableType, Dynamic)> for Scope<'a>
where
K: Into<Cow<'a, str>>,
{
fn extend<T: IntoIterator<Item = (K, Dynamic)>>(&mut self, iter: T) {
self.0
.extend(iter.into_iter().map(|(key, value)| (key.into(), value)));
fn extend<T: IntoIterator<Item = (K, VariableType, Dynamic)>>(&mut self, iter: T) {
self.0.extend(
iter.into_iter()
.map(|(key, var_type, value)| (key.into(), var_type, value)),
);
}
}