-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.cpp
More file actions
112 lines (84 loc) · 2.02 KB
/
lexer.cpp
File metadata and controls
112 lines (84 loc) · 2.02 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
#include "lexer.hpp"
#include <vector>
void Lexer::_advance() {
++pos;
if ((size_t) pos < input.length()) {
currentChar = input[pos];
} else {
currentChar = '\0';
}
}
Lexer::Lexer(std::string i) {
if (!std::empty(i)) {
input = i;
currentChar = input[0];
}
// do something when input is empty
}
Token Lexer::number() {
std::string tokenValue = "";
do {
tokenValue += currentChar;
_advance();
} while (std::isdigit(currentChar));
if (currentChar == '.') {
tokenValue += currentChar;
_advance();
while (std::isdigit(currentChar)) {
tokenValue += currentChar;
_advance();
}
}
return Token(TokenType::NUMBER, tokenValue, std::stod(tokenValue));
}
Token Lexer::alpha() {
std::string tokenValue = "";
do {
tokenValue += currentChar;
_advance();
} while (std::isalpha(currentChar));
if (symbol_map.count(tokenValue)) {
return Token(symbol_map.at(tokenValue), tokenValue, tokenValue);
}
return Token(TokenType::IDENTIFIER, tokenValue, tokenValue);
}
Token Lexer::symbol() {
std::string lexeme(1, currentChar);
TokenType tokenType = symbol_map.at(lexeme);
TokenValue tokenValue = currentChar;
_advance();
return Token(tokenType, lexeme, tokenValue);
}
Token Lexer::get_next_token() {
while (currentChar != '\0') {
if (std::isspace(currentChar)) _advance();
// comment found
if (currentChar == '#') {
do {
_advance();
} while (currentChar != '\n');
}
if (isSymbol(currentChar)) {
return symbol();
}
if (std::isdigit(currentChar)) {
return number();
}
if (std::isalpha(currentChar)) {
return alpha();
}
}
return Token(TokenType::_EOF, "", "");
}
bool Lexer::isSymbol(char c) {
return symbol_map.count(std::string(1, c));
}
std::vector<Token> Lexer::tokenize() {
std::vector<Token> tokens;
Token token = get_next_token();
while (token.get_type() != TokenType::_EOF) {
tokens.push_back(token);
token = get_next_token();
}
return tokens;
}