ucglib/build/opcode/
scope.rs

1// Copyright 2019 Jeremy Wall
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14use 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}