-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Conversation
) -> Result<(), CompileError> { | ||
|
||
// evaluate value | ||
// store into target |
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.
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.
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 ':='
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 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
<location:@L> <expression:TestOrStarExprList> <suffix:AssignSuffix*> => { |
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 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.
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", |
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.
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" |
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.
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 :). |
@@ -787,6 +787,14 @@ MulOp: ast::Operator = { | |||
"@" => ast::Operator::MatMult, | |||
}; | |||
|
|||
NamedExpression: ast::Expression = { | |||
<e1:Expression> <location:@L> ":=" <e2:Expression> => ast::Expression { |
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.
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/