aboutsummaryrefslogtreecommitdiff
path: root/src/preprocess.c
diff options
context:
space:
mode:
authorMarvin Borner2021-05-13 12:03:43 +0200
committerMarvin Borner2021-05-13 12:03:43 +0200
commitf181a8f04dfdfd8829861e0d0d549f39e40081e6 (patch)
treed937ae185e4a0dd97fd61b65be0cee01ac85876b /src/preprocess.c
parent879663d7154201ace191425cbddb36dc18f39402 (diff)
New layout
Diffstat (limited to 'src/preprocess.c')
-rw-r--r--src/preprocess.c46
1 files changed, 46 insertions, 0 deletions
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 <assert.h>
+#include <log.h>
+#include <preprocess.h>
+#include <string.h>
+#include <sys/param.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 ", 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;
+}