-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunction.c
More file actions
231 lines (174 loc) · 3.79 KB
/
function.c
File metadata and controls
231 lines (174 loc) · 3.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "function.h"
#include "macros.h"
#include "common.h"
#include "hash.h"
#include "umalloc.h"
#include "libcall.h"
#include "misc.h"
#define err_msg_ret(ret, fmt, arg...) \
do { \
messsge(fmt, ##arg); \
return(ret); \
} while(0)
#define err_msg(fmt, arg...) \
do { \
message(fmt, ##arg); \
} while(0)
static struct hash_table *function_table;
static void function_init_lib(void);
/* names for functions arguments */
static char *names[] = {
"x1",
"x2",
"x3",
"x4",
"x5",
"x6",
"x7",
"x8",
"x9",
"x10",
NULL
};
static struct {
char *name;
unsigned int nargs;
lib_handler_type_t handler;
} functions[] = {
{ "sin", 1, libcall_sin },
{ "cos", 1, libcall_cos },
{ "exp", 1, libcall_exp },
{ "ln", 1, libcall_ln },
{ "tan", 1, libcall_tan },
{ "matrix", 2, libcall_matrix },
{ "vector", 1, libcall_vector },
{ "sqrt", 1, libcall_sqrt },
{ NULL, -1, NULL }
};
static int
function_hash(unsigned char *name)
{
unsigned long hash = 5381;
int c;
while ((c = *name++) != 0)
hash = ((hash << 5) + hash) + c;
return hash;
}
static int
function_strcmp(const void *a, const void *b)
{
int ret;
ret = strcmp((char *)a, (char *)b);
return ret;
}
void
function_table_create(void)
{
function_table = hash_table_new(0,(hash_callback_t)function_hash,
(hash_compare_t)function_strcmp);
if (function_table == NULL)
error(1, "can't create function table");
function_init_lib();
}
struct function*
function_table_lookup(char *name)
{
struct function *func = NULL;
ret_t ret;
return_val_if_fail(name != NULL, NULL);
ret = hash_table_lookup(function_table, name, (void **)&func);
return func;
}
int
function_table_insert(struct function *function)
{
ret_t ret;
return_val_if_fail(function_table != NULL, -1);
return_val_if_fail(function != NULL, -1);
ret = hash_table_insert_unique(function_table, function->name, function);
if (ret != ret_ok)
error(1, "insert in function table fail");
return ret;
}
struct function*
function_new(char *name)
{
struct function *func;
return_val_if_fail(name != NULL, NULL);
func = umalloc0(sizeof(*func));
func->name = ustrdup(name);
func->is_lib = FALSE;
return func;
}
void
function_destroy(struct function *func)
{
return_if_fail(func != NULL);
ufree(func->name);
if (func->args)
ufree(func->args);
if (func->scope)
symbol_table_destroy(&func->scope);
if (func->body)
ast_node_unref(func->body);
ufree(func);
}
void
function_add_arg(struct function *function, struct symbol *arg)
{
int i;
return_if_fail(function != NULL);
i = function->nargs++;
function->args = urealloc(function->args,
function->nargs * sizeof(*function->args));
function->args[i] = arg;
}
void
function_table_delete_function(char *name)
{
struct function *func;
ret_t ok;
return_if_fail(name != NULL);
func = function_table_lookup(name);
if (!func) {
err_msg("not entry for this function");
return;
}
ok = hash_table_remove(function_table, func->name);
if (!ok) {
err_msg("no entry for this function");
return;
}
function_destroy(func);
}
static void
function_init_args(struct function *lib, int nargs)
{
struct symbol *symbol;
int i;
return_if_fail(lib != NULL);
for (i = 0; i < nargs; i++) {
symbol = symbol_new(names[i], VALUE_TYPE_UNKNOWN);
function_add_arg(lib, symbol);
}
}
static void
function_init_lib(void)
{
struct function *lib;
ret_t ret;
int i;
for (i = 0; functions[i].name != NULL; i++) {
lib = umalloc0(sizeof(*lib));
lib->is_lib = TRUE;
lib->name = ustrdup(functions[i].name);
lib->handler = functions[i].handler;
function_init_args(lib, functions[i].nargs);
ret = hash_table_insert_unique(function_table, lib->name, lib);
if (ret != ret_ok)
error(1, "init lib failed");
}
}