Skip to content

Add named expressions / assignment expressions #1638

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

Closed
wants to merge 1 commit into from

Conversation

dralley
Copy link
Contributor

@dralley dralley commented Dec 19, 2019

) -> Result<(), CompileError> {

// evaluate value
// store into target
Copy link
Contributor Author

@dralley dralley Dec 19, 2019

Choose a reason for hiding this comment

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

TODO -- still having trouble w/ parsing

node: ast::ExpressionType::NamedExpression { target: Box::new(e1), value: Box::new(e2) }
},
NamedExpression,
};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Probably this is wrong. I'm getting LalrpopError::UnrecognizedToken->ParseErrorType::UnrecognizedToken

>>>>> if any(len(longline := line) >= 100 for line in lines):
SyntaxError: 
if any(len(longline := line) >= 100 for line in lines):
                    ↑
                    Got unexpected token ':='

Copy link
Member

Choose a reason for hiding this comment

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

It looks like you might want to use TestOrStarExpr, as that's what the assignment statement does:

<location:@L> <expression:TestOrStarExprList> <suffix:AssignSuffix*> => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It didn't work unfortunately. Might still be doing something wrong, idk. I'll take another shot after New Years and do some deeper digging.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please check with the grammar on this page: https://docs.python.org/3/reference/grammar.html?highlight=grammar that contains useful tips on what to modify.

@@ -359,7 +366,7 @@ impl Expression {
| String {
value: FormattedValue { .. },
} => "f-string expression",
Identifier { .. } => "named expression",
Copy link
Contributor Author

@dralley dralley Dec 19, 2019

Choose a reason for hiding this comment

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

This was a typo, right? Or maybe it predates "named expressions" becoming an actual thing in Python?

@@ -186,6 +186,12 @@ pub enum ExpressionType {
values: Vec<Expression>,
},

/// A named expression aka. assignment expression aka. "Walrus operator"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fact that the PEP and the implementation use different terminology is somewhat irritating.

@coolreader18
Copy link
Member

I haven't reviewed this yet, but you can also refer to #968 for the walrus lexing implementation.

@dralley
Copy link
Contributor Author

dralley commented Dec 19, 2019

Ah, I didn't see that PR, thank you. It looks like it only added it to the lexer though, and my PR does the same thing in that respect (apart from slightly different token name). It's the parsing and the compiling that I'm hung up on (first time dong anything like this).

Although re: the discussion on that PR, I can shelve this for now if the project doesn't want to add 3.8 features just yet.

@windelbouwman
Copy link
Contributor

Since I opened that other pull request, I would like to see the walrus coming :).

@@ -787,6 +787,14 @@ MulOp: ast::Operator = {
"@" => ast::Operator::MatMult,
};

NamedExpression: ast::Expression = {
<e1:Expression> <location:@L> ":=" <e2:Expression> => ast::Expression {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please keep in mind here to check with this page: https://docs.python.org/3/reference/grammar.html?highlight=grammar

That's the reference I used when making the rest of this file. Instead of <e1:Expression> you might want to go for <e1:Test>.

@dralley
Copy link
Contributor Author

dralley commented Jun 23, 2020

Closed by #1934

@dralley dralley closed this Jun 23, 2020
@dralley dralley deleted the walrus branch June 24, 2020 14:53
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