#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: gsp_lex.c,v 1.10 2025/11/24 08:04:28 nia Exp $");
#endif
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include "gsp_ass.h"
#include "gsp_gram.h"
char *lineptr;
char idents[MAXLINE];
char *idptr;
extern YYSTYPE yylval;
int str_match(const char *, const char **);
const char *regnames[] = {
"A0", "A1", "A2", "A3", "A4", "A5", "A6", "A7",
"A8", "A9", "A10", "A11", "A12", "A13", "A14", "SP",
"B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7",
"B8", "B9", "B10", "B11", "B12", "B13", "B14", "B15",
NULL
};
short regnumbers[] = {
GSPA_A0+0, GSPA_A0+1, GSPA_A0+2, GSPA_A0+3,
GSPA_A0+4, GSPA_A0+5, GSPA_A0+6, GSPA_A0+7,
GSPA_A0+8, GSPA_A0+9, GSPA_A0+10, GSPA_A0+11,
GSPA_A0+12, GSPA_A0+13, GSPA_A0+14, GSPA_SP,
GSPA_B0+0, GSPA_B0+1, GSPA_B0+2, GSPA_B0+3,
GSPA_B0+4, GSPA_B0+5, GSPA_B0+6, GSPA_B0+7,
GSPA_B0+8, GSPA_B0+9, GSPA_B0+10, GSPA_B0+11,
GSPA_B0+12, GSPA_B0+13, GSPA_B0+14, GSPA_B0+15,
};
void
lex_init(char *lline)
{
lineptr = lline;
idptr = idents;
}
int
yylex()
{
int c, tok, x, len;
char *lp, *ip;
char *end;
lp = lineptr;
c = *lp;
while( c == ' ' || c == '\t' )
c = *++lp;
if( isalpha(c) || c == '_' || c == '.' ){
ip = lp;
do {
c = *++lp;
} while( isalnum(c) || c == '_' );
len = lp - ip;
if( len == 1 && *ip == '.' )
tok = '.';
else {
strncpy(idptr, ip, len);
idptr[len] = 0;
ucasify(idptr);
x = str_match(idptr, regnames);
if( x == -1 ){
strncpy(idptr, ip, len);
yylval.y_id = idptr;
idptr += len + 1;
tok = ID;
} else {
yylval.y_int = regnumbers[x];
tok = REGISTER;
}
}
} else if( c == '$' ){
++lp;
yylval.y_int = strtoul(lp, &end, 16);
if( end == lp )
perr("Bad number syntax");
else
lp = end;
tok = NUMBER;
} else if( isdigit(c) ){
yylval.y_int = strtoul(lp, &end, 0);
ip = lp;
lp = end;
c = *lp;
if( (c == 'f' || c == 'F' || c == 'b' || c == 'B')
&& !(isalnum((unsigned char)lp[1]) || lp[1] == '_') ){
c = toupper(c);
sprintf(idptr, "%ld%c", (long)yylval.y_int, c);
yylval.y_id = idptr;
idptr += strlen(idptr) + 1;
++lp;
tok = ID;
} else
tok = NUMBER;
} else if( c == '\n' ){
tok = 0;
} else if( c == ';' ){
while( *++lp != 0 )
;
tok = 0;
} else if( c == '"' ){
yylval.y_id = idptr;
while( (c = *++lp) != '"' && c != '\n' && c != 0 )
*idptr++ = c;
*idptr++ = 0;
if( c != '"' )
perr("Unterminated string");
else
++lp;
tok = STRING;
} else if( c == '<' && lp[1] == '<' ){
lp += 2;
tok = LEFT_SHIFT;
} else if( c == '>' && lp[1] == '>' ){
lp += 2;
tok = RIGHT_SHIFT;
} else {
if( c != 0 )
++lp;
tok = c;
}
lineptr = lp;
return tok;
}
void
ucasify(char *p)
{
int c;
for( ; (c = *p) != 0; p++ )
if( islower(c) )
*p = toupper(c);
}
int
str_match(const char *id, const char **names)
{
const char **np;
for( np = names; *np != NULL; ++np )
if( strcmp(id, *np) == 0 )
return np - names;
return -1;
}