Work on builtin functions

This commit is contained in:
ConnorSkees 2020-01-26 15:28:39 -05:00
parent 52c9905b14
commit 8b26fabb62
2 changed files with 38 additions and 9 deletions

View File

@ -3,9 +3,41 @@ use std::collections::BTreeMap;
use super::Builtin;
use crate::value::Value;
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
f.insert("rgb".to_owned(), Box::new(|args| {
let red = args.get("red");
todo!()
}));
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,
},
};
};
}
pub(crate) fn register(f: &mut BTreeMap<String, Builtin>) {
f.insert(
"rgb".to_owned(),
Box::new(|args| {
let channels = args.get("channels").unwrap_or(&Value::Null);
if channels.is_null() {
let _red: &Value = arg!(args, 0, "red");
let _green: &Value = arg!(args, 1, "green");
let _blue: &Value = arg!(args, 2, "blue");
// Value::Color()
} else {
todo!("channels variable in `rgb`")
};
todo!()
}),
);
}

View File

@ -2,13 +2,11 @@ use lazy_static::lazy_static;
use std::collections::BTreeMap;
use crate::args::CallArgs;
use crate::common::Scope;
use crate::function::Function;
use crate::value::Value;
mod color;
pub(crate) type Builtin = Box<dyn Fn(&CallArgs) -> Value + Send + Sync>;
pub(crate) type Builtin = Box<dyn Fn(&CallArgs) -> Option<Value> + Send + Sync>;
lazy_static! {
pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap<String, Builtin> = {
@ -17,4 +15,3 @@ lazy_static! {
m
};
}