Add null coalescing operator.

This commit is contained in:
Stephen Chung
2022-06-10 11:22:33 +08:00
parent 0f1e51b1c9
commit 8999872d62
7 changed files with 87 additions and 15 deletions

View File

@@ -1073,6 +1073,16 @@ fn optimize_expr(expr: &mut Expr, state: &mut OptimizerState, _chaining: bool) {
// lhs || rhs
(lhs, rhs) => { optimize_expr(lhs, state, false); optimize_expr(rhs, state, false); }
},
// () ?? rhs -> rhs
Expr::Coalesce(x, ..) if matches!(x.lhs, Expr::Unit(..)) => {
state.set_dirty();
*expr = mem::take(&mut x.rhs);
},
// lhs:constant ?? rhs -> lhs
Expr::Coalesce(x, ..) if x.lhs.is_constant() => {
state.set_dirty();
*expr = mem::take(&mut x.lhs);
},
// eval!
Expr::FnCall(x, ..) if x.name == KEYWORD_EVAL => {