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
|
#include "graphs.h"
#include <string.h>
#include "mem.h"
void use_node(struct node* node, unsigned int count) {
if (!node)
return;
node->used_count += count;
if (node->kind == NODE_LIST ||
node->kind == NODE_TUPLE ||
node->kind == NODE_APP) {
use_node(node->var1, count);
use_node(node->var2, count);
}
}
void free_node(struct node* node, unsigned int count, unsigned free_first) {
if (!node)
return;
node->used_count -= count;
if (node->kind == NODE_LIST ||
node->kind == NODE_TUPLE ||
node->kind == NODE_APP) {
free_node((struct node*) node->var1, count, 1);
free_node((struct node*) node->var2, count, 1);
}
if (node->used_count == 0) {
if (node->kind == NODE_INT || node->kind == NODE_NAME)
my_free(node->var1);
if (node->kind == NODE_CODE)
my_free(node->var2);
if (free_first)
my_free(node);
}
}
void cpy_expression_to_node(struct node* dst, expression* src) {
if (!dst || !src)
return;
dst->kind = src->kind;
switch (src->kind) {
case EXPR_INT:
dst->var1 = my_calloc(1, sizeof(int));
*((int*) dst->var1) = *((int*) src->var1);
break;
case EXPR_NAME:
dst->var1 = my_calloc(1, 1 + strlen((char*) src->var1));
strcpy(dst->var1, src->var1);
break;
case EXPR_CODE:
dst->var1 = src->var1;
dst->var2 = my_calloc(1, sizeof(unsigned char));
*((unsigned char*) dst->var2) = *((unsigned char*) src->var2);
break;
case EXPR_LIST:
case EXPR_TUPLE:
case EXPR_APP:
dst->var1 = dst->var2 = NULL;
if (src->var1) {
dst->var1 = my_calloc(1, sizeof(struct node));
cpy_expression_to_node(dst->var1, src->var1);
}
if (src->var2) {
dst->var2 = my_calloc(1, sizeof(struct node));
cpy_expression_to_node(dst->var2, src->var2);
}
}
dst->used_count = 1;
}
void cpy_node_to_expression(expression* dst, struct node* src) {
if (!dst || !src)
return;
free_expression(dst);
dst->is_strict = 0;
dst->kind = src->kind;
switch (src->kind) {
case NODE_INT:
dst->var1 = my_calloc(1, sizeof(int));
*((int*) dst->var1) = *((int*) src->var1);
break;
case NODE_NAME:
dst->var1 = my_calloc(1, 1 + strlen((char*) src->var1));
strcpy(dst->var1, src->var1);
break;
case NODE_CODE:
dst->var1 = src->var1;
dst->var2 = my_calloc(1, sizeof(unsigned char));
*((unsigned char*) dst->var2) = *((unsigned char*) src->var2);
break;
case NODE_LIST:
case NODE_TUPLE:
case NODE_APP:
dst->var1 = dst->var2 = NULL;
if (src->var1) {
dst->var1 = my_calloc(1, sizeof(expression));
cpy_node_to_expression(dst->var1, src->var1);
}
if (src->var2) {
dst->var2 = my_calloc(1, sizeof(expression));
cpy_node_to_expression(dst->var2, src->var2);
}
break;
}
}
|