Skip to content

Add named expressions / assignment expressions - #1638

Closed
dralley wants to merge 1 commit into
RustPython:masterfrom
dralley:walrus
Closed

Add named expressions / assignment expressions#1638
dralley wants to merge 1 commit into
RustPython:masterfrom
dralley:walrus

Conversation

@dralley

@dralley dralley commented Dec 19, 2019

Copy link
Copy Markdown
Contributor

Comment thread compiler/src/compile.rs
) -> Result<(), CompileError> {

// evaluate value
// store into target

@dralley dralley Dec 19, 2019

Copy link
Copy Markdown
Contributor Author

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

Comment thread parser/src/python.lalrpop
node: ast::ExpressionType::NamedExpression { target: Box::new(e1), value: Box::new(e2) }
},
NamedExpression,
};

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread parser/src/ast.rs
| String {
value: FormattedValue { .. },
} => "f-string expression",
Identifier { .. } => "named expression",

@dralley dralley Dec 19, 2019

Copy link
Copy Markdown
Contributor Author

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?

Comment thread parser/src/ast.rs
values: Vec<Expression>,
},

/// A named expression aka. assignment expression aka. "Walrus operator"

Copy link
Copy Markdown
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
Copy Markdown
Member

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

@dralley

dralley commented Dec 19, 2019

Copy link
Copy Markdown
Contributor Author

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
Copy Markdown
Contributor

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

Comment thread parser/src/python.lalrpop
};

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

Copy link
Copy Markdown
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

dralley commented Jun 23, 2020

Copy link
Copy Markdown
Contributor Author

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