-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cpp
52 lines (48 loc) · 1.37 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
#include <unordered_map>
#include "lexer.h"
namespace BallLang
{
Token getTok() {
char c;
do {
c = std::cin.get();
} while (c == ' ');
if (c == EOF)
return Token(ENDOFFILE);
else if (c == '(')
return Token(OPEN_PAREN);
else if (c == ')')
return Token(CLOSE_PAREN);
else if (c == ',')
return Token(COMMA);
else if (BinopPrecedence.find(c) != BinopPrecedence.end())
return Token(BINOP, c);
else if (isalpha(c)) { // an identifier
std::string identifier{};
do {
identifier.push_back(c);
c = std::cin.get();
} while (isalnum(c));
if (identifier == "play") return Token(DEF);
else if (identifier == "draft") return Token(EXTERN);
else return Token(IDENTIFIER, std::move(identifier));
}
else if (isdigit(c) || c == '.') { // a number
std::string number{};
do {
number.push_back(c);
c = std::cin.get();
} while (isdigit(c) || c == '.');
double numberVal = strtod(number.c_str(), 0);
return Token(NUMBER, numberVal);
}
else if (c == '#') { // a comment
do {
c = std::cin.get();
} while (c != EOF && c != '\n' && c != '\r');
if (c == EOF) return Token(ENDOFFILE);
else return getTok();
}
else return Token(ERROR);
}
}