-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken_type.hpp
More file actions
executable file
·158 lines (120 loc) · 4.7 KB
/
token_type.hpp
File metadata and controls
executable file
·158 lines (120 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#ifndef MP_TOKEN_TYPE_HPP
#define MP_TOKEN_TYPE_HPP
#include "library.hpp"
#define MATCHING_FUNCTIONS(enumType, stable_enumType, str_to_enumType) \
inline std::string to_string(const enumType t) { return stable_enumType[static_cast<int>(t)]; } \
inline enumType str_to_enumType(const std::string &str) { \
auto matched_str_index{ std::find(stable_enumType.begin(), stable_enumType.end(), str) }; \
auto pos{ std::distance(stable_enumType.begin(), matched_str_index) }; \
if (pos < 0 || pos >= stable_enumType.size()) \
throw std::invalid_argument("\nConversion from std::string to TokenType error.\nInvalid string."); \
return static_cast<enumType>(pos); \
}
namespace mp
{
/**
* @enum TokenType
* Enum class to define the type of tokens that can have an expression
*
*/
enum class TokenType {
NUMBER, FUNCTION, ARGUMENT_SEPARATOR, OPERATOR, CONSTANT,
LEFT_BRACKET, RIGHT_BRACKET, VARIABLE, UNDEFINED_TOKEN
};
static const std::vector<std::string> vs_tt = {
"NUMBER", "FUNCTION", "ARGUMENT_SEPARATOR", "OPERATOR", "CONSTANT",
"LEFT_BRACKET", "RIGHT_BRACKET", "VARIABLE", "UNDEFINED_TOKEN"
};
/**
* @enum FunctionType
* Enum class that defined the implemented functions.
*/
enum class FunctionType {
SQRT, COS, SIN, ATAN, EXP, LOG
};
static const std::vector<std::string> vs_ft = {
"sqrt", "cos", "sin", "atan", "exp", "log"
};
/**
* @fn bool is_function(const std::string &str)
* @return true if str is a function and false if not.
*/
inline bool is_function(const std::string &str)
{
auto matched_str_index{ std::find(vs_ft.begin(), vs_ft.end(), str) };
auto pos{ std::distance(vs_ft.begin(), matched_str_index) };
if( pos < 0 || pos >= vs_ft.size() )
return false;
return true;
}
/**
* @enum OperatorType
* Enum class that defined the implemented operator.
*/
enum class OperatorType {
PLUS, MINUS, MUL, DIV, POW
};
static const std::vector<std::string> vs_ot = {
"+", "-", "*", "/", "^" };
/**
* @fn bool is_operator(const std::string &str)
* @return true if str is an operator and false if not.
*/
inline bool is_operator(const std::string &str)
{
auto matched_str_index{ std::find(vs_ot.begin(), vs_ot.end(), str) };
auto pos{ std::distance(vs_ot.begin(), matched_str_index) };
if( pos < 0 || pos >= vs_ot.size() )
return false;
return true;
}
inline bool is_operator(const char c) { return is_operator(std::string(1,c)); }
/**
* @enum ConstantType
* Enum class that defined the implemented operator.
*/
enum class ConstantType {
pi, e
};
static const std::vector<std::string> vs_ct = {
"pi", "e" };
/**
* @fn bool is_constant(const std::string &str)
* @return true if str is a constant and false if not.
*/
inline bool is_constant(const std::string &str)
{
auto matched_str_index{ std::find(vs_ct.begin(), vs_ct.end(), str) };
auto pos{ std::distance(vs_ct.begin(), matched_str_index) };
if( pos < 0 || pos >= vs_ct.size() )
return false;
return true;
}
inline bool is_constant(const char c) { return is_constant(std::string(1,c)); }
/**
* @preprocessor function
* create functions to easily switch from string to enum class easily.
*
* @fn std::string to_string(const EnumType et)
* @fn EnumType sto_EnumType(const std::string &var)
*/
MATCHING_FUNCTIONS(TokenType, vs_tt, sto_TokenType)
MATCHING_FUNCTIONS(FunctionType, vs_ft, sto_FunctionType)
MATCHING_FUNCTIONS(OperatorType, vs_ot, sto_OperatorType)
MATCHING_FUNCTIONS(ConstantType, vs_ct, sto_ConstantType)
inline bool is_whitespace(const char c) {
return (' ' == c) || ('\n' == c) ||
('\r' == c) || ('\t' == c) ||
('\b' == c) || ('\v' == c) || ('\f' == c) ;
}
inline bool is_letter(const char c) {
return (('a' <= c) && (c <= 'z')) ||
(('A' <= c) && (c <= 'Z')) ;
}
inline bool is_digit(const char c) { return ('0' <= c) && (c <= '9'); }
inline bool is_left_bracket(const char c) { return ('(' == c) || ('[' == c) || ('{' == c); }
inline bool is_left_bracket(const std::string &str) { return is_left_bracket(str[0]); }
inline bool is_right_bracket(const char c) { return (')' == c) || (']' == c) || ('}' == c); }
inline bool is_argument_separtor(const char c) { return c == ','; }
}
#endif // MP_TOKEN_TYPE_HPP