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