From 490ee0d109671b6f7e9d3d56e8122f41647fed4c Mon Sep 17 00:00:00 2001 From: Benoit Giannangeli Date: Thu, 2 Mar 2017 09:54:45 +0100 Subject: [Parsing tests] FORPREP, FORLOOP --- src/lobject.js | 36 +++++++++++++----------------------- src/lparser.js | 2 +- 2 files changed, 14 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/lobject.js b/src/lobject.js index bd234a1..0ef6e9a 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -278,32 +278,28 @@ const luaO_utf8desc = function(buff, x) { return n; }; -const l_str2dloc = function(s, result, mode) { - result[0] = mode === 'x' ? parseInt(s, '16') : parseInt(s); - if (isNaN(result[0])) return null; /* nothing recognized? */ - while (ljstype.lisspace(result[0])) result[0] = result[0].slice(1); /* skip trailing spaces */ - return result[0] === '\0' ? result : null; /* OK if no trailing characters */ +const l_str2dloc = function(s, mode) { + let flt = mode === 'x' ? parseInt(s, '16') : parseFloat(s); + return !isNaN(flt) && /^\d+(\.\d+)?\0?$/.test(s) ? flt : null; /* OK if no trailing characters */ }; const l_str2d = function(s) { - let result = [null, null]; let pidx = /[.xXnN]/g.exec(s).index; - let pmode = pidx ? s.slice(pidx) : null; + let pmode = pidx ? s[pidx] : null; let mode = pmode ? pmode.toLowerCase() : 0; if (mode === 'n') /* reject 'inf' and 'nan' */ return null; - let end = l_str2dloc(s, result, mode)[0]; /* try to convert */ + let end = l_str2dloc(s, mode); /* try to convert */ if (end === null) { /* failed? may be a different locale */ throw new Error("Locale not available to handle number"); // TODO } - return [end, result[1]]; + return end; }; const MAXBY10 = Number.MAX_SAFE_INTEGER / 10; const MAXLASTD = Number.MAX_SAFE_INTEGER % 10; const l_str2int = function(s) { - let result = [null, null]; let a = 0; let empty = true; let neg; @@ -333,28 +329,22 @@ const l_str2int = function(s) { while (ljstype.lisspace(s[0])) s = s.slice(1); /* skip trailing spaces */ - if (empty)/* TODO: || s[0] !== "") */ return null; /* something wrong in the numeral */ + if (empty || s[0] !== "\0") return null; /* something wrong in the numeral */ else { - result[1] = neg ? -a : a; - result[0] = s; - return result; + return neg ? -a : a; } }; const luaO_str2num = function(s) { let s2i = l_str2int(s); - let e = s2i[0]; - let i = s2i[1]; - if (e !== null) { /* try as an integer */ - return new TValue(CT.LUA_TNUMINT, i); + if (s2i !== null) { /* try as an integer */ + return new TValue(CT.LUA_TNUMINT, s2i); } else { /* else try as a float */ - s2i = l_str2d(s); - e = s2i[0]; - i = s2i[1]; + s2i = l_str2d(s.join('')); - if (e !== null) { - return new TValue(CT.LUA_TNUMFLT, i); + if (s2i !== null) { + return new TValue(CT.LUA_TNUMFLT, s2i); } else return false; /* conversion failed */ } diff --git a/src/lparser.js b/src/lparser.js index c21563f..9c52c59 100644 --- a/src/lparser.js +++ b/src/lparser.js @@ -902,7 +902,7 @@ const simpleexp = function(ls, v) { constructor | FUNCTION body | suffixedexp */ switch (ls.t.token) { case R.TK_FLT: { - init_exp(v, expkind.VFLT, 0); + init_exp(v, expkind.VKFLT, 0); v.u.nval = ls.t.seminfo.r; break; } -- cgit v1.2.3-70-g09d2