tiger_lib/parse/pdxfile/
memory.rs1use crate::block::Block;
4use crate::helpers::TigerHashMap;
5use crate::token::Token;
6
7#[derive(Clone, Default, Debug)]
9pub struct PdxfileMemory {
10 variables: TigerHashMap<String, Token>,
12 blocks: TigerHashMap<String, Block>,
14}
15
16impl PdxfileMemory {
17 pub fn merge(&mut self, other: PdxfileMemory) {
18 self.variables.extend(other.variables);
19 self.blocks.extend(other.blocks);
20 }
21}
22
23pub struct CombinedMemory<'global> {
24 global: &'global PdxfileMemory,
25 local: PdxfileMemory,
26}
27
28impl<'global> CombinedMemory<'global> {
29 pub fn new(global: &'global PdxfileMemory) -> Self {
30 Self { global, local: PdxfileMemory::default() }
31 }
32
33 pub fn from_local(global: &'global PdxfileMemory, local: PdxfileMemory) -> Self {
34 Self { global, local }
35 }
36
37 pub fn get_variable(&self, key: &str) -> Option<&Token> {
39 self.local.variables.get(key).or_else(|| self.global.variables.get(key))
40 }
41
42 pub fn has_variable(&self, key: &str) -> bool {
44 self.local.variables.contains_key(key) || self.global.variables.contains_key(key)
45 }
46
47 pub fn set_variable(&mut self, key: String, value: Token) {
49 self.local.variables.insert(key, value);
50 }
51
52 pub fn get_block(&self, key: &str) -> Option<&Block> {
54 self.local.blocks.get(key).or_else(|| self.global.blocks.get(key))
55 }
56
57 pub fn has_block(&self, key: &str) -> bool {
59 self.local.blocks.contains_key(key) || self.global.blocks.contains_key(key)
60 }
61
62 pub fn define_block(&mut self, key: String, block: Block) {
64 self.local.blocks.insert(key, block);
65 }
66
67 pub fn as_global(&self) -> &PdxfileMemory {
69 self.global
70 }
71
72 pub fn get_local(&self) -> PdxfileMemory {
74 self.local.clone()
75 }
76
77 #[cfg(feature = "ck3")]
79 pub fn into_local(self) -> PdxfileMemory {
80 self.local
81 }
82}