-
Notifications
You must be signed in to change notification settings - Fork 5
Object comprehensions (lightscript/lightscript#6) #7
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
Object comprehensions (lightscript/lightscript#6) #7
Conversation
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.
nice, looking good!
|
||
// `for` keyword begins an object comprehension. | ||
if (this.hasPlugin("lightscript") && this.match(tt._for)) { | ||
// ...however, `{ for: x }` is a legal JS object. |
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.
ah, that does complicate things...
At least we dont have to deal with myAnnoyingDestructuredObj = { for }
.
I'd almost rather just add a restriction to the language saying that if you want an object where the first key is for
, you need to wrap it in quotes: { "for": "reasons" }
.
It'd be less confusing as a reader in that case anyway. Thoughts?
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.
Generally speaking I am really against weird edge cases like this that programmers have to remember. I think we can fairly frictionlessly integrate with previously-legal JS at the cost of a lookahead in the parser, which is not that big of a deal. My opinion is we should let people write { for: 4} if they really want to.
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 don't have hard numbers on lookahead perf, but I'm pretty sure it's just about the slowest thing that we can do in the parser 😉 compiler perf can really make a difference to UX.
hmm...
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've been looking at the lookahead code and I think we can actually do this in a performant way. Since our lookahead here isn't actually relying on the whole parser state, nor do we call any functions during the lookahead, we may be able to do a "cheap" lookahead where we ignore the parser state altogether.
That should eliminate any object copying and shouldn't be much more expensive than calling substr
or whatever the tokenizer does.
This is the sort of thing I am less likely to worry about than getting the semantics right, but hey, everyone wants their code to compile fast.
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'm pretty nervous about doing something like that... My impression of the way the parser is set up makes me think that could really open a can of worms... would much rather just take the perf hit
src/plugins/lightscript.js
Outdated
this.expect(tt.bracketR); | ||
return this.finishNode(node, "ArrayComprehension"); | ||
node.comprehends = "array"; | ||
return this.finishNode(node, "Comprehension"); |
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 I'd rather stick with two node types, ArrayComprehension
and ObjectComprehension
. Sound ok to you?
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'm OK either way (in fact, leaving it is less work for me on updating unit tests) but my thinking here is that in later ast transforms we may want a single node type. Especially since the code for both types of comprehensions is going to be essentially the same.
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.
On second thoughts I think I agree with you, worst case we can just do a type union in the ast visitor. I will use two different node types.
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.
Yeah, see comment in lightscript/lightscript#6, would rather do code-sharing with helper-functions. Node types should be based around what the thing is, not what makes for easier internals
@@ -0,0 +1 @@ | |||
{for i=0; i<10; i++: (i,i)} |
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.
yeah, let's avoid for-;;
in tests if possible...
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.
It's your show, but I don't fully agree with this. I think a key philosophical point in eliminating the "big table of for" is that good old fashioned for loops are perfectly acceptable for just counting numbers.
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.
sorry, should have clarified that one of the reasons is that they just generate much larger AST's than simpler for-loops, making them harder to review
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.
but point taken
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.
oh, I see what you mean. OK, will change
@rattrayalex Comments addressed. Working on the AST transform next. |
Cool, I haven't combed through the tests a second time but I assume that's good; want to squash and I'll merge? Oh, we may want to add a test that it errors acceptably if an object comprehension is put somewhere that an object pattern is expected. Eg;
|
Good point, will add that test |
All tests passing, everything looks good over here, if there's nothing else I will squash the branch. |
1d9e7e0
to
5a0125f
Compare
@rattrayalex Squashed anyway, can still make further changes if needed. |
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.
A bunch of these tests are probably unnecessary/redundant, but not worth taking the time to decide which ones.
One thing I don't see are any tests checking for the SequenceExpression without parens. Parens may be a good style for that (not sure what I think personally yet) but both need to be parseable.
A single test without the parens would probably be fine; I can't think of a scenario here where I would expect it to cause an issue. But one can imagine a future change that might break.
Will add additional test for that. I'm sure some of the fixtures are redundant, but I'd rather err on the side of having too many! |
Cool, should be good to merge after that! |
(+7 squashed commits) Squashed commits: [ef65cfe] Update new fixtures to simplify parse tree output [850bd16] Return to using two different parse tree nodes for `ObjectComprehension` vs `ArrayComprehension` [9d9b90d] Revert "Updated existing test fixtures for node type change" This reverts commit d00dbd1. [47b5181] Added test case for `for` as object key [2f28e50] New test fixtures for object comprehensions [d00dbd1] Updated existing test fixtures for node type change [31fe13b] - Added parsing for Object comprehensions - Changed generated parse tree node to `Comprehension` type.
5a0125f
to
88785c3
Compare
Added the test case; the output looks correct. I just amended and force pushed to avoid having to do another squash. |
Yeah that's what I normally do.
Cool will merge!
…On Sat, Mar 25, 2017, 17:26 William C. Johnson ***@***.***> wrote:
@rattrayalex <https://github.com/rattrayalex>
Added the test case; the output looks correct. I just amended and force
pushed to avoid having to do another squash.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#7 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAq_LrI5ZRaZhBFMtD_PuWEn8IPZFAPuks5rpbCegaJpZM4MpK0u>
.
|
Comprehension
with a qualifiercomprehends=object
orcomprehends=array