Implement dynamic ranges (#4151)

Closes #4021

Allows array ranges (e.g., `[0..10]`) to take expression instead of just numeric literals as their start and end values. Both expressions are required (we don't support `[0..]`, etc.).

I've created a new kind of expression in the AST. The alternative was to represent the internals of an array as some kind of pattern which could initially be fully explicit or ranges. I figured the chosen version was simpler and easier to extend to open ranges, whereas the latter would be easier to extend to mixed ranges or other patterns. I chose simpler, it'll be easy enough to refactor if necessary.

Parsing is tested implicitly by the tests of execution and unparsing.

---------

Signed-off-by: Nick Cameron <nrc@ncameron.org>
Co-authored-by: Adam Chalmers <adam.chalmers@zoo.dev>
This commit is contained in:
Nick Cameron
2024-10-17 02:58:04 +13:00
committed by GitHub
parent fbac9935fe
commit 248ef8ebb3
15 changed files with 655 additions and 284 deletions

View File

@ -0,0 +1,17 @@
r1 = [0..4]
assertEqual(r1[4], 4, 0.00001, "last element is included")
four = 4
zero = 0
r2 = [zero..four]
assertEqual(r2[4], 4, 0.00001, "last element is included")
five = int(four + 1)
r3 = [zero..five]
assertEqual(r3[4], 4, 0.00001, "second-to-last element is included")
assertEqual(r3[5], 5, 0.00001, "last element is included")
r4 = [int(zero + 1) .. int(five - 1)]
assertEqual(r4[0], 1, 0.00001, "first element is 1")
assertEqual(r4[2], 3, 0.00001, "second-to-last element is 3")
assertEqual(r4[3], 4, 0.00001, "last element is 4")

View File

@ -0,0 +1,3 @@
xs = [-5..5]
assertEqual(xs[0], -5, 0.001, "first element is -5")
assert(false)

View File

@ -66,6 +66,8 @@ async fn run_fail(code: &str) -> KclError {
gen_test!(property_of_object);
gen_test!(index_of_array);
gen_test!(comparisons);
gen_test!(array_range_expr);
gen_test_fail!(array_range_negative_expr, "syntax: Invalid integer: -5.0");
gen_test_fail!(
invalid_index_str,
"semantic: Only integers >= 0 can be used as the index of an array, but you're using a string"