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

Add new syntax to bind for-in loop indices #1216

Merged
merged 9 commits into from
Nov 5, 2021
Merged

Add new syntax to bind for-in loop indices #1216

merged 9 commits into from
Nov 5, 2021

Conversation

dsainati1
Copy link
Contributor

@dsainati1 dsainati1 commented Nov 3, 2021

Closes #402

Description

This adds a new piece of syntax to for loops to the language to allow users to bind the index into the array being iterated over.

for x, y in array {
}

This introduces two variables x and y into the scope of the loop body; y is the element variable as in a normal for-in loop, while x is of type int, is initialized to 0 and is incremented by one at the end of each iteration of the loop, effectively tracking the index of the array being looped over.


  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

@codecov-commenter
Copy link

codecov-commenter commented Nov 3, 2021

Codecov Report

Merging #1216 (1f37d41) into master (28cd2f1) will increase coverage by 0.01%.
The diff coverage is 94.28%.

❗ Current head 1f37d41 differs from pull request most recent head ede9404. Consider uploading reports for the commit ede9404 to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1216      +/-   ##
==========================================
+ Coverage   77.12%   77.14%   +0.01%     
==========================================
  Files         272      272              
  Lines       33911    33944      +33     
==========================================
+ Hits        26155    26186      +31     
- Misses       6693     6694       +1     
- Partials     1063     1064       +1     
Flag Coverage Δ
unittests 77.14% <94.28%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/ast/statement.go 77.56% <ø> (ø)
runtime/sema/check_for.go 92.72% <85.71%> (-2.40%) ⬇️
runtime/interpreter/interpreter_statement.go 89.47% <100.00%> (+0.43%) ⬆️
runtime/parser2/statement.go 90.00% <100.00%> (+0.31%) ⬆️
runtime/common/address.go 92.00% <0.00%> (-0.31%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 28cd2f1...ede9404. Read the comment docs.

Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Nice work! 👍

BTW: We usually use a branch naming scheme like <name>/<issue-nunber>-<short-name> to avoid conflicts, e.g. for this PR/branch daniel/402-for-loop-index

runtime/tests/checker/for_test.go Outdated Show resolved Hide resolved
runtime/tests/checker/for_test.go Outdated Show resolved Hide resolved
runtime/parser2/statement_test.go Show resolved Hide resolved
runtime/parser2/statement.go Outdated Show resolved Hide resolved
docs/language/control-flow.md Outdated Show resolved Hide resolved
runtime/interpreter/interpreter_statement.go Show resolved Hide resolved
runtime/interpreter/interpreter_statement.go Show resolved Hide resolved

if indexVariable != nil {
index++
indexValue.BigInt.SetInt64(index)
Copy link
Member

@SupunS SupunS Nov 4, 2021

Choose a reason for hiding this comment

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

Is it safe to change the existing value, or should we assign a new value? I think changing the content of the existing value might also mutate the usages of previous indexes as well.

like for eg: what if a user stores the indexes in an array, and prints them later

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that's a good point. I had suggested this above to avoid allocation, but I think you are right

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a test for this, and there are is indeed an error in this case. Nice catch

Copy link
Member

Choose a reason for hiding this comment

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

@dsainati1 Nice, good idea to add a test! Feel free to revert to your original code, that worked and I think was safe 👍

@dsainati1 dsainati1 requested a review from SupunS November 5, 2021 18:37
Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Nice work! 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

for-in loop should bind index
4 participants