xref: /aosp_15_r20/external/libtextclassifier/native/utils/grammar/semantics/evaluators/arithmetic-eval.cc (revision 993b0882672172b81d12fad7a7ac0c3e5c824a12)
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "utils/grammar/semantics/evaluators/arithmetic-eval.h"
18 
19 #include <limits>
20 
21 namespace libtextclassifier3::grammar {
22 namespace {
23 
24 template <typename T>
Reduce(const SemanticExpressionEvaluator * composer,const EvalContext & context,const ArithmeticExpression * expression,UnsafeArena * arena)25 StatusOr<const SemanticValue*> Reduce(
26     const SemanticExpressionEvaluator* composer, const EvalContext& context,
27     const ArithmeticExpression* expression, UnsafeArena* arena) {
28   T result;
29   switch (expression->op()) {
30     case ArithmeticExpression_::Operator_OP_ADD: {
31       result = 0;
32       break;
33     }
34     case ArithmeticExpression_::Operator_OP_MUL: {
35       result = 1;
36       break;
37     }
38     case ArithmeticExpression_::Operator_OP_MIN: {
39       result = std::numeric_limits<T>::max();
40       break;
41     }
42     case ArithmeticExpression_::Operator_OP_MAX: {
43       result = std::numeric_limits<T>::min();
44       break;
45     }
46     default: {
47       return Status(StatusCode::INVALID_ARGUMENT,
48                     "Unexpected op: " +
49                         std::string(ArithmeticExpression_::EnumNameOperator(
50                             expression->op())));
51     }
52   }
53   if (expression->values() != nullptr) {
54     for (const SemanticExpression* semantic_expression :
55          *expression->values()) {
56       TC3_ASSIGN_OR_RETURN(
57           const SemanticValue* value,
58           composer->Apply(context, semantic_expression, arena));
59       if (value == nullptr) {
60         continue;
61       }
62       if (!value->Has<T>()) {
63         return Status(
64             StatusCode::INVALID_ARGUMENT,
65             "Argument didn't evaluate as expected type: " +
66                 std::string(reflection::EnumNameBaseType(value->base_type())));
67       }
68       const T scalar_value = value->Value<T>();
69       switch (expression->op()) {
70         case ArithmeticExpression_::Operator_OP_ADD: {
71           result += scalar_value;
72           break;
73         }
74         case ArithmeticExpression_::Operator_OP_MUL: {
75           result *= scalar_value;
76           break;
77         }
78         case ArithmeticExpression_::Operator_OP_MIN: {
79           result = std::min(result, scalar_value);
80           break;
81         }
82         case ArithmeticExpression_::Operator_OP_MAX: {
83           result = std::max(result, scalar_value);
84           break;
85         }
86         default: {
87           break;
88         }
89       }
90     }
91   }
92   return SemanticValue::Create(result, arena);
93 }
94 
95 }  // namespace
96 
Apply(const EvalContext & context,const SemanticExpression * expression,UnsafeArena * arena) const97 StatusOr<const SemanticValue*> ArithmeticExpressionEvaluator::Apply(
98     const EvalContext& context, const SemanticExpression* expression,
99     UnsafeArena* arena) const {
100   TC3_DCHECK_EQ(expression->expression_type(),
101                 SemanticExpression_::Expression_ArithmeticExpression);
102   const ArithmeticExpression* arithmetic_expression =
103       expression->expression_as_ArithmeticExpression();
104   switch (arithmetic_expression->base_type()) {
105     case reflection::BaseType::Byte:
106       return Reduce<int8>(composer_, context, arithmetic_expression, arena);
107     case reflection::BaseType::UByte:
108       return Reduce<uint8>(composer_, context, arithmetic_expression, arena);
109     case reflection::BaseType::Short:
110       return Reduce<int16>(composer_, context, arithmetic_expression, arena);
111     case reflection::BaseType::UShort:
112       return Reduce<uint16>(composer_, context, arithmetic_expression, arena);
113     case reflection::BaseType::Int:
114       return Reduce<int32>(composer_, context, arithmetic_expression, arena);
115     case reflection::BaseType::UInt:
116       return Reduce<uint32>(composer_, context, arithmetic_expression, arena);
117     case reflection::BaseType::Long:
118       return Reduce<int64>(composer_, context, arithmetic_expression, arena);
119     case reflection::BaseType::ULong:
120       return Reduce<uint64>(composer_, context, arithmetic_expression, arena);
121     case reflection::BaseType::Float:
122       return Reduce<float>(composer_, context, arithmetic_expression, arena);
123     case reflection::BaseType::Double:
124       return Reduce<double>(composer_, context, arithmetic_expression, arena);
125     default:
126       return Status(StatusCode::INVALID_ARGUMENT,
127                     "Unsupported for ArithmeticExpression: " +
128                         std::string(reflection::EnumNameBaseType(
129                             static_cast<reflection::BaseType>(
130                                 arithmetic_expression->base_type()))));
131   }
132 }
133 
134 }  // namespace libtextclassifier3::grammar
135