-
Notifications
You must be signed in to change notification settings - Fork 0
/
func.c
445 lines (341 loc) · 7.84 KB
/
func.c
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "tl2/list.h"
#include "token.h"
#include "scope.h"
#include "func.h"
#include "var.h"
#include "lib.h"
#define FUNC_SCOPE_POSTFIX "-local-scope"
static list *func_list;
extern list *var_scope, *func_scope;
extern int current_scope_tag;
extern char *current_scope;
extern int marked_index;
extern char *ret_val;
void fl_func_list_init() {
func_list = list_init_node(NULL);
}
void fl_add_func(list *proto, list *body, int tag) {
list *last;
func *fptr;
token *proto_tk, *body_tk;
int proto_index, body_index;
// create new node
list_add_node(func_list);
last = list_get_last(func_list);
// create function
fptr = (func*)malloc(sizeof(func));
if (!fptr) {
perror("forp");
exit(0);
}
// init fields
fptr->tag = tag;
fptr->proto = list_init_node(NULL);
fptr->body = list_init_node(NULL);
// extract tokens
proto_tk = (token*)proto->data;
body_tk = (token*)body->data;
if (!proto_tk || !body_tk)
return ;
// get index
proto_index = proto_tk->index;
body_index = body_tk->index;
// copy proto tree to function
fl_copy_tree_by_index(fptr->proto, proto, proto_index);
// copy body tree to function
fl_copy_tree_by_index(fptr->body, body, body_index);
// add func to list
list_add_node(func_list);
last = list_get_last(func_list);
list_set_data(last, fptr);
}
size_t __fl_get_tok_size(token *tk) {
size_t tok_size = 0;
tok_size = 2 * sizeof(int);
if (tk->val)
tok_size += strlen(tk->val);
if (tk->ret)
tok_size += strlen(tk->ret);
return tok_size;
}
void __fl_copy_tk(token *dest_tk, token *src_tk) {
// copy integer fields
dest_tk->index = src_tk->index;
dest_tk->eval_me = src_tk->eval_me;
dest_tk->val = NULL;
dest_tk->ret = NULL;
// copy text
if (src_tk->val) {
dest_tk->val = (char*)malloc(strlen(src_tk->val));
strcpy(dest_tk->val, src_tk->val);
}
if (src_tk->ret) {
dest_tk->ret = (char*)malloc(strlen(src_tk->ret));
strcpy(dest_tk->ret, src_tk->ret);
}
}
void __fl_create_util(list *node, list *dest) {
list *last;
token *tk, *last_tk;
size_t tok_size;
tk = (token*)node->data;
if (!tk)
return ;
list_add_node(dest);
last = list_get_last(dest);
// get token size
tok_size = __fl_get_tok_size(tk);
// allocate memory
last->data = malloc(tok_size);
last_tk = (token*)last->data;
// copy
__fl_copy_tk(last_tk, tk);
}
void fl_copy_tree_by_index(list *dest, list *src, int indx) {
list *node = NULL;
token *tk = NULL;
// copy first node
__fl_create_util(src, dest);
// main loop
node = src->next;
while (node) {
tk = (token*)node->data;
if (!tk) {
node = node->next;
continue;
}
if (tk->index <= indx)
break;
// add new node
__fl_create_util(node, dest);
node = node->next;
}
}
void fl_func_list_free() {
list *node = NULL, *next = NULL;
func *fptr = NULL;
node = func_list;
while (node) {
next = node->next;
if (!node->data) {
node = node->next;
continue;
}
// free func
fptr = (func*)node->data;
if (fptr) {
if (fptr->proto)
list_free_list(fptr->proto);
if (fptr->body)
list_free_list(fptr->body);
free(fptr);
}
// free node
free(node);
node = next;
}
}
void fl_func_show_struct(list *node) {
token *tk;
tk = (token*)node->data;
if (!tk)
return ;
printf("val: %s\n", tk->val);
printf("ret: %s\n", tk->ret);
printf("index: %d\n", tk->index);
}
void fl_func_show(list *lptr) {
func *fptr;
// extract func
fptr = (func*)lptr->data;
if (!fptr)
return ;
// show fields
if (fptr->proto) {
puts("proto\n===");
tl_crawl_list(fptr->proto, fl_func_show_struct);
puts("===\n");
}
if (fptr->body) {
puts("body\n===");
tl_crawl_list(fptr->body, fl_func_show_struct);
puts("===\n");
}
printf("tag = %d\n", fptr->tag);
}
void fl_func_show_all() {
tl_crawl_list(func_list, fl_func_show);
}
char *fl_func_get_name(func *fptr) {
list *lptr;
token *tk;
lptr = fptr->proto->next;
tk = (token*)lptr->data;
return (char*)tk->val;
}
func *fl_func_get(list *target_scope, char *name, int tag) {
list *node;
func *fptr;
char *func_name;
// main loop
node = func_list;
while (node) {
if (!node->data) {
node = node->next;
continue;
}
fptr = (func*)node->data;
if (fptr->tag == tag) {
func_name = fl_func_get_name(fptr);
if (!strcmp(func_name, name))
return fptr;
}
node = node->next;
}
return NULL;
}
func *fl_func_get_with_syntax(list *target_scope, char *name) {
char *scope_name, *func_name;
char *cpy;
int tag;
cpy = (char*)malloc(strlen(name) + 1);
if (!cpy) {
perror("forp");
exit(0);
}
strcpy(cpy, name);
// if has syntax
if (strchr(cpy, ':')) {
scope_name = strtok(cpy, ":");
func_name = strtok(NULL, ":");
tag = sl_scope_get_tag(var_scope, scope_name);
}
else {
func_name = cpy;
tag = current_scope_tag;
}
// return func
return fl_func_get(target_scope, func_name, tag);
}
void __fl_func_set_parameters(func *fptr, list *node) {
list *arg_node, *param_node;
token *arg_tk, *param_tk, *tk;
char *parameter_scope, *parameter_scope_cpy;
int tag = 0, len = 0, col = 0, chk_mode = 0;
// get scope name
len = strlen(FUNC_SCOPE_POSTFIX);
parameter_scope = fl_func_get_name(fptr);
parameter_scope_cpy = (char*)malloc(strlen(parameter_scope) + len + 1);
if (!parameter_scope_cpy) {
perror("forp");
exit(0);
}
strcpy(parameter_scope_cpy, parameter_scope);
strcat(parameter_scope_cpy, FUNC_SCOPE_POSTFIX);
// get scope tag
chk_mode = CHK_STD;
if (!strcmp(parameter_scope_cpy, current_scope))
chk_mode = CHK_REC;
tag = sl_scope_check(var_scope, parameter_scope_cpy, chk_mode);
// get col
tk = (token*)node->data;
if (!tk)
return ;
col = tk->index + 1;
// main loop
arg_node = node->next;
param_node = fptr->proto->next->next;
while (arg_node && param_node) {
arg_tk = (token*)arg_node->data;
param_tk = (token*)param_node->data;
//printf("[%d] %s = %s\n", tag, param_tk->val, arg_tk->ret);
if (arg_tk->index == col) {
vl_var_add(param_tk->val, arg_tk->ret, tag);
param_node = param_node->next;
}
arg_node = arg_node->next;
}
// call new scope & switch current_scope
sl_scope_call(var_scope, tag);
}
void __fl_func_call(func *fptr) {
marked_index = -1;
tl_crawl_list(fptr->body, ll_process_spec_operators);
tl_crawl_list_level(fptr->body, 1, ll_exec);
}
void __fl_func_remove_params(func *fptr, int tag) {
list *node = NULL;
token *tk = NULL;
node = fptr->proto->next->next;
while (node) {
tk = (token*)node->data;
if (!tk) {
node = node->next;
continue;
}
vl_var_remove(tk->val, tag);
node = node->next;
}
}
void __fl_func_unset_parameters(func *fptr) {
int tag = 0;
// get tag
tag = current_scope_tag;
// remove params
__fl_func_remove_params(fptr, tag);
// revoke scope
sl_scope_revoke(var_scope, tag);
// remove scope
sl_scope_remove(var_scope, tag);
}
void __fl_func_set_return(func *fptr, list *node) {
list *head = NULL;
token *head_tk;
char *ret = NULL;
// get head
head = node;
head_tk = (token*)head->data;
if (!head || !head_tk) {
fprintf(stderr, "forp: can\'t get function head\n");
exit(0);
}
// get ret
if (ret_val)
ret = ret_val;
else
ret = fl_func_get_return_value(fptr);
ret_val = NULL;
if (!ret)
ret = "x";
// set ret
if (!head_tk->ret)
head_tk->ret = (char*)malloc(strlen(ret) + 1);
else
head_tk->ret = (char*)realloc(head_tk->ret, strlen(ret) + 1);
// copy
strcpy(head_tk->ret, ret);
}
void fl_func_call(func *fptr, list *node) {
// create variables for function
__fl_func_set_parameters(fptr, node);
// call function
__fl_func_call(fptr);
// set return
__fl_func_set_return(fptr, node);
// unset variables
__fl_func_unset_parameters(fptr);
}
char *fl_func_get_return_value(func *fptr) {
list *node = NULL;
token *tk = NULL;
node = fptr->body->next;
tk = (token*)node->data;
if (!tk) {
fprintf(stderr, "forp: internal interpreter error\n");
exit(0);
}
return tk->ret;
}