diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-12-27 18:25:20 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1998-12-27 18:25:20 -0200 |
| commit | 4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea (patch) | |
| tree | fd95bdb49c335389a222547b072b60d04d23043f /llex.c | |
| parent | d2de2d5eda5779832a6e6ce4de1f1d8aa4f01047 (diff) | |
| download | lua-4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea.tar.gz lua-4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea.tar.bz2 lua-4c94d8cc2cbeac74ae3618b1322c3f3d3ec166ea.zip | |
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.
Diffstat (limited to 'llex.c')
| -rw-r--r-- | llex.c | 67 |
1 files changed, 22 insertions, 45 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: llex.c,v 1.24 1998/07/24 18:02:38 roberto Exp roberto $ | 2 | ** $Id: llex.c,v 1.25 1998/12/03 15:45:15 roberto Exp $ |
| 3 | ** Lexical Analizer | 3 | ** Lexical Analizer |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -46,7 +46,7 @@ void luaX_init (void) | |||
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | void luaX_syntaxerror (LexState *ls, char *s, char *token) { | 48 | void luaX_syntaxerror (LexState *ls, char *s, char *token) { |
| 49 | if (token[0] == 0) | 49 | if (token[0] == '\0') |
| 50 | token = "<eof>"; | 50 | token = "<eof>"; |
| 51 | luaL_verror("%.100s;\n last token read: `%.50s' at line %d in chunk `%.50s'", | 51 | luaL_verror("%.100s;\n last token read: `%.50s' at line %d in chunk `%.50s'", |
| 52 | s, token, ls->linenumber, zname(ls->lex_z)); | 52 | s, token, ls->linenumber, zname(ls->lex_z)); |
| @@ -54,7 +54,7 @@ void luaX_syntaxerror (LexState *ls, char *s, char *token) { | |||
| 54 | 54 | ||
| 55 | 55 | ||
| 56 | void luaX_error (LexState *ls, char *s) { | 56 | void luaX_error (LexState *ls, char *s) { |
| 57 | save(0); | 57 | save('\0'); |
| 58 | luaX_syntaxerror(ls, s, luaL_buffer()); | 58 | luaX_syntaxerror(ls, s, luaL_buffer()); |
| 59 | } | 59 | } |
| 60 | 60 | ||
| @@ -62,7 +62,7 @@ void luaX_error (LexState *ls, char *s) { | |||
| 62 | void luaX_token2str (int token, char *s) { | 62 | void luaX_token2str (int token, char *s) { |
| 63 | if (token < 255) { | 63 | if (token < 255) { |
| 64 | s[0] = token; | 64 | s[0] = token; |
| 65 | s[1] = 0; | 65 | s[1] = '\0'; |
| 66 | } | 66 | } |
| 67 | else | 67 | else |
| 68 | strcpy(s, reserved[token-FIRST_RESERVED]); | 68 | strcpy(s, reserved[token-FIRST_RESERVED]); |
| @@ -221,6 +221,7 @@ static void inclinenumber (LexState *LS) | |||
| 221 | } | 221 | } |
| 222 | 222 | ||
| 223 | 223 | ||
| 224 | |||
| 224 | /* | 225 | /* |
| 225 | ** ======================================================= | 226 | ** ======================================================= |
| 226 | ** LEXICAL ANALIZER | 227 | ** LEXICAL ANALIZER |
| @@ -229,10 +230,7 @@ static void inclinenumber (LexState *LS) | |||
| 229 | 230 | ||
| 230 | 231 | ||
| 231 | 232 | ||
| 232 | 233 | static int read_long_string (LexState *LS) { | |
| 233 | |||
| 234 | static int read_long_string (LexState *LS) | ||
| 235 | { | ||
| 236 | int cont = 0; | 234 | int cont = 0; |
| 237 | for (;;) { | 235 | for (;;) { |
| 238 | switch (LS->current) { | 236 | switch (LS->current) { |
| @@ -262,7 +260,7 @@ static int read_long_string (LexState *LS) | |||
| 262 | save_and_next(LS); | 260 | save_and_next(LS); |
| 263 | } | 261 | } |
| 264 | } endloop: | 262 | } endloop: |
| 265 | save_and_next(LS); /* pass the second ']' */ | 263 | save_and_next(LS); /* skip the second ']' */ |
| 266 | LS->seminfo.ts = luaS_newlstr(L->Mbuffbase+2, | 264 | LS->seminfo.ts = luaS_newlstr(L->Mbuffbase+2, |
| 267 | L->Mbuffnext-(L->Mbuffbase-L->Mbuffer)-4); | 265 | L->Mbuffnext-(L->Mbuffbase-L->Mbuffer)-4); |
| 268 | return STRING; | 266 | return STRING; |
| @@ -270,7 +268,6 @@ static int read_long_string (LexState *LS) | |||
| 270 | 268 | ||
| 271 | 269 | ||
| 272 | int luaX_lex (LexState *LS) { | 270 | int luaX_lex (LexState *LS) { |
| 273 | double a; | ||
| 274 | luaL_resetbuffer(); | 271 | luaL_resetbuffer(); |
| 275 | for (;;) { | 272 | for (;;) { |
| 276 | switch (LS->current) { | 273 | switch (LS->current) { |
| @@ -347,7 +344,7 @@ int luaX_lex (LexState *LS) { | |||
| 347 | c = 10*c + (LS->current-'0'); | 344 | c = 10*c + (LS->current-'0'); |
| 348 | next(LS); | 345 | next(LS); |
| 349 | } while (++i<3 && isdigit(LS->current)); | 346 | } while (++i<3 && isdigit(LS->current)); |
| 350 | if (c >= 256) | 347 | if (c > (unsigned char)c) |
| 351 | luaX_error(LS, "escape sequence too large"); | 348 | luaX_error(LS, "escape sequence too large"); |
| 352 | save(c); | 349 | save(c); |
| 353 | } | 350 | } |
| @@ -382,15 +379,11 @@ int luaX_lex (LexState *LS) { | |||
| 382 | else return CONC; /* .. */ | 379 | else return CONC; /* .. */ |
| 383 | } | 380 | } |
| 384 | else if (!isdigit(LS->current)) return '.'; | 381 | else if (!isdigit(LS->current)) return '.'; |
| 385 | /* LS->current is a digit: goes through to number */ | 382 | goto fraction; /* LS->current is a digit: goes through to number */ |
| 386 | a=0.0; | ||
| 387 | goto fraction; | ||
| 388 | 383 | ||
| 389 | case '0': case '1': case '2': case '3': case '4': | 384 | case '0': case '1': case '2': case '3': case '4': |
| 390 | case '5': case '6': case '7': case '8': case '9': | 385 | case '5': case '6': case '7': case '8': case '9': |
| 391 | a=0.0; | ||
| 392 | do { | 386 | do { |
| 393 | a = 10.0*a + (LS->current-'0'); | ||
| 394 | save_and_next(LS); | 387 | save_and_next(LS); |
| 395 | } while (isdigit(LS->current)); | 388 | } while (isdigit(LS->current)); |
| 396 | if (LS->current == '.') { | 389 | if (LS->current == '.') { |
| @@ -402,35 +395,19 @@ int luaX_lex (LexState *LS) { | |||
| 402 | } | 395 | } |
| 403 | } | 396 | } |
| 404 | fraction: | 397 | fraction: |
| 405 | { double da=0.1; | 398 | while (isdigit(LS->current)) |
| 406 | while (isdigit(LS->current)) | 399 | save_and_next(LS); |
| 407 | { | 400 | if (toupper(LS->current) == 'E') { |
| 408 | a += (LS->current-'0')*da; | 401 | save_and_next(LS); /* read 'E' */ |
| 409 | da /= 10.0; | 402 | save_and_next(LS); /* read '+', '-' or first digit */ |
| 410 | save_and_next(LS); | 403 | while (isdigit(LS->current)) |
| 411 | } | ||
| 412 | if (toupper(LS->current) == 'E') { | ||
| 413 | int e = 0; | ||
| 414 | int neg; | ||
| 415 | double ea; | ||
| 416 | save_and_next(LS); | 404 | save_and_next(LS); |
| 417 | neg = (LS->current=='-'); | ||
| 418 | if (LS->current == '+' || LS->current == '-') save_and_next(LS); | ||
| 419 | if (!isdigit(LS->current)) | ||
| 420 | luaX_error(LS, "invalid numeral format"); | ||
| 421 | do { | ||
| 422 | e = 10*e + (LS->current-'0'); | ||
| 423 | save_and_next(LS); | ||
| 424 | } while (isdigit(LS->current)); | ||
| 425 | for (ea=neg?0.1:10.0; e>0; e>>=1) | ||
| 426 | { | ||
| 427 | if (e & 1) a *= ea; | ||
| 428 | ea *= ea; | ||
| 429 | } | ||
| 430 | } | ||
| 431 | LS->seminfo.r = a; | ||
| 432 | return NUMBER; | ||
| 433 | } | 405 | } |
| 406 | save('\0'); | ||
| 407 | LS->seminfo.r = luaO_str2d(L->Mbuffbase); | ||
| 408 | if (LS->seminfo.r < 0) | ||
| 409 | luaX_error(LS, "invalid numeric format"); | ||
| 410 | return NUMBER; | ||
| 434 | 411 | ||
| 435 | case EOZ: | 412 | case EOZ: |
| 436 | if (LS->iflevel > 0) | 413 | if (LS->iflevel > 0) |
| @@ -450,9 +427,9 @@ int luaX_lex (LexState *LS) { | |||
| 450 | do { | 427 | do { |
| 451 | save_and_next(LS); | 428 | save_and_next(LS); |
| 452 | } while (isalnum(LS->current) || LS->current == '_'); | 429 | } while (isalnum(LS->current) || LS->current == '_'); |
| 453 | save(0); | 430 | save('\0'); |
| 454 | ts = luaS_new(L->Mbuffbase); | 431 | ts = luaS_new(L->Mbuffbase); |
| 455 | if (ts->head.marked >= 'A') | 432 | if (ts->head.marked >= FIRST_RESERVED) |
| 456 | return ts->head.marked; /* reserved word */ | 433 | return ts->head.marked; /* reserved word */ |
| 457 | LS->seminfo.ts = ts; | 434 | LS->seminfo.ts = ts; |
| 458 | return NAME; | 435 | return NAME; |
