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