Added preproc def lookup function for defined directives
authorLukas Krickl <lukas@krickl.dev>
Sat, 11 Nov 2023 13:37:05 +0000 (14:37 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 11 Nov 2023 13:37:05 +0000 (14:37 +0100)
include/ulas.h
src/ulas.c

index 08cddfeced9451e7262799c7ec9929e7d966b231..ebc779c908655598aa72ec7ab791184fd32954d0 100644 (file)
@@ -108,6 +108,7 @@ extern struct ulas ulas;
 enum ulas_ppdirs {
   ULAS_PPDIR_NONE = 1,
   ULAS_PPDIR_DEF,
+  ULAS_PPDIR_UNDEF,
   ULAS_PPDIR_MACRO,
   ULAS_PPDIR_ENDMACRO,
   ULAS_PPDIR_IFDEF,
index 281ca49d6f8597341cd1f76f6eb561a1e3d55ab3..30411c10d0d8b03243ce76b179b9e7ab87a53fff 100644 (file)
@@ -186,6 +186,18 @@ void ulas_strfree(struct ulas_str *s) {
   }
 }
 
+struct ulas_ppdef *ulas_preprocgetdef(struct ulas_preproc *pp, const char *name,
+                                      size_t maxlen) {
+  for (size_t i = 0; i < pp->defslen; i++) {
+    struct ulas_ppdef *def = &pp->defs[i];
+    if (!def->undef && strncmp(def->name, name, maxlen) == 0) {
+      return def;
+    }
+  }
+
+  return NULL;
+}
+
 char *ulas_preprocexpand(struct ulas_preproc *pp, const char *raw_line,
                          size_t *n) {
   const char *praw_line = raw_line;
@@ -198,12 +210,9 @@ char *ulas_preprocexpand(struct ulas_preproc *pp, const char *raw_line,
   // only expand macros if they match toks[0] though!
   // otherwise memcpy the read bytes 1:1 into the new string
   while ((read = ulas_tok(&pp->tok, &praw_line, *n))) {
-    for (size_t i = 0; i < pp->defslen; i++) {
-      struct ulas_ppdef *def = &pp->defs[i];
-      if (strncmp(def->name, pp->tok.buf, pp->tok.maxlen) != 0) {
-        continue;
-      }
-
+    struct ulas_ppdef *def =
+        ulas_preprocgetdef(pp, pp->tok.buf, pp->tok.maxlen);
+    if (def) {
       // if so... expand now and leave
       switch (def->type) {
       case ULAS_PPDEF:
@@ -278,16 +287,12 @@ char *ulas_preprocexpand(struct ulas_preproc *pp, const char *raw_line,
       }
       }
 
-      goto found;
+    } else {
+      // if not found: copy everythin from prev to the current raw_line point -
+      // tok lenght -> this keeps the line in-tact as is
+      ulas_strensr(&pp->line, (*n) + 1);
+      strncat(pp->line.buf, praw_line - read, read);
     }
-    // if not found: copy everythin from prev to the current raw_line point -
-    // tok lenght -> this keeps the line in-tact as is
-    ulas_strensr(&pp->line, (*n) + 1);
-    strncat(pp->line.buf, praw_line - read, read);
-
-    // bit of a hack to allow labels at the end of a compound statement
-  found:
-    continue;
   }
 
 end: