summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--smp.114
-rw-r--r--src/macro_processor/macro_processor.rs30
2 files changed, 44 insertions, 0 deletions
diff --git a/smp.1 b/smp.1
index 35b4c3d..888cced 100644
--- a/smp.1
+++ b/smp.1
@@ -36,6 +36,20 @@ Runs command on shell, and includes the command output in the output
.IP "\fBexpr(<arg1>, <arg2>, ..., <argN>)\fR"
Shorthand for running the expr command, expands all arguments, and executes it on the shell.
+.IP "\fBdefine_array(<macro_name>)\fR"
+Defines a macro as a array, this can later be used with macros like \fBarray_push\fR and \fBarray_each\fR.
+
+.IP "\fBarray_push(<macro_name>, <value>[, <additional value(s)>])\fR"
+On a macro that is defined as a array, this will add one or more elements.
+
+.IP "\fBarray_each(<macro_name>, <template macro>])\fR"
+Push any arguments to array macro
+Process each element in a array as a macro-invokation on the second argument
+Not the best way to do this, it is not sensibly recursive.
+
+.IP "\fBarray_size(<macro_name>)\fR"
+Will return number of elements in a macro array.
+
.IP "\fBformat_time(<format>, <time>)\fR"
Format a RFC3339-timestamp to the specified format. Format is similar to strftime.
Only available if the \fBtime\fR-feature was enabled during compilation.
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<String, SMPError> {
+ 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"),