1 /* Function management */
7 #include "func_types.h"
10 #include "splaytree_types.h"
11 #include "splaytree.h"
12 #include "tree_types.h"
14 #include "builtin_funcs.h"
16 /* A splay tree of builtin functions */
17 splaytree_t * builtin_func_tree;
19 /* Private function prototypes */
20 int compare_func(char * name, char * name2);
21 int insert_func(func_t * name);
22 void * copy_func_key(char * string);
25 void * copy_func_key(char * string) {
29 if ((clone_string = malloc(MAX_TOKEN_SIZE)) == NULL)
32 strncpy(clone_string, string, MAX_TOKEN_SIZE-1);
34 return (void*)clone_string;
38 func_t * create_func (char * name, double (*func_ptr)(), int num_args) {
41 func = (func_t*)malloc(sizeof(func_t));
47 /* Clear name space */
48 memset(func->name, 0, MAX_TOKEN_SIZE);
50 /* Copy given name into function structure */
51 strncpy(func->name, name, MAX_TOKEN_SIZE);
53 /* Assign value pointer */
54 func->func_ptr = func_ptr;
55 func->num_args = num_args;
56 /* Return instantiated function */
61 /* Initialize the builtin function database.
62 Should only be necessary once */
63 int init_builtin_func_db() {
66 builtin_func_tree = create_splaytree(compare_string, copy_string, free_string);
68 if (builtin_func_tree == NULL)
69 return OUTOFMEM_ERROR;
71 retval = load_all_builtin_func();
76 /* Destroy the builtin function database.
77 Generally, do this on projectm exit */
78 int destroy_builtin_func_db() {
80 splay_traverse(free_func, builtin_func_tree);
81 destroy_splaytree(builtin_func_tree);
86 /* Insert a function into the database */
87 int insert_func(func_t * func) {
92 splay_insert(func, func->name, builtin_func_tree);
97 /* Frees a function type, real complicated... */
98 void free_func(func_t * func) {
102 /* Remove a function from the database */
103 int remove_func(func_t * func) {
108 splay_delete(func->name, builtin_func_tree);
113 /* Find a function given its name */
114 func_t * find_func(char * name) {
116 func_t * func = NULL;
118 /* First look in the builtin database */
119 func = (func_t *)splay_find(name, builtin_func_tree);
125 /* Compare string name with function name */
126 int compare_func(char * name, char * name2) {
130 /* Uses string comparison function */
131 cmpval = strncmp(name, name2, MAX_TOKEN_SIZE-1);
136 /* Loads a builtin function */
137 int load_builtin_func(char * name, double (*func_ptr)(), int num_args) {
142 /* Create new function */
143 func = create_func(name, func_ptr, num_args);
146 return OUTOFMEM_ERROR;
148 retval = insert_func(func);
154 /* Loads all builtin functions */
155 int load_all_builtin_func() {
158 if (load_builtin_func("int", int_wrapper, 1) < 0)
160 if (load_builtin_func("abs", abs_wrapper, 1) < 0)
162 if (load_builtin_func("sin", sin_wrapper, 1) < 0)
164 if (load_builtin_func("cos", cos_wrapper, 1) < 0)
166 if (load_builtin_func("tan", tan_wrapper, 1) < 0)
168 if (load_builtin_func("asin", asin_wrapper, 1) < 0)
170 if (load_builtin_func("acos", acos_wrapper, 1) < 0)
172 if (load_builtin_func("atan", atan_wrapper, 1) < 0)
174 if (load_builtin_func("sqr", sqr_wrapper, 1) < 0)
176 if (load_builtin_func("sqrt", sqrt_wrapper, 1) < 0)
178 if (load_builtin_func("pow", pow_wrapper, 2) < 0)
180 if (load_builtin_func("exp", exp_wrapper, 1) < 0)
182 if (load_builtin_func("log", log_wrapper, 1) < 0)
184 if (load_builtin_func("log10", log10_wrapper, 1) < 0)
186 if (load_builtin_func("sign", sign_wrapper, 1) < 0)
188 if (load_builtin_func("min", min_wrapper, 2) < 0)
190 if (load_builtin_func("max", max_wrapper, 2) < 0)
192 if (load_builtin_func("sigmoid", sigmoid_wrapper, 2) < 0)
194 if (load_builtin_func("atan2", atan2_wrapper, 2) < 0)
196 if (load_builtin_func("rand", rand_wrapper, 1) < 0)
198 if (load_builtin_func("band", band_wrapper, 2) < 0)
200 if (load_builtin_func("bor", bor_wrapper, 2) < 0)
202 if (load_builtin_func("bnot", bnot_wrapper, 1) < 0)
204 if (load_builtin_func("if", if_wrapper, 3) < 0)
206 if (load_builtin_func("equal", equal_wrapper, 2) < 0)
208 if (load_builtin_func("above", above_wrapper, 2) < 0)
210 if (load_builtin_func("below", below_wrapper, 2) < 0)
212 if (load_builtin_func("nchoosek", nchoosek_wrapper, 2) < 0)
214 if (load_builtin_func("fact", fact_wrapper, 1) < 0)