Add named expressions / assignment expressions#1638
Add named expressions / assignment expressions#1638dralley wants to merge 1 commit intoRustPython:masterfrom
Conversation
| ) -> Result<(), CompileError> { | ||
|
|
||
| // evaluate value | ||
| // store into target |
There was a problem hiding this comment.
TODO -- still having trouble w/ parsing
| node: ast::ExpressionType::NamedExpression { target: Box::new(e1), value: Box::new(e2) } | ||
| }, | ||
| NamedExpression, | ||
| }; |
There was a problem hiding this comment.
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 ':='
There was a problem hiding this comment.
It looks like you might want to use TestOrStarExpr, as that's what the assignment statement does:
RustPython/parser/src/python.lalrpop
Line 88 in ed37c61
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| | String { | ||
| value: FormattedValue { .. }, | ||
| } => "f-string expression", | ||
| Identifier { .. } => "named expression", |
There was a problem hiding this comment.
This was a typo, right? Or maybe it predates "named expressions" becoming an actual thing in Python?
| values: Vec<Expression>, | ||
| }, | ||
|
|
||
| /// A named expression aka. assignment expression aka. "Walrus operator" |
There was a problem hiding this comment.
The fact that the PEP and the implementation use different terminology is somewhat irritating.
|
I haven't reviewed this yet, but you can also refer to #968 for the walrus lexing implementation. |
|
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. |
|
Since I opened that other pull request, I would like to see the walrus coming :). |
| }; | ||
|
|
||
| NamedExpression: ast::Expression = { | ||
| <e1:Expression> <location:@L> ":=" <e2:Expression> => ast::Expression { |
There was a problem hiding this comment.
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>.
Aka "walrus operator". https://www.python.org/dev/peps/pep-0572/
|
Closed by #1934 |
Aka "walrus operator".
https://www.python.org/dev/peps/pep-0572/