diff options
| author | Ginger Bill <bill@gingerbill.org> | 2017-02-10 16:12:14 +0000 |
|---|---|---|
| committer | Ginger Bill <bill@gingerbill.org> | 2017-02-10 16:12:14 +0000 |
| commit | 73d6a55f5c96459d30eca5747d1458bcf6e9fec4 (patch) | |
| tree | 6351ed11cfbcf08c9a52498bbf8662083886fe89 /src/parser.c | |
| parent | f18ae89931526df578956e63dfab288920b59873 (diff) | |
Remove need for `type` keyword
Diffstat (limited to 'src/parser.c')
| -rw-r--r-- | src/parser.c | 87 |
1 files changed, 38 insertions, 49 deletions
diff --git a/src/parser.c b/src/parser.c index 7531412ef..3179509e7 100644 --- a/src/parser.c +++ b/src/parser.c @@ -250,7 +250,6 @@ AST_NODE_KIND(_ComplexStmtBegin, "", i32) \ AST_NODE_KIND(TypeMatchStmt, "type match statement", struct { \ Token token; \ AstNode *tag; \ - AstNode *var; \ AstNode *body; \ }) \ AST_NODE_KIND(DeferStmt, "defer statement", struct { Token token; AstNode *stmt; }) \ @@ -893,11 +892,10 @@ AstNode *ast_match_stmt(AstFile *f, Token token, AstNode *init, AstNode *tag, As } -AstNode *ast_type_match_stmt(AstFile *f, Token token, AstNode *tag, AstNode *var, AstNode *body) { +AstNode *ast_type_match_stmt(AstFile *f, Token token, AstNode *tag, AstNode *body) { AstNode *result = make_ast_node(f, AstNode_TypeMatchStmt); result->TypeMatchStmt.token = token; result->TypeMatchStmt.tag = tag; - result->TypeMatchStmt.var = var; result->TypeMatchStmt.body = body; return result; } @@ -1756,6 +1754,7 @@ AstNode *parse_operand(AstFile *f, bool lhs) { } else if (str_eq(name.string, str_lit("file"))) { return ast_basic_directive(f, token, name.string); } else if (str_eq(name.string, str_lit("line"))) { return ast_basic_directive(f, token, name.string); } else if (str_eq(name.string, str_lit("procedure"))) { return ast_basic_directive(f, token, name.string); + } else if (str_eq(name.string, str_lit("type"))) { return ast_helper_type(f, token, parse_type(f)); } else { operand = ast_tag_expr(f, token, name, parse_expr(f, false)); } @@ -2180,9 +2179,7 @@ AstNode *parse_value_decl(AstFile *f, AstNodeArray lhs) { bool is_mutable = true; if (allow_token(f, Token_Colon)) { - if (!allow_token(f, Token_type)) { - type = parse_type_attempt(f); - } + type = parse_type_attempt(f); } else if (f->curr_token.kind != Token_Eq && f->curr_token.kind != Token_Semicolon) { syntax_error(f->curr_token, "Expected a type separator `:` or `=`"); @@ -2528,10 +2525,16 @@ AstNode *parse_type_or_ident(AstFile *f) { return e; } - case Token_type: { - Token token = expect_token(f, Token_type); - AstNode *type = parse_type(f); - return ast_helper_type(f, token, type); + case Token_Hash: { + Token hash_token = expect_token(f, Token_Hash); + Token name = expect_token(f, Token_Ident); + String tag = name.string; + if (str_eq(tag, str_lit("type"))) { + AstNode *type = parse_type(f); + return ast_helper_type(f, hash_token, type); + } + syntax_error(name, "Expected `type` after #"); + return ast_bad_expr(f, hash_token, f->curr_token); } case Token_Pointer: { @@ -2941,8 +2944,7 @@ AstNode *parse_for_stmt(AstFile *f) { f->expr_level = -1; if (f->curr_token.kind != Token_Semicolon) { cond = parse_simple_stmt(f, true); - if (cond->kind == AstNode_AssignStmt && - cond->AssignStmt.op.kind == Token_in) { + if (cond->kind == AstNode_AssignStmt && cond->AssignStmt.op.kind == Token_in) { is_range = true; } } @@ -3034,6 +3036,7 @@ AstNode *parse_for_stmt(AstFile *f) { #endif } + AstNode *parse_case_clause(AstFile *f) { Token token = f->curr_token; AstNodeArray list = make_ast_node_array(f); @@ -3066,6 +3069,7 @@ AstNode *parse_type_case_clause(AstFile *f) { } + AstNode *parse_match_stmt(AstFile *f) { if (f->curr_proc == NULL) { syntax_error(f->curr_token, "You cannot use a match statement in the file scope"); @@ -3077,37 +3081,16 @@ AstNode *parse_match_stmt(AstFile *f) { AstNode *tag = NULL; AstNode *body = NULL; Token open, close; + bool is_type_match = false; - if (allow_token(f, Token_type)) { + if (f->curr_token.kind != Token_OpenBrace) { isize prev_level = f->expr_level; f->expr_level = -1; - AstNode *var = parse_ident(f); - expect_token_after(f, Token_in, "match type name"); - tag = parse_simple_stmt(f, false); - - f->expr_level = prev_level; - - open = expect_token(f, Token_OpenBrace); - AstNodeArray list = make_ast_node_array(f); - - while (f->curr_token.kind == Token_case || - f->curr_token.kind == Token_default) { - array_add(&list, parse_type_case_clause(f)); - } - - close = expect_token(f, Token_CloseBrace); - body = ast_block_stmt(f, list, open, close); - - tag = convert_stmt_to_expr(f, tag, str_lit("type match expression")); - return ast_type_match_stmt(f, token, tag, var, body); - } else { - if (f->curr_token.kind != Token_OpenBrace) { - isize prev_level = f->expr_level; - f->expr_level = -1; - if (f->curr_token.kind != Token_Semicolon) { - tag = parse_simple_stmt(f, false); - } + tag = parse_simple_stmt(f, true); + if (tag->kind == AstNode_AssignStmt && tag->AssignStmt.op.kind == Token_in) { + is_type_match = true; + } else { if (allow_token(f, Token_Semicolon)) { init = tag; tag = NULL; @@ -3115,28 +3098,33 @@ AstNode *parse_match_stmt(AstFile *f) { tag = parse_simple_stmt(f, false); } } - - f->expr_level = prev_level; } + f->expr_level = prev_level; + } + open = expect_token(f, Token_OpenBrace); + AstNodeArray list = make_ast_node_array(f); - open = expect_token(f, Token_OpenBrace); - AstNodeArray list = make_ast_node_array(f); - - while (f->curr_token.kind == Token_case || - f->curr_token.kind == Token_default) { + while (f->curr_token.kind == Token_case || + f->curr_token.kind == Token_default) { + if (is_type_match) { + array_add(&list, parse_type_case_clause(f)); + } else { array_add(&list, parse_case_clause(f)); } + } - close = expect_token(f, Token_CloseBrace); + close = expect_token(f, Token_CloseBrace); - body = ast_block_stmt(f, list, open, close); + body = ast_block_stmt(f, list, open, close); + if (!is_type_match) { tag = convert_stmt_to_expr(f, tag, str_lit("match expression")); return ast_match_stmt(f, token, init, tag, body); + } else { + return ast_type_match_stmt(f, token, tag, body); } } - AstNode *parse_defer_stmt(AstFile *f) { if (f->curr_proc == NULL) { syntax_error(f->curr_token, "You cannot use a defer statement in the file scope"); @@ -3328,6 +3316,7 @@ AstNode *parse_stmt(AstFile *f) { Token hash_token = expect_token(f, Token_Hash); Token name = expect_token(f, Token_Ident); String tag = name.string; + if (str_eq(tag, str_lit("import"))) { AstNode *cond = NULL; Token import_name = {0}; |