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