From eb7fff6c62e028e6cdc412266937402412be500d Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 18 Dec 2024 11:39:55 +0100 Subject: Split crunch_and_resolve_pending MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This function originally came from the naive idea I had with how mappings and segments ought to work. For this reason, the function grew more ever more complex. Split this function into more clear responsabilities for each new function. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/assembler.rs | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/xixanta/src/assembler.rs b/lib/xixanta/src/assembler.rs index bf7b5a0..2f09295 100644 --- a/lib/xixanta/src/assembler.rs +++ b/lib/xixanta/src/assembler.rs @@ -100,13 +100,20 @@ impl Assembler { // where scopes start/end, evaluating values for variables, labels, etc. self.eval_context(&parser.nodes)?; - // Finally convert the relevant nodes into binary bundles which can be - // used by the caller. + // Convert the relevant nodes into binary bundles which can be used by + // the caller. This is done for most nodes, even if some of them will + // have to be marked as pending, since they depend on knowing the exact + // size for a given segment. self.stage = Stage::Bundling; self.bundle(&parser.nodes)?; + // Now we know how much each segment spans, and we can resolve (crunch) + // the nodes marked as pending. self.stage = Stage::Crunching; - self.crunch_and_resolve_pending() + self.crunch()?; + + // All set, fill the vector of bundles to be returned. + self.fill() } pub fn eval_context(&mut self, nodes: &[PNode]) -> Result<(), Vec> { @@ -281,8 +288,7 @@ impl Assembler { } } - // TODO: maybe split? - pub fn crunch_and_resolve_pending(&mut self) -> Result, Vec> { + fn crunch(&mut self) -> Result<(), Vec> { let mut errors = vec![]; for pn in self.pending.clone() { @@ -310,6 +316,16 @@ impl Assembler { self.context.force_context_pop(); } + if errors.is_empty() { + Ok(()) + } else { + Err(errors) + } + } + + fn fill(&mut self) -> Result, Vec> { + let mut errors = vec![]; + // Validate the mappings that have been evaluated before spitting it // out. if let Err(e) = crate::mapping::validate(&self.mappings) { -- cgit v1.2.3