aboutsummaryrefslogtreecommitdiff
path: root/interpreters/bf.c
blob: 606c6091b10b77cf87d37860948e2e631d6506d9 (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <errno.h>

#define exit_fatal(...)                                                        \
	do {                                                                       \
		fprintf (stderr, "fatal: " __VA_ARGS__);                               \
		exit (EXIT_FAILURE);                                                   \
	} while (0)

// --- Safe memory management --------------------------------------------------

static void *
xmalloc (size_t n)
{
	void *p = malloc (n);
	if (!p)
		exit_fatal ("malloc: %s\n", strerror (errno));
	return p;
}

static void *
xrealloc (void *o, size_t n)
{
	void *p = realloc (o, n);
	if (!p && n)
		exit_fatal ("realloc: %s\n", strerror (errno));
	return p;
}

// --- Dynamically allocated strings -------------------------------------------

struct str
{
	char *str;                          ///< String data, null terminated
	size_t alloc;                       ///< How many bytes are allocated
	size_t len;                         ///< How long the string actually is
};

static void
str_init (struct str *self)
{
	self->alloc = 16;
	self->len = 0;
	self->str = strcpy (xmalloc (self->alloc), "");
}

static void
str_ensure_space (struct str *self, size_t n)
{
	// We allocate at least one more byte for the terminating null character
	size_t new_alloc = self->alloc;
	while (new_alloc <= self->len + n)
		new_alloc <<= 1;
	if (new_alloc != self->alloc)
		self->str = xrealloc (self->str, (self->alloc = new_alloc));
}

static void
str_append_data (struct str *self, const void *data, size_t n)
{
	str_ensure_space (self, n);
	memcpy (self->str + self->len, data, n);
	self->len += n;
	self->str[self->len] = '\0';
}

static void
str_append_c (struct str *self, char c)
{
	str_append_data (self, &c, 1);
}

// --- Main --------------------------------------------------------------------

int
main (int argc, char *argv[])
{
	struct str program; str_init (&program);
	struct str data;    str_init (&data);

	int c;
	while ((c = fgetc (stdin)) != EOF)
		str_append_c (&program, c);
	if (ferror (stdin))
		exit_fatal ("can't read program\n");

	FILE *input = fopen ("/dev/tty", "rb");
	if (!input)
		exit_fatal ("can't open terminal for reading\n");

	size_t dataptr = 0;
	str_append_c (&data, 0);

	for (size_t i = 0; i < program.len; i++)
	{
		switch (program.str[i])
		{
			long pairs;
		case '>':
			assert (dataptr != SIZE_MAX);
			dataptr++;
			if (dataptr == data.len)
				str_append_c (&data, 0);
			break;
		case '<':
			assert (dataptr != 0);
			dataptr--;
			break;

		case '+': data.str[dataptr]++; break;
		case '-': data.str[dataptr]--; break;

		case '.':
			fputc (data.str[dataptr], stdout);
			break;
		case ',':
			data.str[dataptr] = c = fgetc (input);
			assert (c != EOF);
			break;

		case '[':
			if (data.str[dataptr]) break;

			for (pairs = 0; i < program.len; i++)
			{
				switch (program.str[i])
				{
				case '[': pairs++; break;
				case ']': pairs--; break;
				}
				if (!pairs)
					break;
			}
			assert (!pairs);
			break;
		case ']':
			if (!data.str[dataptr]) break;

			for (pairs = 0; i != SIZE_MAX; i--)
			{
				switch (program.str[i])
				{
				case '[': pairs--; break;
				case ']': pairs++; break;
				}
				if (!pairs)
					break;
			}
			assert (!pairs);
			break;
		default:
			break;
		}
	}
	return 0;
}