mirror of https://github.com/rui314/chibicc
Allow for-loops to define local variables
This commit is contained in:
parent
736232f3d6
commit
a4fea2ba3e
10
parse.c
10
parse.c
|
@ -577,7 +577,14 @@ static Node *stmt(Token **rest, Token *tok) {
|
|||
Node *node = new_node(ND_FOR, tok);
|
||||
tok = skip(tok->next, "(");
|
||||
|
||||
node->init = expr_stmt(&tok, tok);
|
||||
enter_scope();
|
||||
|
||||
if (is_typename(tok)) {
|
||||
Type *basety = declspec(&tok, tok, NULL);
|
||||
node->init = declaration(&tok, tok, basety);
|
||||
} else {
|
||||
node->init = expr_stmt(&tok, tok);
|
||||
}
|
||||
|
||||
if (!equal(tok, ";"))
|
||||
node->cond = expr(&tok, tok);
|
||||
|
@ -588,6 +595,7 @@ static Node *stmt(Token **rest, Token *tok) {
|
|||
tok = skip(tok, ")");
|
||||
|
||||
node->then = stmt(rest, tok);
|
||||
leave_scope();
|
||||
return node;
|
||||
}
|
||||
|
||||
|
|
|
@ -24,6 +24,9 @@ int main() {
|
|||
ASSERT(5, ({ int i=2, j=3; (i=5,j)=6; i; }));
|
||||
ASSERT(6, ({ int i=2, j=3; (i=5,j)=6; j; }));
|
||||
|
||||
ASSERT(55, ({ int j=0; for (int i=0; i<=10; i=i+1) j=j+i; j; }));
|
||||
ASSERT(3, ({ int i=3; int j=0; for (int i=0; i<=10; i=i+1) j=j+i; i; }));
|
||||
|
||||
printf("OK\n");
|
||||
return 0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue