devalang_core/core/store/
variable.rs1use std::collections::HashMap;
2
3use crate::core::shared::value::Value;
4
5#[derive(Debug, Default, Clone, PartialEq)]
6pub struct VariableTable {
7 pub variables: HashMap<String, Value>,
8 pub parent: Option<Box<VariableTable>>,
9}
10
11impl VariableTable {
12 pub fn new() -> Self {
13 VariableTable {
14 variables: HashMap::new(),
15 parent: None,
16 }
17 }
18
19 pub fn set(&mut self, name: String, value: Value) {
20 self.variables.insert(name, value);
21 }
22
23 pub fn with_parent(parent: VariableTable) -> Self {
24 Self { variables: HashMap::new(), parent: Some(Box::new(parent)) }
25 }
26
27 pub fn get(&self, name: &str) -> Option<&Value> {
28 self.variables.get(name)
29 }
30
31 pub fn remove(&mut self, name: &str) -> Option<Value> {
32 self.variables.remove(name)
33 }
34}