MCPcopy Index your code
hub / github.com/RustPython/RustPython / compile_expression

Method compile_expression

crates/codegen/src/compile.rs:7781–8270  ·  view source on GitHub ↗
(&mut self, expression: &ast::Expr)

Source from the content-addressed store, hash-verified

7779 }
7780
7781 fn compile_expression(&mut self, expression: &ast::Expr) -> CompileResult<()> {
7782 trace!("Compiling {expression:?}");
7783 let range = expression.range();
7784 self.set_source_range(range);
7785
7786 match &expression {
7787 ast::Expr::Call(ast::ExprCall {
7788 func, arguments, ..
7789 }) => self.compile_call(func, arguments)?,
7790 ast::Expr::BoolOp(ast::ExprBoolOp { op, values, .. }) => {
7791 self.compile_bool_op(op, values)?
7792 }
7793 ast::Expr::BinOp(ast::ExprBinOp {
7794 left, op, right, ..
7795 }) => {
7796 // optimize_format_str: 'format' % (args,) → f-string bytecode
7797 if matches!(op, ast::Operator::Mod)
7798 && let ast::Expr::StringLiteral(s) = left.as_ref()
7799 && let ast::Expr::Tuple(ast::ExprTuple { elts, .. }) = right.as_ref()
7800 && !elts.iter().any(|e| matches!(e, ast::Expr::Starred(_)))
7801 && self.try_optimize_format_str(s.value.to_str(), elts, range)?
7802 {
7803 return Ok(());
7804 }
7805 self.compile_expression(left)?;
7806 self.compile_expression(right)?;
7807
7808 // Restore full expression range before emitting the operation
7809 self.set_source_range(range);
7810 self.compile_op(op, false);
7811 }
7812 ast::Expr::Subscript(ast::ExprSubscript {
7813 value, slice, ctx, ..
7814 }) => {
7815 self.compile_subscript(value, slice, *ctx)?;
7816 }
7817 ast::Expr::UnaryOp(ast::ExprUnaryOp { op, operand, .. }) => {
7818 self.compile_expression(operand)?;
7819
7820 // Restore full expression range before emitting the operation
7821 self.set_source_range(range);
7822 match op {
7823 ast::UnaryOp::UAdd => emit!(
7824 self,
7825 Instruction::CallIntrinsic1 {
7826 func: bytecode::IntrinsicFunction1::UnaryPositive
7827 }
7828 ),
7829 ast::UnaryOp::USub => emit!(self, Instruction::UnaryNegative),
7830 ast::UnaryOp::Not => {
7831 emit!(self, Instruction::ToBool);
7832 emit!(self, Instruction::UnaryNot);
7833 }
7834 ast::UnaryOp::Invert => emit!(self, Instruction::UnaryInvert),
7835 };
7836 }
7837 ast::Expr::Attribute(ast::ExprAttribute { value, attr, .. }) => {
7838 // Check for super() attribute access optimization

Callers 15

compile_subscriptMethod · 0.80
starunpack_helperMethod · 0.80
load_args_for_superMethod · 0.80
compile_evalMethod · 0.80
compile_statementMethod · 0.80
compile_deleteMethod · 0.80
prepare_decoratorsMethod · 0.80
compile_try_statementMethod · 0.80

Calls 15

newFunction · 0.85
ruff_int_to_bigintFunction · 0.85
set_source_rangeMethod · 0.80
compile_callMethod · 0.80
compile_bool_opMethod · 0.80
to_strMethod · 0.80
compile_opMethod · 0.80
compile_subscriptMethod · 0.80
load_args_for_superMethod · 0.80

Tested by

no test coverage detected