From: Brendan Hansen Date: Fri, 22 Jan 2021 15:46:16 +0000 (-0600) Subject: made the 'proc' keyword optional in procedure types X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=d875002e3972e16c3bacd720d86e49bb4b016476;p=onyx.git made the 'proc' keyword optional in procedure types --- diff --git a/bin/onyx b/bin/onyx index 310ed167..cbc21488 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/onyx.exe b/onyx.exe index 41c79d7f..be1ae8ce 100644 Binary files a/onyx.exe and b/onyx.exe differ diff --git a/src/onyxparser.c b/src/onyxparser.c index 1b5054f6..0dda66fa 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -1557,6 +1557,48 @@ static AstType* parse_compound_type(OnyxParser* parser) { } } +static AstType* parse_function_type(OnyxParser* parser, OnyxToken* proc_token) { + bh_arr(AstType *) params = NULL; + bh_arr_new(global_scratch_allocator, params, 4); + bh_arr_set_length(params, 0); + + expect_token(parser, '('); + while (parser->curr->type != ')') { + if (parser->hit_unexpected_token) return NULL; + + if (peek_token(1)->type == ':') { + expect_token(parser, Token_Type_Symbol); + expect_token(parser, ':'); + } + + AstType* param_type = parse_type(parser); + bh_arr_push(params, param_type); + + if (parser->curr->type != ')') + expect_token(parser, ','); + } + consume_token(parser); + + AstType* return_type = (AstType *) &basic_type_void; + if (parser->curr->type == Token_Type_Right_Arrow) { + consume_token(parser); + return_type = parse_type(parser); + } + + i64 param_count = bh_arr_length(params); + AstFunctionType* new = onyx_ast_node_new(parser->allocator, + sizeof(AstFunctionType) + sizeof(AstType*) * param_count, + Ast_Kind_Function_Type); + new->token = proc_token; + new->param_count = param_count; + new->return_type = return_type; + + if (param_count > 0) + fori (i, 0, param_count) new->params[i] = params[i]; + + return (AstType *) new; +} + // // '^' static AstType* parse_type(OnyxParser* parser) { @@ -1610,46 +1652,7 @@ static AstType* parse_type(OnyxParser* parser) { case Token_Type_Keyword_Proc: { OnyxToken* proc_token = expect_token(parser, Token_Type_Keyword_Proc); - - bh_arr(AstType *) params = NULL; - bh_arr_new(global_scratch_allocator, params, 4); - bh_arr_set_length(params, 0); - - expect_token(parser, '('); - while (parser->curr->type != ')') { - if (parser->hit_unexpected_token) return root; - - if (peek_token(1)->type == ':') { - expect_token(parser, Token_Type_Symbol); - expect_token(parser, ':'); - } - - AstType* param_type = parse_type(parser); - bh_arr_push(params, param_type); - - if (parser->curr->type != ')') - expect_token(parser, ','); - } - consume_token(parser); - - AstType* return_type = (AstType *) &basic_type_void; - if (parser->curr->type == Token_Type_Right_Arrow) { - consume_token(parser); - return_type = parse_type(parser); - } - - i64 param_count = bh_arr_length(params); - AstFunctionType* new = onyx_ast_node_new(parser->allocator, - sizeof(AstFunctionType) + sizeof(AstType*) * param_count, - Ast_Kind_Function_Type); - new->token = proc_token; - new->param_count = param_count; - new->return_type = return_type; - - if (param_count > 0) - fori (i, 0, param_count) new->params[i] = params[i]; - - *next_insertion = (AstType *) new; + *next_insertion = parse_function_type(parser, proc_token); next_insertion = NULL; break; } @@ -1727,12 +1730,18 @@ static AstType* parse_type(OnyxParser* parser) { } case '(': { - expect_token(parser, '('); - - *next_insertion = parse_compound_type(parser); - next_insertion = NULL; + OnyxToken* matching = find_matching_paren(parser->curr); - expect_token(parser, ')'); + if ((matching + 1)->type == Token_Type_Right_Arrow) { + *next_insertion = parse_function_type(parser, parser->curr); + + } else { + expect_token(parser, '('); + *next_insertion = parse_compound_type(parser); + expect_token(parser, ')'); + } + + next_insertion = NULL; break; } diff --git a/tests/multiple_returns_robustness b/tests/multiple_returns_robustness index 64ec3eb1..5fcb6e36 100644 --- a/tests/multiple_returns_robustness +++ b/tests/multiple_returns_robustness @@ -3,4 +3,4 @@ 110 -10 to 200 0 1 2 -3.0 4.0 5.0 +3.0000 4.0000 5.0000 diff --git a/tests/named_arguments_test b/tests/named_arguments_test index 79435032..2942784d 100644 --- a/tests/named_arguments_test +++ b/tests/named_arguments_test @@ -1,14 +1,14 @@ x is 10 -y is 20.0 +y is 20.0000 x is 20 -y is 10.0 +y is 10.0000 ========================= 10 10 20 30 -10.0 -10.0 20.0 30.0 +10.0000 +10.0000 20.0000 30.0000 ========================= @@ -20,7 +20,7 @@ MATCHED Z: 10 ========================= 10 20 -30.0 40.0 50.0 +30.0000 40.0000 50.0000 ========================= diff --git a/tests/operator_overload b/tests/operator_overload index bad108f0..333bd6b1 100644 --- a/tests/operator_overload +++ b/tests/operator_overload @@ -1,7 +1,7 @@ -(2.0, 4.0) -(2.0, 2.0) -(-3.0, 2.0) -(13.0, 24.0) -2.0 3.0 5.0 7.0 13.0 24.0 -(-5.0, 6.0) +(2.0000, 4.0000) +(2.0000, 2.0000) +(-3.0000, 2.0000) +(13.0000, 24.0000) +2.0000 3.0000 5.0000 7.0000 13.0000 24.0000 +(-5.0000, 6.0000) World! diff --git a/tests/polymorphic_array_lengths b/tests/polymorphic_array_lengths index 6189705e..389fe62c 100644 --- a/tests/polymorphic_array_lengths +++ b/tests/polymorphic_array_lengths @@ -1,20 +1,20 @@ -1 2 3 4 5 0.0 -1.0 +1 2 3 4 5 0.0000 +1.0000 1.4142 1.7320 -2.0 +2.0000 2.2360 2.4494 2.6457 2.8284 -3.0 +3.0000 3.1622 3.3166 3.4641 3.6055 3.7416 3.8729 -4.0 +4.0000 4.1231 4.2426 4.3588 diff --git a/tests/struct_robustness b/tests/struct_robustness index 75163735..894dcadb 100644 --- a/tests/struct_robustness +++ b/tests/struct_robustness @@ -7,22 +7,22 @@ Testing a simple union. Testing a struct with default values. -DefaultedStruct(0, 1.0, 2, 3.0) -DefaultedStruct(3, 1.0, 2, 0.0) +DefaultedStruct(0, 1.0000, 2, 3.0000) +DefaultedStruct(3, 1.0000, 2, 0.0000) Testing a struct with `use`. -StructWithUse(1234, (1.0, 2.0), 5678) -StructWithUse(1234, (1.0, 2.0), 5678) +StructWithUse(1234, (1.0000, 2.0000), 5678) +StructWithUse(1234, (1.0000, 2.0000), 5678) Testing a polymorphic struct. -PolyStruct(1234, 5678.0) -PolyStruct(1234.0, 5678) +PolyStruct(1234, 5678.0000) +PolyStruct(1234.0000, 5678) Testing a polymorphic struct with default values. -PolyStruct(1234, 5678.0) +PolyStruct(1234, 5678.0000) Testing a union with use. @@ -37,4 +37,4 @@ PolyUnion(1234) Testing a polymorphic union with use. 16 == 16 -0, 5678.0 +0, 5678.0000