Clang-format before editing.

This commit is contained in:
David Given 2018-09-10 22:25:14 +02:00
parent 185e910246
commit 6275896a11

View file

@ -19,12 +19,12 @@ static int instring(int);
static int inescape(void); static int inescape(void);
static int infbsym(const char*); static int infbsym(const char*);
int int yylex(void)
yylex(void)
{ {
int c, c0, c1; int c, c0, c1;
if (pass == PASS_1) { if (pass == PASS_1)
{
/* scan the input file */ /* scan the input file */
do do
c = nextchar(); c = nextchar();
@ -33,39 +33,54 @@ yylex(void)
c = inident(c); c = inident(c);
else if (isdigit(c)) else if (isdigit(c))
c = innumber(c); c = innumber(c);
else switch (c) { else
switch (c)
{
case '=': case '=':
case '<': case '<':
case '>': case '>':
case '|': case '|':
case '&': case '&':
c = induo(c); break; c = induo(c);
break;
case ASC_SQUO: case ASC_SQUO:
case ASC_DQUO: case ASC_DQUO:
c = instring(c); break; c = instring(c);
break;
case ASC_COMM: case ASC_COMM:
do do
c = nextchar(); c = nextchar();
while (c != '\n' && c != '\0'); while (c != '\n' && c != '\0');
break; break;
case CTRL('A'): case CTRL('A'):
c = CODE1; readcode(1); break; c = CODE1;
readcode(1);
break;
case CTRL('B'): case CTRL('B'):
c = CODE2; readcode(2); break; c = CODE2;
readcode(2);
break;
case CTRL('C'): case CTRL('C'):
c = CODE4; readcode(4); break; c = CODE4;
readcode(4);
break;
} }
/* produce the intermediate token file */ /* produce the intermediate token file */
if (c <= 0) if (c <= 0)
return (0); return (0);
if (c < 256) { if (c < 256)
{
putc(c, tempfile); putc(c, tempfile);
putc(0, tempfile); putc(0, tempfile);
} else { }
else
{
putval(c); putval(c);
} }
} else { }
else
{
/* read from intermediate token file */ /* read from intermediate token file */
c0 = getc(tempfile); c0 = getc(tempfile);
if (c0 == EOF) if (c0 == EOF)
@ -82,24 +97,28 @@ yylex(void)
return (c); return (c);
} }
void void putval(int c)
putval(int c)
{ {
valu_t v; valu_t v;
int n = 0; int n = 0;
char* p = 0; char* p = 0;
assert(c == (c & 0xffff)); assert(c == (c & 0xffff));
switch (c) { switch (c)
{
case CODE1: case CODE1:
n = 1; goto putnum; n = 1;
goto putnum;
case CODE2: case CODE2:
n = 2; goto putnum; n = 2;
goto putnum;
case CODE4: case CODE4:
n = 4; goto putnum; n = 4;
goto putnum;
case NUMBER: case NUMBER:
v = yylval.y_valu; v = yylval.y_valu;
for (n = 0; n < sizeof(v); n++) { for (n = 0; n < sizeof(v); n++)
{
if (v == 0) if (v == 0)
break; break;
v >>= 8; v >>= 8;
@ -116,17 +135,20 @@ putval(int c)
case IDENT: case IDENT:
case FBSYM: case FBSYM:
n = sizeof(item_t*); n = sizeof(item_t*);
p = (char *) &yylval.y_item; break; p = (char*)&yylval.y_item;
break;
#ifdef ASLD #ifdef ASLD
case MODULE: case MODULE:
n = sizeof(char*); n = sizeof(char*);
p = (char *) &yylval.y_strp; break; p = (char*)&yylval.y_strp;
break;
#endif #endif
case STRING: case STRING:
v = stringlen; v = stringlen;
putc(c, tempfile); putc(c, tempfile);
putc(c >> 8, tempfile); putc(c >> 8, tempfile);
for (n = 0; n < sizeof(v); n++) { for (n = 0; n < sizeof(v); n++)
{
if (v == 0) if (v == 0)
break; break;
v >>= 8; v >>= 8;
@ -152,7 +174,8 @@ putval(int c)
break; break;
default: default:
n = sizeof(word_t); n = sizeof(word_t);
p = (char *) &yylval.y_word; break; p = (char*)&yylval.y_word;
break;
} }
putc(c, tempfile); putc(c, tempfile);
putc(c >> 8, tempfile); putc(c >> 8, tempfile);
@ -160,33 +183,44 @@ putval(int c)
putc(*p++, tempfile); putc(*p++, tempfile);
} }
int int getval(int c)
getval(int c)
{ {
int n = 0; int n = 0;
valu_t v; valu_t v;
char* p = 0; char* p = 0;
switch (c) { switch (c)
{
case CODE1: case CODE1:
n = 1; goto getnum; n = 1;
goto getnum;
case CODE2: case CODE2:
n = 2; goto getnum; n = 2;
goto getnum;
case CODE4: case CODE4:
n = 4; goto getnum; n = 4;
goto getnum;
case NUMBER0: case NUMBER0:
c = NUMBER; goto getnum; c = NUMBER;
goto getnum;
case NUMBER1: case NUMBER1:
n = 1; c = NUMBER; goto getnum; n = 1;
c = NUMBER;
goto getnum;
case NUMBER2: case NUMBER2:
n = 2; c = NUMBER; goto getnum; n = 2;
c = NUMBER;
goto getnum;
case NUMBER3: case NUMBER3:
n = 3; c = NUMBER; goto getnum; n = 3;
c = NUMBER;
goto getnum;
case NUMBER: case NUMBER:
n = 4; n = 4;
getnum: getnum:
v = 0; v = 0;
while (--n >= 0) { while (--n >= 0)
{
v <<= 8; v <<= 8;
v |= getc(tempfile); v |= getc(tempfile);
} }
@ -195,17 +229,20 @@ getval(int c)
case IDENT: case IDENT:
case FBSYM: case FBSYM:
n = sizeof(item_t*); n = sizeof(item_t*);
p = (char *) &yylval.y_item; break; p = (char*)&yylval.y_item;
break;
#ifdef ASLD #ifdef ASLD
case MODULE: case MODULE:
n = sizeof(char*); n = sizeof(char*);
p = (char *) &yylval.y_strp; break; p = (char*)&yylval.y_strp;
break;
#endif #endif
case STRING: case STRING:
getval(getc(tempfile) + NUMBER0); getval(getc(tempfile) + NUMBER0);
stringlen = n = yylval.y_valu; stringlen = n = yylval.y_valu;
p = stringbuf; p = stringbuf;
p[n] = '\0'; break; p[n] = '\0';
break;
case OP_EQ: case OP_EQ:
case OP_NE: case OP_NE:
case OP_LE: case OP_LE:
@ -217,7 +254,8 @@ getval(int c)
break; break;
default: default:
n = sizeof(word_t); n = sizeof(word_t);
p = (char *) &yylval.y_word; break; p = (char*)&yylval.y_word;
break;
} }
while (--n >= 0) while (--n >= 0)
*p++ = getc(tempfile); *p++ = getc(tempfile);
@ -226,12 +264,12 @@ getval(int c)
/* ---------- lexical scan in pass 1 ---------- */ /* ---------- lexical scan in pass 1 ---------- */
int int nextchar(void)
nextchar(void)
{ {
int c; int c;
if (peekc != -1) { if (peekc != -1)
{
c = peekc; c = peekc;
peekc = -1; peekc = -1;
return (c); return (c);
@ -251,38 +289,43 @@ nextchar(void)
return (c); return (c);
} }
static void static void readcode(int n)
readcode(int n)
{ {
int c; int c;
yylval.y_valu = 0; yylval.y_valu = 0;
do { do
{
if ( if (
#ifdef ASLD #ifdef ASLD
(archmode && --archsize < 0) (archmode && --archsize < 0) ||
||
#endif #endif
(c = getc(input)) == EOF (c = getc(input)) == EOF)
)
fatal("unexpected EOF in compact input"); fatal("unexpected EOF in compact input");
yylval.y_valu <<= 8; yylval.y_valu <<= 8;
yylval.y_valu |= c; yylval.y_valu |= c;
} while (--n); } while (--n);
} }
static int static int induo(int c)
induo(int c)
{ {
static short duo[] = { static short duo[] = {
('='<<8) | '=', OP_EQ, ('=' << 8) | '=',
('<'<<8) | '>', OP_NE, OP_EQ,
('<'<<8) | '=', OP_LE, ('<' << 8) | '>',
('>'<<8) | '=', OP_GE, OP_NE,
('<'<<8) | '<', OP_LL, ('<' << 8) | '=',
('>'<<8) | '>', OP_RR, OP_LE,
('|'<<8) | '|', OP_OO, ('>' << 8) | '=',
('&'<<8) | '&', OP_AA, OP_GE,
('<' << 8) | '<',
OP_LL,
('>' << 8) | '>',
OP_RR,
('|' << 8) | '|',
OP_OO,
('&' << 8) | '&',
OP_AA,
0 /* terminates array */ 0 /* terminates array */
}; };
short* p; short* p;
@ -297,14 +340,14 @@ induo(int c)
static char name[NAMEMAX + 1]; static char name[NAMEMAX + 1];
static int static int inident(int c)
inident(int c)
{ {
char* p = name; char* p = name;
item_t* ip; item_t* ip;
int n = NAMEMAX; int n = NAMEMAX;
do { do
{
if (--n >= 0) if (--n >= 0)
*p++ = c; *p++ = c;
c = nextchar(); c = nextchar();
@ -312,13 +355,16 @@ inident(int c)
*p = '\0'; *p = '\0';
peekc = c; peekc = c;
ip = item_search(name); ip = item_search(name);
if (ip == 0) { if (ip == 0)
{
ip = item_alloc(S_UND); ip = item_alloc(S_UND);
ip->i_name = remember(name); ip->i_name = remember(name);
/* printf("ident %s %o\n", ip->i_name, ip); */ /* printf("ident %s %o\n", ip->i_name, ip); */
unresolved++; unresolved++;
item_insert(ip, H_LOCAL + (hashindex % H_SIZE)); item_insert(ip, H_LOCAL + (hashindex % H_SIZE));
} else if (hashindex < H_SIZE) { }
else if (hashindex < H_SIZE)
{
assert(H_KEY == 0); assert(H_KEY == 0);
yylval.y_word = (word_t)ip->i_valu; yylval.y_word = (word_t)ip->i_valu;
return (ip->i_type); return (ip->i_type);
@ -328,13 +374,13 @@ inident(int c)
} }
#ifdef ASLD #ifdef ASLD
char * char* readident(int c)
readident(int c)
{ {
int n = NAMEMAX; int n = NAMEMAX;
char* p = name; char* p = name;
do { do
{
if (--n >= 0) if (--n >= 0)
*p++ = c; *p++ = c;
c = nextchar(); c = nextchar();
@ -345,8 +391,7 @@ readident(int c)
} }
#endif #endif
static int static int innumber(int c)
innumber(int c)
{ {
char* p; char* p;
int radix; int radix;
@ -354,7 +399,8 @@ innumber(int c)
p = num; p = num;
radix = 20; radix = 20;
do { do
{
if (--radix < 0) if (--radix < 0)
fatal("number too long"); fatal("number too long");
if (isupper(c)) if (isupper(c))
@ -367,13 +413,17 @@ innumber(int c)
c = *--p; c = *--p;
p = num; p = num;
radix = 10; radix = 10;
if (*p == '0') { if (*p == '0')
{
radix = 8; radix = 8;
p++; p++;
if (*p == 'x') { if (*p == 'x')
{
radix = 16; radix = 16;
p++; p++;
} else if (*p == 'b') { }
else if (*p == 'b')
{
radix = 2; radix = 2;
p++; p++;
} }
@ -381,7 +431,8 @@ innumber(int c)
if (radix != 16 && (c == 'f' || c == 'b')) if (radix != 16 && (c == 'f' || c == 'b'))
return (infbsym(num)); return (infbsym(num));
yylval.y_valu = 0; yylval.y_valu = 0;
while (c = *p++) { while (c = *p++)
{
if (c > '9') if (c > '9')
c -= ('a' - '9' - 1); c -= ('a' - '9' - 1);
c -= '0'; c -= '0';
@ -392,23 +443,26 @@ innumber(int c)
return (NUMBER); return (NUMBER);
} }
static int static int instring(int termc)
instring(int termc)
{ {
char* p; char* p;
int c; int c;
static int maxstring = 0; static int maxstring = 0;
if (! maxstring) { if (!maxstring)
{
maxstring = STRINGMAX; maxstring = STRINGMAX;
if ((stringbuf = malloc(maxstring)) == 0) { if ((stringbuf = malloc(maxstring)) == 0)
{
fatal("out of memory"); fatal("out of memory");
} }
} }
p = stringbuf; p = stringbuf;
for (;;) { for (;;)
{
c = nextchar(); c = nextchar();
if (c == '\n' || c == '\0') { if (c == '\n' || c == '\0')
{
peekc = c; peekc = c;
serror("non-terminated string"); serror("non-terminated string");
break; break;
@ -417,10 +471,12 @@ instring(int termc)
break; break;
if (c == '\\') if (c == '\\')
c = inescape(); c = inescape();
if (p >= &stringbuf[maxstring - 1]) { if (p >= &stringbuf[maxstring - 1])
{
int cnt = p - stringbuf; int cnt = p - stringbuf;
if ((stringbuf = realloc(stringbuf, maxstring += 256)) == 0) { if ((stringbuf = realloc(stringbuf, maxstring += 256)) == 0)
{
fatal("out of memory"); fatal("out of memory");
} }
p = stringbuf + cnt; p = stringbuf + cnt;
@ -432,17 +488,19 @@ instring(int termc)
return (STRING); return (STRING);
} }
static int static int inescape(void)
inescape(void)
{ {
int c, j, r; int c, j, r;
c = nextchar(); c = nextchar();
if (c >= '0' && c <= '7') { if (c >= '0' && c <= '7')
{
r = c - '0'; r = c - '0';
for (j = 0; j < 2; j++) { for (j = 0; j < 2; j++)
{
c = nextchar(); c = nextchar();
if (c < '0' || c > '7') { if (c < '0' || c > '7')
{
peekc = c; peekc = c;
return (r); return (r);
} }
@ -451,29 +509,39 @@ inescape(void)
} }
return (r); return (r);
} }
switch (c) { switch (c)
case 'b': return('\b'); {
case 'f': return('\f'); case 'b':
case 'n': return('\n'); return ('\b');
case 'r': return('\r'); case 'f':
case 't': return('\t'); return ('\f');
case '\'': return('\''); case 'n':
case '"': return('"'); return ('\n');
case 'r':
return ('\r');
case 't':
return ('\t');
case '\'':
return ('\'');
case '"':
return ('"');
} }
return (c); return (c);
} }
static int static int infbsym(const char* p)
infbsym(const char *p)
{ {
int lab; int lab;
item_t* ip; item_t* ip;
lab = *p++ - '0'; lab = *p++ - '0';
if ((unsigned)lab < 10) { if ((unsigned)lab < 10)
if (*p++ == 'f') { {
if (*p++ == 'f')
{
ip = fb_ptr[FB_FORW + lab]; ip = fb_ptr[FB_FORW + lab];
if (ip == 0) { if (ip == 0)
{
ip = fb_alloc(lab); ip = fb_alloc(lab);
fb_ptr[FB_FORW + lab] = ip; fb_ptr[FB_FORW + lab] = ip;
} }
@ -490,29 +558,30 @@ ok:
return (FBSYM); return (FBSYM);
} }
int int hash(const char* p)
hash(const char *p)
{ {
unsigned short h; unsigned short h;
int c; int c;
h = 0; h = 0;
while (c = *p++) { while (c = *p++)
{
h <<= 2; h <<= 2;
h += c; h += c;
} }
return (h % H_SIZE); return (h % H_SIZE);
} }
item_t * item_t* item_search(const char* p)
item_search(const char *p)
{ {
int h; int h;
item_t* ip; item_t* ip;
for (h = hash(p); h < H_TOTAL; h += H_SIZE) { for (h = hash(p); h < H_TOTAL; h += H_SIZE)
{
ip = hashtab[h]; ip = hashtab[h];
while (ip != 0) { while (ip != 0)
{
if (strcmp(p, ip->i_name) == 0) if (strcmp(p, ip->i_name) == 0)
goto done; goto done;
ip = ip->i_next; ip = ip->i_next;
@ -523,21 +592,20 @@ done:
return (ip); return (ip);
} }
void void item_insert(item_t* ip, int h)
item_insert(item_t *ip, int h)
{ {
ip->i_next = hashtab[h]; ip->i_next = hashtab[h];
hashtab[h] = ip; hashtab[h] = ip;
} }
item_t * item_t* item_alloc(int typ)
item_alloc(int typ)
{ {
item_t* ip; item_t* ip;
static int nleft = 0; static int nleft = 0;
static item_t* next; static item_t* next;
if (--nleft < 0) { if (--nleft < 0)
{
next = (item_t*)malloc(MEMINCR); next = (item_t*)malloc(MEMINCR);
if (next == 0) if (next == 0)
fatal("out of memory"); fatal("out of memory");
@ -551,8 +619,7 @@ item_alloc(int typ)
return (ip); return (ip);
} }
item_t * item_t* fb_alloc(int lab)
fb_alloc(int lab)
{ {
item_t *ip, *p; item_t *ip, *p;
@ -566,8 +633,7 @@ fb_alloc(int lab)
return (ip); return (ip);
} }
item_t * item_t* fb_shift(int lab)
fb_shift(int lab)
{ {
item_t* ip; item_t* ip;