From 8b26fabb620d600203c897763b16aec005e2a974 Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Sun, 26 Jan 2020 15:28:39 -0500 Subject: [PATCH] Work on builtin functions --- src/builtin/color.rs | 42 +++++++++++++++++++++++++++++++++++++----- src/builtin/mod.rs | 5 +---- 2 files changed, 38 insertions(+), 9 deletions(-) diff --git a/src/builtin/color.rs b/src/builtin/color.rs index 8d317cb..b932d46 100644 --- a/src/builtin/color.rs +++ b/src/builtin/color.rs @@ -3,9 +3,41 @@ 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, + }, + }; + }; +} + pub(crate) fn register(f: &mut BTreeMap) { - f.insert("rgb".to_owned(), Box::new(|args| { - let red = args.get("red"); - todo!() - })); -} \ No newline at end of file + 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!() + }), + ); +} diff --git a/src/builtin/mod.rs b/src/builtin/mod.rs index 25f2eee..3f4eb52 100644 --- a/src/builtin/mod.rs +++ b/src/builtin/mod.rs @@ -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 Value + Send + Sync>; +pub(crate) type Builtin = Box Option + Send + Sync>; lazy_static! { pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap = { @@ -17,4 +15,3 @@ lazy_static! { m }; } -