1 /* Function management */
8 #include "func_types.h"
11 #include "splaytree_types.h"
12 #include "splaytree.h"
13 #include "tree_types.h"
15 #include "builtin_funcs.h"
17 /* A splay tree of builtin functions */
18 splaytree_t * builtin_func_tree;
20 /* Private function prototypes */
21 int compare_func(char * name, char * name2);
22 int insert_func(func_t * name);
23 void * copy_func_key(char * string);
26 void * copy_func_key(char * string) {
30 if ((clone_string = malloc(MAX_TOKEN_SIZE)) == NULL)
33 strncpy(clone_string, string, MAX_TOKEN_SIZE-1);
35 return (void*)clone_string;
39 func_t * create_func (char * name, double (*func_ptr)(), int num_args) {
42 func = (func_t*)malloc(sizeof(func_t));
48 /* Clear name space */
49 memset(func->name, 0, MAX_TOKEN_SIZE);
51 /* Copy given name into function structure */
52 strncpy(func->name, name, MAX_TOKEN_SIZE);
54 /* Assign value pointer */
55 func->func_ptr = func_ptr;
56 func->num_args = num_args;
57 /* Return instantiated function */
62 /* Initialize the builtin function database.
63 Should only be necessary once */
64 int init_builtin_func_db() {
67 builtin_func_tree = create_splaytree(compare_string, copy_string, free_string);
69 if (builtin_func_tree == NULL)
70 return OUTOFMEM_ERROR;
72 retval = load_all_builtin_func();
77 /* Destroy the builtin function database.
78 Generally, do this on projectm exit */
79 int destroy_builtin_func_db() {
81 splay_traverse(free_func, builtin_func_tree);
82 destroy_splaytree(builtin_func_tree);
87 /* Insert a function into the database */
88 int insert_func(func_t * func) {
93 splay_insert(func, func->name, builtin_func_tree);
98 /* Frees a function type, real complicated... */
99 void free_func(func_t * func) {
103 /* Remove a function from the database */
104 int remove_func(func_t * func) {
109 splay_delete(func->name, builtin_func_tree);
114 /* Find a function given its name */
115 func_t * find_func(char * name) {
117 func_t * func = NULL;
119 /* First look in the builtin database */
120 func = (func_t *)splay_find(name, builtin_func_tree);
126 /* Compare string name with function name */
127 int compare_func(char * name, char * name2) {
131 /* Uses string comparison function */
132 cmpval = strncmp(name, name2, MAX_TOKEN_SIZE-1);
137 /* Loads a builtin function */
138 int load_builtin_func(char * name, double (*func_ptr)(), int num_args) {
143 /* Create new function */
144 func = create_func(name, func_ptr, num_args);
147 return OUTOFMEM_ERROR;
149 retval = insert_func(func);
155 /* Loads all builtin functions */
156 int load_all_builtin_func() {
159 if (load_builtin_func("int", int_wrapper, 1) < 0)
161 if (load_builtin_func("abs", abs_wrapper, 1) < 0)
163 if (load_builtin_func("sin", sin_wrapper, 1) < 0)
165 if (load_builtin_func("cos", cos_wrapper, 1) < 0)
167 if (load_builtin_func("tan", tan_wrapper, 1) < 0)
169 if (load_builtin_func("asin", asin_wrapper, 1) < 0)
171 if (load_builtin_func("acos", acos_wrapper, 1) < 0)
173 if (load_builtin_func("atan", atan_wrapper, 1) < 0)
175 if (load_builtin_func("sqr", sqr_wrapper, 1) < 0)
177 if (load_builtin_func("sqrt", sqrt_wrapper, 1) < 0)
179 if (load_builtin_func("pow", pow_wrapper, 2) < 0)
181 if (load_builtin_func("exp", exp_wrapper, 1) < 0)
183 if (load_builtin_func("log", log_wrapper, 1) < 0)
185 if (load_builtin_func("log10", log10_wrapper, 1) < 0)
187 if (load_builtin_func("sign", sign_wrapper, 1) < 0)
189 if (load_builtin_func("min", min_wrapper, 2) < 0)
191 if (load_builtin_func("max", max_wrapper, 2) < 0)
193 if (load_builtin_func("sigmoid", sigmoid_wrapper, 2) < 0)
195 if (load_builtin_func("atan2", atan2_wrapper, 2) < 0)
197 if (load_builtin_func("rand", rand_wrapper, 1) < 0)
199 if (load_builtin_func("band", band_wrapper, 2) < 0)
201 if (load_builtin_func("bor", bor_wrapper, 2) < 0)
203 if (load_builtin_func("bnot", bnot_wrapper, 1) < 0)
205 if (load_builtin_func("if", if_wrapper, 3) < 0)
207 if (load_builtin_func("equal", equal_wrapper, 2) < 0)
209 if (load_builtin_func("above", above_wrapper, 2) < 0)
211 if (load_builtin_func("below", below_wrapper, 2) < 0)
213 if (load_builtin_func("nchoosek", nchoosek_wrapper, 2) < 0)
215 if (load_builtin_func("fact", fact_wrapper, 1) < 0)