From 587dd5bb80364688419bb43aba4afc044ac0ef48 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Fri, 17 May 2024 07:39:05 +0200 Subject: Initial commit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A lot is missing and there is a long road ahead to make the assembler even work. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/context.rs | 66 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 lib/xixanta/src/context.rs (limited to 'lib/xixanta/src/context.rs') 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, + map: HashMap>, +} + +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> { + self.map.get(name) + } + + pub fn current(&self) -> Option<&HashMap> { + 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> { + 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 + } +} -- cgit v1.2.3