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