diff options
| author | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-07-18 14:39:17 +0200 |
|---|---|---|
| committer | Miquel Sabaté Solà <mikisabate@gmail.com> | 2024-12-12 07:41:03 +0100 |
| commit | 54e1239fcccb2383e5caa88844a7229b5441c31e (patch) | |
| tree | b6bbeb16ff01c3b60ad5543be103f36380a2f8c6 /lib/xixanta | |
| parent | 5b1f387d451b3ff136464dcbc71f0c2310a57d01 (diff) | |
| download | tools.nes-54e1239fcccb2383e5caa88844a7229b5441c31e.tar.gz tools.nes-54e1239fcccb2383e5caa88844a7229b5441c31e.zip | |
Add macros which can take arguments
Adds to 5b1f387d451b ("Add basic support for macros") the possibility to
add parameters to macros.
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'lib/xixanta')
| -rw-r--r-- | lib/xixanta/src/assembler.rs | 47 |
1 files changed, 46 insertions, 1 deletions
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<String> = 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::<Vec<_>>(); + } + 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<String>, 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); |
