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