-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 commentThe 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. |
||
NamedExpression { | ||
target: Box<Expression>, | ||
value: Box<Expression>, | ||
}, | ||
|
||
/// A binary operation on two operands. | ||
Binop { | ||
a: Box<Expression>, | ||
|
@@ -330,6 +336,7 @@ impl Expression { | |
|
||
match &self.node { | ||
BoolOp { .. } | Binop { .. } | Unop { .. } => "operator", | ||
NamedExpression { .. } => "named expression", | ||
Subscript { .. } => "subscript", | ||
Await { .. } => "await expression", | ||
Yield { .. } | YieldFrom { .. } => "yield expression", | ||
|
@@ -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 commentThe 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? |
||
Identifier { .. } => "identifier", | ||
Lambda { .. } => "lambda", | ||
IfExpression { .. } => "conditional expression", | ||
True | False | None => "keyword", | ||
|
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -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 commentThe 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 |
||||
location, | ||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Probably this is wrong. I'm getting
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like you might want to use RustPython/parser/src/python.lalrpop Line 88 in ed37c61
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe 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. |
||||
|
||||
Factor: ast::Expression = { | ||||
<location:@L> <op:UnOp> <e:Factor> => ast::Expression { | ||||
location, | ||||
|
@@ -1169,6 +1177,7 @@ extern { | |||
"//=" => lexer::Tok::DoubleSlashEqual, | ||||
"==" => lexer::Tok::EqEqual, | ||||
"!=" => lexer::Tok::NotEqual, | ||||
":=" => lexer::Tok::ColonEqual, | ||||
"<" => lexer::Tok::Less, | ||||
"<=" => lexer::Tok::LessEqual, | ||||
">" => lexer::Tok::Greater, | ||||
|
Uh oh!
There was an error while loading. Please reload this page.
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