From ebac9899fe2ef7a672d3728071b3a4179ec5a2df Mon Sep 17 00:00:00 2001 From: pixel Date: Thu, 20 Nov 2003 07:37:10 +0000 Subject: Adding Hexa and Octal syntax to lua's numbers --- lib/lua/src/llex.c | 29 +++++++++++++++++++++++++++-- lib/lua/src/lobject.c | 21 ++++++++++++--------- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/lib/lua/src/llex.c b/lib/lua/src/llex.c index e3aea33..c859376 100644 --- a/lib/lua/src/llex.c +++ b/lib/lua/src/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 1.1 2003-11-06 11:56:07 pixel Exp $ +** $Id: llex.c,v 1.2 2003-11-20 07:37:10 pixel Exp $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -172,15 +172,34 @@ static size_t readname (LexState *LS) { /* LUA_NUMBER */ static void read_numeral (LexState *LS, int comma, SemInfo *seminfo) { + int oct = 0, hex = 0; size_t l = 0; checkbuffer(LS, l); if (comma) save(LS, '.', l); - while (isdigit(LS->current)) { + else if (LS->current == '0') { + oct = 1; + checkbuffer(LS, 1); + save_and_next(LS, l); + if (LS->current == 'x') { + oct = 0; + hex = 1; + checkbuffer(LS, 1); + save_and_next(LS, l); + } + } + while (isdigit(LS->current) || (hex && isxdigit(LS->current))) { checkbuffer(LS, l); save_and_next(LS, l); } + checkbuffer(LS, 1); if (LS->current == '.') { save_and_next(LS, l); + if (hex || oct) { + save(LS, '\0', l); + luaX_lexerror(LS, + "error in number, mixing decimal point with octal or hexadecimal", + TK_NUMBER); + } if (LS->current == '.') { save_and_next(LS, l); save(LS, '\0', l); @@ -195,6 +214,12 @@ static void read_numeral (LexState *LS, int comma, SemInfo *seminfo) { } if (LS->current == 'e' || LS->current == 'E') { save_and_next(LS, l); /* read `E' */ + if (hex || oct) { + save(LS, '\0', l); + luaX_lexerror(LS, + "error in number, mixing exponential with octal or hexadecimal", + TK_NUMBER); + } if (LS->current == '+' || LS->current == '-') save_and_next(LS, l); /* optional exponent sign */ while (isdigit(LS->current)) { diff --git a/lib/lua/src/lobject.c b/lib/lua/src/lobject.c index 57a9477..7a3908c 100644 --- a/lib/lua/src/lobject.c +++ b/lib/lua/src/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 1.1 2003-11-06 11:56:07 pixel Exp $ +** $Id: lobject.c,v 1.2 2003-11-20 07:37:10 pixel Exp $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -20,13 +20,6 @@ #include "lstring.h" #include "lvm.h" - -/* function to convert a string to a lua_Number */ -#ifndef lua_str2number -#define lua_str2number(s,p) strtod((s), (p)) -#endif - - const TObject luaO_nilobject = {LUA_TNIL, {NULL}}; @@ -91,7 +84,17 @@ int luaO_rawequalObj (const TObject *t1, const TObject *t2) { int luaO_str2d (const char *s, lua_Number *result) { char *endptr; - lua_Number res = lua_str2number(s, &endptr); + size_t l = strlen(s); + lua_Number res; + if ((l > 0) && (s[0] == '0')) { + if ((l > 2) && (s[1] == 'x')) { + res = strtol(s + 2, &endptr, 16); + } else { + res = strtol(s + 1, &endptr, 8); + } + } else { + res = strtod(s, &endptr); + } if (endptr == s) return 0; /* no conversion */ while (isspace((unsigned char)(*endptr))) endptr++; if (*endptr != '\0') return 0; /* invalid trailing characters? */ -- cgit v1.2.3