aboutsummaryrefslogtreecommitdiff
path: root/sloth.c
blob: 7c861c83cce734aeb0599108ad3607aba410c5a6 (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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include "sloth.h"
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xxhash.h>

SlothError sloth_init(Sloth *ctx)
{
	SlothError err;
	err = sloth_dict_append(ctx, SLOTH_DICT_C("+", sloth_add));
	if (err != NULL) {
		sloth_deinit(ctx);
		return err;
	}
	err = sloth_dict_append(ctx, SLOTH_DICT_C(".", sloth_write));
	if (err != NULL) {
		sloth_deinit(ctx);
		return err;
	}
	err = sloth_dict_append(ctx, SLOTH_DICT_C("@", sloth_store));
	if (err != NULL) {
		sloth_deinit(ctx);
		return err;
	}
	err = sloth_dict_append(ctx, SLOTH_DICT_C("!", sloth_retrieve));
	if (err != NULL) {
		sloth_deinit(ctx);
		return err;
	}
	err = sloth_dict_append(ctx, SLOTH_DICT_C("<", sloth_compare));
	if (err != NULL) {
		sloth_deinit(ctx);
		return err;
	}
	err = sloth_dict_append(ctx, SLOTH_DICT_C(":", sloth_compile_begin));
	if (err != NULL) {
		sloth_deinit(ctx);
		return err;
	}
	err = sloth_dict_append(ctx, SLOTH_DICT_C(";", sloth_compile_end));
	if (err != NULL) {
		sloth_deinit(ctx);
		return err;
	}
	return NULL;
}

void sloth_deinit(Sloth *ctx)
{
	if (ctx->stack != NULL) {
		free(ctx->stack);
		ctx->stack = NULL;
	}
	while (ctx->dict != NULL) {
		SlothDict *const next = ctx->dict->next;
		if (ctx->dict->instrs != NULL) {
			SlothInstr *const next_instr = ctx->dict->instrs->next;
			free(ctx->dict->instrs);
			ctx->dict->instrs = next_instr;
		}
		free(ctx->dict);
		ctx->dict = next;
	}
	if (ctx->comp.instrs != NULL) {
		SlothInstr *const next_instr = ctx->comp.instrs->next;
		free(ctx->comp.instrs);
		ctx->comp.instrs = next_instr;
	}
}

SlothError sloth_dict_append(Sloth *ctx, SlothDict dict)
{
	SlothDict **dest;
	if (ctx->dict == NULL)
		dest = &ctx->dict;
	else {
		SlothDict *last = ctx->dict;
		while (last->next != NULL)
			last = last->next;
		dest = &last->next;
	}
	*dest = malloc(sizeof(SlothDict));
	if (*dest == NULL)
		return "sloth_dict_append: malloc failed";
	memcpy(*dest, &dict, sizeof(SlothDict));
	return NULL;
}

SlothError sloth_str_to_instr(Sloth *ctx, const char *s, SlothInstr *instr)
{
	const SlothHash hash = XXH32(s, strlen(s), 0);
	instr->next = NULL;
	for (SlothDict *dict = ctx->dict; dict != NULL; dict = dict->next)
		if (dict->hash == hash) {
			instr->litteral = 0;
			instr->hash = hash;
			return NULL;
		}
	for (size_t i = 0; s[i] != '\0'; i++)
		if (!isdigit(s[i]))
			return "sloth_str_to_instr: unrecognized token";
	instr->litteral = 1;
	instr->byte = atoi(s);
	return NULL;
}

SlothError sloth_exec_instr(Sloth *ctx, const SlothInstr *instr)
{
	SlothError err = NULL;
	while (instr != NULL) {
		if (instr->litteral)
			err = sloth_push(ctx, instr->byte);
		else for (SlothDict *dict = ctx->dict; dict != NULL;
		          dict = dict->next)
		{
			if (dict->hash == instr->hash) {
				if (dict->c_func)
					err = dict->func(ctx);
				else
					err = sloth_exec_instr(ctx, dict->instrs);
				break;
			}
		}
		if (err != NULL)
			return err;
		instr = instr->next;
	}
	return NULL;
}

SlothError sloth_exec(Sloth *ctx, const char *s)
{
	SlothError err;
	SlothInstr instr = {0};
	err = sloth_str_to_instr(ctx, s, &instr);
	if (err != NULL)
		return err;
	err = sloth_exec_instr(ctx, &instr);
	if (err != NULL)
		return err;
	return NULL;
}

SlothError sloth_compile(Sloth *ctx, const char *s)
{
	SlothError err;
	if (ctx->comp.hash == 0) {
		ctx->comp.hash = XXH32(s, strlen(s), 0);
		return NULL;
	}
	SlothInstr instr = {0};
	err = sloth_str_to_instr(ctx, s, &instr);
	if (err != NULL)
		return err;
	SlothInstr **dest;
	if (ctx->comp.instrs == NULL)
		dest = &ctx->comp.instrs;
	else {
		SlothInstr *last = ctx->comp.instrs;
		while (last->next != NULL)
			last = last->next;
		dest = &last->next;
	}
	*dest = malloc(sizeof(SlothInstr));
	if (*dest == NULL)
		return "sloth_compile: malloc failed";
	memcpy(*dest, &instr, sizeof(SlothInstr));
	return NULL;
}

SlothError sloth_exec_line(Sloth *ctx, char *s)
{
	SlothError err = NULL;
	static const char *sep = "\t\n\v\f\r ";
	for (char *tok = strtok(s, sep); tok != NULL; tok = strtok(NULL, sep)) {
		if (ctx->compile && strcmp(tok, ";") != 0)
			err = sloth_compile(ctx, tok);
		else
			err = sloth_exec(ctx, tok);
		if (err != NULL)
			return err;
	}
	return NULL;
}

SlothError sloth_pop(Sloth *ctx, SlothByte *v)
{
	if (ctx->stack_size == 0)
		return "sloth_pull: stack is empty";
	*v = ctx->stack[ctx->stack_size - 1];
	ctx->stack_size -= 1;
	return NULL;
}

SlothError sloth_push(Sloth *ctx, SlothByte v)
{
	if (ctx->stack == NULL) {
		ctx->stack = malloc(sizeof(SlothByte) * 16);
		if (ctx->stack == NULL)
			return "sloth_push: calloc failed";
		ctx->stack_capacity = 16;
	} else if (ctx->stack_size == ctx->stack_capacity) {
		SlothByte *const new_stack =
			realloc(ctx->stack, ctx->stack_capacity * 2);
		if (new_stack == NULL)
			return "sloth_push: realloc failed";
		ctx->stack_capacity *= 2;
	}
	ctx->stack[ctx->stack_size] = v;
	ctx->stack_size += 1;
	return NULL;
}

SlothError sloth_add(Sloth *ctx)
{
	SlothError err;
	SlothByte a, b;
	err = sloth_pop(ctx, &a);
	if (err != NULL)
		return err;
	err = sloth_pop(ctx, &b);
	if (err != NULL)
		return err;
	a += b;
	err = sloth_push(ctx, a);
	if (err != NULL)
		return err;
	return NULL;
}

SlothError sloth_write(Sloth *ctx)
{
	SlothError err;
	SlothByte a;
	err = sloth_pop(ctx, &a);
	if (err != NULL)
		return err;
	printf("%c", (unsigned char)a);
	return NULL;
}

SlothError sloth_store(Sloth *ctx)
{
	SlothError err;
	SlothByte a, b;
	err = sloth_pop(ctx, &a);
	if (err != NULL)
		return err;
	err = sloth_pop(ctx, &b);
	if (err != NULL)
		return err;
	ctx->mem[a] = b;
	return NULL;
}

SlothError sloth_retrieve(Sloth *ctx)
{
	SlothError err;
	SlothByte a;
	err = sloth_pop(ctx, &a);
	if (err != NULL)
		return err;
	err = sloth_push(ctx, ctx->mem[a]);
	if (err != NULL)
		return err;
	return NULL;
}

SlothError sloth_compare(Sloth *ctx)
{
	SlothError err;
	SlothByte a, b;
	err = sloth_pop(ctx, &a);
	if (err != NULL)
		return err;
	err = sloth_pop(ctx, &b);
	if (err != NULL)
		return err;
	err = sloth_push(ctx, b < a);
	if (err != NULL)
		return err;
	return NULL;
}

SlothError sloth_compile_begin(Sloth *ctx)
{
	if (ctx->compile)
		return "sloth_compile_begin: already in compile mode";
	ctx->comp.hash = 0;
	ctx->comp.c_func = 0;
	ctx->comp.func = NULL;
	ctx->comp.instrs = NULL;
	ctx->comp.next = NULL;
	ctx->compile = 1;
	return NULL;
}

SlothError sloth_compile_end(Sloth *ctx)
{
	if (!ctx->compile)
		return "sloth_compile_end: not in compile mode";
	if (ctx->comp.hash == 0)
		return "sloth_compile_end: compiled instruction missing hash";
	const SlothError err = sloth_dict_append(ctx, ctx->comp);
	if (err != NULL)
		return NULL;
	ctx->comp.hash = 0;
	ctx->comp.instrs = NULL;
	ctx->compile = 0;
	return NULL;
}

void sloth_inspect_stack(const Sloth *ctx)
{
	printf("<%lu> ", ctx->stack_size);
	for (size_t i = 0; i < ctx->stack_size; i++)
		printf("%d ", ctx->stack[i]);
}