Add if() function
This commit is contained in:
parent
6faebf5105
commit
efc62a2433
@ -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`")
|
||||
};
|
||||
|
@ -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)
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
@ -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
|
||||
};
|
||||
}
|
||||
|
@ -217,7 +217,7 @@ impl Value {
|
||||
}
|
||||
|
||||
pub fn is_true(&self) -> bool {
|
||||
todo!()
|
||||
!(self == &Value::Null || self == &Value::False)
|
||||
}
|
||||
|
||||
pub fn unquote(self) -> Self {
|
||||
|
Loading…
x
Reference in New Issue
Block a user