changed slice creation syntax to use ranges
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 4 Sep 2020 03:54:22 +0000 (22:54 -0500)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Fri, 4 Sep 2020 03:54:22 +0000 (22:54 -0500)
13 files changed:
core/array.onyx
core/file.onyx
core/string.onyx
core/sys/wasi.onyx
docs/plan
include/onyxastnodes.h
onyx
progs/poly_test.onyx
src/onyxchecker.c
src/onyxclone.c
src/onyxparser.c
src/onyxsymres.c
src/onyxwasm.c

index 9f0fb12278c3995130b01dd6ba088d4e1b8709fe..6e06574d28ad5b517cb8b1430199c20d02f7a088 100644 (file)
@@ -94,7 +94,7 @@ array_average :: proc (arr: ^[..] $T) -> T {
 }
 
 array_to_slice :: proc (arr: ^[..] $T) -> [] T {
-    return arr.data[0 : arr.count];
+    return arr.data[0 .. arr.count];
 }
 
 /*
@@ -123,4 +123,4 @@ array_fold :: proc (arr: ^[..] $T, init: $R, f: proc (T, R) -> R) -> R {
 
 array_map :: proc (arr: ^[..] $T, f: proc (T) -> T) {
     for i: 0, arr.count do arr.data[i] = f(arr.data[i]);
-}
\ No newline at end of file
+}
index 348e3e03762f7656e6fee6651364b0f8a303fb18..3a0e5ea5c62e73b18bdbc72adfb7c17cb87301ee 100644 (file)
@@ -105,7 +105,7 @@ file_get_contents_from_file :: proc (file: File) -> string {
 
     fd_seek(file.fd, prev_loc, Whence.Set, ^dummy);
 
-    return data[0 : size];
+    return data[0 .. size];
 }
 
 file_get_contents :: proc #overloaded {
@@ -119,4 +119,4 @@ file_get_contents :: proc #overloaded {
 
         return file_get_contents(tmp_file);
     }
-}
\ No newline at end of file
+}
index 9dac85d455b970b28307c30c65463a8f934a5795..dea3170183df2e55188c8568eedbe94384dc5ab3 100644 (file)
@@ -53,15 +53,15 @@ string_split :: proc (str: string, delim: u8) -> []string {
 
     for i: 0, str.count {
         if str[i] == delim {
-            strarr[curr_str] = str.data[begin : i];
+            strarr[curr_str] = str.data[begin .. i];
             begin = i + 1;
             curr_str += 1;
         }
     }
 
-    strarr[curr_str] = str.data[begin : str.count];
+    strarr[curr_str] = str.data[begin .. str.count];
 
-    return strarr[0 : delim_count + 1];
+    return strarr[0 .. delim_count + 1];
 }
 
 string_substr :: proc (str: string, sub: string) -> string {
@@ -69,11 +69,11 @@ string_substr :: proc (str: string, sub: string) -> string {
         while j := 0; j < sub.count && str[i + j] == sub[j] {
             j += 1;
 
-            if j == sub.count do return str.data[i : i + j];
+            if j == sub.count do return str.data[i .. i + j];
         }
     }
 
-    return str.data[0:0];
+    return str.data[0 .. 0];
 }
 
 string_contains :: proc (str: string, c: u8) -> bool {
@@ -183,7 +183,7 @@ i64_to_string :: proc (n_: i64, base: u64, buf: Buffer) -> string {
         c -= 1;
 
         n /= base;
-        
+
     } else {
         *c = #char "0";
         len += 1;
index eda86edb5a8641ade546a923fabcbe8072e23eda..85d26044ff8ac6bd435d87477586badc74f7b071 100644 (file)
@@ -37,7 +37,7 @@ proc #export "_start" {
 
     stdio_init();
 
-    main.main(argv[0 : argc]);
+    main.main(argv[0 .. argc]);
 
     print_buffer_flush();
 }
index 610ee4a6172828312ef6a2a71f54a5c9eae005b3..a765769419c7afbc409caec54f9e3a702dfa0e7e 100644 (file)
--- a/docs/plan
+++ b/docs/plan
@@ -231,6 +231,15 @@ HOW:
             - I think all the infrastructure is there for this
 
         [ ] data structure based iteration
+            - Currently, I do not plan on having custom iterators. This may very well change in the near future.
+            - For now, a for loop will be reimagined to look like:
+                for val: iterable ...
+
+            - 'iterable' will be something of a type that the compiler knows how to iterate over:
+                * range
+                * array
+                * slice
+                * dynamic array
 
         [ ] baked parameters
             - Compile time known parameters
index 74747d8450d2e5304f1f539224a4fa7581bf08a4..11815151e5ee52728b4623b0ddeb3ea90421af39 100644 (file)
@@ -18,7 +18,6 @@ typedef struct AstArgument AstArgument;
 typedef struct AstAddressOf AstAddressOf;
 typedef struct AstDereference AstDereference;
 typedef struct AstArrayAccess AstArrayAccess;
-typedef struct AstSlice AstSlice;
 typedef struct AstFieldAccess AstFieldAccess;
 typedef struct AstSizeOf AstSizeOf;
 typedef struct AstAlignOf AstAlignOf;
@@ -309,7 +308,6 @@ struct AstArgument      { AstTyped_base; AstTyped *value; };
 struct AstAddressOf     { AstTyped_base; AstTyped *expr; };
 struct AstDereference   { AstTyped_base; AstTyped *expr; };
 struct AstArrayAccess   { AstTyped_base; AstTyped *addr; AstTyped *expr; u64 elem_size; };
-struct AstSlice         { AstTyped_base; AstTyped *addr; AstTyped *lo, *hi; u64 elem_size; };
 struct AstFieldAccess   { AstTyped_base; AstTyped *expr; u32 offset; u32 idx; };
 struct AstSizeOf        { AstTyped_base; AstType *so_type; u64 size; };
 struct AstAlignOf       { AstTyped_base; AstType *ao_type; u64 alignment; };
diff --git a/onyx b/onyx
index 4d064dbdef8ae331e5dc198c57a8e2dd5a01d0a1..ca03de2a34cabab57896a0afe228f7e6cf028c4a 100755 (executable)
Binary files a/onyx and b/onyx differ
index 80de2d0d42ab90efdd9654228169ba0a49e15160..08bed003ba4d4d822d184e00b82b826a9f6f4016 100644 (file)
@@ -101,7 +101,7 @@ main :: proc (args: [] cstring) {
     array_sort(^s.a, cmp_dec);
     array_sort(^s.b, cmp_asc);
 
-    print_array(^s.a);
+    print_array(s.a.data[21 .. 27]);
     print_array(^s.b);
 
     print("After adding...\n");
index 8204f0d6fea7f6a888d9d8fc8d2ccf9604f11875..df2fdbd16e2681e6b66db2c19f1e0bec128a3d31 100644 (file)
@@ -21,7 +21,7 @@ CHECK(expression, AstTyped** expr);
 CHECK(address_of, AstAddressOf* aof);
 CHECK(dereference, AstDereference* deref);
 CHECK(array_access, AstArrayAccess* expr);
-CHECK(slice, AstSlice* sl);
+CHECK(slice, AstArrayAccess* sl);
 CHECK(field_access, AstFieldAccess** pfield);
 CHECK(range_literal, AstBinaryOp** range);
 CHECK(size_of, AstSizeOf* so);
@@ -877,28 +877,15 @@ b32 check_array_access(AstArrayAccess* aa) {
     return 0;
 }
 
-b32 check_slice(AstSlice* sl) {
+b32 check_slice(AstArrayAccess* sl) {
     if (check_expression(&sl->addr)) return 1;
-    if (check_expression(&sl->lo)) return 1;
-    if (check_expression(&sl->hi)) return 1;
+    if (check_expression(&sl->expr)) return 1;
 
     if (!type_is_pointer(sl->addr->type)) {
         onyx_report_error(sl->token->pos, "Expected pointer or array type for left of slice creation.");
         return 1;
     }
 
-    if (sl->lo->type->kind != Type_Kind_Basic
-            || (sl->lo->type->Basic.kind != Basic_Kind_I32 && sl->lo->type->Basic.kind != Basic_Kind_U32)) {
-        onyx_report_error(sl->lo->token->pos, "Expected type u32 or i32 for lower index.");
-        return 1;
-    }
-
-    if (sl->hi->type->kind != Type_Kind_Basic
-            || (sl->hi->type->Basic.kind != Basic_Kind_I32 && sl->hi->type->Basic.kind != Basic_Kind_U32)) {
-        onyx_report_error(sl->hi->token->pos, "Expected type u32 or i32 for upper index.");
-        return 1;
-    }
-
     Type *of = NULL;
     if (sl->addr->type->kind == Type_Kind_Pointer)
         of = sl->addr->type->Pointer.elem;
@@ -1041,7 +1028,7 @@ b32 check_expression(AstTyped** pexpr) {
         case Ast_Kind_Address_Of:   retval = check_address_of((AstAddressOf *) expr); break;
         case Ast_Kind_Dereference:  retval = check_dereference((AstDereference *) expr); break;
         case Ast_Kind_Array_Access: retval = check_array_access((AstArrayAccess *) expr); break;
-        case Ast_Kind_Slice:        retval = check_slice((AstSlice *) expr); break;
+        case Ast_Kind_Slice:        retval = check_slice((AstArrayAccess *) expr); break;
         case Ast_Kind_Field_Access: retval = check_field_access((AstFieldAccess **) pexpr); break;
         case Ast_Kind_Size_Of:      retval = check_size_of((AstSizeOf *) expr); break;
         case Ast_Kind_Align_Of:     retval = check_align_of((AstAlignOf *) expr); break;
index c0c32c93ecd6387880a799f201840a43374d128e..f11837fb98cef4c01f2060335ac2aadb52442065 100644 (file)
@@ -69,7 +69,7 @@ static inline i32 ast_kind_to_size(AstNode* node) {
         case Ast_Kind_Address_Of: return sizeof(AstAddressOf);
         case Ast_Kind_Dereference: return sizeof(AstDereference);
         case Ast_Kind_Array_Access: return sizeof(AstArrayAccess);
-        case Ast_Kind_Slice: return sizeof(AstSlice);
+        case Ast_Kind_Slice: return sizeof(AstArrayAccess);
         case Ast_Kind_Field_Access: return sizeof(AstFieldAccess);
         case Ast_Kind_Pipe: return sizeof(AstBinaryOp);
         case Ast_Kind_Range: return sizeof(AstBinaryOp);
@@ -121,6 +121,7 @@ AstNode* ast_clone(bh_allocator a, void* n) {
 
        switch ((u16) node->kind) {
                case Ast_Kind_Binary_Op:
+        case Ast_Kind_Range:
                        ((AstBinaryOp *) nn)->left  = (AstTyped *) ast_clone(a, ((AstBinaryOp *) node)->left);
                        ((AstBinaryOp *) nn)->right = (AstTyped *) ast_clone(a, ((AstBinaryOp *) node)->right);
                        break;
@@ -151,17 +152,12 @@ AstNode* ast_clone(bh_allocator a, void* n) {
                        ((AstDereference *) nn)->expr = (AstTyped *) ast_clone(a, ((AstDereference *) node)->expr);
                        break;
 
+               case Ast_Kind_Slice:
                case Ast_Kind_Array_Access:
                        ((AstArrayAccess *) nn)->addr = (AstTyped *) ast_clone(a, ((AstArrayAccess *) node)->addr);
                        ((AstArrayAccess *) nn)->expr = (AstTyped *) ast_clone(a, ((AstArrayAccess *) node)->expr);
                        break;
 
-               case Ast_Kind_Slice:
-                       ((AstSlice *) nn)->lo = (AstTyped *) ast_clone(a, ((AstSlice *) node)->lo);
-                       ((AstSlice *) nn)->hi = (AstTyped *) ast_clone(a, ((AstSlice *) node)->hi);
-                       ((AstSlice *) nn)->addr = (AstTyped *) ast_clone(a, ((AstSlice *) node)->addr);
-                       break;
-
                case Ast_Kind_Field_Access:
                        ((AstFieldAccess *) nn)->expr = (AstTyped *) ast_clone(a, ((AstFieldAccess *) node)->expr);
                        break;
index 70603f3e5c4264b0d8c99b363a62366fc4eaf760..c7ff7585f753d08230a9c5f8caa7d0a6ec35dff6 100644 (file)
@@ -517,31 +517,19 @@ static AstTyped* parse_factor(OnyxParser* parser) {
         switch ((u16) parser->curr->type) {
             case '[': {
                 OnyxToken *open_bracket = expect_token(parser, '[');
-                AstTyped *lo = parse_expression(parser);
+                AstTyped *expr = parse_expression(parser);
 
-                if (parser->curr->type == ':') {
-                    consume_token(parser);
-
-                    AstSlice *sl_node = make_node(AstSlice, Ast_Kind_Slice);
-                    sl_node->token = open_bracket;
-                    sl_node->addr  = retval;
-                    sl_node->lo    = lo;
-                    sl_node->hi    = parse_expression(parser);
-
-                    retval = (AstTyped *) sl_node;
-                    expect_token(parser, ']');
-                    goto factor_parsed;
+                AstKind kind = Ast_Kind_Array_Access;
+                if (expr->kind == Ast_Kind_Range)
+                    kind = Ast_Kind_Slice;
 
-                } else {
-                    AstArrayAccess* aa_node = make_node(AstArrayAccess, Ast_Kind_Array_Access);
-                    aa_node->token = open_bracket;
-                    aa_node->addr  = retval;
-                    aa_node->expr  = lo;
-
-                    retval = (AstTyped *) aa_node;
-                    expect_token(parser, ']');
-                }
+                AstArrayAccess *aa_node = make_node(AstArrayAccess, kind);
+                aa_node->token = open_bracket;
+                aa_node->addr = retval;
+                aa_node->expr = expr;
 
+                retval = (AstTyped *) aa_node;
+                expect_token(parser, ']');
                 break;
             }
 
index 4871c6d39979b94d71590706c901c8f26c8ab48e..b09f447b7404abd35f31fb1c650058a1366f0fb7 100644 (file)
@@ -317,17 +317,12 @@ static void symres_expression(AstTyped** expr) {
             (*expr)->type_node = symres_type(builtin_string_type);
             break;
 
+        case Ast_Kind_Slice:
         case Ast_Kind_Array_Access:
             symres_expression(&((AstArrayAccess *)(*expr))->addr);
             symres_expression(&((AstArrayAccess *)(*expr))->expr);
             break;
 
-        case Ast_Kind_Slice:
-            symres_expression(&((AstSlice *)(*expr))->addr);
-            symres_expression(&((AstSlice *)(*expr))->lo);
-            symres_expression(&((AstSlice *)(*expr))->hi);
-            break;
-
         case Ast_Kind_Struct_Literal:
             symres_struct_literal((AstStructLiteral *)(*expr));
             break;
index 48dfb87fa59d02a118b66b8195261b4d46d571e6..e8385ed501340e47413c4a2fef090d712b58f3d5 100644 (file)
@@ -1584,7 +1584,7 @@ EMIT_FUNC(expression, AstTyped* expr) {
 
                 if (smem.idx == 1)
                     WID(WI_I32_CONST, ((AstStrLit *) field->expr)->length);
-                
+
                 break;
             }
 
@@ -1595,11 +1595,23 @@ EMIT_FUNC(expression, AstTyped* expr) {
         }
 
         case Ast_Kind_Slice: {
-            AstSlice* sl = (AstSlice *) expr;
+            AstArrayAccess* sl = (AstArrayAccess *) expr;
+
+            AstTyped *lo, *hi;
+
+            // NOTE: Since all ranges are converted to struct literals,
+            // we need to extract the expressions from the struct literal
+            // data. Doing it in this verbose way for robustness sake.
+            AstStructLiteral *range_literal = (AstStructLiteral *) sl->expr;
+            StructMember smem;
+            type_lookup_member(range_literal->type, "low", &smem);
+            lo = range_literal->values[smem.idx];
+            type_lookup_member(range_literal->type, "high", &smem);
+            hi = range_literal->values[smem.idx];
 
             u64 tmp_local = local_raw_allocate(mod->local_alloc, WASM_TYPE_INT32);
 
-            emit_expression(mod, &code, sl->lo);
+            emit_expression(mod, &code, lo);
             WIL(WI_LOCAL_TEE, tmp_local);
             if (sl->elem_size != 1) {
                 WID(WI_I32_CONST, sl->elem_size);
@@ -1607,7 +1619,7 @@ EMIT_FUNC(expression, AstTyped* expr) {
             }
             emit_expression(mod, &code, sl->addr);
             WI(WI_I32_ADD);
-            emit_expression(mod, &code, sl->hi);
+            emit_expression(mod, &code, hi);
             WIL(WI_LOCAL_GET, tmp_local);
             WI(WI_I32_SUB);
 
@@ -2827,7 +2839,7 @@ static void output_instruction(WasmFunc* func, WasmInstruction* instr, bh_buffer
             break;
 
         case WI_JUMP_TABLE: {
-            BranchTable* bt = (BranchTable *) instr->data.p; 
+            BranchTable* bt = (BranchTable *) instr->data.p;
 
             leb = uint_to_uleb128((u64) bt->count, &leb_len);
             bh_buffer_append(buff, leb, leb_len);