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