pipeline_script/context/
scope.rs

1use crate::llvm::value::LLVMValue;
2use std::collections::HashMap;
3use std::rc::Rc;
4use std::sync::RwLock;
5
6#[derive(Clone, Debug)]
7pub struct Scope {
8    symbol_table: Rc<RwLock<HashMap<String, LLVMValue>>>,
9}
10impl Default for Scope {
11    fn default() -> Self {
12        Self::new()
13    }
14}
15
16impl Scope {
17    pub fn new() -> Self {
18        Self {
19            symbol_table: Rc::new(RwLock::new(HashMap::new())),
20        }
21    }
22    pub fn set(&self, name: impl Into<String>, value: LLVMValue) {
23        let mut symbol_table = self.symbol_table.write().unwrap();
24        symbol_table.insert(name.into(), value);
25    }
26    pub fn has(&self, name: impl AsRef<str>) -> bool {
27        let symbol_table = self.symbol_table.read().unwrap();
28        symbol_table.contains_key(name.as_ref())
29    }
30    pub fn get(&self, name: impl AsRef<str>) -> Option<LLVMValue> {
31        let symbol_table = self.symbol_table.read().unwrap();
32        symbol_table.get(name.as_ref()).cloned()
33    }
34}