1 /* Function management */
9 #include "func_types.h"
12 #include "splaytree_types.h"
13 #include "splaytree.h"
14 #include "tree_types.h"
16 #include "builtin_funcs.h"
18 /* A splay tree of builtin functions */
19 splaytree_t * builtin_func_tree;
21 /* Private function prototypes */
22 int compare_func(char * name, char * name2);
23 int insert_func(func_t * name);
24 void * copy_func_key(char * string);
27 void * copy_func_key(char * string) {
31 if ((clone_string = malloc(MAX_TOKEN_SIZE)) == NULL)
34 strncpy(clone_string, string, MAX_TOKEN_SIZE-1);
36 return (void*)clone_string;
40 func_t * create_func (char * name, double (*func_ptr)(), int num_args) {
43 func = (func_t*)malloc(sizeof(func_t));
49 /* Clear name space */
50 memset(func->name, 0, MAX_TOKEN_SIZE);
52 /* Copy given name into function structure */
53 strncpy(func->name, name, MAX_TOKEN_SIZE);
55 /* Assign value pointer */
56 func->func_ptr = func_ptr;
57 func->num_args = num_args;
58 /* Return instantiated function */
63 /* Initialize the builtin function database.
64 Should only be necessary once */
65 int init_builtin_func_db() {
68 builtin_func_tree = create_splaytree(compare_string, copy_string, free_string);
70 if (builtin_func_tree == NULL)
71 return OUTOFMEM_ERROR;
73 retval = load_all_builtin_func();
78 /* Destroy the builtin function database.
79 Generally, do this on projectm exit */
80 int destroy_builtin_func_db() {
82 splay_traverse(free_func, builtin_func_tree);
83 destroy_splaytree(builtin_func_tree);
88 /* Insert a function into the database */
89 int insert_func(func_t * func) {
94 splay_insert(func, func->name, builtin_func_tree);
99 /* Frees a function type, real complicated... */
100 void free_func(func_t * func) {
104 /* Remove a function from the database */
105 int remove_func(func_t * func) {
110 splay_delete(func->name, builtin_func_tree);
115 /* Find a function given its name */
116 func_t * find_func(char * name) {
118 func_t * func = NULL;
120 /* First look in the builtin database */
121 func = (func_t *)splay_find(name, builtin_func_tree);
127 /* Compare string name with function name */
128 int compare_func(char * name, char * name2) {
132 /* Uses string comparison function */
133 cmpval = strncmp(name, name2, MAX_TOKEN_SIZE-1);
138 /* Loads a builtin function */
139 int load_builtin_func(char * name, double (*func_ptr)(), int num_args) {
144 /* Create new function */
145 func = create_func(name, func_ptr, num_args);
148 return OUTOFMEM_ERROR;
150 retval = insert_func(func);
156 /* Loads all builtin functions */
157 int load_all_builtin_func() {
160 if (load_builtin_func("int", int_wrapper, 1) < 0)
162 if (load_builtin_func("abs", abs_wrapper, 1) < 0)
164 if (load_builtin_func("sin", sin_wrapper, 1) < 0)
166 if (load_builtin_func("cos", cos_wrapper, 1) < 0)
168 if (load_builtin_func("tan", tan_wrapper, 1) < 0)
170 if (load_builtin_func("asin", asin_wrapper, 1) < 0)
172 if (load_builtin_func("acos", acos_wrapper, 1) < 0)
174 if (load_builtin_func("atan", atan_wrapper, 1) < 0)
176 if (load_builtin_func("sqr", sqr_wrapper, 1) < 0)
178 if (load_builtin_func("sqrt", sqrt_wrapper, 1) < 0)
180 if (load_builtin_func("pow", pow_wrapper, 2) < 0)
182 if (load_builtin_func("exp", exp_wrapper, 1) < 0)
184 if (load_builtin_func("log", log_wrapper, 1) < 0)
186 if (load_builtin_func("log10", log10_wrapper, 1) < 0)
188 if (load_builtin_func("sign", sign_wrapper, 1) < 0)
190 if (load_builtin_func("min", min_wrapper, 2) < 0)
192 if (load_builtin_func("max", max_wrapper, 2) < 0)
194 if (load_builtin_func("sigmoid", sigmoid_wrapper, 2) < 0)
196 if (load_builtin_func("atan2", atan2_wrapper, 2) < 0)
198 if (load_builtin_func("rand", rand_wrapper, 1) < 0)
200 if (load_builtin_func("band", band_wrapper, 2) < 0)
202 if (load_builtin_func("bor", bor_wrapper, 2) < 0)
204 if (load_builtin_func("bnot", bnot_wrapper, 1) < 0)
206 if (load_builtin_func("if", if_wrapper, 3) < 0)
208 if (load_builtin_func("equal", equal_wrapper, 2) < 0)
210 if (load_builtin_func("above", above_wrapper, 2) < 0)
212 if (load_builtin_func("below", below_wrapper, 2) < 0)
214 if (load_builtin_func("nchoosek", nchoosek_wrapper, 2) < 0)
216 if (load_builtin_func("fact", fact_wrapper, 1) < 0)