-
Notifications
You must be signed in to change notification settings - Fork 1
/
list.c
80 lines (61 loc) · 1.24 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
#include <string.h>
#include "object.h"
#include "mem.h"
object *g_the_empty_list;
object* get_empty_list(void) {
return g_the_empty_list;
}
int is_empty_list(object *obj) {
return obj == g_the_empty_list;
}
int empty_list_init(void) {
object *p;
p = sc_malloc(sizeof(object));
if (p == NULL) {
return -1;
}
memset(p, 0, sizeof(object));
type(p) = THE_EMPTY_LIST;
g_the_empty_list = p;
return 0;
}
void empty_list_dispose(void) {
sc_free(g_the_empty_list);
}
int is_pair(object *obj) {
return obj != NULL && type(obj) == PAIR;
}
object* cons(object *car, object *cdr) {
object *p;
p = alloc_object();
type(p) = PAIR;
obj_pv(p).car = car;
obj_pv(p).cdr = cdr;
return p;
}
object* car(object *pair) {
if (!is_pair(pair)) {
return NULL;
}
return obj_pv(pair).car;
}
object* cdr(object *pair) {
if (!is_pair(pair)) {
return NULL;
}
return obj_pv(pair).cdr;
}
int set_car(object *pair, object *car) {
if (is_pair(pair)) {
obj_pv(pair).car = car;
return 0;
}
return -1;
}
int set_cdr(object *pair, object *cdr) {
if (is_pair(pair)) {
obj_pv(pair).cdr = cdr;
return 0;
}
return -1;
}