-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.hpp
More file actions
executable file
·120 lines (89 loc) · 3.88 KB
/
token.hpp
File metadata and controls
executable file
·120 lines (89 loc) · 3.88 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
#ifndef MP_TOKEN_DYNAMIC_HPP
#define MP_TOKEN_DYNAMIC_HPP
#include "design_pattern.hpp"
#include "token_type.hpp"
namespace mp
{
class Variable;
/**
* @class Token
* Enum class to define the type of tokens that can have an expression
*
*/
class Token
{
public:
Token() {}
virtual ~Token() = default;
static TokenType detect_tokenType(const std::string &var)
{
TokenType tt {TokenType::UNDEFINED_TOKEN};
if(var.size() == 0)
throw std::invalid_argument("\nError while reading raw expression:\nToken is empty");
if( mp::is_operator(var) )
tt = TokenType::OPERATOR;
else if(is_digit(var[0]) || var[0] == '-' )
tt = TokenType::NUMBER;
else if(mp::is_function(var))
tt = TokenType::FUNCTION;
else if(mp::is_constant(var))
tt = TokenType::CONSTANT;
else if(var.size() == 1)
{
if( mp::is_left_bracket(var[0]) )
tt = TokenType::LEFT_BRACKET;
else if( mp::is_right_bracket(var[0]) )
tt = TokenType::RIGHT_BRACKET;
else
tt = TokenType::VARIABLE;
}
else
tt = TokenType::VARIABLE;
// PRINT("token \"" << var << "\"\t: " << mp::to_string(tt));
return tt;
}
virtual bool is_function() const { return false; };
virtual bool is_operator() const { return false; };
virtual bool is_constant() const { return false; };
virtual bool is_variable() const { return false; };
virtual bool is_number() const { return false; };
virtual bool is_left_bracket() const { return false; };
virtual bool is_right_bracket() const { return false; };
virtual double evaluate() const = 0;
virtual double evaluate(double const) const = 0;
virtual double evaluate(double const, double const) const = 0;
virtual void set_value( double const &a) { throw std::invalid_argument("Token does not handle set_value"); }
// Friend streaming function
virtual std::ostream& print(std::ostream& os) const
{
os << to_string();
return os;
}
virtual std::string to_string() const { return "UndefinedToken"; }
};
typedef std::shared_ptr<Token> sp_token;
std::ostream& operator<<(std::ostream& os, const Token& t) { return t.print(os); }
std::string to_string(const Token& t) { return t.to_string(); }
inline bool operator==(const Token &lhs, const Token &rhs) { return lhs.to_string() == rhs.to_string(); }
inline bool operator!=(const Token &lhs, const Token &rhs) { return !(lhs==rhs); }
// Friend function for better code writing (friend keyword could be deleted)
inline bool is_function(const Token &t) { return t.is_function(); }
inline bool is_operator(const Token &t) { return t.is_operator(); }
inline bool is_constant(const Token &t) { return t.is_constant(); }
inline bool is_variable(const Token &t) { return t.is_variable(); }
inline bool is_number(const Token &t) { return t.is_number(); }
inline bool is_left_bracket(const Token &t) { return t.is_left_bracket(); }
inline bool is_right_bracket(const Token &t) { return t.is_right_bracket(); }
inline bool is_operand(const Token &t)
{ return is_number(t) || is_constant(t) || is_variable(t); }
inline bool is_function(std::shared_ptr<Token> t) { return t->is_function(); }
inline bool is_operator(std::shared_ptr<Token> t) { return t->is_operator(); }
inline bool is_constant(std::shared_ptr<Token> t) { return t->is_constant(); }
inline bool is_variable(std::shared_ptr<Token> t) { return t->is_variable(); }
inline bool is_number(std::shared_ptr<Token> t) { return t->is_number(); }
inline bool is_left_bracket(std::shared_ptr<Token> t) { return t->is_left_bracket(); }
inline bool is_right_bracket(std::shared_ptr<Token> t) { return t->is_right_bracket(); }
inline bool is_operand(std::shared_ptr<Token> t)
{ return is_number(t) || is_constant(t) || is_variable(t); }
}
#endif // MP_TOKEN_DYNAMIC_HPP