aboutsummaryrefslogtreecommitdiff
path: root/src/object.c
blob: 89720203001d933ef255052475714e5fa9f405a3 (plain)
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
#include "object.h"

static const char *roscha_types[] = {
	[ROSCHA_NULL]   = "null",
	[ROSCHA_INT]    = "int",
	[ROSCHA_BOOL]   = "bool",
	[ROSCHA_STRING] = "string",
	[ROSCHA_SLICE]  = "slice",
	[ROSCHA_VECTOR] = "vector",
	[ROSCHA_HMAP]   = "hashmap",
};

extern inline const char *
roscha_type_print(enum roscha_type type)
{
	return roscha_types[type];
}

static void
roscha_object_destroy_hmap_cb(const struct slice *key, void *val)
{
	struct roscha_object *obj = val;
	roscha_object_unref(obj);
}

static inline sds
bool_string(bool val, sds str)
{
	sdscat(str, val ? "true" : "false");
	return str;
}

static inline sds
vector_string(struct vector *vec, sds str)
{
	size_t                i;
	struct roscha_object *obj;
	str = sdscat(str, "[ ");
	vector_foreach (vec, i, obj) {
		str = roscha_object_string(obj, str);
		str = sdscat(str, ", ");
	}
	str = sdscat(str, "]");
	return str;
}

static inline sds
hmap_string(struct hmap *map, sds str)
{
	str = sdscat(str, "{ ");
	const struct slice *key;
	void               *val;
	struct hmap_iter   *iter = hmap_iter_new(map);
	hmap_iter_foreach (iter, &key, &val) {
		str                             = sdscatfmt(str, "\"%s\": ", key->str);
		const struct roscha_object *obj = val;
		str                             = roscha_object_string(obj, str);
		str                             = sdscat(str, ", ");
	}
	str = sdscat(str, "}");

	return str;
}

sds
roscha_object_string(const struct roscha_object *obj, sds str)
{
	switch (obj->type) {
	case ROSCHA_NULL:
		return sdscat(str, "null");
	case ROSCHA_BOOL:
		return bool_string(obj->boolean, str);
	case ROSCHA_INT:
		return sdscatfmt(str, "%I", obj->integer);
	case ROSCHA_STRING:
		return sdscat(str, obj->string);
	case ROSCHA_SLICE:
		return slice_string(&obj->slice, str);
	case ROSCHA_VECTOR:
		return vector_string(obj->vector, str);
	case ROSCHA_HMAP:
		return hmap_string(obj->hmap, str);
	}
	return str;
}

struct roscha_object *
roscha_object_new_int(int64_t val)
{
	struct roscha_object *obj = malloc(sizeof(*obj));
	obj->type                 = ROSCHA_INT;
	obj->refcount             = 1;
	obj->integer              = val;
	return obj;
}

struct roscha_object *
roscha_object_new_slice(struct slice s)
{
	struct roscha_object *obj = malloc(sizeof(*obj));
	obj->type                 = ROSCHA_SLICE;
	obj->refcount             = 1;
	obj->slice                = s;
	return obj;
}

struct roscha_object *
roscha_object_new_string(sds str)
{
	struct roscha_object *obj = malloc(sizeof(*obj));
	obj->type                 = ROSCHA_STRING;
	obj->refcount             = 1;
	obj->string               = str;
	return obj;
}

struct roscha_object *
roscha_object_new_vector(struct vector *vec)
{
	struct roscha_object *obj = malloc(sizeof(*obj));
	obj->type                 = ROSCHA_VECTOR;
	obj->refcount             = 1;
	obj->vector               = vec;
	return obj;
}

struct roscha_object *
roscha_object_new_hmap(struct hmap *map)
{
	struct roscha_object *obj = malloc(sizeof(*obj));
	obj->type                 = ROSCHA_HMAP;
	obj->refcount             = 1;
	obj->hmap                 = map;
	return obj;
}

static inline void
roscha_object_vector_destroy(struct roscha_object *obj)
{
	size_t                i;
	struct roscha_object *subobj;
	vector_foreach (obj->vector, i, subobj) {
		roscha_object_unref(subobj);
	}
	vector_free(obj->vector);
}

void
roscha_object_unref(struct roscha_object *obj)
{
	if (obj == NULL) return;
	if (obj->type == ROSCHA_NULL || obj->type == ROSCHA_BOOL) return;
	if (--obj->refcount < 1) {
		switch (obj->type) {
		case ROSCHA_STRING:
			sdsfree(obj->string);
			break;
		case ROSCHA_VECTOR:
			roscha_object_vector_destroy(obj);
			break;
		case ROSCHA_HMAP:
			hmap_destroy(obj->hmap, roscha_object_destroy_hmap_cb);
			break;
		default:
			break;
		}
		free(obj);
	}
}

void
roscha_vector_push(struct roscha_object *vec, struct roscha_object *val)
{
	roscha_object_ref(val);
	vector_push(vec->vector, val);
}

struct roscha_object *
roscha_vector_pop(struct roscha_object *vec)
{
	return (struct roscha_object *)vector_pop(vec->vector);
}

struct roscha_object *
roscha_hmap_sets(struct roscha_object *hmap, struct slice key,
                 struct roscha_object *value)
{
	roscha_object_ref(value);
	return hmap_sets(hmap->hmap, key, value);
}

struct roscha_object *
roscha_hmap_setstr(struct roscha_object *hmap, const char *key,
                   struct roscha_object *value)
{
	roscha_object_ref(value);
	return hmap_set(hmap->hmap, key, value);
}

struct roscha_object *
roscha_hmap_gets(struct roscha_object *hmap, const struct slice *key)
{
	return (struct roscha_object *)hmap_gets(hmap->hmap, key);
}

struct roscha_object *
roscha_hmap_getstr(struct roscha_object *hmap, const char *key)
{
	return (struct roscha_object *)hmap_get(hmap->hmap, key);
}

struct roscha_object *
roscha_hmap_pops(struct roscha_object *hmap, const struct slice *key)
{
	return (struct roscha_object *)hmap_removes(hmap->hmap, key);
}

struct roscha_object *
roscha_hmap_popstr(struct roscha_object *hmap,
                   const char           *key)
{
	return (struct roscha_object *)hmap_remove(hmap->hmap, key);
}

void
roscha_hmap_unsets(struct roscha_object *hmap, const struct slice *key)
{
	struct roscha_object *obj = hmap_removes(hmap->hmap, key);
	if (obj) roscha_object_unref(obj);
}

void
roscha_hmap_unsetstr(struct roscha_object *hmap, const char *key)
{
	struct roscha_object *obj = hmap_remove(hmap->hmap, key);
	if (obj) roscha_object_unref(obj);
}