-
Notifications
You must be signed in to change notification settings - Fork 0
/
as_tree.c
755 lines (535 loc) · 15 KB
/
as_tree.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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "as_tree.h"
#include "macros.h"
#include "umalloc.h"
static struct ast_node*
ast_node_new(size_t size)
{
struct ast_node *node;
node = umalloc0(size);
return node;
}
static void
ast_node_stub_free(struct ast_node *node)
{
return_if_fail(node != NULL);
ufree(node);
}
struct ast_node_stub*
ast_node_stub(void)
{
struct ast_node_stub *node;
node = (struct ast_node_stub *)ast_node_new(sizeof(*node));
AST_NODE(node)->destructor = ast_node_stub_free;
AST_NODE(node)->type = NODE_TYPE_STUB;
return node;
}
void
ast_node_unref(struct ast_node *node)
{
return_if_fail(node != NULL);
if (node->destructor != NULL)
node->destructor(node);
}
static void
ast_node_const_free(struct ast_node *node)
{
struct ast_node_const *_const;
return_if_fail(node != NULL);
_const = (struct ast_node_const *)node;
if (_const->v_type == VALUE_TYPE_STRING)
ufree(_const->string);
ufree(_const);
}
struct ast_node_const*
ast_node_const(value_t v_type, void *data)
{
struct ast_node_const *_const;
_const = (struct ast_node_const *)ast_node_new(sizeof(*_const));
switch(v_type) {
case VALUE_TYPE_DIGIT:
_const->digit = *(double *)data;
break;
case VALUE_TYPE_STRING:
_const->string = ustrdup((char *)data);
break;
default:
error(1, "incompatible value type");
}
_const->v_type = v_type;
AST_NODE(_const)->type = NODE_TYPE_CONST;
AST_NODE(_const)->destructor = ast_node_const_free;
return _const;
}
static void
ast_node_id_free(struct ast_node *node)
{
struct ast_node_id *id;
return_if_fail(node != NULL);
id = (struct ast_node_id *)node;
ufree(id->name);
ufree(id);
}
struct ast_node_id*
ast_node_id(char *name)
{
struct ast_node_id *node;
node = (struct ast_node_id *)ast_node_new(sizeof(*node));
node->name = strdup(name);
AST_NODE(node)->type = NODE_TYPE_ID;
AST_NODE(node)->destructor = ast_node_id_free;
return node;
}
static void
ast_node_op_free(struct ast_node *node)
{
struct ast_node_op *operation;
return_if_fail(node != NULL);
operation = (struct ast_node_op *)node;
ast_node_unref(operation->left);
ast_node_unref(operation->right);
ufree(operation);
}
struct ast_node_op*
ast_node_op(char op_helper, struct ast_node *left, struct ast_node *right)
{
struct ast_node_op *operation;
return_val_if_fail(left != NULL, NULL);
return_val_if_fail(right != NULL, NULL);
operation = (struct ast_node_op *)ast_node_new(sizeof(*operation));
switch(op_helper) {
case '^':
operation->opcode = OPCODE_EXP;
AST_NODE(operation)->type = NODE_TYPE_EXP_OP;
break;
case '*':
operation->opcode = OPCODE_MULT;
AST_NODE(operation)->type = NODE_TYPE_MULT_OP;
break;
case '/':
operation->opcode = OPCODE_DIV;
AST_NODE(operation)->type = NODE_TYPE_MULT_OP;
break;
case '+':
operation->opcode = OPCODE_ADD;
AST_NODE(operation)->type = NODE_TYPE_ADD_OP;
break;
case '-':
operation->opcode = OPCODE_SUB;
AST_NODE(operation)->type = NODE_TYPE_ADD_OP;
break;
default:
fprintf(stderr, "unexpected operation %c\n", op_helper);
break;
}
operation->left = left;
operation->right = right;
AST_NODE(operation)->child = left;
AST_NODE(operation)->destructor = ast_node_op_free;
left->parent = AST_NODE(operation);
left->next = right;
right->parent = AST_NODE(operation);
return operation;
}
static void
ast_node_assign_free(struct ast_node *node)
{
struct ast_node_assign *assign;
assign = (struct ast_node_assign *)node;
ast_node_unref(assign->left);
ast_node_unref(assign->right);
ufree(assign);
}
struct ast_node_assign*
ast_node_assign(struct ast_node *left, struct ast_node *right)
{
struct ast_node_assign *assign;
return_val_if_fail(left != NULL, NULL);
return_val_if_fail(right != NULL, NULL);
assign = (struct ast_node_assign *)ast_node_new(sizeof(*assign));
assign->left = left;
assign->right = right;
AST_NODE(assign)->type = NODE_TYPE_ASSIGN;
AST_NODE(assign)->child = left;
AST_NODE(assign)->destructor = ast_node_assign_free;
left->parent = AST_NODE(assign);
left->next = right;
right->parent = AST_NODE(assign);
return assign;
}
static void
ast_node_func_call_free(struct ast_node *node)
{
int i;
struct ast_node_func_call *func_call;
func_call = (struct ast_node_func_call *)node;
for(i = 0; i < func_call->nargs; i++)
ast_node_unref(func_call->args[i]);
ufree(func_call->name);
ufree(func_call);
}
struct ast_node_func_call*
ast_node_func_call(char *name)
{
struct ast_node_func_call *func_call;
func_call = (struct ast_node_func_call *)ast_node_new(sizeof(*func_call));
AST_NODE(func_call)->type = NODE_TYPE_FUNC_CALL;
AST_NODE(func_call)->destructor = ast_node_func_call_free;
func_call->name = ustrdup(name);
return func_call;
}
void
ast_node_func_call_add_arg(struct ast_node_func_call *func_call, struct ast_node *arg)
{
int i;
return_if_fail(func_call != NULL);
i = func_call->nargs++;
func_call->args = urealloc(func_call->args, sizeof(struct ast_node *) * func_call->nargs);
func_call->args[i] = arg;
}
static void
ast_node_end_scope_free(struct ast_node *node)
{
struct ast_node_end_scope *end_scope;
end_scope = (struct ast_node_end_scope *)node;
ufree(end_scope);
}
struct ast_node_end_scope*
ast_node_end_scope(void)
{
struct ast_node_end_scope *end_scope;
end_scope = (struct ast_node_end_scope *)ast_node_new(sizeof(*end_scope));
AST_NODE(end_scope)->type = NODE_TYPE_END_SCOPE;
AST_NODE(end_scope)->destructor = ast_node_end_scope_free;
return end_scope;
}
void
ast_node_return_free(struct ast_node *node)
{
struct ast_node_return *_return;
return_if_fail(node != NULL);
_return = (struct ast_node_return *)node;
if (_return->ret_val)
ast_node_unref(_return->ret_val);
ufree(_return);
}
struct ast_node_return*
ast_node_return(struct ast_node *ret_val)
{
struct ast_node_return *_return;
_return = (struct ast_node_return *)ast_node_new(sizeof(*_return));
_return->ret_val = ret_val;
AST_NODE(_return)->type = NODE_TYPE_RETURN;
AST_NODE(_return)->child = ret_val;
AST_NODE(_return)->destructor = ast_node_return_free;
ret_val->parent = AST_NODE(_return);
return _return;
}
struct ast_node_op*
ast_node_rel_op(opcode_type_t opcode, struct ast_node *left, struct ast_node *right)
{
struct ast_node_op *rel_node;
return_val_if_fail(left != NULL, NULL);
return_val_if_fail(right != NULL, NULL);
rel_node = (struct ast_node_op *)ast_node_new(sizeof(*rel_node));
switch(opcode) {
case OPCODE_LT:
case OPCODE_LE:
case OPCODE_GT:
case OPCODE_GE:
case OPCODE_EQ:
case OPCODE_NE:
rel_node->opcode = opcode;
break;
default:
error(1, "incompatible operation");
break;
}
rel_node->left = left;
rel_node->right = right;
AST_NODE(rel_node)->type = NODE_TYPE_REL_OP;
AST_NODE(rel_node)->child = left;
AST_NODE(rel_node)->destructor = ast_node_op_free;
left->parent = AST_NODE(rel_node);
left->next = right;
right->parent = AST_NODE(rel_node);
return rel_node;
}
struct ast_node_op*
ast_node_logic_op(opcode_type_t opcode, struct ast_node *left, struct ast_node *right)
{
struct ast_node_op *logic_node;
return_val_if_fail(left != NULL, NULL);
return_val_if_fail(right != NULL, NULL);
logic_node = (struct ast_node_op *)ast_node_new(sizeof(*logic_node));
switch(opcode) {
case OPCODE_OR:
logic_node->opcode = opcode;
AST_NODE(logic_node)->type = NODE_TYPE_AND_OP;
break;
case OPCODE_AND:
logic_node->opcode = opcode;
AST_NODE(logic_node)->type = NODE_TYPE_OR_OP;
break;
default:
error(1, "incompatible operation");
break;
}
logic_node->left = left;
logic_node->right = right;
AST_NODE(logic_node)->child = left;
AST_NODE(logic_node)->destructor = ast_node_op_free;
left->parent = AST_NODE(logic_node);
left->next = right;
right->parent = AST_NODE(logic_node);
return logic_node;
}
static void
ast_node_if_free(struct ast_node *node)
{
struct ast_node_if *if_node;
return_if_fail(node != NULL);
if_node = (struct ast_node_if *)node;
ast_node_unref(if_node->expr);
ast_node_unref(if_node->stmt);
if (if_node->_else)
ast_node_unref(if_node->_else);
ufree(if_node);
}
struct ast_node_if*
ast_node_if(struct ast_node *expr, struct ast_node *stmt, struct ast_node *_else)
{
struct ast_node_if *if_node;
return_val_if_fail(expr != NULL, NULL);
return_val_if_fail(stmt != NULL, NULL);
if_node = (struct ast_node_if *)ast_node_new(sizeof(*if_node));
if_node->expr = expr;
if_node->stmt = stmt;
if_node->_else = _else;
AST_NODE(if_node)->type = NODE_TYPE_IF;
AST_NODE(if_node)->child = expr;
AST_NODE(if_node)->destructor = ast_node_if_free;
expr->parent = AST_NODE(if_node);
return if_node;
}
static void
ast_node_for_free(struct ast_node *node)
{
struct ast_node_for *for_node;
return_if_fail(node != NULL);
for_node = (struct ast_node_for *)node;
if (for_node->expr1)
ast_node_unref(for_node->expr1);
if (for_node->expr2)
ast_node_unref(for_node->expr2);
if (for_node->expr3)
ast_node_unref(for_node->expr3);
if (for_node->stmt)
ast_node_unref(for_node->stmt);
ufree(for_node);
}
struct ast_node_for*
ast_node_for(struct ast_node *expr1, struct ast_node *expr2, struct ast_node *expr3, struct ast_node *stmt)
{
struct ast_node_for *for_node;
for_node = (struct ast_node_for *)ast_node_new(sizeof(*for_node));
for_node->expr1 = expr1;
for_node->expr2 = expr2;
for_node->expr3 = expr3;
for_node->stmt = stmt;
AST_NODE(for_node)->type = NODE_TYPE_FOR;
AST_NODE(for_node)->child = expr1;
AST_NODE(for_node)->destructor = ast_node_for_free;
if (expr1)
expr1->parent = AST_NODE(for_node);
if (expr2)
expr2->parent = AST_NODE(for_node);
if (expr3)
expr3->parent = AST_NODE(for_node);
if (stmt)
stmt->parent = AST_NODE(for_node);
return for_node;
}
static void
ast_node_while_free(struct ast_node *node)
{
struct ast_node_while *while_node;
return_if_fail(node != NULL);
while_node = (struct ast_node_while *)node;
ast_node_unref(while_node->expr);
if (while_node->stmt)
ast_node_unref(while_node->stmt);
ufree(while_node);
}
struct ast_node_while*
ast_node_while(struct ast_node *expr, struct ast_node *stmt)
{
struct ast_node_while *while_node;
return_val_if_fail(expr != NULL, NULL);
while_node = (struct ast_node_while *)ast_node_new(sizeof(*while_node));
while_node->expr = expr;
while_node->stmt = stmt;
AST_NODE(while_node)->type = NODE_TYPE_WHILE;
AST_NODE(while_node)->child = expr;
AST_NODE(while_node)->destructor = ast_node_while_free;
expr->parent = AST_NODE(while_node);
stmt->parent = AST_NODE(while_node);
return while_node;
}
static void
ast_node_break_free(struct ast_node *node)
{
struct ast_node_break *break_node;
return_if_fail(node != NULL);
break_node = (struct ast_node_break *)node;
ufree(break_node);
}
struct ast_node_break*
ast_node_break(void)
{
struct ast_node_break *break_node;
break_node = (struct ast_node_break *)ast_node_new(sizeof(*break_node));
AST_NODE(break_node)->type = NODE_TYPE_BREAK;
AST_NODE(break_node)->destructor = ast_node_break_free;
return break_node;
}
static void
ast_node_continue_free(struct ast_node *node)
{
struct ast_node_continue *continue_node;
return_if_fail(node != NULL);
continue_node = (struct ast_node_continue *)node;
ufree(continue_node);
}
struct ast_node_continue*
ast_node_continue(void)
{
struct ast_node_continue *continue_node;
continue_node = (struct ast_node_continue *)ast_node_new(sizeof(*continue_node));
AST_NODE(continue_node)->type = NODE_TYPE_CONTINUE;
AST_NODE(continue_node)->destructor = ast_node_continue_free;
return continue_node;
}
static void
ast_node_vector_free(struct ast_node *node)
{
struct ast_node_vector *vc_node;
int i;
return_if_fail(node != NULL);
vc_node = (struct ast_node_vector *)node;
for (i = 0; i < vc_node->size; i++)
ast_node_unref(vc_node->elem[i]);
ufree(vc_node);
}
struct ast_node_vector*
ast_node_vector(struct ast_node **elem, int size)
{
struct ast_node_vector *vc_node;
return_val_if_fail(elem != NULL, NULL);
vc_node = (struct ast_node_vector *)ast_node_new(sizeof(*vc_node));
AST_NODE(vc_node)->type = NODE_TYPE_VECTOR;
AST_NODE(vc_node)->destructor = ast_node_vector_free;
vc_node->elem = elem;
vc_node->size = size;
return vc_node;
}
static void
ast_node_matrix_free(struct ast_node *node)
{
struct ast_node_matrix *mx_node;
int i, len;
return_if_fail(node != NULL);
mx_node = (struct ast_node_matrix *)node;
len = mx_node->size1 * mx_node->size2;
for (i = 0; i < len; i++)
ast_node_unref(mx_node->elem[i]);
ufree(mx_node);
}
struct ast_node_matrix*
ast_node_matrix(struct ast_node **elem, int size1, int size2)
{
struct ast_node_matrix *mx_node;
return_val_if_fail(elem != NULL, NULL);
mx_node = (struct ast_node_matrix *)ast_node_new(sizeof(*mx_node));
AST_NODE(mx_node)->type = NODE_TYPE_MATRIX;
AST_NODE(mx_node)->destructor = ast_node_matrix_free;
mx_node->elem = elem;
mx_node->size1 = size1;
mx_node->size2 = size2;
return mx_node;
}
void
ast_node_include_free(struct ast_node *node)
{
struct ast_node_include *inc_node;
return_if_fail(node != NULL);
inc_node = (struct ast_node_include *)node;
ufree(inc_node->fname);
ufree(inc_node);
}
struct ast_node_include*
ast_node_include(char *fname)
{
struct ast_node_include *inc_node;
return_val_if_fail(fname != NULL, NULL);
inc_node = (struct ast_node_include *)ast_node_new(sizeof(*inc_node));
AST_NODE(inc_node)->type = NODE_TYPE_INCLUDE;
AST_NODE(inc_node)->destructor = ast_node_include_free;
inc_node->fname = ustrdup(fname);
return inc_node;
}
static void
ast_node_access_free(struct ast_node *node)
{
struct ast_node_access *ac_node;
return_if_fail(node != NULL);
ac_node = (struct ast_node_access *)node;
ufree(ac_node->name);
if (ac_node->dims)
ufree(ac_node->dims);
ufree(ac_node);
}
struct ast_node_access*
ast_node_access(value_t v_type, char *name)
{
struct ast_node_access *ac_node;
return_val_if_fail(name != NULL, NULL);
ac_node = (struct ast_node_access *)ast_node_new(sizeof(*ac_node));
AST_NODE(ac_node)->type = NODE_TYPE_ACCESS;
AST_NODE(ac_node)->destructor = ast_node_access_free;
ac_node->v_type = v_type;
ac_node->name = ustrdup(name);
return ac_node;
}
void
ast_node_access_add(struct ast_node_access *ac_node, struct ast_node *dim)
{
int idx;
return_if_fail(ac_node != NULL);
idx = ac_node->ndims++;
ac_node->dims = urealloc(ac_node->dims,
sizeof(struct ast_node *) * ac_node->ndims);
ac_node->dims[idx] = dim;
}
static void
ast_node_root_free(struct ast_node *node)
{
struct ast_node_root *root_node;
return_if_fail(node != NULL);
root_node = (struct ast_node_root *)node;
if (AST_NODE(root_node)->child)
ast_node_unref(node->child);
ufree(root_node);
}
struct ast_node_root*
ast_node_root(struct ast_node *node)
{
struct ast_node_root *root_node;
root_node = (struct ast_node_root *)ast_node_new(sizeof(*root_node));
AST_NODE(root_node)->type = NODE_TYPE_ROOT;
AST_NODE(root_node)->destructor = ast_node_root_free;
AST_NODE(root_node)->child = node;
return root_node;
}