Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 81 lines (61 sloc) 1.272 kb
d33ec81 @cpylua rename files, remove sc_ prefix
authored
1 #include <string.h>
2 #include "object.h"
3 #include "mem.h"
4
5 object *g_the_empty_list;
6
9106b7b @cpylua fix hash table dispose bug
authored
7 object* get_empty_list(void) {
d33ec81 @cpylua rename files, remove sc_ prefix
authored
8 return g_the_empty_list;
9 }
10
11 int is_empty_list(object *obj) {
443b90d @cpylua code review for GC stack root
authored
12 return obj == g_the_empty_list;
d33ec81 @cpylua rename files, remove sc_ prefix
authored
13 }
14
9106b7b @cpylua fix hash table dispose bug
authored
15 int empty_list_init(void) {
d33ec81 @cpylua rename files, remove sc_ prefix
authored
16 object *p;
17
c9355d7 @cpylua initial gc support, stack local objects not protected
authored
18 p = sc_malloc(sizeof(object));
19 if (p == NULL) {
20 return -1;
21 }
d33ec81 @cpylua rename files, remove sc_ prefix
authored
22 memset(p, 0, sizeof(object));
23 type(p) = THE_EMPTY_LIST;
24 g_the_empty_list = p;
25 return 0;
26 }
27
9106b7b @cpylua fix hash table dispose bug
authored
28 void empty_list_dispose(void) {
29 sc_free(g_the_empty_list);
30 }
31
32
d33ec81 @cpylua rename files, remove sc_ prefix
authored
33
34 int is_pair(object *obj) {
35 return obj != NULL && type(obj) == PAIR;
36 }
37
38 object* cons(object *car, object *cdr) {
39 object *p;
40
41 p = alloc_object();
42 type(p) = PAIR;
43 obj_pv(p).car = car;
44 obj_pv(p).cdr = cdr;
45 return p;
46 }
47
48 object* car(object *pair) {
443b90d @cpylua code review for GC stack root
authored
49 if (!is_pair(pair)) {
d33ec81 @cpylua rename files, remove sc_ prefix
authored
50 return NULL;
51 }
52
53 return obj_pv(pair).car;
54 }
55
56 object* cdr(object *pair) {
443b90d @cpylua code review for GC stack root
authored
57 if (!is_pair(pair)) {
d33ec81 @cpylua rename files, remove sc_ prefix
authored
58 return NULL;
59 }
60
61 return obj_pv(pair).cdr;
62 }
63
64 int set_car(object *pair, object *car) {
65 if (is_pair(pair)) {
66 obj_pv(pair).car = car;
67 return 0;
68 }
69 return -1;
70 }
71
72 int set_cdr(object *pair, object *cdr) {
73 if (is_pair(pair)) {
74 obj_pv(pair).cdr = cdr;
75 return 0;
76 }
77
78 return -1;
79 }
80
Something went wrong with that request. Please try again.