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