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

Function fold_expr

crates/vm/src/stdlib/_ast.rs:661–711  ·  view source on GitHub ↗
(expr: &mut ast::Expr)

Source from the content-addressed store, hash-verified

659
660#[cfg(feature = "parser")]
661fn fold_expr(expr: &mut ast::Expr) {
662 use ast::Expr;
663 if let Expr::UnaryOp(unary) = expr {
664 fold_expr(&mut unary.operand);
665 if matches!(unary.op, ast::UnaryOp::USub)
666 && let Expr::NumberLiteral(number_literal) = unary.operand.as_ref()
667 {
668 let number = match &number_literal.value {
669 ast::Number::Int(value) => {
670 if *value == ast::Int::ZERO {
671 Some(ast::Number::Int(ast::Int::ZERO))
672 } else {
673 None
674 }
675 }
676 ast::Number::Float(value) => Some(ast::Number::Float(-value)),
677 ast::Number::Complex { real, imag } => Some(ast::Number::Complex {
678 real: -real,
679 imag: -imag,
680 }),
681 };
682 if let Some(number) = number {
683 *expr = Expr::NumberLiteral(ast::ExprNumberLiteral {
684 node_index: unary.node_index.clone(),
685 range: unary.range,
686 value: number,
687 });
688 return;
689 }
690 }
691 }
692 if let Expr::BinOp(binop) = expr {
693 fold_expr(&mut binop.left);
694 fold_expr(&mut binop.right);
695
696 let Expr::NumberLiteral(left) = binop.left.as_ref() else {
697 return;
698 };
699 let Expr::NumberLiteral(right) = binop.right.as_ref() else {
700 return;
701 };
702
703 if let Some(number) = fold_number_binop(&left.value, &binop.op, &right.value) {
704 *expr = Expr::NumberLiteral(ast::ExprNumberLiteral {
705 node_index: binop.node_index.clone(),
706 range: binop.range,
707 value: number,
708 });
709 }
710 }
711}
712
713#[cfg(feature = "parser")]
714fn fold_number_binop(

Callers 2

fold_stmtFunction · 0.85
fold_patternFunction · 0.85

Calls 6

fold_number_binopFunction · 0.85
SomeClass · 0.50
IntClass · 0.50
FloatClass · 0.50
as_refMethod · 0.45
cloneMethod · 0.45

Tested by

no test coverage detected