aboutsummaryrefslogtreecommitdiff
path: root/sloth.c
blob: 9cb045a6d2ebd294c164bd42b430f2d69b3eb795 (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
#include "sloth.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;
	}
	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;
		free(ctx->dict);
		ctx->dict = next;
	}
}

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_exec(Sloth *ctx, const char *s)
{
	const SlothHash hash = XXH32(s, strlen(s), 0);
	for (SlothDict *dict = ctx->dict; dict != NULL; dict = dict->next)
		if (dict->hash == hash) {
			const SlothError err = dict->func(ctx);
			if (err != NULL)
				return err;
		}
	return "sloth_exec: unrecognized token";
}

SlothError sloth_exec_line(Sloth *ctx, char *s)
{
	static const char *sep = "\t\n\v\f\r ";
	for (char *tok = strtok(s, sep); tok != NULL; tok = strtok(NULL, sep)) {
		const SlothError 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;
}

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]);
}