| 188 | } |
| 189 | |
| 190 | absl::optional<Expr> ExpandMap2Macro(MacroExprFactory& factory, Expr& target, |
| 191 | absl::Span<Expr> args) { |
| 192 | if (args.size() != 2) { |
| 193 | return factory.ReportError("map() requires 2 arguments"); |
| 194 | } |
| 195 | if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) { |
| 196 | return factory.ReportErrorAt( |
| 197 | args[0], "map() variable name must be a simple identifier"); |
| 198 | } |
| 199 | if (args[0].ident_expr().name() == kAccumulatorVariableName) { |
| 200 | return factory.ReportErrorAt(args[1], |
| 201 | absl::StrCat("map() variable name cannot be ", |
| 202 | kAccumulatorVariableName)); |
| 203 | } |
| 204 | auto init = factory.NewList(); |
| 205 | auto condition = factory.NewBoolConst(true); |
| 206 | auto accu_ref = factory.NewAccuIdent(); |
| 207 | auto accu_update = |
| 208 | factory.NewList(factory.NewListElement(std::move(args[1]))); |
| 209 | auto step = factory.NewCall(CelOperator::ADD, std::move(accu_ref), |
| 210 | std::move(accu_update)); |
| 211 | return factory.NewComprehension(args[0].ident_expr().name(), |
| 212 | std::move(target), factory.AccuVarName(), |
| 213 | std::move(init), std::move(condition), |
| 214 | std::move(step), factory.NewAccuIdent()); |
| 215 | } |
| 216 | |
| 217 | Macro MakeMap2Macro() { |
| 218 | auto status_or_macro = Macro::Receiver(CelOperator::MAP, 2, ExpandMap2Macro); |
nothing calls this directly
no test coverage detected