Skip to content

Conversation

wcjohnson
Copy link

  • Added object comprehension parsing
  • Parse tree node is now Comprehension with a qualifier comprehends=object or comprehends=array

Copy link

@rattrayalex rattrayalex left a 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.

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?

Copy link
Author

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.

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...

Copy link
Author

@wcjohnson wcjohnson Mar 25, 2017

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.

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

this.expect(tt.bracketR);
return this.finishNode(node, "ArrayComprehension");
node.comprehends = "array";
return this.finishNode(node, "Comprehension");

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?

Copy link
Author

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.

Copy link
Author

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.

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)}

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...

Copy link
Author

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.

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

Choose a reason for hiding this comment

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

but point taken

Copy link
Author

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

@wcjohnson
Copy link
Author

@rattrayalex Comments addressed. Working on the AST transform next.

@rattrayalex
Copy link

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;

const { for elem x in arr: x, x } = {}

@wcjohnson
Copy link
Author

Good point, will add that test

@wcjohnson
Copy link
Author

All tests passing, everything looks good over here, if there's nothing else I will squash the branch.

@wcjohnson wcjohnson force-pushed the feature/comprehensions branch from 1d9e7e0 to 5a0125f Compare March 25, 2017 23:53
@wcjohnson
Copy link
Author

@rattrayalex Squashed anyway, can still make further changes if needed.

@wcjohnson wcjohnson changed the title [WIP] Comprehensions (lightscript/lightscript#6) Object comprehensions (lightscript/lightscript#6) Mar 25, 2017
Copy link

@rattrayalex rattrayalex left a 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.

@wcjohnson
Copy link
Author

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!

@rattrayalex
Copy link

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.
@wcjohnson wcjohnson force-pushed the feature/comprehensions branch from 5a0125f to 88785c3 Compare March 26, 2017 00:25
@wcjohnson
Copy link
Author

@rattrayalex

Added the test case; the output looks correct. I just amended and force pushed to avoid having to do another squash.

@rattrayalex
Copy link

rattrayalex commented Mar 26, 2017 via email

@rattrayalex rattrayalex merged commit fec945b into lightscript:lightscript Mar 26, 2017
@wcjohnson wcjohnson deleted the feature/comprehensions branch March 26, 2017 02:46
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.

2 participants