From 54e1239fcccb2383e5caa88844a7229b5441c31e Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Thu, 18 Jul 2024 14:39:17 +0200 Subject: Add macros which can take arguments MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds to 5b1f387d451b ("Add basic support for macros") the possibility to add parameters to macros. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/assembler.rs | 47 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 46 insertions(+), 1 deletion(-) (limited to 'lib/xixanta/src/assembler.rs') diff --git a/lib/xixanta/src/assembler.rs b/lib/xixanta/src/assembler.rs index d2cd03d..1a542ac 100644 --- a/lib/xixanta/src/assembler.rs +++ b/lib/xixanta/src/assembler.rs @@ -914,7 +914,34 @@ impl Assembler { } } - fn parse_control(&mut self, id: PString, line: &str) -> Result<()> { + fn parse_control(&mut self, mut id: PString, line: &str) -> Result<()> { + self.skip_whitespace(line); + + // Try to handle arguments passed to the control statement. + let mut args: Vec = vec![]; + if let Some(open) = line.find(|c: char| c == '(') { + match line.find(|c: char| c == ')') { + Some(close) => { + id.value = id + .value + .get(..open) + .unwrap_or(id.value.as_str()) + .to_string(); + id.range.end = open; + + args = line + .get(open + 1..close) + .unwrap_or(" ") + .split(',') + .map(|w| w.trim().to_string()) + .collect::>(); + } + None => { + return Err(id.parser_error(format!("open parenthesis on macro call").as_str())) + } + } + } + match id.value.to_lowercase().as_str() { ".scope" => self.parse_scope_definition(&id, line), ".endscope" => self.parse_scope_end(&id), @@ -925,6 +952,8 @@ impl Assembler { ".endproc" => self.parse_proc_end(&id), ".macro" => self.parse_macro_definition(&id, line), ".endmacro" => self.parse_macro_end(&id), + ".hibyte" => self.parse_hi_lo_byte(&id, &args, true), + ".lobyte" => self.parse_hi_lo_byte(&id, &args, false), _ => { return Err( id.parser_error(format!("unknown control statement '{}'", id.value).as_str()) @@ -933,6 +962,22 @@ impl Assembler { } } + fn parse_hi_lo_byte(&mut self, id: &PString, args: &Vec, hi: bool) -> Result<()> { + if args.len() > 1 { + return Err(id.parser_error( + format!( + "only 1 argument was expected, but {} were passed", + args.len() + ) + .as_str(), + )); + } + + println!("{:#?} -- {:#?} -- {}", id, args, hi); + + Ok(()) + } + fn parse_macro_definition(&mut self, id: &PString, line: &str) -> Result<()> { self.skip_whitespace(line); -- cgit v1.2.3