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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions compiler/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1565,6 +1565,17 @@ impl<O: OutputStream> Compiler<O> {
Ok(())
}

fn compile_named_expression(
&mut self,
target: &ast::Expression,
value: &ast::Expression,
) -> 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

Ok(())
}

fn compile_expression(&mut self, expression: &ast::Expression) -> Result<(), CompileError> {
trace!("Compiling {:?}", expression);
self.set_source_location(&expression.location);
Expand All @@ -1577,6 +1588,7 @@ impl<O: OutputStream> Compiler<O> {
keywords,
} => self.compile_call(function, args, keywords)?,
BoolOp { op, values } => self.compile_bool_op(op, values)?,
NamedExpression { target, value} => self.compile_named_expression(target, value)?,
Binop { a, op, b } => {
self.compile_expression(a)?;
self.compile_expression(b)?;
Expand Down Expand Up @@ -1913,6 +1925,7 @@ impl<O: OutputStream> Compiler<O> {
Ok(has_stars)
}


fn compile_comprehension(
&mut self,
kind: &ast::ComprehensionKind,
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/symboltable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,10 @@ impl SymbolTableBuilder {
self.scan_expression(a, context)?;
self.scan_expression(b, context)?;
}
NamedExpression {target, value} => {
self.scan_expression(target, &ExpressionContext::Store)?;
self.scan_expression(value, &ExpressionContext::Load)?;
},
BoolOp { values, .. } => {
self.scan_expressions(values, context)?;
}
Expand Down
9 changes: 8 additions & 1 deletion parser/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.

NamedExpression {
target: Box<Expression>,
value: Box<Expression>,
},

/// A binary operation on two operands.
Binop {
a: Box<Expression>,
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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?

Identifier { .. } => "identifier",
Lambda { .. } => "lambda",
IfExpression { .. } => "conditional expression",
True | False | None => "keyword",
Expand Down
11 changes: 10 additions & 1 deletion parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,16 @@ where
self.nesting -= 1;
}
':' => {
self.eat_single_char(Tok::Colon);
let tok_start = self.get_pos();
self.next_char();
if let Some('=') = self.chr0 {
self.next_char();
let tok_end = self.get_pos();
self.emit((tok_start, Tok::ColonEqual, tok_end));
} else {
let tok_end = self.get_pos();
self.emit((tok_start, Tok::Colon, tok_end));
}
}
';' => {
self.eat_single_char(Tok::Semi);
Expand Down
9 changes: 9 additions & 0 deletions parser/src/python.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -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>.

location,
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.


Factor: ast::Expression = {
<location:@L> <op:UnOp> <e:Factor> => ast::Expression {
location,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions parser/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ pub enum Tok {
CircumflexEqual, // '^='
LeftShiftEqual,
RightShiftEqual,
ColonEqual,
DoubleSlash, // '//'
DoubleSlashEqual,
At,
Expand Down Expand Up @@ -181,6 +182,7 @@ impl fmt::Display for Tok {
CircumflexEqual => f.write_str("'^='"),
LeftShiftEqual => f.write_str("'<<='"),
RightShiftEqual => f.write_str("'>>='"),
ColonEqual => f.write_str("':='"),
DoubleSlash => f.write_str("'//'"),
DoubleSlashEqual => f.write_str("'//='"),
At => f.write_str("'@'"),
Expand Down
3 changes: 3 additions & 0 deletions vm/src/exceptions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ pub struct ExceptionZoo {
pub syntax_error: PyClassRef,
pub indentation_error: PyClassRef,
pub tab_error: PyClassRef,
pub target_scope_error: PyClassRef,
pub system_error: PyClassRef,
pub type_error: PyClassRef,
pub value_error: PyClassRef,
Expand Down Expand Up @@ -473,6 +474,7 @@ impl ExceptionZoo {
let eof_error = create_type("EOFError", &type_type, &exception_type);
let indentation_error = create_type("IndentationError", &type_type, &syntax_error);
let tab_error = create_type("TabError", &type_type, &indentation_error);
let target_scope_error = create_type("TargetScopeError", &type_type, &syntax_error);
let unicode_error = create_type("UnicodeError", &type_type, &value_error);
let unicode_decode_error = create_type("UnicodeDecodeError", &type_type, &unicode_error);
let unicode_encode_error = create_type("UnicodeEncodeError", &type_type, &unicode_error);
Expand Down Expand Up @@ -571,6 +573,7 @@ impl ExceptionZoo {
keyboard_interrupt,
generator_exit,
system_exit,
target_scope_error,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions vm/src/stdlib/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,12 @@ fn expression_to_ast(vm: &VirtualMachine, expression: &ast::Expression) -> PyRes
args => expressions_to_ast(vm, args)?,
keywords => map_ast(keyword_to_ast, vm, keywords)?,
}),
NamedExpression {target, value} => {
node!(vm, NamedExpression, {
target => expression_to_ast(vm, target)?,
value => expression_to_ast(vm, value)?,
})
},
Binop { a, op, b } => {
// Operator:
node!(vm, BinOp, {
Expand Down