-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
249 lines (181 loc) · 5.72 KB
/
list.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
#include "list.h"
#include "error.h"
#include "zalloc.h"
struct mojo_list* mojo_list_init(void)
{
struct mojo_list *l = zalloc(sizeof(struct mojo_list));
TAILQ_INIT(&l->mojo_list_head);
return l;
}
void mojo_list_append(struct mojo_list *list, struct Object *o)
{
if (list == NULL || o == NULL)
return;
struct mojo_list_elem *e = zalloc(sizeof(struct mojo_list_elem));
e->obj = o;
TAILQ_INSERT_TAIL(&list->mojo_list_head, e, mojo_lists);
}
void mojo_list_remove(struct mojo_list* list)
{
struct mojo_list_elem *e = TAILQ_LAST(&list->mojo_list_head, Lists_head);
TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists);
/* Also free the associated object : */
free_object(e->obj);
free(e);
}
struct Object* mojo_list_nth(struct mojo_list *list, int n)
{
if (list == NULL)
return NULL;
struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head);
int i = 0;
for (i = 0; i < n && TAILQ_NEXT(e, mojo_lists) != NULL; i++) {
e = TAILQ_NEXT(e, mojo_lists);
}
return e->obj;
}
struct Object* mojo_list_last(struct mojo_list *list)
{
if (list == NULL)
return NULL;
struct mojo_list_elem *e = TAILQ_LAST(&list->mojo_list_head, Lists_head);
return e->obj;
}
int mojo_list_length(struct mojo_list *list)
{
if (list == NULL)
return -1;
struct mojo_list_elem *e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head);
int i = 0;
TAILQ_FOREACH(e, &list->mojo_list_head, mojo_lists) {
i++;
}
return i;
}
void mojo_list_free(struct mojo_list *list) {
if (list == NULL)
return;
//printf("len : %d\n", mojo_list_length(list));
/*
while(mojo_list_length(list) > 0)
mojo_list_remove(list);
free(list);
*/
struct mojo_list_elem *e;
int count = 0;
printf(">>> list len : %d, addr: %x\n", mojo_list_length(list), list);
while (e = (struct mojo_list_elem *) TAILQ_FIRST(&list->mojo_list_head)) {
TAILQ_REMOVE(&list->mojo_list_head, e, mojo_lists);
if (e->obj != NULL)
free_object(e->obj);
free(e);
count++;
printf("count : %d\n", count);
}
}
/*
Here begins the public facing list api.
*/
struct Object* create_list_object(void)
{
/* init base object - this needs to be called only once*/
/* the rest of the time, this object is cloned */
/* We can't use new_object because it calls create_list recursively */
struct Object * list_object = (struct Object *) zalloc(sizeof(struct Object));
if (list_object == NULL)
fail("Unable to create list object");
list_object->super = nil_object;
list_object->type = T_LIST;
list_object->name = strdup("List");
list_object->value.l_value = mojo_list_init();
/* Create a second list object that contains the methods of the list object */
struct Object *l1 = (struct Object *) zalloc(sizeof(struct Object));
struct Object *l2 = (struct Object *) zalloc(sizeof(struct Object));
if (l1 == NULL || l2 == NULL)
fail("Unable to create list object at %s %d", __FILE__, __LINE__);
l1->super = nil_object;
l1->type = T_LIST;
l1->name = strdup("List");
l1->value.l_value = mojo_list_init();
l1->methods = nil_object; /* the methods of l1 are in l2, which is an empty list. This way, we don't fall into
an infinite loop in clone_object()
*/
struct Object *met = zalloc(sizeof(struct Object));
if (met == NULL)
fail("Unable to create a method %d %d", __FILE__, __LINE__);
met->type = T_BUILTIN;
met->name = strdup("+");
met->value.c_method = BUILTIN_CAST list_append;
list_append(l1, met);
met = zalloc(sizeof(struct Object));
if (met == NULL)
fail("Unable to create a method %d %d", __FILE__, __LINE__);
met->type = T_BUILTIN;
met->name = strdup("remove");
met->value.c_method = BUILTIN_CAST list_remove;
list_append(l1, met);
met = zalloc(sizeof(struct Object));
if (met == NULL)
fail("Unable to create a method %d %d", __FILE__, __LINE__);
met->type = T_BUILTIN;
met->name = strdup("nth:");
met->value.c_method = BUILTIN_CAST list_nth;
list_append(l1, met);
met = zalloc(sizeof(struct Object));
if (met == NULL)
fail("Unable to create a method %d %d", __FILE__, __LINE__);
met->type = T_BUILTIN;
met->name = strdup("last");
met->value.c_method = BUILTIN_CAST list_last;
list_append(l1, met);
list_object->methods = l1;
return list_object;
}
struct Object* list_append(struct Object *list, struct Object *o)
{
if (list == NULL || (list->type != T_LIST && list->type != T_BLOCK) || list->value.l_value == NULL) {
return nil_object;
}
if (o == NULL) {
return NULL;
}
mojo_list_append(list->value.l_value, o);
return o;
}
struct Object* list_remove(struct Object *list)
{
if (list == NULL || (list->type != T_LIST && list->type != T_BLOCK) || list->value.l_value == NULL)
return nil_object;
mojo_list_remove(list->value.l_value);
return nil_object;
}
struct Object* list_nth(struct Object *list, int n)
{
if (list == NULL || (list->type != T_LIST && list->type != T_BLOCK) || list->value.l_value == NULL)
return nil_object;
struct Object *o = mojo_list_nth(list->value.l_value, n);
if (o != NULL)
return o;
else
return nil_object;
}
struct Object* list_last(struct Object *list)
{
if (list == NULL || (list->type != T_LIST && list->type != T_BLOCK) || list->value.l_value == NULL)
return nil_object;
struct Object *o = mojo_list_last(list->value.l_value);
if (o != NULL)
return o;
else
return nil_object;
}
struct Object* list_length(struct Object *list)
{
if (list == NULL || (list->type != T_LIST && list->type != T_BLOCK) || list->value.l_value == NULL)
return nil_object;
struct Object *o = clone_object(number_object);
if (o == NULL)
fail("Unable to clone number_object in %s at line %d", __FILE__, __LINE__);
o->value.i_value = mojo_list_length(list->value.l_value);
return o;
}