From 77923a4d12717902137f8cd29a512796eff1a3af Mon Sep 17 00:00:00 2001 From: Melody Horn Date: Fri, 2 Apr 2021 19:28:31 -0600 Subject: implement `if` --- src/makefile/functions.rs | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) 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 { + 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::*; -- cgit v1.2.3