forked from adamdunkels/ubasic
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtokenizer.c
340 lines (310 loc) · 9.15 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
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
/*
* Copyright (c) 2006, Adam Dunkels
* All rights reserved.
*
* Copyright (c) 2018, TK Chia
* 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 author 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 AUTHOR 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 AUTHOR 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.
*
*/
#define DEBUG 0
#if DEBUG
#define DEBUG_PRINTF(...) printf(__VA_ARGS__)
#else
#define DEBUG_PRINTF(...)
#endif
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <unistd.h>
#include "ubasic.h"
#include "tokenizer.h"
static char const *ptr, *nextptr;
static char const *saved_ptr, *saved_next;
static int saved_token;
#define MAX_NUMLEN 6
struct keyword_token {
char *keyword;
int token;
};
uint8_t current_token = TOKENIZER_ERROR;
static const struct keyword_token keywords[] = {
{"let", TOKENIZER_LET},
{"print", TOKENIZER_PRINT},
{"if", TOKENIZER_IF},
{"then", TOKENIZER_THEN},
{"else", TOKENIZER_ELSE},
{"for", TOKENIZER_FOR},
{"to", TOKENIZER_TO},
{"next", TOKENIZER_NEXT},
{"step", TOKENIZER_STEP},
{"go", TOKENIZER_GO},
{"sub", TOKENIZER_SUB},
{"return", TOKENIZER_RETURN},
{"call", TOKENIZER_CALL},
{"rem", TOKENIZER_REM},
{"poke", TOKENIZER_POKE},
{"peek", TOKENIZER_PEEK},
{"int", TOKENIZER_INT},
{"abs", TOKENIZER_ABS},
{"sgn", TOKENIZER_SGN},
{"len", TOKENIZER_LEN},
{"code", TOKENIZER_CODE},
{"val", TOKENIZER_VAL},
{"stop", TOKENIZER_STOP},
{"end", TOKENIZER_END},
{"and", TOKENIZER_AND},
{"or", TOKENIZER_OR},
{"dim", TOKENIZER_DIM},
/* FIXME {"not", TOKENIZER_NOT}, */
{"data", TOKENIZER_DATA},
{"randomize", TOKENIZER_RANDOMIZE},
{"option", TOKENIZER_OPTION},
{"base", TOKENIZER_BASE},
{"input", TOKENIZER_INPUT},
{"restore", TOKENIZER_RESTORE},
{"tab", TOKENIZER_TAB},
{"left$", TOKENIZER_LEFTSTR},
{"right$", TOKENIZER_RIGHTSTR},
{"mid$", TOKENIZER_MIDSTR},
{"chr$", TOKENIZER_CHRSTR},
{"mod", TOKENIZER_MOD},
{"at", TOKENIZER_AT},
{"cls", TOKENIZER_CLS},
{NULL, TOKENIZER_ERROR}
};
/*---------------------------------------------------------------------------*/
static uint8_t doublechar(void)
{
/* Special case the paired single char symbols */
if (*ptr == '>' && ptr[1] == '=')
return TOKENIZER_GE;
if (*ptr == '<' && ptr[1] == '=')
return TOKENIZER_LE;
if (*ptr == '<' && ptr[1] == '>')
return TOKENIZER_NE;
if (*ptr == '*' && ptr[1] == '*')
return TOKENIZER_POWER;
if (*ptr == '\r' && ptr[1] == '\n')
return TOKENIZER_NL;
if (*ptr == '\n' && ptr[1] == '\r')
return TOKENIZER_NL;
return 0;
}
/*---------------------------------------------------------------------------*/
static uint8_t singlechar(void)
{
if (strchr("\n,;+-&|*/(#)<>=^:?", *ptr))
return *ptr;
if (*ptr == '\r')
return TOKENIZER_NL;
/* Not semantically meaningful */
return 0;
}
/*---------------------------------------------------------------------------*/
static uint8_t get_next_token(void)
{
struct keyword_token const *kt;
int i;
uint8_t t;
DEBUG_PRINTF("get_next_token(): '%s'\n", ptr);
if(*ptr == 0) {
return TOKENIZER_ENDOFINPUT;
}
if ((isdigit(*ptr)) || (*ptr == '-' && isdigit(ptr[1]))) {
i = 0;
if (*ptr == '-')
i = 1;
for(; i < MAX_NUMLEN; ++i) {
if(!isdigit(ptr[i])) {
if(i > 0) {
nextptr = ptr + i;
return TOKENIZER_NUMBER;
} else {
DEBUG_PRINTF("get_next_token: error due to too short number\n");
return TOKENIZER_ERROR;
}
}
if(!isdigit(ptr[i])) {
DEBUG_PRINTF("get_next_token: error due to malformed number\n");
return TOKENIZER_ERROR;
}
}
DEBUG_PRINTF("get_next_token: error due to too long number\n");
return TOKENIZER_ERROR;
} else if((t = doublechar()) != 0) {
nextptr = ptr + 2;
return t;
} else if((t = singlechar()) != 0) {
nextptr = ptr + 1;
return t;
} else if(*ptr == '"') {
nextptr = ptr;
do {
++nextptr;
if (!*nextptr || *nextptr == '\n' || *nextptr == '\r')
ubasic_tokenizer_error();
} while(*nextptr != '"');
++nextptr;
return TOKENIZER_STRING;
} else {
for(kt = keywords; kt->keyword != NULL; ++kt) {
if(strncasecmp(ptr, kt->keyword, strlen(kt->keyword)) == 0) {
nextptr = ptr + strlen(kt->keyword);
return kt->token;
}
}
}
if ((*ptr >= 'a' && *ptr <= 'z') || (*ptr >= 'A' && *ptr <= 'Z')) {
nextptr = ptr + 1;
if (*nextptr == '$') {
nextptr++;
return TOKENIZER_STRINGVAR;
}
if (isdigit(*nextptr)) /* A0-A9/B0-B9/etc */
nextptr++;
return TOKENIZER_INTVAR;
}
return TOKENIZER_ERROR;
}
/*---------------------------------------------------------------------------*/
void tokenizer_goto(const char *program)
{
ptr = program;
current_token = get_next_token();
}
/*---------------------------------------------------------------------------*/
void tokenizer_init(const char *program)
{
tokenizer_goto(program);
current_token = get_next_token();
}
/*---------------------------------------------------------------------------*/
void tokenizer_push(void)
{
saved_ptr = ptr;
saved_next = nextptr;
saved_token = current_token;
}
/*---------------------------------------------------------------------------*/
void tokenizer_pop(void)
{
ptr = saved_ptr;
nextptr = saved_next;
current_token = saved_token;
}
/*---------------------------------------------------------------------------*/
void tokenizer_next(void)
{
if(tokenizer_finished()) {
return;
}
DEBUG_PRINTF("tokenizer_next: %p\n", nextptr);
ptr = nextptr;
while(*ptr == ' ') {
++ptr;
}
current_token = get_next_token();
DEBUG_PRINTF("tokenizer_next: '%s' %d\n", ptr, current_token);
return;
}
/*---------------------------------------------------------------------------*/
void tokenizer_newline(void)
{
while(!(*nextptr == '\n' || *nextptr == '\r' || tokenizer_finished())) {
++nextptr;
}
tokenizer_next();
}
/*---------------------------------------------------------------------------*/
value_t tokenizer_num(void)
{
return atoi(ptr);
}
/*---------------------------------------------------------------------------*/
int tokenizer_string_len(void)
{
char *string_end;
if(current_token != TOKENIZER_STRING) {
write(2, "strlbotch\n", 10);
exit(1);
}
string_end = strchr(ptr + 1, '"');
/* Pass -1 back so we can keep the notional split between the tokenizer
and core code cleaner */
if(string_end == NULL)
ubasic_tokenizer_error();
return string_end - ptr - 1;
}
/*---------------------------------------------------------------------------*/
char const *tokenizer_string(void)
{
return ptr + 1;
}
/*---------------------------------------------------------------------------*/
void tokenizer_string_func(stringfunc_t func, void *ctx)
{
const char *string_end, *p;
if(current_token != TOKENIZER_STRING) {
return;
}
p = ptr + 1;
string_end = strchr(p, '"');
if(string_end == NULL)
ubasic_tokenizer_error();
while(p != string_end)
func(*p++, ctx);
}
/*---------------------------------------------------------------------------*/
void tokenizer_error_print(void)
{
ubasic_tokenizer_error();
}
/*---------------------------------------------------------------------------*/
int tokenizer_finished(void)
{
return *ptr == 0 || current_token == TOKENIZER_ENDOFINPUT;
}
/*---------------------------------------------------------------------------*/
int tokenizer_variable_num(void)
{
if (ptr[1] == '$')
return STRINGFLAG | (toupper(*ptr) - 'A');
/* FIXME: hard code to use &~0x20 as we already know it is a letter */
if (!isdigit(ptr[1]))
return toupper(*ptr) - 'A';
else {
/* One day we'll need long vars and brains, until then.. */
return (toupper(*ptr) - '@') * 11 + ptr[1] - '0';
}
}
/*---------------------------------------------------------------------------*/
char const *tokenizer_pos(void)
{
return ptr;
}