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