From: Brendan Hansen Date: Fri, 4 Sep 2020 03:54:22 +0000 (-0500) Subject: changed slice creation syntax to use ranges X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=413ac3bff178d137848e740fccf80d80e71363e3;p=onyx.git changed slice creation syntax to use ranges --- diff --git a/core/array.onyx b/core/array.onyx index 9f0fb122..6e06574d 100644 --- a/core/array.onyx +++ b/core/array.onyx @@ -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 +} diff --git a/core/file.onyx b/core/file.onyx index 348e3e03..3a0e5ea5 100644 --- a/core/file.onyx +++ b/core/file.onyx @@ -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 +} diff --git a/core/string.onyx b/core/string.onyx index 9dac85d4..dea31701 100644 --- a/core/string.onyx +++ b/core/string.onyx @@ -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; diff --git a/core/sys/wasi.onyx b/core/sys/wasi.onyx index eda86edb..85d26044 100644 --- a/core/sys/wasi.onyx +++ b/core/sys/wasi.onyx @@ -37,7 +37,7 @@ proc #export "_start" { stdio_init(); - main.main(argv[0 : argc]); + main.main(argv[0 .. argc]); print_buffer_flush(); } diff --git a/docs/plan b/docs/plan index 610ee4a6..a7657694 100644 --- 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 diff --git a/include/onyxastnodes.h b/include/onyxastnodes.h index 74747d84..11815151 100644 --- a/include/onyxastnodes.h +++ b/include/onyxastnodes.h @@ -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 4d064dbd..ca03de2a 100755 Binary files a/onyx and b/onyx differ diff --git a/progs/poly_test.onyx b/progs/poly_test.onyx index 80de2d0d..08bed003 100644 --- a/progs/poly_test.onyx +++ b/progs/poly_test.onyx @@ -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"); diff --git a/src/onyxchecker.c b/src/onyxchecker.c index 8204f0d6..df2fdbd1 100644 --- a/src/onyxchecker.c +++ b/src/onyxchecker.c @@ -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; diff --git a/src/onyxclone.c b/src/onyxclone.c index c0c32c93..f11837fb 100644 --- a/src/onyxclone.c +++ b/src/onyxclone.c @@ -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; diff --git a/src/onyxparser.c b/src/onyxparser.c index 70603f3e..c7ff7585 100644 --- a/src/onyxparser.c +++ b/src/onyxparser.c @@ -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; } diff --git a/src/onyxsymres.c b/src/onyxsymres.c index 4871c6d3..b09f447b 100644 --- a/src/onyxsymres.c +++ b/src/onyxsymres.c @@ -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; diff --git a/src/onyxwasm.c b/src/onyxwasm.c index 48dfb87f..e8385ed5 100644 --- a/src/onyxwasm.c +++ b/src/onyxwasm.c @@ -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);