fgen.l revision 1.30 1 %{
2 /* $NetBSD: fgen.l,v 1.30 2009/04/12 03:35:43 lukem Exp $ */
3 /* FLEX input for FORTH input file scanner */
4 /*
5 * Copyright (c) 1998 Eduardo Horvath.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Eduardo Horvath.
19 * 4. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 /*
34 Specifications are as follows:
35
36 The function "yylex()" always returns a pointer to a structure:
37
38 struct tok {
39 int type;
40 char *text;
41 }
42 #define TOKEN struct tok
43 */
44 #include <sys/cdefs.h>
45 #ifdef HAVE_NBTOOL_CONFIG_H
46 #include "nbtool_config.h"
47 #endif
48
49 #if defined(__RCSID) && !defined(lint)
50 __RCSID("$NetBSD: fgen.l,v 1.30 2009/04/12 03:35:43 lukem Exp $");
51 #endif
52
53 %}
54
55 %option yylineno
56
57 decimal [0-9.]
58 hex [0-9A-Fa-f.]
59 octal [0-7.]
60 white [ \t\n\r\f]
61 tail {white}
62
63 %{
64 #include <sys/types.h>
65 #include <arpa/inet.h>
66
67 #include <assert.h>
68 #include <err.h>
69 #include <errno.h>
70 #include <fcntl.h>
71 #include <stdarg.h>
72 #include <stdio.h>
73 #include <string.h>
74 #include <unistd.h>
75
76 #include "fgen.h"
77 TOKEN ltoken;
78
79 /*
80 * Global variables that control the parse state.
81 */
82
83 struct fcode *dictionary = NULL;
84 struct macro *aliases = NULL;
85 int outf = 1; /* stdout */
86 int state = 0;
87 int nextfcode = 0x800;
88 int numbase = TOK_HEX;
89 long outpos;
90 char *outbuf = NULL;
91 char *outfile, *infile;
92 #define BUFCLICK (1024*1024)
93 size_t outbufsiz = 0;
94 char *myname = NULL;
95 int offsetsize = 8;
96 int defining = 0;
97 int tokenizer = 0;
98
99 #define PSTKSIZ 1024
100 Cell parse_stack[PSTKSIZ];
101 int parse_stack_ptr = 0;
102
103 void token_err(int, const char *, const char *, const char *, ...)
104 __attribute__((__format__(__printf__, 4, 5)));
105 YY_DECL;
106
107 int debug = 0;
108 #define ASSERT if (debug) assert
109 #define STATE(y, x) do { if (debug) printf( "%ld State %s: token `%s'\n", outpos, x, y); } while (0)
110
111 #define YY_NO_UNPUT
112 %}
113
114 %%
115
116 0 { ltoken.type = TOK_OTHER; ltoken.text = yytext; return <oken; }
117
118 1 { ltoken.type = TOK_OTHER; ltoken.text = yytext; return <oken; }
119
120 2 { ltoken.type = TOK_OTHER; ltoken.text = yytext; return <oken; }
121
122 3 { ltoken.type = TOK_OTHER; ltoken.text = yytext; return <oken; }
123
124 -1 { ltoken.type = TOK_OTHER; ltoken.text = yytext; return <oken; }
125
126 \. { ltoken.type = TOK_OTHER; ltoken.text = yytext; return <oken; }
127
128 {white}* /* whitespace -- keep looping */ ;
129
130 \\[^\n]*\n /* end of line comment -- keep looping */ { STATE(yytext, "EOL comment"); }
131
132 -?{hex}+ { ltoken.type = TOK_NUMBER; ltoken.text = yytext;
133 return <oken; }
134
135 \'.\' { ltoken.type = TOK_C_LIT; ltoken.text = yytext; return <oken; }
136
137 \"{white}*(\\\"|[^"])*\" { ltoken.type = TOK_STRING_LIT; ltoken.text = yytext;
138 return <oken; } /* String started by `"' or `."' */
139
140 \.\({white}*(\\\"|[^)])*\) { ltoken.type = TOK_PSTRING; ltoken.text = yytext;
141 return <oken; } /* String of type `.(.....)' */
142
143 \.\"{white}*(\\\"|[^"])*\" { ltoken.type = TOK_PSTRING; ltoken.text = yytext;
144 return <oken; }
145
146 "(" { ltoken.type = TOK_COMMENT; ltoken.text = yytext;
147 return <oken; }
148
149 ")" { ltoken.type = TOK_ENDCOMMENT; ltoken.text = yytext;
150 return <oken; }
151
152 ":" { ltoken.type = TOK_COLON; ltoken.text = yytext;
153 return <oken; }
154
155 ";" { ltoken.type = TOK_SEMICOLON; ltoken.text = yytext;
156 return <oken; }
157
158 \' { ltoken.type = TOK_TOKENIZE; ltoken.text = yytext;
159 return <oken; }
160
161 [aA][gG][aA][iI][nN] { ltoken.type = TOK_AGAIN; ltoken.text = yytext;
162 return <oken; }
163
164 [aA][lL][iI][aA][sS] { ltoken.type = TOK_ALIAS; ltoken.text = yytext;
165 return <oken; }
166
167 \[\'\] { ltoken.type = TOK_GETTOKEN; ltoken.text = yytext;
168 return <oken; }
169
170 [aA][sS][cC][iI][iI] { ltoken.type = TOK_ASCII; ltoken.text = yytext;
171 return <oken; }
172
173 [bB][eE][gG][iI][nN] { ltoken.type = TOK_BEGIN; ltoken.text = yytext;
174 return <oken; }
175
176 [bB][uU][fF][fF][eE][rR]: { ltoken.type = TOK_BUFFER; ltoken.text = yytext;
177 return <oken; }
178
179 [cC][aA][sS][eE] { ltoken.type = TOK_CASE; ltoken.text = yytext;
180 return <oken; }
181
182 [cC][oO][nN][sS][tT][aA][nN][tT] { ltoken.type = TOK_CONSTANT; ltoken.text = yytext;
183 return <oken; }
184
185 [cC][oO][nN][tT][rR][oO][lL] { ltoken.type = TOK_CONTROL; ltoken.text = yytext;
186 return <oken; }
187
188 [cC][rR][eE][aA][tT][eE] { ltoken.type = TOK_CREATE; ltoken.text = yytext;
189 return <oken; }
190
191 [dD]# { ltoken.type = TOK_DECIMAL; ltoken.text = yytext;
192 return <oken; }
193
194 [dD][eE][cC][iI][mM][aA][lL] { ltoken.type = TOK_DECIMAL; ltoken.text = yytext;
195 return <oken; }
196
197 [dD][eE][fF][eE][rR] { ltoken.type = TOK_DEFER; ltoken.text = yytext;
198 return <oken; }
199
200 \??[dD][oO] { ltoken.type = TOK_DO; ltoken.text = yytext;
201 return <oken; }
202
203 [eE][lL][sS][eE] { ltoken.type = TOK_ELSE; ltoken.text = yytext;
204 return <oken; }
205
206 [eE][nN][dD][cC][aA][sS][eE] { ltoken.type = TOK_ENDCASE; ltoken.text = yytext;
207 return <oken; }
208
209 [eE][nN][dD][oO][fF] { ltoken.type = TOK_ENDOF; ltoken.text = yytext;
210 return <oken; }
211
212 [eE][xX][tT][eE][rR][nN][aA][lL] { ltoken.type = TOK_EXTERNAL; ltoken.text = yytext;
213 return <oken; }
214
215 [fF][iI][eE][lL][dD] { ltoken.type = TOK_FIELD; ltoken.text = yytext;
216 return <oken; }
217
218 [hH]# { ltoken.type = TOK_HEX; ltoken.text = yytext;
219 return <oken; }
220
221 [hH][eE][aA][dD][eE][rR][lL][eE][sS][sS] { ltoken.type = TOK_HEADERLESS; ltoken.text = yytext;
222 return <oken; }
223
224 [hH][eE][aA][dD][eE][rR][sS] { ltoken.type = TOK_HEADERS; ltoken.text = yytext;
225 return <oken; }
226
227 [hH][eE][xX] { ltoken.type = TOK_HEX; ltoken.text = yytext;
228 return <oken; }
229
230 [iI][fF] { ltoken.type = TOK_IF; ltoken.text = yytext;
231 return <oken; }
232
233 \??[lL][eE][aA][vV][eE] { ltoken.type = TOK_LEAVE; ltoken.text = yytext;
234 return <oken; }
235
236 \+?[lL][oO][oO][pP] { ltoken.type = TOK_LOOP; ltoken.text = yytext;
237 return <oken; }
238
239 [oO]# { ltoken.type = TOK_OCTAL; ltoken.text = yytext;
240 return <oken; }
241
242 [oO][cC][tT][aA][lL] { ltoken.type = TOK_OCTAL; ltoken.text = yytext;
243 return <oken; }
244
245 [oO][fF] { ltoken.type = TOK_OF; ltoken.text = yytext;
246 return <oken; }
247
248 [rR][eE][pP][eE][aA][tT] { ltoken.type = TOK_REPEAT; ltoken.text = yytext;
249 return <oken; }
250
251 [tT][hH][eE][nN] { ltoken.type = TOK_THEN; ltoken.text = yytext;
252 return <oken; }
253
254 [tT][oO] { ltoken.type = TOK_TO; ltoken.text = yytext;
255 return <oken; }
256
257 [uU][nN][tT][iI][lL] { ltoken.type = TOK_UNTIL; ltoken.text = yytext;
258 return <oken; }
259
260 [vV][aA][lL][uU][eE] { ltoken.type = TOK_VALUE; ltoken.text = yytext;
261 return <oken; }
262
263 [vV][aA][rR][iI][aA][bB][lL][eE] { ltoken.type = TOK_VARIABLE; ltoken.text = yytext;
264 return <oken; }
265
266 [wW][hH][iI][lL][eE] { ltoken.type = TOK_WHILE; ltoken.text = yytext;
267 return <oken; }
268
269 offset16 { ltoken.type = TOK_OFFSET16; ltoken.text = yytext;
270 return <oken; }
271
272 tokenizer\[ { ltoken.type = TOK_BEGTOK; ltoken.text = yytext;
273 return <oken; }
274
275 emit-byte { ltoken.type = TOK_EMIT_BYTE; ltoken.text = yytext;
276 return <oken; }
277
278 \]tokenizer { ltoken.type = TOK_ENDTOK; ltoken.text = yytext;
279 return <oken; }
280
281 fload { ltoken.type = TOK_FLOAD; ltoken.text = yytext;
282 return <oken; }
283
284
285 [^ \n\t\r\f]+ { ltoken.type = TOK_OTHER; ltoken.text = yytext;
286 return <oken; }
287
288 <<EOF>> { return NULL; }
289 %%
290
291 /* Function definitions */
292 void push(Cell);
293 Cell pop(void);
294 int depth(void);
295 int fadd(struct fcode *, struct fcode *);
296 struct fcode *flookup(struct fcode *, const char *);
297 int aadd(struct macro *, struct macro *);
298 struct macro *alookup(struct macro *, const char *);
299 void initdic(void);
300 void usage(const char *);
301 void tokenize(YY_BUFFER_STATE);
302 int emit(const char *);
303 int spit(long);
304 void sspit(const char *);
305 int apply_macros(YY_BUFFER_STATE, const char *);
306 int main(int argc, char *argv[]);
307 Cell cvt(const char *, char **, int base);
308
309 /*
310 * Standard FCode names and numbers. Includes standard
311 * tokenizer aliases.
312 */
313 struct fcode fcodes[] = {
314 { "end0", 0x0000, 0, NULL, NULL },
315 { "b(lit)", 0x0010, 0, NULL, NULL },
316 { "b(')", 0x0011, 0, NULL, NULL },
317 { "b(\")", 0x0012, 0, NULL, NULL },
318 { "bbranch", 0x0013, 0, NULL, NULL },
319 { "b?branch", 0x0014, 0, NULL, NULL },
320 { "b(loop)", 0x0015, 0, NULL, NULL },
321 { "b(+loop)", 0x0016, 0, NULL, NULL },
322 { "b(do)", 0x0017, 0, NULL, NULL },
323 { "b(?do)", 0x0018, 0, NULL, NULL },
324 { "i", 0x0019, 0, NULL, NULL },
325 { "j", 0x001a, 0, NULL, NULL },
326 { "b(leave)", 0x001b, 0, NULL, NULL },
327 { "b(of)", 0x001c, 0, NULL, NULL },
328 { "execute", 0x001d, 0, NULL, NULL },
329 { "+", 0x001e, 0, NULL, NULL },
330 { "-", 0x001f, 0, NULL, NULL },
331 { "*", 0x0020, 0, NULL, NULL },
332 { "/", 0x0021, 0, NULL, NULL },
333 { "mod", 0x0022, 0, NULL, NULL },
334 { "and", 0x0023, 0, NULL, NULL },
335 { "or", 0x0024, 0, NULL, NULL },
336 { "xor", 0x0025, 0, NULL, NULL },
337 { "invert", 0x0026, 0, NULL, NULL },
338 { "lshift", 0x0027, 0, NULL, NULL },
339 { "rshift", 0x0028, 0, NULL, NULL },
340 { ">>a", 0x0029, 0, NULL, NULL },
341 { "/mod", 0x002a, 0, NULL, NULL },
342 { "u/mod", 0x002b, 0, NULL, NULL },
343 { "negate", 0x002c, 0, NULL, NULL },
344 { "abs", 0x002d, 0, NULL, NULL },
345 { "min", 0x002e, 0, NULL, NULL },
346 { "max", 0x002f, 0, NULL, NULL },
347 { ">r", 0x0030, 0, NULL, NULL },
348 { "r>", 0x0031, 0, NULL, NULL },
349 { "r@", 0x0032, 0, NULL, NULL },
350 { "exit", 0x0033, 0, NULL, NULL },
351 { "0=", 0x0034, 0, NULL, NULL },
352 { "0<>", 0x0035, 0, NULL, NULL },
353 { "0<", 0x0036, 0, NULL, NULL },
354 { "0<=", 0x0037, 0, NULL, NULL },
355 { "0>", 0x0038, 0, NULL, NULL },
356 { "0>=", 0x0039, 0, NULL, NULL },
357 { "<", 0x003a, 0, NULL, NULL },
358 { ">", 0x003b, 0, NULL, NULL },
359 { "=", 0x003c, 0, NULL, NULL },
360 { "<>", 0x003d, 0, NULL, NULL },
361 { "u>", 0x003e, 0, NULL, NULL },
362 { "u<=", 0x003f, 0, NULL, NULL },
363 { "u<", 0x0040, 0, NULL, NULL },
364 { "u>=", 0x0041, 0, NULL, NULL },
365 { ">=", 0x0042, 0, NULL, NULL },
366 { "<=", 0x0043, 0, NULL, NULL },
367 { "between", 0x0044, 0, NULL, NULL },
368 { "within", 0x0045, 0, NULL, NULL },
369 { "drop", 0x0046, 0, NULL, NULL },
370 { "dup", 0x0047, 0, NULL, NULL },
371 { "over", 0x0048, 0, NULL, NULL },
372 { "swap", 0x0049, 0, NULL, NULL },
373 { "rot", 0x004a, 0, NULL, NULL },
374 { "-rot", 0x004b, 0, NULL, NULL },
375 { "tuck", 0x004c, 0, NULL, NULL },
376 { "nip", 0x004d, 0, NULL, NULL },
377 { "pick", 0x004e, 0, NULL, NULL },
378 { "roll", 0x004f, 0, NULL, NULL },
379 { "?dup", 0x0050, 0, NULL, NULL },
380 { "depth", 0x0051, 0, NULL, NULL },
381 { "2drop", 0x0052, 0, NULL, NULL },
382 { "2dup", 0x0053, 0, NULL, NULL },
383 { "2over", 0x0054, 0, NULL, NULL },
384 { "2swap", 0x0055, 0, NULL, NULL },
385 { "2rot", 0x0056, 0, NULL, NULL },
386 { "2/", 0x0057, 0, NULL, NULL },
387 { "u2/", 0x0058, 0, NULL, NULL },
388 { "2*", 0x0059, 0, NULL, NULL },
389 { "/c", 0x005a, 0, NULL, NULL },
390 { "/w", 0x005b, 0, NULL, NULL },
391 { "/l", 0x005c, 0, NULL, NULL },
392 { "/n", 0x005d, 0, NULL, NULL },
393 { "ca+", 0x005e, 0, NULL, NULL },
394 { "wa+", 0x005f, 0, NULL, NULL },
395 { "la+", 0x0060, 0, NULL, NULL },
396 { "na+", 0x0061, 0, NULL, NULL },
397 { "char+", 0x0062, 0, NULL, NULL },
398 { "wa1+", 0x0063, 0, NULL, NULL },
399 { "la1+", 0x0064, 0, NULL, NULL },
400 { "cell+", 0x0065, 0, NULL, NULL },
401 { "chars", 0x0066, 0, NULL, NULL },
402 { "/w*", 0x0067, 0, NULL, NULL },
403 { "/l*", 0x0068, 0, NULL, NULL },
404 { "cells", 0x0069, 0, NULL, NULL },
405 { "on", 0x006a, 0, NULL, NULL },
406 { "off", 0x006b, 0, NULL, NULL },
407 { "+!", 0x006c, 0, NULL, NULL },
408 { "@", 0x006d, 0, NULL, NULL },
409 { "l@", 0x006e, 0, NULL, NULL },
410 { "w@", 0x006f, 0, NULL, NULL },
411 { "<w@", 0x0070, 0, NULL, NULL },
412 { "c@", 0x0071, 0, NULL, NULL },
413 { "!", 0x0072, 0, NULL, NULL },
414 { "l!", 0x0073, 0, NULL, NULL },
415 { "w!", 0x0074, 0, NULL, NULL },
416 { "c!", 0x0075, 0, NULL, NULL },
417 { "2@", 0x0076, 0, NULL, NULL },
418 { "2!", 0x0077, 0, NULL, NULL },
419 { "move", 0x0078, 0, NULL, NULL },
420 { "fill", 0x0079, 0, NULL, NULL },
421 { "comp", 0x007a, 0, NULL, NULL },
422 { "noop", 0x007b, 0, NULL, NULL },
423 { "lwsplit", 0x007c, 0, NULL, NULL },
424 { "wjoin", 0x007d, 0, NULL, NULL },
425 { "lbsplit", 0x007e, 0, NULL, NULL },
426 { "bljoin", 0x007f, 0, NULL, NULL },
427 { "wbflip", 0x0080, 0, NULL, NULL },
428 { "upc", 0x0081, 0, NULL, NULL },
429 { "lcc", 0x0082, 0, NULL, NULL },
430 { "pack", 0x0083, 0, NULL, NULL },
431 { "count", 0x0084, 0, NULL, NULL },
432 { "body>", 0x0085, 0, NULL, NULL },
433 { ">body", 0x0086, 0, NULL, NULL },
434 { "fcode-revision", 0x0087, 0, NULL, NULL },
435 { "span", 0x0088, 0, NULL, NULL },
436 { "unloop", 0x0089, 0, NULL, NULL },
437 { "expect", 0x008a, 0, NULL, NULL },
438 { "alloc-mem", 0x008b, 0, NULL, NULL },
439 { "free-mem", 0x008c, 0, NULL, NULL },
440 { "key?", 0x008d, 0, NULL, NULL },
441 { "key", 0x008e, 0, NULL, NULL },
442 { "emit", 0x008f, 0, NULL, NULL },
443 { "type", 0x0090, 0, NULL, NULL },
444 { "(cr", 0x0091, 0, NULL, NULL },
445 { "cr", 0x0092, 0, NULL, NULL },
446 { "#out", 0x0093, 0, NULL, NULL },
447 { "#line", 0x0094, 0, NULL, NULL },
448 { "hold", 0x0095, 0, NULL, NULL },
449 { "<#", 0x0096, 0, NULL, NULL },
450 { "u#>", 0x0097, 0, NULL, NULL },
451 { "sign", 0x0098, 0, NULL, NULL },
452 { "u#", 0x0099, 0, NULL, NULL },
453 { "u#s", 0x009a, 0, NULL, NULL },
454 { "u.", 0x009b, 0, NULL, NULL },
455 { "u.r", 0x009c, 0, NULL, NULL },
456 { ".", 0x009d, 0, NULL, NULL },
457 { ".r", 0x009e, 0, NULL, NULL },
458 { ".s", 0x009f, 0, NULL, NULL },
459 { "base", 0x00a0, 0, NULL, NULL },
460 { "convert", 0x00a1, 0, NULL, NULL },
461 { "$number", 0x00a2, 0, NULL, NULL },
462 { "digit", 0x00a3, 0, NULL, NULL },
463 { "-1", 0x00a4, 0, NULL, NULL },
464 { "true", 0x00a4, 0, NULL, NULL },
465 { "0", 0x00a5, 0, NULL, NULL },
466 { "1", 0x00a6, 0, NULL, NULL },
467 { "2", 0x00a7, 0, NULL, NULL },
468 { "3", 0x00a8, 0, NULL, NULL },
469 { "bl", 0x00a9, 0, NULL, NULL },
470 { "bs", 0x00aa, 0, NULL, NULL },
471 { "bell", 0x00ab, 0, NULL, NULL },
472 { "bounds", 0x00ac, 0, NULL, NULL },
473 { "here", 0x00ad, 0, NULL, NULL },
474 { "aligned", 0x00ae, 0, NULL, NULL },
475 { "wbsplit", 0x00af, 0, NULL, NULL },
476 { "bwjoin", 0x00b0, 0, NULL, NULL },
477 { "b(<mark)", 0x00b1, 0, NULL, NULL },
478 { "b(>resolve)", 0x00b2, 0, NULL, NULL },
479 { "set-token-table", 0x00b3, 0, NULL, NULL },
480 { "set-table", 0x00b4, 0, NULL, NULL },
481 { "new-token", 0x00b5, 0, NULL, NULL },
482 { "named-token", 0x00b6, 0, NULL, NULL },
483 { "b(:)", 0x00b7, 0, NULL, NULL },
484 { "b(value)", 0x00b8, 0, NULL, NULL },
485 { "b(variable)", 0x00b9, 0, NULL, NULL },
486 { "b(constant)", 0x00ba, 0, NULL, NULL },
487 { "b(create)", 0x00bb, 0, NULL, NULL },
488 { "b(defer)", 0x00bc, 0, NULL, NULL },
489 { "b(buffer:)", 0x00bd, 0, NULL, NULL },
490 { "b(field)", 0x00be, 0, NULL, NULL },
491 { "b(code)", 0x00bf, 0, NULL, NULL },
492 { "instance", 0x00c0, 0, NULL, NULL },
493 { "b(;)", 0x00c2, 0, NULL, NULL },
494 { "b(to)", 0x00c3, 0, NULL, NULL },
495 { "b(case)", 0x00c4, 0, NULL, NULL },
496 { "b(endcase)", 0x00c5, 0, NULL, NULL },
497 { "b(endof)", 0x00c6, 0, NULL, NULL },
498 { "#", 0x00c7, 0, NULL, NULL },
499 { "#s", 0x00c8, 0, NULL, NULL },
500 { "#>", 0x00c9, 0, NULL, NULL },
501 { "external-token", 0x00ca, 0, NULL, NULL },
502 { "$find", 0x00cb, 0, NULL, NULL },
503 { "offset16", 0x00cc, 0, NULL, NULL },
504 { "evaluate", 0x00cd, 0, NULL, NULL },
505 { "c,", 0x00d0, 0, NULL, NULL },
506 { "w,", 0x00d1, 0, NULL, NULL },
507 { "l,", 0x00d2, 0, NULL, NULL },
508 { "'", 0x00d3, 0, NULL, NULL },
509 { "um*", 0x00d4, 0, NULL, NULL },
510 { "um/mod", 0x00d5, 0, NULL, NULL },
511 { "d+", 0x00d8, 0, NULL, NULL },
512 { "d-", 0x00d9, 0, NULL, NULL },
513 { "get-token", 0x00da, 0, NULL, NULL },
514 { "set-token", 0x00db, 0, NULL, NULL },
515 { "state", 0x00dc, 0, NULL, NULL },
516 { "compile,", 0x00dd, 0, NULL, NULL },
517 { "behavior", 0x00de, 0, NULL, NULL },
518 { "start0", 0x00f0, 0, NULL, NULL },
519 { "start1", 0x00f1, 0, NULL, NULL },
520 { "start2", 0x00f2, 0, NULL, NULL },
521 { "start4", 0x00f3, 0, NULL, NULL },
522 { "ferror", 0x00fc, 0, NULL, NULL },
523 { "version1", 0x00fd, 0, NULL, NULL },
524 { "4-byte-id", 0x00fe, 0, NULL, NULL },
525 { "end1", 0x00ff, 0, NULL, NULL },
526 { "dma-alloc", 0x0101, 0, NULL, NULL },
527 { "my-address", 0x0102, 0, NULL, NULL },
528 { "my-space", 0x0103, 0, NULL, NULL },
529 { "memmap", 0x0104, 0, NULL, NULL },
530 { "free-virtual", 0x0105, 0, NULL, NULL },
531 { ">physical", 0x0106, 0, NULL, NULL },
532 { "my-params", 0x010f, 0, NULL, NULL },
533 { "property", 0x0110, 0, NULL, NULL },
534 { "encode-int", 0x0111, 0, NULL, NULL },
535 { "encode+", 0x0112, 0, NULL, NULL },
536 { "encode-phys", 0x0113, 0, NULL, NULL },
537 { "encode-string", 0x0114, 0, NULL, NULL },
538 { "encode-bytes", 0x0115, 0, NULL, NULL },
539 { "reg", 0x0116, 0, NULL, NULL },
540 { "intr", 0x0117, 0, NULL, NULL },
541 { "driver", 0x0118, 0, NULL, NULL },
542 { "model", 0x0119, 0, NULL, NULL },
543 { "device-type", 0x011a, 0, NULL, NULL },
544 { "parse-2int", 0x011b, 0, NULL, NULL },
545 { "is-install", 0x011c, 0, NULL, NULL },
546 { "is-remove", 0x011d, 0, NULL, NULL },
547 { "is-selftest", 0x011e, 0, NULL, NULL },
548 { "new-device", 0x011f, 0, NULL, NULL },
549 { "diagnostic-mode?", 0x0120, 0, NULL, NULL },
550 { "display-status", 0x0121, 0, NULL, NULL },
551 { "memory-test-suite", 0x0122, 0, NULL, NULL },
552 { "group-code", 0x0123, 0, NULL, NULL },
553 { "mask", 0x0124, 0, NULL, NULL },
554 { "get-msecs", 0x0125, 0, NULL, NULL },
555 { "ms", 0x0126, 0, NULL, NULL },
556 { "find-device", 0x0127, 0, NULL, NULL },
557 { "decode-phys", 0x0128, 0, NULL, NULL },
558 { "map-low", 0x0130, 0, NULL, NULL },
559 { "sbus-intr>cpu", 0x0131, 0, NULL, NULL },
560 { "#lines", 0x0150, 0, NULL, NULL },
561 { "#columns", 0x0151, 0, NULL, NULL },
562 { "line#", 0x0152, 0, NULL, NULL },
563 { "column#", 0x0153, 0, NULL, NULL },
564 { "inverse?", 0x0154, 0, NULL, NULL },
565 { "inverse-screen?", 0x0155, 0, NULL, NULL },
566 { "frame-buffer-busy?", 0x0156, 0, NULL, NULL },
567 { "draw-character", 0x0157, 0, NULL, NULL },
568 { "reset-screen", 0x0158, 0, NULL, NULL },
569 { "toggle-cursor", 0x0159, 0, NULL, NULL },
570 { "erase-screen", 0x015a, 0, NULL, NULL },
571 { "blink-screen", 0x015b, 0, NULL, NULL },
572 { "invert-screen", 0x015c, 0, NULL, NULL },
573 { "insert-characters", 0x015d, 0, NULL, NULL },
574 { "delete-characters", 0x015e, 0, NULL, NULL },
575 { "insert-lines", 0x015f, 0, NULL, NULL },
576 { "delete-lines", 0x0160, 0, NULL, NULL },
577 { "draw-logo", 0x0161, 0, NULL, NULL },
578 { "frame-buffer-addr", 0x0162, 0, NULL, NULL },
579 { "screen-height", 0x0163, 0, NULL, NULL },
580 { "screen-width", 0x0164, 0, NULL, NULL },
581 { "window-top", 0x0165, 0, NULL, NULL },
582 { "window-left", 0x0166, 0, NULL, NULL },
583 { "default-font", 0x016a, 0, NULL, NULL },
584 { "set-font", 0x016b, 0, NULL, NULL },
585 { "char-height", 0x016c, 0, NULL, NULL },
586 { "char-width", 0x016d, 0, NULL, NULL },
587 { ">font", 0x016e, 0, NULL, NULL },
588 { "fontbytes", 0x016f, 0, NULL, NULL },
589 { "fb8-draw-character", 0x0180, 0, NULL, NULL },
590 { "fb8-reset-screen", 0x0181, 0, NULL, NULL },
591 { "fb8-toggle-cursor", 0x0182, 0, NULL, NULL },
592 { "fb8-erase-screen", 0x0183, 0, NULL, NULL },
593 { "fb8-blink-screen", 0x0184, 0, NULL, NULL },
594 { "fb8-invert-screen", 0x0185, 0, NULL, NULL },
595 { "fb8-insert-characters", 0x0186, 0, NULL, NULL },
596 { "fb8-delete-characters", 0x0187, 0, NULL, NULL },
597 { "fb8-inisert-lines", 0x0188, 0, NULL, NULL },
598 { "fb8-delete-lines", 0x0189, 0, NULL, NULL },
599 { "fb8-draw-logo", 0x018a, 0, NULL, NULL },
600 { "fb8-install", 0x018b, 0, NULL, NULL },
601 { "return-buffer", 0x01a0, 0, NULL, NULL },
602 { "xmit-packet", 0x01a1, 0, NULL, NULL },
603 { "poll-packet", 0x01a2, 0, NULL, NULL },
604 { "mac-address", 0x01a4, 0, NULL, NULL },
605 { "device-name", 0x0201, 0, NULL, NULL },
606 { "my-args", 0x0202, 0, NULL, NULL },
607 { "my-self", 0x0203, 0, NULL, NULL },
608 { "find-package", 0x0204, 0, NULL, NULL },
609 { "open-package", 0x0205, 0, NULL, NULL },
610 { "close-package", 0x0206, 0, NULL, NULL },
611 { "find-method", 0x0207, 0, NULL, NULL },
612 { "call-package", 0x0208, 0, NULL, NULL },
613 { "$call-parent", 0x0209, 0, NULL, NULL },
614 { "my-parent", 0x020a, 0, NULL, NULL },
615 { "ihandle>phandle", 0x020b, 0, NULL, NULL },
616 { "my-unit", 0x020d, 0, NULL, NULL },
617 { "$call-method", 0x020e, 0, NULL, NULL },
618 { "$open-package", 0x020f, 0, NULL, NULL },
619 { "processor-type", 0x0210, 0, NULL, NULL },
620 { "firmware-version", 0x0211, 0, NULL, NULL },
621 { "fcode-version", 0x0212, 0, NULL, NULL },
622 { "alarm", 0x0213, 0, NULL, NULL },
623 { "(is-user-word)", 0x0214, 0, NULL, NULL },
624 { "suspend-fcode", 0x0215, 0, NULL, NULL },
625 { "abort", 0x0216, 0, NULL, NULL },
626 { "catch", 0x0217, 0, NULL, NULL },
627 { "throw", 0x0218, 0, NULL, NULL },
628 { "user-abort", 0x0219, 0, NULL, NULL },
629 { "get-my-property", 0x021a, 0, NULL, NULL },
630 { "decode-int", 0x021b, 0, NULL, NULL },
631 { "decode-string", 0x021c, 0, NULL, NULL },
632 { "get-inherited-property", 0x021d, 0, NULL, NULL },
633 { "delete-property", 0x021e, 0, NULL, NULL },
634 { "get-package-property", 0x021f, 0, NULL, NULL },
635 { "cpeek", 0x0220, 0, NULL, NULL },
636 { "wpeek", 0x0221, 0, NULL, NULL },
637 { "lpeek", 0x0222, 0, NULL, NULL },
638 { "cpoke", 0x0223, 0, NULL, NULL },
639 { "wpoke", 0x0224, 0, NULL, NULL },
640 { "lpoke", 0x0225, 0, NULL, NULL },
641 { "lwflip", 0x0226, 0, NULL, NULL },
642 { "lbflip", 0x0227, 0, NULL, NULL },
643 { "lbflips", 0x0228, 0, NULL, NULL },
644 { "adr-mask", 0x0229, 0, NULL, NULL },
645 { "rb@", 0x0230, 0, NULL, NULL },
646 { "rb!", 0x0231, 0, NULL, NULL },
647 { "rw@", 0x0232, 0, NULL, NULL },
648 { "rw!", 0x0233, 0, NULL, NULL },
649 { "rl@", 0x0234, 0, NULL, NULL },
650 { "rl!", 0x0235, 0, NULL, NULL },
651 { "wbflips", 0x0236, 0, NULL, NULL },
652 { "lwflips", 0x0237, 0, NULL, NULL },
653 { "probe", 0x0238, 0, NULL, NULL },
654 { "probe-virtual", 0x0239, 0, NULL, NULL },
655 { "child", 0x023b, 0, NULL, NULL },
656 { "peer", 0x023c, 0, NULL, NULL },
657 { "next-property", 0x023d, 0, NULL, NULL },
658 { "byte-load", 0x023e, 0, NULL, NULL },
659 { "set-args", 0x023f, 0, NULL, NULL },
660 { "left-parse-string", 0x0240, 0, NULL, NULL },
661 /* 64-bit FCode extensions */
662 { "bxjoin", 0x0241, 0, NULL, NULL },
663 { "<l@", 0x0242, 0, NULL, NULL },
664 { "lxjoin", 0x0243, 0, NULL, NULL },
665 { "rx@", 0x022e, 0, NULL, NULL },
666 { "rx!", 0x022f, 0, NULL, NULL },
667 { "wxjoin", 0x0244, 0, NULL, NULL },
668 { "x,", 0x0245, 0, NULL, NULL },
669 { "x@", 0x0246, 0, NULL, NULL },
670 { "x!", 0x0247, 0, NULL, NULL },
671 { "/x", 0x0248, 0, NULL, NULL },
672 { "/x*", 0x0249, 0, NULL, NULL },
673 { "xa+", 0x024a, 0, NULL, NULL },
674 { "xa1+", 0x024b, 0, NULL, NULL },
675 { "xbflip", 0x024c, 0, NULL, NULL },
676 { "xbflips", 0x024d, 0, NULL, NULL },
677 { "xbsplit", 0x024e, 0, NULL, NULL },
678 { "xlflip", 0x024f, 0, NULL, NULL },
679 { "xlflips", 0x0250, 0, NULL, NULL },
680 { "xlsplit", 0x0251, 0, NULL, NULL },
681 { "xwflip", 0x0252, 0, NULL, NULL },
682 { "xwflips", 0x0253, 0, NULL, NULL },
683 { "xwsplit", 0x0254, 0, NULL, NULL },
684 { NULL, 0, 0, NULL, NULL }
685 };
686
687 /*
688 * Default macros -- can be overridden by colon definitions.
689 */
690 struct macro macros[] = {
691 { "eval", "evaluate", 0, NULL, NULL }, /* Build a more balanced tree */
692 { "(.)", "dup abs <# u#s swap sign u#>", 0, NULL, NULL },
693 { "<<", "lshift", 0, NULL, NULL },
694 { ">>", "rshift", 0, NULL, NULL },
695 { "?", "@ .", 0, NULL, NULL },
696 { "1+", "1 +", 0, NULL, NULL },
697 { "1-", "1 -", 0, NULL, NULL },
698 { "2+", "2 +", 0, NULL, NULL },
699 { "2-", "2 -", 0, NULL, NULL },
700 { "abort\"", "-2 throw", 0, NULL, NULL },
701 { "accept", "span @ -rot expect span @ swap span !", 0, NULL, NULL },
702 { "allot", "0 max 0 ?do 0 c, loop", 0, NULL, NULL },
703 { "blank", "bl fill", 0, NULL, NULL },
704 { "/c*", "chars", 0, NULL, NULL },
705 { "ca1+", "char+", 0, NULL, NULL },
706 { "carret", "b(lit) 00 00 00 0x0d", 0, NULL, NULL },
707 { ".d", "base @ swap 0x0a base ! . base !", 0, NULL, NULL },
708 { "decode-bytes", ">r over r@ + swap r@ - rot r>", 0, NULL, NULL },
709 { "3drop", "drop 2drop", 0, NULL, NULL },
710 { "3dup", "2 pick 2 pick 2 pick", 0, NULL, NULL },
711 { "erase", "0 fill", 0, NULL, NULL },
712 { "false", "0", 0, NULL, NULL },
713 { ".h", "base @ swap 0x10 base ! . base !", 0, NULL, NULL },
714 { "linefeed", "b(lit) 00 00 00 0x0a", 0, NULL, NULL },
715 { "/n*", "cells", 0, NULL, NULL },
716 { "na1+", "cell+", 0, NULL, NULL },
717 { "not", "invert", 0, NULL, NULL },
718 { "s.", "(.) type space", 0, NULL, NULL },
719 { "space", "bl emit", 0, NULL, NULL },
720 { "spaces", "0 max 0 ?do space loop", 0, NULL, NULL },
721 { "struct", "0", 0, NULL, NULL },
722 { "true", "-1", 0, NULL, NULL },
723 { "(u,)", "<# u#s u#>", 0, NULL, NULL },
724 { NULL, NULL, 0, NULL, NULL }
725 };
726
727 /*
728 * Utility functions.
729 */
730
731 /*
732 * ASCII -> long int converter, eats `.'s
733 */
734 #define strtol(x, y, z) cvt(x, y, z)
735 Cell
736 cvt(const char *s, char **e, int base)
737 {
738 Cell v = 0;
739 int c, n = 0;
740
741 c = *s;
742 if (c == '-') { n = 1; s++; }
743
744 for (c = *s; (c = *s); s++) {
745
746 /* Ignore `.' */
747 if (c == '.')
748 continue;
749 if (c >= '0' && c <= '9')
750 c -= '0';
751 else if (c >= 'a' && c <= 'f')
752 c += 10 - 'a';
753 else if (c >= 'A' && c <= 'F')
754 c += 10 - 'A';
755 if (c >= base)
756 break;
757 v *= base;
758 v += c;
759 }
760 if (e)
761 *e = __UNCONST(s);
762 if (n)
763 return (-v);
764 return (v);
765 }
766
767 /*
768 * Parser stack control functions.
769 */
770
771 void
772 push(Cell val)
773 {
774 parse_stack[parse_stack_ptr++] = val;
775 if (parse_stack_ptr >= PSTKSIZ) {
776 (void)printf( "Parse stack overflow\n");
777 exit(1);
778 }
779 }
780
781 Cell
782 pop(void)
783 {
784 ASSERT(parse_stack_ptr);
785 return parse_stack[--parse_stack_ptr];
786 }
787
788 int
789 depth(void)
790 {
791 return (parse_stack_ptr);
792 }
793
794 /*
795 * Insert fcode into dictionary.
796 */
797 int
798 fadd(struct fcode *dict, struct fcode *new)
799 {
800 int res = strcmp(dict->name, new->name);
801
802 #ifdef DEBUG
803 new->type = FCODE;
804 ASSERT(dict->type == FCODE);
805 #endif
806 /* Don't allow duplicate entries. */
807 if (!res) return (0);
808 if (res < 0) {
809 if (dict->l)
810 return fadd(dict->l, new);
811 else {
812 #ifdef DEBUG
813 if (debug > 1)
814 (void)printf( "fadd: new FCode `%s' is %lx\n",
815 new->name, new->num);
816 #endif
817 new->l = new->r = NULL;
818 dict->l = new;
819 }
820 } else {
821 if (dict->r)
822 return fadd(dict->r, new);
823 else {
824 #ifdef DEBUG
825 if (debug > 1)
826 (void)printf( "fadd: new FCode `%s' is %lx\n",
827 new->name, new->num);
828 #endif
829 new->l = new->r = NULL;
830 dict->r = new;
831 }
832 }
833 return (1);
834 }
835
836 /*
837 * Look for a code in the dictionary.
838 */
839 struct fcode *
840 flookup(struct fcode *dict, const char *str)
841 {
842 int res;
843 if (!dict) return (dict);
844
845 res = strcmp(dict->name, str);
846 #ifdef DEBUG
847 ASSERT(dict->type == FCODE);
848 if (debug > 2)
849 (void)printf( "flookup: `%s' and `%s' %s match\n",
850 str, dict->name, res?"don't":"do");
851 #endif
852 if (!res) return (dict);
853 if (res < 0)
854 return (flookup(dict->l, str));
855 else
856 return (flookup(dict->r, str));
857
858 }
859
860 /*
861 * Insert alias into macros.
862 */
863 int
864 aadd(struct macro *dict, struct macro *new)
865 {
866 int res = strcmp(dict->name, new->name);
867
868 #ifdef DEBUG
869 new->type = MACRO;
870 ASSERT(dict->type == MACRO);
871 #endif
872 /* Don't allow duplicate entries. */
873 if (!res) return (0);
874 if (res < 0) {
875 if (dict->l)
876 return aadd(dict->l, new);
877 else {
878 new->l = new->r = NULL;
879 dict->l = new;
880 #ifdef DEBUG
881 if (debug > 1)
882 (void)printf( "aadd: new alias `%s' to `%s'\n",
883 new->name, new->equiv);
884 #endif
885 }
886 } else {
887 if (dict->r)
888 return aadd(dict->r, new);
889 else {
890 new->l = new->r = NULL;
891 dict->r = new;
892 #ifdef DEBUG
893 if (debug > 1)
894 (void)printf( "aadd: new alias `%s' to `%s'\n",
895 new->name, new->equiv);
896 #endif
897 }
898 }
899 return (1);
900 }
901
902 /*
903 * Look for a macro in the aliases.
904 */
905 struct macro *
906 alookup(struct macro *dict, const char *str)
907 {
908 int res;
909 if (!dict) return (dict);
910
911 #ifdef DEBUG
912 ASSERT(dict->type == MACRO);
913 #endif
914 res = strcmp(dict->name, str);
915 if (!res) return (dict);
916 if (res < 0)
917 return (alookup(dict->l, str));
918 else
919 return (alookup(dict->r, str));
920
921 }
922
923 /*
924 * Bootstrap the dictionary and then install
925 * all the standard FCodes.
926 */
927 void
928 initdic(void)
929 {
930 struct fcode *code = fcodes;
931 struct macro *alias = macros;
932
933 ASSERT(dictionary == NULL);
934 code->l = code->r = NULL;
935 dictionary = code;
936 #ifdef DEBUG
937 code->type = FCODE;
938 #endif
939
940 while ((++code)->name) {
941 if(!fadd(dictionary, code)) {
942 printf("init: duplicate dictionary entry %s\n",
943 code->name);
944 abort();
945 }
946 }
947
948 ASSERT(aliases == NULL);
949 aliases = alias;
950 alias->l = alias->r = NULL;
951 #ifdef DEBUG
952 alias->type = MACRO;
953 #endif
954 while ((++alias)->name) {
955 if(!aadd(aliases, alias)) {
956 printf("init: duplicate macro entry %s\n",
957 alias->name);
958 abort();
959 }
960 }
961
962 }
963
964 int
965 apply_macros(YY_BUFFER_STATE yinput, const char *str)
966 {
967 struct macro *xform = alookup(aliases, str);
968
969 if (xform) {
970 YY_BUFFER_STATE newbuf;
971
972 newbuf = yy_scan_string(xform->equiv);
973 yy_switch_to_buffer(newbuf);
974 tokenize(newbuf);
975 yy_switch_to_buffer(yinput);
976 yy_delete_buffer(newbuf);
977 }
978 return (xform != NULL);
979 }
980
981 void
982 usage(const char *me)
983 {
984 (void)fprintf(stderr, "%s: [-d level] [-o outfile] infile\n", me);
985 exit(1);
986 }
987
988 int
989 main(int argc, char *argv[])
990 {
991 int bflag, ch;
992 FILE *inf;
993 struct fcode_header *fheader;
994 YY_BUFFER_STATE inbuf;
995 const char *hdrtype = "version1";
996 int i;
997
998 outf = 1; /* stdout */
999 myname = argv[0];
1000
1001 bflag = 0;
1002 while ((ch = getopt(argc, argv, "d:o:")) != -1)
1003 switch(ch) {
1004 case 'd':
1005 debug = atol(optarg);
1006 break;
1007 case 'o':
1008 outfile = optarg;
1009 break;
1010 default:
1011 usage(myname);
1012 }
1013 argc -= optind;
1014 argv += optind;
1015
1016 if (argc != 1)
1017 usage(myname);
1018
1019 infile = argv[0];
1020
1021 /*
1022 * Initialization stuff.
1023 */
1024 initdic();
1025 outbufsiz = BUFCLICK;
1026 outbuf = malloc(outbufsiz);
1027 fheader = (struct fcode_header *)outbuf;
1028 outpos = 0;
1029 emit(hdrtype);
1030 outpos = sizeof(*fheader);
1031
1032 /*
1033 * Do it.
1034 */
1035 if ((inf = fopen(infile, "r")) == NULL)
1036 (void)err(1, "can not open %s for reading", infile);
1037
1038 inbuf = yy_create_buffer( inf, YY_BUF_SIZE );
1039 yy_switch_to_buffer(inbuf);
1040 tokenize(inbuf);
1041 yy_delete_buffer(inbuf);
1042 fclose(inf);
1043 emit("end0");
1044
1045 /* Now calculate length and checksum and stick them in the header */
1046 fheader->format = 0x08;
1047 fheader->length = htonl(outpos);
1048 fheader->checksum = 0;
1049 for (i = sizeof(*fheader); i<outpos; i++)
1050 fheader->checksum += outbuf[i];
1051 fheader->checksum = htons(fheader->checksum);
1052
1053 if ((outf = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
1054 err(1, "can out open %s for writing", outfile);
1055
1056 if (write(outf, outbuf, outpos) != outpos) {
1057 close(outf);
1058 unlink(outfile);
1059 err(1, "write error");
1060 }
1061 close(outf);
1062 return (0);
1063 };
1064
1065 /*
1066 * Tokenize one file. This is a separate function so it can
1067 * be called recursively to parse mutiple levels of include files.
1068 */
1069
1070 void
1071 tokenize(YY_BUFFER_STATE yinput)
1072 {
1073 FILE *inf;
1074 YY_BUFFER_STATE inbuf;
1075 TOKEN *token;
1076 const char *last_token = "";
1077 struct fcode *fcode;
1078 int pos, off;
1079
1080 while ((token = yylex()) != NULL) {
1081 switch (token->type) {
1082 case TOK_NUMBER:
1083 STATE(token->text, "TOK_NUMBER");
1084 {
1085 char *end;
1086 Cell value;
1087
1088 if (tokenizer) {
1089 push(strtol(token->text, &end, 16));
1090 break;
1091 }
1092 value = strtol(token->text, &end, numbase);
1093 if (*end != 0)
1094 token_err(yylineno, infile, yytext,
1095 "illegal number conversion");
1096
1097 /*
1098 * If this is a 64-bit value we need to store two literals
1099 * and issue a `lxjoin' to combine them. But that's a future
1100 * project.
1101 */
1102 emit("b(lit)");
1103 spit((value>>24)&0x0ff);
1104 spit((value>>16)&0x0ff);
1105 spit((value>>8)&0x0ff);
1106 spit(value&0x0ff);
1107 if ((value>>32) != value && (value>>32) != 0 &&
1108 (value>>32) != -1) {
1109 emit("b(lit)");
1110 spit((value>>56)&0x0ff);
1111 spit((value>>48)&0x0ff);
1112 spit((value>>40)&0x0ff);
1113 spit((value>>32)&0x0ff);
1114 emit("lxjoin");
1115 }
1116 }
1117 break;
1118 case TOK_C_LIT:
1119 STATE(token->text, "TOK_C_LIT");
1120 emit("b(lit)");
1121 spit(0);
1122 spit(0);
1123 spit(0);
1124 spit(token->text[1]);
1125 break;
1126 case TOK_STRING_LIT:
1127 STATE(token->text, "TOK_STRING_LIT:");
1128 {
1129 int len;
1130 char *p = token->text;
1131
1132 ++p; /* Skip the quote */
1133 len = strlen(++p); /* Skip the 1st space */
1134
1135 #define ERR_TOOLONG \
1136 token_err(yylineno, infile, yytext, "string length %d too long", len)
1137
1138 if (len > 255)
1139 ERR_TOOLONG;
1140
1141 if (p[len-1] == ')' ||
1142 p[len-1] == '"') {
1143 p[len-1] = 0;
1144 }
1145 emit("b(\")");
1146 sspit(p);
1147 }
1148 break;
1149 case TOK_PSTRING:
1150 STATE(token->text, "TOK_PSTRING:");
1151 {
1152 int len;
1153 char *p = token->text;
1154
1155 if (*p++ == '.') p++; /* Skip over delimiter */
1156 p++; /* Skip over space/tab */
1157
1158 len = strlen(p);
1159 if (len > 255)
1160 ERR_TOOLONG;
1161
1162 if (p[len-1] == ')' ||
1163 p[len-1] == '"') {
1164 p[len-1] = 0;
1165 }
1166 emit("b(\")");
1167 sspit(p);
1168 emit("type");
1169 }
1170 break;
1171 case TOK_TOKENIZE:
1172 STATE(token->text, "TOK_TOKENIZE");
1173 /* The next pass should tokenize the FCODE number */
1174 emit("b(')");
1175 break;
1176 case TOK_COMMENT:
1177 STATE(token->text, "TOK_COMMENT:");
1178 while (((token = yylex()) != NULL) && token->type != TOK_ENDCOMMENT)
1179 ;
1180 break;
1181 case TOK_ENDCOMMENT:
1182 STATE(token->text, "TOK_ENDCOMMENT");
1183 token_err(yylineno, infile, NULL,
1184 "ENDCOMMENT encountered outside comment");
1185 break;
1186 case TOK_COLON:
1187 STATE(token->text, "TOK_COLON:");
1188
1189 token = yylex();
1190 if (token == NULL)
1191 token_err(yylineno, infile, yytext,
1192 "EOF in colon definition");
1193
1194 /* Add new code to dictionary */
1195 fcode = malloc(sizeof(*fcode));
1196 fcode->num = nextfcode++;
1197 fcode->name = strdup(token->text);
1198 if (!fadd(dictionary, fcode))
1199 token_err(yylineno, infile, NULL,
1200 "Duplicate definition: `%s'\n", fcode->name);
1201 #ifdef DEBUG
1202 if (debug)
1203 (void)printf("Adding %s to dictionary\n", token->text);
1204 #endif
1205 if (state == 0)
1206 emit("new-token");
1207 else {
1208 if (state == TOK_EXTERNAL)
1209 emit("external-token");
1210 else
1211 /* Here we have a choice of new-token or named-token */
1212 emit("named-token");
1213 sspit(token->text);
1214 }
1215 spit(fcode->num);
1216 emit("b(:)");
1217 last_token = fcode->name;
1218 defining = 1;
1219 break;
1220 case TOK_SEMICOLON:
1221 STATE(token->text, "TOK_SEMICOLON:");
1222 emit("b(;)");
1223 defining = 0;
1224 if (depth()) {
1225 token_err(yylineno, infile, NULL,
1226 "Warning: stack depth %d at end of %s\n",
1227 depth(), last_token);
1228 }
1229 last_token = "";
1230 break;
1231
1232 /* These are special */
1233 case TOK_AGAIN:
1234 STATE(token->text, "TOK_AGAIN");
1235 emit("bbranch");
1236 pos = pop();
1237 pos -= outpos;
1238 if (offsetsize == 16) {
1239 spit((pos>>8)&0xff);
1240 }
1241 spit(pos&0xff);
1242 break;
1243 case TOK_ALIAS:
1244 STATE(token->text, "TOK_ALIAS");
1245 {
1246 struct macro *alias;
1247
1248 token = yylex();
1249 if (token == NULL) {
1250 (void)printf( "EOF in alias definition\n");
1251 return;
1252 }
1253 if (token->type != TOK_OTHER) {
1254 (void)printf( "ENDCOMMENT aliasing weird token type %d\n",
1255 token->type);
1256 }
1257 alias = malloc(sizeof(*alias));
1258 alias->name = strdup(token->text);
1259 token = yylex();
1260 if (token == NULL) {
1261 free(__UNCONST(alias->name));
1262 free(alias);
1263 (void)printf( "EOF in alias definition\n");
1264 return;
1265 }
1266 alias->equiv = strdup(token->text);
1267 if (!aadd(aliases, alias)) {
1268 (void)printf( "ERROR: Duplicate alias %s\n",
1269 alias->name);
1270 exit(1);
1271 }
1272 }
1273 break;
1274 case TOK_GETTOKEN:
1275 STATE(token->text, "TOK_GETTOKEN");
1276 /* This is caused by ['] */
1277 emit("b(')");
1278 token = yylex();
1279 if (token == NULL) {
1280 (void)printf( "EOF in [']\n");
1281 return;
1282 }
1283 if ((fcode = flookup(dictionary, token->text)) == NULL) {
1284 (void)printf( "[']: %s not found\n", token->text);
1285 exit(1);
1286 }
1287 spit(fcode->num);
1288 break;
1289 case TOK_ASCII:
1290 STATE(token->text, "TOK_ASCII");
1291 token = yylex();
1292 if (token == NULL) {
1293 (void)printf( "EOF after \"ascii\"\n");
1294 exit(1);
1295 }
1296 emit("b(lit)");
1297 spit(0);
1298 spit(0);
1299 spit(0);
1300 spit(token->text[0]);
1301 break;
1302 case TOK_BEGIN:
1303 STATE(token->text, "TOK_BEGIN");
1304 emit("b(<mark)");
1305 push(outpos);
1306 break;
1307 case TOK_BUFFER:
1308 STATE(token->text, "TOK_BUFFER");
1309
1310 token = yylex();
1311 if (token == NULL) {
1312 (void)printf( "EOF in colon definition\n");
1313 return;
1314 }
1315
1316 /* Add new code to dictionary */
1317 fcode = malloc(sizeof(*fcode));
1318 fcode->num = nextfcode++;
1319 fcode->name = strdup(token->text);
1320 fadd(dictionary, fcode);
1321
1322 if (state == 0)
1323 emit("new-token");
1324 else {
1325 if (state == TOK_EXTERNAL)
1326 emit("external-token");
1327 else
1328 /* Here we have a choice of new-token or named-token */
1329 emit("named-token");
1330 sspit(token->text);
1331 }
1332 spit(fcode->num);
1333 emit("b(buffer:)");
1334 break;
1335 case TOK_CASE:
1336 STATE(token->text, "TOK_CASE");
1337 emit("b(case)");
1338 push(0);
1339 break;
1340 case TOK_CONSTANT:
1341 STATE(token->text, "TOK_CONSTANT");
1342
1343 token = yylex();
1344 if (token == NULL) {
1345 (void)printf( "EOF in constant definition\n");
1346 return;
1347 }
1348
1349 /* Add new code to dictionary */
1350 fcode = malloc(sizeof(*fcode));
1351 fcode->num = nextfcode++;
1352 fcode->name = strdup(token->text);
1353 fadd(dictionary, fcode);
1354
1355 if (state == 0)
1356 emit("new-token");
1357 else {
1358 if (state == TOK_EXTERNAL)
1359 emit("external-token");
1360 else
1361 /* Here we have a choice of new-token or named-token */
1362 emit("named-token");
1363 sspit(token->text);
1364 }
1365 spit(fcode->num);
1366 emit("b(constant)");
1367 break;
1368 case TOK_CONTROL:
1369 STATE(token->text, "TOK_CONTROL");
1370 token = yylex();
1371 if (token == NULL) {
1372 (void)printf( "EOF after \"ascii\"\n");
1373 exit(1);
1374 }
1375 emit("b(lit)");
1376 spit(0);
1377 spit(0);
1378 spit(0);
1379 spit(token->text[0]&0x1f);
1380 break;
1381 case TOK_CREATE:
1382 STATE(token->text, "TOK_CREATE");
1383 /* Don't know what this does or if it's right */
1384 token = yylex();
1385 if (token == NULL) {
1386 (void)printf( "EOF in create definition\n");
1387 return;
1388 }
1389
1390 /* Add new code to dictionary */
1391 fcode = malloc(sizeof(*fcode));
1392 fcode->num = nextfcode++;
1393 fcode->name = strdup(token->text);
1394 fadd(dictionary, fcode);
1395
1396 if (state == 0)
1397 emit("new-token");
1398 else {
1399 if (state == TOK_EXTERNAL)
1400 emit("external-token");
1401 else
1402 /* Here we have a choice of new-token or named-token */
1403 emit("named-token");
1404 sspit(token->text);
1405 }
1406 spit(fcode->num);
1407 emit("b(create)");
1408 break;
1409 case TOK_DECIMAL:
1410 STATE(token->text, "TOK_DECIMAL");
1411 if (token->text[1] != '#') {
1412 if (defining) {
1413 spit(10);
1414 emit("base");
1415 emit("!");
1416 } else
1417 numbase = TOK_DECIMAL;
1418 } else {
1419 char *end;
1420 Cell value;
1421
1422 token = yylex();
1423 if (token == NULL) {
1424 (void)printf( "EOF after d#\n");
1425 return;
1426 }
1427 if (token->type == TOK_OTHER) {
1428 if (strcmp("-1", token->text) == 0) {
1429 emit(token->text);
1430 break;
1431 }
1432 }
1433 value = strtol(token->text, &end, 10);
1434 if (*end != 0)
1435 token_err(yylineno, infile, NULL,
1436 "Illegal number conversion: %s", token->text);
1437
1438 /*
1439 * If this is a 64-bit value we need to store two literals
1440 * and issue a `lxjoin' to combine them. But that's a future
1441 * project.
1442 */
1443 emit("b(lit)");
1444 spit((value>>24)&0x0ff);
1445 spit((value>>16)&0x0ff);
1446 spit((value>>8)&0x0ff);
1447 spit(value&0x0ff);
1448 if ((value>>32) != value && (value>>32) != 0) {
1449 emit("b(lit)");
1450 spit((value>>56)&0x0ff);
1451 spit((value>>48)&0x0ff);
1452 spit((value>>40)&0x0ff);
1453 spit((value>>32)&0x0ff);
1454 emit("lxjoin");
1455 }
1456 }
1457 break;
1458 case TOK_DEFER:
1459 STATE(token->text, "TOK_DEFER");
1460 /* Don't know what this does or if it's right */
1461 token = yylex();
1462 if (token == NULL) {
1463 (void)printf( "EOF in colon definition\n");
1464 return;
1465 }
1466
1467 /* Add new code to dictionary */
1468 fcode = malloc(sizeof(*fcode));
1469 fcode->num = nextfcode++;
1470 fcode->name = strdup(token->text);
1471 fadd(dictionary, fcode);
1472
1473 if (state == 0)
1474 emit("new-token");
1475 else {
1476 if (state == TOK_EXTERNAL)
1477 emit("external-token");
1478 else
1479 /* Here we have a choice of new-token or named-token */
1480 emit("named-token");
1481 sspit(token->text);
1482 }
1483 spit(fcode->num);
1484 emit("b(defer)");
1485 break;
1486 case TOK_DO:
1487 STATE(token->text, "TOK_DO");
1488 /*
1489 * From the 1275 spec. B is branch location, T is branch target.
1490 *
1491 * b(do) offset1 ... b(loop) offset2 ...
1492 * b(do) offset1 ... b(+loop) offset2 ...
1493 * b(?do) offset1 ... b(loop) offset2 ...
1494 * b(?do) offset1 ... b(+loop) offset2 ...
1495 * ^ ^
1496 * B1 ^ ^ T1
1497 * T2 B2
1498 *
1499 * How we do this is we generate the b(do) or b(?do), spit out a
1500 * zero offset while remembering b1 and t2. Then we call tokenize()
1501 * to generate the body. When tokenize() finds a b(loop) or b(+loop),
1502 * it generates the FCode and returns, with outpos at b2. We then
1503 * calculate the offsets, put them in the right slots and finishup.
1504 */
1505
1506 if (token->text[0] == '?')
1507 emit("b(?do)");
1508 else
1509 emit("b(do)");
1510 push(outpos);
1511 if (offsetsize == 16) {
1512 spit(0);
1513 }
1514 spit(0); /* Place holder for later */
1515 push(outpos);
1516 break;
1517 case TOK_ELSE:
1518 STATE(token->text, "TOK_ELSE");
1519 /* Get where we need to patch */
1520 off = pop();
1521 emit("bbranch");
1522 /* Save where we are now. */
1523 push(outpos);
1524 if (offsetsize == 16) {
1525 spit(0); /* Place holder for later */
1526 }
1527 spit(0); /* Place holder for later */
1528 emit("b(>resolve)");
1529 /* Rewind and patch the if branch */
1530 pos = outpos;
1531 outpos = off;
1532 off = pos - off;
1533 if (offsetsize == 16) {
1534 spit(0); /* Place holder for later */
1535 }
1536 spit(0); /* Place holder for later */
1537 /* revert to the end */
1538 outpos = pos;
1539 break;
1540 case TOK_ENDCASE:
1541 STATE(token->text, "TOK_ENDCASE:");
1542 pos = outpos; /* Remember where we need to branch to */
1543
1544 /* Thread our way backwards and install proper offsets */
1545 off = pop();
1546 while (off) {
1547 int tmp;
1548
1549 /* Move to this offset */
1550 outpos = off;
1551 /* Load next offset to process */
1552 tmp = outbuf[outpos];
1553
1554 /* process this offset */
1555 off = pos - outpos;
1556 if (offsetsize == 16) {
1557 spit((off>>8)&0xff);
1558 }
1559 spit(off&0xff);
1560 off = tmp;
1561 }
1562 outpos = pos;
1563 emit("b(endcase)");
1564 break;
1565 case TOK_ENDOF:
1566 STATE(token->text, "TOK_ENDOF");
1567 off = pop();
1568 emit("b(endof)");
1569 /*
1570 * Save back pointer in the offset field so we can traverse
1571 * the linked list and patch it in the endcase.
1572 */
1573 pos = pop(); /* get position of prev link. */
1574 push(outpos); /* save position of this link. */
1575 spit(pos); /* save potision of prev link. */
1576 if (offsetsize == 16) {
1577 spit(0);
1578 }
1579 pos = outpos;
1580 /* Now point the offset from b(of) here. */
1581 outpos = off;
1582 off = outpos - off;
1583 if (offsetsize == 16) {
1584 spit((off>>8)&0xff);
1585 }
1586 spit(off&0xff);
1587 /* Restore position */
1588 outpos = pos;
1589 break;
1590 case TOK_EXTERNAL:
1591 STATE(token->text, "TOK_EXTERNAL");
1592 state = TOK_EXTERNAL;
1593 break;
1594 case TOK_FIELD:
1595 STATE(token->text, "TOK_FIELD");
1596
1597 token = yylex();
1598 if (token == NULL) {
1599 (void)printf( "EOF in field definition\n");
1600 return;
1601 }
1602
1603 /* Add new code to dictionary */
1604 fcode = malloc(sizeof(*fcode));
1605 fcode->num = nextfcode++;
1606 fcode->name = strdup(token->text);
1607 fadd(dictionary, fcode);
1608
1609 if (state == 0)
1610 emit("new-token");
1611 else {
1612 if (state == TOK_EXTERNAL)
1613 emit("external-token");
1614 else
1615 /* Here we have a choice of new-token or named-token */
1616 emit("named-token");
1617 sspit(token->text);
1618 }
1619 spit(fcode->num);
1620 emit("b(field)");
1621 break;
1622
1623 case TOK_HEX:
1624 STATE(token->text, "TOK_HEX");
1625 if (token->text[1] != '#') {
1626 if (defining) {
1627 spit(16);
1628 emit("base");
1629 emit("!");
1630 } else
1631 numbase = TOK_HEX;
1632 } else {
1633 char *end;
1634 Cell value;
1635
1636 token = yylex();
1637 if (token == NULL) {
1638 (void)printf( "EOF after h#\n");
1639 return;
1640 }
1641 value = strtol(token->text, &end, 16);
1642 if (*end != 0) {
1643 (void)printf("Illegal number conversion:%s:%d: %s\n",
1644 infile, yylineno, yytext);
1645 exit(1);
1646 }
1647 /*
1648 * If this is a 64-bit value we need to store two literals
1649 * and issue a `lxjoin' to combine them. But that's a future
1650 * project.
1651 */
1652 emit("b(lit)");
1653 spit((value>>24)&0x0ff);
1654 spit((value>>16)&0x0ff);
1655 spit((value>>8)&0x0ff);
1656 spit(value&0x0ff);
1657 if ((value>>32) != value && (value>>32) != 0) {
1658 emit("b(lit)");
1659 spit((value>>56)&0x0ff);
1660 spit((value>>48)&0x0ff);
1661 spit((value>>40)&0x0ff);
1662 spit((value>>32)&0x0ff);
1663 emit("lxjoin");
1664 }
1665 }
1666 break;
1667 case TOK_HEADERLESS:
1668 STATE(token->text, "TOK_HEADERLESS");
1669 state = 0;
1670 break;
1671 case TOK_HEADERS:
1672 STATE(token->text, "TOK_HEADERS");
1673 state = TOK_HEADERS;
1674 break;
1675 case TOK_OFFSET16:
1676 STATE(token->text, "TOK_OFFSET16");
1677 offsetsize = 16;
1678 emit("offset16");
1679 break;
1680 case TOK_IF:
1681 STATE(token->text, "TOK_IF");
1682 /*
1683 * Similar to do but simpler since we only deal w/one branch.
1684 */
1685 emit("b?branch");
1686 push(outpos);
1687 if (offsetsize == 16) {
1688 spit(0); /* Place holder for later */
1689 }
1690 spit(0); /* Place holder for later */
1691 break;
1692 case TOK_LEAVE:
1693 STATE(token->text, "TOK_LEAVE");
1694 emit("b(leave)");
1695 break;
1696 case TOK_LOOP:
1697 STATE(token->text, "TOK_LOOP");
1698
1699 if (token->text[0] == '+')
1700 emit("b(+loop)");
1701 else
1702 emit("b(loop)");
1703 /* First do backwards branch of loop */
1704 pos = pop();
1705 off = pos - outpos;
1706 if (offsetsize == 16) {
1707 spit((off>>8)&0xff);
1708 }
1709 spit(off&0xff);
1710 /* Now do forward branch of do */
1711 pos = outpos;
1712 outpos = pop();
1713 off = pos - outpos;
1714 if (offsetsize == 16) {
1715 spit((off>>8)&0xff);
1716 }
1717 spit(off&0xff);
1718 /* Restore output position */
1719 outpos = pos;
1720 break;
1721 case TOK_OCTAL:
1722 STATE(token->text, "TOK_OCTAL");
1723 if (token->text[1] != '#') {
1724 if (defining) {
1725 spit(16);
1726 emit("base");
1727 emit("!");
1728 } else
1729 numbase = TOK_OCTAL;
1730 } else {
1731 char *end;
1732 Cell value;
1733
1734 token = yylex();
1735 if (token == NULL) {
1736 (void)printf( "EOF after o#\n");
1737 return;
1738 }
1739 value = strtol(token->text, &end, 8);
1740 if (*end != 0) {
1741 (void)printf("Illegal number conversion:%s:%d: %s\n",
1742 infile, yylineno, yytext);
1743 exit(1);
1744 }
1745 /*
1746 * If this is a 64-bit value we need to store two literals
1747 * and issue a `lxjoin' to combine them. But that's a future
1748 * project.
1749 */
1750 emit("b(lit)");
1751 spit((value>>24)&0x0ff);
1752 spit((value>>16)&0x0ff);
1753 spit((value>>8)&0x0ff);
1754 spit(value&0x0ff);
1755 if ((value>>32) != value && (value>>32) != 0) {
1756 emit("b(lit)");
1757 spit((value>>56)&0x0ff);
1758 spit((value>>48)&0x0ff);
1759 spit((value>>40)&0x0ff);
1760 spit((value>>32)&0x0ff);
1761 emit("lxjoin");
1762 }
1763 }
1764 break;
1765 case TOK_OF:
1766 STATE(token->text, "TOK_OF");
1767 /*
1768 * Let's hope I get the semantics right.
1769 *
1770 * The `of' behaves almost the same as an
1771 * `if'. The difference is that `endof'
1772 * takes a branch offset to the associated
1773 * `endcase'. Here we will generate a temporary
1774 * offset of the `of' associated with the `endof'.
1775 * Then in `endcase' we should be pointing just
1776 * after the offset of the last `endof' so we
1777 * calculate the offset and thread our way backwards
1778 * searching for the previous `b(case)' or `b(endof)'.
1779 */
1780 emit("b(of)");
1781 push(outpos);
1782 if (offsetsize == 16) {
1783 spit(0);
1784 }
1785 spit(0); /* Place holder for later */
1786 break;
1787 case TOK_REPEAT:
1788 STATE(token->text, "TOK_REPEAT");
1789 emit("bbranch");
1790 pos = pop();
1791 off = pop();
1792 /* First the offset for the branch back to the begin */
1793 off -= outpos;
1794 if (offsetsize == 16) {
1795 spit((off>>8)&0xff);
1796 }
1797 spit(off&0xff);
1798 emit("b(>resolve)");
1799 /* Now point the offset of the while here. */
1800 off = outpos;
1801 outpos = pos;
1802 pos = off - pos;
1803 if (offsetsize == 16) {
1804 spit((pos>>8)&0xff);
1805 }
1806 spit(pos&0xff);
1807 /* Return to the end of the output */
1808 outpos = off;
1809 break;
1810 case TOK_THEN:
1811 STATE(token->text, "TOK_THEN");
1812 emit("b(>resolve)");
1813 pos = outpos;
1814 outpos = pop();
1815 off = pos - outpos;
1816 if (offsetsize == 16) {
1817 spit((off>>8)&0xff);
1818 }
1819 spit(off&0xff);
1820 outpos = pos;
1821 break;
1822 case TOK_TO:
1823 STATE(token->text, "TOK_TO");
1824 /* The next pass should tokenize the FCODE number */
1825 emit("b(to)");
1826 break;
1827 case TOK_UNTIL:
1828 STATE(token->text, "TOK_UNTIL");
1829 emit("b?branch");
1830 pos = pop();
1831 pos -= outpos;
1832 if (offsetsize == 16) {
1833 spit((pos>>8)&0xff);
1834 }
1835 spit(pos&0xff);
1836 break;
1837 case TOK_VALUE:
1838 STATE(token->text, "TOK_VALUE");
1839
1840 token = yylex();
1841 if (token == NULL) {
1842 (void)printf( "EOF in value definition\n");
1843 return;
1844 }
1845
1846 /* Add new code to dictionary */
1847 fcode = malloc(sizeof(*fcode));
1848 fcode->num = nextfcode++;
1849 fcode->name = strdup(token->text);
1850 fadd(dictionary, fcode);
1851
1852 if (state == 0)
1853 emit("new-token");
1854 else {
1855 if (state == TOK_EXTERNAL)
1856 emit("external-token");
1857 else
1858 /* Here we have a choice of new-token or named-token */
1859 emit("named-token");
1860 sspit(token->text);
1861 }
1862 spit(fcode->num);
1863 emit("b(value)");
1864 break;
1865 case TOK_VARIABLE:
1866 STATE(token->text, "TOK_VARIABLE");
1867
1868 token = yylex();
1869 if (token == NULL) {
1870 (void)printf( "EOF in variable definition\n");
1871 return;
1872 }
1873
1874 /* Add new code to dictionary */
1875 fcode = malloc(sizeof(*fcode));
1876 fcode->num = nextfcode++;
1877 fcode->name = strdup(token->text);
1878 fadd(dictionary, fcode);
1879
1880 if (state == 0)
1881 emit("new-token");
1882 else {
1883 if (state == TOK_EXTERNAL)
1884 emit("external-token");
1885 else
1886 /* Here we have a choice of new-token or named-token */
1887 emit("named-token");
1888 sspit(token->text);
1889 }
1890 spit(fcode->num);
1891 emit("b(variable)");
1892 break;
1893 case TOK_WHILE:
1894 STATE(token->text, "TOK_WHILE");
1895 emit("b?branch");
1896 push(outpos);
1897 if (offsetsize == 16) {
1898 spit(0);
1899 }
1900 spit(0);
1901 break;
1902
1903 /* Tokenizer directives */
1904 case TOK_BEGTOK:
1905 STATE(token->text, "TOK_BEGTOK");
1906 tokenizer = 1;
1907 break;
1908 case TOK_EMIT_BYTE:
1909 STATE(token->text, "TOK_EMIT_BYTE");
1910 spit(pop());
1911 break;
1912 case TOK_ENDTOK:
1913 STATE(token->text, "TOK_ENDTOK");
1914 tokenizer = 0;
1915 break;
1916 case TOK_FLOAD:
1917 STATE(token->text, "TOK_FLOAD");
1918 /* Parse a different file for a while */
1919 token = yylex();
1920 if ((inf = fopen(token->text, "r")) == NULL) {
1921 (void)printf("%s: Could not open %s: %s\n",
1922 myname, token->text, strerror(errno));
1923 break;
1924 }
1925 inbuf = yy_create_buffer(inf, YY_BUF_SIZE);
1926 yy_switch_to_buffer(inbuf);
1927 {
1928 char *oldinfile = infile;
1929
1930 infile = token->text;
1931 tokenize(inbuf);
1932 infile = oldinfile;
1933 }
1934 yy_switch_to_buffer(yinput);
1935 yy_delete_buffer(inbuf);
1936 fclose(inf);
1937 break;
1938 case TOK_OTHER:
1939 STATE(token->text, "TOK_OTHER");
1940 if (apply_macros(yinput, token->text))
1941 break;
1942 if (emit(token->text)) {
1943 #if 0
1944 /*
1945 * Call an external command
1946 *
1947 * XXXXX assumes it will always find the command
1948 */
1949 sspit(token->text);
1950 emit("$find");
1951 emit("drop");
1952 emit("execute");
1953 #else
1954 (void)printf( "%s: undefined token `%s'\n",
1955 myname, token->text);
1956 fflush(stderr);
1957 exit(1);
1958 #endif
1959 }
1960 break;
1961 default:
1962 /* Nothing */ ;
1963 }
1964 }
1965 return;
1966 }
1967
1968 /*
1969 * print a tokenizer error message
1970 */
1971 void
1972 token_err(int lineno, const char *file, const char *text, const char *fmt, ...)
1973 {
1974 va_list ap;
1975
1976 va_start(ap, fmt);
1977 if (file)
1978 (void)fprintf(stderr, "%s:%d: ", file, lineno);
1979 if (fmt)
1980 (void)vfprintf(stderr, fmt, ap);
1981 fputc('\n', stderr);
1982 if (text)
1983 fprintf(stderr, "\t%s", text);
1984 va_end(ap);
1985 exit(1);
1986 }
1987
1988 /*
1989 * Lookup fcode string in dictionary and spit it out.
1990 *
1991 * Fcode must be in dictionary. No alias conversion done.
1992 */
1993 int
1994 emit(const char *str)
1995 {
1996 struct fcode *code;
1997 if ((code = flookup( dictionary, str)))
1998 spit(code->num);
1999 #ifdef DEBUG
2000 if (debug > 1) {
2001 if (code)
2002 (void)printf( "emitting `%s'\n", code->name);
2003 else
2004 (void)printf( "emit: not found `%s'\n", str);
2005 }
2006 #endif
2007 return (code == NULL);
2008 }
2009
2010 /*
2011 * Spit out an integral value as a series of FCodes.
2012 *
2013 * It will spit out one zero byte or as many bytes as are
2014 * non-zero.
2015 */
2016 int
2017 spit(long n)
2018 {
2019 int count = 1;
2020
2021 if (n >> 8)
2022 count += spit(n >> 8);
2023 if ((size_t)outpos >= outbufsiz) {
2024 while ((size_t)outpos >= outbufsiz) outbufsiz += BUFCLICK;
2025 if (!(outbuf = realloc(outbuf, outbufsiz))) {
2026 (void)printf( "realloc of %ld bytes failed -- out of memory\n",
2027 (long)outbufsiz);
2028 exit(1);
2029 }
2030 }
2031 if (debug > 1) printf("spitting %2.2x\n", (unsigned char)n);
2032 outbuf[outpos++] = n;
2033 return (count);
2034 }
2035
2036 /*
2037 * Spit out an FCode string.
2038 */
2039 void
2040 sspit(const char *s)
2041 {
2042 int len = strlen(s);
2043
2044 if (len > 255) {
2045 (void)printf( "string length %d too long\n", len);
2046 return;
2047 }
2048 #ifdef DEBUG
2049 if (debug > 1)
2050 (void)printf( "sspit: len %d str `%s'\n", len, s);
2051 #endif
2052 spit(len);
2053 while (*s)
2054 spit(*s++);
2055 }
2056
2057 int
2058 yywrap(void)
2059 {
2060 /* Always generate EOF */
2061 return (1);
2062 }
2063