diff options
author | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-27 15:35:50 +0100 |
---|---|---|
committer | Benoit Giannangeli <benoit.giannangeli@boursorama.fr> | 2017-02-27 15:35:50 +0100 |
commit | c1cf887d702a36729bab0257247066745223cf6e (patch) | |
tree | 31958bdfafc4603a95581b751ac996311c06ef2f | |
parent | 6beeccbd0a859f3a9d1be4142d16a3d11ac30743 (diff) | |
download | fengari-c1cf887d702a36729bab0257247066745223cf6e.tar.gz fengari-c1cf887d702a36729bab0257247066745223cf6e.tar.bz2 fengari-c1cf887d702a36729bab0257247066745223cf6e.zip |
TK_LOCAL, TK_NAME, TK_INT
-rw-r--r-- | src/llex.js | 10 | ||||
-rw-r--r-- | src/lobject.js | 26 | ||||
-rw-r--r-- | tests/llex.js | 52 |
3 files changed, 70 insertions, 18 deletions
diff --git a/src/llex.js b/src/llex.js index 1adac0a..479fe48 100644 --- a/src/llex.js +++ b/src/llex.js @@ -250,7 +250,7 @@ const txtToken = function(ls, token) { case R.TK_NAME: case R.TK_STRING: case R.TK_FLT: case R.TK_INT: save(ls, '\0'); - return lapi.lua_pushstring(ls.L, `'${ls.buff.buffer}'`); + return lapi.lua_pushstring(ls.L, `'${ls.buff.buffer.join('')}'`); default: return luaX_token2str(ls, token); } @@ -306,7 +306,10 @@ const read_long_string = function(ls, seminfo, sep) { case '\n': case '\r': { save(ls, '\n'); inclinenumber(ls); - if (!seminfo) ls.buff.n = 0; + if (!seminfo) { + ls.buff.n = 0; + ls.buff.buffer = []; + } break; } default: { @@ -444,6 +447,7 @@ const read_string = function(ls, del, seminfo) { const llex = function(ls, seminfo) { ls.buff.n = 0; + ls.buff.buffer = []; for (;;) { switch (ls.current) { @@ -463,9 +467,11 @@ const llex = function(ls, seminfo) { if (ls.current === '[') { /* long comment? */ let sep = skip_sep(ls); ls.buff.n = 0; /* 'skip_sep' may dirty the buffer */ + ls.buff.buffer = []; if (sep >= 0) { read_long_string(ls, null, sep); /* skip long comment */ ls.buff.n = 0; /* previous call may dirty the buff. */ + ls.buff.buffer = []; break; } } diff --git a/src/lobject.js b/src/lobject.js index 93e23c2..f6c6ef0 100644 --- a/src/lobject.js +++ b/src/lobject.js @@ -294,22 +294,22 @@ const l_str2int = function(s) { let empty = true; let neg; - while (ljstype.lisspace(s.charAt(0))) s = s.slice(1); /* skip initial spaces */ - neg = s.charAt(0) === '-'; + while (ljstype.lisspace(s[0])) s = s.slice(1); /* skip initial spaces */ + neg = s[0] === '-'; - if (neg || s.charAt(0) === '+') + if (neg || s[0] === '+') s = s.slice(1); if (s[0] === '0' && (s[1] === 'x' || s[1] === 'X')) { /* hex? */ s = s.slice(2); /* skip '0x' */ - for (; ljstype.lisxdigit(s); s = s.slice(1)) { + for (; ljstype.lisxdigit(s[0]); s = s.slice(1)) { a = a * 16 + luaO_hexavalue(s); empty = false; } } else { /* decimal */ - for (; ljstype.lisdigit(s); s = s.slice(1)) { - let d = parseInt(s.charAt(0)); + for (; ljstype.lisdigit(s[0]); s = s.slice(1)) { + let d = parseInt(s[0]); if (a >= MAXBY10 && (a > MAXBY10 || d > MAXLASTD + neg)) /* overflow? */ return null; /* do not accept it (as integer) */ a = a * 10 + d; @@ -317,9 +317,9 @@ const l_str2int = function(s) { } } - while (ljstype.lisspace(s.charAt(0))) s = s.slice(1); /* skip trailing spaces */ + while (ljstype.lisspace(s[0])) s = s.slice(1); /* skip trailing spaces */ - if (empty || s.charAt(0) !== "") return null; /* something wrong in the numeral */ + if (empty)/* TODO: || s[0] !== "") */ return null; /* something wrong in the numeral */ else { result[1] = neg ? -a : a; result[0] = s; @@ -332,19 +332,17 @@ const luaO_str2num = function(s) { let e = s2i[0]; let i = s2i[1]; - let o; - if (e !== null) /* try as an integer */ - o = new TValue(CT.LUA_TNUMINT, i); - else { /* else try as a float */ + if (e !== null) { /* try as an integer */ + return new TValue(CT.LUA_TNUMINT, i);; + } else { /* else try as a float */ s2i = l_str2d(s); e = s2i[0]; i = s2i[1]; if (e !== null) { - o = new TValue(CT.LUA_TNUMFLT, i); + return new TValue(CT.LUA_TNUMFLT, i); } else return false; /* conversion failed */ - return (e - s) + 1; // TODO: wrong } }; diff --git a/tests/llex.js b/tests/llex.js index 3c05366..725d3f8 100644 --- a/tests/llex.js +++ b/tests/llex.js @@ -13,7 +13,7 @@ const lua = require('../src/lua.js'); const R = llex.RESERVED; -test('basic lexing', function (t) { +test('basic lexing: TK_RETURN, TK_STRING', function (t) { let luaCode = ` return "hello lex !" `, L; @@ -32,7 +32,7 @@ test('basic lexing', function (t) { llex.luaX_next(ls); while (ls.t.token !== R.TK_EOS) { - console.log(llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED]); + // console.log(llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED]); readTokens.push(ls.t.token); llex.luaX_next(ls); @@ -47,4 +47,52 @@ test('basic lexing', function (t) { "Correct tokens found" ) +}); + + +test('TK_LOCAL, TK_NAME, TK_INT', function (t) { + let luaCode = ` + local f = testing(aBunch) + return "of" + end + + return f("things") + 12 + `, L; + + t.plan(2); + + let readTokens = []; + + t.doesNotThrow(function () { + + L = lauxlib.luaL_newstate(); + + let ls = new llex.LexState(); + llex.luaX_setinput(L, ls, new llex.MBuffer(luaCode), luaCode, luaCode.charAt(0)); + + llex.luaX_next(ls); + + while (ls.t.token !== R.TK_EOS) { + // console.log(ls.t.token >= llex.FIRST_RESERVED ? + // llex.luaX_tokens[ls.t.token - llex.FIRST_RESERVED] + // : ls.t.token); + + readTokens.push(ls.t.token); + llex.luaX_next(ls); + } + + + }, "JS Lua program ran without error"); + + t.deepEqual( + readTokens, + [ + R.TK_LOCAL, R.TK_NAME, '=', R.TK_NAME, '(', R.TK_NAME, ')', + R.TK_RETURN, R.TK_STRING, + R.TK_END, + R.TK_RETURN, R.TK_NAME, '(', R.TK_STRING, ')', '+', R.TK_INT + ], + "Correct tokens found" + ) + });
\ No newline at end of file |