diff --git a/src/builtin/color.rs b/src/builtin/color.rs index b932d46..f56df50 100644 --- a/src/builtin/color.rs +++ b/src/builtin/color.rs @@ -2,27 +2,7 @@ use std::collections::BTreeMap; use super::Builtin; use crate::value::Value; - -macro_rules! arg { - ($args:ident, $idx:literal, $name:literal) => { - match $args.get(stringify!($idx)) { - Some(v) => v, - None => match $args.get($name) { - Some(v) => v, - None => panic!("missing variable"), - }, - }; - }; - ($args:ident, $idx:literal, $name:literal, $default:literal) => { - match $args.get(stringify!($idx)) { - Some(v) => v, - None => match $args.get($name) { - Some(v) => v, - None => $default, - }, - }; - }; -} +use crate::color::Color; pub(crate) fn register(f: &mut BTreeMap) { f.insert( @@ -33,7 +13,7 @@ pub(crate) fn register(f: &mut BTreeMap) { let _red: &Value = arg!(args, 0, "red"); let _green: &Value = arg!(args, 1, "green"); let _blue: &Value = arg!(args, 2, "blue"); - // Value::Color() + // Value::Color(Color::RGB(red, blue, green)) } else { todo!("channels variable in `rgb`") }; diff --git a/src/builtin/meta.rs b/src/builtin/meta.rs index 8b13789..6717275 100644 --- a/src/builtin/meta.rs +++ b/src/builtin/meta.rs @@ -1 +1,20 @@ +use std::collections::BTreeMap; +use super::Builtin; +use crate::value::Value; + +pub(crate) fn register(f: &mut BTreeMap) { + f.insert( + "if".to_owned(), + Box::new(|args| { + let cond: &Value = arg!(args, 0, "condition"); + let if_true = arg!(args, 1, "if-true").clone(); + let if_false = arg!(args, 2, "if-false").clone(); + if cond.is_true() { + Some(if_true) + } else { + Some(if_false) + } + }), + ); +} \ No newline at end of file diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 33fa519..2f72ab3 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -4,6 +4,28 @@ use std::collections::BTreeMap; use crate::args::CallArgs; use crate::value::Value; +macro_rules! arg { + ($args:ident, $idx:literal, $name:literal) => { + match $args.get(stringify!($idx)) { + Some(v) => v, + None => match $args.get($name) { + Some(v) => v, + None => panic!("missing variable"), + }, + }; + }; + ($args:ident, $idx:literal, $name:literal, $default:literal) => { + match $args.get(stringify!($idx)) { + Some(v) => v, + None => match $args.get($name) { + Some(v) => v, + None => $default, + }, + }; + }; +} + + mod color; mod list; mod map; @@ -18,6 +40,7 @@ lazy_static! { pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap = { let mut m = BTreeMap::new(); color::register(&mut m); + meta::register(&mut m); m }; } diff --git a/src/value.rs b/src/value.rs index 56e2e68..8763b65 100644 --- a/src/value.rs +++ b/src/value.rs @@ -217,7 +217,7 @@ impl Value { } pub fn is_true(&self) -> bool { - todo!() + !(self == &Value::Null || self == &Value::False) } pub fn unquote(self) -> Self {