Permalink
Browse files

Merge pull request #44 from paulcuth/dev

v0.3.3
  • Loading branch information...
paulcuth committed Aug 10, 2016
2 parents f4e5253 + b124f87 commit aea143a5403de15c8e88bd2903f9bf694f6d5f8b
View
@@ -1,7 +1,7 @@
{
"name": "starlight",
"description": "A Lua -> ES6 transpiler",
"version": "0.3.2",
"version": "0.3.3",
"author": {
"name": "Paul Cuthbertson"
},
@@ -153,16 +153,17 @@ const GENERATORS = {
ForGenericStatement(node, outerScope) {
const { scope, scopeDef } = extendScope(outerScope);
const { scope: iterationScope, scopeDef: iterationScopeDef } = extendScope(scope);
const iterators = parseExpressionList(node.iterators, outerScope).join(', ');
const body = this.Chunk(node, scope);
const body = this.Chunk(node, iterationScope);
const variables = node.variables.map((variable, index) => {
const name = generate(variable, scope);
return `$setLocal($, '${name}', __star_tmp[${index}])`;
}).join(';\n');
const defs = scopeDef.split(', ');
return `${defs[0]};\n[$${scope}._iterator, $${scope}._table, $${scope}._next] = [${iterators}];\nwhile((__star_tmp = __star_call($${scope}._iterator, $${scope}._table, $${scope}._next)),__star_tmp[0] !== undefined) {\nlet ${defs[1]}\$${scope}._next = __star_tmp[0]\n${variables}\n${body}\n}`;
return `${defs[0]};\n[$${scope}._iterator, $${scope}._table, $${scope}._next] = [${iterators}];\nwhile((__star_tmp = __star_call($${scope}._iterator, $${scope}._table, $${scope}._next)),__star_tmp[0] !== undefined) {\n${iterationScopeDef}\$${scope}._next = __star_tmp[0]\n${variables}\n${body}\n}`;
},
@@ -68,10 +68,11 @@ module.exports = function(grunt) {
var start = "require'" + mainFilename + "'\n";
var ast = parser.parse(src + start);
var js = generateJS(ast);
var runtimeInit = getRuntimeInit();
js = ';(()=>{' + runtimeInit + js + '})();';
// Write the destination file.
var runtimeInit = getRuntimeInit();
grunt.file.write(f.dest, runtimeInit + js);
grunt.file.write(f.dest, js);
// Print a success message.
grunt.log.writeln('File "' + f.dest + '" created.');
@@ -255,9 +255,10 @@ export function _require(modname) {
throw new LuaError(`module '${modname}' not found:\n\tno field package.preload['${modname}']`);
}
mod = modinit(modname)[0];
loaded.rawset(modname, mod !== void 0 ? mod : true);
let modResult = modinit(modname);
mod = (modResult instanceof Array) ? modResult[0] : modResult;
loaded.rawset(modname, mod !== void 0 ? mod : true);
return mod;
}
@@ -39,6 +39,21 @@ assertTrue (a == 5, 'For loop should iterate the correct number of times')
assertTrue (b == 15, 'For loop variable should hold the value of the current iteration')
local funcs = {}
local b = ''
for i = 1, 3 do
table.insert(funcs, function () b = b..i end)
end
funcs[1]()
funcs[2]()
funcs[3]()
assertTrue (b == '123', 'Each loop of numeric for should maintain its own scope')
a = { a = 1, b = 2 }
b = 0
@@ -124,7 +139,20 @@ for x,y,z in iter, {4,5,6}, 0 do
end
assertTrue (a == '[1,4,8][2,5,10][3,6,12]', 'Generic for loop should pass all values returned from iterator to the variables in the loop')
local funcs = {}
local a = {1,2,3}
local b = ''
for _, i in ipairs(a) do
table.insert(funcs, function () b = b..i end)
end
funcs[1]()
funcs[2]()
funcs[3]()
assertTrue (b == '123', 'Each loop of generic for should maintain its own scope')
a = ''
b = 1
@@ -164,6 +192,21 @@ end
assertTrue (b == '1', 'Break should exit while loop')
local funcs = {}
local b = ''
i = 1
while i < 4 do
table.insert(funcs, function () b = b..i end)
i = i + 1
end
funcs[1]()
funcs[2]()
funcs[3]()
assertTrue (b == '444', 'Each loop of while should not maintain its own scope')
a = ''
b = 1
@@ -204,6 +247,21 @@ until i == 4
assertTrue (b == '1', 'Break should exit repeat loop')
local funcs = {}
local b = ''
i = 1
repeat
table.insert(funcs, function () b = b..i end)
i = i + 1
until i == 4
funcs[1]()
funcs[2]()
funcs[3]()
assertTrue (b == '444', 'Each loop of repeat should not maintain its own scope')
a = ':'
t = { 123, 456, x = 789, 10 }
for i, v in ipairs(t) do

0 comments on commit aea143a

Please sign in to comment.