-
-
Notifications
You must be signed in to change notification settings - Fork 166
[osh] Implement InitializerList for a=(v [k]=v [k]+=v)
#2279
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
Conversation
57c50bf
to
a444d81
Compare
Cleanup of ShArrayLiteralAnother thing to discuss is how much we should clean up the ShArrayLiteral-related codes. We no longer use I guess the definition in
I think the uses of
I guessed the uses in Line 1222 in a444d81
which means that the What I removedI removed the processing of What I leftThere are still places where
Lines 1845 to 1853 in a444d81
This anyway produces an error message, so I think the removal of this part wouldn't affect correct scripts. However, I'm not sure if there is indeed a case where
Lines 99 to 107 in a444d81
Lines 1020 to 1026 in a444d81
Renaming
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow thank you! I read over the docs ONLY
That helps and clarifies what it does ... I don't think I fully understood that
I will have more comments later once I look at the code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This generally looks good -- I mostly looked at the tests and the ASDL schema
I think we should rename ShArrayLiteral to YshArrayLiteral -- that is a good idea, since it's only used for YSH now
) | ||
|
||
AssocPair = (CompoundWord key, CompoundWord value) | ||
AssocPair = (CompoundWord key, CompoundWord value, bool has_plus) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed in frontend/syntax.asdl
we have assign_op = Equal | PlusEqual
, and AssignPair
uses it
In core/runtime.asdl, AssignArg
has bool has_plus
(One of them is static and one is dynamic)
It might be better to be consistent
|
||
IntBox = (int i) | ||
|
||
InitializerValue = (str? key, str rval, bool plus_eq) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it might be cleaner to use frontend/syntax.asdl assign_op everywhere, although we can also refactor that separately probably
1d9bab2
to
13b26f1
Compare
* [osh/word_parse] Allow mixed forms of initializer in BashAssocLiteral We allow mixed forms of initializer such as (1 2 [3]=4). We extend the element type of "BashAssocLiteral.pairs" to "InitializerWord", which is either "ArrayWord" or "AssocPair". "ArrayWord" is the non array-assignment form of a word. In addition, "AssocPair" now records also whether the word in initializer has the form "[]=" or "[]+=". * [refactor frontend/syntax] Rename "{BashAssoc => Initializer}Literal"
The current OSH behavior of overwriting "assoc" with a new indexed array for "assoc=(1 2)" conflicts with the Bash 5.1 feature "assoc=(key value)". The current master behavior will soon be changed when the Bash 5.1 feature is implemented, yet we keep the current master behavior for now to avoid changing the behavior too many times. This commit adjusts the behavior to be backward compatible with the current master version.
Now BashArray/BashAssoc cannot appear on the RHS. The construct =(...) always produce InitializerList.
* [spec/assoc] Update spec tests for "assoc=([key]=value)" We now support the sparse-array initialization of the form "arr=([index]=value)", so an attempt of assigning an initializer list to an indexed array no longer overwrites the original array with BashAssoc. With this change, the expected behaviors of three spec tests in spec/assoc.test.sh are changed. We adjust the expected results of the three tests and copy them into spec/ble-sparse.test.sh. We also leave BashAssoc versions of spec tests in spec/assoc.test.sh. * [spec/ysh-printing] Update spec tests for "assoc=([key]=value)"
The initialier-list implementation resolves the inconsistency in the sparse array representation of the form "declare -a sp=([2]=v)". This commit removes the "oils_failures_allowed" introduced in Ref. [1] (as a part of PR 2257, which turned on the sparse array representation of indexed arrays). [1] bfa7497#diff-097d35f191fa3ada9a05d61d3020a2b3acb43e800141b67021094fb05d8e769e
OSH now implements the feature required by spec/assoc#37 "Implicit increment of keys" so passes the test.
* [doc/ref/chap-osh-assign] Add descriptions about the initializer list * [doc/{ref/{chap-type-method,toc-osh},known-differences,quirks}] Update * [doc/ref/chap-osh-assign] Move the paragraph about =() vs +=() * [doc/ref/chap-osh-assign] Fix a typo "asso{ => ci}ative" * [doc/ref/chap-type-method] Mention sh-init-list for initialization/mutation * [doc/ref/toc-osh] Update an example for "sh-init-list" * [doc/ref/chap-osh-assign] Explain LHS type for the list initialization using a table * [doc/ref/chap-osh-assign] Explain list initialization of BashArray/BashAssoc using examples
13b26f1
to
ad997f1
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks great, thank you! I fixed a couple typos in the docs
I think we should consider the renaming mentioned
And I may make some more tweaks to the docs, but this is a big step forward!
See commit messages.
Note: Now
([k1]=v1 [k2]=v2)
is not a literal for BashAssocThe construct
(...)
ina=(...)
anda+=(...)
always produces the new type InitializerList regardless of the forms of its items. The LHS object receives the initializer list and reads items one by one to modify its content. When the variablea
is Undef,a
is initialized to be an indexed array and processes the initializer list, soa=([k1]=v1 [k2]=v2)
produces a sparse array (but not an associative array).