-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken.h
172 lines (151 loc) · 4.15 KB
/
token.h
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#ifndef TOKEN_H
#define TOKEN_H
#include <string>
#include <optional>
#include <functional>
#define TOKENS \
X(IDENTIFIER) \
X(VARIABLE) \
X(STRING) \
X(NUMBER) \
X(PAIR) \
X(BOOLEAN) \
X(TRUE) \
X(FALSE) \
X(NIL) \
X(AND) \
X(OR) \
X(NOT) \
X(EQ) \
X(NE) \
X(LT) \
X(LE) \
X(GT) \
X(GE) \
X(PLUS) \
X(MINUS) \
X(SLASH) \
X(STAR) \
X(PERCENT) \
X(ASSIGN) \
X(COMMA) \
X(IF) \
X(ELIF) \
X(ELSE) \
X(ENDIF) \
X(FOR) \
X(IN) \
X(ENDFOR) \
X(LEFT_PAREN) \
X(RIGHT_PAREN) \
X(LEFT_BRACKET) \
X(RIGHT_BRACKET) \
X(RANGE) \
X(PRINT) \
X(SIZE) \
X(EXCEPT) \
X(INSERT)
#define SINGLE_TOKEN \
X('(', LEFT_PAREN) \
X(')', RIGHT_PAREN) \
X('[', LEFT_BRACKET) \
X(']', RIGHT_BRACKET) \
X(',', COMMA) \
X('+', PLUS) \
X('-', MINUS) \
X('*', STAR) \
X('/', SLASH) \
X('%', PERCENT) \
X('=', ASSIGN)
#define KEYWORDS \
X("true", TRUE) \
X("false", FALSE) \
X("boolean", BOOLEAN) \
X("null", NIL) \
X("and", AND) \
X("or", OR) \
X("not", NOT) \
X("eq", EQ) \
X("ne", NE) \
X("lt", LT) \
X("le", LE) \
X("gt", GT) \
X("ge", GE) \
X("if", IF) \
X("elif", ELIF) \
X("else", ELSE) \
X("endif", ENDIF) \
X("for", FOR) \
X("in", IN) \
X("endfor", ENDFOR) \
X("range", RANGE) \
X("print", PRINT) \
X("size", SIZE) \
X("insert", INSERT)
namespace amps
{
enum class token_types : uint8_t
{
#define X(name) name,
TOKENS
#undef X
EOT,
};
inline std::string get_token_name(token_types t)
{
switch (t) {
#define X(name) case token_types::name: \
return std::string(#name);
TOKENS
#undef X
case token_types::EOT:
return "END";
default:
return "undefined";
}
}
inline std::ostream& operator<<(std::ostream &os, const token_types &type)
{
os << get_token_name(type);
return os;
}
class token_t
{
using value_t = std::optional<std::string>;
token_types type_;
value_t value_;
public:
token_t();
token_t(token_types type);
token_t(token_types type, value_t value);
token_t(const token_t&) = default;
token_t(token_t &&) = default;
~token_t() = default;
token_t &operator=(const token_t &) = default;
token_t &operator=(token_t &&) = default;
std::string to_string() const;
token_types type() const;
value_t value() const;
size_t hash() const;
};
inline size_t token_t::hash() const
{
std::hash<std::string> hash_string;
return hash_string(to_string());
}
inline std::string token_t::to_string() const
{
std::string ret = "token: " + get_token_name(type_) +
", object: " + value_.value_or("<null>");
return ret;
}
inline token_types token_t::type() const
{
return type_;
}
inline token_t::value_t token_t::value() const
{
return value_;
}
}
#endif // TOKEN_H