-
Notifications
You must be signed in to change notification settings - Fork 2
/
tokenize.c
391 lines (357 loc) · 11.9 KB
/
tokenize.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
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
/* tokenize.c
* - routines to tokenize C64/C128 BASIC
*/
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "tokenize.h"
#include "tokens.h"
#define FALSE 0
#define TRUE 1
#ifdef __EMX__
#define strcasecmp stricmp
#define strncasecmp strnicmp
#endif
/* The bytestream buffer used in the function (output) is from the line
* number up to the ending null character. The "next line" pointer is
* not included
*/
/* tokenize
* - tokenizes a C64/C128 BASIC (in tok64 pseudocode) line
* in: input_p - pointer to string to tokenize
* output_p - pointer to bytestream to put results in, MUST BE ALLOCATED
* length_p - pointer to integer to write length counter to
* mode - BASIC version to tokenize
* out: nonzero on error
*/
int tokenize(const char *input_p, char *output_p, int *length_p, basic_t mode)
{
int quotemode = FALSE; /* flag for quote mode */
unsigned short i; /* loop counter */
unsigned linenumber; /* line number */
int inputleft = strlen(input_p); /* amount left of line to tokenize */
int tokenlen; /* length of current token */
int match; /* match found flag */
int notokenize = FALSE; /* REM/DATA no tokenize flag */
int rc = 0; /* return code */
char buf[16]; /* buffer for special character match */
char *start_p = output_p; /* pointer to input */
/* Skip any initial whitespace */
while (' ' == *input_p || '\t' == *input_p) input_p ++;
/* Get the line number */
linenumber = 0;
while (isdigit(*input_p)) { /* line number consits of numerals */
linenumber = linenumber * 10 + (*input_p - '0');
input_p ++;
} /* while */
if (linenumber >= 64000) {
rc = 1;
fprintf(stderr, "* Illegal line number: %u\n", linenumber);
} /* if */
/* Insert line number in byte stream */
*(output_p ++) = linenumber & 255; /* low */
*(output_p ++) = linenumber >> 8; /* high */
/* Kill off any extraneous spaces */
while (' ' == *input_p) input_p ++;
/* Now process the rest of the line */
while (*input_p) { /* while string isn't ended */
if ('{' == *input_p) { /* special character */
/* copy special character name to buffer
* character name ends with '}' or '*'
*/
i = 0; /* buffer position counter */
input_p ++; /* position at first character of name */
while (i < 16 && *input_p != '*' && *input_p != '}' && *input_p) {
/* terminate loop on:
* . buffer size overrun (error)
* . '*' in input stream
* . '}' in input stream
* . null in input stream (error)
*
* else: copy from character name to buffer
*/
buf[i ++] = *(input_p ++);
} /* while */
buf[i] = 0; /* terminate with null */
/* Check for error condition */
if (*input_p != '*' && *input_p != '}') {
rc = 1;
fprintf(stderr, "* Special character sequence incorrect: '{%s'"
" at line %u\n",
buf, linenumber);
} /* if */
else {
/* It seems to be ok, so look it up */
match = FALSE;
/* Threedigit numeric? */
if (strlen(buf) == 3 &&
isdigit(buf[0]) && isdigit(buf[1]) && isdigit(buf[2])) {
sscanf(buf, "%d", &match);
}
/* Look it up in the PETSCII table */
for (i = 1; i <= 255 && !match; i ++) {
if ((i & 0x7f) >= 0x41 && (i & 0x7f) <= 0x5A) {
/* Upper-/lowercase PETSCII must be matched with
* case also (otherwise 'e' would match 'E')
*/
if (0 == strcmp(petscii[i], buf))
match = i; /* match */
} /* if */
else {
if (0 == strcasecmp(petscii[i], buf))
match = i; /* match */
} /* else */
} /* for */
/* Special condition: space (32) can be repeated, and
* is then called 'space', which is not in the petscii
* table
*/
if (!match && 0 == strcasecmp("space", buf)) {
match = ' ';
} /* if */
/* Now check whether or not we got a match */
if (match) {
/* We have found which PETSCII character was meant,
* now we check whether or not we wanted more than one
* character of this kind ("{char*n}")
*/
i = 1; /* one copy wanted */
if ('*' == *input_p) { /* multiple copies wanted */
input_p ++; /* adjust to point to counter */
i = 0;
while (isdigit(*input_p)) {
i = i * 10 + (*input_p - '0'); /* count */
input_p ++;
} /* while */
/* Check for error condition */
if ('}' != *input_p || i == 0 || i > 255) {
rc = 1;
i = 0;
fprintf(stderr, "* Illegal character count at line "
"%u\n",
linenumber);
} /* if */
} /* if */
if (i > 0) {
/* Copy the wanted number of characters */
while (i) {
*(output_p ++) = match;
i --;
} /* while */
/* Input should now point to the } end delimeter,
* skip this
*/
input_p ++;
} /* if */
} /* if */
else {
rc = 1;
fprintf(stderr, "* Illegal special character: {%s} at "
"line %u\n",
buf, linenumber);
} /* else */
} /* else */
} /* if */
else if (!quotemode) { /* check for token */
match = FALSE;
/* Skip tokenization attempt if:
* . No tokenization flag is set
* . Input string starts with numeral or space
* (no tokens starts with numerals or spaces)
*/
if (notokenize || ' ' == *input_p || isdigit(*input_p))
goto skiptokenize; /* Looks better than nested if */
/* C64 BASIC */
for (i = 0; i <= 75 && !match; i ++) {
tokenlen = strlen(c64tokens[i]); /* -=TODO:=- table lookup */
if (inputleft >= tokenlen &&
0 == strncasecmp(input_p, c64tokens[i], tokenlen)) {
/* token match found */
match = TRUE;
(*output_p ++) = i + 128; /* write token */
input_p += tokenlen; /* skip token */
inputleft -= tokenlen;
if (15 == i || 3 == i) { /* REM & DATA */
notokenize = TRUE;
}
} /* if */
} /* for */
/* C128 BASIC 7.0/7.1 */
if (!match && (Basic7 == mode || Basic71 == mode)) {
for (i = 2; i <= ((mode == Basic7) ? 38 : 55) && !match;
i ++) {
tokenlen = strlen(c128FEtokens[i]); /* as above */
if (tokenlen && inputleft >= tokenlen &&
0 == strncasecmp(input_p, c128FEtokens[i], tokenlen)) {
/* token match found */
match = TRUE;
(*output_p ++) = 0xFE; /* token escape */
(*output_p ++) = i; /* write token */
input_p += tokenlen; /* skip token */
inputleft -= tokenlen;
} /* if */
} /* for */
if (match) goto skipover; /* nicer than nested ifs */
for (i = 2; i <= 9 && !match; i ++) {
tokenlen = strlen(c128CEtokens[i]); /* as above */
if (tokenlen && inputleft >= tokenlen &&
0 == strncasecmp(input_p, c128CEtokens[i], tokenlen)) {
/* token match found */
match = TRUE;
(*output_p ++) = 0xCE; /* token escape */
(*output_p ++) = i; /* write token */
input_p += tokenlen; /* skip token */
inputleft -= tokenlen;
} /* if */
} /* for */
skipover: ;
} /* if */
if (!match && (Basic7 == mode || Basic71 == mode ||
Basic35 == mode)) {
for (i = 0; i <= 49 && !match; i ++) {
if (0xCE == i && Basic35 != mode) i ++;
/* skip prefix 0xCE in BASIC 7.0/7.1 */
tokenlen = strlen(c128tokens[i]); /* as above */
if (tokenlen && inputleft >= tokenlen &&
0 == strncasecmp(input_p, c128tokens[i], tokenlen)) {
/* token match found */
match = TRUE;
(*output_p ++) = i + 204; /* write token */
input_p += tokenlen; /* skip token */
inputleft -= tokenlen;
} /* if */
} /* for */
} /* if */
/* TFC3 */
if (!match && TFC3 == mode) {
for (i = 0; i <= 28 && !match; i ++) {
tokenlen = strlen(tfc3tokens[i]); /* as above */
if (inputleft >= tokenlen &&
0 == strncasecmp(input_p, tfc3tokens[i], tokenlen)) {
/* token match found */
match = TRUE;
(*output_p ++) = i + 204; /* write token */
input_p += tokenlen; /* skip token */
inputleft -= tokenlen;
} /* if */
} /* for */
} /* if */
/* Graphics52 */
if (!match && Graphics52 == mode) {
for (i = 0; i <= 50 && !match; i ++) {
tokenlen = strlen(graphics52tokens[i]); /* as above */
if (inputleft >= tokenlen &&
0 == strncasecmp(input_p, graphics52tokens[i],
tokenlen)) {
/* token match found */
match = TRUE;
(*output_p ++) = i + 204; /* write token */
input_p += tokenlen; /* skip token */
inputleft -= tokenlen;
} /* if */
} /* for */
} /* if */
/* PET BASIC 4.0/C64 BASIC 4.0 extension */
if (!match && Basic4 == mode) {
for (i = 0; i <= 23 && !match; i ++) {
tokenlen = strlen(basic4tokens[i]); /* as above */
if (inputleft >= tokenlen &&
0 == strncasecmp(input_p, basic4tokens[i], tokenlen)) {
/* token match found */
match = TRUE;
(*output_p ++) = i + 204; /* write token */
input_p += tokenlen; /* skip token */
inputleft -= tokenlen;
} /* if */
} /* for */
} /* if */
/* VIC Super Extender BASIC extension */
if (!match && VicSuper == mode) {
for (i = 0; i <= 17 && !match; i ++) {
tokenlen = strlen(supertokens[i]); /* as above */
if (inputleft >= tokenlen &&
0 == strncasecmp(input_p, supertokens[i], tokenlen)) {
/* token match found */
match = TRUE;
(*output_p ++) = i + 204; /* write token */
input_p += tokenlen; /* skip token */
inputleft -= tokenlen;
} /* if */
} /* for */
} /* if */
skiptokenize:
if (!match) {
/* there was no match on a token, just convert to
* petscii and write out.
* Since everything other than lowercase letters and
* special characters should have been catched here,
* everything should be written as such.
* Other characters are reported as errors.
*/
if ((*input_p >= 32 && *input_p <= 91) ||
*input_p == 93) {
/* these need not to be converted
* (uppercase ASCII => lowercase PETSCII)
*/
*(output_p ++) = *input_p;
if ('\"' == *input_p) {
quotemode = !quotemode; /* invert quotemode */
} /* if */
input_p ++;
inputleft --;
} /* if */
else if (*input_p >= 96 && *input_p <= 122) {
/* lowercase ASCII, convert to lowercase PETSCII) */
*(output_p ++) = *input_p - 32;
input_p ++;
inputleft --;
} /* if */
else { /* illegal character */
fprintf(stderr, "* Illegal character in input (nonquoted): "
"%c (%hu) at line %u\n",
*input_p, (unsigned short) *input_p, linenumber);
input_p ++;
rc = 1;
} /* else */
} /* if */
} /* else */
else if (quotemode) { /* non-special character quoted */
/* Map special characters (32-64) on themselves
* uppercase ASCII (65-90) on uppercase PETSCII (193-228)
* lowercase ASCII (97-122) on lowercase PETSCII (65-90)
* other specials (91,93,94) on themselves
* other characters should not be there
*/
if ((*input_p >= 32 && *input_p <= 64) ||
(91 == *input_p || 93 == *input_p || 94 == *input_p)) {
*(output_p ++) = *input_p;
if ('\"' == *input_p) {
quotemode = !quotemode; /* invert quotemode */
} /* if */
input_p ++;
inputleft --;
} /* if */
else if (*input_p >= 65 && *input_p <= 90) {
*(output_p ++) = *input_p | 128;
input_p ++;
inputleft --;
} /* if */
else if (*input_p >= 97 && *input_p <= 122) {
*(output_p ++) = *input_p & (~32);
input_p ++;
inputleft --;
} /* if */
else {
/* Unknown character */
fprintf(stderr, "* Illegal character in input (quoted): "
"%c (%hu) at line %u\n",
*input_p, (unsigned short) *input_p, linenumber);
input_p ++;
rc = 1;
} /* else */
} /* else */
} /* while */
*output_p = 0; /* end bytestream with a 0 */
*length_p = output_p - start_p + 1; /* tokenized stream length */
return rc; /* return errorcode */
}