diff options
| author | Andrew Kelley <superjoe30@gmail.com> | 2016-12-07 00:22:14 -0500 |
|---|---|---|
| committer | Andrew Kelley <superjoe30@gmail.com> | 2016-12-07 00:22:14 -0500 |
| commit | c0b2fe4d6cf3f8c6aa07899863861d506f588ef5 (patch) | |
| tree | d48eb21337f5da54b535bbc6be22290a3e3bd00a /src/parser.cpp | |
| parent | 7d9fa01ed54e99368f7351dbf1193e4f79ddf806 (diff) | |
| download | zig-c0b2fe4d6cf3f8c6aa07899863861d506f588ef5.tar.gz zig-c0b2fe4d6cf3f8c6aa07899863861d506f588ef5.zip | |
IR: add error for assigning runtime value to inline var
Diffstat (limited to 'src/parser.cpp')
| -rw-r--r-- | src/parser.cpp | 32 |
1 files changed, 24 insertions, 8 deletions
diff --git a/src/parser.cpp b/src/parser.cpp index 9e34bc55ab..2f4824ad39 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -1306,25 +1306,40 @@ static AstNode *ast_parse_else(ParseContext *pc, size_t *token_index, bool manda /* IfExpression : IfVarExpression | IfBoolExpression -IfBoolExpression : token(If) token(LParen) Expression token(RParen) Expression option(Else) -IfVarExpression = "if" "(" ("const" | "var") option("*") "Symbol" option(":" TypeExpr) "?=" Expression ")" Expression Option(Else) +IfBoolExpression = option("inline") "if" "(" Expression ")" Expression option(Else) +IfVarExpression = option("inline") "if" "(" ("const" | "var") option("*") Symbol option(":" TypeExpr) "?=" Expression ")" Expression Option(Else) */ static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool mandatory) { - Token *if_tok = &pc->tokens->at(*token_index); - if (if_tok->id != TokenIdKeywordIf) { - if (mandatory) { + Token *first_token = &pc->tokens->at(*token_index); + Token *if_tok; + + bool is_inline; + if (first_token->id == TokenIdKeywordInline) { + if_tok = &pc->tokens->at(*token_index + 1); + if (if_tok->id == TokenIdKeywordIf) { + is_inline = true; + *token_index += 2; + } else if (mandatory) { ast_expect_token(pc, if_tok, TokenIdKeywordIf); } else { return nullptr; } + } else if (first_token->id == TokenIdKeywordIf) { + if_tok = first_token; + is_inline = false; + *token_index += 1; + } else if (mandatory) { + ast_expect_token(pc, first_token, TokenIdKeywordIf); + } else { + return nullptr; } - *token_index += 1; ast_eat_token(pc, token_index, TokenIdLParen); Token *token = &pc->tokens->at(*token_index); if (token->id == TokenIdKeywordConst || token->id == TokenIdKeywordVar) { AstNode *node = ast_create_node(pc, NodeTypeIfVarExpr, if_tok); + node->data.if_var_expr.is_inline = is_inline; node->data.if_var_expr.var_decl.is_const = (token->id == TokenIdKeywordConst); *token_index += 1; @@ -1362,6 +1377,7 @@ static AstNode *ast_parse_if_expr(ParseContext *pc, size_t *token_index, bool ma return node; } else { AstNode *node = ast_create_node(pc, NodeTypeIfBoolExpr, if_tok); + node->data.if_bool_expr.is_inline = is_inline; node->data.if_bool_expr.condition = ast_parse_expression(pc, token_index, true); ast_eat_token(pc, token_index, TokenIdRParen); node->data.if_bool_expr.then_block = ast_parse_expression(pc, token_index, true); @@ -1561,12 +1577,12 @@ static AstNode *ast_parse_while_expr(ParseContext *pc, size_t *token_index, bool bool is_inline; if (first_token->id == TokenIdKeywordInline) { - is_inline = true; while_token = &pc->tokens->at(*token_index + 1); if (while_token->id == TokenIdKeywordWhile) { + is_inline = true; *token_index += 2; } else if (mandatory) { - ast_expect_token(pc, first_token, TokenIdKeywordWhile); + ast_expect_token(pc, while_token, TokenIdKeywordWhile); } else { return nullptr; } |
