fgen.l revision 1.3 1 %{
2 /* FLEX input for FORTH input file scanner */
3 /*
4 * Copyright (c) 1998 Eduardo Horvath.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Eduardo Horvath.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software withough specific prior written permission
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 /*
33 Specifications are as follows:
34
35 The function "yylex()" always returns a pointer to a structure:
36
37 struct tok {
38 int type;
39 char *text;
40 }
41 #define TOKEN struct tok
42 */
43 %}
44
45 decimal [0-9]
46 hex [0-9A-Fa-f]
47 octal [0-7]
48 white [ \t\n\r\f]
49 tail {white}
50
51 %{
52 #include <sys/types.h>
53
54 #include <assert.h>
55 #include <errno.h>
56 #include <fcntl.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <unistd.h>
60
61 #include "fgen.h"
62 TOKEN token;
63
64 /*
65 * Global variables that control the parse state.
66 */
67
68 struct fcode *dictionary = NULL;
69 struct macro *aliases = NULL;
70 int outf = 1; /* stdout */
71 int state = 0;
72 int nextfcode = 0x800;
73 int base = TOK_HEX;
74 long outpos;
75 char *outbuf = NULL;
76 #define BUFCLICK (1024*1024)
77 size_t outbufsiz = 0;
78 char *myname = NULL;
79 int offsetsize = 8;
80 int defining = 0;
81 int tokenizer = 0;
82
83 #define PSTKSIZ 1024
84 Cell parse_stack[PSTKSIZ];
85 int parse_stack_ptr = 0;
86
87 int main __P((int, char *[]));
88 YY_DECL;
89
90 int debug = 0;
91 #define ASSERT if (debug) assert
92 #define STATE(y, x) do { if (debug) printf( "%ld State %s: token `%s'\n", outpos, x, y); } while (0)
93
94 #define YY_NO_UNPUT
95 %}
96
97 %%
98
99 0 { token.type = TOK_OTHER; token.text = yytext;
100 return &token; }
101
102 1 { token.type = TOK_OTHER; token.text = yytext;
103 return &token; }
104
105 2 { token.type = TOK_OTHER; token.text = yytext;
106 return &token; }
107
108 3 { token.type = TOK_OTHER; token.text = yytext;
109 return &token; }
110
111 -1 { token.type = TOK_OTHER; token.text = yytext;
112 return &token; }
113
114 {white}* /* whitespace -- keep looping */ ;
115
116 \\[^\n]*\n /* end of line comment -- keep looping */ { STATE(yytext, "EOL comment"); }
117
118 -?{hex}+ { token.type = TOK_NUMBER; token.text = yytext;
119 return &token; }
120
121 \'.\' { token.type = TOK_C_LIT; token.text = yytext; return &token; }
122
123 \"{white}*(\\\"|[^"])*\" { token.type = TOK_STRING_LIT; token.text = yytext;
124 return &token; } /* String started by `"' or `."' */
125
126 \.\({white}*(\\\"|[^)])*\) { token.type = TOK_PSTRING; token.text = yytext;
127 return &token; } /* String of type `.(.....)' */
128
129 \.\"{white}*(\\\"|[^"])*\" { token.type = TOK_PSTRING; token.text = yytext;
130 return &token; }
131
132 "(" { token.type = TOK_COMMENT; token.text = yytext;
133 return &token; }
134
135 ")" { token.type = TOK_ENDCOMMENT; token.text = yytext;
136 return &token; }
137
138 ":" { token.type = TOK_COLON; token.text = yytext;
139 return &token; }
140
141 ";" { token.type = TOK_SEMICOLON; token.text = yytext;
142 return &token; }
143
144 \' { token.type = TOK_TOKENIZE; token.text = yytext;
145 return &token; }
146
147 [aA][gG][aA][iI][nN] { token.type = TOK_AGAIN; token.text = yytext;
148 return &token; }
149
150 [aA][lL][iI][aA][sS] { token.type = TOK_ALIAS; token.text = yytext;
151 return &token; }
152
153 \[\'\] { token.type = TOK_GETTOKEN; token.text = yytext;
154 return &token; }
155
156 [aA][sS][cC][iI][iI] { token.type = TOK_ASCII; token.text = yytext;
157 return &token; }
158
159 [bB][eE][gG][iI][nN] { token.type = TOK_BEGIN; token.text = yytext;
160 return &token; }
161
162 [bB][uU][fF][fF][eE][rR]: { token.type = TOK_BUFFER; token.text = yytext;
163 return &token; }
164
165 [cC][aA][sS][eE] { token.type = TOK_CASE; token.text = yytext;
166 return &token; }
167
168 [cC][oO][nN][sS][tT][aA][nN][tT] { token.type = TOK_CONSTANT; token.text = yytext;
169 return &token; }
170
171 [cC][oO][nN][tT][rR][oO][lL] { token.type = TOK_CONTROL; token.text = yytext;
172 return &token; }
173
174 [cC][rR][eE][aA][tT][eE] { token.type = TOK_CREATE; token.text = yytext;
175 return &token; }
176
177 [dD]# { token.type = TOK_DECIMAL; token.text = yytext;
178 return &token; }
179
180 [dD][eE][cC][iI][mM][aA][lL] { token.type = TOK_DECIMAL; token.text = yytext;
181 return &token; }
182
183 [dD][eE][fF][eE][rR] { token.type = TOK_DEFER; token.text = yytext;
184 return &token; }
185
186 \??[dD][oO] { token.type = TOK_DO; token.text = yytext;
187 return &token; }
188
189 [eE][lL][sS][eE] { token.type = TOK_ELSE; token.text = yytext;
190 return &token; }
191
192 [eE][nN][dD][cC][aA][sS][eE] { token.type = TOK_ENDCASE; token.text = yytext;
193 return &token; }
194
195 [eE][nN][dD][oO][fF] { token.type = TOK_ENDOF; token.text = yytext;
196 return &token; }
197
198 [eE][xX][tT][eE][rR][nN][aA][lL] { token.type = TOK_EXTERNAL; token.text = yytext;
199 return &token; }
200
201 [fF][iI][eE][lL][dD] { token.type = TOK_FIELD; token.text = yytext;
202 return &token; }
203
204 [hH]# { token.type = TOK_HEX; token.text = yytext;
205 return &token; }
206
207 [hH][eE][aA][dD][eE][rR][lL][eE][sS][sS] { token.type = TOK_HEADERLESS; token.text = yytext;
208 return &token; }
209
210 [hH][eE][aA][dD][eE][rR][sS] { token.type = TOK_HEADERS; token.text = yytext;
211 return &token; }
212
213 [hH][eE][xX] { token.type = TOK_HEX; token.text = yytext;
214 return &token; }
215
216 [iI][fF] { token.type = TOK_IF; token.text = yytext;
217 return &token; }
218
219 \??[lL][eE][aA][vV][eE] { token.type = TOK_LEAVE; token.text = yytext;
220 return &token; }
221
222 \+?[lL][oO][oO][pP] { token.type = TOK_LOOP; token.text = yytext;
223 return &token; }
224
225 [oO]# { token.type = TOK_OCTAL; token.text = yytext;
226 return &token; }
227
228 [oO][cC][tT][aA][lL] { token.type = TOK_OCTAL; token.text = yytext;
229 return &token; }
230
231 [oO][fF] { token.type = TOK_OF; token.text = yytext;
232 return &token; }
233
234 [rR][eE][pP][eE][aA][tT] { token.type = TOK_REPEAT; token.text = yytext;
235 return &token; }
236
237 [tT][hH][eE][nN] { token.type = TOK_THEN; token.text = yytext;
238 return &token; }
239
240 [tT][oO] { token.type = TOK_TO; token.text = yytext;
241 return &token; }
242
243 [uU][nN][tT][iI][lL] { token.type = TOK_UNTIL; token.text = yytext;
244 return &token; }
245
246 [vV][aA][lL][uU][eE] { token.type = TOK_VALUE; token.text = yytext;
247 return &token; }
248
249 [vV][aA][rR][iI][aA][bB][lL][eE] { token.type = TOK_VARIABLE; token.text = yytext;
250 return &token; }
251
252 [wW][hH][iI][lL][eE] { token.type = TOK_WHILE; token.text = yytext;
253 return &token; }
254
255 offset16 { token.type = TOK_OFFSET16; token.text = yytext;
256 return &token; }
257
258 tokenizer\[ { token.type = TOK_BEGTOK; token.text = yytext;
259 return &token; }
260
261 emit-byte { token.type = TOK_EMIT_BYTE; token.text = yytext;
262 return &token; }
263
264 \]tokenizer { token.type = TOK_ENDTOK; token.text = yytext;
265 return &token; }
266
267 fload { token.type = TOK_FLOAD; token.text = yytext;
268 return &token; }
269
270
271 [^ \n\t\r\f]+ { token.type = TOK_OTHER; token.text = yytext;
272 return &token; }
273
274 <<EOF>> { return NULL; }
275 %%
276
277 /* Function definitions */
278 void push __P((Cell));
279 Cell pop __P((void));
280 int fadd __P((struct fcode *, struct fcode *));
281 struct fcode *flookup __P((struct fcode *, char *));
282 int aadd __P((struct macro *, struct macro *));
283 struct macro *alookup __P((struct macro *, char *));
284 void initdic __P((void));
285 void usage __P((char *));
286 void tokenize __P((YY_BUFFER_STATE));
287 int emit __P((char *));
288 int spit __P((long));
289 void sspit __P((char *));
290 int apply_macros __P((YY_BUFFER_STATE, char *));
291 int main __P((int argc, char *argv[]));
292
293 /*
294 * Standard FCode names and numbers. Includes standard
295 * tokenizer aliases.
296 */
297 struct fcode fcodes[] = {
298 { "end0", 0x0000 },
299 { "b(lit)", 0x0010 },
300 { "b(')", 0x0011 },
301 { "b(\")", 0x0012 },
302 { "bbranch", 0x0013 },
303 { "b?branch", 0x0014 },
304 { "b(loop)", 0x0015 },
305 { "b(+loop)", 0x0016 },
306 { "b(do)", 0x0017 },
307 { "b(?do)", 0x0018 },
308 { "i", 0x0019 },
309 { "j", 0x001a },
310 { "b(leave)", 0x001b },
311 { "b(of)", 0x001c },
312 { "execute", 0x001d },
313 { "+", 0x001e },
314 { "-", 0x001f },
315 { "*", 0x0020 },
316 { "/", 0x0021 },
317 { "mod", 0x0022 },
318 { "and", 0x0023 },
319 { "or", 0x0024 },
320 { "xor", 0x0025 },
321 { "invert", 0x0026 },
322 { "lshift", 0x0027 },
323 { "rshift", 0x0028 },
324 { ">>a", 0x0029 },
325 { "/mod", 0x002a },
326 { "u/mod", 0x002b },
327 { "negate", 0x002c },
328 { "abs", 0x002d },
329 { "min", 0x002e },
330 { "max", 0x002f },
331 { ">r", 0x0030 },
332 { "r>", 0x0031 },
333 { "r@", 0x0032 },
334 { "exit", 0x0033 },
335 { "0=", 0x0034 },
336 { "0<>", 0x0035 },
337 { "0<", 0x0036 },
338 { "0<=", 0x0037 },
339 { "0>", 0x0038 },
340 { "0>=", 0x0039 },
341 { "<", 0x003a },
342 { ">", 0x003b },
343 { "=", 0x003c },
344 { "<>", 0x003d },
345 { "u>", 0x003e },
346 { "u<=", 0x003f },
347 { "u<", 0x0040 },
348 { "u>=", 0x0041 },
349 { ">=", 0x0042 },
350 { "<=", 0x0043 },
351 { "between", 0x0044 },
352 { "within", 0x0045 },
353 { "drop", 0x0046 },
354 { "dup", 0x0047 },
355 { "over", 0x0048 },
356 { "swap", 0x0049 },
357 { "rot", 0x004a },
358 { "-rot", 0x004b },
359 { "tuck", 0x004c },
360 { "nip", 0x004d },
361 { "pick", 0x004e },
362 { "roll", 0x004f },
363 { "?dup", 0x0050 },
364 { "depth", 0x0051 },
365 { "2drop", 0x0052 },
366 { "2dup", 0x0053 },
367 { "2over", 0x0054 },
368 { "2swap", 0x0055 },
369 { "2rot", 0x0056 },
370 { "2/", 0x0057 },
371 { "u2/", 0x0058 },
372 { "2*", 0x0059 },
373 { "/c", 0x005a },
374 { "/w", 0x005b },
375 { "/l", 0x005c },
376 { "/n", 0x005d },
377 { "ca+", 0x005e },
378 { "wa+", 0x005f },
379 { "la+", 0x0060 },
380 { "na+", 0x0061 },
381 { "char+", 0x0062 },
382 { "wa1+", 0x0063 },
383 { "la1+", 0x0064 },
384 { "cell+", 0x0065 },
385 { "chars", 0x0066 },
386 { "/w*", 0x0067 },
387 { "/l*", 0x0068 },
388 { "cells", 0x0069 },
389 { "on", 0x006a },
390 { "off", 0x006b },
391 { "+!", 0x006c },
392 { "@", 0x006d },
393 { "l@", 0x006e },
394 { "w@", 0x006f },
395 { "<w@", 0x0070 },
396 { "c@", 0x0071 },
397 { "!", 0x0072 },
398 { "l!", 0x0073 },
399 { "w!", 0x0074 },
400 { "c!", 0x0075 },
401 { "2@", 0x0076 },
402 { "2!", 0x0077 },
403 { "move", 0x0078 },
404 { "fill", 0x0079 },
405 { "comp", 0x007a },
406 { "noop", 0x007b },
407 { "lwsplit", 0x007c },
408 { "wjoin", 0x007d },
409 { "lbsplit", 0x007e },
410 { "bljoin", 0x007f },
411 { "wbflip", 0x0080 },
412 { "upc", 0x0081 },
413 { "lcc", 0x0082 },
414 { "pack", 0x0083 },
415 { "count", 0x0084 },
416 { "body>", 0x0085 },
417 { ">body", 0x0086 },
418 { "fcode-revision", 0x0087 },
419 { "span", 0x0088 },
420 { "unloop", 0x0089 },
421 { "expect", 0x008a },
422 { "alloc-mem", 0x008b },
423 { "free-mem", 0x008c },
424 { "key?", 0x008d },
425 { "key", 0x008e },
426 { "emit", 0x008f },
427 { "type", 0x0090 },
428 { "(cr", 0x0091 },
429 { "cr", 0x0092 },
430 { "#out", 0x0093 },
431 { "#line", 0x0094 },
432 { "hold", 0x0095 },
433 { "<#", 0x0096 },
434 { "u#>", 0x0097 },
435 { "sign", 0x0098 },
436 { "u#", 0x0099 },
437 { "u#s", 0x009a },
438 { "u.", 0x009b },
439 { "u.r", 0x009c },
440 { ".", 0x009d },
441 { ".r", 0x009e },
442 { ".s", 0x009f },
443 { "base", 0x00a0 },
444 { "convert", 0x00a1 },
445 { "$number", 0x00a2 },
446 { "digit", 0x00a3 },
447 { "-1", 0x00a4 },
448 { "true", 0x00a4 },
449 { "0", 0x00a5 },
450 { "1", 0x00a6 },
451 { "2", 0x00a7 },
452 { "3", 0x00a8 },
453 { "bl", 0x00a9 },
454 { "bs", 0x00aa },
455 { "bell", 0x00ab },
456 { "bounds", 0x00ac },
457 { "here", 0x00ad },
458 { "aligned", 0x00ae },
459 { "wbsplit", 0x00af },
460 { "bwjoin", 0x00b0 },
461 { "b(<mark)", 0x00b1 },
462 { "b(>resolve)", 0x00b2 },
463 { "set-token-table", 0x00b3 },
464 { "set-table", 0x00b4 },
465 { "new-token", 0x00b5 },
466 { "named-token", 0x00b6 },
467 { "b(:)", 0x00b7 },
468 { "b(value)", 0x00b8 },
469 { "b(variable)", 0x00b9 },
470 { "b(constant)", 0x00ba },
471 { "b(create)", 0x00bb },
472 { "b(defer)", 0x00bc },
473 { "b(buffer:)", 0x00bd },
474 { "b(field)", 0x00be },
475 { "b(code)", 0x00bf },
476 { "instance", 0x00c0 },
477 { "b(;)", 0x00c2 },
478 { "b(to)", 0x00c3 },
479 { "b(case)", 0x00c4 },
480 { "b(endcase)", 0x00c5 },
481 { "b(endof)", 0x00c6 },
482 { "#", 0x00c7 },
483 { "#s", 0x00c8 },
484 { "#>", 0x00c9 },
485 { "external-token", 0x00ca },
486 { "$find", 0x00cb },
487 { "offset16", 0x00cc },
488 { "evaluate", 0x00cd },
489 { "c,", 0x00d0 },
490 { "w,", 0x00d1 },
491 { "l,", 0x00d2 },
492 { "'", 0x00d3 },
493 { "um*", 0x00d4 },
494 { "um/mod", 0x00d5 },
495 { "d+", 0x00d8 },
496 { "d-", 0x00d9 },
497 { "get-token", 0x00da },
498 { "set-token", 0x00db },
499 { "state", 0x00dc },
500 { "compile,", 0x00dd },
501 { "behavior", 0x00de },
502 { "start0", 0x00f0 },
503 { "start1", 0x00f1 },
504 { "start2", 0x00f2 },
505 { "start4", 0x00f3 },
506 { "ferror", 0x00fc },
507 { "version1", 0x00fd },
508 { "4-byte-id", 0x00fe },
509 { "end1", 0x00ff },
510 { "dma-alloc", 0x0101 },
511 { "my-address", 0x0102 },
512 { "my-space", 0x0103 },
513 { "memmap", 0x0104 },
514 { "free-virtual", 0x0105 },
515 { ">physical", 0x0106 },
516 { "my-params", 0x010f },
517 { "property", 0x0110 },
518 { "encode-int", 0x0111 },
519 { "encode+", 0x0112 },
520 { "encode-phys", 0x0113 },
521 { "encode-string", 0x0114 },
522 { "encode-bytes", 0x0115 },
523 { "reg", 0x0116 },
524 { "intr", 0x0117 },
525 { "driver", 0x0118 },
526 { "model", 0x0119 },
527 { "device-type", 0x011a },
528 { "parse-2int", 0x011b },
529 { "is-install", 0x011c },
530 { "is-remove", 0x011d },
531 { "is-selftest", 0x011e },
532 { "new-device", 0x011f },
533 { "diagnostic-mode?", 0x0120 },
534 { "display-status", 0x0121 },
535 { "memory-test-suite", 0x0122 },
536 { "group-code", 0x0123 },
537 { "mask", 0x0124 },
538 { "get-msecs", 0x0125 },
539 { "ms", 0x0126 },
540 { "find-device", 0x0127 },
541 { "decode-phys", 0x0128 },
542 { "map-low", 0x0130 },
543 { "sbus-intr>cpu", 0x0131 },
544 { "#lines", 0x0150 },
545 { "#columns", 0x0151 },
546 { "line#", 0x0152 },
547 { "column#", 0x0153 },
548 { "inverse?", 0x0154 },
549 { "inverse-screen?", 0x0155 },
550 { "frame-buffer-busy?", 0x0156 },
551 { "draw-character", 0x0157 },
552 { "reset-screen", 0x0158 },
553 { "toggle-cursor", 0x0159 },
554 { "erase-screen", 0x015a },
555 { "blink-screen", 0x015b },
556 { "invert-screen", 0x015c },
557 { "insert-characters", 0x015d },
558 { "delete-characters", 0x015e },
559 { "insert-lines", 0x015f },
560 { "delete-lines", 0x0160 },
561 { "draw-logo", 0x0161 },
562 { "frame-buffer-addr", 0x0162 },
563 { "screen-height", 0x0163 },
564 { "screen-width", 0x0164 },
565 { "window-top", 0x0165 },
566 { "window-left", 0x0166 },
567 { "default-font", 0x016a },
568 { "set-font", 0x016b },
569 { "char-height", 0x016c },
570 { "char-width", 0x016d },
571 { ">font", 0x016e },
572 { "fontbytes", 0x016f },
573 { "fb8-draw-character", 0x0180 },
574 { "fb8-reset-screen", 0x0181 },
575 { "fb8-toggle-cursor", 0x0182 },
576 { "fb8-erase-screen", 0x0183 },
577 { "fb8-blink-screen", 0x0184 },
578 { "fb8-invert-screen", 0x0185 },
579 { "fb8-insert-characters", 0x0186 },
580 { "fb8-delete-characters", 0x0187 },
581 { "fb8-inisert-lines", 0x0188 },
582 { "fb8-delete-lines", 0x0189 },
583 { "fb8-draw-logo", 0x018a },
584 { "fb8-install", 0x018b },
585 { "return-buffer", 0x01a0 },
586 { "xmit-packet", 0x01a1 },
587 { "poll-packet", 0x01a2 },
588 { "mac-address", 0x01a4 },
589 { "device-name", 0x0201 },
590 { "my-args", 0x0202 },
591 { "my-self", 0x0203 },
592 { "find-package", 0x0204 },
593 { "open-package", 0x0205 },
594 { "close-package", 0x0206 },
595 { "find-method", 0x0207 },
596 { "call-package", 0x0208 },
597 { "$call-parent", 0x0209 },
598 { "my-parent", 0x020a },
599 { "ihandle>phandle", 0x020b },
600 { "my-unit", 0x020d },
601 { "$call-method", 0x020e },
602 { "$open-package", 0x020f },
603 { "processor-type", 0x0210 },
604 { "firmware-version", 0x0211 },
605 { "fcode-version", 0x0212 },
606 { "alarm", 0x0213 },
607 { "(is-user-word)", 0x0214 },
608 { "suspend-fcode", 0x0215 },
609 { "abort", 0x0216 },
610 { "catch", 0x0217 },
611 { "throw", 0x0218 },
612 { "user-abort", 0x0219 },
613 { "get-my-property", 0x021a },
614 { "decode-int", 0x021b },
615 { "decode-string", 0x021c },
616 { "get-inherited-property", 0x021d },
617 { "delete-property", 0x021e },
618 { "get-package-property", 0x021f },
619 { "cpeek", 0x0220 },
620 { "wpeek", 0x0221 },
621 { "lpeek", 0x0222 },
622 { "cpoke", 0x0223 },
623 { "wpoke", 0x0224 },
624 { "lpoke", 0x0225 },
625 { "lwflip", 0x0226 },
626 { "lbflip", 0x0227 },
627 { "lbflips", 0x0228 },
628 { "adr-mask", 0x0229 },
629 { "rb@", 0x0230 },
630 { "rb!", 0x0231 },
631 { "rw@", 0x0232 },
632 { "rw!", 0x0233 },
633 { "rl@", 0x0234 },
634 { "rl!", 0x0235 },
635 { "wbflips", 0x0236 },
636 { "lwflips", 0x0237 },
637 { "probe", 0x0238 },
638 { "probe-virtual", 0x0239 },
639 { "child", 0x023b },
640 { "peer", 0x023c },
641 { "next-property", 0x023d },
642 { "byte-load", 0x023e },
643 { "set-args", 0x023f },
644 { "left-parse-string", 0x0240 },
645 /* 64-bit FCode extensions */
646 { "bxjoin", 0x0241 },
647 { "<l@", 0x0242 },
648 { "lxjoin", 0x0243 },
649 { "rx@", 0x022e },
650 { "rx!", 0x022f },
651 { "wxjoin", 0x0244 },
652 { "x,", 0x0245 },
653 { "x@", 0x0246 },
654 { "x!", 0x0247 },
655 { "/x", 0x0248 },
656 { "/x*", 0x0249 },
657 { "xa+", 0x024a },
658 { "xa1+", 0x024b },
659 { "xbflip", 0x024c },
660 { "xbflips", 0x024d },
661 { "xbsplit", 0x024e },
662 { "xlflip", 0x024f },
663 { "xlflips", 0x0250 },
664 { "xlsplit", 0x0251 },
665 { "xwflip", 0x0252 },
666 { "xwflips", 0x0253 },
667 { "xwsplit", 0x0254 },
668 { NULL, NULL }
669 };
670
671 /*
672 * Default macros -- can be overridden by colon definitions.
673 */
674 struct macro macros[] = {
675 { "eval", "evaluate" }, /* Build a more balanced tree */
676 { "(.)", "dup abs <# u#s swap sign u#>" },
677 { "<<", "lshift" },
678 { ">>", "rshift" },
679 { "?", "@ ." },
680 { "1+", "1 +" },
681 { "1-", "1 -" },
682 { "2+", "2 +" },
683 { "2-", "2 -" },
684 { "abort\"", "-2 throw" },
685 { "accept", "span @ -rot expect span @ swap span !" },
686 { "allot", "0 max 0 ?do 0 c, loop" },
687 { "blank", "bl fill" },
688 { "/c*", "chars" },
689 { "ca1+", "char+" },
690 { "carret", "b(lit) 00 00 00 0x0d" },
691 { ".d" "base @ swap 0x0a base ! . base !" },
692 { "decode-bytes", ">r over r@ + swap r@ - rot r>" },
693 { "3drop", "drop 2drop" },
694 { "3dup", "2 pick 2 pick 2 pick" },
695 { "erase", "0 fill" },
696 { "false", "0" },
697 { ".h" "base @ swap 0x10 base ! . base !" },
698 { "linefeed", "b(lit) 00 00 00 0x0a" },
699 { "/n*", "cells" },
700 { "na1+", "cell+", },
701 { "not", "invert", },
702 { "s.", "(.) type space" },
703 { "space", "bl emit" },
704 { "spaces", "0 max 0 ?do space loop" },
705 { "struct", "0" },
706 { "true", "-1" },
707 { "(u,)", "<# u#s u#>" },
708 { NULL, NULL }
709 };
710
711 /*
712 * Parser stack control functions.
713 */
714
715 void
716 push(val)
717 Cell val;
718 {
719 parse_stack[parse_stack_ptr++] = val;
720 if (parse_stack_ptr >= PSTKSIZ) {
721 (void)printf( "Parse stack overflow\n");
722 exit(1);
723 }
724 }
725
726 Cell
727 pop()
728 {
729 ASSERT(parse_stack_ptr);
730 return parse_stack[--parse_stack_ptr];
731 }
732
733 /*
734 * Insert fcode into dictionary.
735 */
736 int
737 fadd(dict, new)
738 struct fcode *dict, *new;
739 {
740 int res = strcmp(dict->name, new->name);
741
742 #ifdef DEBUG
743 new->type = FCODE;
744 ASSERT(dict->type == FCODE);
745 #endif
746 /* Don't allow duplicate entries. */
747 if (!res) return (0);
748 if (res < 0) {
749 if (dict->l)
750 return fadd(dict->l, new);
751 else {
752 #ifdef DEBUG
753 if (debug > 1)
754 (void)printf( "fadd: new FCode `%s' is %lx\n",
755 new->name, new->num);
756 #endif
757 new->l = new->r = NULL;
758 dict->l = new;
759 }
760 } else {
761 if (dict->r)
762 return fadd(dict->r, new);
763 else {
764 #ifdef DEBUG
765 if (debug > 1)
766 (void)printf( "fadd: new FCode `%s' is %lx\n",
767 new->name, new->num);
768 #endif
769 new->l = new->r = NULL;
770 dict->r = new;
771 }
772 }
773 return (1);
774 }
775
776 /*
777 * Look for a code in the dictionary.
778 */
779 struct fcode *
780 flookup(dict, str)
781 struct fcode *dict;
782 char *str;
783 {
784 int res;
785 if (!dict) return (dict);
786
787 res = strcmp(dict->name, str);
788 #ifdef DEBUG
789 ASSERT(dict->type == FCODE);
790 if (debug > 2)
791 (void)printf( "flookup: `%s' and `%s' %s match\n",
792 str, dict->name, res?"don't":"do");
793 #endif
794 if (!res) return (dict);
795 if (res < 0)
796 return (flookup(dict->l, str));
797 else
798 return (flookup(dict->r, str));
799
800 }
801
802 /*
803 * Insert alias into macros.
804 */
805 int
806 aadd(dict, new)
807 struct macro *dict, *new;
808 {
809 int res = strcmp(dict->name, new->name);
810
811 #ifdef DEBUG
812 new->type = MACRO;
813 ASSERT(dict->type == MACRO);
814 #endif
815 /* Don't allow duplicate entries. */
816 if (!res) return (0);
817 if (res < 0) {
818 if (dict->l)
819 return aadd(dict->l, new);
820 else {
821 new->l = new->r = NULL;
822 dict->l = new;
823 #ifdef DEBUG
824 if (debug > 1)
825 (void)printf( "aadd: new alias `%s' to `%s'\n",
826 new->name, new->equiv);
827 #endif
828 }
829 } else {
830 if (dict->r)
831 return aadd(dict->r, new);
832 else {
833 new->l = new->r = NULL;
834 dict->r = new;
835 #ifdef DEBUG
836 if (debug > 1)
837 (void)printf( "aadd: new alias `%s' to `%s'\n",
838 new->name, new->equiv);
839 #endif
840 }
841 }
842 return (1);
843 }
844
845 /*
846 * Look for a macro in the aliases.
847 */
848 struct macro *
849 alookup(dict, str)
850 struct macro *dict;
851 char *str;
852 {
853 int res;
854 if (!dict) return (dict);
855
856 #ifdef DEBUG
857 ASSERT(dict->type == MACRO);
858 #endif
859 res = strcmp(dict->name, str);
860 if (!res) return (dict);
861 if (res < 0)
862 return (alookup(dict->l, str));
863 else
864 return (alookup(dict->r, str));
865
866 }
867
868 /*
869 * Bootstrap the dictionary and then install
870 * all the standard FCodes.
871 */
872 void
873 initdic()
874 {
875 struct fcode *code = fcodes;
876 struct macro *alias = macros;
877
878 ASSERT(dictionary == NULL);
879 code->l = code->r = NULL;
880 dictionary = code;
881 #ifdef DEBUG
882 code->type = FCODE;
883 #endif
884
885 while ((++code)->name) {
886 if(!fadd(dictionary, code)) {
887 printf("init: duplicate dictionary entry %s\n",
888 code->name);
889 abort();
890 }
891 }
892
893 ASSERT(aliases == NULL);
894 aliases = alias;
895 alias->l = alias->r = NULL;
896 #ifdef DEBUG
897 alias->type = MACRO;
898 #endif
899 while ((++alias)->name) {
900 if(!aadd(aliases, alias)) {
901 printf("init: duplicate macro entry %s\n",
902 alias->name);
903 abort();
904 }
905 }
906
907 }
908
909 int
910 apply_macros(input, str)
911 YY_BUFFER_STATE input;
912 char *str;
913 {
914 struct macro *xform = alookup(aliases, str);
915
916 if (xform) {
917 YY_BUFFER_STATE newbuf;
918
919 newbuf = yy_scan_string(xform->equiv);
920 yy_switch_to_buffer(newbuf);
921 tokenize(newbuf);
922 yy_switch_to_buffer(input);
923 yy_delete_buffer(newbuf);
924 }
925 return (xform != NULL);
926 }
927
928 void
929 usage(me)
930 char *me;
931 {
932 (void)fprintf(stderr, "%s: [-o <outfile>] <infile>\n", me);
933 exit(1);
934 }
935
936 int
937 main(argc, argv)
938 int argc;
939 char *argv[];
940 {
941 extern char *optarg;
942 extern int optind;
943 int bflag, ch;
944 FILE *inf;
945 struct fcode_header *fheader;
946 YY_BUFFER_STATE inbuf;
947 char *hdrtype = "version1";
948 int i;
949
950 outf = 1; /* stdout */
951 myname = argv[0];
952
953 bflag = 0;
954 while ((ch = getopt(argc, argv, "d:o:")) != -1)
955 switch(ch) {
956 case 'd':
957 debug = atol(optarg);
958 break;
959 case 'o':
960 if ((outf = open(optarg, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == NULL) {
961 (void)printf(
962 "%s: %s: %s\n",
963 myname, optarg, strerror(errno));
964 exit(1);
965 }
966 break;
967 case '?':
968 default:
969 printf( "Illegal argument %c\n", ch);
970 usage(myname);
971 }
972 argc -= optind;
973 argv += optind;
974
975 if (argc != 1) printf( "argc = %d\n", argc);
976 if (argc != 1) usage(myname);
977
978 /*
979 * Initialization stuff.
980 */
981 initdic();
982 outbufsiz = BUFCLICK;
983 outbuf = malloc(outbufsiz);
984 fheader = (struct fcode_header *)outbuf;
985 outpos = 0;
986 emit(hdrtype);
987 outpos = sizeof(*fheader);
988
989 /*
990 * Do it.
991 */
992
993 if ((inf = fopen(argv[0], "r")) == NULL) {
994 (void)printf( "%s: Could not open %s: %s\n",
995 myname, argv[0], strerror(errno));
996 }
997
998 inbuf = yy_create_buffer( inf, YY_BUF_SIZE );
999 yy_switch_to_buffer(inbuf);
1000 tokenize(inbuf);
1001 yy_delete_buffer(inbuf);
1002 fclose(inf);
1003 emit("end0");
1004
1005 /* Now calculate length and checksum and stick them in the header */
1006 fheader->format = 0x08;
1007 fheader->length = htonl(outpos);
1008 fheader->checksum = 0;
1009 for (i = sizeof(*fheader); i<outpos; i++)
1010 fheader->checksum += outbuf[i];
1011 fheader->checksum = htons(fheader->checksum);
1012
1013 if (write(outf, outbuf, outpos) != outpos) {
1014 close(outf);
1015 (void)printf( "%s: write error %s\n",
1016 myname, strerror(errno));
1017 exit(1);
1018 }
1019 close(outf);
1020 return (0);
1021 };
1022
1023 /*
1024 * Tokenize one file. This is a separate function so it can
1025 * be called recursively to parse mutiple levels of include files.
1026 */
1027
1028 void
1029 tokenize(input)
1030 YY_BUFFER_STATE input;
1031 {
1032 FILE *inf;
1033 YY_BUFFER_STATE inbuf;
1034 TOKEN *token;
1035 struct fcode *fcode;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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;
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 (long)outbufsiz);
1918 exit(1);
1919 }
1920 }
1921 outbuf[outpos++] = n;
1922 return (count);
1923 }
1924
1925 /*
1926 * Spit out an FCode string.
1927 */
1928 void
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;
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)
1944 spit(*s++);
1945 }
1946
1947 int
1948 yywrap()
1949 {
1950 /* Always generate EOF */
1951 return (1);
1952 }
1953