-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenizer.c
236 lines (213 loc) · 5.35 KB
/
tokenizer.c
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include "tokenizer.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
struct Token Token_create(enum TokenKind kind, struct String str)
{
struct Token tok = {};
tok.kind = kind;
tok.str = str;
return tok;
}
struct Token Token_create_number(int value, struct String str)
{
struct Token tok = {};
tok.kind = TK_NUMBER;
tok.value = value;
tok.str = str;
return tok;
}
struct Token Token_create_end()
{
struct Token tok = {};
tok.kind = TK_END;
return tok;
}
bool TokenStream_empty(struct TokenStream* stream)
{
return (stream->head == NULL);
}
void TokenStream_push(struct TokenStream* stream, struct Token tok)
{
// case for creating the stream
if (!stream->head)
{
stream->head = (struct TokenStreamNode *)calloc(1, sizeof(struct TokenStreamNode));
stream->tail = stream->head;
}
// tail is full, make a new node
if (stream->tail->count == TOKENS_PER_NODE)
{
stream->tail->next = (struct TokenStreamNode *)calloc(1, sizeof(struct TokenStreamNode));
stream->tail = stream->tail->next;
}
// tail has room, just insert in the array
stream->tail->tokens[stream->tail->count] = tok;
++stream->tail->count;
}
void TokenStream_pop(struct TokenStream *stream)
{
if (TokenStream_empty(stream))
{
return;
}
++stream->head->first;
--stream->head->count;
if (stream->head->first > TOKENS_PER_NODE || stream->head->count == 0)
{
// remove the last one from the list
if (stream->tail == stream->head)
stream->tail = NULL;
struct TokenStreamNode *to_free = stream->head;
stream->head = stream->head->next;
free(to_free);
}
}
struct Token TokenStream_next(struct TokenStream *stream)
{
return stream->head->tokens[stream->head->first];
}
bool TokenStream_end(struct TokenStream* stream)
{
return TokenStream_next(stream).kind == TK_END;
}
struct Token TokenStream_expect(struct TokenStream *stream, enum TokenKind kind)
{
struct Token tok = TokenStream_next(stream);
if (tok.kind != kind)
{
fatal_error("Expected TokenKind id '%d'", kind);
}
TokenStream_pop(stream);
return tok;
}
struct Token TokenStream_consume(struct TokenStream *stream)
{
struct Token tok = TokenStream_next(stream);
TokenStream_pop(stream);
return tok;
}
bool TokenStream_discard(struct TokenStream *stream, enum TokenKind kind)
{
struct Token tok = TokenStream_next(stream);
if (tok.kind != kind)
{
return false;
}
TokenStream_pop(stream);
return true;
}
// tokenize the input string and create token stream
struct TokenStream tokenize(char const *p)
{
struct TokenStream stream;
memset(&stream, 0, sizeof(struct TokenStream));
while (*p)
{
// skip whitespace
if (isspace(*p))
{
p++;
continue;
}
if (*p == '+')
{
TokenStream_push(&stream, Token_create(TK_PLUS, (struct String){p++, 1}));
continue;
}
else if (*p == '-')
{
TokenStream_push(&stream, Token_create(TK_MINUS, (struct String){p++, 1}));
continue;
}
else if (*p == '*')
{
TokenStream_push(&stream, Token_create(TK_ASTERISK, (struct String){p++, 1}));
continue;
}
else if (*p == '/')
{
TokenStream_push(&stream, Token_create(TK_BACKSLASH, (struct String){p++, 1}));
continue;
}
else if (*p == '(')
{
TokenStream_push(&stream, Token_create(TK_PAREN_L, (struct String){p++, 1}));
continue;
}
else if (*p == ')')
{
TokenStream_push(&stream, Token_create(TK_PAREN_R, (struct String){p++, 1}));
continue;
}
else if (*p == '=')
{
if (p[1] == '=')
{
TokenStream_push(&stream, Token_create(TK_EQUAL, (struct String){p, 2}));
p += 2;
continue;
}
TokenStream_push(&stream, Token_create(TK_EQUALS, (struct String){p++, 1}));
continue;
}
else if (*p == '!')
{
if (p[1] == '=')
{
TokenStream_push(&stream, Token_create(TK_NOT_EQUAL, (struct String){p, 2}));
p += 2;
continue;
}
TokenStream_push(&stream, Token_create(TK_EXCLAMATION, (struct String){p++, 1}));
continue;
}
else if (*p == '>')
{
if (p[1] == '=')
{
TokenStream_push(&stream, Token_create(TK_GTE, (struct String){p, 2}));
p += 2;
continue;
}
TokenStream_push(&stream, Token_create(TK_GT, (struct String){p++, 1}));
continue;
}
else if (*p == '<')
{
if (p[1] == '=')
{
TokenStream_push(&stream, Token_create(TK_LTE, (struct String){p, 2}));
p += 2;
continue;
}
TokenStream_push(&stream, Token_create(TK_LT, (struct String){p++, 1}));
continue;
}
else if (*p == ';')
{
TokenStream_push(&stream, Token_create(TK_SEMICOLON, (struct String){p++, 1}));
continue;
}
if (isdigit(*p))
{
char const *str = p;
long value = strtol(p, (char **)&p, 10);
TokenStream_push(&stream, Token_create_number(value, (struct String){str, p - str}));
continue;
}
// @oni TODO: unicode identifiers
if (isalpha(*p))
{
char const *str = p;
do {
++p;
} while (isalnum(*p));
TokenStream_push(&stream, Token_create(TK_IDENTIFIER, (struct String){str, p - str}));
continue;
}
fatal_error("Tokenizer can't parse '%c'", *p);
}
TokenStream_push(&stream, Token_create_end());
return stream;
}