13void Lexer::skipShebang()
15 if (position == 0 && peek() ==
'#' && position + 1 < source.length() && source[position + 1] ==
'!')
17 while (!isAtEnd() && peek() !=
'\n')
24std::vector<Phasor::Token> Lexer::tokenize()
26 std::vector<Phasor::Token> tokens;
33 tokens.push_back(scanToken());
43 return source[position];
48 char c = source[position++];
60 return position >= source.length();
63void Lexer::skipWhitespace()
68 if (std::isspace(
static_cast<unsigned char>(c)))
72 else if (c ==
'/' && position + 1 < source.length() && source[position + 1] ==
'/')
75 while (!isAtEnd() && peek() !=
'\n')
90 if (std::isalpha(
static_cast<unsigned char>(c)))
92 if (std::isdigit(
static_cast<unsigned char>(c)))
97 return complexString();
100 if (c ==
'+' && position + 1 < source.length() && source[position + 1] ==
'+')
106 if (c ==
'-' && position + 1 < source.length() && source[position + 1] ==
'-')
112 if (c ==
'=' && position + 1 < source.length() && source[position + 1] ==
'=')
118 if (c ==
'!' && position + 1 < source.length() && source[position + 1] ==
'=')
124 if (c ==
'-' && position + 1 < source.length() && source[position + 1] ==
'>')
130 if (c ==
'<' && position + 1 < source.length() && source[position + 1] ==
'=')
136 if (c ==
'>' && position + 1 < source.length() && source[position + 1] ==
'=')
142 if (c ==
'&' && position + 1 < source.length() && source[position + 1] ==
'&')
148 if (c ==
'|' && position + 1 < source.length() && source[position + 1] ==
'|')
156 if (std::string(
"()+-*/%<>=!&|.{}:;,[]").find(c) != std::string::npos)
168 size_t start = position;
169 while (std::isalnum(
static_cast<unsigned char>(peek())) || peek() ==
'_')
171 std::string text = source.substr(start, position - start);
173 static const std::vector<std::string> keywords = {
"let",
"func",
"print",
"if",
"else",
"while"};
175 for (
const auto &kw : keywords)
188 size_t start = position;
189 while (std::isdigit(
static_cast<unsigned char>(peek())))
191 if (peek() ==
'.' && position + 1 < source.length() &&
192 std::isdigit(
static_cast<unsigned char>(source[position + 1])))
195 while (std::isdigit(
static_cast<unsigned char>(peek())))
203 if (c >=
'0' && c <=
'9')
205 if (c >=
'a' && c <=
'f')
206 return 10 + (c -
'a');
207 if (c >=
'A' && c <=
'F')
208 return 10 + (c -
'A');
214 size_t tokenLine = line;
215 size_t tokenColumn = column;
216 std::ostringstream out;
237 char esc = advance();
279 if (v1 < 0 || v2 < 0)
281 char value =
static_cast<char>((v1 << 4) | v2);
308 size_t tokenLine = line;
309 size_t tokenColumn = column;
310 std::ostringstream out;
Lexer(const std::string &source)
static int hexValue(char c)
The Pulsar Scripting Language.
static int hexValue(char c)