authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-26 23:33:07-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-11-26 23:33:07-05:00
log1fba7f36960551fe0a12aa754c2d789c8784a8cc
tree7392244fa87b45413b9b57a651088ad4d97c3b00
parentb3ff28189ce8f28abf077dcf343d0d95bf5645c3

IR: add inline goto


2 files changed, 39 insertions(+), 11 deletions(-)

doc/langref.md+1-1
......@@ -147,7 +147,7 @@ PrimaryExpression = Number | String | CharLiteral | KeywordLiteral | GroupedExpr
147147
148148ArrayType = "[" option(Expression) "]" option("const") TypeExpr
149149
150GotoExpression = "goto" Symbol
150GotoExpression = option("inline") "goto" Symbol
151151
152152GroupedExpression = "(" Expression ")"
153153
src/parser.cpp+38-10
......@@ -583,6 +583,40 @@ static AstNode *ast_parse_asm_expr(ParseContext *pc, size_t *token_index, bool m
583583 return node;
584584}
585585
586/*
587GotoExpression = option("inline") "goto" Symbol
588*/
589static AstNode *ast_parse_goto_expr(ParseContext *pc, size_t *token_index, bool mandatory) {
590 Token *first_token = &pc->tokens->at(*token_index);
591 Token *goto_token;
592 bool is_inline;
593 if (first_token->id == TokenIdKeywordInline) {
594 is_inline = true;
595 goto_token = &pc->tokens->at(*token_index + 1);
596 if (goto_token->id == TokenIdKeywordGoto) {
597 *token_index += 2;
598 } else if (mandatory) {
599 ast_expect_token(pc, first_token, TokenIdKeywordGoto);
600 } else {
601 return nullptr;
602 }
603 } else if (first_token->id == TokenIdKeywordGoto) {
604 goto_token = first_token;
605 is_inline = false;
606 *token_index += 1;
607 } else if (mandatory) {
608 ast_expect_token(pc, first_token, TokenIdKeywordGoto);
609 } else {
610 return nullptr;
611 }
612
613 AstNode *node = ast_create_node(pc, NodeTypeGoto, goto_token);
614
615 Token *dest_symbol = ast_eat_token(pc, token_index, TokenIdSymbol);
616 node->data.goto_expr.name = token_buf(dest_symbol);
617 node->data.goto_expr.is_inline = is_inline;
618 return node;
619}
586620/*
587621PrimaryExpression = "Number" | "String" | "CharLiteral" | KeywordLiteral | GroupedExpression | GotoExpression | BlockExpression | "Symbol" | ("@" "Symbol" FnCallExpression) | ArrayType | FnProto | AsmExpression | ("error" "." "Symbol")
588622KeywordLiteral = "true" | "false" | "null" | "break" | "continue" | "undefined" | "zeroes" | "error" | "type" | "this"
......@@ -672,18 +706,12 @@ static AstNode *ast_parse_primary_expr(ParseContext *pc, size_t *token_index, bo
672706 AstNode *node = ast_create_node(pc, NodeTypeSymbol, token);
673707 node->data.symbol_expr.symbol = token_buf(token);
674708 return node;
675 } else if (token->id == TokenIdKeywordGoto) {
676 AstNode *node = ast_create_node(pc, NodeTypeGoto, token);
677 *token_index += 1;
678
679 Token *dest_symbol = &pc->tokens->at(*token_index);
680 *token_index += 1;
681 ast_expect_token(pc, dest_symbol, TokenIdSymbol);
682
683 node->data.goto_expr.name = token_buf(dest_symbol);
684 return node;
685709 }
686710
711 AstNode *goto_node = ast_parse_goto_expr(pc, token_index, false);
712 if (goto_node)
713 return goto_node;
714
687715 AstNode *grouped_expr_node = ast_parse_grouped_expr(pc, token_index, false);
688716 if (grouped_expr_node) {
689717 return grouped_expr_node;