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

Refactors PrintStatement so that .values array is all expressions #1296

Merged
merged 8 commits into from
Jan 3, 2025

Conversation

markwpearce
Copy link
Collaborator

@markwpearce markwpearce commented Sep 9, 2024

; and , are converted to PrintSeparatorExpression

Addresses #1280

@markwpearce markwpearce added this to the v1.0.0 milestone Sep 9, 2024
@TwitchBronBron TwitchBronBron deleted the branch release-1.0.0 September 25, 2024 15:06
@TwitchBronBron TwitchBronBron reopened this Oct 1, 2024
Copy link
Member

@TwitchBronBron TwitchBronBron left a comment

Choose a reason for hiding this comment

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

The more I look at this, the more I think LiteralExpression is not the correct expression type for this. Perhaps we should make a new PrintSeparatorExpression instead, just to retain the semantic integrity of LiteralExpression.

@elsassph You've had a decent amount of experience with parsers. Any thoughts on how we'd approach this one?

@philippe-wm
Copy link
Contributor

It does feel wrong to make ; into an expression, but considering the way print parameters is modelled as an array of expression it is coherent.

I guess the alternative would be to model it as binary-expressions where ; or , is the operator, but that would be quite a breaking change.

@TwitchBronBron
Copy link
Member

Hmm. I'm not crazy about the idea of BinaryExpression to represent the print expression. Let's go with a new expression called PrintSeparatorExpression that looks similar to LiteralExpression but semantically means something else.

@TwitchBronBron
Copy link
Member

TwitchBronBron commented Oct 11, 2024

The more I dug into this, the more I dislike the concept of this expression basically not actually representing code.

I just had another thought. What if we stored an array of separators on .tokens instead? Something like this:

 constructor(options: {
        print: Token;
        expressions: Array<Expression>;
        /**
          * This list must always contain 1 less item than `expressions`. For example, `print 1, 2 ; 3` would contain
          *  expressions: [1, 2, 3] and separators: [',', ';']. 
          */
        separators: Array<Token>
    }) {
}

This keeps the separators separate from the actual "code", while retaining their position. It makes things slightly for annoying Ast creation, but we have similar problems with TemplateStringExpression, and we're not even capturing the commas for function parameters at this point.

@TwitchBronBron
Copy link
Member

TwitchBronBron commented Oct 11, 2024

That doesn't work quite right either. This silly print statement can have all sorts of crazy combinations.

image

@TwitchBronBron
Copy link
Member

I refactored this to leverage a new expression called PrintSeparatorExpression. However, it's still not feeling quite right, because these things are not actual expressions. :/

What about instead storing arrays between each section? Similar to one of my previous examples, except add another level of array.

 constructor(options: {
        print: Token;
        expressions: Array<Expression>;
        separators: Array<Array<Token>>
    }) {
}

Here's an example:

print ,,,1;2 3;;

We'd produce the following ast:

new PrintStatement({
    print: token('print'),
    expressions: [
        literal('1'), 
        literal('2'),
        literal('3')
    ],
    //separators can come before the first expression, so we start with them.
    separators: [
        [token(','), token(','), token(',')],
        [token(';')],
        //no separator between 2 and 3
        [],
        [token(';'), token(';')]
    ]
})

This feels more "correct" in that we don't have these weird expressions floating around that aren't actual code. However, it gets a bit extra gross neeting to maintain these deep nested lists of things. From a plugin perspective, it's a lot easier to build the list of print items just by pushing to a flat expressions list.

@markwpearce @philippe-wm any final thoughts? If you are both okay with the current implementation of PrintSeparatorExpression, then I'll merge it as-is and discard this idea.

@markwpearce
Copy link
Collaborator Author

@TwitchBronBron I think this is ready for merging. I'm happy with this approach.

@TwitchBronBron TwitchBronBron merged commit 4319d5c into release-1.0.0 Jan 3, 2025
6 checks passed
@TwitchBronBron TwitchBronBron deleted the better_PrintStatment_expressions branch January 3, 2025 14:39
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