more robust array literals
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 13 Oct 2021 13:10:50 +0000 (08:10 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Wed, 13 Oct 2021 13:10:50 +0000 (08:10 -0500)
bin/onyx
src/astnodes.c
tests/array_struct_robustness.onyx

index 9ea715f6dd1ef1da1900ce37923ea8157e7f24c1..30fc0b3b28b6beee1169e288241533b2805c6392 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 1c82e901a428a8dafdf6eef2f01a1b7684f270ee..a99d18d9aa19b18ecb344914f5554a1327f7958c 100644 (file)
@@ -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);
 
index 39d93bc0477a2b6ddabc50836e10b63a754f1951..7c587e2fb543c8c3310b08fff114dd65f84a4887 100644 (file)
@@ -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]);