use crate::any::{Dynamic, Variant};
use crate::parser::{map_dynamic_to_expr, Expr};
use crate::token::Position;
use crate::stdlib::{borrow::Cow, boxed::Box, iter, vec::Vec};
#[derive(Debug, Eq, PartialEq, Hash, Copy, Clone)]
pub enum EntryType {
Normal,
Constant,
}
#[derive(Debug)]
pub struct Entry<'a> {
pub name: Cow<'a, str>,
pub typ: EntryType,
pub value: Dynamic,
pub expr: Option<Box<Expr>>,
}
#[derive(Debug)]
pub struct Scope<'a>(Vec<Entry<'a>>);
impl<'a> Scope<'a> {
pub fn new() -> Self {
Self(Vec::new())
}
pub fn clear(&mut self) {
self.0.clear();
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.len() == 0
}
pub fn push<K: Into<Cow<'a, str>>, T: Variant + Clone>(&mut self, name: K, value: T) {
self.push_dynamic_value(name, EntryType::Normal, Dynamic::from(value), false);
}
pub fn push_dynamic<K: Into<Cow<'a, str>>>(&mut self, name: K, value: Dynamic) {
self.push_dynamic_value(name, EntryType::Normal, value, false);
}
pub fn push_constant<K: Into<Cow<'a, str>>, T: Variant + Clone>(&mut self, name: K, value: T) {
self.push_dynamic_value(name, EntryType::Constant, Dynamic::from(value), true);
}
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);
}
pub(crate) fn push_dynamic_value<K: Into<Cow<'a, str>>>(
&mut self,
name: K,
entry_type: EntryType,
value: Dynamic,
map_expr: bool,
) {
let expr = if map_expr {
map_dynamic_to_expr(value.clone(), Position::none()).map(Box::new)
} else {
None
};
self.0.push(Entry {
name: name.into(),
typ: entry_type,
value: value.into(),
expr,
});
}
pub fn rewind(&mut self, size: usize) {
self.0.truncate(size);
}
pub fn contains(&self, name: &str) -> bool {
self.0
.iter()
.rev() .any(|Entry { name: key, .. }| name == key)
}
pub(crate) fn get(&self, name: &str) -> Option<(usize, EntryType)> {
self.0
.iter()
.enumerate()
.rev() .find_map(|(index, Entry { name: key, typ, .. })| {
if name == key {
Some((index, *typ))
} else {
None
}
})
}
pub fn get_value<T: Variant + Clone>(&self, name: &str) -> Option<T> {
self.0
.iter()
.rev()
.find(|Entry { name: key, .. }| name == key)
.and_then(|Entry { value, .. }| value.downcast_ref::<T>().cloned())
}
pub fn set_value<T: Variant + Clone>(&mut self, name: &'a str, value: T) {
match self.get(name) {
Some((_, EntryType::Constant)) => panic!("variable {} is constant", name),
Some((index, EntryType::Normal)) => {
self.0.get_mut(index).unwrap().value = Dynamic::from(value)
}
None => self.push(name, value),
}
}
pub(crate) fn get_mut(&mut self, index: usize) -> (&mut Dynamic, EntryType) {
let entry = self.0.get_mut(index).expect("invalid index in Scope");
(&mut entry.value, entry.typ)
}
pub(crate) fn iter(&self) -> impl Iterator<Item = &Entry> {
self.0.iter().rev() }
}
impl Default for Scope<'_> {
fn default() -> Self {
Scope::new()
}
}
impl<'a, K: Into<Cow<'a, str>>> iter::Extend<(K, EntryType, Dynamic)> for Scope<'a> {
fn extend<T: IntoIterator<Item = (K, EntryType, Dynamic)>>(&mut self, iter: T) {
self.0
.extend(iter.into_iter().map(|(name, typ, value)| Entry {
name: name.into(),
typ,
value: value.into(),
expr: None,
}));
}
}