-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.cpp
180 lines (150 loc) · 3.57 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
172
173
174
175
176
177
178
179
180
#include "../include/lexer.hpp"
#include "../include/error.hpp"
#include <string>
#include <vector>
// Lexer
void Lexer::lexToken() {
char currentChar = advance();
switch (currentChar) {
// Single character lexemes
case '(':
addToken(TokenType::OpenParen);
break;
case ')':
addToken(TokenType::CloseParen);
break;
case '+':
addToken(TokenType::Plus);
break;
case '-':
addToken(TokenType::Minus);
break;
case '*':
addToken(TokenType::Star);
break;
// Linebreaks and whitespace
case ' ':
case '\r':
case '\t':
// Ignore whitespace.
break;
case '\n':
line++;
break;
// Comments
case '/':
if (match('/')) {
while (peek() != '\n' && !isAtEnd())
advance();
} else {
Error::report(line, "Division operator currently not supported");
this->hadError = true;
}
break;
// String literals
case '"':
string();
break;
default:
if (isDigit(currentChar)) {
number();
} else if (isAlpha(currentChar)) {
identifier();
} else {
Error::report(line, "Unexpected character");
this->hadError = true;
}
break;
}
}
char Lexer::advance() {
char currentChar = source.at(current);
this->current++;
return currentChar;
}
bool Lexer::match(char expected) {
if (isAtEnd())
return false;
if (source.at(current) != expected)
return false;
this->current++;
return true;
}
char Lexer::peek() {
if (isAtEnd())
return '\0';
else
return source.at(current);
}
void Lexer::addToken(TokenType token) { addToken(token, ""); }
void Lexer::addToken(TokenType token, std::string value) {
Token t = Token{.token = token, .value = value, .line = line};
this->tokens.push_back(t);
}
bool Lexer::isAtEnd() { return current >= source.length(); }
bool Lexer::isDigit(char c) { return c >= '0' && c <= '9'; }
bool Lexer::isAlpha(char c) {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c == '_');
}
bool Lexer::isAlphaNumeric(char c) { return (isAlpha(c) || isDigit(c)); }
void Lexer::string() {
// Go till end of string
// Escape sequences not supported
while (peek() != '"' && !isAtEnd()) {
if (peek() == '\n') line++;
advance();
}
if (isAtEnd()) {
Error::report(line, "Unterminated string");
this->hadError=true;
return;
}
// Closing "
advance();
// Get string literal
std::string value = this->source.substr(start + 1, current - 1);
addToken(TokenType::StrConstant, value);
}
void Lexer::number() {
// Go till end of number
while (isDigit(peek()) && !isAtEnd())
advance();
if (isAtEnd()) {
Error::report(line, "Lisp program should end with ')'");
this->hadError=true;
return;
}
std::string value = source.substr(start, current);
addToken(TokenType::Constant, value);
}
void Lexer::identifier() {
while (isAlphaNumeric(peek()) && !isAtEnd()) advance();
if (isAtEnd()) {
Error::report(line, "Lisp program should end with ')'");
this->hadError=true;
return;
}
// Check for reserved keywords
std::string text = source.substr(start, current);
if (text == "lambda") {
addToken(TokenType::Lambda, text);
} else {
addToken(TokenType::Identifier, text);
}
}
Lexer::Lexer(std::string source) {
this->source = source;
}
std::vector<Token> Lexer::lex() {
while (current < source.length()) {
// At beginning of next lexeme
start = current;
Lexer::lexToken();
}
tokens.push_back(
Token{.token = TokenType::Eof, .value = "", .line = line});
return tokens;
}
bool Lexer::lexError() {
return this->hadError;
}