ucglib/build/opcode/
scope.rs1use std::collections::BTreeMap;
15use std::rc::Rc;
16
17use super::Value;
18use crate::ast::Position;
19
20#[derive(Clone, PartialEq, Debug)]
21pub struct Stack {
22 curr: BTreeMap<String, (Rc<Value>, Position)>,
23}
24
25impl Stack {
26 pub fn new() -> Self {
27 Stack {
28 curr: BTreeMap::new(),
29 }
30 }
31
32 pub fn get(&self, name: &str) -> Option<(Rc<Value>, Position)> {
33 self.curr.get(name).cloned()
34 }
35
36 pub fn remove_symbol(&mut self, name: &str) -> Option<(Rc<Value>, Position)> {
37 self.curr.remove(name)
38 }
39
40 pub fn is_bound(&self, name: &str) -> bool {
41 self.curr.get(name).is_some()
42 }
43
44 pub fn add(&mut self, name: String, val: Rc<Value>, pos: Position) {
45 self.curr.insert(name, (val, pos));
46 }
47
48 pub fn symbol_list(&self) -> Vec<&String> {
49 self.curr.keys().collect()
50 }
51
52 pub fn snapshot(&self) -> Self {
53 Self {
54 curr: self.curr.clone(),
55 }
56 }
57}