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
|
/*
* Copyright (c) 2026, Chloe M.
* Provided under the BSD-3 clause
*/
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "cescal/lexer.h"
#include "cescal/log.h"
#include "cescal/ptrbox.h"
static inline void
lexer_putback(struct cescal_state *state, char c)
{
if (state == NULL) {
return;
}
state->lex_putback = c;
}
static inline char
lexer_putback_pop(struct cescal_state *state)
{
char retc;
if (state == NULL) {
return '\0';
}
retc = state->lex_putback;
state->lex_putback = '\0';
return retc;
}
/*
* Returns true if the given character is a whitespace
*
* @c: Character to check
*/
static inline bool
lexer_is_ws(char c)
{
switch (c) {
case '\t':
case '\n':
case ' ':
case '\f':
case '\r':
return true;
}
return false;
}
/*
* Consume a single character from the input source file and
* optionally skip whitespace
*
* @state: Compiler state
* @skip_ws: If true skip whitespace
*/
static char
lexer_consume_single(struct cescal_state *state, bool skip_ws)
{
char c;
if (state == NULL) {
return '\0';
}
if ((c = lexer_putback_pop(state)) != '\0') {
if (!skip_ws) {
return c;
}
if (skip_ws && !lexer_is_ws(c)) {
return c;
}
}
while ((c = readbuf_read(&state->rb, state->in_fd)) != '\0') {
if (lexer_is_ws(c) && skip_ws) {
continue;
}
return c;
}
return '\0';
}
static int
lexer_scan_ident(struct cescal_state *state, char lc, struct token *res)
{
char *buf, c;
size_t bufsz, bufcap;
if (state == NULL || res == NULL) {
errno = EINVAL;
return -1;
}
bufsz = 0;
bufcap = 8;
if ((buf = malloc(bufcap)) == NULL) {
return -1;
}
if (lc != '_' && !isalpha(lc)) {
cc_error("bad identifier\n");
return -1;
}
buf[bufsz++] = lc;
for (;;) {
c = lexer_consume_single(state, false);
if (c != '_' && !isalnum(c)) {
lexer_putback(state, c);
buf[bufsz] = '\0';
break;
}
buf[bufsz++] = c;
if (bufsz >= bufcap) {
bufcap += 8;
buf = realloc(buf, bufcap);
}
if (buf == NULL) {
return -1;
}
}
res->s = ptrbox_strdup(&state->ptrbox, buf);
res->type = TT_IDENT;
free(buf);
return 0;
}
/*
* Checks if an identifier token is actually a keyword
*
* @state: Compiler state
* @res: Token result
*/
static void
lexer_check_kw(struct cescal_state *state, struct token *res)
{
if (state == NULL || res == NULL) {
return;
}
switch (*res->s) {
case 'p':
if (strcmp(res->s, "pub") == 0) {
res->type = TT_PUB;
return;
}
if (strcmp(res->s, "proc") == 0) {
res->type = TT_PROC;
return;
}
break;
case 'b':
if (strcmp(res->s, "begin") == 0) {
res->type = TT_BEGIN;
return;
}
break;
case 'e':
if (strcmp(res->s, "end") == 0) {
res->type = TT_END;
return;
}
break;
case 'r':
if (strcmp(res->s, "return") == 0) {
res->type = TT_RETURN;
return;
}
}
}
/*
* Check if an identifier token is actually a directive
*
* @state: Compiler state
* @res: Token result is written here
*/
static int
lexer_check_direc(struct cescal_state *state, struct token *res)
{
if (state == NULL || res == NULL) {
errno = EINVAL;
return -1;
}
switch (*res->s) {
case 'd':
if (strcmp(res->s, "define") == 0) {
res->type = TT_DEFINE;
return 0;
}
break;
case 'i':
if (strcmp(res->s, "ifndef") == 0) {
res->type = TT_IFNDEF;
return 0;
}
if (strcmp(res->s, "ifdef") == 0) {
res->type = TT_IFDEF;
return 0;
}
break;
}
return -1;
}
/*
* Skip anything after a comment
*
* @state: Compiler state
*/
static void
lexer_skip_comment(struct cescal_state *state)
{
char c;
if (state == NULL) {
return;
}
while ((c = lexer_consume_single(state, false)) != '\n') {
if (c == '\0') {
break;
}
}
}
int
lexer_nom(struct cescal_state *state, struct token *res)
{
char c;
if (state == NULL || res == NULL) {
errno = EINVAL;
return -1;
}
if ((c = lexer_consume_single(state, true)) == '\0') {
return -1;
}
switch (c) {
case '(':
res->type = TT_LPAREN;
res->c = c;
return 0;
case ')':
res->type = TT_RPAREN;
res->c = c;
return 0;
case ',':
res->type = TT_COMMA;
res->c = c;
return 0;
case '#':
if ((c = lexer_consume_single(state, true)) == '\0') {
return -1;
}
if (lexer_scan_ident(state, c, res) == 0) {
if (lexer_check_direc(state, res) != 0) {
cc_error("bad directive '%s'\n", res->s);
return -1;
}
}
return 0;
case '/':
if (lexer_consume_single(state, true) == '/') {
res->type = TT_COMMENT;
res->c = c;
lexer_skip_comment(state);
return 0;
}
return -1;
case '-':
res->c = c;
if (lexer_consume_single(state, true) == '>') {
res->type = TT_ARROW;
return 0;
}
return -1;
default:
if (lexer_scan_ident(state, c, res) == 0) {
lexer_check_kw(state, res);
return 0;
}
}
cc_error("got unknown token '%c'\n", c);
return -1;
}
|