fgen.l revision 1.16 1 %{
2 /* $NetBSD: fgen.l,v 1.16 2001/12/31 18:31:59 thorpej 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 Cell 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 emit("b(lit)");
1458 spit((value>>56)&0x0ff);
1459 spit((value>>48)&0x0ff);
1460 spit((value>>40)&0x0ff);
1461 spit((value>>32)&0x0ff);
1462 emit("lxjoin");
1463 }
1464 }
1465 break;
1466 case TOK_DEFER:
1467 STATE(token->text, "TOK_DEFER");
1468 /* Don't know what this does or if it's right */
1469 token = yylex();
1470 if (token == NULL) {
1471 (void)printf( "EOF in colon definition\n");
1472 return;
1473 }
1474
1475 /* Add new code to dictionary */
1476 fcode = malloc(sizeof(*fcode));
1477 fcode->num = nextfcode++;
1478 fcode->name = strdup(token->text);
1479 fadd(dictionary, fcode);
1480
1481 if (state == 0)
1482 emit("new-token");
1483 else {
1484 if (state == TOK_EXTERNAL)
1485 emit("external-token");
1486 else
1487 /* Here we have a choice of new-token or named-token */
1488 emit("named-token");
1489 sspit(token->text);
1490 }
1491 spit(fcode->num);
1492 emit("b(defer)");
1493 break;
1494 case TOK_DO:
1495 STATE(token->text, "TOK_DO");
1496 /*
1497 * From the 1275 spec. B is branch location, T is branch target.
1498 *
1499 * b(do) offset1 ... b(loop) offset2 ...
1500 * b(do) offset1 ... b(+loop) offset2 ...
1501 * b(?do) offset1 ... b(loop) offset2 ...
1502 * b(?do) offset1 ... b(+loop) offset2 ...
1503 * ^ ^
1504 * B1 ^ ^ T1
1505 * T2 B2
1506 *
1507 * How we do this is we generate the b(do) or b(?do), spit out a
1508 * zero offset while remembering b1 and t2. Then we call tokenize()
1509 * to generate the body. When tokenize() finds a b(loop) or b(+loop),
1510 * it generates the FCode and returns, with outpos at b2. We then
1511 * calculate the offsets, put them in the right slots and finishup.
1512 */
1513
1514 if (token->text[0] == '?')
1515 emit("b(?do)");
1516 else
1517 emit("b(do)");
1518 push(outpos);
1519 if (offsetsize == 16) {
1520 spit(0);
1521 }
1522 spit(0); /* Place holder for later */
1523 push(outpos);
1524 break;
1525 case TOK_ELSE:
1526 STATE(token->text, "TOK_ELSE");
1527 /* Get where we need to patch */
1528 off = pop();
1529 emit("bbranch");
1530 /* Save where we are now. */
1531 push(outpos);
1532 if (offsetsize == 16) {
1533 spit(0); /* Place holder for later */
1534 }
1535 spit(0); /* Place holder for later */
1536 emit("b(>resolve)");
1537 /* Rewind and patch the if branch */
1538 pos = outpos;
1539 outpos = off;
1540 off = pos - off;
1541 if (offsetsize == 16) {
1542 spit(0); /* Place holder for later */
1543 }
1544 spit(0); /* Place holder for later */
1545 /* revert to the end */
1546 outpos = pos;
1547 break;
1548 case TOK_ENDCASE:
1549 STATE(token->text, "TOK_ENDCASE:");
1550 pos = outpos; /* Remember where we need to branch to */
1551
1552 /* Thread our way backwards and install proper offsets */
1553 off = pop();
1554 while (off) {
1555 int tmp;
1556
1557 /* Move to this offset */
1558 outpos = off;
1559 /* Load next offset to process */
1560 tmp = outbuf[outpos];
1561
1562 /* process this offset */
1563 off = pos - outpos;
1564 if (offsetsize == 16) {
1565 spit((off>>8)&0xff);
1566 }
1567 spit(off&0xff);
1568 off = tmp;
1569 }
1570 outpos = pos;
1571 emit("b(endcase)");
1572 break;
1573 case TOK_ENDOF:
1574 STATE(token->text, "TOK_ENDOF");
1575 off = pop();
1576 emit("b(endof)");
1577 /*
1578 * Save back pointer in the offset field so we can traverse
1579 * the linked list and patch it in the endcase.
1580 */
1581 pos = pop(); /* get position of prev link. */
1582 push(outpos); /* save position of this link. */
1583 spit(pos); /* save potision of prev link. */
1584 if (offsetsize == 16) {
1585 spit(0);
1586 }
1587 pos = outpos;
1588 /* Now point the offset from b(of) here. */
1589 outpos = off;
1590 off = outpos - off;
1591 if (offsetsize == 16) {
1592 spit((off>>8)&0xff);
1593 }
1594 spit(off&0xff);
1595 /* Restore position */
1596 outpos = pos;
1597 break;
1598 case TOK_EXTERNAL:
1599 STATE(token->text, "TOK_EXTERNAL");
1600 state = TOK_EXTERNAL;
1601 break;
1602 case TOK_FIELD:
1603 STATE(token->text, "TOK_FIELD");
1604
1605 token = yylex();
1606 if (token == NULL) {
1607 (void)printf( "EOF in field definition\n");
1608 return;
1609 }
1610
1611 /* Add new code to dictionary */
1612 fcode = malloc(sizeof(*fcode));
1613 fcode->num = nextfcode++;
1614 fcode->name = strdup(token->text);
1615 fadd(dictionary, fcode);
1616
1617 if (state == 0)
1618 emit("new-token");
1619 else {
1620 if (state == TOK_EXTERNAL)
1621 emit("external-token");
1622 else
1623 /* Here we have a choice of new-token or named-token */
1624 emit("named-token");
1625 sspit(token->text);
1626 }
1627 spit(fcode->num);
1628 emit("b(field)");
1629 break;
1630
1631 case TOK_HEX:
1632 STATE(token->text, "TOK_HEX");
1633 if (token->text[1] != '#') {
1634 if (defining) {
1635 spit(16);
1636 emit("base");
1637 emit("!");
1638 } else
1639 base = TOK_HEX;
1640 } else {
1641 char *end;
1642 Cell value;
1643
1644 token = yylex();
1645 if (token == NULL) {
1646 (void)printf( "EOF after h#\n");
1647 return;
1648 }
1649 value = strtol(token->text, &end, 16);
1650 if (*end != 0) {
1651 (void)printf("Illegal number conversion:%s:%d: %s\n",
1652 infile, yylineno, yytext);
1653 exit(1);
1654 }
1655 /*
1656 * If this is a 64-bit value we need to store two literals
1657 * and issue a `lxjoin' to combine them. But that's a future
1658 * project.
1659 */
1660 emit("b(lit)");
1661 spit((value>>24)&0x0ff);
1662 spit((value>>16)&0x0ff);
1663 spit((value>>8)&0x0ff);
1664 spit(value&0x0ff);
1665 if ((value>>32) != value && (value>>32) != 0) {
1666 emit("b(lit)");
1667 spit((value>>56)&0x0ff);
1668 spit((value>>48)&0x0ff);
1669 spit((value>>40)&0x0ff);
1670 spit((value>>32)&0x0ff);
1671 emit("lxjoin");
1672 }
1673 }
1674 break;
1675 case TOK_HEADERLESS:
1676 STATE(token->text, "TOK_HEADERLESS");
1677 state = 0;
1678 break;
1679 case TOK_HEADERS:
1680 STATE(token->text, "TOK_HEADERS");
1681 state = TOK_HEADERS;
1682 break;
1683 case TOK_OFFSET16:
1684 STATE(token->text, "TOK_OFFSET16");
1685 offsetsize = 16;
1686 emit("offset16");
1687 break;
1688 case TOK_IF:
1689 STATE(token->text, "TOK_IF");
1690 /*
1691 * Similar to do but simpler since we only deal w/one branch.
1692 */
1693 emit("b?branch");
1694 push(outpos);
1695 if (offsetsize == 16) {
1696 spit(0); /* Place holder for later */
1697 }
1698 spit(0); /* Place holder for later */
1699 break;
1700 case TOK_LEAVE:
1701 STATE(token->text, "TOK_LEAVE");
1702 emit("b(leave)");
1703 break;
1704 case TOK_LOOP:
1705 STATE(token->text, "TOK_LOOP");
1706
1707 if (token->text[0] == '+')
1708 emit("b(+loop)");
1709 else
1710 emit("b(loop)");
1711 /* First do backwards branch of loop */
1712 pos = pop();
1713 off = pos - outpos;
1714 if (offsetsize == 16) {
1715 spit((off>>8)&0xff);
1716 }
1717 spit(off&0xff);
1718 /* Now do forward branch of do */
1719 pos = outpos;
1720 outpos = pop();
1721 off = pos - outpos;
1722 if (offsetsize == 16) {
1723 spit((off>>8)&0xff);
1724 }
1725 spit(off&0xff);
1726 /* Restore output position */
1727 outpos = pos;
1728 break;
1729 case TOK_OCTAL:
1730 STATE(token->text, "TOK_OCTAL");
1731 if (token->text[1] != '#') {
1732 if (defining) {
1733 spit(16);
1734 emit("base");
1735 emit("!");
1736 } else
1737 base = TOK_OCTAL;
1738 } else {
1739 char *end;
1740 Cell value;
1741
1742 token = yylex();
1743 if (token == NULL) {
1744 (void)printf( "EOF after o#\n");
1745 return;
1746 }
1747 value = strtol(token->text, &end, 8);
1748 if (*end != 0) {
1749 (void)printf("Illegal number conversion:%s:%d: %s\n",
1750 infile, yylineno, yytext);
1751 exit(1);
1752 }
1753 /*
1754 * If this is a 64-bit value we need to store two literals
1755 * and issue a `lxjoin' to combine them. But that's a future
1756 * project.
1757 */
1758 emit("b(lit)");
1759 spit((value>>24)&0x0ff);
1760 spit((value>>16)&0x0ff);
1761 spit((value>>8)&0x0ff);
1762 spit(value&0x0ff);
1763 if ((value>>32) != value && (value>>32) != 0) {
1764 emit("b(lit)");
1765 spit((value>>56)&0x0ff);
1766 spit((value>>48)&0x0ff);
1767 spit((value>>40)&0x0ff);
1768 spit((value>>32)&0x0ff);
1769 emit("lxjoin");
1770 }
1771 }
1772 break;
1773 case TOK_OF:
1774 STATE(token->text, "TOK_OF");
1775 /*
1776 * Let's hope I get the semantics right.
1777 *
1778 * The `of' behaves almost the same as an
1779 * `if'. The difference is that `endof'
1780 * takes a branch offset to the associated
1781 * `endcase'. Here we will generate a temporary
1782 * offset of the `of' associated with the `endof'.
1783 * Then in `endcase' we should be pointing just
1784 * after the offset of the last `endof' so we
1785 * calculate the offset and thread our way backwards
1786 * searching for the previous `b(case)' or `b(endof)'.
1787 */
1788 emit("b(of)");
1789 push(outpos);
1790 if (offsetsize == 16) {
1791 spit(0);
1792 }
1793 spit(0); /* Place holder for later */
1794 break;
1795 case TOK_REPEAT:
1796 STATE(token->text, "TOK_REPEAT");
1797 emit("bbranch");
1798 pos = pop();
1799 off = pop();
1800 /* First the offset for the branch back to the begin */
1801 off -= outpos;
1802 if (offsetsize == 16) {
1803 spit((off>>8)&0xff);
1804 }
1805 spit(off&0xff);
1806 emit("b(>resolve)");
1807 /* Now point the offset of the while here. */
1808 off = outpos;
1809 outpos = pos;
1810 pos = off - pos;
1811 if (offsetsize == 16) {
1812 spit((pos>>8)&0xff);
1813 }
1814 spit(pos&0xff);
1815 /* Return to the end of the output */
1816 outpos = off;
1817 break;
1818 case TOK_THEN:
1819 STATE(token->text, "TOK_THEN");
1820 emit("b(>resolve)");
1821 pos = outpos;
1822 outpos = pop();
1823 off = pos - outpos;
1824 if (offsetsize == 16) {
1825 spit((off>>8)&0xff);
1826 }
1827 spit(off&0xff);
1828 outpos = pos;
1829 break;
1830 case TOK_TO:
1831 STATE(token->text, "TOK_TO");
1832 /* The next pass should tokenize the FCODE number */
1833 emit("b(to)");
1834 break;
1835 case TOK_UNTIL:
1836 STATE(token->text, "TOK_UNTIL");
1837 {
1838 int pos;
1839
1840 emit("b?branch");
1841 pos = pop();
1842 pos -= outpos;
1843 if (offsetsize == 16) {
1844 spit((pos>>8)&0xff);
1845 }
1846 spit(pos&0xff);
1847 }
1848 break;
1849 case TOK_VALUE:
1850 STATE(token->text, "TOK_VALUE");
1851
1852 token = yylex();
1853 if (token == NULL) {
1854 (void)printf( "EOF in value definition\n");
1855 return;
1856 }
1857
1858 /* Add new code to dictionary */
1859 fcode = malloc(sizeof(*fcode));
1860 fcode->num = nextfcode++;
1861 fcode->name = strdup(token->text);
1862 fadd(dictionary, fcode);
1863
1864 if (state == 0)
1865 emit("new-token");
1866 else {
1867 if (state == TOK_EXTERNAL)
1868 emit("external-token");
1869 else
1870 /* Here we have a choice of new-token or named-token */
1871 emit("named-token");
1872 sspit(token->text);
1873 }
1874 spit(fcode->num);
1875 emit("b(value)");
1876 break;
1877 case TOK_VARIABLE:
1878 STATE(token->text, "TOK_VARIABLE");
1879
1880 token = yylex();
1881 if (token == NULL) {
1882 (void)printf( "EOF in variable definition\n");
1883 return;
1884 }
1885
1886 /* Add new code to dictionary */
1887 fcode = malloc(sizeof(*fcode));
1888 fcode->num = nextfcode++;
1889 fcode->name = strdup(token->text);
1890 fadd(dictionary, fcode);
1891
1892 if (state == 0)
1893 emit("new-token");
1894 else {
1895 if (state == TOK_EXTERNAL)
1896 emit("external-token");
1897 else
1898 /* Here we have a choice of new-token or named-token */
1899 emit("named-token");
1900 sspit(token->text);
1901 }
1902 spit(fcode->num);
1903 emit("b(variable)");
1904 break;
1905 case TOK_WHILE:
1906 STATE(token->text, "TOK_WHILE");
1907 emit("b?branch");
1908 push(outpos);
1909 if (offsetsize == 16) {
1910 spit(0);
1911 }
1912 spit(0);
1913 break;
1914
1915 /* Tokenizer directives */
1916 case TOK_BEGTOK:
1917 STATE(token->text, "TOK_BEGTOK");
1918 tokenizer = 1;
1919 break;
1920 case TOK_EMIT_BYTE:
1921 STATE(token->text, "TOK_EMIT_BYTE");
1922 spit(pop());
1923 break;
1924 case TOK_ENDTOK:
1925 STATE(token->text, "TOK_ENDTOK");
1926 tokenizer = 0;
1927 break;
1928 case TOK_FLOAD:
1929 STATE(token->text, "TOK_FLOAD");
1930 /* Parse a different file for a while */
1931 token = yylex();
1932 if ((inf = fopen(token->text, "r")) == NULL) {
1933 (void)printf("%s: Could not open %s: %s\n",
1934 myname, token->text, strerror(errno));
1935 break;
1936 }
1937 inbuf = yy_create_buffer(inf, YY_BUF_SIZE);
1938 yy_switch_to_buffer(inbuf);
1939 {
1940 char *oldinfile = infile;
1941
1942 infile = token->text;
1943 tokenize(inbuf);
1944 infile = oldinfile;
1945 }
1946 yy_switch_to_buffer(input);
1947 yy_delete_buffer(inbuf);
1948 fclose(inf);
1949 break;
1950 case TOK_OTHER:
1951 STATE(token->text, "TOK_OTHER");
1952 if (apply_macros(input, token->text))
1953 break;
1954 if (emit(token->text)) {
1955 #if 0
1956 /*
1957 * Call an external command
1958 *
1959 * XXXXX assumes it will always find the command
1960 */
1961 sspit(token->text);
1962 emit("$find");
1963 emit("drop");
1964 emit("execute");
1965 #else
1966 (void)printf( "%s: undefined token `%s'\n",
1967 myname, token->text);
1968 fflush(stderr);
1969 exit(1);
1970 #endif
1971 }
1972 break;
1973 default:
1974 /* Nothing */ ;
1975 }
1976 }
1977 return;
1978 }
1979
1980 /*
1981 * print a tokenizer error message
1982 */
1983 void
1984 token_err(int lineno, char *infile, char *text, char *fmt, ...)
1985 {
1986 va_list ap;
1987
1988 va_start(ap, fmt);
1989 if (infile)
1990 (void)fprintf(stderr, "%s:%d: ", infile, lineno);
1991 if (fmt)
1992 (void)vfprintf(stderr, fmt, ap);
1993 fputc('\n', stderr);
1994 if (text)
1995 fprintf(stderr, "\t%s", text);
1996 va_end(ap);
1997 exit(1);
1998 }
1999
2000 /*
2001 * Lookup fcode string in dictionary and spit it out.
2002 *
2003 * Fcode must be in dictionary. No alias conversion done.
2004 */
2005 int
2006 emit(str)
2007 char *str;
2008 {
2009 struct fcode *code;
2010 if ((code = flookup( dictionary, str)))
2011 spit(code->num);
2012 #ifdef DEBUG
2013 if (debug > 1) {
2014 if (code)
2015 (void)printf( "emitting `%s'\n", code->name);
2016 else
2017 (void)printf( "emit: not found `%s'\n", str);
2018 }
2019 #endif
2020 return (code == NULL);
2021 }
2022
2023 /*
2024 * Spit out an integral value as a series of FCodes.
2025 *
2026 * It will spit out one zero byte or as many bytes as are
2027 * non-zero.
2028 */
2029 int
2030 spit(n)
2031 long n;
2032 {
2033 int count = 1;
2034
2035 if (n >> 8)
2036 count += spit(n >> 8);
2037 if (outpos >= outbufsiz) {
2038 while (outpos >= outbufsiz) outbufsiz += BUFCLICK;
2039 if (!(outbuf = realloc(outbuf, outbufsiz))) {
2040 (void)printf( "realloc of %ld bytes failed -- out of memory\n",
2041 (long)outbufsiz);
2042 exit(1);
2043 }
2044 }
2045 if (debug > 1) printf("spitting %2.2x\n", (unsigned char)n);
2046 outbuf[outpos++] = n;
2047 return (count);
2048 }
2049
2050 /*
2051 * Spit out an FCode string.
2052 */
2053 void
2054 sspit(s)
2055 char *s;
2056 {
2057 int len = strlen(s);
2058
2059 if (len > 255) {
2060 (void)printf( "string length %d too long\n", len);
2061 return;
2062 }
2063 #ifdef DEBUG
2064 if (debug > 1)
2065 (void)printf( "sspit: len %d str `%s'\n", len, s);
2066 #endif
2067 spit(len);
2068 while (*s)
2069 spit(*s++);
2070 }
2071
2072 int
2073 yywrap()
2074 {
2075 /* Always generate EOF */
2076 return (1);
2077 }
2078