Add if() function

This commit is contained in:
ConnorSkees 2020-02-02 22:33:04 -05:00
parent 6faebf5105
commit efc62a2433
4 changed files with 45 additions and 23 deletions

View File

@ -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<String, Builtin>) {
f.insert(
@ -33,7 +13,7 @@ pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
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`")
};

View File

@ -1 +1,20 @@
use std::collections::BTreeMap;
use super::Builtin;
use crate::value::Value;
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
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)
}
}),
);
}

View File

@ -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<String, Builtin> = {
let mut m = BTreeMap::new();
color::register(&mut m);
meta::register(&mut m);
m
};
}

View File

@ -217,7 +217,7 @@ impl Value {
}
pub fn is_true(&self) -> bool {
todo!()
!(self == &Value::Null || self == &Value::False)
}
pub fn unquote(self) -> Self {