tiger_lib/parse/pdxfile/
memory.rsuse crate::block::Block;
use crate::helpers::TigerHashMap;
use crate::token::Token;
#[derive(Clone, Default, Debug)]
pub struct PdxfileMemory {
variables: TigerHashMap<String, Token>,
blocks: TigerHashMap<String, Block>,
}
impl PdxfileMemory {
pub fn merge(&mut self, other: PdxfileMemory) {
self.variables.extend(other.variables);
self.blocks.extend(other.blocks);
}
}
pub struct CombinedMemory<'global> {
global: &'global PdxfileMemory,
local: PdxfileMemory,
}
impl<'global> CombinedMemory<'global> {
pub fn new(global: &'global PdxfileMemory) -> Self {
Self { global, local: PdxfileMemory::default() }
}
pub fn from_local(global: &'global PdxfileMemory, local: PdxfileMemory) -> Self {
Self { global, local }
}
pub fn get_variable(&self, key: &str) -> Option<&Token> {
self.local.variables.get(key).or_else(|| self.global.variables.get(key))
}
pub fn has_variable(&self, key: &str) -> bool {
self.local.variables.contains_key(key) || self.global.variables.contains_key(key)
}
pub fn set_variable(&mut self, key: String, value: Token) {
self.local.variables.insert(key, value);
}
pub fn get_block(&self, key: &str) -> Option<&Block> {
self.local.blocks.get(key).or_else(|| self.global.blocks.get(key))
}
pub fn has_block(&self, key: &str) -> bool {
self.local.blocks.contains_key(key) || self.global.blocks.contains_key(key)
}
pub fn define_block(&mut self, key: String, block: Block) {
self.local.blocks.insert(key, block);
}
pub fn as_global(&self) -> &PdxfileMemory {
self.global
}
pub fn get_local(&self) -> PdxfileMemory {
self.local.clone()
}
#[cfg(feature = "ck3")]
pub fn into_local(self) -> PdxfileMemory {
self.local
}
}