Refine Scope API.

This commit is contained in:
Stephen Chung 2020-03-25 11:27:18 +08:00
parent 3bc02a99ad
commit 5aea997672
4 changed files with 156 additions and 156 deletions

View File

@ -3,7 +3,7 @@
use crate::any::{Any, AnyExt, Dynamic, Variant}; use crate::any::{Any, AnyExt, Dynamic, Variant};
use crate::parser::{Expr, FnDef, Position, ReturnType, Stmt, INT}; use crate::parser::{Expr, FnDef, Position, ReturnType, Stmt, INT};
use crate::result::EvalAltResult; use crate::result::EvalAltResult;
use crate::scope::{Scope, ScopeSource, VariableType}; use crate::scope::{EntryRef as ScopeSource, EntryType as ScopeEntryType, Scope};
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
use crate::optimize::OptimizationLevel; use crate::optimize::OptimizationLevel;
@ -86,9 +86,8 @@ pub struct Engine<'e> {
pub(crate) on_debug: Box<dyn FnMut(&str) + 'e>, pub(crate) on_debug: Box<dyn FnMut(&str) + 'e>,
} }
impl Engine<'_> { impl Default for Engine<'_> {
/// Create a new `Engine` fn default() -> Self {
pub fn new() -> Self {
// User-friendly names for built-in types // User-friendly names for built-in types
let type_names = [ let type_names = [
#[cfg(not(feature = "no_index"))] #[cfg(not(feature = "no_index"))]
@ -97,7 +96,7 @@ impl Engine<'_> {
(type_name::<Dynamic>(), "dynamic"), (type_name::<Dynamic>(), "dynamic"),
] ]
.iter() .iter()
.map(|(k, v)| (k.to_string(), v.to_string())) .map(|(k, v)| ((*k).to_string(), (*v).to_string()))
.collect(); .collect();
// Create the new scripting Engine // Create the new scripting Engine
@ -125,6 +124,13 @@ impl Engine<'_> {
engine engine
} }
}
impl Engine<'_> {
/// Create a new `Engine`
pub fn new() -> Self {
Default::default()
}
/// Control whether and how the `Engine` will optimize an AST after compilation /// Control whether and how the `Engine` will optimize an AST after compilation
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
@ -178,7 +184,7 @@ impl Engine<'_> {
.params .params
.iter() .iter()
.zip(args.iter().map(|x| (*x).into_dynamic())) .zip(args.iter().map(|x| (*x).into_dynamic()))
.map(|(name, value)| (name, VariableType::Normal, value)), .map(|(name, value)| (name, ScopeEntryType::Normal, value)),
); );
// Evaluate // Evaluate
@ -288,8 +294,9 @@ impl Engine<'_> {
} else { } else {
// Otherwise, if `src` is `Some`, then it holds a name and index into `scope`; // Otherwise, if `src` is `Some`, then it holds a name and index into `scope`;
// using `get_mut` on `scope` to retrieve a mutable reference for return. // using `get_mut` on `scope` to retrieve a mutable reference for return.
let ScopeSource { name, idx, .. } = src.expect("expected source in scope"); scope
scope.get_mut(name, idx).as_mut() .get_mut(src.expect("expected source in scope"))
.as_mut()
} }
} }
@ -419,18 +426,14 @@ impl Engine<'_> {
match dot_lhs { match dot_lhs {
// id.??? // id.???
Expr::Variable(id, pos) => { Expr::Variable(id, pos) => {
let (ScopeSource { idx, var_type, .. }, _) = let (entry, _) = Self::search_scope(scope, id, Ok, *pos)?;
Self::search_scope(scope, id, Ok, *pos)?;
// Avoid referencing scope which is used below as mut
let entry = ScopeSource { name: id, ..entry };
// This is a variable property access (potential function call). // This is a variable property access (potential function call).
// Use a direct index into `scope` to directly mutate the variable value. // Use a direct index into `scope` to directly mutate the variable value.
let src = ScopeSource { self.get_dot_val_helper(scope, Some(entry), None, dot_rhs)
name: id,
idx,
var_type,
};
self.get_dot_val_helper(scope, Some(src), None, dot_rhs)
} }
// idx_lhs[idx_expr].??? // idx_lhs[idx_expr].???
@ -442,14 +445,14 @@ impl Engine<'_> {
// In case the expression mutated `target`, we need to update it back into the scope because it is cloned. // In case the expression mutated `target`, we need to update it back into the scope because it is cloned.
if let Some(src) = src { if let Some(src) = src {
match src.var_type { match src.typ {
VariableType::Constant => { ScopeEntryType::Constant => {
return Err(EvalAltResult::ErrorAssignmentToConstant( return Err(EvalAltResult::ErrorAssignmentToConstant(
src.name.to_string(), src.name.to_string(),
idx_lhs.position(), idx_lhs.position(),
)); ));
} }
VariableType::Normal => { ScopeEntryType::Normal => {
Self::update_indexed_var_in_scope( Self::update_indexed_var_in_scope(
src_type, src_type,
scope, scope,
@ -570,8 +573,8 @@ impl Engine<'_> {
src_type, src_type,
Some(ScopeSource { Some(ScopeSource {
name: &id, name: &id,
var_type: src.var_type, typ: src.typ,
idx: src.idx, index: src.index,
}), }),
idx as usize, idx as usize,
val, val,
@ -614,14 +617,14 @@ impl Engine<'_> {
match src_type { match src_type {
// array_id[idx] = val // array_id[idx] = val
IndexSourceType::Array => { IndexSourceType::Array => {
let arr = scope.get_mut_by_type::<Array>(src.name, src.idx); let arr = scope.get_mut_by_type::<Array>(src);
arr[idx as usize] = new_val.0; arr[idx as usize] = new_val.0;
Ok(().into_dynamic()) Ok(().into_dynamic())
} }
// string_id[idx] = val // string_id[idx] = val
IndexSourceType::String => { IndexSourceType::String => {
let s = scope.get_mut_by_type::<String>(src.name, src.idx); let s = scope.get_mut_by_type::<String>(src);
let pos = new_val.1; let pos = new_val.1;
// Value must be a character // Value must be a character
let ch = *new_val let ch = *new_val
@ -793,19 +796,21 @@ impl Engine<'_> {
match dot_lhs { match dot_lhs {
// id.??? // id.???
Expr::Variable(id, pos) => { Expr::Variable(id, pos) => {
let (ScopeSource { idx, var_type, .. }, mut target) = let (entry, mut target) = Self::search_scope(scope, id, Ok, *pos)?;
Self::search_scope(scope, id, Ok, *pos)?;
match var_type { match entry.typ {
VariableType::Constant => Err(EvalAltResult::ErrorAssignmentToConstant( ScopeEntryType::Constant => Err(EvalAltResult::ErrorAssignmentToConstant(
id.to_string(), id.to_string(),
op_pos, op_pos,
)), )),
_ => { _ => {
// Avoid referencing scope which is used below as mut
let entry = ScopeSource { name: id, ..entry };
let val = self.set_dot_val_helper(scope, target.as_mut(), dot_rhs, new_val); let val = self.set_dot_val_helper(scope, target.as_mut(), dot_rhs, new_val);
// In case the expression mutated `target`, we need to update it back into the scope because it is cloned. // In case the expression mutated `target`, we need to update it back into the scope because it is cloned.
*scope.get_mut(id, idx) = target; *scope.get_mut(entry) = target;
val val
} }
@ -823,14 +828,14 @@ impl Engine<'_> {
// In case the expression mutated `target`, we need to update it back into the scope because it is cloned. // In case the expression mutated `target`, we need to update it back into the scope because it is cloned.
if let Some(src) = src { if let Some(src) = src {
match src.var_type { match src.typ {
VariableType::Constant => { ScopeEntryType::Constant => {
return Err(EvalAltResult::ErrorAssignmentToConstant( return Err(EvalAltResult::ErrorAssignmentToConstant(
src.name.to_string(), src.name.to_string(),
lhs.position(), lhs.position(),
)); ));
} }
VariableType::Normal => { ScopeEntryType::Normal => {
Self::update_indexed_var_in_scope( Self::update_indexed_var_in_scope(
src_type, src_type,
scope, scope,
@ -880,29 +885,30 @@ impl Engine<'_> {
match lhs.as_ref() { match lhs.as_ref() {
// name = rhs // name = rhs
Expr::Variable(name, pos) => match scope.get(name) { Expr::Variable(name, pos) => match scope
Some(( .get(name)
.ok_or_else(|| EvalAltResult::ErrorVariableNotFound(name.clone(), *pos))?
.0
{
entry
@
ScopeSource { ScopeSource {
idx, typ: ScopeEntryType::Normal,
var_type: VariableType::Normal,
.. ..
}, } => {
_, // Avoid referencing scope which is used below as mut
)) => { let entry = ScopeSource { name, ..entry };
*scope.get_mut(name, idx) = rhs_val.clone();
*scope.get_mut(entry) = rhs_val.clone();
Ok(rhs_val) Ok(rhs_val)
} }
Some((
ScopeSource { ScopeSource {
var_type: VariableType::Constant, typ: ScopeEntryType::Constant,
.. ..
}, } => Err(EvalAltResult::ErrorAssignmentToConstant(
_,
)) => Err(EvalAltResult::ErrorAssignmentToConstant(
name.to_string(), name.to_string(),
*op_pos, *op_pos,
)), )),
_ => Err(EvalAltResult::ErrorVariableNotFound(name.clone(), *pos)),
}, },
// idx_lhs[idx_expr] = rhs // idx_lhs[idx_expr] = rhs
@ -912,14 +918,14 @@ impl Engine<'_> {
self.eval_index_expr(scope, idx_lhs, idx_expr, *op_pos)?; self.eval_index_expr(scope, idx_lhs, idx_expr, *op_pos)?;
if let Some(src) = src { if let Some(src) = src {
match src.var_type { match src.typ {
VariableType::Constant => { ScopeEntryType::Constant => {
Err(EvalAltResult::ErrorAssignmentToConstant( Err(EvalAltResult::ErrorAssignmentToConstant(
src.name.to_string(), src.name.to_string(),
idx_lhs.position(), idx_lhs.position(),
)) ))
} }
VariableType::Normal => Ok(Self::update_indexed_var_in_scope( ScopeEntryType::Normal => Ok(Self::update_indexed_var_in_scope(
src_type, src_type,
scope, scope,
src, src,
@ -1203,10 +1209,15 @@ impl Engine<'_> {
if let Some(iter_fn) = self.type_iterators.get(&tid) { if let Some(iter_fn) = self.type_iterators.get(&tid) {
scope.push(name.clone(), ()); scope.push(name.clone(), ());
let idx = scope.len() - 1;
let entry = ScopeSource {
name,
index: scope.len() - 1,
typ: ScopeEntryType::Normal,
};
for a in iter_fn(&arr) { for a in iter_fn(&arr) {
*scope.get_mut(name, idx) = a; *scope.get_mut(entry) = a;
match self.eval_stmt(scope, body) { match self.eval_stmt(scope, body) {
Ok(_) => (), Ok(_) => (),
@ -1214,7 +1225,8 @@ impl Engine<'_> {
Err(x) => return Err(x), Err(x) => return Err(x),
} }
} }
scope.pop();
scope.rewind(scope.len() - 1);
Ok(().into_dynamic()) Ok(().into_dynamic())
} else { } else {
Err(EvalAltResult::ErrorFor(expr.position())) Err(EvalAltResult::ErrorFor(expr.position()))
@ -1253,7 +1265,7 @@ impl Engine<'_> {
// Let statement // Let statement
Stmt::Let(name, Some(expr), _) => { Stmt::Let(name, Some(expr), _) => {
let val = self.eval_expr(scope, expr)?; let val = self.eval_expr(scope, expr)?;
scope.push_dynamic(name.clone(), VariableType::Normal, val); scope.push_dynamic_value(name.clone(), ScopeEntryType::Normal, val, false);
Ok(().into_dynamic()) Ok(().into_dynamic())
} }
@ -1265,7 +1277,7 @@ impl Engine<'_> {
// Const statement // Const statement
Stmt::Const(name, expr, _) if expr.is_constant() => { Stmt::Const(name, expr, _) if expr.is_constant() => {
let val = self.eval_expr(scope, expr)?; let val = self.eval_expr(scope, expr)?;
scope.push_dynamic(name.clone(), VariableType::Constant, val); scope.push_dynamic_value(name.clone(), ScopeEntryType::Constant, val, true);
Ok(().into_dynamic()) Ok(().into_dynamic())
} }

View File

@ -6,7 +6,7 @@ use crate::engine::{
KEYWORD_TYPE_OF, KEYWORD_TYPE_OF,
}; };
use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST}; use crate::parser::{map_dynamic_to_expr, Expr, FnDef, ReturnType, Stmt, AST};
use crate::scope::{Scope, ScopeEntry, VariableType}; use crate::scope::{Entry as ScopeEntry, EntryType as ScopeEntryType, Scope};
use crate::stdlib::{ use crate::stdlib::{
boxed::Box, boxed::Box,
@ -512,9 +512,9 @@ pub(crate) fn optimize<'a>(statements: Vec<Stmt>, engine: &Engine<'a>, scope: &S
// Add constants from the scope into the state // Add constants from the scope into the state
scope scope
.iter() .iter()
.filter(|ScopeEntry { var_type, expr, .. }| { .filter(|ScopeEntry { typ, expr, .. }| {
// Get all the constants with definite constant expressions // Get all the constants with definite constant expressions
*var_type == VariableType::Constant *typ == ScopeEntryType::Constant
&& expr.as_ref().map(Expr::is_constant).unwrap_or(false) && expr.as_ref().map(Expr::is_constant).unwrap_or(false)
}) })
.for_each(|ScopeEntry { name, expr, .. }| { .for_each(|ScopeEntry { name, expr, .. }| {

View File

@ -3,7 +3,7 @@
use crate::any::{Any, AnyExt, Dynamic}; use crate::any::{Any, AnyExt, Dynamic};
use crate::engine::Engine; use crate::engine::Engine;
use crate::error::{LexError, ParseError, ParseErrorType}; use crate::error::{LexError, ParseError, ParseErrorType};
use crate::scope::{Scope, VariableType}; use crate::scope::{EntryType as ScopeEntryType, Scope};
#[cfg(not(feature = "no_optimize"))] #[cfg(not(feature = "no_optimize"))]
use crate::optimize::optimize_into_ast; use crate::optimize::optimize_into_ast;
@ -2024,7 +2024,7 @@ fn parse_for<'a>(
/// Parse a variable definition statement. /// Parse a variable definition statement.
fn parse_let<'a>( fn parse_let<'a>(
input: &mut Peekable<TokenIterator<'a>>, input: &mut Peekable<TokenIterator<'a>>,
var_type: VariableType, var_type: ScopeEntryType,
allow_stmt_expr: bool, allow_stmt_expr: bool,
) -> Result<Stmt, ParseError> { ) -> Result<Stmt, ParseError> {
// let/const... (specified in `var_type`) // let/const... (specified in `var_type`)
@ -2049,13 +2049,13 @@ fn parse_let<'a>(
match var_type { match var_type {
// let name = expr // let name = expr
VariableType::Normal => Ok(Stmt::Let(name, Some(Box::new(init_value)), pos)), ScopeEntryType::Normal => Ok(Stmt::Let(name, Some(Box::new(init_value)), pos)),
// const name = { expr:constant } // const name = { expr:constant }
VariableType::Constant if init_value.is_constant() => { ScopeEntryType::Constant if init_value.is_constant() => {
Ok(Stmt::Const(name, Box::new(init_value), pos)) Ok(Stmt::Const(name, Box::new(init_value), pos))
} }
// const name = expr - error // const name = expr - error
VariableType::Constant => Err(ParseError( ScopeEntryType::Constant => Err(ParseError(
PERR::ForbiddenConstantExpr(name), PERR::ForbiddenConstantExpr(name),
init_value.position(), init_value.position(),
)), )),
@ -2186,8 +2186,8 @@ fn parse_stmt<'a>(
} }
} }
(Token::LeftBrace, _) => parse_block(input, breakable, allow_stmt_expr), (Token::LeftBrace, _) => parse_block(input, breakable, allow_stmt_expr),
(Token::Let, _) => parse_let(input, VariableType::Normal, allow_stmt_expr), (Token::Let, _) => parse_let(input, ScopeEntryType::Normal, allow_stmt_expr),
(Token::Const, _) => parse_let(input, VariableType::Constant, allow_stmt_expr), (Token::Const, _) => parse_let(input, ScopeEntryType::Constant, allow_stmt_expr),
_ => parse_expr_stmt(input, allow_stmt_expr), _ => parse_expr_stmt(input, allow_stmt_expr),
} }
} }

View File

@ -10,32 +10,34 @@ use crate::stdlib::{
vec::Vec, vec::Vec,
}; };
/// Type of a variable in the Scope. /// Type of an entry in the Scope.
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)] #[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
pub enum VariableType { pub enum EntryType {
/// Normal variable. /// Normal value.
Normal, Normal,
/// Immutable constant value. /// Immutable constant value.
Constant, Constant,
} }
/// An entry in the Scope. /// An entry in the Scope.
pub struct ScopeEntry<'a> { #[derive(Debug, Clone)]
/// Name of the variable. pub struct Entry<'a> {
/// Name of the entry.
pub name: Cow<'a, str>, pub name: Cow<'a, str>,
/// Type of the variable. /// Type of the entry.
pub var_type: VariableType, pub typ: EntryType,
/// Current value of the variable. /// Current value of the entry.
pub value: Dynamic, pub value: Dynamic,
/// A constant expression if the initial value matches one of the recognized types. /// A constant expression if the initial value matches one of the recognized types.
pub expr: Option<Expr>, pub expr: Option<Expr>,
} }
/// Information about a particular entry in the Scope. /// Information about a particular entry in the Scope.
pub(crate) struct ScopeSource<'a> { #[derive(Debug, Hash, Copy, Clone)]
pub(crate) struct EntryRef<'a> {
pub name: &'a str, pub name: &'a str,
pub idx: usize, pub index: usize,
pub var_type: VariableType, pub typ: EntryType,
} }
/// A type containing information about the current scope. /// A type containing information about the current scope.
@ -57,9 +59,9 @@ pub(crate) struct ScopeSource<'a> {
/// # } /// # }
/// ``` /// ```
/// ///
/// When searching for variables, newly-added variables are found before similarly-named but older variables, /// When searching for entries, newly-added entries are found before similarly-named but older entries,
/// allowing for automatic _shadowing_ of variables. /// allowing for automatic _shadowing_.
pub struct Scope<'a>(Vec<ScopeEntry<'a>>); pub struct Scope<'a>(Vec<Entry<'a>>);
impl<'a> Scope<'a> { impl<'a> Scope<'a> {
/// Create a new Scope. /// Create a new Scope.
@ -72,7 +74,7 @@ impl<'a> Scope<'a> {
self.0.clear(); self.0.clear();
} }
/// Get the number of variables inside the Scope. /// Get the number of entries inside the Scope.
pub fn len(&self) -> usize { pub fn len(&self) -> usize {
self.0.len() self.0.len()
} }
@ -82,19 +84,14 @@ impl<'a> Scope<'a> {
self.0.len() == 0 self.0.len() == 0
} }
/// Add (push) a new variable to the Scope. /// Add (push) a new entry to the Scope.
pub fn push<K: Into<Cow<'a, str>>, T: Any + Clone>(&mut self, name: K, value: T) { pub fn push<K: Into<Cow<'a, str>>, T: Any + Clone>(&mut self, name: K, value: T) {
let value = value.into_dynamic(); self.push_dynamic_value(name, EntryType::Normal, value.into_dynamic(), false);
}
// Map into constant expressions /// Add (push) a new `Dynamic` entry to the Scope.
//let (expr, value) = map_dynamic_to_expr(value, Position::none()); pub fn push_dynamic<K: Into<Cow<'a, str>>>(&mut self, name: K, value: Dynamic) {
self.push_dynamic_value(name, EntryType::Normal, value, false);
self.0.push(ScopeEntry {
name: name.into(),
var_type: VariableType::Normal,
value,
expr: None,
});
} }
/// Add (push) a new constant to the Scope. /// Add (push) a new constant to the Scope.
@ -104,84 +101,75 @@ impl<'a> Scope<'a> {
/// However, in order to be used for optimization, constants must be in one of the recognized types: /// 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`. /// `INT` (default to `i64`, `i32` if `only_i32`), `f64`, `String`, `char` and `bool`.
pub fn push_constant<K: Into<Cow<'a, str>>, T: Any + Clone>(&mut self, name: K, value: T) { pub fn push_constant<K: Into<Cow<'a, str>>, T: Any + Clone>(&mut self, name: K, value: T) {
let value = value.into_dynamic(); self.push_dynamic_value(name, EntryType::Constant, value.into_dynamic(), true);
// Map into constant expressions
let (expr, value) = map_dynamic_to_expr(value, Position::none());
self.0.push(ScopeEntry {
name: name.into(),
var_type: VariableType::Constant,
value,
expr,
});
} }
/// Add (push) a new variable with a `Dynamic` value to the Scope. /// Add (push) a new constant with a `Dynamic` value to the Scope.
pub(crate) fn push_dynamic<K: Into<Cow<'a, str>>>( ///
/// 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, the `Dynamic` value must be in one of the
/// recognized types:
/// `INT` (default to `i64`, `i32` if `only_i32`), `f64`, `String`, `char` and `bool`.
pub fn push_constant_dynamic<K: Into<Cow<'a, str>>>(&mut self, name: K, value: Dynamic) {
self.push_dynamic_value(name, EntryType::Constant, value, true);
}
/// Add (push) a new entry with a `Dynamic` value to the Scope.
pub(crate) fn push_dynamic_value<K: Into<Cow<'a, str>>>(
&mut self, &mut self,
name: K, name: K,
var_type: VariableType, entry_type: EntryType,
value: Dynamic, value: Dynamic,
map_expr: bool,
) { ) {
let (expr, value) = map_dynamic_to_expr(value, Position::none()); let (expr, value) = if map_expr {
map_dynamic_to_expr(value, Position::none())
} else {
(None, value)
};
self.0.push(ScopeEntry { self.0.push(Entry {
name: name.into(), name: name.into(),
var_type, typ: entry_type,
value, value,
expr, expr,
}); });
} }
/// Remove (pop) the last variable from the Scope.
pub fn pop(&mut self) -> Option<(String, VariableType, Dynamic)> {
self.0.pop().map(
|ScopeEntry {
name,
var_type,
value,
..
}| (name.to_string(), var_type, value),
)
}
/// Truncate (rewind) the Scope to a previous size. /// Truncate (rewind) the Scope to a previous size.
pub fn rewind(&mut self, size: usize) { pub fn rewind(&mut self, size: usize) {
self.0.truncate(size); self.0.truncate(size);
} }
/// Does the scope contain the variable? /// Does the scope contain the entry?
pub fn contains(&self, key: &str) -> bool { pub fn contains(&self, key: &str) -> bool {
self.0 self.0
.iter() .iter()
.enumerate() .enumerate()
.rev() // Always search a Scope in reverse order .rev() // Always search a Scope in reverse order
.any(|(_, ScopeEntry { name, .. })| name == key) .any(|(_, Entry { name, .. })| name == key)
} }
/// Find a variable in the Scope, starting from the last. /// Find an entry in the Scope, starting from the last.
pub(crate) fn get(&self, key: &str) -> Option<(ScopeSource, Dynamic)> { pub(crate) fn get(&self, key: &str) -> Option<(EntryRef, Dynamic)> {
self.0 self.0
.iter() .iter()
.enumerate() .enumerate()
.rev() // Always search a Scope in reverse order .rev() // Always search a Scope in reverse order
.find(|(_, ScopeEntry { name, .. })| name == key) .find(|(_, Entry { name, .. })| name == key)
.map( .map(
|( |(
i, i,
ScopeEntry { Entry {
name, name, typ, value, ..
var_type,
value,
..
}, },
)| { )| {
( (
ScopeSource { EntryRef {
name: name.as_ref(), name: name.as_ref(),
idx: i, index: i,
var_type: *var_type, typ: *typ,
}, },
value.clone(), value.clone(),
) )
@ -189,41 +177,41 @@ impl<'a> Scope<'a> {
) )
} }
/// Get the value of a variable in the Scope, starting from the last. /// Get the value of an entry in the Scope, starting from the last.
pub fn get_value<T: Any + Clone>(&self, key: &str) -> Option<T> { pub fn get_value<T: Any + Clone>(&self, key: &str) -> Option<T> {
self.0 self.0
.iter() .iter()
.enumerate() .enumerate()
.rev() // Always search a Scope in reverse order .rev() // Always search a Scope in reverse order
.find(|(_, ScopeEntry { name, .. })| name == key) .find(|(_, Entry { name, .. })| name == key)
.and_then(|(_, ScopeEntry { value, .. })| value.downcast_ref::<T>()) .and_then(|(_, Entry { value, .. })| value.downcast_ref::<T>())
.map(T::clone) .map(T::clone)
} }
/// Get a mutable reference to a variable in the Scope. /// Get a mutable reference to an entry in the Scope.
pub(crate) fn get_mut(&mut self, name: &str, index: usize) -> &mut Dynamic { pub(crate) fn get_mut(&mut self, key: EntryRef) -> &mut Dynamic {
let entry = self.0.get_mut(index).expect("invalid index in Scope"); let entry = self.0.get_mut(key.index).expect("invalid index in Scope");
assert_eq!(entry.typ, key.typ, "entry type not matched");
// assert_ne!( // assert_ne!(
// entry.var_type, // entry.typ,
// VariableType::Constant, // EntryType::Constant,
// "get mut of constant variable" // "get mut of constant entry"
// ); // );
assert_eq!(entry.name, name, "incorrect key at Scope entry"); assert_eq!(entry.name, key.name, "incorrect key at Scope entry");
&mut entry.value &mut entry.value
} }
/// Get a mutable reference to a variable in the Scope and downcast it to a specific type /// Get a mutable reference to an entry in the Scope and downcast it to a specific type
#[cfg(not(feature = "no_index"))] pub(crate) fn get_mut_by_type<T: Any + Clone>(&mut self, key: EntryRef) -> &mut T {
pub(crate) fn get_mut_by_type<T: Any + Clone>(&mut self, name: &str, index: usize) -> &mut T { self.get_mut(key)
self.get_mut(name, index)
.downcast_mut::<T>() .downcast_mut::<T>()
.expect("wrong type cast") .expect("wrong type cast")
} }
/// Get an iterator to variables in the Scope. /// Get an iterator to entries in the Scope.
pub fn iter(&self) -> impl Iterator<Item = &ScopeEntry> { pub fn iter(&self) -> impl Iterator<Item = &Entry> {
self.0.iter().rev() // Always search a Scope in reverse order self.0.iter().rev() // Always search a Scope in reverse order
} }
} }
@ -234,15 +222,15 @@ impl Default for Scope<'_> {
} }
} }
impl<'a, K> iter::Extend<(K, VariableType, Dynamic)> for Scope<'a> impl<'a, K> iter::Extend<(K, EntryType, Dynamic)> for Scope<'a>
where where
K: Into<Cow<'a, str>>, K: Into<Cow<'a, str>>,
{ {
fn extend<T: IntoIterator<Item = (K, VariableType, Dynamic)>>(&mut self, iter: T) { fn extend<T: IntoIterator<Item = (K, EntryType, Dynamic)>>(&mut self, iter: T) {
self.0 self.0
.extend(iter.into_iter().map(|(name, var_type, value)| ScopeEntry { .extend(iter.into_iter().map(|(name, typ, value)| Entry {
name: name.into(), name: name.into(),
var_type, typ,
value, value,
expr: None, expr: None,
})); }));