From f181a8f04dfdfd8829861e0d0d549f39e40081e6 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Thu, 13 May 2021 12:03:43 +0200 Subject: New layout --- src/preprocess.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/preprocess.c (limited to 'src/preprocess.c') diff --git a/src/preprocess.c b/src/preprocess.c new file mode 100644 index 0000000..673bd20 --- /dev/null +++ b/src/preprocess.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include +#include + +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 ", MIN(4, ctx->size - i)) == 0) { + // TODO: Add include features + } else { + errln(ctx, "Invalid preprocessing directive"); + } + preprocess_erase(ctx, i); + } + } + + ctx->data = ctx->raw; + ctx->line = 0; + ctx->column = 0; +} -- cgit v1.2.3