Skip to content

Commit

Permalink
Merge pull request #21 from paulcuth/dev
Browse files Browse the repository at this point in the history
v0.1.6
  • Loading branch information
paulcuth committed Mar 22, 2016
2 parents 8fa9af7 + 9a7ba44 commit 4c09ddb
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 100 deletions.
45 changes: 0 additions & 45 deletions ci/bump-project-version.js

This file was deleted.

3 changes: 0 additions & 3 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
npm:
branch: master
commands:
- git config --global user.email '[email protected]'
- git config --global user.name "CircleCI"
- node ci/bump-project-version
- grunt grunt-plugin
- node ci/sync-plugin-version
- node ci/tag
Expand Down
86 changes: 44 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
{
"name": "starlight",
"description": "A Lua -> ES6 transpiler",
"version": "0.1.5",
"author": {
"name": "Paul Cuthbertson"
},
"repository": {
"type": "git",
"url": "https://github.com/paulcuth/starlight"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/paulcuth/starlight/issues"
},
"dependencies": {},
"devDependencies": {
"babel": "^5.4.7",
"browserify": "^10.2.4",
"grunt": "^0.4.5",
"grunt-babel": "^5.0.1",
"grunt-browserify": "^3.8.0",
"grunt-cli": "^0.1.13",
"grunt-contrib-concat": "^0.3.0",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-uglify": "^0.9.1",
"grunt-task-loader": "^0.6.0",
"gulp": "^3.8.11",
"gulp-babel": "^5.1.0",
"gulp-concat": "^2.5.2",
"gulp-minify-html": "^1.0.3",
"gulp-sourcemaps": "^1.5.2",
"gulp-uglify": "^1.2.0",
"gulp-util": "^3.0.4",
"luaparse": "^0.1.15",
"through": "^2.3.7",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
},
"scripts": {
"test": "grunt grunt-plugin node-test"
}
}
"name": "starlight",
"description": "A Lua -> ES6 transpiler",
"version": "0.1.6",
"author": {
"name": "Paul Cuthbertson"
},
"repository": {
"type": "git",
"url": "https://github.com/paulcuth/starlight"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/paulcuth/starlight/issues"
},
"dependencies": {
"printf": "^0.2.3"
},
"devDependencies": {
"babel": "^5.4.7",
"browserify": "^10.2.4",
"grunt": "^0.4.5",
"grunt-babel": "^5.0.1",
"grunt-browserify": "^3.8.0",
"grunt-cli": "^0.1.13",
"grunt-contrib-concat": "^0.3.0",
"grunt-contrib-copy": "^0.8.0",
"grunt-contrib-uglify": "^0.9.1",
"grunt-task-loader": "^0.6.0",
"gulp": "^3.8.11",
"gulp-babel": "^5.1.0",
"gulp-concat": "^2.5.2",
"gulp-minify-html": "^1.0.3",
"gulp-sourcemaps": "^1.5.2",
"gulp-uglify": "^1.2.0",
"gulp-util": "^3.0.4",
"luaparse": "^0.1.15",
"through": "^2.3.7",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
},
"scripts": {
"test": "grunt grunt-plugin node-test"
}
}
29 changes: 21 additions & 8 deletions src/build-tools/common/code-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ const GENERATORS = {
DoStatement(node, outerScope) {
let { scope, scopeDef } = extendScope(outerScope);
let body = this.Chunk(node, scope);
return `()=>{\n${scopeDef}\n${body}\n}()`;
return `(()=>{\n${scopeDef}\n${body}\n})()`;
},


Expand Down Expand Up @@ -206,7 +206,7 @@ const GENERATORS = {
name = identifier.property.replace(/'/g, '');

if (node.identifier.indexer === ':') {
params.unshift("$set($, 'self', __star_shift(args))");
params.unshift("$setLocal($, 'self', __star_shift(args))");
}
} else {
name = identifier;
Expand Down Expand Up @@ -295,6 +295,14 @@ const GENERATORS = {
let right = scoped(node.right, scope);
let operator = node.operator;

if (isCallExpression(node.left)) {
left += '[0]';
}

if (isCallExpression(node.right)) {
right += '[0]';
}

if (operator === 'and') {
return `(!__star.op.bool(${left})?${left}:${right})`

Expand Down Expand Up @@ -355,8 +363,12 @@ const GENERATORS = {


StringLiteral(node) {
let escaped = node.value.replace(/["']/g, '\\$&').replace(/\n/g, '\\n');
return `'${escaped}'`;
const raw = node.raw.replace(/\\(\d+)/g, (_, oct) => `\\x0${parseInt(oct, 8).toString(16)}`);
if (/^\[\[[^]*]$/m.test(raw)) {
return `\`${raw.substr(2, raw.length - 4)}\``;
} else {
return raw;
}
},


Expand Down Expand Up @@ -389,7 +401,7 @@ const GENERATORS = {


TableKey(node, scope) {
let name = generate(node.key, scope);
let name = scoped(node.key, scope);
let value = scoped(node.value, scope);
return `Tset(t, ${name}, ${value})`;
},
Expand Down Expand Up @@ -466,20 +478,21 @@ function generate(ast, scope) {


export function getRuntimeInit() {
let init = 'let __star = global.starlight.runtime, $0 = __star.globalScope, $ = $0, __star_tmp;\n';
let init = '"use strict"; if (typeof global === \'undefined\' && typeof window !== \'undefined\') { window.global = window; }\n';
init += 'let __star = global.starlight.runtime, $0 = __star.globalScope, $ = $0, __star_tmp;\n';
init += 'let __star_call = __star.call, __star_T = __star.T, __star_op_bool = __star.op.bool;';
init += 'let __star_op_unm = __star.op.unm, __star_op_not = __star.op.not, __star_op_len = __star.op.len, __star_op_concat = __star.op.concat, __star_op_add = __star.op.add, __star_op_sub = __star.op.sub, __star_op_mul = __star.op.mul, __star_op_div = __star.op.div, __star_op_mod = __star.op.mod, __star_op_eq = __star.op.eq, __star_op_neq = __star.op.neq, __star_op_lt = __star.op.lt, __star_op_gt = __star.op.gt, __star_op_lte = __star.op.lte, __star_op_gte = __star.op.gte, __star_op_pow = __star.op.pow;';
init += 'let __star_op_and = __star.op.and, __star_op_or = __star.op.or;\n';

init += 'let Tget, Tset, Tins, $get, $set, $setLocal, __star_shift;';

init += '()=>{';
init += '(()=>{';
init += 'let call = Function.prototype.call, bind = call.bind.bind(call), Tproto = __star_T.prototype, $proto = __star.globalScope.constructor.prototype;';

init += 'Tget = bind(Tproto.get), Tset = bind(Tproto.set), Tins = bind(Tproto.insert);';
init += '$get = bind($proto.get), $set = bind($proto.set), $setLocal = bind($proto.setLocal);';
init += '__star_shift = bind(Array.prototype.shift);';
init += '}();'
init += '})();'

return init;
}
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/lib/globals.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,9 +236,11 @@ export function rawset(table, index, value) {

export function _require(modname) {
modname = coerceArgToString(modname, 'require', 1);
modname = modname.replace(/\//g, '.');

let [preload, loaded] = getPackageMethods();

let modinit = preload.rawget(modname);

if (modinit === void 0) {
throw new LuaError(`module '${modname}' not found:\n\tno field package.preload['${modname}']`);
}
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/lib/string.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { default as T } from '../Table';
import { default as LuaError } from '../LuaError';
import { tostring } from './string';
import printf from 'printf';

import {
coerceToNumber,
Expand Down Expand Up @@ -134,6 +135,9 @@ export function find(s, pattern, init = 1, plain = false) {


// TODO string.format (formatstring, ···)
export function format(formatstring, ...args) {
return printf(formatstring, ...args);
}


export function gmatch(s, pattern) {
Expand Down Expand Up @@ -290,6 +294,7 @@ const string = new T({
char,
dump,
find,
format,
gmatch,
gsub,
len,
Expand Down
5 changes: 4 additions & 1 deletion test/lua/lib-require.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ assertTrue (testModName == 'lib-require', 'A module\'s name should be passed int


local sub = require 'lib-require.sub-module' -- test dot syntax
assertTrue(type(sub) == 'table', 'Module should be able to load more modules.')
assertTrue(type(sub) == 'table', 'Module should be able to load more modules using dot syntax.')

local sub2 = require 'lib-require/sub-module' -- test slash syntax
assertTrue(type(sub2) == 'table', 'Module should be able to load more modules using slash syntax.')

mainGlobal1 = 'innerGlbl'
local innerLocal = 'innerLoc'
Expand Down
16 changes: 16 additions & 0 deletions test/lua/operators.lua
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,22 @@ assertTrue (not (f or f), 'Or operator should return false if both operands are
assertEqual(t and 0 or 1, 0, 'Ternary logic should return the correct result[1]')
assertEqual(f and 0 or 1, 1, 'Ternary logic should return the correct result[2]')

function f(x)
return x
end

assertEqual (f('moo') or false, 'moo', 'Or operator should work with function calls as left operand (+ve)')
assertEqual (f(false) or false, false, 'Or operator should work with function calls as left operand (-ve)')
assertEqual (false or f('moo'), 'moo', 'Or operator should work with function calls as right operand (+ve)')
assertEqual (false or f(false), false, 'Or operator should work with function calls as right operand (-ve)')
assertEqual (f(false) or f('moo'), 'moo', 'Or operator should work with function calls as both operands')

assertEqual (f('moo') and 'baa', 'baa', 'And operator should work with function calls as left operand (+ve)')
assertEqual (f(false) and true, false, 'And operator should work with function calls as left operand (-ve)')
assertEqual (true and f('moo'), 'moo', 'And operator should work with function calls as right operand (+ve)')
assertEqual (true and f(false), false, 'And operator should work with function calls as right operand (-ve)')
assertEqual (f('moo') and f('moo'), 'moo', 'And operator should work with function calls as both operands')


local function test()
return true
Expand Down
8 changes: 8 additions & 0 deletions test/lua/tables.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ assertTrue (a[2] == nil, 'Square brackets operation on table should return corre
assertTrue (a[3] == 40, 'Square brackets operation on table should return correct value for index when keys are used in literal assignment [3]')
assertTrue (a[4] == nil, 'Square brackets operation on table should return correct value for index when keys are used in literal assignment [4]')

b = 'hello'
a = {[b] = 'value'}
assertEqual (a.hello, 'value', 'Identifier should be able to be used as a key')

b = 2
a = {[b] = 'value2'}
assertEqual (a[2], 'value2', 'Numeric identifier should be able to be used as a key')




Expand Down

0 comments on commit 4c09ddb

Please sign in to comment.