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

Define and implement an array repetition operator #329

Merged
merged 3 commits into from
Apr 12, 2024
Merged

Conversation

camh-
Copy link
Member

@camh- camh- commented Apr 11, 2024

Define the array * num operator to repeat array num number of
times. This allows for easy initialisation of variable sized arrays
without needing to use a loop to do so.

Update the spec with a definition of the operator. Update
syntax_by_example.md to give some basic examples.

Implement this in the evaluator and the bytecode compiler/vm.

Issue: evylang/todo#101

pkg/bytecode/vm.go Outdated Show resolved Hide resolved
pkg/bytecode/vm.go Outdated Show resolved Hide resolved
Copy link
Member

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

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

awesome, thank you so much. a couple of doc nits, otherwise really clear.

docs/spec.md Outdated Show resolved Hide resolved
docs/spec.md Outdated Show resolved Hide resolved
pkg/bytecode/vm.go Outdated Show resolved Hide resolved
@camh-
Copy link
Member Author

camh- commented Apr 11, 2024

All fixed and pushed. PTAL

Copy link
Member

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

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

looks still great to me. just a question around negative repetition counts.

pkg/evaluator/evaluator.go Outdated Show resolved Hide resolved
docs/spec.md Outdated Show resolved Hide resolved
Copy link
Collaborator

@Rossiar Rossiar left a comment

Choose a reason for hiding this comment

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

some small things, overall lgtm

pkg/bytecode/compiler.go Outdated Show resolved Hide resolved
pkg/bytecode/vm.go Outdated Show resolved Hide resolved
pkg/bytecode/vm.go Show resolved Hide resolved
Copy link
Member

@juliaogris juliaogris left a comment

Choose a reason for hiding this comment

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

still 💚

@camh-
Copy link
Member Author

camh- commented Apr 11, 2024

do not merge label because I need to squash in the fixup commits before merging. I've forgotten to do that before, so this may help.

Copy link
Collaborator

@Rossiar Rossiar left a comment

Choose a reason for hiding this comment

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

lgtm

Allow the `*` operator to be used on arrays with a number on the
right-hand side to repeat an array a number of times, producing an array
of the same type with repeated elements. This allows for creating arrays
of variable length without needing to write a loop.

e.g.

    n := 4
    a := [0] * n

will produce `[0 0 0 0]`.

There can be any number of elements in the array and they will all be
repeated. e.g. `[1 2] * 2` == `[1 2 1 2]`.

Update the spec to document this. Add an example to
`syntax_by_example.md`

Gen-command: make doc docs
Implement construction of arrays via array repetition, a `BinaryExpr`
with an array value on the left and a number on the right. The result is
the left array repeated the right number of times. Zero times results in
an empty array. A non-integer value results in an error.

Add test cases for all distinct cases - basic, zero, precedence,
variable and error.
Implement construction of arrays via array repetition, similar to the
evaluator. A new opcode, `OpArrayRepeat` is added for this operation
which the compiler emits based on the types of the arguments to the `*`
binary operator.

Add tests for some basic cases and the error case.

Disable some lint errors due to "complex" code. `vm.Run()` already
disabled one, but now there's another. The test case is not really even
complex. Stupid linters.
@camh- camh- merged commit 48ed21f into main Apr 12, 2024
3 checks passed
@camh- camh- deleted the array-repetition branch April 12, 2024 06:44
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.

3 participants