From e24f155e9768198d29432f8f1c86023c9dc6750c Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Wed, 30 Oct 2024 15:36:23 -0400 Subject: [PATCH 1/2] Inequality slicing without `..`: `x[` or `>=`: reversed := x[..>=] +If you just want to specify one endpoint of an increasing slice, +you can avoid `..` altogether: + + +x is x[<=i] + x[>i] +x is x[=i] + + ## Strings Strings can span multiple lines: diff --git a/source/parser.hera b/source/parser.hera index b2f1c70b..d68bd6ce 100644 --- a/source/parser.hera +++ b/source/parser.hera @@ -1553,6 +1553,32 @@ SliceParameters children, } + Loc:l RangeEnd:rend Expression:e -> + let start, end, children + if (rend.increasing) { // right end + end = e + if (rend.inclusive) { + end = [makeLeftHandSideExpression(end), ` + 1`] + } + start = { + $loc: l.$loc, + token: "0", + } + children = [start, ", ", end] + } else { // left end + start = e + if (!rend.inclusive) { + start = [makeLeftHandSideExpression(start), ` + 1`] + } + children = [start] + } + return { + type: "SliceParameters", + start, + end, + children, + } + AccessStart PropertyAccessModifier?:modifier Dot:dot ![.\s] -> return { @@ -2951,7 +2977,7 @@ RangeDots triple: true, children: [], } - RangeEnd:left _?:ws1 DotDot:dots _?:ws2 RangeEnd:right -> + OptionalRangeEnd:left _?:ws1 DotDot:dots _?:ws2 OptionalRangeEnd:right -> // Inherit increasing flag from either side const increasing = left.increasing ?? right.increasing if (left.increasing != null && right.increasing != null && @@ -2971,6 +2997,10 @@ RangeDots children: [ws1, ws2] } +OptionalRangeEnd + RangeEnd + "" -> { increasing: undefined, inclusive: true, raw: "" } + RangeEnd /([<>])(=?)|([≤≥])/ -> let dir = $1, equal = $2, unicode = $3 @@ -2987,7 +3017,6 @@ RangeEnd inclusive: equal === "=", raw: $0, } - "" -> { increasing: undefined, inclusive: true, raw: "" } RangeExpression Expression:start __:ws RangeDots:range Expression:end -> diff --git a/test/slice.civet b/test/slice.civet index a2f54e01..60a3e276 100644 --- a/test/slice.civet +++ b/test/slice.civet @@ -251,6 +251,43 @@ describe "slice", -> unknown:1:9 Slice range cannot be decreasing in assignment """ + describe "inequality without ..", -> + testCase """ + simple + --- + x[>=a] + x[≥a] + x[>a] + x[<=b] + x[≤b] + x[=a ?? a0] + x[≥a ?? a0] + x[>a ?? a0] + x[<=b ?? b0] + x[≤b ?? b0] + x[ testCase """ everything From 5be8f5334285b494e250d0fcd52bf54d8fe7923c Mon Sep 17 00:00:00 2001 From: Erik Demaine Date: Wed, 30 Oct 2024 16:03:50 -0400 Subject: [PATCH 2/2] Improve slice example --- civet.dev/reference.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/civet.dev/reference.md b/civet.dev/reference.md index 16bc63f5..868e764f 100644 --- a/civet.dev/reference.md +++ b/civet.dev/reference.md @@ -461,8 +461,8 @@ If you just want to specify one endpoint of an increasing slice, you can avoid `..` altogether: -x is x[<=i] + x[>i] -x is x[=i] +[left, right] = [x[<=i], x[>i]] +[left, right] = [x[=i]] ## Strings