new function "luaO_str2d" to convert strings to numbers, because
old "lex" algorithm had aproximation errors, but strtod (and atof and scanf) are too slow.
This commit is contained in:
67
llex.c
67
llex.c
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
** $Id: llex.c,v 1.24 1998/07/24 18:02:38 roberto Exp roberto $
|
||||
** $Id: llex.c,v 1.25 1998/12/03 15:45:15 roberto Exp $
|
||||
** Lexical Analizer
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@@ -46,7 +46,7 @@ void luaX_init (void)
|
||||
|
||||
|
||||
void luaX_syntaxerror (LexState *ls, char *s, char *token) {
|
||||
if (token[0] == 0)
|
||||
if (token[0] == '\0')
|
||||
token = "<eof>";
|
||||
luaL_verror("%.100s;\n last token read: `%.50s' at line %d in chunk `%.50s'",
|
||||
s, token, ls->linenumber, zname(ls->lex_z));
|
||||
@@ -54,7 +54,7 @@ void luaX_syntaxerror (LexState *ls, char *s, char *token) {
|
||||
|
||||
|
||||
void luaX_error (LexState *ls, char *s) {
|
||||
save(0);
|
||||
save('\0');
|
||||
luaX_syntaxerror(ls, s, luaL_buffer());
|
||||
}
|
||||
|
||||
@@ -62,7 +62,7 @@ void luaX_error (LexState *ls, char *s) {
|
||||
void luaX_token2str (int token, char *s) {
|
||||
if (token < 255) {
|
||||
s[0] = token;
|
||||
s[1] = 0;
|
||||
s[1] = '\0';
|
||||
}
|
||||
else
|
||||
strcpy(s, reserved[token-FIRST_RESERVED]);
|
||||
@@ -221,6 +221,7 @@ static void inclinenumber (LexState *LS)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** =======================================================
|
||||
** LEXICAL ANALIZER
|
||||
@@ -229,10 +230,7 @@ static void inclinenumber (LexState *LS)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
static int read_long_string (LexState *LS)
|
||||
{
|
||||
static int read_long_string (LexState *LS) {
|
||||
int cont = 0;
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
@@ -262,7 +260,7 @@ static int read_long_string (LexState *LS)
|
||||
save_and_next(LS);
|
||||
}
|
||||
} endloop:
|
||||
save_and_next(LS); /* pass the second ']' */
|
||||
save_and_next(LS); /* skip the second ']' */
|
||||
LS->seminfo.ts = luaS_newlstr(L->Mbuffbase+2,
|
||||
L->Mbuffnext-(L->Mbuffbase-L->Mbuffer)-4);
|
||||
return STRING;
|
||||
@@ -270,7 +268,6 @@ static int read_long_string (LexState *LS)
|
||||
|
||||
|
||||
int luaX_lex (LexState *LS) {
|
||||
double a;
|
||||
luaL_resetbuffer();
|
||||
for (;;) {
|
||||
switch (LS->current) {
|
||||
@@ -347,7 +344,7 @@ int luaX_lex (LexState *LS) {
|
||||
c = 10*c + (LS->current-'0');
|
||||
next(LS);
|
||||
} while (++i<3 && isdigit(LS->current));
|
||||
if (c >= 256)
|
||||
if (c > (unsigned char)c)
|
||||
luaX_error(LS, "escape sequence too large");
|
||||
save(c);
|
||||
}
|
||||
@@ -382,15 +379,11 @@ int luaX_lex (LexState *LS) {
|
||||
else return CONC; /* .. */
|
||||
}
|
||||
else if (!isdigit(LS->current)) return '.';
|
||||
/* LS->current is a digit: goes through to number */
|
||||
a=0.0;
|
||||
goto fraction;
|
||||
goto fraction; /* LS->current is a digit: goes through to number */
|
||||
|
||||
case '0': case '1': case '2': case '3': case '4':
|
||||
case '5': case '6': case '7': case '8': case '9':
|
||||
a=0.0;
|
||||
do {
|
||||
a = 10.0*a + (LS->current-'0');
|
||||
save_and_next(LS);
|
||||
} while (isdigit(LS->current));
|
||||
if (LS->current == '.') {
|
||||
@@ -402,35 +395,19 @@ int luaX_lex (LexState *LS) {
|
||||
}
|
||||
}
|
||||
fraction:
|
||||
{ double da=0.1;
|
||||
while (isdigit(LS->current))
|
||||
{
|
||||
a += (LS->current-'0')*da;
|
||||
da /= 10.0;
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(LS);
|
||||
if (toupper(LS->current) == 'E') {
|
||||
save_and_next(LS); /* read 'E' */
|
||||
save_and_next(LS); /* read '+', '-' or first digit */
|
||||
while (isdigit(LS->current))
|
||||
save_and_next(LS);
|
||||
}
|
||||
if (toupper(LS->current) == 'E') {
|
||||
int e = 0;
|
||||
int neg;
|
||||
double ea;
|
||||
save_and_next(LS);
|
||||
neg = (LS->current=='-');
|
||||
if (LS->current == '+' || LS->current == '-') save_and_next(LS);
|
||||
if (!isdigit(LS->current))
|
||||
luaX_error(LS, "invalid numeral format");
|
||||
do {
|
||||
e = 10*e + (LS->current-'0');
|
||||
save_and_next(LS);
|
||||
} while (isdigit(LS->current));
|
||||
for (ea=neg?0.1:10.0; e>0; e>>=1)
|
||||
{
|
||||
if (e & 1) a *= ea;
|
||||
ea *= ea;
|
||||
}
|
||||
}
|
||||
LS->seminfo.r = a;
|
||||
return NUMBER;
|
||||
}
|
||||
save('\0');
|
||||
LS->seminfo.r = luaO_str2d(L->Mbuffbase);
|
||||
if (LS->seminfo.r < 0)
|
||||
luaX_error(LS, "invalid numeric format");
|
||||
return NUMBER;
|
||||
|
||||
case EOZ:
|
||||
if (LS->iflevel > 0)
|
||||
@@ -450,9 +427,9 @@ int luaX_lex (LexState *LS) {
|
||||
do {
|
||||
save_and_next(LS);
|
||||
} while (isalnum(LS->current) || LS->current == '_');
|
||||
save(0);
|
||||
save('\0');
|
||||
ts = luaS_new(L->Mbuffbase);
|
||||
if (ts->head.marked >= 'A')
|
||||
if (ts->head.marked >= FIRST_RESERVED)
|
||||
return ts->head.marked; /* reserved word */
|
||||
LS->seminfo.ts = ts;
|
||||
return NAME;
|
||||
|
||||
Reference in New Issue
Block a user