aboutsummaryrefslogtreecommitdiff
path: root/src/object.c
blob: d3f589d0d98939621346a4e33f6abdccb3083a55 (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
#include "object.h"
#include "ast.h"
#include "hmap.h"
#include "vector.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char *object_types[] = {
	"error",
	"null",
	"int",
	"bool",
	"return",
};

static void
environment_destroy_cb(const void *key, void *val)
{
	struct object *obj = val;
	object_unref(obj);
}

static inline char *
bool_sprint(bool val, char *str)
{
	sprintf(str, val ? "true" : "false");
	return str;
}

extern inline const char *
object_type_print(enum object_type type)
{
	return object_types[type];
}

static inline char *
object_func_sprint(struct object *obj, char *str)
{
	char *cur = str;
	sprintf(cur, "fn(");
	cur += strlen(cur);
	size_t i;
	struct expression *ident;
	vector_foreach(obj->func.params, i, ident) {
		node_sprint(ident, cur);
		cur += strlen(cur);
		if (i < (obj->func.params->len - 1)) {
			cur[0] = ',', cur[1] = ' ', cur += 2;
		}
	}
	sprintf(cur, ") {\n");
	cur += strlen(cur);
	node_sprint(obj->func.body, cur);
	cur += strlen(cur);
	sprintf(cur, "\n}");

	return str;
}

char *
object_sprint(struct object *obj, char *str)
{
	switch (obj->type) {
	case OBJECT_NULL:
		sprintf(str, "null");
		break;
	case OBJECT_BOOL:
		return bool_sprint(obj->boolean, str);
	case OBJECT_INT:
		sprintf(str, "%ld", obj->integer);
		break;
	case OBJECT_RETURN:
		object_sprint(obj->retrn.value, str);
	case OBJECT_ERROR:
		strcpy(str, obj->error.msg);
	case OBJECT_FUNC:
		return object_func_sprint(obj, str);
	}
	return str;
}

struct object *
object_new_int(int64_t val)
{
	struct object *obj = malloc(sizeof(*obj));
	obj->type = OBJECT_INT;
	obj->refcount = 1;
	obj->integer = val;
	return obj;
}

struct object *
object_new_error(char *msg)
{
	struct object *obj = malloc(sizeof(*obj));
	obj->type = OBJECT_ERROR;
	obj->refcount = 1;
	obj->error.msg = msg;
	return obj;
}

struct object *
object_new_return(struct object *val)
{
	struct object *obj = malloc(sizeof(*obj));
	obj->type = OBJECT_RETURN;
	obj->refcount = 1;
	obj->retrn.value = val;
	return obj;
}

struct object *
object_new_func(struct expression *expr)
{
	struct object *obj = malloc(sizeof(*obj));
	obj->type = OBJECT_FUNC;
	obj->refcount = 1;
	obj->func.body = node_dup(expr->func.body);
	obj->func.params = expression_vector_dup(expr->func.parameters);
	return obj;
}

void
object_ref(struct object *obj)
{
	obj->refcount++;
}

void
object_unref(struct object *obj)
{
	if (obj->type == OBJECT_NULL || obj->type == OBJECT_BOOL) return;
	if (--obj->refcount < 1) {
		switch (obj->type) {
		case OBJECT_RETURN:
			object_unref(obj->retrn.value);
			break;
		case OBJECT_ERROR:
			free(obj->error.msg);
			break;
		case OBJECT_FUNC:
			node_destroy(obj->func.body);
			size_t i;
			struct expression *param;
			vector_foreach(obj->func.params, i, param) {
				node_destroy(param);
			}
			vector_free(obj->func.params);
			break;
		default:
			break;
		}
		free(obj);
	}
}

struct environment *
environment_new_enclosed(struct environment *outer)
{
	struct environment *env = malloc(sizeof(*env));
	env->store = hmap_new();
	if (!env->store) {
		free(env);
		return NULL;
	}
	env->outer = outer;

	return env;
}

struct object *
environment_set(struct environment *env, struct slice key, struct object *val)
{
	object_ref(val);
	return hmap_sets(env->store, key, val);
}

struct object *
environment_get(struct environment *env, const struct slice *key)
{
	struct object *obj = hmap_gets(env->store, key);
	if (!obj && env->outer) {
		obj = environment_get(env->outer, key);
	}
	return obj;
}

void
environment_destroy(struct environment *env)
{
	hmap_destroy(env->store, environment_destroy_cb);
	free(env);
}