From a5e42a046531405e0b8830ae092470c2aa2e67a8 Mon Sep 17 00:00:00 2001 From: gingerBill Date: Sat, 26 Oct 2019 14:36:28 +0100 Subject: [PATCH] Add `ranged_fields_for_array_compound_literals` --- examples/demo/demo.odin | 44 ++++++++++++++++++++++++++++++++++------- src/check_expr.cpp | 9 ++++++--- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/examples/demo/demo.odin b/examples/demo/demo.odin index cd6ac6bf5..fa970fa43 100644 --- a/examples/demo/demo.odin +++ b/examples/demo/demo.odin @@ -1194,13 +1194,43 @@ ranged_fields_for_array_compound_literals :: proc() { foo := [?]int{1, 4, 9, 16}; fmt.println(foo); } - i := 2; - foo := [?]int { - 0 = 123, - 5..9 = 54, - 10..<16 = i*3 + (i-1)*2, - }; - fmt.println(foo); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8] + { // Indexed + foo := [?]int{ + 3 = 16, + 1 = 4, + 2 = 9, + 0 = 1, + }; + fmt.println(foo); + } + { // Ranges + i := 2; + foo := [?]int { + 0 = 123, + 5..9 = 54, + 10..<16 = i*3 + (i-1)*2, + }; + #assert(len(foo) == 16); + fmt.println(foo); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8] + } + { // Slice and Dynamic Array support + i := 2; + foo_slice := []int { + 0 = 123, + 5..9 = 54, + 10..<16 = i*3 + (i-1)*2, + }; + assert(len(foo) == 16); + fmt.println(foo_slice); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8] + + foo_dynamic_array := [dynamic]int { + 0 = 123, + 5..9 = 54, + 10..<16 = i*3 + (i-1)*2, + }; + assert(len(foo) == 16); + fmt.println(foo_dynamic_array); // [123, 0, 0, 0, 0, 54, 54, 54, 54, 54, 8, 8, 8, 8, 8] + } } main :: proc() { diff --git a/src/check_expr.cpp b/src/check_expr.cpp index f07ebe09c..7491094c8 100644 --- a/src/check_expr.cpp +++ b/src/check_expr.cpp @@ -7205,10 +7205,13 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type i64 lo = exact_value_to_i64(x.value); i64 hi = exact_value_to_i64(y.value); + i64 max_index = hi; if (op.kind == Token_RangeHalf) { hi -= 1; } - i64 max_index = hi; + if (op.kind == Token_Ellipsis) { + max_index += 1; + } bool new_range = range_cache_add_range(&rc, lo, hi); if (!new_range) { @@ -7257,8 +7260,8 @@ ExprKind check_expr_base_internal(CheckerContext *c, Operand *o, Ast *node, Type continue; } - if (max < index) { - max = index; + if (max < index+1) { + max = index+1; } Operand operand = {};