fgen.l revision 1.21 1 %{
2 /* $NetBSD: fgen.l,v 1.21 2003/06/23 13:05:48 agc 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 #ifndef lint
47 __RCSID("$NetBSD: fgen.l,v 1.21 2003/06/23 13:05:48 agc 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 case '?':
1021 default:
1022 warnx("Illegal argument: %c", ch);
1023 usage(myname);
1024 }
1025 argc -= optind;
1026 argv += optind;
1027
1028 if (argc != 1)
1029 usage(myname);
1030
1031 infile = argv[0];
1032
1033 /*
1034 * Initialization stuff.
1035 */
1036 initdic();
1037 outbufsiz = BUFCLICK;
1038 outbuf = malloc(outbufsiz);
1039 fheader = (struct fcode_header *)outbuf;
1040 outpos = 0;
1041 emit(hdrtype);
1042 outpos = sizeof(*fheader);
1043
1044 /*
1045 * Do it.
1046 */
1047 if ((inf = fopen(infile, "r")) == NULL)
1048 (void)err(1, "can not open %s for reading", infile);
1049
1050 inbuf = yy_create_buffer( inf, YY_BUF_SIZE );
1051 yy_switch_to_buffer(inbuf);
1052 tokenize(inbuf);
1053 yy_delete_buffer(inbuf);
1054 fclose(inf);
1055 emit("end0");
1056
1057 /* Now calculate length and checksum and stick them in the header */
1058 fheader->format = 0x08;
1059 fheader->length = htonl(outpos);
1060 fheader->checksum = 0;
1061 for (i = sizeof(*fheader); i<outpos; i++)
1062 fheader->checksum += outbuf[i];
1063 fheader->checksum = htons(fheader->checksum);
1064
1065 if ((outf = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
1066 err(1, "can out open %s for writing", outfile);
1067
1068 if (write(outf, outbuf, outpos) != outpos) {
1069 close(outf);
1070 unlink(outfile);
1071 err(1, "write error");
1072 }
1073 close(outf);
1074 return (0);
1075 };
1076
1077 /*
1078 * Tokenize one file. This is a separate function so it can
1079 * be called recursively to parse mutiple levels of include files.
1080 */
1081
1082 void
1083 tokenize(input)
1084 YY_BUFFER_STATE input;
1085 {
1086 FILE *inf;
1087 YY_BUFFER_STATE inbuf;
1088 TOKEN *token;
1089 char *last_token = "";
1090 struct fcode *fcode;
1091 int pos, off;
1092
1093 while ((token = yylex()) != NULL) {
1094 switch (token->type) {
1095 case TOK_NUMBER:
1096 STATE(token->text, "TOK_NUMBER");
1097 {
1098 char *end;
1099 Cell value;
1100
1101 if (tokenizer) {
1102 push(strtol(token->text, &end, 16));
1103 break;
1104 }
1105 value = strtol(token->text, &end, base);
1106 if (*end != 0)
1107 token_err(yylineno, infile, yytext,
1108 "illegal number conversion");
1109
1110 /*
1111 * If this is a 64-bit value we need to store two literals
1112 * and issue a `lxjoin' to combine them. But that's a future
1113 * project.
1114 */
1115 emit("b(lit)");
1116 spit((value>>24)&0x0ff);
1117 spit((value>>16)&0x0ff);
1118 spit((value>>8)&0x0ff);
1119 spit(value&0x0ff);
1120 if ((value>>32) != value && (value>>32) != 0 &&
1121 (value>>32) != -1) {
1122 emit("b(lit)");
1123 spit((value>>56)&0x0ff);
1124 spit((value>>48)&0x0ff);
1125 spit((value>>40)&0x0ff);
1126 spit((value>>32)&0x0ff);
1127 emit("lxjoin");
1128 }
1129 }
1130 break;
1131 case TOK_C_LIT:
1132 STATE(token->text, "TOK_C_LIT");
1133 emit("b(lit)");
1134 spit(0);
1135 spit(0);
1136 spit(0);
1137 spit(token->text[1]);
1138 break;
1139 case TOK_STRING_LIT:
1140 STATE(token->text, "TOK_STRING_LIT:");
1141 {
1142 int len;
1143 char *p = token->text;
1144
1145 ++p; /* Skip the quote */
1146 len = strlen(++p); /* Skip the 1st space */
1147
1148 #define ERR_TOOLONG \
1149 token_err(yylineno, infile, yytext, "string length %d too long", len)
1150
1151 if (len > 255)
1152 ERR_TOOLONG;
1153
1154 if (p[len-1] == ')' ||
1155 p[len-1] == '"') {
1156 p[len-1] = 0;
1157 }
1158 emit("b(\")");
1159 sspit(p);
1160 }
1161 break;
1162 case TOK_PSTRING:
1163 STATE(token->text, "TOK_PSTRING:");
1164 {
1165 int len;
1166 char *p = token->text;
1167
1168 if (*p++ == '.') p++; /* Skip over delimiter */
1169 p++; /* Skip over space/tab */
1170
1171 len = strlen(p);
1172 if (len > 255)
1173 ERR_TOOLONG;
1174
1175 if (p[len-1] == ')' ||
1176 p[len-1] == '"') {
1177 p[len-1] = 0;
1178 }
1179 emit("b(\")");
1180 sspit(p);
1181 emit("type");
1182 }
1183 break;
1184 case TOK_TOKENIZE:
1185 STATE(token->text, "TOK_TOKENIZE");
1186 /* The next pass should tokenize the FCODE number */
1187 emit("b(')");
1188 break;
1189 case TOK_COMMENT:
1190 STATE(token->text, "TOK_COMMENT:");
1191 while (((token = yylex()) != NULL) && token->type != TOK_ENDCOMMENT)
1192 ;
1193 break;
1194 case TOK_ENDCOMMENT:
1195 STATE(token->text, "TOK_ENDCOMMENT");
1196 token_err(yylineno, infile, NULL,
1197 "ENDCOMMENT encountered outside comment");
1198 break;
1199 case TOK_COLON:
1200 STATE(token->text, "TOK_COLON:");
1201
1202 token = yylex();
1203 if (token == NULL)
1204 token_err(yylineno, infile, yytext,
1205 "EOF in colon definition");
1206
1207 /* Add new code to dictionary */
1208 fcode = malloc(sizeof(*fcode));
1209 fcode->num = nextfcode++;
1210 fcode->name = strdup(token->text);
1211 if (!fadd(dictionary, fcode))
1212 token_err(yylineno, infile, NULL,
1213 "Duplicate definition: `%s'\n", fcode->name);
1214 #ifdef DEBUG
1215 if (debug)
1216 (void)printf("Adding %s to dictionary\n", token->text);
1217 #endif
1218 if (state == 0)
1219 emit("new-token");
1220 else {
1221 if (state == TOK_EXTERNAL)
1222 emit("external-token");
1223 else
1224 /* Here we have a choice of new-token or named-token */
1225 emit("named-token");
1226 sspit(token->text);
1227 }
1228 spit(fcode->num);
1229 emit("b(:)");
1230 last_token = fcode->name;
1231 defining = 1;
1232 break;
1233 case TOK_SEMICOLON:
1234 STATE(token->text, "TOK_SEMICOLON:");
1235 emit("b(;)");
1236 defining = 0;
1237 if (depth()) {
1238 token_err(yylineno, infile, NULL,
1239 "Warning: stack depth %d at end of %s\n",
1240 depth(), last_token);
1241 }
1242 last_token = "";
1243 break;
1244
1245 /* These are special */
1246 case TOK_AGAIN:
1247 STATE(token->text, "TOK_AGAIN");
1248 emit("bbranch");
1249 pos = pop();
1250 pos -= outpos;
1251 if (offsetsize == 16) {
1252 spit((pos>>8)&0xff);
1253 }
1254 spit(pos&0xff);
1255 break;
1256 case TOK_ALIAS:
1257 STATE(token->text, "TOK_ALIAS");
1258 {
1259 struct macro *alias;
1260
1261 token = yylex();
1262 if (token == NULL) {
1263 (void)printf( "EOF in alias definition\n");
1264 return;
1265 }
1266 if (token->type != TOK_OTHER) {
1267 (void)printf( "ENDCOMMENT aliasing weird token type %d\n",
1268 token->type);
1269 }
1270 alias = malloc(sizeof(*alias));
1271 alias->name = strdup(token->text);
1272 token = yylex();
1273 if (token == NULL) {
1274 (void)printf( "EOF in alias definition\n");
1275 return;
1276 }
1277 alias->equiv = strdup(token->text);
1278 if (!aadd(aliases, alias)) {
1279 (void)printf( "ERROR: Duplicate alias %s\n",
1280 alias->name);
1281 exit(1);
1282 }
1283 }
1284 break;
1285 case TOK_GETTOKEN:
1286 STATE(token->text, "TOK_GETTOKEN");
1287 /* This is caused by ['] */
1288 emit("b(')");
1289 token = yylex();
1290 if (token == NULL) {
1291 (void)printf( "EOF in [']\n");
1292 return;
1293 }
1294 if ((fcode = flookup(dictionary, token->text)) == NULL) {
1295 (void)printf( "[']: %s not found\n", token->text);
1296 exit(1);
1297 }
1298 spit(fcode->num);
1299 break;
1300 case TOK_ASCII:
1301 STATE(token->text, "TOK_ASCII");
1302 token = yylex();
1303 if (token == NULL) {
1304 (void)printf( "EOF after \"ascii\"\n");
1305 exit(1);
1306 }
1307 emit("b(lit)");
1308 spit(0);
1309 spit(0);
1310 spit(0);
1311 spit(token->text[0]);
1312 break;
1313 case TOK_BEGIN:
1314 STATE(token->text, "TOK_BEGIN");
1315 emit("b(<mark)");
1316 push(outpos);
1317 break;
1318 case TOK_BUFFER:
1319 STATE(token->text, "TOK_BUFFER");
1320
1321 token = yylex();
1322 if (token == NULL) {
1323 (void)printf( "EOF in colon definition\n");
1324 return;
1325 }
1326
1327 /* Add new code to dictionary */
1328 fcode = malloc(sizeof(*fcode));
1329 fcode->num = nextfcode++;
1330 fcode->name = strdup(token->text);
1331 fadd(dictionary, fcode);
1332
1333 if (state == 0)
1334 emit("new-token");
1335 else {
1336 if (state == TOK_EXTERNAL)
1337 emit("external-token");
1338 else
1339 /* Here we have a choice of new-token or named-token */
1340 emit("named-token");
1341 sspit(token->text);
1342 }
1343 spit(fcode->num);
1344 emit("b(buffer:)");
1345 break;
1346 case TOK_CASE:
1347 STATE(token->text, "TOK_CASE");
1348 emit("b(case)");
1349 push(0);
1350 break;
1351 case TOK_CONSTANT:
1352 STATE(token->text, "TOK_CONSTANT");
1353
1354 token = yylex();
1355 if (token == NULL) {
1356 (void)printf( "EOF in constant definition\n");
1357 return;
1358 }
1359
1360 /* Add new code to dictionary */
1361 fcode = malloc(sizeof(*fcode));
1362 fcode->num = nextfcode++;
1363 fcode->name = strdup(token->text);
1364 fadd(dictionary, fcode);
1365
1366 if (state == 0)
1367 emit("new-token");
1368 else {
1369 if (state == TOK_EXTERNAL)
1370 emit("external-token");
1371 else
1372 /* Here we have a choice of new-token or named-token */
1373 emit("named-token");
1374 sspit(token->text);
1375 }
1376 spit(fcode->num);
1377 emit("b(constant)");
1378 break;
1379 case TOK_CONTROL:
1380 STATE(token->text, "TOK_CONTROL");
1381 token = yylex();
1382 if (token == NULL) {
1383 (void)printf( "EOF after \"ascii\"\n");
1384 exit(1);
1385 }
1386 emit("b(lit)");
1387 spit(0);
1388 spit(0);
1389 spit(0);
1390 spit(token->text[0]&0x1f);
1391 break;
1392 case TOK_CREATE:
1393 STATE(token->text, "TOK_CREATE");
1394 /* Don't know what this does or if it's right */
1395 token = yylex();
1396 if (token == NULL) {
1397 (void)printf( "EOF in create definition\n");
1398 return;
1399 }
1400
1401 /* Add new code to dictionary */
1402 fcode = malloc(sizeof(*fcode));
1403 fcode->num = nextfcode++;
1404 fcode->name = strdup(token->text);
1405 fadd(dictionary, fcode);
1406
1407 if (state == 0)
1408 emit("new-token");
1409 else {
1410 if (state == TOK_EXTERNAL)
1411 emit("external-token");
1412 else
1413 /* Here we have a choice of new-token or named-token */
1414 emit("named-token");
1415 sspit(token->text);
1416 }
1417 spit(fcode->num);
1418 emit("b(create)");
1419 break;
1420 case TOK_DECIMAL:
1421 STATE(token->text, "TOK_DECIMAL");
1422 if (token->text[1] != '#') {
1423 if (defining) {
1424 spit(10);
1425 emit("base");
1426 emit("!");
1427 } else
1428 base = TOK_DECIMAL;
1429 } else {
1430 char *end;
1431 Cell value;
1432
1433 token = yylex();
1434 if (token == NULL) {
1435 (void)printf( "EOF after d#\n");
1436 return;
1437 }
1438 if (token->type == TOK_OTHER) {
1439 if (strcmp("-1", token->text) == 0) {
1440 emit(token->text);
1441 break;
1442 }
1443 }
1444 value = strtol(token->text, &end, 10);
1445 if (*end != 0)
1446 token_err(yylineno, infile, NULL,
1447 "Illegal number conversion: %s", token->text);
1448
1449 /*
1450 * If this is a 64-bit value we need to store two literals
1451 * and issue a `lxjoin' to combine them. But that's a future
1452 * project.
1453 */
1454 emit("b(lit)");
1455 spit((value>>24)&0x0ff);
1456 spit((value>>16)&0x0ff);
1457 spit((value>>8)&0x0ff);
1458 spit(value&0x0ff);
1459 if ((value>>32) != value && (value>>32) != 0) {
1460 emit("b(lit)");
1461 spit((value>>56)&0x0ff);
1462 spit((value>>48)&0x0ff);
1463 spit((value>>40)&0x0ff);
1464 spit((value>>32)&0x0ff);
1465 emit("lxjoin");
1466 }
1467 }
1468 break;
1469 case TOK_DEFER:
1470 STATE(token->text, "TOK_DEFER");
1471 /* Don't know what this does or if it's right */
1472 token = yylex();
1473 if (token == NULL) {
1474 (void)printf( "EOF in colon definition\n");
1475 return;
1476 }
1477
1478 /* Add new code to dictionary */
1479 fcode = malloc(sizeof(*fcode));
1480 fcode->num = nextfcode++;
1481 fcode->name = strdup(token->text);
1482 fadd(dictionary, fcode);
1483
1484 if (state == 0)
1485 emit("new-token");
1486 else {
1487 if (state == TOK_EXTERNAL)
1488 emit("external-token");
1489 else
1490 /* Here we have a choice of new-token or named-token */
1491 emit("named-token");
1492 sspit(token->text);
1493 }
1494 spit(fcode->num);
1495 emit("b(defer)");
1496 break;
1497 case TOK_DO:
1498 STATE(token->text, "TOK_DO");
1499 /*
1500 * From the 1275 spec. B is branch location, T is branch target.
1501 *
1502 * b(do) offset1 ... b(loop) offset2 ...
1503 * b(do) offset1 ... b(+loop) offset2 ...
1504 * b(?do) offset1 ... b(loop) offset2 ...
1505 * b(?do) offset1 ... b(+loop) offset2 ...
1506 * ^ ^
1507 * B1 ^ ^ T1
1508 * T2 B2
1509 *
1510 * How we do this is we generate the b(do) or b(?do), spit out a
1511 * zero offset while remembering b1 and t2. Then we call tokenize()
1512 * to generate the body. When tokenize() finds a b(loop) or b(+loop),
1513 * it generates the FCode and returns, with outpos at b2. We then
1514 * calculate the offsets, put them in the right slots and finishup.
1515 */
1516
1517 if (token->text[0] == '?')
1518 emit("b(?do)");
1519 else
1520 emit("b(do)");
1521 push(outpos);
1522 if (offsetsize == 16) {
1523 spit(0);
1524 }
1525 spit(0); /* Place holder for later */
1526 push(outpos);
1527 break;
1528 case TOK_ELSE:
1529 STATE(token->text, "TOK_ELSE");
1530 /* Get where we need to patch */
1531 off = pop();
1532 emit("bbranch");
1533 /* Save where we are now. */
1534 push(outpos);
1535 if (offsetsize == 16) {
1536 spit(0); /* Place holder for later */
1537 }
1538 spit(0); /* Place holder for later */
1539 emit("b(>resolve)");
1540 /* Rewind and patch the if branch */
1541 pos = outpos;
1542 outpos = off;
1543 off = pos - off;
1544 if (offsetsize == 16) {
1545 spit(0); /* Place holder for later */
1546 }
1547 spit(0); /* Place holder for later */
1548 /* revert to the end */
1549 outpos = pos;
1550 break;
1551 case TOK_ENDCASE:
1552 STATE(token->text, "TOK_ENDCASE:");
1553 pos = outpos; /* Remember where we need to branch to */
1554
1555 /* Thread our way backwards and install proper offsets */
1556 off = pop();
1557 while (off) {
1558 int tmp;
1559
1560 /* Move to this offset */
1561 outpos = off;
1562 /* Load next offset to process */
1563 tmp = outbuf[outpos];
1564
1565 /* process this offset */
1566 off = pos - outpos;
1567 if (offsetsize == 16) {
1568 spit((off>>8)&0xff);
1569 }
1570 spit(off&0xff);
1571 off = tmp;
1572 }
1573 outpos = pos;
1574 emit("b(endcase)");
1575 break;
1576 case TOK_ENDOF:
1577 STATE(token->text, "TOK_ENDOF");
1578 off = pop();
1579 emit("b(endof)");
1580 /*
1581 * Save back pointer in the offset field so we can traverse
1582 * the linked list and patch it in the endcase.
1583 */
1584 pos = pop(); /* get position of prev link. */
1585 push(outpos); /* save position of this link. */
1586 spit(pos); /* save potision of prev link. */
1587 if (offsetsize == 16) {
1588 spit(0);
1589 }
1590 pos = outpos;
1591 /* Now point the offset from b(of) here. */
1592 outpos = off;
1593 off = outpos - off;
1594 if (offsetsize == 16) {
1595 spit((off>>8)&0xff);
1596 }
1597 spit(off&0xff);
1598 /* Restore position */
1599 outpos = pos;
1600 break;
1601 case TOK_EXTERNAL:
1602 STATE(token->text, "TOK_EXTERNAL");
1603 state = TOK_EXTERNAL;
1604 break;
1605 case TOK_FIELD:
1606 STATE(token->text, "TOK_FIELD");
1607
1608 token = yylex();
1609 if (token == NULL) {
1610 (void)printf( "EOF in field definition\n");
1611 return;
1612 }
1613
1614 /* Add new code to dictionary */
1615 fcode = malloc(sizeof(*fcode));
1616 fcode->num = nextfcode++;
1617 fcode->name = strdup(token->text);
1618 fadd(dictionary, fcode);
1619
1620 if (state == 0)
1621 emit("new-token");
1622 else {
1623 if (state == TOK_EXTERNAL)
1624 emit("external-token");
1625 else
1626 /* Here we have a choice of new-token or named-token */
1627 emit("named-token");
1628 sspit(token->text);
1629 }
1630 spit(fcode->num);
1631 emit("b(field)");
1632 break;
1633
1634 case TOK_HEX:
1635 STATE(token->text, "TOK_HEX");
1636 if (token->text[1] != '#') {
1637 if (defining) {
1638 spit(16);
1639 emit("base");
1640 emit("!");
1641 } else
1642 base = TOK_HEX;
1643 } else {
1644 char *end;
1645 Cell value;
1646
1647 token = yylex();
1648 if (token == NULL) {
1649 (void)printf( "EOF after h#\n");
1650 return;
1651 }
1652 value = strtol(token->text, &end, 16);
1653 if (*end != 0) {
1654 (void)printf("Illegal number conversion:%s:%d: %s\n",
1655 infile, yylineno, yytext);
1656 exit(1);
1657 }
1658 /*
1659 * If this is a 64-bit value we need to store two literals
1660 * and issue a `lxjoin' to combine them. But that's a future
1661 * project.
1662 */
1663 emit("b(lit)");
1664 spit((value>>24)&0x0ff);
1665 spit((value>>16)&0x0ff);
1666 spit((value>>8)&0x0ff);
1667 spit(value&0x0ff);
1668 if ((value>>32) != value && (value>>32) != 0) {
1669 emit("b(lit)");
1670 spit((value>>56)&0x0ff);
1671 spit((value>>48)&0x0ff);
1672 spit((value>>40)&0x0ff);
1673 spit((value>>32)&0x0ff);
1674 emit("lxjoin");
1675 }
1676 }
1677 break;
1678 case TOK_HEADERLESS:
1679 STATE(token->text, "TOK_HEADERLESS");
1680 state = 0;
1681 break;
1682 case TOK_HEADERS:
1683 STATE(token->text, "TOK_HEADERS");
1684 state = TOK_HEADERS;
1685 break;
1686 case TOK_OFFSET16:
1687 STATE(token->text, "TOK_OFFSET16");
1688 offsetsize = 16;
1689 emit("offset16");
1690 break;
1691 case TOK_IF:
1692 STATE(token->text, "TOK_IF");
1693 /*
1694 * Similar to do but simpler since we only deal w/one branch.
1695 */
1696 emit("b?branch");
1697 push(outpos);
1698 if (offsetsize == 16) {
1699 spit(0); /* Place holder for later */
1700 }
1701 spit(0); /* Place holder for later */
1702 break;
1703 case TOK_LEAVE:
1704 STATE(token->text, "TOK_LEAVE");
1705 emit("b(leave)");
1706 break;
1707 case TOK_LOOP:
1708 STATE(token->text, "TOK_LOOP");
1709
1710 if (token->text[0] == '+')
1711 emit("b(+loop)");
1712 else
1713 emit("b(loop)");
1714 /* First do backwards branch of loop */
1715 pos = pop();
1716 off = pos - outpos;
1717 if (offsetsize == 16) {
1718 spit((off>>8)&0xff);
1719 }
1720 spit(off&0xff);
1721 /* Now do forward branch of do */
1722 pos = outpos;
1723 outpos = pop();
1724 off = pos - outpos;
1725 if (offsetsize == 16) {
1726 spit((off>>8)&0xff);
1727 }
1728 spit(off&0xff);
1729 /* Restore output position */
1730 outpos = pos;
1731 break;
1732 case TOK_OCTAL:
1733 STATE(token->text, "TOK_OCTAL");
1734 if (token->text[1] != '#') {
1735 if (defining) {
1736 spit(16);
1737 emit("base");
1738 emit("!");
1739 } else
1740 base = TOK_OCTAL;
1741 } else {
1742 char *end;
1743 Cell value;
1744
1745 token = yylex();
1746 if (token == NULL) {
1747 (void)printf( "EOF after o#\n");
1748 return;
1749 }
1750 value = strtol(token->text, &end, 8);
1751 if (*end != 0) {
1752 (void)printf("Illegal number conversion:%s:%d: %s\n",
1753 infile, yylineno, yytext);
1754 exit(1);
1755 }
1756 /*
1757 * If this is a 64-bit value we need to store two literals
1758 * and issue a `lxjoin' to combine them. But that's a future
1759 * project.
1760 */
1761 emit("b(lit)");
1762 spit((value>>24)&0x0ff);
1763 spit((value>>16)&0x0ff);
1764 spit((value>>8)&0x0ff);
1765 spit(value&0x0ff);
1766 if ((value>>32) != value && (value>>32) != 0) {
1767 emit("b(lit)");
1768 spit((value>>56)&0x0ff);
1769 spit((value>>48)&0x0ff);
1770 spit((value>>40)&0x0ff);
1771 spit((value>>32)&0x0ff);
1772 emit("lxjoin");
1773 }
1774 }
1775 break;
1776 case TOK_OF:
1777 STATE(token->text, "TOK_OF");
1778 /*
1779 * Let's hope I get the semantics right.
1780 *
1781 * The `of' behaves almost the same as an
1782 * `if'. The difference is that `endof'
1783 * takes a branch offset to the associated
1784 * `endcase'. Here we will generate a temporary
1785 * offset of the `of' associated with the `endof'.
1786 * Then in `endcase' we should be pointing just
1787 * after the offset of the last `endof' so we
1788 * calculate the offset and thread our way backwards
1789 * searching for the previous `b(case)' or `b(endof)'.
1790 */
1791 emit("b(of)");
1792 push(outpos);
1793 if (offsetsize == 16) {
1794 spit(0);
1795 }
1796 spit(0); /* Place holder for later */
1797 break;
1798 case TOK_REPEAT:
1799 STATE(token->text, "TOK_REPEAT");
1800 emit("bbranch");
1801 pos = pop();
1802 off = pop();
1803 /* First the offset for the branch back to the begin */
1804 off -= outpos;
1805 if (offsetsize == 16) {
1806 spit((off>>8)&0xff);
1807 }
1808 spit(off&0xff);
1809 emit("b(>resolve)");
1810 /* Now point the offset of the while here. */
1811 off = outpos;
1812 outpos = pos;
1813 pos = off - pos;
1814 if (offsetsize == 16) {
1815 spit((pos>>8)&0xff);
1816 }
1817 spit(pos&0xff);
1818 /* Return to the end of the output */
1819 outpos = off;
1820 break;
1821 case TOK_THEN:
1822 STATE(token->text, "TOK_THEN");
1823 emit("b(>resolve)");
1824 pos = outpos;
1825 outpos = pop();
1826 off = pos - outpos;
1827 if (offsetsize == 16) {
1828 spit((off>>8)&0xff);
1829 }
1830 spit(off&0xff);
1831 outpos = pos;
1832 break;
1833 case TOK_TO:
1834 STATE(token->text, "TOK_TO");
1835 /* The next pass should tokenize the FCODE number */
1836 emit("b(to)");
1837 break;
1838 case TOK_UNTIL:
1839 STATE(token->text, "TOK_UNTIL");
1840 {
1841 int pos;
1842
1843 emit("b?branch");
1844 pos = pop();
1845 pos -= outpos;
1846 if (offsetsize == 16) {
1847 spit((pos>>8)&0xff);
1848 }
1849 spit(pos&0xff);
1850 }
1851 break;
1852 case TOK_VALUE:
1853 STATE(token->text, "TOK_VALUE");
1854
1855 token = yylex();
1856 if (token == NULL) {
1857 (void)printf( "EOF in value definition\n");
1858 return;
1859 }
1860
1861 /* Add new code to dictionary */
1862 fcode = malloc(sizeof(*fcode));
1863 fcode->num = nextfcode++;
1864 fcode->name = strdup(token->text);
1865 fadd(dictionary, fcode);
1866
1867 if (state == 0)
1868 emit("new-token");
1869 else {
1870 if (state == TOK_EXTERNAL)
1871 emit("external-token");
1872 else
1873 /* Here we have a choice of new-token or named-token */
1874 emit("named-token");
1875 sspit(token->text);
1876 }
1877 spit(fcode->num);
1878 emit("b(value)");
1879 break;
1880 case TOK_VARIABLE:
1881 STATE(token->text, "TOK_VARIABLE");
1882
1883 token = yylex();
1884 if (token == NULL) {
1885 (void)printf( "EOF in variable definition\n");
1886 return;
1887 }
1888
1889 /* Add new code to dictionary */
1890 fcode = malloc(sizeof(*fcode));
1891 fcode->num = nextfcode++;
1892 fcode->name = strdup(token->text);
1893 fadd(dictionary, fcode);
1894
1895 if (state == 0)
1896 emit("new-token");
1897 else {
1898 if (state == TOK_EXTERNAL)
1899 emit("external-token");
1900 else
1901 /* Here we have a choice of new-token or named-token */
1902 emit("named-token");
1903 sspit(token->text);
1904 }
1905 spit(fcode->num);
1906 emit("b(variable)");
1907 break;
1908 case TOK_WHILE:
1909 STATE(token->text, "TOK_WHILE");
1910 emit("b?branch");
1911 push(outpos);
1912 if (offsetsize == 16) {
1913 spit(0);
1914 }
1915 spit(0);
1916 break;
1917
1918 /* Tokenizer directives */
1919 case TOK_BEGTOK:
1920 STATE(token->text, "TOK_BEGTOK");
1921 tokenizer = 1;
1922 break;
1923 case TOK_EMIT_BYTE:
1924 STATE(token->text, "TOK_EMIT_BYTE");
1925 spit(pop());
1926 break;
1927 case TOK_ENDTOK:
1928 STATE(token->text, "TOK_ENDTOK");
1929 tokenizer = 0;
1930 break;
1931 case TOK_FLOAD:
1932 STATE(token->text, "TOK_FLOAD");
1933 /* Parse a different file for a while */
1934 token = yylex();
1935 if ((inf = fopen(token->text, "r")) == NULL) {
1936 (void)printf("%s: Could not open %s: %s\n",
1937 myname, token->text, strerror(errno));
1938 break;
1939 }
1940 inbuf = yy_create_buffer(inf, YY_BUF_SIZE);
1941 yy_switch_to_buffer(inbuf);
1942 {
1943 char *oldinfile = infile;
1944
1945 infile = token->text;
1946 tokenize(inbuf);
1947 infile = oldinfile;
1948 }
1949 yy_switch_to_buffer(input);
1950 yy_delete_buffer(inbuf);
1951 fclose(inf);
1952 break;
1953 case TOK_OTHER:
1954 STATE(token->text, "TOK_OTHER");
1955 if (apply_macros(input, token->text))
1956 break;
1957 if (emit(token->text)) {
1958 #if 0
1959 /*
1960 * Call an external command
1961 *
1962 * XXXXX assumes it will always find the command
1963 */
1964 sspit(token->text);
1965 emit("$find");
1966 emit("drop");
1967 emit("execute");
1968 #else
1969 (void)printf( "%s: undefined token `%s'\n",
1970 myname, token->text);
1971 fflush(stderr);
1972 exit(1);
1973 #endif
1974 }
1975 break;
1976 default:
1977 /* Nothing */ ;
1978 }
1979 }
1980 return;
1981 }
1982
1983 /*
1984 * print a tokenizer error message
1985 */
1986 void
1987 token_err(int lineno, char *infile, char *text, char *fmt, ...)
1988 {
1989 va_list ap;
1990
1991 va_start(ap, fmt);
1992 if (infile)
1993 (void)fprintf(stderr, "%s:%d: ", infile, lineno);
1994 if (fmt)
1995 (void)vfprintf(stderr, fmt, ap);
1996 fputc('\n', stderr);
1997 if (text)
1998 fprintf(stderr, "\t%s", text);
1999 va_end(ap);
2000 exit(1);
2001 }
2002
2003 /*
2004 * Lookup fcode string in dictionary and spit it out.
2005 *
2006 * Fcode must be in dictionary. No alias conversion done.
2007 */
2008 int
2009 emit(str)
2010 char *str;
2011 {
2012 struct fcode *code;
2013 if ((code = flookup( dictionary, str)))
2014 spit(code->num);
2015 #ifdef DEBUG
2016 if (debug > 1) {
2017 if (code)
2018 (void)printf( "emitting `%s'\n", code->name);
2019 else
2020 (void)printf( "emit: not found `%s'\n", str);
2021 }
2022 #endif
2023 return (code == NULL);
2024 }
2025
2026 /*
2027 * Spit out an integral value as a series of FCodes.
2028 *
2029 * It will spit out one zero byte or as many bytes as are
2030 * non-zero.
2031 */
2032 int
2033 spit(n)
2034 long n;
2035 {
2036 int count = 1;
2037
2038 if (n >> 8)
2039 count += spit(n >> 8);
2040 if (outpos >= outbufsiz) {
2041 while (outpos >= outbufsiz) outbufsiz += BUFCLICK;
2042 if (!(outbuf = realloc(outbuf, outbufsiz))) {
2043 (void)printf( "realloc of %ld bytes failed -- out of memory\n",
2044 (long)outbufsiz);
2045 exit(1);
2046 }
2047 }
2048 if (debug > 1) printf("spitting %2.2x\n", (unsigned char)n);
2049 outbuf[outpos++] = n;
2050 return (count);
2051 }
2052
2053 /*
2054 * Spit out an FCode string.
2055 */
2056 void
2057 sspit(s)
2058 char *s;
2059 {
2060 int len = strlen(s);
2061
2062 if (len > 255) {
2063 (void)printf( "string length %d too long\n", len);
2064 return;
2065 }
2066 #ifdef DEBUG
2067 if (debug > 1)
2068 (void)printf( "sspit: len %d str `%s'\n", len, s);
2069 #endif
2070 spit(len);
2071 while (*s)
2072 spit(*s++);
2073 }
2074
2075 int
2076 yywrap()
2077 {
2078 /* Always generate EOF */
2079 return (1);
2080 }
2081