update for loop syntax

it matches more closely the %% binary operator syntax

See #51
This commit is contained in:
Andrew Kelley
2016-02-05 17:15:19 -07:00
parent 4208435f66
commit 4339d55562
8 changed files with 28 additions and 22 deletions

View File

@@ -1797,7 +1797,7 @@ static AstNode *ast_parse_symbol(ParseContext *pc, int *token_index) {
}
/*
ForExpression : token(For) token(LParen) Symbol token(Comma) Expression option(token(Comma) token(Symbol)) token(RParen) Expression
ForExpression = "for" "(" Expression ")" option("|" "Symbol" option("," "Symbol") "|") Expression
*/
static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mandatory) {
Token *token = &pc->tokens->at(*token_index);
@@ -1814,18 +1814,24 @@ static AstNode *ast_parse_for_expr(ParseContext *pc, int *token_index, bool mand
AstNode *node = ast_create_node(pc, NodeTypeForExpr, token);
ast_eat_token(pc, token_index, TokenIdLParen);
node->data.for_expr.elem_node = ast_parse_symbol(pc, token_index);
ast_eat_token(pc, token_index, TokenIdComma);
node->data.for_expr.array_expr = ast_parse_expression(pc, token_index, true);
Token *comma = &pc->tokens->at(*token_index);
if (comma->id == TokenIdComma) {
*token_index += 1;
node->data.for_expr.index_node = ast_parse_symbol(pc, token_index);
}
ast_eat_token(pc, token_index, TokenIdRParen);
Token *maybe_bar = &pc->tokens->at(*token_index);
if (maybe_bar->id == TokenIdBinOr) {
*token_index += 1;
node->data.for_expr.elem_node = ast_parse_symbol(pc, token_index);
Token *maybe_comma = &pc->tokens->at(*token_index);
if (maybe_comma->id == TokenIdComma) {
*token_index += 1;
node->data.for_expr.index_node = ast_parse_symbol(pc, token_index);
}
ast_eat_token(pc, token_index, TokenIdBinOr);
}
node->data.for_expr.body = ast_parse_expression(pc, token_index, true);
normalize_parent_ptrs(node);