}
}
+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;
+}
+
// <symbol>
// '^' <type>
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;
}
}
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;
}
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<i32, f32>(1234, 5678.0)
-PolyStruct<f32, i32>(1234.0, 5678)
+PolyStruct<i32, f32>(1234, 5678.0000)
+PolyStruct<f32, i32>(1234.0000, 5678)
Testing a polymorphic struct with default values.
-PolyStruct<i32, f32>(1234, 5678.0)
+PolyStruct<i32, f32>(1234, 5678.0000)
Testing a union with use.
Testing a polymorphic union with use.
16 == 16
-0, 5678.0
+0, 5678.0000