Added special $$ macro variable
authorLukas Krickl <lukas@krickl.dev>
Sat, 11 Nov 2023 17:31:41 +0000 (18:31 +0100)
committerLukas Krickl <lukas@krickl.dev>
Sat, 11 Nov 2023 17:31:41 +0000 (18:31 +0100)
doc/ulas.man
include/ulas.h
src/test.c
src/ulas.c

index 60401c8b8f5971a2242ee3925348231feff82f72..d3a6a207249d40f638f97a353250020459dae011 100644 (file)
@@ -31,8 +31,7 @@ the define or macro will be expanded.
 Withing preprocessor macros 
 - $0 will expand to the literal argument string 
 - $1 to $9 will expand to the comma separated arguments of the macro 
-- $$ will expand to an internal macro call counter which can be used to make labels 
-  that are expanded by a macro unique.
+- $$ will insert a unique numeric literal 
 
 Macros:
 
index 7369010fee3fed5b34cb794ca0cfbe24f1b97bec..4b55b7af4108a1377614c5fa70875fcd5116ecf5 100644 (file)
@@ -98,10 +98,16 @@ struct ulas {
 
   char *filename;
   size_t line;
+
+  // internal counter
+  // used whenever a new unique number might be needed
+  int icntr;
 };
 
 extern struct ulas ulas;
 
+int ulas_icntr(void);
+
 /**
  * Preproc
  */
@@ -229,6 +235,7 @@ char *ulas_strndup(const char *src, size_t n);
 // consumed or -1 on error
 // returns 0 when no more tokens can be read
 int ulas_tok(struct ulas_str *dst, const char **out_line, size_t n);
+
 int ulas_tokuntil(struct ulas_str *dst, char c, const char **out_line,
                   size_t n);
 
index 4968d4f08a7c9a05a869e425e73fc29984278ce7..968e0c50fa5560c3a24be8fcdba43989f6f851d1 100644 (file)
@@ -107,8 +107,9 @@ void test_preproc(void) {
                  "#define test 123\ntest\n#undefine test\ntest");
 
   // macro
-  assert_preproc("  line p1 1\n  line p2 2\n  line p3 3 p1, p2, p3\n", 0,
-                 "#macro test\n  line $1 1\n  line $2 2\n  line $3 3 "
+  assert_preproc("  line p1 1 label01\n  line p2 2\n  line p3 3 p1, p2, p3\n",
+                 0,
+                 "#macro test\n  line $1 1 label$$$$\n  line $2 2\n  line $3 3 "
                  "$0\n#endmacro\ntest p1, p2, p3");
   assert_preproc("test macro with no args\n", 0,
                  "#macro test\ntest macro with no args\n#endmacro\ntest");
index f6b04bfe3c67720e23d9e44136c6c8f101a4c7d7..2e9173dbd2ee8d3b6076c3db46a9e0fb9803e0c7 100644 (file)
@@ -25,6 +25,8 @@ void ulas_init(struct ulas_config cfg) {
   memset(&ulas, 0, sizeof(ulas));
 }
 
+int ulas_icntr(void) { return ulas.icntr++; }
+
 struct ulas_config ulas_cfg_from_env(void) {
   struct ulas_config cfg;
   memset(&cfg, 0, sizeof(cfg));
@@ -286,6 +288,13 @@ char *ulas_preprocexpand(struct ulas_preproc *pp, const char *raw_line,
             ulas_strensr(&pp->line, pp->line.maxlen + linelen);
             strncat(pp->line.buf, line, linelen);
             found = 1;
+          } else if (linelen && strncmp("$$", pp->macrobuf.buf,
+                                        pp->macrobuf.maxlen) == 0) {
+            char numbuf[128];
+            sprintf(numbuf, "%x", ulas_icntr());
+            ulas_strensr(&pp->line, pp->line.maxlen + 128);
+            strncat(pp->line.buf, numbuf, 128);
+            found = 1;
           }
 
           if (!found) {
@@ -469,7 +478,6 @@ found:
         return -1;
       }
       struct ulas_ppdef *def = NULL;
-      printf("name: %s\n", pp->tok.buf);
       while ((def = ulas_preprocgetdef(pp, pp->tok.buf, pp->tok.maxlen))) {
         def->undef = 1;
       }