Allow parenthesized expressions as macro arguments

This commit is contained in:
Rui Ueyama 2020-03-29 22:18:02 +09:00
parent dd4306cdd8
commit c7d7ce0f0c
2 changed files with 14 additions and 1 deletions

View File

@ -279,10 +279,17 @@ static void read_macro_definition(Token **rest, Token *tok) {
static MacroArg *read_macro_arg_one(Token **rest, Token *tok) {
Token head = {};
Token *cur = &head;
int level = 0;
while (!equal(tok, ",") && !equal(tok, ")")) {
while (level > 0 || (!equal(tok, ",") && !equal(tok, ")"))) {
if (tok->kind == TK_EOF)
error_tok(tok, "premature end of input");
if (equal(tok, "("))
level++;
else if (equal(tok, ")"))
level--;
cur = cur->next = copy_token(tok);
tok = tok->next;
}

View File

@ -216,6 +216,12 @@ int main() {
#define M8(x,y) x y
assert(9, M8(, 4+5), "M8(, 4+5)");
#define M8(x,y) x*y
assert(20, M8((2+3), 4), "M8((2+3), 4)");
#define M8(x,y) x*y
assert(12, M8((2,3), 4), "M8((2,3), 4)");
printf("OK\n");
return 0;
}