Implement basic builtin functions

This commit is contained in:
ConnorSkees 2020-01-25 23:33:45 -05:00
parent 16d7dec4cc
commit 4cbbff259c
3 changed files with 20 additions and 4 deletions

11
src/builtin/color.rs Normal file
View File

@ -0,0 +1,11 @@
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!()
}));
}

View File

@ -1,15 +1,20 @@
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;
pub(crate) type Builtin = dyn Fn(&Scope) -> Value + Send + Sync;
mod color;
pub(crate) type Builtin = Box<dyn Fn(&CallArgs) -> Value + Send + Sync>;
lazy_static! {
pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap<String, Function> = {
let m = BTreeMap::new();
pub(crate) static ref GLOBAL_FUNCTIONS: BTreeMap<String, Builtin> = {
let mut m = BTreeMap::new();
color::register(&mut m);
m
};
}

View File

@ -279,7 +279,7 @@ impl Value {
let func = match scope.functions.get(&s) {
Some(f) => f,
None => match GLOBAL_FUNCTIONS.get(&s) {
Some(f) => f,
Some(f) => return Some(f(&args)),
None => todo!("called undefined function"),
},
};