chaiscript_algebraic.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // This file is distributed under the BSD License.
  2. // See "license.txt" for details.
  3. // Copyright 2009-2012, Jonathan Turner ([email protected])
  4. // Copyright 2009-2017, Jason Turner ([email protected])
  5. // http://www.chaiscript.com
  6. // This is an open source non-commercial project. Dear PVS-Studio, please check it.
  7. // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
  8. #ifndef CHAISCRIPT_ALGEBRAIC_HPP_
  9. #define CHAISCRIPT_ALGEBRAIC_HPP_
  10. #include "../utility/fnv1a.hpp"
  11. #include <string>
  12. namespace chaiscript
  13. {
  14. struct Operators {
  15. enum class Opers
  16. {
  17. boolean_flag,
  18. equals, less_than, greater_than, less_than_equal, greater_than_equal, not_equal,
  19. non_const_flag,
  20. assign, pre_increment, pre_decrement, assign_product, assign_sum,
  21. assign_quotient, assign_difference,
  22. non_const_int_flag,
  23. assign_bitwise_and, assign_bitwise_or, assign_shift_left, assign_shift_right,
  24. assign_remainder, assign_bitwise_xor,
  25. const_int_flag,
  26. shift_left, shift_right, remainder, bitwise_and, bitwise_or, bitwise_xor, bitwise_complement,
  27. const_flag,
  28. sum, quotient, product, difference, unary_plus, unary_minus,
  29. invalid
  30. };
  31. static const char *to_string(Opers t_oper) {
  32. static const char *opers[] = {
  33. "",
  34. "==", "<", ">", "<=", ">=", "!=",
  35. "",
  36. "=", "++", "--", "*=", "+=",
  37. "/=", "-=",
  38. "",
  39. "&=", "|=", "<<=", ">>=",
  40. "%=", "^=",
  41. "",
  42. "<<", ">>", "%", "&", "|", "^", "~",
  43. "",
  44. "+", "/", "*", "-", "+", "-",
  45. ""
  46. };
  47. return opers[static_cast<int>(t_oper)];
  48. }
  49. static Opers to_operator(const std::string &t_str, bool t_is_unary = false)
  50. {
  51. #ifdef CHAISCRIPT_MSVC
  52. #pragma warning(push)
  53. #pragma warning(disable : 4307)
  54. #endif
  55. const auto op_hash = utility::fnv1a_32(t_str.c_str());
  56. switch (op_hash) {
  57. case utility::fnv1a_32("=="): { return Opers::equals; }
  58. case utility::fnv1a_32("<"): { return Opers::less_than; }
  59. case utility::fnv1a_32(">"): { return Opers::greater_than; }
  60. case utility::fnv1a_32("<="): { return Opers::less_than_equal; }
  61. case utility::fnv1a_32(">="): { return Opers::greater_than_equal; }
  62. case utility::fnv1a_32("!="): { return Opers::not_equal; }
  63. case utility::fnv1a_32("="): { return Opers::assign; }
  64. case utility::fnv1a_32("++"): { return Opers::pre_increment; }
  65. case utility::fnv1a_32("--"): { return Opers::pre_decrement; }
  66. case utility::fnv1a_32("*="): { return Opers::assign_product; }
  67. case utility::fnv1a_32("+="): { return Opers::assign_sum; }
  68. case utility::fnv1a_32("-="): { return Opers::assign_difference; }
  69. case utility::fnv1a_32("&="): { return Opers::assign_bitwise_and; }
  70. case utility::fnv1a_32("|="): { return Opers::assign_bitwise_or; }
  71. case utility::fnv1a_32("<<="): { return Opers::assign_shift_left; }
  72. case utility::fnv1a_32(">>="): { return Opers::assign_shift_right; }
  73. case utility::fnv1a_32("%="): { return Opers::assign_remainder; }
  74. case utility::fnv1a_32("^="): { return Opers::assign_bitwise_xor; }
  75. case utility::fnv1a_32("<<"): { return Opers::shift_left; }
  76. case utility::fnv1a_32(">>"): { return Opers::shift_right; }
  77. case utility::fnv1a_32("%"): { return Opers::remainder; }
  78. case utility::fnv1a_32("&"): { return Opers::bitwise_and; }
  79. case utility::fnv1a_32("|"): { return Opers::bitwise_or; }
  80. case utility::fnv1a_32("^"): { return Opers::bitwise_xor; }
  81. case utility::fnv1a_32("~"): { return Opers::bitwise_complement; }
  82. case utility::fnv1a_32("+"): { return t_is_unary ? Opers::unary_plus : Opers::sum; }
  83. case utility::fnv1a_32("-"): { return t_is_unary ? Opers::unary_minus : Opers::difference; }
  84. case utility::fnv1a_32("/"): { return Opers::quotient; }
  85. case utility::fnv1a_32("*"): { return Opers::product; }
  86. default: { return Opers::invalid; }
  87. }
  88. #ifdef CHAISCRIPT_MSVC
  89. #pragma warning(pop)
  90. #endif
  91. }
  92. };
  93. }
  94. #endif /* _CHAISCRIPT_ALGEBRAIC_HPP */