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