mirror of https://github.com/rui314/chibicc
Allow parenthesized expressions as macro arguments
This commit is contained in:
parent
dd4306cdd8
commit
c7d7ce0f0c
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue