1 /* Function management */
10 #include "func_types.h"
13 #include "splaytree_types.h"
14 #include "splaytree.h"
15 #include "tree_types.h"
17 #include "builtin_funcs.h"
19 /* A splay tree of builtin functions */
20 splaytree_t * builtin_func_tree;
22 /* Private function prototypes */
23 int compare_func(char * name, char * name2);
24 int insert_func(func_t * name);
25 void * copy_func_key(char * string);
28 void * copy_func_key(char * string) {
32 if ((clone_string = malloc(MAX_TOKEN_SIZE)) == NULL)
35 strncpy(clone_string, string, MAX_TOKEN_SIZE-1);
37 return (void*)clone_string;
41 func_t * create_func (char * name, double (*func_ptr)(), int num_args) {
44 func = (func_t*)malloc(sizeof(func_t));
50 /* Clear name space */
51 memset(func->name, 0, MAX_TOKEN_SIZE);
53 /* Copy given name into function structure */
54 strncpy(func->name, name, MAX_TOKEN_SIZE);
56 /* Assign value pointer */
57 func->func_ptr = func_ptr;
58 func->num_args = num_args;
59 /* Return instantiated function */
64 /* Initialize the builtin function database.
65 Should only be necessary once */
66 int init_builtin_func_db() {
69 builtin_func_tree = create_splaytree(compare_string, copy_string, free_string);
71 if (builtin_func_tree == NULL)
72 return OUTOFMEM_ERROR;
74 retval = load_all_builtin_func();
79 /* Destroy the builtin function database.
80 Generally, do this on projectm exit */
81 int destroy_builtin_func_db() {
83 splay_traverse(free_func, builtin_func_tree);
84 destroy_splaytree(builtin_func_tree);
89 /* Insert a function into the database */
90 int insert_func(func_t * func) {
95 splay_insert(func, func->name, builtin_func_tree);
100 /* Frees a function type, real complicated... */
101 void free_func(func_t * func) {
105 /* Remove a function from the database */
106 int remove_func(func_t * func) {
111 splay_delete(func->name, builtin_func_tree);
116 /* Find a function given its name */
117 func_t * find_func(char * name) {
119 func_t * func = NULL;
121 /* First look in the builtin database */
122 func = (func_t *)splay_find(name, builtin_func_tree);
128 /* Compare string name with function name */
129 int compare_func(char * name, char * name2) {
133 /* Uses string comparison function */
134 cmpval = strncmp(name, name2, MAX_TOKEN_SIZE-1);
139 /* Loads a builtin function */
140 int load_builtin_func(char * name, double (*func_ptr)(), int num_args) {
145 /* Create new function */
146 func = create_func(name, func_ptr, num_args);
149 return OUTOFMEM_ERROR;
151 retval = insert_func(func);
157 /* Loads all builtin functions */
158 int load_all_builtin_func() {
161 if (load_builtin_func("int", int_wrapper, 1) < 0)
163 if (load_builtin_func("abs", abs_wrapper, 1) < 0)
165 if (load_builtin_func("sin", sin_wrapper, 1) < 0)
167 if (load_builtin_func("cos", cos_wrapper, 1) < 0)
169 if (load_builtin_func("tan", tan_wrapper, 1) < 0)
171 if (load_builtin_func("asin", asin_wrapper, 1) < 0)
173 if (load_builtin_func("acos", acos_wrapper, 1) < 0)
175 if (load_builtin_func("atan", atan_wrapper, 1) < 0)
177 if (load_builtin_func("sqr", sqr_wrapper, 1) < 0)
179 if (load_builtin_func("sqrt", sqrt_wrapper, 1) < 0)
181 if (load_builtin_func("pow", pow_wrapper, 2) < 0)
183 if (load_builtin_func("exp", exp_wrapper, 1) < 0)
185 if (load_builtin_func("log", log_wrapper, 1) < 0)
187 if (load_builtin_func("log10", log10_wrapper, 1) < 0)
189 if (load_builtin_func("sign", sign_wrapper, 1) < 0)
191 if (load_builtin_func("min", min_wrapper, 2) < 0)
193 if (load_builtin_func("max", max_wrapper, 2) < 0)
195 if (load_builtin_func("sigmoid", sigmoid_wrapper, 2) < 0)
197 if (load_builtin_func("atan2", atan2_wrapper, 2) < 0)
199 if (load_builtin_func("rand", rand_wrapper, 1) < 0)
201 if (load_builtin_func("band", band_wrapper, 2) < 0)
203 if (load_builtin_func("bor", bor_wrapper, 2) < 0)
205 if (load_builtin_func("bnot", bnot_wrapper, 1) < 0)
207 if (load_builtin_func("if", if_wrapper, 3) < 0)
209 if (load_builtin_func("equal", equal_wrapper, 2) < 0)
211 if (load_builtin_func("above", above_wrapper, 2) < 0)
213 if (load_builtin_func("below", below_wrapper, 2) < 0)
215 if (load_builtin_func("nchoosek", nchoosek_wrapper, 2) < 0)
217 if (load_builtin_func("fact", fact_wrapper, 1) < 0)