%{
/* $NetBSD: fgen.l,v 1.41 2024/05/20 20:46:05 wiz Exp $ */
/* FLEX input for FORTH input file scanner */
/*
* Copyright (c) 1998 Eduardo Horvath.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
Specifications are as follows:
The function "yylex()" always returns a pointer to a structure:
struct tok {
int type;
char *text;
}
#define TOKEN struct tok
*/
#include <sys/cdefs.h>
#ifdef HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
static int debug = 0;
#define ASSERT if (debug) assert
#define STATE(y, x) do { if (debug) printf("%lx State %s: token `%s'\n", outpos, x, y); } while (0)
static int mark_fload = 0;
/*
* ASCII -> long int converter, eats `.'s
*/
#define strtol(x, y, z) cvt(x, y, z)
static Cell
cvt(const char *s, char **e, int base)
{
Cell v = 0;
int c, n = 0;
c = *s;
if (c == '-') { n = 1; s++; }
for (c = *s; (c = *s); s++) {
/* Ignore `.' */
if (c == '.')
continue;
if (c >= '0' && c <= '9')
c -= '0';
else if (c >= 'a' && c <= 'f')
c += 10 - 'a';
else if (c >= 'A' && c <= 'F')
c += 10 - 'A';
if (c >= base)
break;
v *= base;
v += c;
}
if (e)
*e = __UNCONST(s);
if (n)
return (-v);
return (v);
}
static int
depth(void)
{
return (parse_stack_ptr);
}
/*
* Insert fcode into dictionary.
*/
static int
fadd(struct fcode *dict, struct fcode *new)
{
int res = strcmp(dict->name, new->name);
new->type = FCODE;
ASSERT(dict->type == FCODE);
if (!res) {
/*
* Duplicate entry. Give the old name the new FCode
* number.
*/
dict->num = new->num;
return (0);
}
if (res < 0) {
if (dict->l)
return fadd(dict->l, new);
else {
if (debug > 5)
printf("fadd: new FCode `%s' is %lx\n",
new->name, new->num);
new->l = new->r = NULL;
dict->l = new;
}
} else {
if (dict->r)
return fadd(dict->r, new);
else {
if (debug > 5)
printf("fadd: new FCode `%s' is %lx\n",
new->name, new->num);
new->l = new->r = NULL;
dict->r = new;
}
}
return (1);
}
/*
* Look for a code in the dictionary.
*/
static struct fcode *
flookup(struct fcode *dict, const char *str)
{
int res;
if (!dict) return (dict);
res = strcmp(dict->name, str);
ASSERT(dict->type == FCODE);
if (debug > 5)
printf("flookup: `%s' and `%s' %s match\n",
str, dict->name, res?"don't":"do");
if (!res) return (dict);
if (res < 0)
return (flookup(dict->l, str));
else
return (flookup(dict->r, str));
}
/*
* Insert alias into macros.
*/
static int
aadd(struct macro *dict, struct macro *new)
{
int res = strcmp(dict->name, new->name);
new->type = MACRO;
ASSERT(dict->type == MACRO);
if (!res) {
/* Duplicate name. Replace the old macro */
dict->equiv = new->equiv;
/* We can't free the old equiv since it may be static data. */
return (0);
}
if (res < 0) {
if (dict->l)
return aadd(dict->l, new);
else {
new->l = new->r = NULL;
dict->l = new;
if (debug > 5)
printf("aadd: new alias `%s' to `%s'\n",
new->name, new->equiv);
}
} else {
if (dict->r)
return aadd(dict->r, new);
else {
new->l = new->r = NULL;
dict->r = new;
if (debug > 5)
printf("aadd: new alias `%s' to `%s'\n",
new->name, new->equiv);
}
}
return (1);
}
/*
* Look for a macro in the aliases.
*/
static struct macro *
alookup(struct macro *dict, const char *str)
{
int res;
if (!dict) return (dict);
ASSERT(dict->type == MACRO);
res = strcmp(dict->name, str);
if (!res) return (dict);
if (res < 0)
return (alookup(dict->l, str));
else
return (alookup(dict->r, str));
}
/*
* Bootstrap the dictionary and then install
* all the standard FCodes.
*/
static void
initdic(void)
{
struct fcode *code = fcodes;
struct macro *alias = macros;
/* Now calculate length and checksum and stick them in the header */
fheader->format = 0x08;
fheader->length = htonl(outpos);
fheader->checksum = 0;
for (i = sizeof(*fheader); i<outpos; i++)
fheader->checksum += (unsigned char)outbuf[i];
fheader->checksum = htons(fheader->checksum);
if ((outf = open(outfile, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1)
err(EXIT_FAILURE, "Cannot open `%s'", outfile);
while ((token = yylex()) != NULL) {
switch (token->type) {
case TOK_NUMBER:
STATE(token->text, "TOK_NUMBER");
{
char *end;
Cell value;
if (tokenizer) {
push(strtol(token->text, &end, 16));
break;
}
value = strtol(token->text, &end, numbase);
if (*end != 0)
token_err(yylineno, infile, yytext,
"illegal number conversion");
/*
* If this is a 64-bit value we need to store two literals
* and issue a `lxjoin' to combine them. But that's a future
* project.
*/
emit("b(lit)");
spit((value>>24)&0x0ff);
spit((value>>16)&0x0ff);
spit((value>>8)&0x0ff);
spit(value&0x0ff);
if ((value>>32) != value && (value>>32) != 0 &&
(value>>32) != -1) {
emit("b(lit)");
spit((value>>56)&0x0ff);
spit((value>>48)&0x0ff);
spit((value>>40)&0x0ff);
spit((value>>32)&0x0ff);
emit("lxjoin");
}
}
break;
case TOK_C_LIT:
STATE(token->text, "TOK_C_LIT");
emit("b(lit)");
spit(0);
spit(0);
spit(0);
spit(token->text[1]);
break;
case TOK_STRING_LIT:
STATE(token->text, "TOK_STRING_LIT:");
{
size_t len;
char *p = token->text;
++p; /* Skip the quote */
len = strlen(++p); /* Skip the 1st space */
case TOK_TOKENIZE:
STATE(token->text, "TOK_TOKENIZE");
/* The next pass should tokenize the FCODE number */
emit("b(')");
break;
case TOK_COMMENT:
STATE(token->text, "TOK_COMMENT:");
do {
off = input();
} while ((off != ')') && (off != '\n') &&
(off != EOF));
break;
case TOK_COLON:
STATE(token->text, "TOK_COLON:");
token = yylex();
if (token == NULL)
token_err(yylineno, infile, yytext,
"EOF in colon definition");
/* Add new code to dictionary */
fcode = emalloc(sizeof(*fcode));
fcode->num = nextfcode++;
fcode->name = estrdup(token->text);
if (!fadd(dictionary, fcode)) {
/* Duplicate definition. Free the memory. */
if (debug)
printf("%s: duplicate FCode\n",
token->text);
free(__UNCONST(fcode->name));
free(fcode);
}
if (debug)
printf("Adding %s to dictionary\n", token->text);
if (state == 0)
emit("new-token");
else {
if (state == TOK_EXTERNAL)
emit("external-token");
else
/* Here we have a choice of new-token or named-token */
emit("named-token");
sspit(token->text);
}
spit(fcode->num);
emit("b(:)");
last_token = fcode->name;
defining = 1;
break;
case TOK_SEMICOLON:
STATE(token->text, "TOK_SEMICOLON:");
emit("b(;)");
defining = 0;
if (depth()) {
token_err(yylineno, infile, NULL,
"Warning: stack depth %d at end of %s\n",
depth(), last_token);
}
last_token = "";
break;
/* These are special */
case TOK_AGAIN:
STATE(token->text, "TOK_AGAIN");
emit("bbranch");
pos = pop();
pos = pos - outpos;
offspit(pos);
break;
case TOK_ALIAS:
STATE(token->text, "TOK_ALIAS");
{
struct macro *alias;
token = yylex();
if (token == NULL) {
warnx("EOF in alias definition");
return;
}
if (token->type != TOK_OTHER) {
warnx("ENDCOMMENT aliasing weird token type %d",
token->type);
}
alias = emalloc(sizeof(*alias));
alias->name = estrdup(token->text);
token = yylex();
if (token == NULL) {
warnx("EOF in alias definition");
free(__UNCONST(alias->name));
free(alias);
return;
}
alias->equiv = estrdup(token->text);
if (!aadd(aliases, alias)) {
free(__UNCONST(alias->name));
free(alias);
}
}
break;
case TOK_GETTOKEN:
STATE(token->text, "TOK_GETTOKEN");
/* This is caused by ['] */
emit("b(')");
token = yylex();
if (token == NULL) {
warnx("EOF in [']");
return;
}
if ((fcode = flookup(dictionary, token->text)) == NULL)
errx(EXIT_FAILURE, "[']: %s not found",
token->text);
spit(fcode->num);
break;
case TOK_ASCII:
STATE(token->text, "TOK_ASCII");
token = yylex();
if (token == NULL)
errx(EXIT_FAILURE, "EOF after \"ascii\"");
emit("b(lit)");
spit(0);
spit(0);
spit(0);
spit(token->text[0]);
break;
case TOK_BEGIN:
STATE(token->text, "TOK_BEGIN");
emit("b(<mark)");
push(outpos);
break;
case TOK_BUFFER:
STATE(token->text, "TOK_BUFFER");
token = yylex();
if (token == NULL) {
warnx("EOF in colon definition");
return;
}
/* Add new code to dictionary */
fcode = emalloc(sizeof(*fcode));
fcode->num = nextfcode++;
fcode->name = estrdup(token->text);
fadd(dictionary, fcode);
if (state == 0)
emit("new-token");
else {
if (state == TOK_EXTERNAL)
emit("external-token");
else
/* Here we have a choice of new-token or named-token */
emit("named-token");
sspit(token->text);
}
spit(fcode->num);
emit("b(buffer:)");
break;
case TOK_CASE:
STATE(token->text, "TOK_CASE");
emit("b(case)");
push(0);
break;
case TOK_CONSTANT:
STATE(token->text, "TOK_CONSTANT");
token = yylex();
if (token == NULL) {
warnx("EOF in constant definition");
return;
}
/* Add new code to dictionary */
fcode = emalloc(sizeof(*fcode));
fcode->num = nextfcode++;
fcode->name = estrdup(token->text);
fadd(dictionary, fcode);
if (state == 0)
emit("new-token");
else {
if (state == TOK_EXTERNAL)
emit("external-token");
else
/* Here we have a choice of new-token or named-token */
emit("named-token");
sspit(token->text);
}
spit(fcode->num);
emit("b(constant)");
break;
case TOK_CONTROL:
STATE(token->text, "TOK_CONTROL");
token = yylex();
if (token == NULL)
errx(EXIT_FAILURE, "EOF after \"ascii\"");
emit("b(lit)");
spit(0);
spit(0);
spit(0);
spit(token->text[0]&0x1f);
break;
case TOK_CREATE:
STATE(token->text, "TOK_CREATE");
/* Don't know what this does or if it's right */
token = yylex();
if (token == NULL) {
warnx("EOF in create definition");
return;
}
/* Add new code to dictionary */
fcode = emalloc(sizeof(*fcode));
fcode->num = nextfcode++;
fcode->name = estrdup(token->text);
fadd(dictionary, fcode);
if (state == 0)
emit("new-token");
else {
if (state == TOK_EXTERNAL)
emit("external-token");
else
/* Here we have a choice of new-token or named-token */
emit("named-token");
sspit(token->text);
}
spit(fcode->num);
emit("b(create)");
break;
case TOK_DECIMAL:
STATE(token->text, "TOK_DECIMAL");
if (token->text[1] != '#') {
if (defining) {
emit("b(lit)");
spit(0);
spit(0);
spit(0);
spit(10);
emit("base");
emit("!");
} else
numbase = TOK_DECIMAL;
} else {
char *end;
Cell value;
token = yylex();
if (token == NULL) {
warnx("EOF after d#");
return;
}
if (token->type == TOK_OTHER) {
if (strcmp("-1", token->text) == 0) {
emit(token->text);
break;
}
}
value = strtol(token->text, &end, 10);
if (*end != 0)
token_err(yylineno, infile, NULL,
"Illegal number conversion: %s", token->text);
/*
* If this is a 64-bit value we need to store two literals
* and issue a `lxjoin' to combine them. But that's a future
* project.
*/
emit("b(lit)");
spit((value>>24)&0x0ff);
spit((value>>16)&0x0ff);
spit((value>>8)&0x0ff);
spit(value&0x0ff);
if ((value>>32) != value && (value>>32) != 0) {
emit("b(lit)");
spit((value>>56)&0x0ff);
spit((value>>48)&0x0ff);
spit((value>>40)&0x0ff);
spit((value>>32)&0x0ff);
emit("lxjoin");
}
}
break;
case TOK_DEFER:
STATE(token->text, "TOK_DEFER");
/* Don't know what this does or if it's right */
token = yylex();
if (token == NULL) {
warnx("EOF in colon definition");
return;
}
/* Add new code to dictionary */
fcode = emalloc(sizeof(*fcode));
fcode->num = nextfcode++;
fcode->name = estrdup(token->text);
fadd(dictionary, fcode);
if (state == 0)
emit("new-token");
else {
if (state == TOK_EXTERNAL)
emit("external-token");
else
/* Here we have a choice of new-token or named-token */
emit("named-token");
sspit(token->text);
}
spit(fcode->num);
emit("b(defer)");
break;
case TOK_DO:
STATE(token->text, "TOK_DO");
/*
* From the 1275 spec. B is branch location, T is branch target.
*
* b(do) offset1 ... b(loop) offset2 ...
* b(do) offset1 ... b(+loop) offset2 ...
* b(?do) offset1 ... b(loop) offset2 ...
* b(?do) offset1 ... b(+loop) offset2 ...
* ^ ^
* B1 ^ ^ T1
* T2 B2
*
* How we do this is we generate the b(do) or b(?do), spit out a
* zero offset while remembering b1 and t2. Then we call tokenize()
* to generate the body. When tokenize() finds a b(loop) or b(+loop),
* it generates the FCode and returns, with outpos at b2. We then
* calculate the offsets, put them in the right slots and finishup.
*/
if (token->text[0] == '?')
emit("b(?do)");
else
emit("b(do)");
push(outpos);
offspit(0); /* Place holder for later */
push(outpos);
break;
case TOK_END0:
STATE(token->text, "TOK_END0");
emit("end0");
/* Remember we already generated end0 */
need_end0 = 0;
break;
case TOK_ELSE:
STATE(token->text, "TOK_ELSE");
/* Get where we need to patch */
off = pop();
emit("bbranch");
/* Save where we are now. */
push(outpos);
offspit(0); /* Place holder for later */
emit("b(>resolve)");
/* Rewind and patch the if branch */
pos = outpos;
outpos = off;
off = pos - off;
offspit(off); /* Place holder for later */
/* revert to the end */
outpos = pos;
break;
case TOK_ENDCASE:
STATE(token->text, "TOK_ENDCASE:");
emit("b(endcase)");
pos = outpos; /* Remember where we need to branch to */
/* Thread our way backwards and install proper offsets */
off = pop();
while (off) {
int disp;
int next;
/* Move to this offset */
outpos = off;
/* Load next offset to process */
disp = (signed char)(outbuf[outpos]);
if (offsetsize == 16) {
disp = (disp << 8) |
(unsigned char)outbuf[outpos+1];
}
next = outpos + disp;
if (debug > -3)
printf("Next endof: %x at %x\n",
disp, next);
/* process this offset */
off = pos - outpos;
offspit(off);
if ((off = disp))
off = next;
}
outpos = pos;
break;
case TOK_ENDOF:
STATE(token->text, "TOK_ENDOF");
off = pop();
emit("b(endof)");
/*
* Save back pointer in the offset field so we can traverse
* the linked list and patch it in the endcase.
*/
pos = pop(); /* get position of prev link. */
push(outpos); /* save position of this link. */
if (pos)
/* save potision of prev link. */
offspit(pos - outpos);
else
/* This is the first statement */
offspit(0);
pos = outpos;
/* Now point the offset from b(of) here. */
outpos = off;
off = pos - off;
offspit(off);
/* Restore position */
outpos = pos;
break;
case TOK_EXTERNAL:
STATE(token->text, "TOK_EXTERNAL");
state = TOK_EXTERNAL;
break;
case TOK_FCODE_VERSION2:
/* This is actually a tokenizer directive. */
STATE(token->text, "TOK_FCODE_VERSION2");
offsetsize = 16;
pos = outpos;
outpos = 0;
emit("start1");
outpos = pos;
break;
case TOK_FCODE_END:
/*
* Another tokenizer directive.
*
* This should generate end0 and finish filling in
* the FCode header. But that's all done in main().
*/
STATE(token->text, "TOK_FCODE_END");
return;
case TOK_FIELD:
STATE(token->text, "TOK_FIELD");
token = yylex();
if (token == NULL) {
warnx("EOF in field definition");
return;
}
/* Add new code to dictionary */
fcode = emalloc(sizeof(*fcode));
fcode->num = nextfcode++;
fcode->name = estrdup(token->text);
fadd(dictionary, fcode);
if (state == 0)
emit("new-token");
else {
if (state == TOK_EXTERNAL)
emit("external-token");
else
/* Here we have a choice of new-token or named-token */
emit("named-token");
sspit(token->text);
}
spit(fcode->num);
emit("b(field)");
break;
token = yylex();
if (token == NULL) {
warnx("EOF after h#");
return;
}
value = strtol(token->text, &end, 16);
if (*end != 0)
errx(EXIT_FAILURE, "Illegal number"
" conversion:%s:%d: %s\n",
infile, yylineno, yytext);
/*
* If this is a 64-bit value we need to store two literals
* and issue a `lxjoin' to combine them. But that's a future
* project.
*/
emit("b(lit)");
spit((value>>24)&0x0ff);
spit((value>>16)&0x0ff);
spit((value>>8)&0x0ff);
spit(value&0x0ff);
if ((value>>32) != value && (value>>32) != 0) {
emit("b(lit)");
spit((value>>56)&0x0ff);
spit((value>>48)&0x0ff);
spit((value>>40)&0x0ff);
spit((value>>32)&0x0ff);
emit("lxjoin");
}
}
break;
case TOK_HEADERLESS:
STATE(token->text, "TOK_HEADERLESS");
state = 0;
break;
case TOK_HEADERS:
STATE(token->text, "TOK_HEADERS");
state = TOK_HEADERS;
break;
case TOK_IF:
STATE(token->text, "TOK_IF");
/*
* Similar to do but simpler since we only deal w/one branch.
*/
emit("b?branch");
push(outpos);
offspit(0); /* Place holder for later */
break;
case TOK_LEAVE:
STATE(token->text, "TOK_LEAVE");
emit("b(leave)");
break;
case TOK_LOOP:
STATE(token->text, "TOK_LOOP");
if (token->text[0] == '+')
emit("b(+loop)");
else
emit("b(loop)");
/* First do backwards branch of loop */
pos = pop();
off = pos - outpos;
offspit(off);
/* Now do forward branch of do */
pos = outpos;
outpos = pop();
off = pos - outpos;
spit(off);
/* Restore output position */
outpos = pos;
break;
case TOK_OCTAL:
STATE(token->text, "TOK_OCTAL");
if (token->text[1] != '#') {
if (defining) {
spit(16);
emit("base");
emit("!");
} else
numbase = TOK_OCTAL;
} else {
char *end;
Cell value;
token = yylex();
if (token == NULL) {
warnx("EOF after o#");
return;
}
value = strtol(token->text, &end, 8);
if (*end != 0) {
errx(EXIT_FAILURE, "Illegal number"
" conversion:%s:%d: %s\n",
infile, yylineno, yytext);
}
/*
* If this is a 64-bit value we need to store two literals
* and issue a `lxjoin' to combine them. But that's a future
* project.
*/
emit("b(lit)");
spit((value>>24)&0x0ff);
spit((value>>16)&0x0ff);
spit((value>>8)&0x0ff);
spit(value&0x0ff);
if ((value>>32) != value && (value>>32) != 0) {
emit("b(lit)");
spit((value>>56)&0x0ff);
spit((value>>48)&0x0ff);
spit((value>>40)&0x0ff);
spit((value>>32)&0x0ff);
emit("lxjoin");
}
}
break;
case TOK_OF:
STATE(token->text, "TOK_OF");
/*
* Let's hope I get the semantics right.
*
* The `of' behaves almost the same as an
* `if'. The difference is that `endof'
* takes a branch offset to the associated
* `endcase'. Here we will generate a temporary
* offset of the `of' associated with the `endof'.
* Then in `endcase' we should be pointing just
* after the offset of the last `endof' so we
* calculate the offset and thread our way backwards
* searching for the previous `b(case)' or `b(endof)'.
*/
emit("b(of)");
push(outpos);
offspit(0); /* Place holder for later */
break;
case TOK_OFFSET16:
STATE(token->text, "TOK_OFFSET16");
offsetsize = 16;
emit("offset16");
break;
case TOK_REPEAT:
STATE(token->text, "TOK_REPEAT");
emit("bbranch");
pos = pop();
off = pop();
/* First the offset for the branch back to the begin */
off -= outpos;
offspit(off);
emit("b(>resolve)");
/* Now point the offset of the while here. */
off = outpos;
outpos = pos;
pos = off - pos;
offspit(pos);
/* Return to the end of the output */
outpos = off;
break;
case TOK_STARTX:
/* Put a "startX" at addr 0. */
STATE(token->text, "TOK_FCODE_VERSION2");
offsetsize = 16;
pos = outpos;
outpos = 0;
emit(token->text);
outpos = pos;
break;
case TOK_THEN:
STATE(token->text, "TOK_THEN");
emit("b(>resolve)");
pos = outpos;
outpos = pop();
off = pos - outpos;
offspit(off);
outpos = pos;
break;
case TOK_TO:
STATE(token->text, "TOK_TO");
/* The next pass should tokenize the FCODE number */
emit("b(to)");
break;
case TOK_UNTIL:
STATE(token->text, "TOK_UNTIL");
emit("b?branch");
pos = pop();
pos -= outpos;
offspit(pos);
break;
case TOK_VALUE:
STATE(token->text, "TOK_VALUE");
token = yylex();
if (token == NULL) {
warnx("EOF in value definition");
return;
}
/* Add new code to dictionary */
fcode = emalloc(sizeof(*fcode));
fcode->num = nextfcode++;
fcode->name = estrdup(token->text);
fadd(dictionary, fcode);
if (state == 0)
emit("new-token");
else {
if (state == TOK_EXTERNAL)
emit("external-token");
else
/* Here we have a choice of new-token or named-token */
emit("named-token");
sspit(token->text);
}
spit(fcode->num);
emit("b(value)");
break;
case TOK_VARIABLE:
STATE(token->text, "TOK_VARIABLE");
token = yylex();
if (token == NULL) {
warnx("EOF in variable definition");
return;
}
/* Add new code to dictionary */
fcode = emalloc(sizeof(*fcode));
fcode->num = nextfcode++;
fcode->name = estrdup(token->text);
fadd(dictionary, fcode);
if (state == 0)
emit("new-token");
else {
if (state == TOK_EXTERNAL)
emit("external-token");
else
/* Here we have a choice of new-token or named-token */
emit("named-token");
sspit(token->text);
}
spit(fcode->num);
emit("b(variable)");
break;
case TOK_VERSION1:
/* This is actually a tokenizer directive. */
STATE(token->text, "TOK_FCODE_VERSION1");
offsetsize = 8;
pos = outpos;
outpos = 0;
emit("version1");
outpos = pos;
break;
case TOK_WHILE:
STATE(token->text, "TOK_WHILE");
emit("b?branch");
push(outpos);
offspit(0);
break;
/* Tokenizer directives */
case TOK_BEGTOK:
STATE(token->text, "TOK_BEGTOK");
tokenizer = 1;
break;
case TOK_EMIT_BYTE:
STATE(token->text, "TOK_EMIT_BYTE");
spit(pop());
break;
case TOK_ENDTOK:
STATE(token->text, "TOK_ENDTOK");
tokenizer = 0;
break;
case TOK_FLOAD:
{
char *oldinfile = infile;
STATE(token->text, "TOK_FLOAD");
/* Parse a different file for a while */
token = yylex();
if ((inf = fopen(token->text, "r")) == NULL) {
warn("Cannot open `%s'", token->text);
break;
}
infile = estrdup(token->text);
if (mark_fload) {
/*
* Insert commands to print out the
* filename into the instruction
* stream
*/
emit("b(\")");
sspit("fload-ing ");
emit("type");
emit("b(\")");
sspit(infile);
emit("type");
emit("cr");
emit(".s");
}
inbuf = yy_create_buffer(inf, YY_BUF_SIZE);
yy_switch_to_buffer(inbuf);
printf("======= fload file %s\n", infile);
tokenize(inbuf);
printf("======= done file %s\n", infile);
yy_switch_to_buffer(yinput);
yy_delete_buffer(inbuf);
fclose(inf);
if (mark_fload) {
/*
* Insert commands to print out the
* filename into the instruction
* stream
*/
emit("b(\")");
sspit("fload-ed ");
emit("type");
emit("b(\")");
sspit(infile);
emit("type");
emit("cr");
emit(".s");
emit("cr");
}
free(infile);
infile = oldinfile;
}
break;
case TOK_OTHER:
STATE(token->text, "TOK_OTHER");
if (apply_macros(yinput, token->text))
break;
if (emit(token->text)) {
#if 0
/*
* Call an external command
*
* XXXXX assumes it will always find the command
*/
sspit(token->text);
emit("$find");
emit("drop");
emit("execute");
#else
token_err(yylineno, infile, yytext,
"%s: undefined token `%s'\n",
__func__, token->text);
#endif
}
break;
default:
/* Nothing */ ;
}
}
return;
}
va_start(ap, fmt);
fprintf(stderr, "%s: ", getprogname());
if (file)
(void)fprintf(stderr, "%s,%d: ", file, lineno);
if (fmt)
(void)vfprintf(stderr, fmt, ap);
fputc('\n', stderr);
if (text)
fprintf(stderr, "\t%s", text);
va_end(ap);
exit(EXIT_FAILURE);
}
/*
* Lookup fcode string in dictionary and spit it out.
*
* Fcode must be in dictionary. No alias conversion done.
*/
static int
emit(const char *str)
{
struct fcode *code;
if ((code = flookup(dictionary, str)))
spit(code->num);
if (debug > 1) {
if (code)
printf("emitting `%s'\n", code->name);
else
printf("emit: not found `%s'\n", str);
}
return (code == NULL);
}
/*
* Spit out an integral value as a series of FCodes.
*
* It will spit out one zero byte or as many bytes as are
* non-zero.
*/
static int
spit(long n)
{
int count = 1;
/*
* Spit out an FCode string.
*/
static void
sspit(const char *s)
{
int len = strlen(s);
if (len > 255) {
warnx("string length %d too long", len);
return;
}
if (debug > 2)
printf("sspit: len %d str `%s'\n", len, s);
spit(len);
while (len--)
spit(*s++);
}
/*
* Spit out an offset. Offsets can be 8 or 16 bits.
* Bail if the value overflows. This is a little complicated since
* offsets can be negative numbers.
*/
static int
offspit(long n)
{
if (offsetsize == 16) {
volatile int16_t off16 = n;