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

Disallow array and string index with decimal places #317

Closed
juliaogris opened this issue Mar 28, 2024 · 0 comments · Fixed by #331
Closed

Disallow array and string index with decimal places #317

juliaogris opened this issue Mar 28, 2024 · 0 comments · Fixed by #331

Comments

@juliaogris
Copy link
Member

juliaogris commented Mar 28, 2024

x := "abc"
print "x:" x[1.62] // x: b

This should cause a runtime error.

More specifically, the index must be an integer, whether a constant, variable or expression. In Go, this would be float64(int(i)) == i where i is the index.

Currently, the value is truncated via conversion to int: https://github.com/evylang/evy/blob/main/pkg/evaluator/value.go#L374

@juliaogris juliaogris transferred this issue from evylang/todo Mar 28, 2024
juliaogris added a commit that referenced this issue Apr 13, 2024
Error on non-integer index and slice expressions as it doesn't make much sense
to use it, e.g. in arr[1.1]. We are reusing the nice error message wording
that was introduced in with the ErrBadRepetion. We have also copied the way
we do normalizeSlices indices from evaluator, basing it on normalizeIndex.

Fixes #317
juliaogris added a commit that referenced this issue Apr 13, 2024
Error on non-integer index and slice expressions as it doesn't make much sense
to use it, e.g. in arr[1.1]. We are reusing the nice error message wording
that was introduced in with the ErrBadRepetion.

Fixes #317
juliaogris added a commit that referenced this issue Apr 13, 2024
Error on non-integer index and slice expressions as it doesn't make much sense
to use it, e.g. in arr[1.1]. We are reusing the nice error message wording
that was introduced in with the ErrBadRepetion. We have also copied the way
we do normalizeSlices indices from evaluator, basing it on normalizeIndex.

Fixes #317

This merges the following commits:
* bytecode: Refactor slice and index normalization
* eval: Error on non-integer index and slice expressions

     pkg/bytecode/value.go           | 78 ++++++++++++++++++++-------------
     pkg/bytecode/vm_test.go         | 29 ++++++++++++
     pkg/evaluator/evaluator.go      |  1 +
     pkg/evaluator/evaluator_test.go |  5 ++-
     pkg/evaluator/value.go          |  4 ++
     5 files changed, 85 insertions(+), 32 deletions(-)

Pull-request: #331
@juliaogris juliaogris added this to the 🚸 Learn v0.2.0 milestone Apr 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

1 participant