From 9970b48411b9816fbfa5b6b100a91baed0d44849 Mon Sep 17 00:00:00 2001 From: Qrius Date: Thu, 26 Sep 2024 00:11:05 +0200 Subject: Add array_size --- src/macro_processor/macro_processor.rs | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/macro_processor') diff --git a/src/macro_processor/macro_processor.rs b/src/macro_processor/macro_processor.rs index ba28b15..612d2c5 100644 --- a/src/macro_processor/macro_processor.rs +++ b/src/macro_processor/macro_processor.rs @@ -412,6 +412,32 @@ fn smp_builtin_array_each( Ok(out) } +fn smp_builtin_array_size( + smp: &mut MacroProcessor, + macro_name: &str, + args: &mut [String], +) -> Result { + let Some(macro_body) = smp.macros.get(&args[0]) else { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("{:?} is not a macro", args[0]), + )); + return Ok(String::new()); + }; + let MacroType::Array(array) = macro_body else { + smp.warnings + .push(MacroProcessorWarning::from_macro_invocation( + macro_name, + args, + format!("{:?} is not a macro of type array", args[0]), + )); + return Ok(String::new()); + }; + Ok(array.len().to_string()) +} + #[cfg(feature = "time")] fn smp_builtin_format_time( smp: &mut MacroProcessor, @@ -632,6 +658,10 @@ impl MacroProcessor { String::from("array_each"), MacroType::Function(smp_builtin_array_each), ); + self.define_macro( + String::from("array_size"), + MacroType::Function(smp_builtin_array_size), + ); #[cfg(feature = "time")] self.define_macro( String::from("format_time"), -- cgit v1.2.3