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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
/*
* Copyright (c) 2025 Ian Marco Moffett and L5 engineers
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the project nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* Description: Pirho compiler lexer
* Author: Ian Marco Moffett
*/
#include <sys/types.h>
#include <sys/errno.h>
#include <sys/syslog.h>
#include <os/np.h>
#include <os/kalloc.h>
#include <np/lex.h>
#include <string.h>
#define pr_trace(fmt, ...) printf("pirho.lex: " fmt, ##__VA_ARGS__)
#define pr_error(fmt, ...) printf("pirho.lex: error: " fmt, ##__VA_ARGS__)
/* Max identifier length */
#define MAX_ID_LEN 32
/* Just some helpers */
#define to_lower(c) ((c) | 0x20)
#define is_alpha(c) (to_lower(c) >= 'a' && to_lower(c) <= 'z')
#define is_num(c) ((c) >= '0' && (c) <= '9')
#define is_space(c) ((c) == ' ' || (c) == '\t' || (c) == '\f' || (c) == '\n')
/*
* Pop a character from the input work
*
* @work: Input work to pop from
*/
static char
lex_pop(struct np_work *work)
{
struct lexer_state *lex_st;
char c;
if (work == NULL) {
return '\0';
}
/*
* First we check the cache, if there is a char,
* grab and clear it.
*/
if (work->ccache != '\0') {
c = work->ccache;
work->ccache = '\0';
return c;
}
/* Don't overflow the source file */
lex_st = &work->lex_st;
if (lex_st->source_idx >= work->source_size) {
return '\0';
}
return work->source[lex_st->source_idx++];
}
/*
* Compare a token with existing integer types (used internally)
*/
static void
lex_cmp_itype(const char *tokstr, struct lex_token *res)
{
switch (*tokstr) {
case 'u':
if (strcmp(tokstr, TOKEN_U8) == 0) {
res->token = TT_U8;
break;
}
if (strcmp(tokstr, TOKEN_U16) == 0) {
res->token = TT_U16;
break;
}
if (strcmp(tokstr, TOKEN_U32) == 0) {
res->token = TT_U32;
break;
}
if (strcmp(tokstr, TOKEN_U64) == 0) {
res->token = TT_U64;
break;
}
break;
case 'i':
if (strcmp(tokstr, TOKEN_I8) == 0) {
res->token = TT_I8;
break;
}
if (strcmp(tokstr, TOKEN_I16) == 0) {
res->token = TT_I16;
break;
}
if (strcmp(tokstr, TOKEN_I32) == 0) {
res->token = TT_I32;
break;
}
if (strcmp(tokstr, TOKEN_I64) == 0) {
res->token = TT_I64;
break;
}
break;
}
}
/*
* Compare a token with existing tokens (used internally)
*/
static int
lex_cmptok(char *tokstr, struct lex_token *res)
{
switch (*tokstr) {
case 'r':
if (strcmp(tokstr, TOKEN_RETURN) == 0) {
res->token = TT_RETURN;
}
return 0;
case 'b':
if (strcmp(tokstr, TOKEN_BEGIN) == 0) {
res->token = TT_BEGIN;
}
return 0;
case 'e':
if (strcmp(tokstr, TOKEN_END) == 0) {
res->token = TT_END;
}
return 0;
case 'p':
if (strcmp(tokstr, TOKEN_PROC) == 0) {
res->token = TT_PROC;
}
return 0;
case 'u':
case 'i':
lex_cmp_itype(tokstr, res);
return 0;
}
res->token = TT_IDENT;
return 0;
}
/*
* Match a token with a set of known tokens
*
* @work: Input work
* @c: Current character
* @res: Token result
*
* Returns zero on success
*/
static int
lex_matchstr(struct np_work *work, char c, struct lex_token *res)
{
char id[MAX_ID_LEN + 1];
size_t id_idx = 0;
int error;
if (work == NULL || res == NULL) {
return -EINVAL;
}
/* Grab the identifier */
do {
if (id_idx >= sizeof(id) - 1) {
pr_error("identifier too long!\n");
return -1;
}
if (!is_alpha(c) && !is_num(c)) {
work->ccache = c;
break;
}
id[id_idx++] = c;
} while ((c = lex_pop(work)) != 0);
id[id_idx] = '\0';
res->val_str = ptrbox_strdup(id, work->work_mem);
/* Match the tokens */
error = lex_cmptok(id, res);
if (error < 0) {
pr_error("invalid indentifier '%s'\n", id);
}
return error;
}
/*
* Scan arithmetic operators and return the token type
* (tt_t) on success
*
* @work: Input work
* @c: Character to check
*
* Returns a less than zero value on failure
*/
static int
lex_arithop(struct np_work *work, char c, struct lex_token *res)
{
switch (c) {
case '*':
res->token = TT_STAR;
break;
case '-':
res->token = TT_MINUS;
break;
case '+':
res->token = TT_PLUS;
break;
case '/':
res->token = TT_SLASH;
break;
default:
return -1;
}
return res->token;
}
/*
* Scan compare operators and return the token type
* (tt_t) on success
*
* @work: Input work
* @c: Character to check
*
* Returns a less than zero value on failure
*/
static int
lex_cmpop(struct np_work *work, char c, struct lex_token *res)
{
switch (c) {
case '>':
res->token = TT_GT;
break;
case '<':
res->token = TT_LT;
break;
default:
return -1;
}
return res->token;
}
/*
* Parse a number and get a token value
*
* @work: Input work
* @c: First character [digit]
* @res: Result
*/
static int
lex_nomnum(struct np_work *work, char c, struct lex_token *res)
{
uint64_t num = 0;
if (work == NULL || res == NULL) {
return -EINVAL;
}
while (is_num(c)) {
num = num * 10 + (c - '0');
c = lex_pop(work);
}
res->token = TT_NUMBER;
res->val = num;
return 0;
}
/*
* Parse a string and return the result as token
*
* @work: Current work
* @c: Current character
* @res: Result is written here
*
* Returns zero on success, otherwise a less than zero
* value to indicate failure
*/
static int
lex_nomstr(struct np_work *work, char c, struct lex_token *res)
{
char *strbuf;
size_t strbuf_i = 0, start_line;
bool have_term = false;
int retval = 0;
if (work == NULL || res == NULL) {
return -EINVAL;
}
start_line = work->line_no;
/*
* Create a string buffer. This might be configured to
* be a rather large number so we allocate on the heap
* to not overflow the stack.
*/
if ((strbuf = kalloc(LEX_MAX_STRLEN)) == NULL) {
pr_error("failed to allocate string buffer\n");
return -1;
}
while ((c = lex_pop(work)) != 0) {
if (strbuf_i >= (LEX_MAX_STRLEN - 1)) {
pr_error("line %d: string too long!\n", start_line);
retval = -1;
break;
}
/* End our scanning once we have an end quote */
if (c == '"') {
strbuf[strbuf_i] = '\0';
have_term = true;
break;
}
strbuf[strbuf_i++] = c;
}
/*
* If everything went well, copy the string into the
* token store.
*/
if (have_term) {
res->val_str = ptrbox_strdup(strbuf, work->work_mem);
} else {
pr_error("line %d: Unterminated string\n", start_line);
retval = -1;
}
res->token = TT_STR;
kfree(strbuf);
return retval;
}
/*
* Nom a token
*/
int
lex_nom(struct np_work *work, struct lex_token *res)
{
struct lexer_state *lex_st;
int error = 0;
char c;
if (work == NULL || res == NULL) {
return -EINVAL;
}
lex_st = &work->lex_st;
res->token = TT_NONE;
/* Skip all whitespace */
while ((c = lex_pop(work)) != 0) {
if (c == '\n') {
++work->line_no;
}
if (is_space(c)) {
continue;
}
break;
}
/* Match the token type */
switch (c) {
case '\0':
return LEX_EOF;
case '(':
res->token = TT_LPAREN;
break;
case ')':
res->token = TT_RPAREN;
break;
case ',':
res->token = TT_COMMA;
break;
case '=':
res->token = TT_EQUALS;
break;
case '[':
res->token = TT_LBRACK;
break;
case ']':
res->token = TT_RBRACK;
break;
case ':':
res->token = TT_COLON;
break;
case '"':
/* Handle a string */
if ((error = lex_nomstr(work, c, res)) < 0) {
return error;
}
break;
default:
if (is_num(c)) {
lex_nomnum(work, c, res);
break;
}
if (lex_arithop(work, c, res) >= 0)
break;
if (lex_cmpop(work, c, res) >= 0)
break;
/* Stuff like '1var_name' is invalid */
if (!is_alpha(c)) {
pr_error("unexpected token '%c'\n", c);
return -1;
}
error = lex_matchstr(work, c, res);
if (error == 0) {
break;
}
error = -1;
break;
}
return error;
}
int
lex_init(struct lexer_state *state, struct np_work *work)
{
if (state == NULL) {
return -EINVAL;
}
memset(state, 0, sizeof(*state));
state->work = work;
return 0;
}
|