Browse Source

Add `ranged_fields_for_array_compound_literals`

gingerBill 5 years ago
parent
commit
a5e42a0465
2 changed files with 43 additions and 10 deletions
  1. 37 7
      examples/demo/demo.odin
  2. 6 3
      src/check_expr.cpp

+ 37 - 7
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() {

+ 6 - 3
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 = {};