MCPcopy Create free account
hub / github.com/cel-expr/cel-cpp / Calculate

Method Calculate

eval/eval/logic_step.cc:199–251  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

197
198 private:
199 void Calculate(ExecutionFrame* frame, absl::Span<const Value> args,
200 Value& result) const {
201 bool bool_args[2];
202 bool has_bool_args[2];
203
204 for (size_t i = 0; i < args.size(); i++) {
205 has_bool_args[i] = args[i]->Is<BoolValue>();
206 if (has_bool_args[i]) {
207 bool_args[i] = args[i].GetBool().NativeValue();
208 if (bool_args[i] == shortcircuit_) {
209 result = BoolValue{bool_args[i]};
210 return;
211 }
212 }
213 }
214
215 if (has_bool_args[0] && has_bool_args[1]) {
216 switch (op_type_) {
217 case OpType::kAnd:
218 result = BoolValue{bool_args[0] && bool_args[1]};
219 return;
220 case OpType::kOr:
221 result = BoolValue{bool_args[0] || bool_args[1]};
222 return;
223 }
224 }
225
226 // As opposed to regular function, logical operation treat Unknowns with
227 // higher precedence than error. This is due to the fact that after Unknown
228 // is resolved to actual value, it may short-circuit and thus hide the
229 // error.
230 if (frame->enable_unknowns()) {
231 // Check if unknown?
232 absl::optional<cel::UnknownValue> unknown_set =
233 frame->attribute_utility().MergeUnknowns(args);
234 if (unknown_set.has_value()) {
235 result = std::move(*unknown_set);
236 return;
237 }
238 }
239
240 if (args[0]->Is<cel::ErrorValue>()) {
241 result = args[0];
242 return;
243 } else if (args[1]->Is<cel::ErrorValue>()) {
244 result = args[1];
245 return;
246 }
247
248 // Fallback.
249 result = cel::ErrorValue(CreateNoMatchingOverloadError(
250 (op_type_ == OpType::kOr) ? cel::builtin::kOr : cel::builtin::kAnd));
251 }
252
253 const OpType op_type_;
254 bool shortcircuit_;

Callers

nothing calls this directly

Calls 8

ErrorValueFunction · 0.85
MergeUnknownsMethod · 0.80
sizeMethod · 0.45
NativeValueMethod · 0.45
GetBoolMethod · 0.45
enable_unknownsMethod · 0.45
has_valueMethod · 0.45

Tested by

no test coverage detected