From: Brendan Hansen Date: Wed, 13 Oct 2021 13:10:50 +0000 (-0500) Subject: more robust array literals X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=58e9e989fd7b36f211b2c11c67df706707ce9c87;p=onyx.git more robust array literals --- diff --git a/bin/onyx b/bin/onyx index 9ea715f6..30fc0b3b 100755 Binary files a/bin/onyx and b/bin/onyx differ diff --git a/src/astnodes.c b/src/astnodes.c index 1c82e901..a99d18d9 100644 --- a/src/astnodes.c +++ b/src/astnodes.c @@ -725,6 +725,23 @@ Type* resolve_expression_type(AstTyped* node) { return &basic_types[Basic_Kind_Type_Index]; } + if (node->kind == Ast_Kind_Array_Literal && node->type == NULL) { + AstArrayLiteral* al = (AstArrayLiteral *) node; + Type* elem_type = &basic_types[Basic_Kind_Void]; + if (bh_arr_length(al->values) > 0) { + elem_type = resolve_expression_type(al->values[0]); + } + + if (elem_type) { + node->type = type_make_array(context.ast_alloc, elem_type, bh_arr_length(al->values)); + node->flags |= Ast_Flag_Array_Literal_Typed; + + if (node->entity == NULL) { + add_entities_for_node(NULL, (AstNode *) node, NULL, NULL); + } + } + } + if (node->type == NULL) node->type = type_build_from_ast(context.ast_alloc, node->type_node); diff --git a/tests/array_struct_robustness.onyx b/tests/array_struct_robustness.onyx index 39d93bc0..7c587e2f 100644 --- a/tests/array_struct_robustness.onyx +++ b/tests/array_struct_robustness.onyx @@ -52,18 +52,18 @@ main :: (args: [] cstr) { println("Array of structs on a struct."); es : EntityStore; - es.positions = Vec2.[ - Vec2.{ 0, 0 }, - Vec2.{ 1, 1 }, - Vec2.{ 2, 4 }, - Vec2.{ 3, 9 }, + es.positions = .[ + .{ 0, 0 }, + .{ 1, 1 }, + .{ 2, 4 }, + .{ 3, 9 }, ]; - es.velocities = Vec2.[ - Vec2.{ 0, 0 }, - Vec2.{ 1, 1 }, - Vec2.{ 2, 8 }, - Vec2.{ 3, 27 }, + es.velocities = .[ + .{ 0, 0 }, + .{ 1, 1 }, + .{ 2, 8 }, + .{ 3, 27 }, ]; println(es.positions[3]);