-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLexer.hpp
98 lines (83 loc) · 2.63 KB
/
Lexer.hpp
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
#ifndef _LEXER_HPP_
#define _LEXER_HPP_
#include "includes.hpp"
#include "StaticString.hpp"
namespace Lexer {
enum TokenId {
tok_number,
tok_identifier,
tok_character,
tok_def,
tok_end,
tok_empty
};
struct Token {
TokenId id;
double num;
char c;
StaticString<16> str;
Token() : id(tok_empty) {}
Token(double num) : id(tok_number), num(num), c('\0') {}
Token(char c) : id(tok_character), num(0.0), c(c) {}
template <size_t Capacity>
Token(const StaticString<Capacity> &str) : id(tok_identifier), num(0.0), c('\0'), str(str) {}
};
static Token current_tok;
static StaticString<128> str;
static size_t pos;
static void get_next_token();
static void load_input_string(const char *const input_str) {
str = input_str;
pos = 0;
current_tok.id = tok_empty;
get_next_token(); // Load the first token
}
static void get_next_token() {
if (pos >= str.size()) {
current_tok.id = tok_end;
return;
}
StaticString<16> temp_str;
while (pos <str.size() && isSpace(str[pos])) ++pos; // skip whitespaces
if (pos >= str.size()) {
current_tok.id = tok_end;
return;
}
if (isDigit(str[pos])) { // NUM
temp_str.append(str[pos++]);
while (pos < str.size() && (isDigit(str[pos]) || str[pos] == '.')) {
temp_str.append(str[pos]);
++pos;
}
current_tok = Token(temp_str.to_double());
} else if (isAlpha(str[pos])) { // ID
temp_str.append(str[pos++]);
while (pos < str.size() && (isAlphaNumeric(str[pos]))) {
temp_str.append(str[pos]);
++pos;
}
current_tok = Token(temp_str);
if (temp_str == "def") {
current_tok.id = tok_def;
}
} else if (str[pos] == '\0') {
current_tok.id = tok_end;
} else {
current_tok = Token(str[pos]);
++pos;
}
}
static Token peek_next_token(int how_many = 1) {
if (how_many <= 0) return Token();
Token backup_tok(current_tok);
size_t pos_backup = pos;
for (int i = 0; i < how_many; i++) {
get_next_token();
}
Token result_tok = current_tok;
current_tok = backup_tok;
pos = pos_backup;
return result_tok;
}
}
#endif // ! _LEXER_HPP_