From 509744ad69135447ba0f445b3f68207722ffab62 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:05 +0200 Subject: Add a macro for array_push --- src/macro_processor/macro_processor.rs | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index 866fbdd..ad230c0 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -286,6 +286,25 @@ fn smp_builtin_indent( lin.push_str(&smp.process_input(&l)?); out.push_str(&lin); } + Ok(out) +} + +/// Push any arguments to array macro +fn smp_builtin_array_push( + smp: &mut MacroProcessor, + macro_name: &str, + args: &mut [String], +) -> Result { + for arg in args.iter() { + if let Err(e) = smp.array_push(macro_name, MacroType::String(arg.to_string())) { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("Error executing array_push ({:?})", e), + )); + } + } Ok(String::new()) } @@ -428,6 +447,10 @@ impl MacroProcessor { MacroType::Function(smp_builtin_indent), ); self.define_macro(String::from("expr"), MacroType::Function(smp_builtin_expr)); + self.define_macro( + String::from("array_push"), + MacroType::Function(smp_builtin_array_push), + ); #[cfg(feature = "time")] self.define_macro( String::from("format_time"), @@ -456,8 +479,9 @@ impl MacroProcessor { self.macros.insert(name, macro_expansion); } - pub fn array_push(&mut self, name: String, element: MacroType) -> Result<(), SMPError> { - let Some(macro_body) = self.macros.get_mut(&name) else { + /// Push a MacroType into a array, this allows creating special macros that can iterate + pub fn array_push(&mut self, name: &str, element: MacroType) -> Result<(), SMPError> { + let Some(macro_body) = self.macros.get_mut(name) else { return Err(SMPError::UnknownError(4, None)); }; let MacroType::Array(array) = macro_body else { -- cgit v1.2.3