From 681869239b45606fcef3cd1421fae17d82b6754b Mon Sep 17 00:00:00 2001 From: Lukas Krickl Date: Sat, 11 Nov 2023 18:31:41 +0100 Subject: [PATCH] Added special $$ macro variable --- doc/ulas.man | 3 +-- include/ulas.h | 7 +++++++ src/test.c | 5 +++-- src/ulas.c | 10 +++++++++- 4 files changed, 20 insertions(+), 5 deletions(-) diff --git a/doc/ulas.man b/doc/ulas.man index 60401c8..d3a6a20 100644 --- a/doc/ulas.man +++ b/doc/ulas.man @@ -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: diff --git a/include/ulas.h b/include/ulas.h index 7369010..4b55b7a 100644 --- a/include/ulas.h +++ b/include/ulas.h @@ -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); diff --git a/src/test.c b/src/test.c index 4968d4f..968e0c5 100644 --- a/src/test.c +++ b/src/test.c @@ -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"); diff --git a/src/ulas.c b/src/ulas.c index f6b04bf..2e9173d 100644 --- a/src/ulas.c +++ b/src/ulas.c @@ -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; } -- 2.30.2