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:
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
*/
// 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);
"#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");
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));
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) {
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;
}