-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cpp
172 lines (160 loc) · 4.86 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
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
#include "token.cpp"
#include <iostream>
#include <string.h>
using namespace std;
/* Allow lower and upper case letters, underscore */
bool isLetter(char c) {
return 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || c == '_';
}
bool isDigit(char c) {
return '0' <= c && c <= '9';
}
class Lexer {
string input;
int currPos = 0; // position of current character
int nextPos = 0; // after current char
char currChar; // current character
public:
Lexer() = default;
Lexer(const string input) {
this->input = input;
readChar();
}
/* Read the next character in input, if reach the
end, set current character to NUL */
void readChar() {
if (nextPos > input.length()) {
currChar = 0;
} else {
currChar = input[nextPos];
currPos = nextPos;
nextPos++;
}
};
char peekChar() {
if (nextPos >= input.length()) {
return 0;
} else {
return input[nextPos];
}
}
string readIdentifier() {
int startPos = currPos;
while (isLetter(currChar)) {
readChar();
}
return input.substr(startPos, currPos - startPos);
}
string readNumber() {
int startPos = currPos;
while (isDigit(currChar)) {
readChar();
}
return input.substr(startPos, currPos - startPos);
}
void skipWhitespace() {
while (currChar == ' ' || currChar == '\t' || currChar == '\n' || currChar ==
'\r') {
readChar();
}
}
/* Get Next Token */
Token nextToken() {
Token token;
skipWhitespace();
switch (currChar) {
case '=':
if (peekChar() == '=') {
token.type = types.EQ;
token.literal = "==";
readChar();
} else {
token = NewToken(types.ASSIGN, currChar);
}
break;
case ';':
token = NewToken(types.SEMICOLON, currChar);
break;
case '(':
token = NewToken(types.LPAREN, currChar);
break;
case ')':
token = NewToken(types.RPAREN, currChar);
break;
case ',':
token = NewToken(types.COMMA, currChar);
break;
case ':':
token = NewToken(types.COLON, currChar);
break;
case '+':
token = NewToken(types.PLUS, currChar);
break;
case '-':
token = NewToken(types.MINUS, currChar);
break;
case '/':
token = NewToken(types.SLASH, currChar);
break;
case '*':
token = NewToken(types.ASTERISK, currChar);
break;
case '!':
if (peekChar() == '=') {
token.type = types.NOT_EQ;
token.literal = "!=";
readChar();
} else {
token = NewToken(types.SURPRISE, currChar);
}
break;
case '<':
token = NewToken(types.LESS, currChar);
break;
case '>':
token = NewToken(types.GREATER, currChar);
break;
case '[':
token = NewToken(types.LBRACKET, currChar);
break;
case ']':
token = NewToken(types.RBRACKET, currChar);
break;
case '{':
token = NewToken(types.LBRACE, currChar);
break;
case '}':
token = NewToken(types.RBRACE, currChar);
break;
case '\0':
token = NewToken(types.EoF, currChar);
break;
case '\"':
{
token.type = types.STRING;
string lit = "";
readChar();
while (currChar != '\"' && currChar != 0) {
lit += currChar;
readChar();
}
token.literal = lit;
}
break;
default:
if (isLetter(currChar)) {
token.literal = readIdentifier();
token.type = getType(token.literal);
return token; // already advanced to next char, so exit early
} else if (isDigit(currChar)) {
token.type = types.INT;
token.literal = readNumber();
return token;
} else {
token = NewToken(types.ILLEGAL, currChar);
}
}
readChar();
return token;
}
};