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