aboutsummaryrefslogtreecommitdiff
path: root/ls_run.c
blob: 3ab09fa02060c7fff1312233c80a1ea9bcf4fd49 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
// This software disclaims copyright. Do what you want with it. Be gay, do
// crime. Originally written by Alexis Lockwood in 2021. Ⓐ

// --- DEPENDENCIES ------------------------------------------------------------

// Supporting modules
#include "ls.h"
#include "ls_internal.h"
#include "ls_lex.h"

// External dependencies
#include <libexplain/ferror.h>
#include <libexplain/fopen.h>
#include <libexplain/fstat.h>
#include <libexplain/calloc.h>

// Standard headers
#include <stdbool.h>
#include <stddef.h>
#include <inttypes.h>
#include <stdio.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <assert.h>
#include <limits.h>

// --- PRIVATE MACROS ----------------------------------------------------------
// --- PRIVATE DATATYPES -------------------------------------------------------

typedef struct {
	FILE * f;
	size_t cache_page_size;
	size_t n_cache_pages;
	size_t n_page;
	size_t total_size;

	char ** cache;
	size_t * cache_offsets;
} file_fetcher_t;

// --- PRIVATE CONSTANTS -------------------------------------------------------
// --- PRIVATE FUNCTION PROTOTYPES ---------------------------------------------

static int _fetcher(void * arg, uint16_t loc);
static void _usage(char const * argv0, bool short_text);
static void _tokenize(ls_t * ls);
static void _line_trace_hook(ls_t * ls);

// --- PUBLIC VARIABLES --------------------------------------------------------
// --- PRIVATE VARIABLES -------------------------------------------------------

static bool cachedebug = false;
static bool pooldebug = false;

// --- PUBLIC FUNCTIONS --------------------------------------------------------

int main(int argc, char ** argv)
{
	enum { ACT_RUN, ACT_TOKENIZE } action = ACT_RUN;
	bool tron = false;

	int opt;
	while ((opt = getopt(argc, argv, "hcpkt")) != -1)
	{
		switch (opt)
		{
		default:
			_usage(argv[0], false);
			exit(opt == 'h' ? EXIT_SUCCESS : EXIT_FAILURE);
			break;

		case 'c':
			cachedebug = true;
			break;

		case 'p':
			pooldebug = true;
			break;

		case 'k':
			action = ACT_TOKENIZE;
			break;

		case 't':
			tron = true;
			break;
		}
	}

	if (optind >= argc)
	{
		_usage(argv[0], true);
		exit(EXIT_FAILURE);
	}

	FILE * f = explain_fopen_or_die(argv[optind], "r");

	struct stat statbuf;
	explain_fstat_or_die(fileno(f), &statbuf);

	size_t const n_cache_pages = 3;
	size_t const cache_page_size = 8; // This should be bigger lol

	file_fetcher_t fet;
	fet.f = f;
	fet.cache_page_size = cache_page_size;
	fet.n_cache_pages = n_cache_pages;
	fet.n_page = (size_t) -1;
	assert(statbuf.st_size >= 0);
	fet.total_size = (size_t) statbuf.st_size;
	fet.cache = explain_calloc_or_die(n_cache_pages, sizeof(*fet.cache));
	fet.cache_offsets = explain_calloc_or_die(n_cache_pages,
		sizeof(*fet.cache_offsets));
	for (size_t i = 0; i < n_cache_pages; i++)
	{
		fet.cache[i] = explain_calloc_or_die(cache_page_size,
			sizeof(char));
		fet.cache_offsets[i] = (size_t) -1;
	}

	ls_t ls;
	ls.fetcher = _fetcher;
	ls.fetcher_arg = &fet;
	ls.line_trace_hook = tron ? _line_trace_hook : NULL;

	if (action == ACT_TOKENIZE)
	{
		ls._pc = 0;
		ls._labels = NULL;
		_tokenize(&ls);
		exit(EXIT_SUCCESS);
	}

	ls_value_t pool[1000];
	ls_error_t e = ls_run(&ls, pool, 1000);

	uint16_t line = 0, col = 0;
	ls_translate_pc(&ls, ls._pc, &line, &col);

	if (e == LS_STOPPED)
	{
		fprintf(stderr, ">stopped at %u:%u\n", line, col);
	}
	else if (e != 0)
	{
		fprintf(stderr, ">error: %s at %u:%u\n",
		        ls_strerror(e), line, col);
	}

	fclose(f);

	if (pooldebug)
	{
		printf("\n\n==== POOL ====\n");

		printf("First free: <%.4u>\n\n", (unsigned)(ls._pool - pool));
		for (size_t i = 0; i < sizeof(pool)/sizeof(pool[0]); i++)
		{
			ls_print_value(stdout, &pool[i], &pool[0]);
			printf("\n");
			if (pool[i].ty == LS_TY_PRISTINE)
				break;
		}
	}

	return 0;
}

// --- PRIVATE FUNCTION DEFINITIONS --------------------------------------------

static int _fetcher(void * arg, uint16_t loc)
{
	// TODO: this needs to be optimized a lot, but I'd like to have a good
	// one to provide as a layer for use by implementers (as this will be
	// a very common sort of use case) so will work on that when I get a
	// chance to do it really well.
	file_fetcher_t * self = (file_fetcher_t *) arg;

	if (loc >= self->total_size)
		return -LS_NO_MORE_PROGRAM;

	// Extremely rudimentary cache fetch. A realistic one would be more
	// heavily optimized for hits.
	if (self->n_page != (size_t) -1 &&
		self->cache_offsets[self->n_page] != (size_t) -1 &&
		self->cache_offsets[self->n_page] <= loc &&
		loc - self->cache_offsets[self->n_page] < self->cache_page_size)
	{
		// HIT
		char c = self->cache[self->n_page][
			loc - self->cache_offsets[self->n_page]
		];

		return (unsigned char) c;
	}
	else
	{
		for (size_t i = 0; i < self->n_cache_pages; i++)
		{
			if (self->cache_offsets[i] != (size_t) -1 &&
				self->cache_offsets[i] <= loc &&
				loc - self->cache_offsets[i] < self->cache_page_size)
			{
				if (cachedebug)
					fprintf(stderr, ">FAR HIT: %"PRIu16"\n", loc);
				self->n_page = i;
				char c = self->cache[self->n_page][
					loc - self->cache_offsets[self->n_page]
				];

				return (unsigned char) c;
			}
		}

		if (cachedebug)
			fprintf(stderr, ">MISS: %"PRIu16"\n", loc);
		self->n_page = (self->n_page + 1) % self->n_cache_pages;
		size_t file_loc = (loc / self->cache_page_size)
			* self->cache_page_size;
		assert(file_loc <= LONG_MAX);
		fseek(self->f, (long) file_loc, SEEK_SET);
		self->cache_offsets[self->n_page] = file_loc;
		size_t n = fread(self->cache[self->n_page],
			self->cache_page_size, 1, self->f);
		if (n < self->cache_page_size)
		{
			if (ferror(self->f))
				return -LS_IO_ERROR;
		}

		char c = self->cache[self->n_page][
			loc - self->cache_offsets[self->n_page]
		];

		return (unsigned char) c;
	}
}

static void _usage(char const * argv0, bool short_text)
{
	fprintf(stderr, "usage: %s [OPTION] SCRIPT\n", argv0);

	if (short_text)
		return;

	fprintf(stderr, "Execute a Littlescript file\n");
	fprintf(stderr, "\n");
	fprintf(stderr, "  -h    display this help text\n");
	fprintf(stderr, "\n");
	fprintf(stderr, "  -c    print cache debug text\n");
	fprintf(stderr, "  -k    print tokenization and quit\n");
	fprintf(stderr, "  -p    print pool usage info\n");
	fprintf(stderr, "  -t    trace executing line numbers\n");
}

static void _tokenize(ls_t * ls)
{
	ls_value_t pool[100]; // for labels
	ls_init(ls, pool, sizeof pool / sizeof pool[0]);

	if (setjmp(ls->_error_jmp_buf))
	{
		uint16_t line = 0, col = 0;
		ls_translate_pc(ls, ls->_pc, &line, &col);
		fprintf(stderr, "error %d at %u:%u",
			(int) ls->_error, line, col);
		exit(EXIT_FAILURE);
	}

	for (;;)
	{
		ls_token_t tok = ls_lex(ls);

		switch (tok)
		{
		case LS_TOK_NUMBER:
			printf("NUMBER %"PRId32"\n", ls->_token.number);
			break;
		case LS_TOK_WORD:
			printf("WORD   %s\n", ls->_token.word);
			break;
		case LS_TOK_STR_LABEL:
			printf("SLABEL %s\n", ls->_token.word);
			break;
		case LS_TOK_NUM_LABEL:
			printf("NLABEL %"PRId32"\n", ls->_token.number);
			break;
		case LS_TOK_STRING:
			printf("STRING %"PRIu16" to %"PRIu16"\n",
				ls->_token.string[0],
				ls->_token.string[1]);
			break;
		case LS_TOK_COMMA:
			printf("COMMA  ,\n");
			break;
		case LS_TOK_NEWLINE:
			printf("NEWLINE\n");
			break;
		case LS_TOK_SEMICOLON:
			printf("SEMICO ;\n");
			break;
		case LS_TOK_INVALID:
			printf("INVALD\n");
			break;
		case LS_OP_ABS:		printf("OPERAT ABS\n"); break;
		case LS_OP_NOT:		printf("OPERAT NOT\n"); break;
		case LS_OP_AND:		printf("OPERAT AND\n"); break;
		case LS_OP_OR:		printf("OPERAT OR\n"); break;
		case LS_OP_XOR:		printf("OPERAT XOR\n"); break;
		case LS_OP_EQV:		printf("OPERAT EQV\n"); break;
		case LS_OP_IMP:		printf("OPERAT IMP\n"); break;
		case LS_OP_NEQ:		printf("OPERAT <>\n"); break;
		case LS_OP_LEQ:		printf("OPERAT <=\n"); break;
		case LS_OP_GEQ:		printf("OPERAT >=\n"); break;
		case LS_OP_LPAREN:	printf("OPERAT (\n"); break;
		case LS_OP_RPAREN:	printf("OPERAT )\n"); break;
		case LS_OP_POW:		printf("OPERAT ^\n"); break;
		case LS_OP_MUL:		printf("OPERAT *\n"); break;
		case LS_OP_DIV:		printf("OPERAT /\n"); break;
		case LS_OP_MOD:		printf("OPERAT MOD\n"); break;
		case LS_OP_NEG:		printf("OPERAT - (neg)\n"); break;
		case LS_OP_ADD:		printf("OPERAT +\n"); break;
		case LS_OP_SUB:		printf("OPERAT - (sub)\n"); break;
		case LS_OP_EQ:		printf("OPERAT =\n"); break;
		case LS_OP_LT:		printf("OPERAT <\n"); break;
		case LS_OP_GT:		printf("OPERAT >\n"); break;
		case LS_NO_OP:		printf("OPERAT --no oper--\n"); break;
		default:
			if (LS_TOK_KEYWORD(tok))
				printf("KEYWRD %s\n",
					ls_kwmap[tok - LS_KW_OFFSET].name
				);
			else
				printf("BADBAD\n"); // what the fuck?
			break;

		case LS_TOK_NONE:
			printf("END\n");
			return;
		}
	}
}

static void _line_trace_hook(ls_t * ls)
{
	uint16_t line = 0, col = 0;
	bool cachedebug_hold = cachedebug;
	cachedebug = false;
	ls_translate_pc(ls, ls->_pc, &line, &col);
	cachedebug = cachedebug_hold;
	fprintf(stderr, ">line %"PRIu16"\n", line);
}