use std::collections::HashMap;
#[derive(Default)]
pub struct Scopes {
stack: Vec<HashMap<String, String>>,
}
impl Scopes {
pub fn new() -> Self {
Self {
stack: vec![HashMap::new()],
}
}
pub fn push(&mut self) {
self.stack.push(HashMap::new());
}
pub fn pop(&mut self) {
self.stack.pop();
}
pub fn insert(&mut self, name: String, struct_name: String) {
if let Some(top) = self.stack.last_mut() {
top.insert(name, struct_name);
}
}
pub fn lookup(&self, name: &str) -> Option<&str> {
self.stack.last()?.get(name).map(String::as_str)
}
pub fn snapshot(&self) -> HashMap<String, String> {
self.stack.last().cloned().unwrap_or_default()
}
}