diff --git a/lcode.c b/lcode.c index 5ff659d0..8e0b443d 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 2.62 2012/08/16 17:34:28 roberto Exp $ +** $Id: lcode.c,v 2.63 2013/04/15 15:43:34 roberto Exp roberto $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -30,7 +30,7 @@ static int isnumeral(expdesc *e) { - return (e->k == VKNUM && e->t == NO_JUMP && e->f == NO_JUMP); + return (e->k == VKFLT && e->t == NO_JUMP && e->f == NO_JUMP); } @@ -322,6 +322,13 @@ int luaK_stringK (FuncState *fs, TString *s) { } +static int luaK_intK (FuncState *fs, lua_Integer n) { + TValue o; + setivalue(&o, n); + return addk(fs, &o, &o); +} + + int luaK_numberK (FuncState *fs, lua_Number r) { int n; lua_State *L = fs->ls->L; @@ -333,8 +340,11 @@ int luaK_numberK (FuncState *fs, lua_Number r) { n = addk(fs, L->top - 1, &o); L->top--; } - else - n = addk(fs, &o, &o); /* regular case */ + else { + TValue k; + setnvalue(&k, r + 0.5); /* ???? (avoid some collisions with ints) */ + n = addk(fs, &k, &o); /* regular case */ + } return n; } @@ -432,10 +442,14 @@ static void discharge2reg (FuncState *fs, expdesc *e, int reg) { luaK_codek(fs, reg, e->u.info); break; } - case VKNUM: { + case VKFLT: { luaK_codek(fs, reg, luaK_numberK(fs, e->u.nval)); break; } + case VKINT: { + luaK_codek(fs, reg, luaK_intK(fs, e->u.ival)); + break; + } case VRELOCABLE: { Instruction *pc = &getcode(fs, e); SETARG_A(*pc, reg); @@ -537,12 +551,18 @@ int luaK_exp2RK (FuncState *fs, expdesc *e) { } else break; } - case VKNUM: { + case VKINT: { + e->u.info = luaK_intK(fs, e->u.ival); + e->k = VK; + goto vk; + } + case VKFLT: { e->u.info = luaK_numberK(fs, e->u.nval); e->k = VK; /* go through */ } case VK: { + vk: if (e->u.info <= MAXINDEXRK) /* constant fits in argC? */ return RKASK(e->u.info); else break; @@ -626,7 +646,7 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { pc = e->u.info; break; } - case VK: case VKNUM: case VTRUE: { + case VK: case VKFLT: case VKINT: case VTRUE: { pc = NO_JUMP; /* always true; do nothing */ break; } @@ -671,7 +691,7 @@ static void codenot (FuncState *fs, expdesc *e) { e->k = VTRUE; break; } - case VK: case VKNUM: case VTRUE: { + case VK: case VKFLT: case VKINT: case VTRUE: { e->k = VFALSE; break; } @@ -760,7 +780,7 @@ static void codecomp (FuncState *fs, OpCode op, int cond, expdesc *e1, void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e, int line) { expdesc e2; - e2.t = e2.f = NO_JUMP; e2.k = VKNUM; e2.u.nval = 0; + e2.t = e2.f = NO_JUMP; e2.k = VKFLT; e2.u.nval = 0; switch (op) { case OPR_MINUS: { if (isnumeral(e)) /* minus constant? */ diff --git a/llex.c b/llex.c index 3423f86f..bb5f36ab 100644 --- a/llex.c +++ b/llex.c @@ -1,5 +1,5 @@ /* -** $Id: llex.c,v 2.62 2012/12/05 19:57:00 roberto Exp roberto $ +** $Id: llex.c,v 2.63 2013/03/16 21:10:18 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -39,7 +39,7 @@ static const char *const luaX_tokens [] = { "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while", "..", "...", "==", ">=", "<=", "~=", "::", "", - "", "", "" + "", "", "", "" }; @@ -90,9 +90,8 @@ const char *luaX_token2str (LexState *ls, int token) { static const char *txtToken (LexState *ls, int token) { switch (token) { - case TK_NAME: - case TK_STRING: - case TK_NUMBER: + case TK_NAME: case TK_STRING: + case TK_FLT: case TK_INT: save(ls, '\0'); return luaO_pushfstring(ls->L, LUA_QS, luaZ_buffer(ls->buff)); default: @@ -216,7 +215,7 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { if (!buff2d(ls->buff, &seminfo->r)) { /* format error with correct decimal point: no more options */ buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */ - lexerror(ls, "malformed number", TK_NUMBER); + lexerror(ls, "malformed number", TK_FLT); } } @@ -224,9 +223,10 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { /* LUA_NUMBER */ /* ** this function is quite liberal in what it accepts, as 'luaO_str2d' -** will reject ill-formed numerals. +** will reject ill-formed numerals. 'isf' means the numeral is not +** an integer (it has a dot or an exponent). */ -static void read_numeral (LexState *ls, SemInfo *seminfo) { +static int read_numeral (LexState *ls, SemInfo *seminfo, int isf) { const char *expo = "Ee"; int first = ls->current; lua_assert(lisdigit(ls->current)); @@ -234,16 +234,30 @@ static void read_numeral (LexState *ls, SemInfo *seminfo) { if (first == '0' && check_next(ls, "Xx")) /* hexadecimal? */ expo = "Pp"; for (;;) { - if (check_next(ls, expo)) /* exponent part? */ + if (check_next(ls, expo)) { /* exponent part? */ check_next(ls, "+-"); /* optional exponent sign */ - if (lisxdigit(ls->current) || ls->current == '.') + isf = 1; + } + if (lisxdigit(ls->current)) save_and_next(ls); - else break; + else if (ls->current == '.') { + save_and_next(ls); + isf = 1; + } + else break; } save(ls, '\0'); - buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ - if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ - trydecpoint(ls, seminfo); /* try to update decimal point separator */ + if (!isf) { + if (!luaO_str2int(luaZ_buffer(ls->buff), &seminfo->i)) + lexerror(ls, "malformed number", TK_INT); + return TK_INT; + } + else { + buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */ + if (!buff2d(ls->buff, &seminfo->r)) /* format error? */ + trydecpoint(ls, seminfo); /* try to update decimal point separator */ + return TK_FLT; + } } @@ -472,12 +486,11 @@ static int llex (LexState *ls, SemInfo *seminfo) { else return TK_CONCAT; /* '..' */ } else if (!lisdigit(ls->current)) return '.'; - /* else go through */ + else return read_numeral(ls, seminfo, 1); } case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { - read_numeral(ls, seminfo); - return TK_NUMBER; + return read_numeral(ls, seminfo, 0); } case EOZ: { return TK_EOS; diff --git a/llex.h b/llex.h index 1125e836..14eb548d 100644 --- a/llex.h +++ b/llex.h @@ -1,5 +1,5 @@ /* -** $Id: llex.h,v 1.71 2011/06/20 16:52:48 roberto Exp roberto $ +** $Id: llex.h,v 1.72 2011/11/30 12:43:51 roberto Exp roberto $ ** Lexical Analyzer ** See Copyright Notice in lua.h */ @@ -27,7 +27,7 @@ enum RESERVED { TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE, /* other terminal symbols */ TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_DBCOLON, TK_EOS, - TK_NUMBER, TK_NAME, TK_STRING + TK_FLT, TK_INT, TK_NAME, TK_STRING }; /* number of reserved words */ @@ -36,6 +36,7 @@ enum RESERVED { typedef union { lua_Number r; + lua_Integer i; TString *ts; } SemInfo; /* semantics information */ diff --git a/lobject.c b/lobject.c index 8bea8972..53de02a0 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 2.57 2013/01/29 16:00:40 roberto Exp roberto $ +** $Id: lobject.c,v 2.58 2013/02/20 14:08:56 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -169,6 +169,25 @@ int luaO_str2d (const char *s, size_t len, lua_Number *result) { } +int luaO_str2int (const char *s, lua_Integer *result) { + lua_Unsigned a = 0; + if (s[0] == '0' && + (s[1] == 'x' || s[1] == 'X')) { /* hexa? */ + s += 2; /* skip '0x' */ + for (; lisxdigit(cast_uchar(*s)); s++) + a = a * 16 + luaO_hexavalue(cast_uchar(*s)); + } + else { /* decimal */ + for (; lisdigit(cast_uchar(*s)); s++) + a = a * 10 + luaO_hexavalue(cast_uchar(*s)); + } + if (*s != '\0') return 0; /* something wrong in the numeral */ + else { + *result = cast(lua_Integer, a); + return 1; + } +} + static void pushstr (lua_State *L, const char *str, size_t l) { setsvalue2s(L, L->top++, luaS_newlstr(L, str, l)); diff --git a/lobject.h b/lobject.h index 40977e99..8e7916a1 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 2.70 2012/05/11 14:10:50 roberto Exp $ +** $Id: lobject.h,v 2.73 2013/04/15 15:44:46 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -498,6 +498,7 @@ LUAI_FUNC int luaO_fb2int (int x); LUAI_FUNC int luaO_ceillog2 (unsigned int x); LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2); LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result); +LUAI_FUNC int luaO_str2int (const char *s, lua_Integer *result); LUAI_FUNC int luaO_hexavalue (int c); LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp); diff --git a/lparser.c b/lparser.c index b83b0c33..0ac318ff 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 2.129 2012/08/06 13:36:34 roberto Exp roberto $ +** $Id: lparser.c,v 2.130 2013/02/06 13:37:39 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -935,14 +935,19 @@ static void suffixedexp (LexState *ls, expdesc *v) { static void simpleexp (LexState *ls, expdesc *v) { - /* simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... | + /* simpleexp -> FLT | INT | STRING | NIL | TRUE | FALSE | ... | constructor | FUNCTION body | suffixedexp */ switch (ls->t.token) { - case TK_NUMBER: { - init_exp(v, VKNUM, 0); + case TK_FLT: { + init_exp(v, VKFLT, 0); v->u.nval = ls->t.seminfo.r; break; } + case TK_INT: { + init_exp(v, VKINT, 0); + v->u.ival = ls->t.seminfo.i; + break; + } case TK_STRING: { codestring(ls, v, ls->t.seminfo.ts); break; diff --git a/lparser.h b/lparser.h index 33257932..41f3c42b 100644 --- a/lparser.h +++ b/lparser.h @@ -1,5 +1,5 @@ /* -** $Id: lparser.h,v 1.69 2011/07/27 18:09:01 roberto Exp roberto $ +** $Id: lparser.h,v 1.70 2012/05/08 13:53:33 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -22,7 +22,8 @@ typedef enum { VTRUE, VFALSE, VK, /* info = index of constant in `k' */ - VKNUM, /* nval = numerical value */ + VKFLT, /* nval = numerical float value */ + VKINT, /* nval = numerical integer value */ VNONRELOC, /* info = result register */ VLOCAL, /* info = local register */ VUPVAL, /* info = index of upvalue in 'upvalues' */ @@ -46,7 +47,8 @@ typedef struct expdesc { lu_byte vt; /* whether 't' is register (VLOCAL) or upvalue (VUPVAL) */ } ind; int info; /* for generic use */ - lua_Number nval; /* for VKNUM */ + lua_Number nval; /* for VKFLT */ + lua_Integer ival; /* for VKINT */ } u; int t; /* patch list of `exit when true' */ int f; /* patch list of `exit when false' */