void test_preproc(void) {
TESTBEGIN("preproc");
+ // no directive
assert_preproc(" test line", 0, " test line");
+
+ // define
assert_preproc(" 123", 0, " #define test 123\ntest");
assert_preproc("", -1, " #define 1test 123\n");
+ assert_preproc("", -1, " #define\n");
assert_preproc("this is a 123 for defs", 0,
" #define test 123\nthis is a test for defs");
+
+ // undefined
+ assert_preproc(" 123\ntest", 0,
+ "#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 "
"$0\n#endmacro\ntest p1, p2, p3");
memset(pp->line.buf, 0, pp->line.maxlen);
int read = 0;
+ int first_tok = 1;
// go through all tokens, see if a define matches the token,
// if so expand it
// 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))) {
+ // if it is the first token, and it begins with a # do not process at all!
+ if (first_tok && pp->tok.buf[0] == ULAS_TOK_PREPROC_BEGIN) {
+ ulas_strensr(&pp->line, (*n) + 1);
+ strncat(pp->line.buf, raw_line, *n);
+ break;
+ }
+ first_tok = 0;
+
struct ulas_ppdef *def =
ulas_preprocgetdef(pp, pp->tok.buf, pp->tok.maxlen);
if (def) {
char *line = ulas_preprocexpand(pp, raw_line, &n);
const char *pline = line;
- const char *dirstrs[] = {ULAS_PPSTR_DEF,
- ULAS_PPSTR_MACRO,
- ULAS_PPSTR_IFDEF,
- ULAS_PPSTR_IFNDEF,
- ULAS_PPSTR_ENDIF,
- ULAS_PPSTR_ENDMACRO,
- NULL};
+ const char *dirstrs[] = {ULAS_PPSTR_DEF, ULAS_PPSTR_MACRO,
+ ULAS_PPSTR_IFDEF, ULAS_PPSTR_IFNDEF,
+ ULAS_PPSTR_ENDIF, ULAS_PPSTR_ENDMACRO,
+ ULAS_PPSTR_UNDEF, NULL};
enum ulas_ppdirs dirs[] = {ULAS_PPDIR_DEF, ULAS_PPDIR_MACRO,
ULAS_PPDIR_IFDEF, ULAS_PPDIR_IFNDEF,
- ULAS_PPDIR_ENDIF, ULAS_PPDIR_ENDMACRO};
+ ULAS_PPDIR_ENDIF, ULAS_PPDIR_ENDMACRO,
+ ULAS_PPDIR_UNDEF};
enum ulas_ppdirs found_dir = ULAS_PPDIR_NONE;
case ULAS_PPDIR_ENDIF:
// TODO: implement
ULASPANIC("Preproc directive is not implemented!\n");
+ case ULAS_PPDIR_UNDEF: {
+ if (ulas_tok(&pp->tok, &pline, n) == 0) {
+ ULASERR("Expected name for #undef\n");
+ 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;
+ }
+
break;
+ }
default:
// this should not happen!
break;
// the end of a directive should have no further tokens!
if (ulas_preprochasstray(pp, pline, n)) {
+ ULASERR("Stray token at end of preprocessor directive!");
return -1;
}
dirdone: