forked from llvm-class/project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.h
321 lines (275 loc) · 6.98 KB
/
lexer.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#pragma once
#ifndef H_LEX
#define H_LEX
#include <ciso646>
#include <sstream>
#include "llvm.h"
namespace rift {
struct Token {
enum class Type {
add,
sub,
mul,
div,
eq,
neq,
lt,
gt,
dot,
assign,
opar,
cpar,
osbr,
csbr,
ocbr,
ccbr,
semicolon,
comma,
number,
character,
ident,
kwFunction,
kwIf,
kwElse,
kwWhile,
kwC,
kwLength,
kwEval,
kwType,
eof
};
static std::string typeToStr(Type t) {
switch(t) {
case Type::add:
return "+";
case Type::sub:
return "-";
case Type::mul:
return "*";
case Type::div:
return "/";
case Type::eq:
return "==";
case Type::neq:
return "!=";
case Type::lt:
return "<";
case Type::gt:
return ">";
case Type::dot:
return "%*%";
case Type::assign:
return "<-";
case Type::opar:
return "(";
case Type::cpar:
return ")";
case Type::osbr:
return "[";
case Type::csbr:
return "]";
case Type::ocbr:
return "{";
case Type::ccbr:
return "}";
case Type::semicolon:
return ";";
case Type::comma:
return ",";
case Type::number:
return "number";
case Type::character:
return "character";
case Type::ident:
return "identifier";
case Type::kwFunction:
return "keyword function";
case Type::kwIf:
return "keyword if";
case Type::kwElse:
return "keyword else";
case Type::kwWhile:
return "keyword while";
case Type::kwC:
return "keyword c";
case Type::kwLength:
return "keyword length";
case Type::kwEval:
return "keyword eval";
case Type::kwType:
return "keyword type";
case Type::eof:
return "EOF";
default:
return "unknown";
}
}
Type type;
union {
// actual double value
double d;
// index to constant pool where the string is stored
unsigned c;
};
Token(Type t):
type(t) {}
Token(double value):
type(Type::number),
d(value) {}
Token(Type t, int index):
type(t),
c(index) {}
bool operator == (Token::Type t) {
return type == t;
}
bool operator != (Token::Type t) {
return type != t;
}
};
class Lexer {
public:
void scan(std::string const & input) {
std::stringstream ss(input);
scan(ss);
}
void scan(std::istream & input) {
while (not input.eof())
tokens_.push_back(readToken(input));
if (tokens_.back() != Token::Type::eof)
tokens_.push_back(Token::Type::eof);
}
bool eof() {
return tidx_ >= tokens_.size() - 1;
}
Token top() {
return tokens_[tidx_];
}
Token pop() {
if (eof())
return tokens_[tidx_];
else
return tokens_[tidx_++];
}
private:
Token readToken(std::istream & input) {
char c = input.get();
// skip the whitespace and comments
bool skip = false;
while (skip or c == ' ' or c == '\n' or c == '\t' or c == '#') {
if (c == '#')
skip = true;
if (skip and c == '\n')
skip = false;
c = input.get();
}
if (input.eof())
return Token(Token::Type::eof);
switch (c) {
case '+':
return Token(Token::Type::add);
case '-':
return Token(Token::Type::sub);
case '*':
return Token(Token::Type::mul);
case '/':
return Token(Token::Type::div);
case '(':
return Token(Token::Type::opar);
case ')':
return Token(Token::Type::cpar);
case '[':
return Token(Token::Type::osbr);
case ']':
return Token(Token::Type::csbr);
case '{':
return Token(Token::Type::ocbr);
case '}':
return Token(Token::Type::ccbr);
case '<':
if (input.peek() == '-') {
input.get();
return Token(Token::Type::assign);
}
return Token(Token::Type::lt);
case '>':
return Token(Token::Type::gt);
case ';':
return Token(Token::Type::semicolon);
case ',':
return Token(Token::Type::comma);
case '=':
if (input.peek() != '=') {
return Token(Token::Type::assign);
}
input.get();
return Token(Token::Type::eq);
case '!':
if (input.get() != '=')
throw "Expected != but only ! found";
return Token(Token::Type::neq);
case '%':
if (input.get() == '*')
if (input.get() == '%')
return Token(Token::Type::dot);
throw "Expected %*% operator";
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return number(c - '0', input);
case '.':
return fractionNumber(0, input);
case '"':
return stringLiteral(input);
default:
return identOrKeyword(c, input);
}
}
bool isNumber(char c) {
return c >= '0' and c <= '9';
}
bool isLetter(char c) {
return (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') or c == '_';
}
Token number(double n, std::istream & input) {
while (true) {
char c = input.peek();
if (isNumber(c)) {
input.get();
n = n * 10 + (c - '0');
} else if (c == '.') {
input.get();
return fractionNumber(n, input);
} else {
return Token(n);
}
}
}
Token fractionNumber(double n, std::istream & input) {
double d = 10;
while (true) {
char c = input.peek();
if (isNumber(c)) {
input.get();
n = n + ((c - '0') / d);
d = d * 10;
} else {
if (d == 10)
throw "At least one digit must be present after dot";
return Token(n);
}
}
}
Token identOrKeyword(char start, std::istream & input);
Token stringLiteral(std::istream & input);
std::vector<Token> tokens_;
unsigned tidx_ = 0;
};
} // namespace rift
#endif