diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-05-17 07:39:05 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-12-12 07:30:15 +0100 |
| commit | 587dd5bb80364688419bb43aba4afc044ac0ef48 (patch) | |
| tree | 3f79940572c4b5ff7fab38e9d3a92a908afc8907 /lib/xixanta/src/context.rs | |
| download | tools.nes-587dd5bb8036.tar.gz tools.nes-587dd5bb8036.zip | |
Initial commit
A lot is missing and there is a long road ahead to make the assembler
even work.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'lib/xixanta/src/context.rs')
| -rw-r--r-- | lib/xixanta/src/context.rs | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/xixanta/src/context.rs b/lib/xixanta/src/context.rs new file mode 100644 index 0000000..62ec7e0 --- /dev/null +++ b/lib/xixanta/src/context.rs @@ -0,0 +1,66 @@ +use crate::instruction::PString; +use std::collections::HashMap; + +const GLOBAL_CONTEXT: &str = "Global"; + +#[derive(Debug)] +pub struct Context { + stack: Vec<String>, + map: HashMap<String, HashMap<String, PString>>, +} + +impl Default for Context { + fn default() -> Self { + Context::new() + } +} + +impl Context { + pub fn new() -> Self { + Context { + stack: vec![], + map: HashMap::from([(String::from(GLOBAL_CONTEXT), HashMap::new())]), + } + } + + pub fn reset(&mut self) { + self.stack = vec![]; + } + + pub fn find(&self, name: &str) -> Option<&HashMap<String, PString>> { + self.map.get(name) + } + + pub fn current(&self) -> Option<&HashMap<String, PString>> { + match self.stack.last() { + Some(name) => self.map.get(name), + None => self.map.get(GLOBAL_CONTEXT), + } + } + + pub fn current_mut(&mut self) -> Option<&mut HashMap<String, PString>> { + match self.stack.last() { + Some(name) => self.map.get_mut(name), + None => self.map.get_mut(GLOBAL_CONTEXT), + } + } + + pub fn push(&mut self, identifier: &String) { + let name = match self.stack.last() { + Some(n) => n.to_owned() + &String::from("::") + identifier, + None => identifier.to_string(), + }; + + self.stack.push(name.clone()); + self.map.entry(name).or_default(); + } + + pub fn pop(&mut self) -> bool { + if self.stack.is_empty() { + return false; + } + + self.stack.truncate(self.stack.len() - 1); + true + } +} |
