-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
63 lines (49 loc) · 1.5 KB
/
lexer.cpp
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
#include <vector>
#include <string>
#include <stdexcept>
#include "token.h"
#include "lexer.h"
using namespace std;
Token *Lexer::readIdentifier() {
string name;
char c = text[offset];
name.append(string(1, c));
while (offset + 1 != text.size() && isalnum(text[offset + 1])) {
offset++;
c = text[offset];
name.append(string(1, c));
}
if (name == "if") return IfToken::getInstance();
else if (name == "else") return ElseToken::getInstance();
else return new Identifier(name);
}
Token *Lexer::readNumber() {
string number;
char c = text[offset];
number.append(string(1, c));
while (offset + 1 != text.size() && (isdigit(text[offset + 1]) || text[offset + 1] == '.')) {
offset++;
c = text[offset];
number.append(string(1, c));
}
return new Number(stod(number));
}
vector<Token *> Lexer::tokenize() {
vector<Token *> output;
for (offset = 0; offset < text.size(); offset++) {
char c = text[offset];
if (isspace(c)) continue;
else if (isalpha(c)) {
output.push_back(readIdentifier());
} else if (isdigit(c)) {
output.push_back(readNumber());
} else if (Operator::isOperator(c)) {
output.push_back(Operator::get(c));
} else if (Token::isNonOperatorToken(c)) {
output.push_back(Token::get(c));
} else {
throw invalid_argument("Unrecognized symbol during tokenizing");
}
}
return output;
}