runCel function

({bool allowed, Map<String, int> spends}) runCel(
  1. CelProgram program,
  2. CelEnvironment env
)

Evaluates program in env, and what its spend("…", n) calls spend when it allows: the credits on the path that allowed it, summed by name (05 §3.4); empty when it denies. Throws as evaluateCel does.

Implementation

({bool allowed, Map<String, int> spends}) runCel(
  CelProgram program,
  CelEnvironment env,
) {
  final evaluator = _Evaluator(env);
  final result = evaluator.eval(program._root);
  if (result is! _BoolValue) {
    throw const CelEvaluationError('expression did not produce a boolean');
  }
  final spends = <String, int>{};
  if (result.v) {
    for (final (credits, amount) in evaluator.spent) {
      spends[credits] = (spends[credits] ?? 0) + amount;
    }
  }
  return (allowed: result.v, spends: spends);
}