aboutsummaryrefslogtreecommitdiff
path: root/src/preprocess.c
blob: 8d9394a8052d1c92ba61ff406c904418053da765 (plain) (blame)
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
#include <assert.h>
#include <math.h>
#include <stddef.h>
#include <string.h>

#include <log.h>
#include <preprocess.h>

static void preprocess_erase(struct ctx *ctx, size_t start)
{
	assert(ctx->raw[start] == '#');

	for (size_t i = start; i < ctx->size; i++) {
		char cur = ctx->raw[i];
		if (cur == '\n' || cur == '\0')
			break;

		ctx->raw[i] = ' '; // Spaces get skipped by tokenizer anyways
	}
}

void preprocess(struct ctx *ctx)
{
	for (size_t i = 0; i < ctx->size; i++) {
		const char cur = ctx->raw[i];

		ctx->column++;

		if (cur == '\n') {
			ctx->line++;
			ctx->column = 0;
			continue;
		} else if (cur == '\0') {
			break;
		} else if (cur == '#' && ctx->column == 1) {
			if (strncmp(ctx->raw + i + 1, "inc ", fmin(4, ctx->size - i)) == 0) {
				// TODO: Add include features
			} else if (*(ctx->raw + i + 1) == '#') {
				// Comment
			} else {
				errln(ctx, "Invalid preprocessing directive");
			}
			preprocess_erase(ctx, i);
		}
	}

	ctx->data = ctx->raw;
	ctx->line = 0;
	ctx->column = 0;
}