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