ChaiScript
chaiscript_algebraic.hpp
1 // This file is distributed under the BSD License.
2 // See "license.txt" for details.
3 // Copyright 2009-2012, Jonathan Turner (jonathan@emptycrate.com)
4 // Copyright 2009-2018, Jason Turner (jason@emptycrate.com)
5 // http://www.chaiscript.com
6 
7 // This is an open source non-commercial project. Dear PVS-Studio, please check it.
8 // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
9 
10 #ifndef CHAISCRIPT_ALGEBRAIC_HPP_
11 #define CHAISCRIPT_ALGEBRAIC_HPP_
12 
13 #include "../utility/hash.hpp"
14 
15 #include <array>
16 #include <string>
17 
18 namespace chaiscript {
19  struct Operators {
20  enum class Opers {
21  equals,
22  less_than,
23  greater_than,
24  less_than_equal,
25  greater_than_equal,
26  not_equal,
27  assign,
28  pre_increment,
29  pre_decrement,
30  assign_product,
31  assign_sum,
32  assign_quotient,
33  assign_difference,
34  assign_bitwise_and,
35  assign_bitwise_or,
36  assign_shift_left,
37  assign_shift_right,
38  assign_remainder,
39  assign_bitwise_xor,
40  shift_left,
41  shift_right,
42  remainder,
43  bitwise_and,
44  bitwise_or,
45  bitwise_xor,
46  bitwise_complement,
47  sum,
48  quotient,
49  product,
50  difference,
51  unary_plus,
52  unary_minus,
53  invalid
54  };
55 
56  constexpr static std::string_view to_string(Opers t_oper) noexcept {
57  constexpr const std::array opers
58  = {"", "==", "<", ">", "<=", ">=", "!=", "", "=", "++", "--", "*=", "+=", "/=", "-=", "", "&=", "|=", "<<=", ">>=", "%=", "^=", "", "<<", ">>", "%", "&", "|", "^", "~", "", "+", "/", "*", "-", "+", "-", ""};
59  return opers[static_cast<std::size_t>(t_oper)];
60  }
61 
62  constexpr static Opers to_operator(std::string_view t_str, bool t_is_unary = false) noexcept {
63 #ifdef CHAISCRIPT_MSVC
64 #pragma warning(push)
65 #pragma warning(disable : 4307)
66 #endif
67 
68  const auto op_hash = utility::hash(t_str);
69  switch (op_hash) {
70  case utility::hash("=="): {
71  return Opers::equals;
72  }
73  case utility::hash("<"): {
74  return Opers::less_than;
75  }
76  case utility::hash(">"): {
77  return Opers::greater_than;
78  }
79  case utility::hash("<="): {
80  return Opers::less_than_equal;
81  }
82  case utility::hash(">="): {
83  return Opers::greater_than_equal;
84  }
85  case utility::hash("!="): {
86  return Opers::not_equal;
87  }
88  case utility::hash("="): {
89  return Opers::assign;
90  }
91  case utility::hash("++"): {
92  return Opers::pre_increment;
93  }
94  case utility::hash("--"): {
95  return Opers::pre_decrement;
96  }
97  case utility::hash("*="): {
98  return Opers::assign_product;
99  }
100  case utility::hash("+="): {
101  return Opers::assign_sum;
102  }
103  case utility::hash("-="): {
104  return Opers::assign_difference;
105  }
106  case utility::hash("&="): {
107  return Opers::assign_bitwise_and;
108  }
109  case utility::hash("|="): {
110  return Opers::assign_bitwise_or;
111  }
112  case utility::hash("<<="): {
113  return Opers::assign_shift_left;
114  }
115  case utility::hash(">>="): {
116  return Opers::assign_shift_right;
117  }
118  case utility::hash("%="): {
119  return Opers::assign_remainder;
120  }
121  case utility::hash("^="): {
122  return Opers::assign_bitwise_xor;
123  }
124  case utility::hash("<<"): {
125  return Opers::shift_left;
126  }
127  case utility::hash(">>"): {
128  return Opers::shift_right;
129  }
130  case utility::hash("%"): {
131  return Opers::remainder;
132  }
133  case utility::hash("&"): {
134  return Opers::bitwise_and;
135  }
136  case utility::hash("|"): {
137  return Opers::bitwise_or;
138  }
139  case utility::hash("^"): {
140  return Opers::bitwise_xor;
141  }
142  case utility::hash("~"): {
143  return Opers::bitwise_complement;
144  }
145  case utility::hash("+"): {
146  return t_is_unary ? Opers::unary_plus : Opers::sum;
147  }
148  case utility::hash("-"): {
149  return t_is_unary ? Opers::unary_minus : Opers::difference;
150  }
151  case utility::hash("/"): {
152  return Opers::quotient;
153  }
154  case utility::hash("*"): {
155  return Opers::product;
156  }
157  default: {
158  return Opers::invalid;
159  }
160  }
161 #ifdef CHAISCRIPT_MSVC
162 #pragma warning(pop)
163 #endif
164  }
165  };
166 } // namespace chaiscript
167 
168 #endif /* _CHAISCRIPT_ALGEBRAIC_HPP */
Namespace chaiscript contains every API call that the average user will be concerned with...
Definition: chaiscript_algebraic.hpp:19