| 221 | } |
| 222 | |
| 223 | absl::optional<Expr> ExpandMap3Macro(MacroExprFactory& factory, Expr& target, |
| 224 | absl::Span<Expr> args) { |
| 225 | if (args.size() != 3) { |
| 226 | return factory.ReportError("map() requires 3 arguments"); |
| 227 | } |
| 228 | if (!args[0].has_ident_expr() || args[0].ident_expr().name().empty()) { |
| 229 | return factory.ReportErrorAt( |
| 230 | args[0], "map() variable name must be a simple identifier"); |
| 231 | } |
| 232 | if (args[0].ident_expr().name() == kAccumulatorVariableName) { |
| 233 | return factory.ReportErrorAt(args[1], |
| 234 | absl::StrCat("map() variable name cannot be ", |
| 235 | kAccumulatorVariableName)); |
| 236 | } |
| 237 | auto init = factory.NewList(); |
| 238 | auto condition = factory.NewBoolConst(true); |
| 239 | auto accu_ref = factory.NewAccuIdent(); |
| 240 | auto accu_update = |
| 241 | factory.NewList(factory.NewListElement(std::move(args[2]))); |
| 242 | auto step = factory.NewCall(CelOperator::ADD, std::move(accu_ref), |
| 243 | std::move(accu_update)); |
| 244 | step = factory.NewCall(CelOperator::CONDITIONAL, std::move(args[1]), |
| 245 | std::move(step), factory.NewAccuIdent()); |
| 246 | return factory.NewComprehension(args[0].ident_expr().name(), |
| 247 | std::move(target), factory.AccuVarName(), |
| 248 | std::move(init), std::move(condition), |
| 249 | std::move(step), factory.NewAccuIdent()); |
| 250 | } |
| 251 | |
| 252 | Macro MakeMap3Macro() { |
| 253 | auto status_or_macro = Macro::Receiver(CelOperator::MAP, 3, ExpandMap3Macro); |
nothing calls this directly
no test coverage detected