-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScanner.cpp
More file actions
185 lines (165 loc) · 4.79 KB
/
Scanner.cpp
File metadata and controls
185 lines (165 loc) · 4.79 KB
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
181
182
183
184
185
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Scanner.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yfarini <yfarini@student.1337.ma> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/03 16:06:34 by yfarini #+# #+# */
/* Updated: 2023/01/14 15:29:51 by yfarini ### ########.fr */
/* */
/* ************************************************************************** */
#include "Scanner.hpp"
const std::unordered_map<std::string, token_type_t> Scanner::keywords = {
{"push", TOKEN_PUSH},
{"pop", TOKEN_POP},
{"dump", TOKEN_DUMP},
{"assert", TOKEN_ASSERT},
{"add", TOKEN_ADD},
{"sub", TOKEN_SUB},
{"mul", TOKEN_MUL},
{"div", TOKEN_DIV},
{"mod", TOKEN_MOD},
{"and", TOKEN_AND},
{"or", TOKEN_OR},
{"xor", TOKEN_XOR},
{"print", TOKEN_PRINT},
{"exit", TOKEN_EXIT},
{"int8", TOKEN_INT8},
{"int16", TOKEN_INT16},
{"int32", TOKEN_INT32},
{"float", TOKEN_FLOAT},
{"double", TOKEN_DOUBLE},
};
Scanner::Scanner(const char *_source): source(_source), line(1), current(_source)
{
curr_line_start = source;
curr_line_end = get_line_end(curr_line_start);
}
Token Scanner::get_next_token()
{
skip_non_tokens();
if (reach_end()) return create_token(TOKEN_EOF);
start = current;
char c = advance();
switch (c)
{
case '-': return number(true);
case '(': return create_token(TOKEN_OPEN_PARENT);
case ')': return create_token(TOKEN_CLOSED_PARENT);
case ';': advance(); return create_token(TOKEN_EO_STDIN);
case '\n':
{
Token tmp = create_token(TOKEN_SEP);
line++;
curr_line_start = this->current;
curr_line_end = get_line_end(curr_line_start);
return tmp;
}
default:
if (isnumber(c)) return number();
else if (isalpha(c)) return identifier();
return error("use of unknown charachter");
}
}
bool Scanner::reach_end() const
{
return *current == '\0';
}
char Scanner::advance()
{
++current;
return current[-1];
}
char Scanner::peek() const
{
return current[0];
}
char Scanner::peek_next() const
{
if (current[0])
return current[1];
return '\0';
}
Token Scanner::create_token(token_type_t type) const
{
return (Token) {
.type = type,
.line = this->line,
.start = this->start,
.length = static_cast<uint32_t>(this->current - this->start),
.error = NULL,
.line_start = curr_line_start,
.line_end = curr_line_end,
};
}
Token Scanner::error(const char* message) const
{
return (Token) {
.type = TOKEN_ERROR,
.line = this->line,
.start = this->start,
.length = static_cast<uint32_t>(this->current - this->start),
.error = message,
.line_start = curr_line_start,
.line_end = curr_line_end,
};
}
Token Scanner::number(bool is_negative)
{
if (is_negative && !isnumber(peek()))
return error("expected digit after `-`");
else if (is_negative)
advance();
while (isnumber(peek()))
advance();
if (peek() == '.')
{
advance();
if (!isnumber(peek()))
return error("expected digit after `.`");
while (isnumber(peek()))
advance();
return create_token(TOKEN_REAL_NUMBER);
}
return create_token(TOKEN_NUMBER);
}
Token Scanner::identifier()
{
while (isalnum(peek()))
advance();
std::string _identifier(this->start, this->current);
if (this->keywords.count(_identifier) == 0)
return error("use of undeclared keyword");
return create_token(this->keywords.at(_identifier));
}
void Scanner::skip_non_tokens()
{
while (skip_whitespace() || skip_comments());
}
bool Scanner::skip_whitespace()
{
char c = peek();
if (c != ' ' && c != '\t')
return false;
while ((c = peek()) && (c == ' ' || c == '\t'))
advance();
return true;
}
bool Scanner::skip_comments()
{
if (peek() == ';' && peek_next() != ';')
{
char c;
while ((c = peek()) && c != '\n')
advance();
return true;
}
return false;
}
const char *Scanner::get_line_end(const char *curr) const
{
while (*curr && *curr != '\n')
++curr;
return curr;
}