From 184c39579227c0add80a61d489c242120001d6b6 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Thu, 26 Sep 2024 15:53:57 +0200 Subject: Re-work the parser from scratch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The initial implementation was incredibly naive on how complex an assembler can actually be. Because of this, it never bothered to define and produce a proper AST, and hence it was overall extremely fragile on (not so) corner cases. This commit re-writes everything from scratch for the parser, and it should really be the last fundamental change to the parser other than minor additions/features here and there. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/context.rs | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) (limited to 'lib/xixanta/src/context.rs') diff --git a/lib/xixanta/src/context.rs b/lib/xixanta/src/context.rs index 32f9de9..2ac6da8 100644 --- a/lib/xixanta/src/context.rs +++ b/lib/xixanta/src/context.rs @@ -1,12 +1,13 @@ -use crate::instruction::PString; +use crate::instruction::Bundle; +use crate::parser::PNode; use std::collections::HashMap; const GLOBAL_CONTEXT: &str = "Global"; #[derive(Debug)] pub struct PValue { - pub node: PString, - pub value: usize, + pub node: PNode, + pub value: Bundle, pub label: bool, } @@ -30,10 +31,6 @@ impl Context { } } - pub fn global(&mut self) { - self.stack = vec![]; - } - pub fn find(&self, name: &str) -> Option<&HashMap> { self.map.get(name) } @@ -52,6 +49,17 @@ impl Context { } } + pub fn is_global(&self) -> bool { + self.stack.is_empty() + } + + pub fn name(&self) -> &str { + match self.stack.last() { + Some(name) => name, + None => GLOBAL_CONTEXT, + } + } + pub fn push(&mut self, identifier: &String) { let name = match self.stack.last() { Some(n) => n.to_owned() + &String::from("::") + identifier, @@ -62,14 +70,14 @@ impl Context { self.map.entry(name).or_default(); } - pub fn push_stack(&mut self, identifier: &String) { - let name = match self.stack.last() { - Some(n) => n.to_owned() + &String::from("::") + identifier, - None => identifier.to_string(), - }; + // pub fn push_stack(&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.stack.push(name.clone()); + // } pub fn pop(&mut self) -> bool { if self.stack.is_empty() { -- cgit v1.2.3