Enable dot operations on constant variables.

This commit is contained in:
Stephen Chung
2020-03-20 19:27:02 +08:00
parent 16ea8f416e
commit ed996e71d6
7 changed files with 385 additions and 238 deletions

View File

@@ -31,6 +31,13 @@ pub struct ScopeEntry<'a> {
pub expr: Option<Expr>,
}
/// Information about a particular entry in the Scope.
pub(crate) struct ScopeSource<'a> {
pub name: &'a str,
pub idx: usize,
pub var_type: VariableType,
}
/// A type containing information about the current scope.
/// Useful for keeping state between `Engine` evaluation runs.
///
@@ -150,7 +157,7 @@ impl<'a> Scope<'a> {
}
/// Find a variable in the Scope, starting from the last.
pub fn get(&self, key: &str) -> Option<(usize, &str, VariableType, Dynamic)> {
pub(crate) fn get(&self, key: &str) -> Option<(ScopeSource, Dynamic)> {
self.0
.iter()
.enumerate()
@@ -165,7 +172,16 @@ impl<'a> Scope<'a> {
value,
..
},
)| (i, name.as_ref(), *var_type, value.clone()),
)| {
(
ScopeSource {
name: name.as_ref(),
idx: i,
var_type: *var_type,
},
value.clone(),
)
},
)
}
@@ -184,11 +200,11 @@ impl<'a> Scope<'a> {
pub(crate) fn get_mut(&mut self, name: &str, index: usize) -> &mut Dynamic {
let entry = self.0.get_mut(index).expect("invalid index in Scope");
assert_ne!(
entry.var_type,
VariableType::Constant,
"get mut of constant variable"
);
// assert_ne!(
// entry.var_type,
// VariableType::Constant,
// "get mut of constant variable"
// );
assert_eq!(entry.name, name, "incorrect key at Scope entry");
&mut entry.value