added anonymous array literals and uncovered bugs
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 22 Feb 2021 22:15:43 +0000 (16:15 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Mon, 22 Feb 2021 22:15:43 +0000 (16:15 -0600)
bin/onyx
include/onyxastnodes.h
src/onyxastnodes.c
src/onyxchecker.c
src/onyxsymres.c

index dce1879a71799d12bf4081182ffc287702019f8b..b9065107f876dd2396d7855f36931fef582dacf8 100755 (executable)
Binary files a/bin/onyx and b/bin/onyx differ
index 40150f2c2c661e13b2652057c7e54d73c6e4bf60..e36cbf5bf9d201639791bbba77d11cfc024ce021 100644 (file)
@@ -218,6 +218,8 @@ typedef enum AstFlags {
     Ast_Flag_From_Polymorphism     = BH_BIT(23),
 
     Ast_Flag_Incomplete_Body       = BH_BIT(24),
+
+    Ast_Flag_Array_Literal_Typed   = BH_BIT(25),
 } AstFlags;
 
 typedef enum UnaryOp {
index 98719b8dac6e1e99e1ed68e328cb4f12c101fddc..606770b4b519468ba15402f4f5f7da24df55b0c8 100644 (file)
@@ -433,6 +433,15 @@ b32 type_check_or_auto_cast(AstTyped** pnode, Type* type) {
         return 1;
     }
 
+    if (node->kind == Ast_Kind_Array_Literal && node->type_node == NULL) {
+        i32 value_count = bh_arr_length(((AstArrayLiteral *) node)->values);
+        node->type = type; // type_make_array(context.ast_alloc, type, value_count);
+        node->flags |= Ast_Flag_Array_Literal_Typed;
+
+        add_entities_for_node(NULL, (AstNode *) node, NULL, NULL);
+        return 1;
+    }
+
     if (node->kind == Ast_Kind_Polymorphic_Proc) {
         AstFunction* func = polymorphic_proc_lookup((AstPolyProc *) node, PPLM_By_Function_Type, type, node->token);
         if (func == NULL) return 0;
index f2f1902d604dee80bc9cc8d26492bd6ea26380e8..328984c0d021bb63dbf089623ea4c0e6c1348a38 100644 (file)
@@ -1102,17 +1102,24 @@ CheckStatus check_struct_literal(AstStructLiteral* sl) {
 }
 
 CheckStatus check_array_literal(AstArrayLiteral* al) {
-    if (!node_is_type((AstNode *) al->atnode)) {
-        onyx_report_error(al->token->pos, "Array type is not a type.");
-        return Check_Error;
-    }
+    if ((al->flags & Ast_Flag_Array_Literal_Typed) == 0) {
+        if (al->atnode == NULL) return Check_Success;
 
-    fill_in_type((AstTyped *) al);
+        if (!node_is_type((AstNode *) al->atnode)) {
+            onyx_report_error(al->token->pos, "Array type is not a type.");
+            return Check_Error;
+        }
 
-    al->type = type_make_array(context.ast_alloc, al->type, bh_arr_length(al->values));
-    if (al->type == NULL || al->type->kind != Type_Kind_Array) {
-        onyx_report_error(al->token->pos, "Expected array type for array literal. This is a compiler bug.");
-        return Check_Error;
+        fill_in_type((AstTyped *) al);
+        if (al->type == NULL) return Check_Error;
+
+        al->type = type_make_array(context.ast_alloc, al->type, bh_arr_length(al->values));
+        if (al->type == NULL || al->type->kind != Type_Kind_Array) {
+            onyx_report_error(al->token->pos, "Expected array type for array literal. This is a compiler bug.");
+            return Check_Error;
+        }
+
+        al->flags |= Ast_Flag_Array_Literal_Typed;
     }
 
     if (al->type->Array.count != (u32) bh_arr_length(al->values)) {
index c25168ed9836b6eee650623df53738dc955688e6..43f7a2db373770ef6aedbd24cf3067eb169d4ac3 100644 (file)
@@ -403,7 +403,7 @@ static SymresStatus symres_array_literal(AstArrayLiteral* al) {
     SYMRES(type, (AstType **) &al->atnode);
 
     al->type_node = (AstType *) al->atnode;
-    while (al->type_node->kind == Ast_Kind_Type_Alias)
+    while (al->type_node && al->type_node->kind == Ast_Kind_Type_Alias)
         al->type_node = ((AstTypeAlias *) al->type_node)->to;
 
     bh_arr_each(AstTyped *, expr, al->values)