3
0

MathExpressionOperators.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Math/MathUtils.h>
  9. #include <ExpressionEngine/MathOperators/MathExpressionOperators.h>
  10. #include <ExpressionEngine/Utils.h>
  11. #include <ExpressionEvaluation/ExpressionEngine/ExpressionTypes.h>
  12. namespace ExpressionEvaluation
  13. {
  14. ////////////////////////////
  15. // MathExpressionOperators
  16. ////////////////////////////
  17. ElementInformation MathExpressionOperators::AddOperator()
  18. {
  19. ElementInformation elementInfo;
  20. elementInfo.m_id = MathExpressionOperators::Add;
  21. elementInfo.m_priority = AddSubtract;
  22. return elementInfo;
  23. }
  24. ElementInformation MathExpressionOperators::SubtractOperator()
  25. {
  26. ElementInformation elementInfo;
  27. elementInfo.m_id = MathExpressionOperators::Subtract;
  28. elementInfo.m_priority = AddSubtract;
  29. return elementInfo;
  30. }
  31. ElementInformation MathExpressionOperators::MultiplyOperator()
  32. {
  33. ElementInformation elementInfo;
  34. elementInfo.m_id = MathExpressionOperators::Multiply;
  35. elementInfo.m_priority = MultiplyDivideModulo;
  36. return elementInfo;
  37. }
  38. ElementInformation MathExpressionOperators::DivideOperator()
  39. {
  40. ElementInformation elementInfo;
  41. elementInfo.m_id = MathExpressionOperators::Divide;
  42. elementInfo.m_priority = MultiplyDivideModulo;
  43. return elementInfo;
  44. }
  45. ElementInformation MathExpressionOperators::ModuloOperator()
  46. {
  47. ElementInformation elementInfo;
  48. elementInfo.m_id = MathExpressionOperators::Modulo;
  49. elementInfo.m_priority = MultiplyDivideModulo;
  50. return elementInfo;
  51. }
  52. ExpressionParserId MathExpressionOperators::GetParserId() const
  53. {
  54. return Interfaces::MathOperators;
  55. }
  56. MathExpressionOperators::ParseResult MathExpressionOperators::ParseElement(const AZStd::string& inputText, size_t offset) const
  57. {
  58. ParseResult result;
  59. char firstChar = inputText.at(offset);
  60. if (firstChar == '+')
  61. {
  62. result.m_charactersConsumed = 1;
  63. result.m_element = AddOperator();
  64. }
  65. else if (firstChar == '-')
  66. {
  67. result.m_charactersConsumed = 1;
  68. result.m_element = SubtractOperator();
  69. }
  70. else if (firstChar == '*')
  71. {
  72. result.m_charactersConsumed = 1;
  73. result.m_element = MultiplyOperator();
  74. }
  75. else if (firstChar == '/')
  76. {
  77. result.m_charactersConsumed = 1;
  78. result.m_element = DivideOperator();
  79. }
  80. else if (firstChar == '%')
  81. {
  82. result.m_charactersConsumed = 1;
  83. result.m_element = ModuloOperator();
  84. }
  85. return result;
  86. }
  87. void MathExpressionOperators::EvaluateToken(const ElementInformation& elementInformation, ExpressionResultStack& resultStack) const
  88. {
  89. if (resultStack.size() < 2)
  90. {
  91. return;
  92. }
  93. auto rightValue = resultStack.PopAndReturn();
  94. auto leftValue = resultStack.PopAndReturn();
  95. ExpressionResult result;
  96. switch (elementInformation.m_id)
  97. {
  98. case Add:
  99. result = OnAddOperator(leftValue, rightValue);
  100. break;
  101. case Subtract:
  102. result = OnSubtractOperator(leftValue, rightValue);
  103. break;
  104. case Multiply:
  105. result = OnMultiplyOperator(leftValue, rightValue);
  106. break;
  107. case Divide:
  108. result = OnDivideOperator(leftValue, rightValue);
  109. break;
  110. case Modulo:
  111. result = OnModuloOperator(leftValue, rightValue);
  112. break;
  113. default:
  114. break;
  115. }
  116. if (!result.empty())
  117. {
  118. resultStack.emplace(AZStd::move(result));
  119. }
  120. }
  121. ExpressionResult MathExpressionOperators::OnAddOperator(const AZStd::any& leftValue, const AZStd::any& rightValue) const
  122. {
  123. double lhsValue = Utils::GetAnyValue<double>(leftValue);
  124. double rhsValue = Utils::GetAnyValue<double>(rightValue);
  125. return ExpressionResult(lhsValue + rhsValue);
  126. }
  127. ExpressionResult MathExpressionOperators::OnSubtractOperator(const AZStd::any& leftValue, const AZStd::any& rightValue) const
  128. {
  129. double lhsValue = Utils::GetAnyValue<double>(leftValue);
  130. double rhsValue = Utils::GetAnyValue<double>(rightValue);
  131. return ExpressionResult(lhsValue - rhsValue);
  132. }
  133. ExpressionResult MathExpressionOperators::OnMultiplyOperator(const AZStd::any& leftValue, const AZStd::any& rightValue) const
  134. {
  135. double lhsValue = Utils::GetAnyValue<double>(leftValue);
  136. double rhsValue = Utils::GetAnyValue<double>(rightValue);
  137. return ExpressionResult(lhsValue * rhsValue);
  138. }
  139. ExpressionResult MathExpressionOperators::OnDivideOperator(const AZStd::any& leftValue, const AZStd::any& rightValue) const
  140. {
  141. double lhsValue = Utils::GetAnyValue<double>(leftValue);
  142. double rhsValue = Utils::GetAnyValue<double>(rightValue);
  143. if (!AZ::IsClose(rhsValue, 0.0, std::numeric_limits<double>::epsilon()))
  144. {
  145. return ExpressionResult(lhsValue / rhsValue);
  146. }
  147. return ExpressionResult();
  148. }
  149. ExpressionResult MathExpressionOperators::OnModuloOperator(const AZStd::any& leftValue, const AZStd::any& rightValue) const
  150. {
  151. double lhsValue = Utils::GetAnyValue<double>(leftValue);
  152. double rhsValue = Utils::GetAnyValue<double>(rightValue);
  153. if (!AZ::IsClose(rhsValue, 0.0, std::numeric_limits<double>().epsilon()))
  154. {
  155. return ExpressionResult(aznumeric_cast<double>(aznumeric_cast<int>(lhsValue) % aznumeric_cast<int>(rhsValue)));
  156. }
  157. return ExpressionResult();
  158. }
  159. }