Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ysh] Implement 1..<5 and 1..=5 open/closed range syntax #2102

Merged
merged 10 commits into from
Oct 25, 2024

Conversation

PossiblyAShrub
Copy link
Collaborator

@PossiblyAShrub PossiblyAShrub commented Oct 22, 2024

BREAKING: The a..b syntax has now been removed:

  = 1..2
     ^~
[ -c flag ]:1: Use 1..<5 for an open range, or 1..=5 for a closed range

Depends on #2101 to prevent merge conflicts (assuming #2101 will be merged before this PR.)


TODO:

  • Create an OILS-ERR entry
  • Should use the more correct "inclusive" and "exclusive" (or maybe "closed" / "half-open"?) terminology

@PossiblyAShrub
Copy link
Collaborator Author

This is ready for a review.

Copy link
Contributor

@andychu andychu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is excellent, thanks for tracking it all down!

I was thinking if we want to pretty print the ranges separately, like

$ = 1 ..= 3
(Range) 1 ..= 3

But I think the way it is may be more transparent in a sense - people can see the equivalence between the two range syntaxes. It is printed in one canonical form


So it looks good other than some tiny doc tweaks

```

There are two types of [range syntax](ref/chap-expr-lang#range). The `..<`
syntax is for half-open ranges and `..=` is for closed ranges:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would s/syntax/operator to be precise

"The ..< operator is for half-open ranges"

echo $i
}
=> 0
=> 1
=> 2

As with slices, the last number isn't included. To iterate from 1 to n, you
can use this idiom:
The `..<` syntax is for half-open ranges. `..=` is for closed ranges:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move the operator before it's example:

A range is ... .

The ..< operator is for half-open ranges:

[example]

The ..= operator is for closed ranges:

[example]

I also like writing it with spaces everywhere, like 0 ..= 3 instead of 0..=3, although that reminds me we need a formatter

ysh/expr_eval.py Outdated
@@ -1260,6 +1260,10 @@ def _EvalExpr(self, node):
i2 = _ConvertToInt(self._EvalExpr(node.upper),
'Range end should be Int', node.op)

# i2 is a big int so we don't have to worry about overflow
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah it should be a BigInt, but isn't yet.

The first step may be to raise a hard error for overflow, which is OK

Also unfortunately the ranges are still truncated in the lines below, I think we should fix that, though not sure if it should come before or after the BigInt changes mentioned

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I've removed the comment and left the TODO below it.

Where are we at with a BigInt implementation? I remember there were a few Zulip threads a while back.

@PossiblyAShrub
Copy link
Collaborator Author

This is ready for another review.

@andychu andychu changed the base branch from master to soil-staging October 25, 2024 01:09
@andychu andychu merged commit 6e5ce18 into soil-staging Oct 25, 2024
18 checks passed
@PossiblyAShrub PossiblyAShrub deleted the add-closed-range-iter branch October 25, 2024 01:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants