diff options
-rw-r--r-- | src/makefile/functions.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/src/makefile/functions.rs b/src/makefile/functions.rs index a4d206f..bc244cb 100644 --- a/src/makefile/functions.rs +++ b/src/makefile/functions.rs @@ -36,6 +36,11 @@ pub fn expand_call(name: &str, args: &[TokenString], macros: &MacroSet) -> Resul file_name::wildcard(macros, &args[0]) } + "if" => { + assert!(args.len() == 2 || args.len() == 3); + conditional::r#if(macros, &args[0], &args[1], args.get(2)) + } + // foreach "foreach" => { assert_eq!(args.len(), 3); @@ -183,6 +188,32 @@ mod file_name { } } +// Functions for Conditionals +mod conditional { + use super::*; + + pub fn r#if( + macros: &MacroSet, + condition: &TokenString, + if_true: &TokenString, + if_false: Option<&TokenString>, + ) -> Result<String> { + let mut condition = condition.clone(); + condition.trim_start(); + condition.trim_end(); + let condition = macros.expand(&condition)?; + if condition.is_empty() { + if let Some(if_false) = if_false { + macros.expand(if_false) + } else { + Ok(String::new()) + } + } else { + macros.expand(if_true) + } + } +} + // foreach mod foreach { use super::*; |