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