Skip to content

Commit

Permalink
Inequality slicing without ..: x[<i] etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
edemaine committed Oct 30, 2024
1 parent a565978 commit e24f155
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
8 changes: 8 additions & 0 deletions civet.dev/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,14 @@ Slices are increasing by default, but you can reverse them with `>` or `>=`:
reversed := x[..>=]
</Playground>

If you just want to specify one endpoint of an increasing slice,
you can avoid `..` altogether:

<Playground>
x is x[<=i] + x[>i]
x is x[<i] + x[>=i]
</Playground>

## Strings

Strings can span multiple lines:
Expand Down
33 changes: 31 additions & 2 deletions source/parser.hera
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 &&
Expand All @@ -2971,6 +2997,10 @@ RangeDots
children: [ws1, ws2]
}

OptionalRangeEnd
RangeEnd
"" -> { increasing: undefined, inclusive: true, raw: "" }

RangeEnd
/([<>])(=?)|([≤≥])/ ->
let dir = $1, equal = $2, unicode = $3
Expand All @@ -2987,7 +3017,6 @@ RangeEnd
inclusive: equal === "=",
raw: $0,
}
"" -> { increasing: undefined, inclusive: true, raw: "" }

RangeExpression
Expression:start __:ws RangeDots:range Expression:end ->
Expand Down
37 changes: 37 additions & 0 deletions test/slice.civet
Original file line number Diff line number Diff line change
Expand Up @@ -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[<b]
---
x.slice(a)
x.slice(a)
x.slice(a + 1)
x.slice(0, b + 1)
x.slice(0, b + 1)
x.slice(0, b)
"""

testCase """
complex
---
x[>=a ?? a0]
x[≥a ?? a0]
x[>a ?? a0]
x[<=b ?? b0]
x[≤b ?? b0]
x[<b ?? b0]
---
x.slice(a ?? a0)
x.slice(a ?? a0)
x.slice((a ?? a0) + 1)
x.slice(0, (b ?? b0) + 1)
x.slice(0, (b ?? b0) + 1)
x.slice(0, b ?? b0)
"""

describe "assignment", ->
testCase """
everything
Expand Down

0 comments on commit e24f155

Please sign in to comment.