From da0109bd14cb5ac084ad8260b8eb835bf7eb310c Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 27 Dec 2013 04:00:46 -0800 Subject: [PATCH 001/344] make lint use ref, whitelist nil, true, false --- moonscript/cmd/lint.lua | 12 ++++++++---- moonscript/cmd/lint.moon | 12 ++++++++---- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index b47842bf..291cf71f 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -50,7 +50,10 @@ local whitelist_globals = Set({ 'setmetatable', 'tonumber', 'collectgarbage', - 'coroutine' + 'coroutine', + "nil", + "true", + "false" }) local LinterBlock do @@ -75,15 +78,16 @@ do _parent_0.__init(self, ...) local vc = self.value_compilers self.value_compilers = setmetatable({ - raw_value = function(block, name) - if name:match("^[%w_]+$") and not block:has_name(name) and not whitelist_globals[name] then + ref = function(block, val) + local name = val[2] + if not (block:has_name(name) or whitelist_globals[name]) then local stm = block.current_stms[block.current_stm_i] insert(self.lint_errors, { "accessing global " .. tostring(name), stm[-1] }) end - return vc.raw_value(block, name) + return vc.raw_value(block, val) end }, { __index = vc diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index b0fcd58e..236a4748 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -42,6 +42,10 @@ whitelist_globals = Set { 'tonumber' 'collectgarbage' 'coroutine' + + "nil" + "true" + "false" } class LinterBlock extends Block @@ -50,16 +54,16 @@ class LinterBlock extends Block vc = @value_compilers @value_compilers = setmetatable { - raw_value: (block, name) -> - - if name\match("^[%w_]+$") and not block\has_name(name) and not whitelist_globals[name] + ref: (block, val) -> + name = val[2] + unless block\has_name(name) or whitelist_globals[name] stm = block.current_stms[block.current_stm_i] insert @lint_errors, { "accessing global #{name}" stm[-1] } - vc.raw_value block, name + vc.raw_value block, val }, __index: vc block: (...) => From 53cf5dd649312ebd99c521796715b6015f53a2ff Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 27 Dec 2013 12:57:07 -0800 Subject: [PATCH 002/344] fix lint, mark pos of all refs --- moonscript/cmd/lint.lua | 8 ++++---- moonscript/cmd/lint.moon | 8 ++++---- moonscript/parse.lua | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 291cf71f..5b0bfd17 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -62,6 +62,7 @@ do block = function(self, ...) do local _with_0 = _parent_0.block(self, ...) + _with_0.block = self.block _with_0.value_compilers = self.value_compilers return _with_0 end @@ -80,14 +81,13 @@ do self.value_compilers = setmetatable({ ref = function(block, val) local name = val[2] - if not (block:has_name(name) or whitelist_globals[name]) then - local stm = block.current_stms[block.current_stm_i] + if not (block:has_name(name) or whitelist_globals[name] or name:match("%.")) then insert(self.lint_errors, { "accessing global " .. tostring(name), - stm[-1] + val[-1] }) end - return vc.raw_value(block, val) + return vc.ref(block, val) end }, { __index = vc diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 236a4748..9d883d39 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -56,18 +56,18 @@ class LinterBlock extends Block @value_compilers = setmetatable { ref: (block, val) -> name = val[2] - unless block\has_name(name) or whitelist_globals[name] - stm = block.current_stms[block.current_stm_i] + unless block\has_name(name) or whitelist_globals[name] or name\match "%." insert @lint_errors, { "accessing global #{name}" - stm[-1] + val[-1] } - vc.raw_value block, val + vc.ref block, val }, __index: vc block: (...) => with super ... + .block = @block .value_compilers = @value_compilers format_lint = (errors, code, header) -> diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 0a28f58b..499ae1b7 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -511,7 +511,7 @@ local build_grammar = wrap_env(function() LuaStringOpen = sym"[" * P"="^0 * "[" / trim, LuaStringClose = "]" * P"="^0 * "]", - Callable = Name / mark"ref" + SelfName + VarArg + Parens / mark"parens", + Callable = pos(Name / mark"ref") + SelfName + VarArg + Parens / mark"parens", Parens = sym"(" * Exp * sym")", FnArgs = symx"(" * Ct(ExpList^-1) * sym")" + sym"!" * -P"=" * Ct"", From db439f880a35bba0c3403a29f854cdc8d9918b1a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 27 Dec 2013 15:30:21 -0800 Subject: [PATCH 003/344] load a lint config --- moonscript/cmd/lint.lua | 43 ++++++++++++++++++++++++++++++++-------- moonscript/cmd/lint.moon | 34 ++++++++++++++++++++++++++----- 2 files changed, 64 insertions(+), 13 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 5b0bfd17..9dbbcc53 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -13,7 +13,7 @@ do local _obj_0 = require("moonscript.compile") Block = _obj_0.Block end -local whitelist_globals = Set({ +local default_whitelist = Set({ 'loadstring', 'select', '_VERSION', @@ -71,12 +71,12 @@ do _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) local _class_0 = setmetatable({ - __init = function(self, lint_errors, ...) - if lint_errors == nil then - lint_errors = { } + __init = function(self, whitelist_globals, ...) + if whitelist_globals == nil then + whitelist_globals = default_whitelist end - self.lint_errors = lint_errors _parent_0.__init(self, ...) + self.lint_errors = { } local vc = self.value_compilers self.value_compilers = setmetatable({ ref = function(block, val) @@ -157,8 +157,35 @@ format_lint = function(errors, code, header) end return table.concat(formatted, "\n\n") end +local whitelist_for_file +do + local lint_config + whitelist_for_file = function(fname) + if not (lint_config) then + lint_config = { } + pcall(function() + lint_config = require("lint_config") + end) + end + if not (lint_config.whitelist_globals) then + return default_whitelist + end + local final_list = { } + for pattern, list in pairs(lint_config.whitelist_globals) do + if fname:match(pattern) then + for _index_0 = 1, #list do + local item = list[_index_0] + insert(final_list, item) + end + end + end + return setmetatable(Set(final_list), { + __index = default_whitelist + }) + end +end local lint_code -lint_code = function(code, name) +lint_code = function(code, name, whitelist_globals) if name == nil then name = "string input" end @@ -167,7 +194,7 @@ lint_code = function(code, name) if not (tree) then return nil, err end - local scope = LinterBlock() + local scope = LinterBlock(whitelist_globals) scope:stms(tree) return format_lint(scope.lint_errors, code, name) end @@ -177,7 +204,7 @@ lint_file = function(fname) if not (f) then return nil, err end - return lint_code(f:read("*a"), fname) + return lint_code(f:read("*a"), fname, whitelist_for_file(fname)) end return { lint_code = lint_code, diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 9d883d39..93730bcd 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -4,7 +4,7 @@ import Set from require "moonscript.data" import Block from require "moonscript.compile" -- globals allowed to be referenced -whitelist_globals = Set { +default_whitelist = Set { 'loadstring' 'select' '_VERSION' @@ -49,8 +49,9 @@ whitelist_globals = Set { } class LinterBlock extends Block - new: (@lint_errors={}, ...) => + new: (whitelist_globals=default_whitelist, ...) => super ... + @lint_errors = {} vc = @value_compilers @value_compilers = setmetatable { @@ -94,19 +95,42 @@ format_lint = (errors, code, header) -> table.concat formatted, "\n\n" -lint_code = (code, name="string input") -> +-- { +-- whitelist_globals: { +-- ["some_file_pattern"]: { +-- "some_var", "another_var" +-- } +-- } +-- } +whitelist_for_file = do + local lint_config + (fname) -> + unless lint_config + lint_config = {} + pcall -> lint_config = require "lint_config" + + return default_whitelist unless lint_config.whitelist_globals + final_list = {} + for pattern, list in pairs lint_config.whitelist_globals + if fname\match(pattern) + for item in *list + insert final_list, item + + setmetatable Set(final_list), __index: default_whitelist + +lint_code = (code, name="string input", whitelist_globals) -> parse = require "moonscript.parse" tree, err = parse.string code return nil, err unless tree - scope = LinterBlock! + scope = LinterBlock whitelist_globals scope\stms tree format_lint scope.lint_errors, code, name lint_file = (fname) -> f, err = io.open fname return nil, err unless f - lint_code f\read("*a"), fname + lint_code f\read("*a"), fname, whitelist_for_file fname { :lint_code, :lint_file } From 49b1c6e8a34650a5e0dbd33fccf8a3061da755de Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 27 Dec 2013 15:32:06 -0800 Subject: [PATCH 004/344] show failed files when linting --- bin/moonc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index 7ce00f37..7c6f1f34 100755 --- a/bin/moonc +++ b/bin/moonc @@ -390,9 +390,11 @@ if opts.w then elseif opts.l then for _, fname in pairs(files) do lint = require "moonscript.cmd.lint" - local res = lint.lint_file(fname) + local res, err = lint.lint_file(fname) if res then io.stderr:write(res .. "\n\n") + elseif err then + io.stderr:write(fname .. "\n" .. err.. "\n\n") end end else From ca4427cf6d5cca874746f95dbbef898970730685 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 27 Dec 2013 15:57:01 -0800 Subject: [PATCH 005/344] add missing functions to default whitelist, sort it --- moonscript/cmd/lint.lua | 58 +++++++++++++++++++++------------------- moonscript/cmd/lint.moon | 58 +++++++++++++++++++++------------------- 2 files changed, 60 insertions(+), 56 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 9dbbcc53..a8c08903 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -14,43 +14,45 @@ do Block = _obj_0.Block end local default_whitelist = Set({ - 'loadstring', - 'select', + '_G', '_VERSION', - 'pcall', - 'package', + 'assert', + 'bit32', + 'collectgarbage', + 'coroutine', + 'debug', + 'dofile', 'error', - 'rawget', - 'pairs', - 'xpcall', - 'rawlen', + 'getfenv', + 'getmetatable', 'io', - 'loadfile', 'ipairs', - 'table', - 'require', - 'os', + 'load', + 'loadfile', + 'loadstring', + 'math', 'module', - 'debug', - 'type', - 'getmetatable', + 'next', + 'os', + 'package', + 'pairs', + 'pcall', + 'print', 'rawequal', - 'dofile', - 'unpack', - 'math', - 'load', - 'bit32', - 'string', + 'rawget', + 'rawlen', 'rawset', - 'tostring', - 'print', - 'assert', - '_G', - 'next', + 'require', + 'select', + 'setfenv', 'setmetatable', + 'string', + 'table', 'tonumber', - 'collectgarbage', - 'coroutine', + 'tostring', + 'type', + 'unpack', + 'xpcall', "nil", "true", "false" diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 93730bcd..4add665b 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -5,43 +5,45 @@ import Block from require "moonscript.compile" -- globals allowed to be referenced default_whitelist = Set { - 'loadstring' - 'select' + '_G' '_VERSION' - 'pcall' - 'package' + 'assert' + 'bit32' + 'collectgarbage' + 'coroutine' + 'debug' + 'dofile' 'error' - 'rawget' - 'pairs' - 'xpcall' - 'rawlen' + 'getfenv' + 'getmetatable' 'io' - 'loadfile' 'ipairs' - 'table' - 'require' - 'os' + 'load' + 'loadfile' + 'loadstring' + 'math' 'module' - 'debug' - 'type' - 'getmetatable' + 'next' + 'os' + 'package' + 'pairs' + 'pcall' + 'print' 'rawequal' - 'dofile' - 'unpack' - 'math' - 'load' - 'bit32' - 'string' + 'rawget' + 'rawlen' 'rawset' - 'tostring' - 'print' - 'assert' - '_G' - 'next' + 'require' + 'select' + 'setfenv' 'setmetatable' + 'string' + 'table' 'tonumber' - 'collectgarbage' - 'coroutine' + 'tostring' + 'type' + 'unpack' + 'xpcall' "nil" "true" From dd1a06ff1a0018cabf8e520ed259a405218cb17b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 30 Dec 2013 01:38:22 -0800 Subject: [PATCH 006/344] update thoughts --- thoughts | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/thoughts b/thoughts index 735f6684..dfd6b69d 100644 --- a/thoughts +++ b/thoughts @@ -3,16 +3,10 @@ # # --- local * and local ^ - --- seems like running in moon messes up require order - - don't reuse _, put a local on it, so we don't keep around trash -- swithc X with Func -- or= and= - - error with stray comma at end of line * multiline comments @@ -20,22 +14,18 @@ * combine for and if line decorators -* export later? nah - - x = 232 - export x - - * allow return anywhere in block -* any/every keywords for comprehensions? (what about iterators) +-- all function literals have a string that is their function definition -* let array items in table be defined without {} when indented (no, too similar to arguments) +-- super should work here: --- for searching? for returning to accumulator early? -x = for thing in *things - if is_important thing - break thing +thing = Thing! +thing.method = -> + super 1,2,3 --- all function literals have a string that is their function definition +-- goes to +thing.method = function(self) do + self.__class:method(1,2,3) +end From 23a99aa365f2f81ec42a994f85c1708a87dc4a91 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 30 Dec 2013 01:43:25 -0800 Subject: [PATCH 007/344] cleaning up --- moonscript/data.lua | 24 +++++++++++------------- moonscript/data.moon | 20 ++++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/moonscript/data.lua b/moonscript/data.lua index 69176969..2e159c83 100644 --- a/moonscript/data.lua +++ b/moonscript/data.lua @@ -5,12 +5,12 @@ do end local Set Set = function(items) - local self = { } + local _tbl_0 = { } for _index_0 = 1, #items do - local key = items[_index_0] - self[key] = true + local k = items[_index_0] + _tbl_0[k] = true end - return self + return _tbl_0 end local Stack do @@ -21,9 +21,13 @@ do pop = function(self) return remove(self) end, - push = function(self, value) + push = function(self, value, ...) insert(self, value) - return value + if ... then + return self:push(...) + else + return value + end end, top = function(self) return self[#self] @@ -32,13 +36,7 @@ do _base_0.__index = _base_0 local _class_0 = setmetatable({ __init = function(self, ...) - local _list_0 = { - ... - } - for _index_0 = 1, #_list_0 do - local v = _list_0[_index_0] - self:push(v) - end + self:push(...) return nil end, __base = _base_0, diff --git a/moonscript/data.moon b/moonscript/data.moon index a9fbf328..4bb9b4fb 100644 --- a/moonscript/data.moon +++ b/moonscript/data.moon @@ -2,30 +2,30 @@ import concat, remove, insert from table -Set = (items) -> - self = {} - self[key] = true for key in *items - self +Set = (items) -> {k,true for k in *items} class Stack __tostring: => "" new: (...) => - @push v for v in *{...} + @push ... nil pop: => - remove self + remove @ - push: (value) => - insert self, value - value + push: (value, ...) => + insert @, value + if ... + @push ... + else + value top: => self[#self] -lua_keywords = Set{ +lua_keywords = Set { 'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'if', 'in', 'local', 'nil', 'not', 'or', From 5d9ee66c147662d6ab5ca7e1001b7edf48ce6319 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 30 Dec 2013 01:45:59 -0800 Subject: [PATCH 008/344] remove extra tables --- moonscript/compile.lua | 12 ++---------- moonscript/compile.moon | 4 ++-- moonscript/compile/statement.lua | 5 +---- moonscript/compile/statement.moon | 6 ++---- moonscript/compile/value.lua | 5 +---- moonscript/compile/value.moon | 5 ++--- 6 files changed, 10 insertions(+), 27 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 2b8e1630..368eead7 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -16,16 +16,8 @@ do local _obj_0 = require("moonscript.types") ntype, has_value = _obj_0.ntype, _obj_0.has_value end -local statement_compilers -do - local _obj_0 = require("moonscript.compile.statement") - statement_compilers = _obj_0.statement_compilers -end -local value_compilers -do - local _obj_0 = require("moonscript.compile.value") - value_compilers = _obj_0.value_compilers -end +local statement_compilers = require("moonscript.compile.statement") +local value_compilers = require("moonscript.compile.value") local concat, insert do local _obj_0 = table diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 812ad74e..27ab017c 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -7,8 +7,8 @@ import NameProxy, LocalName from require "moonscript.transform.names" import Set from require "moonscript.data" import ntype, has_value from require "moonscript.types" -import statement_compilers from require "moonscript.compile.statement" -import value_compilers from require "moonscript.compile.value" +statement_compilers = require "moonscript.compile.statement" +value_compilers = require "moonscript.compile.value" import concat, insert from table import pos_to_line, get_closest_line, trim, unpack from util diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index 65cc2d5e..abccd627 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -12,7 +12,7 @@ do local _obj_0 = table concat, insert = _obj_0.concat, _obj_0.insert end -local statement_compilers = { +return { raw = function(self, node) return self:add(node[2]) end, @@ -243,6 +243,3 @@ local statement_compilers = { end, noop = function(self) end } -return { - statement_compilers = statement_compilers -} diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index cf89e144..996b3855 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -6,7 +6,7 @@ import reversed, unpack from util import ntype from require "moonscript.types" import concat, insert from table -statement_compilers = +{ raw: (node) => @add node[2] lines: (node) => @@ -136,6 +136,4 @@ statement_compilers = \stms node[2] noop: => -- nothing! - - -{ :statement_compilers } +} diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 0a9acd80..560352ba 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -22,7 +22,7 @@ local string_chars = { ["\r"] = "\\r", ["\n"] = "\\n" } -local value_compilers = { +return { exp = function(self, node) local _comp _comp = function(i, value) @@ -316,6 +316,3 @@ local value_compilers = { return tostring(value) end } -return { - value_compilers = value_compilers -} diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 27aa9f2f..515fed87 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -14,7 +14,7 @@ string_chars = { "\n": "\\n" } -value_compilers = +{ -- list of values separated by binary operators exp: (node) => _comp = (i, value) -> @@ -197,5 +197,4 @@ value_compilers = @send "varargs" tostring value - -{ :value_compilers } +} From 314272c3763e85dd5f4d63b10417708afc280412 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 30 Dec 2013 01:52:13 -0800 Subject: [PATCH 009/344] don't create unnecessary tables --- moonscript/transform/names.lua | 36 ++++++++++++--------------------- moonscript/transform/names.moon | 14 ++++++------- 2 files changed, 19 insertions(+), 31 deletions(-) diff --git a/moonscript/transform/names.lua b/moonscript/transform/names.lua index abc0d5b9..a7584da1 100644 --- a/moonscript/transform/names.lua +++ b/moonscript/transform/names.lua @@ -47,31 +47,21 @@ do return self.name end, chain = function(self, ...) - local items - do - local _accum_0 = { } - local _len_0 = 1 - local _list_0 = { - ... - } - for _index_0 = 1, #_list_0 do - local i = _list_0[_index_0] - if type(i) == "string" then - _accum_0[_len_0] = { - "dot", - i - } - else - _accum_0[_len_0] = i - end - _len_0 = _len_0 + 1 + local items = { + base = self, + ... + } + for k, v in ipairs(items) do + if type(v) == "string" then + items[k] = { + "dot", + v + } + else + items[k] = v end - items = _accum_0 end - return build.chain({ - base = self, - unpack(items) - }) + return build.chain(items) end, index = function(self, key) if type(key) == "string" then diff --git a/moonscript/transform/names.moon b/moonscript/transform/names.moon index daf17ed9..220d862d 100644 --- a/moonscript/transform/names.moon +++ b/moonscript/transform/names.moon @@ -18,16 +18,14 @@ class NameProxy @name chain: (...) => - items = for i in *{...} - if type(i) == "string" - {"dot", i} + items = { base: @, ... } + for k,v in ipairs items + items[k] = if type(v) == "string" + {"dot", v} else - i + v - build.chain { - base: self - unpack items - } + build.chain items index: (key) => if type(key) == "string" From 9c5940d439fc592cc72af6b07804b106d0ef745b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 2 Jan 2014 22:27:06 -0800 Subject: [PATCH 010/344] fix typo --- docs/reference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference.md b/docs/reference.md index aab65d09..6af574df 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -644,7 +644,7 @@ have_coins = false if have_coins then print "Got coins" else print "No coins" ``` -Because if statements can be used as expressions, this can able be written as: +Because if statements can be used as expressions, this can also be written as: ```moon have_coins = false From 6aa545e4d325693c09e31f9cd8dfff92af3b5efd Mon Sep 17 00:00:00 2001 From: Lee Miller Date: Fri, 3 Jan 2014 23:27:35 -0800 Subject: [PATCH 011/344] Docs typo fix --- docs/reference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference.md b/docs/reference.md index 6af574df..42992670 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -556,7 +556,7 @@ for item in *items do print item for j = 1,10,3 do print j ``` -A for loop can also be used an expression. The last statement in the body of +A for loop can also be used as an expression. The last statement in the body of the for loop is coerced into an expression and appended to an accumulating array table. From d3f0a455e75dfabe5461cd566b15049063a36820 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 4 Jan 2014 15:41:20 -0800 Subject: [PATCH 012/344] also listen for moved_to --- bin/moonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index 7c6f1f34..dec5ad7c 100755 --- a/bin/moonc +++ b/bin/moonc @@ -314,7 +314,7 @@ function create_watcher(files) local wd_table = {} local handle = inotify.init() for _, dir in ipairs(dirs) do - local wd = handle:addwatch(dir, inotify.IN_CLOSE_WRITE) + local wd = handle:addwatch(dir, inotify.IN_CLOSE_WRITE, inotify.IN_MOVED_TO) wd_table[wd] = dir end From f49e88b470fa78c45f639aeec1178d2e7291c71e Mon Sep 17 00:00:00 2001 From: Eloff Date: Sun, 5 Jan 2014 01:25:04 -0500 Subject: [PATCH 013/344] Stopped swallowing errors in moonloader. --- moonscript/init.lua | 9 ++++++--- moonscript/init.moon | 10 +++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/moonscript/init.lua b/moonscript/init.lua index 2488d6d9..deb1a8cc 100644 --- a/moonscript/init.lua +++ b/moonscript/init.lua @@ -56,10 +56,13 @@ moon_loader = function(name) if file then local text = file:read("*a") file:close() - return loadstring(text, file_path) - else - return nil, "Could not find moon file" + local res, err = loadstring(text, file_path) + if not res then + error(err) + end + return res end + return nil, "Could not find moon file" end if not package.moonpath then package.moonpath = create_moonpath(package.path) diff --git a/moonscript/init.moon b/moonscript/init.moon index 2c9fe413..977c63ba 100644 --- a/moonscript/init.moon +++ b/moonscript/init.moon @@ -47,9 +47,13 @@ moon_loader = (name) -> if file text = file\read "*a" file\close! - loadstring text, file_path - else - nil, "Could not find moon file" + res, err = loadstring text, file_path + if not res + error err + + return res + + return nil, "Could not find moon file" if not package.moonpath package.moonpath = create_moonpath package.path From c1f53271143aef88bfcc4ab684f131782a5f5d65 Mon Sep 17 00:00:00 2001 From: Eloff Date: Sun, 5 Jan 2014 15:25:09 -0500 Subject: [PATCH 014/344] Add support for LL and ULL number suffixes (LuaJIT) --- moonscript/parse.lua | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 499ae1b7..eb4bffc4 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -51,7 +51,8 @@ local AlphaNum = R("az", "AZ", "09", "__") local _Name = C(R("az", "AZ", "__") * AlphaNum^0) local Name = Space * _Name -local Num = P"0x" * R("09", "af", "AF")^1 + +local Num = P"0x" * R("09", "af", "AF")^1 * (S"uU"^-1 * S"lL"^2)^-1 + + R"09"^1 * (S"uU"^-1 * S"lL"^2) + ( R"09"^1 * (P"." * R"09"^1)^-1 + P"." * R"09"^1 From a926e968133c96211cf622f0ad173b282e0de216 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 6 Jan 2014 10:38:01 -0800 Subject: [PATCH 015/344] specs for new number literals #124 --- spec/inputs/literals.moon | 8 ++++++++ spec/outputs/literals.lua | 6 ++++++ 2 files changed, 14 insertions(+) diff --git a/spec/inputs/literals.moon b/spec/inputs/literals.moon index 7f58b6ee..c3a24a6f 100644 --- a/spec/inputs/literals.moon +++ b/spec/inputs/literals.moon @@ -15,6 +15,14 @@ .2323e-1 .2323e13434 + +1LL +1ULL +9332LL +9332 +0x2aLL +0x2aULL + [[ hello world ]] [=[ hello world ]=] diff --git a/spec/outputs/literals.lua b/spec/outputs/literals.lua index b49c57a8..d8fc7e2b 100644 --- a/spec/outputs/literals.lua +++ b/spec/outputs/literals.lua @@ -10,6 +10,12 @@ _ = 0xABCDEF _ = .2323 _ = .2323e-1 _ = .2323e13434 +_ = 1LL +_ = 1ULL +_ = 9332LL +_ = 9332 +_ = 0x2aLL +_ = 0x2aULL _ = [[ hello world ]] _ = [=[ hello world ]=] _ = [====[ hello world ]====] From 9d2665f8dc08f1f6326df02e399762601e6fb366 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Feb 2014 09:29:39 -0800 Subject: [PATCH 016/344] let colon stubs in assignments get simplified --- moonscript/transform.lua | 11 +++++++++-- moonscript/transform.moon | 10 +++++++++- spec/outputs/class.lua | 12 +++++------- spec/outputs/stub.lua | 13 +++++++------ 4 files changed, 30 insertions(+), 16 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 449e983a..a56069b6 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -378,6 +378,11 @@ Statement = Transformer({ if num_names == 1 and num_values == 1 then local first_value = values[1] local first_name = names[1] + local first_type = ntype(first_value) + if first_type == "chain" then + first_value = Value:transform_once(self, first_value) + first_type = ntype(first_value) + end local _exp_0 = ntype(first_value) if "block_exp" == _exp_0 then local block_body = first_value[2] @@ -397,6 +402,8 @@ Statement = Transformer({ }) elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 or "foreach" == _exp_0 or "for" == _exp_0 or "while" == _exp_0 then return build.assign_one(first_name, Value:transform_once(self, first_value)) + else + values[1] = first_value end end local transformed @@ -1538,7 +1545,7 @@ Value = Transformer({ local base_name = NameProxy("base") local fn_name = NameProxy("fn") local is_super = ntype(node[2]) == "ref" and node[2][2] == "super" - return self.transform.value(build.block_exp({ + return build.block_exp({ build.assign({ names = { base_name @@ -1580,7 +1587,7 @@ Value = Transformer({ }) } }) - })) + }) end end, block_exp = function(self, node) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 9175d417..863f1561 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -206,6 +206,12 @@ Statement = Transformer { if num_names == 1 and num_values == 1 first_value = values[1] first_name = names[1] + first_type = ntype first_value + + -- reduce colon stub chain to block exp + if first_type == "chain" + first_value = Value\transform_once @, first_value + first_type = ntype first_value switch ntype first_value when "block_exp" @@ -220,6 +226,8 @@ Statement = Transformer { when "comprehension", "tblcomprehension", "foreach", "for", "while" return build.assign_one first_name, Value\transform_once @, first_value + else + values[1] = first_value -- bubble cascading assigns transformed = if num_values == 1 @@ -922,7 +930,7 @@ Value = Transformer { fn_name = NameProxy "fn" is_super = ntype(node[2]) == "ref" and node[2][2] == "super" - @transform.value build.block_exp { + build.block_exp { build.assign { names: {base_name} values: {node} diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index 1e3a3015..67227911 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -266,13 +266,11 @@ do local _base_0 = { val = 2323, something = function(self) - return (function() - local _base_1 = _parent_0 - local _fn_0 = _base_1.something - return function(...) - return _fn_0(self, ...) - end - end)() + local _base_1 = _parent_0 + local _fn_0 = _base_1.something + return function(...) + return _fn_0(self, ...) + end end } _base_0.__index = _base_0 diff --git a/spec/outputs/stub.lua b/spec/outputs/stub.lua index 1f02d09c..4ceac088 100644 --- a/spec/outputs/stub.lua +++ b/spec/outputs/stub.lua @@ -4,19 +4,20 @@ local x = { return print(self.val) end } -local fn = (function() +local fn +do local _base_0 = x local _fn_0 = _base_0.val - return function(...) + fn = function(...) return _fn_0(_base_0, ...) end -end)() +end print(fn()) print(x:val()) -x = (function(...) +do local _base_0 = hello(...) local _fn_0 = _base_0.world - return function(...) + x = function(...) return _fn_0(_base_0, ...) end -end)(...) \ No newline at end of file +end \ No newline at end of file From 6307c6a9c1ce60c0b46506ff91d073f70d39ca0e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Feb 2014 13:07:57 -0800 Subject: [PATCH 017/344] compile before installing locally --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 30b30229..45b90f8a 100644 --- a/Makefile +++ b/Makefile @@ -2,13 +2,13 @@ test:: busted -p "_spec.moon$$" -local: +local: compile luarocks make --local moonscript-dev-1.rockspec global: sudo luarocks make moonscript-dev-1.rockspec -compile: +compile:: bin/moonc moon/ moonscript/ From 727fe0eb1cf627f174ce7c3b5874e1f242cb902b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Feb 2014 13:08:27 -0800 Subject: [PATCH 018/344] show file in error message in moon loader --- moonscript/base.lua | 2 +- moonscript/base.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/base.lua b/moonscript/base.lua index f3b6b392..06b3f6d6 100644 --- a/moonscript/base.lua +++ b/moonscript/base.lua @@ -62,7 +62,7 @@ moon_loader = function(name) file:close() local res, err = loadstring(text, file_path) if not res then - error(err) + error(file_path .. ": " .. err) end return res end diff --git a/moonscript/base.moon b/moonscript/base.moon index fe8e3def..256b0d76 100644 --- a/moonscript/base.moon +++ b/moonscript/base.moon @@ -48,7 +48,7 @@ moon_loader = (name) -> file\close! res, err = loadstring text, file_path if not res - error err + error file_path .. ": " .. err return res From 38d06207ae23daff83acd5f8a11436e983967386 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 8 Feb 2014 18:31:21 -0800 Subject: [PATCH 019/344] enhance missing value compiler error message --- moonscript/compile.lua | 44 ++++++++++++++++++++++++----------------- moonscript/compile.moon | 43 +++++++++++++++++++++++++--------------- 2 files changed, 53 insertions(+), 34 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 368eead7..2e4ff908 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -474,8 +474,12 @@ do action = node[1] end local fn = self.value_compilers[action] - if not fn then - error("Failed to compile value: " .. dump.value(node)) + if not (fn) then + error({ + "compile-error", + "Failed to find value compiler for: " .. dump.value(node), + node[-1] + }) end local out = fn(self, node, ...) if type(node) == "table" and node[-1] then @@ -659,13 +663,17 @@ do end local format_error format_error = function(msg, pos, file_str) - local line = pos_to_line(file_str, pos) - local line_str - line_str, line = get_closest_line(file_str, line) - line_str = line_str or "" + local line_message + if pos then + local line = pos_to_line(file_str, pos) + local line_str + line_str, line = get_closest_line(file_str, line) + line_str = line_str or "" + line_message = (" [%d] >> %s"):format(line, trim(line_str)) + end return concat({ "Compile error: " .. msg, - (" [%d] >> %s"):format(line, trim(line_str)) + line_message }, "\n") end local value @@ -689,27 +697,27 @@ tree = function(tree, options) return scope:root_stms(tree) end) local success, err = coroutine.resume(runner) - if not success then - local error_msg + if not (success) then + local error_msg, error_pos if type(err) == "table" then local error_type = err[1] - if error_type == "user-error" then - error_msg = err[2] + local _exp_0 = err[1] + if "user-error" == _exp_0 or "compile-error" == _exp_0 then + error_msg, error_pos = unpack(err, 2) else - error_msg = error("Unknown error thrown", util.dump(error_msg)) + error_msg, error_pos = error("Unknown error thrown", util.dump(error_msg)) end else - error_msg = concat({ + error_msg, error_pos = concat({ err, debug.traceback(runner) }, "\n") end - return nil, error_msg, scope.last_pos - else - local lua_code = scope:render() - local posmap = scope._lines:flatten_posmap() - return lua_code, posmap + return nil, error_msg, error_pos or scope.last_pos end + local lua_code = scope:render() + local posmap = scope._lines:flatten_posmap() + return lua_code, posmap end do local data = require("moonscript.data") diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 27ab017c..31762805 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -341,7 +341,12 @@ class Block node[1] fn = @value_compilers[action] - error "Failed to compile value: "..dump.value node if not fn + unless fn + error { + "compile-error" + "Failed to find value compiler for: " .. dump.value node + node[-1] + } out = fn self, node, ... @@ -416,12 +421,15 @@ class RootBlock extends Block table.concat buffer format_error = (msg, pos, file_str) -> - line = pos_to_line file_str, pos - line_str, line = get_closest_line file_str, line - line_str = line_str or "" + line_message = if pos + line = pos_to_line file_str, pos + line_str, line = get_closest_line file_str, line + line_str = line_str or "" + (" [%d] >> %s")\format line, trim line_str + concat { "Compile error: "..msg - (" [%d] >> %s")\format line, trim line_str + line_message }, "\n" value = (value) -> @@ -440,21 +448,24 @@ tree = (tree, options={}) -> scope\root_stms tree success, err = coroutine.resume runner - if not success - error_msg = if type(err) == "table" + + unless success + error_msg, error_pos = if type(err) == "table" error_type = err[1] - if error_type == "user-error" - err[2] - else - error "Unknown error thrown", util.dump error_msg + switch err[1] + when "user-error", "compile-error" + unpack err, 2 + else + -- unknown error, bubble it + error "Unknown error thrown", util.dump error_msg else concat {err, debug.traceback runner}, "\n" - nil, error_msg, scope.last_pos - else - lua_code = scope\render! - posmap = scope._lines\flatten_posmap! - lua_code, posmap + return nil, error_msg, error_pos or scope.last_pos + + lua_code = scope\render! + posmap = scope._lines\flatten_posmap! + lua_code, posmap -- mmmm with data = require "moonscript.data" From df340f7104ca671aae0d398d681950d0501e5ecb Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 8 Feb 2014 18:41:29 -0800 Subject: [PATCH 020/344] avoid creating unnecessary functions when using return keyword with statements and block_exps --- moonscript/transform.lua | 40 +++++++++------ moonscript/transform.moon | 26 +++++++--- spec/inputs/lists.moon | 4 -- spec/inputs/return.moon | 55 ++++++++++++++++++++ spec/outputs/lists.lua | 19 +------ spec/outputs/return.lua | 102 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 201 insertions(+), 45 deletions(-) create mode 100644 spec/inputs/return.moon create mode 100644 spec/outputs/return.lua diff --git a/moonscript/transform.lua b/moonscript/transform.lua index a56069b6..803d5d70 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -329,15 +329,27 @@ Statement = Transformer({ return apply_to_last(body, implicitly_return(self)) end, ["return"] = function(self, node) - node[2] = Value:transform_once(self, node[2]) - if "block_exp" == ntype(node[2]) then - local block_exp = node[2] - local block_body = block_exp[2] - local idx = #block_body - node[2] = block_body[idx] - block_body[idx] = node - return build.group(block_body) + local ret_val = node[2] + local ret_val_type = ntype(ret_val) + if ret_val_type == "explist" and #ret_val == 2 then + ret_val = ret_val[2] + ret_val_type = ntype(ret_val) end + if types.cascading[ret_val_type] then + return implicitly_return(self)(ret_val) + end + if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" then + ret_val = Value:transform_once(self, ret_val) + if ntype(ret_val) == "block_exp" then + return build.group(apply_to_last(ret_val[2], function(stm) + return { + "return", + stm + } + end)) + end + end + node[2] = ret_val return node end, declare_glob = function(self, node) @@ -1418,13 +1430,11 @@ Value = Transformer({ end end if #node <= 3 then - return (function() - if type(node[3]) == "string" then - return node - else - return convert_part(node[3]) - end - end)() + if type(node[3]) == "string" then + return node + else + return convert_part(node[3]) + end end local e = { "exp", diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 863f1561..059f7d84 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -124,6 +124,7 @@ class Transformer transform: (scope, node, ...) => return node if @seen_nodes[node] + @seen_nodes[node] = true while true transformer = @transformers[ntype node] @@ -131,8 +132,10 @@ class Transformer transformer(scope, node, ...) or node else node + return node if res == node node = res + node bind: (scope) => @@ -173,17 +176,24 @@ Statement = Transformer { apply_to_last body, implicitly_return @ return: (node) => - node[2] = Value\transform_once @, node[2] + ret_val = node[2] + ret_val_type = ntype ret_val + + if ret_val_type == "explist" and #ret_val == 2 + ret_val = ret_val[2] + ret_val_type = ntype ret_val - if "block_exp" == ntype node[2] - block_exp = node[2] - block_body = block_exp[2] + if types.cascading[ret_val_type] + return implicitly_return(@) ret_val - idx = #block_body - node[2] = block_body[idx] - block_body[idx] = node - return build.group block_body + -- flatten things that create block exp + if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" + ret_val = Value\transform_once @, ret_val + if ntype(ret_val) == "block_exp" + return build.group apply_to_last ret_val[2], (stm)-> + {"return", stm} + node[2] = ret_val node declare_glob: (node) => diff --git a/spec/inputs/lists.moon b/spec/inputs/lists.moon index 91e559e2..c1191857 100644 --- a/spec/inputs/lists.moon +++ b/spec/inputs/lists.moon @@ -69,8 +69,4 @@ print thing for thing in *test -> a = b for row in *rows --- testing implicit return --> x for x in *things --> [x for x in *things] - diff --git a/spec/inputs/return.moon b/spec/inputs/return.moon new file mode 100644 index 00000000..61d3dcad --- /dev/null +++ b/spec/inputs/return.moon @@ -0,0 +1,55 @@ +-- testing `return` propagation + +-> x for x in *things +-> [x for x in *things] + + +-- doesn't make sense on purpose +do + return x for x in *things + +do + return [x for x in *things] + +do + return {x,y for x,y in *things} + +-> + if a + if a + a + else + b + elseif b + if a + a + else + b + else + if a + a + else + b + + +do + return if a + if a + a + else + b + elseif b + if a + a + else + b + else + if a + a + else + b + +-> a\b +do a\b + + diff --git a/spec/outputs/lists.lua b/spec/outputs/lists.lua index 81aaa5b9..4905ee0c 100644 --- a/spec/outputs/lists.lua +++ b/spec/outputs/lists.lua @@ -254,27 +254,10 @@ for _index_0 = 1, #test do local thing = test[_index_0] print(thing) end -local _ -_ = function() +return function() local _list_0 = rows for _index_0 = 1, #_list_0 do local row = _list_0[_index_0] a = b end -end -_ = function() - for _index_0 = 1, #things do - x = things[_index_0] - _ = x - end -end -return function() - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #things do - x = things[_index_0] - _accum_0[_len_0] = x - _len_0 = _len_0 + 1 - end - return _accum_0 end \ No newline at end of file diff --git a/spec/outputs/return.lua b/spec/outputs/return.lua new file mode 100644 index 00000000..4eeaca20 --- /dev/null +++ b/spec/outputs/return.lua @@ -0,0 +1,102 @@ +local _ +_ = function() + local _list_0 = things + for _index_0 = 1, #_list_0 do + local x = _list_0[_index_0] + _ = x + end +end +_ = function() + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = things + for _index_0 = 1, #_list_0 do + local x = _list_0[_index_0] + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + end + return _accum_0 +end +do + local _list_0 = things + for _index_0 = 1, #_list_0 do + local x = _list_0[_index_0] + return x + end +end +do + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = things + for _index_0 = 1, #_list_0 do + local x = _list_0[_index_0] + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + end + return _accum_0 +end +do + local _tbl_0 = { } + local _list_0 = things + for _index_0 = 1, #_list_0 do + local x, y = _list_0[_index_0] + _tbl_0[x] = y + end + return _tbl_0 +end +_ = function() + if a then + if a then + return a + else + return b + end + elseif b then + if a then + return a + else + return b + end + else + if a then + return a + else + return b + end + end +end +do + if a then + if a then + return a + else + return b + end + elseif b then + if a then + return a + else + return b + end + else + if a then + return a + else + return b + end + end +end +_ = function() + local _base_0 = a + local _fn_0 = _base_0.b + return function(...) + return _fn_0(_base_0, ...) + end +end +do + local _base_0 = a + local _fn_0 = _base_0.b + return function(...) + return _fn_0(_base_0, ...) + end +end \ No newline at end of file From da1bc6f8ec0d23e2bd3cf1be0ea592c06c30f8b5 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 15 Feb 2014 14:07:36 -0800 Subject: [PATCH 021/344] use xpcall and get backtrace --- moonscript/parse.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index eb4bffc4..f6d84dd5 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -616,13 +616,17 @@ local build_grammar = wrap_env(function() end local tree - local pass, err = pcall(function(...) - tree = self._g:match(str, ...) - end, ...) + local parse_args = {...} + + local pass, err = xpcall(function() + tree = self._g:match(str, unpack(parse_args)) + end, function(err) + return debug.traceback(err, 2) + end) -- regular error, let it bubble up if type(err) == "string" then - error(err) + return nil, err end if not tree then From 437be6efb52902dcd9d8587a642495e797e4f0f5 Mon Sep 17 00:00:00 2001 From: Yorwba Date: Sat, 1 Feb 2014 22:27:48 +0100 Subject: [PATCH 022/344] Fixed lua/moon line mapping --- moonscript/compile.lua | 3 +++ moonscript/compile.moon | 1 + 2 files changed, 4 insertions(+) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 2e4ff908..379c9c91 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -61,6 +61,9 @@ do local _exp_0 = mtype(l) if "string" == _exp_0 or DelayedLine == _exp_0 then line_no = line_no + 1 + for _ in l:gmatch("\n") do + line_no = line_no + 1 + end out[line_no] = posmap[i] elseif Lines == _exp_0 then local _ diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 31762805..95395669 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -44,6 +44,7 @@ class Lines switch mtype l when "string", DelayedLine line_no += 1 + line_no += 1 for _ in l\gmatch"\n" out[line_no] = posmap[i] when Lines _, line_no = l\flatten_posmap line_no, out From 5f4d476ad0a93cea93cb6da94ffaf55a50937ac5 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 4 Mar 2014 10:12:09 -0800 Subject: [PATCH 023/344] add posmap matching specs --- spec/error_rewriting_spec.moon | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/spec/error_rewriting_spec.moon b/spec/error_rewriting_spec.moon index d2f55c98..3c6d1280 100644 --- a/spec/error_rewriting_spec.moon +++ b/spec/error_rewriting_spec.moon @@ -3,6 +3,8 @@ moonscript = require "moonscript.base" errors = require "moonscript.errors" util = require "moonscript.util" +import unindent from require "spec.helpers" + get_rewritten_line_no = (fname) -> fname = "spec/error_inputs/#{fname}.moon" chunk = moonscript.loadfile fname @@ -28,3 +30,35 @@ describe "error rewriting", -> it "should rewrite line number", -> assert.same get_rewritten_line_no(name), expected_no +describe "line map", -> + import to_lua from require "moonscript.base" + + it "should create line table", -> + moon_code = unindent [[ + print "hello world" + if something + print "cats" + ]] + + lua_code, posmap = assert to_lua moon_code + -- print util.debug_posmap(posmap, moon_code, lua_code) + assert.same { 7, 29, 42, 27 }, posmap + + it "should create line table for multiline string", -> + moon_code = unindent [[ + print "one" + x = [==[ + one + two + thre + yes + no + ]==] + print "two" + ]] + + lua_code, posmap = assert to_lua moon_code + -- print util.debug_posmap(posmap, moon_code, lua_code) + assert.same {[1]: 7, [7]: 19, [8]: 63}, posmap + + From 318192cd41e38806302680649f3e08b00909d3ae Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 4 Mar 2014 10:13:05 -0800 Subject: [PATCH 024/344] fix indentation from patch --- moonscript/compile.moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 95395669..af7242eb 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -44,7 +44,7 @@ class Lines switch mtype l when "string", DelayedLine line_no += 1 - line_no += 1 for _ in l\gmatch"\n" + line_no += 1 for _ in l\gmatch"\n" out[line_no] = posmap[i] when Lines _, line_no = l\flatten_posmap line_no, out From 7bfbe66c099f758e08e1161a8cf55fd3e6e808ed Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 4 Mar 2014 10:26:29 -0800 Subject: [PATCH 025/344] put changelog in repository --- CHANGELOG.md | 232 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 CHANGELOG.md diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 00000000..b834c435 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,232 @@ + +# MoonScript v0.2.4 (2013-07-02) + +I'm happy to announce [MoonScript][1] version 0.2.4, the CoffeeScript inspired +language that compiles to Lua. It's been about 5 months since the last release. + +## Changes + +* The way the subtraction operator works has changed. There was always a little confusion as to the rules regarding whitespace around it and it was recommended to always add whitespace around the operator when doing subtraction. Not anymore. Hopefully it now [works how you would expect](http://moonscript.org/reference/#considerations). (`a-b` compiles to `a - b` and not `a(-b)` anymore). +* The `moon` library is no longer sets a global variable and instead returns the module. Your code should now be: + +```moon +moon = require "moon" +``` + +* Generated code will reuse local variables when appropriate. Local variables are guaranteed to not have side effects when being accessed as opposed to expressions and global variables. MoonScript will now take advantage of this and reuse those variable without creating and copying to a temporary name. +* Reduced the creation of anonymous functions that are called immediately. + MoonScript uses this technique to convert a series of statements into a single expression. It's inefficient because it allocates a new function object and has to do a function call. It also obfuscates stack traces. MoonScript will flatten these functions into the current scope in a lot of situations now. +* Reduced the amount of code generated for classes. Parent class code it left out if there is no parent. + +## New Things + +* You can now put line breaks inside of string literals. It will be replaced with `\n` in the generated code. + +```moon +x = "hello +world" +``` + +* Added `moonscript.base` module. It's a way of including the `moonscript` module without automatically installing the moonloader. +* You are free to use any whitespace around the name list in an import statement. It has the same rules as an array table, meaning you can delimit names with line breaks. + +```moon +import a, b + c, d from z +``` + +* Added significantly better tests. Previously the testing suite would only verify that code compiled to an expected string. Now there are unit tests that execute the code as well. This will make it easier to change the generated output while still guaranteeing the semantics are the same. + +## Bug Fixes + +* `b` is not longer treated as self assign in `{ a : b }` +* load functions will return `nil` instead of throwing error, as described in documentation +* fixed an issue with `moon.mixin` where it did not work as described + + +# MoonScript v0.2.3-2 (2013-01-29) + +Fixed bug with moonloader not loading anything + +# MoonScript v0.2.3 (2013-01-24) + +Today marks [MoonScript][1] version 0.2.3, the CoffeeScript inspired language +that compiles to Lua. It's been about 3 months since last release. I've got a +couple new features, fixes, Lua 5.2 support and a backwards incompatible +change. + +## Changes + +* For loops when used as expressions will no longer discard nil values when accumulating into an array table. **This is a backwards incompatible change**. Instead you should use the `continue` keyword to filter out iterations you don't want to keep. [Read more here](https://github.com/leafo/moonscript/issues/66). +* The `moonscript` module no longer sets a global value for `moonscript` and instead returns it. You should update your code: + +```moon +moonscript = require "moonscript" +``` + +## New Things + +* Lua 5.2 Support. The compiler can now run in either Lua 5.2 and 5.1 +* A switch `when` clause [can take multiple values](http://moonscript.org/reference/#switch), comma separated. +* Added [destructuring assignment](http://moonscript.org/reference/#destructuring_assignment). +* Added `local *` (and `local ^`) for [hoisting variable declarations](http://moonscript.org/reference/#local_statement) in the current scope +* List comprehensions and line decorators now support numeric loop syntax + +## Bug Fixes + +* Numbers that start with a dot, like `.03`, are correctly parsed +* Fixed typo in `fold` library function +* Fix declaration hoisting inside of class body, works the same as `local *` now + +## Other Stuff + +MoonScript has [made its way into GitHub](https://github.com/github/linguist/pull/246). `.moon` files should start to be recognized in the near future. + + +# MoonScript v0.2.2 (2012-11-03) + +Today marks [MoonScript][1] version 0.2.2, the CoffeeScript inspired language +that compiles to Lua. It's been approximately 11 months since the last release, +and I'd like to apologize for the long gap. Hopefully we'll see more frequent +updates in the future. + +## Changes + +* Compiled [files will now implicitly return](http://moonscript.org/reference/#implicit_returns_on_files) their last statement. Be careful, this might change what `require` returns. + +## New Things + +### The Language + +* Added [`continue` keyword](http://moonscript.org/reference/#continue) for skipping the current iteration in a loop. +* Added [string interpolation](http://moonscript.org/reference/#string_interpolation). +* Added [`do` expression and block](http://moonscript.org/reference/#do). +* Added `unless` as a block and line decorator. Is the inverse of `if`. +* Assignment can be used in an [`if` statement's expression](http://moonscript.org/reference/#with_assignment). +* Added `or=` and `and=` operators. +* `@@` can be prefixed in front of a name to access that name within `self.__class` +* `@` and `@@` can be [used as values](http://moonscript.org/reference/#_and__values) to reference `self` and `self.__class`. +* In class declarations it's possible to [assign to the class object](http://moonscript.org/reference/#class_variables) instead of the instance metatable by prefixing the key with `@`. +* Class methods can access [locals defined within the body](http://moonscript.org/reference/#class_declaration_statements) of the class declaration. +* Super classes are [notified when they are extended](http://moonscript.org/reference/#inheritance) from with an `__inherited` callback. +* Classes can now [implicitly return and be expressions](http://moonscript.org/reference/#anonymous_classes). +* `local` keyword returns, can be used for forward declaration or shadowing a variable. +* String literals can be used as keys in [table literals](http://moonscript.org/reference/#table_literals). +* Call methods on string literals without wrapping in parentheses: `"hello"\upper!` +* Table comprehensions can return a single value that is unpacked into the key and value. +* The expression in a [`with` statement can now be an assignment](http://moonscript.org/reference/#with_statement), to give a name to the expression that is being operated on. + + +### The API + +* The `load` functions can take an [optional last argument of options](http://moonscript.org/reference/#load_functions). + +### The Tools + +* The [online compiler](http://moonscript.org/compiler/) now runs through a web service instead of emscripten, should work reliably on any computer now. +* [Windows binaries](http://moonscript.org/bin/) have been updated. + +## Bug Fixes + +* Significantly improved the [line number rewriter](http://moonscript.org/reference/#error_rewriting). It should now accurately report all line numbers. +* Generic `for` loops correctly parse for multiple values as defined in Lua. +* Update expressions don't fail with certain combinations of precedence. +* All statements/expressions are allowed in a class body, not just some. +* `x = "hello" if something` will extract the declaration of `x` if it's not in scope yet. Preventing an impossible to access variable from being created. +* varargs, `...`, correctly bubble up through automatically generated anonymous functions. +* Compiler doesn't crash if you try to assign something that isn't assignable. +* Numerous other small fixes. See [commit log](https://github.com/leafo/moonscript/commits/master). + + +# MoonScript v0.2.0 (2011-12-12) + +Exactly 3 months ago I released [MoonScript][1]. The CoffeeScript inspired +language that compiles to Lua. Since then I've both written a lot of MoonScript +and enhanced the MoonScript compiler. + +Today I'm proud to release v0.2.0. I've got a handful of new features and bug fixes. + +## Changes + +* `,` is used instead of `:` for delimiting table slice parts. +* Class objects store the metatable of their instances in `__base`. `__base` is also used in inheritance when chaining metatables. + +## New Things + +### The Language + +* Added [key-value table comprehensions][4]. +* Added a [`switch` statement][7]. +* The body of a class can contain arbitrary expressions in addition to assigning properties. `self` in this scope refers to the class itself. +* Class objects themselves support accessing the properties of the superclass they extend (like instances). +* Class objects store their name as a string in the `__name` property. +* [Enhanced the `super` keyword][8] in instance methods. +* Bound methods can be created for an object by using `object\function_name` as a value. Called [function stubs][6]. +* Added `export *` statement to export all assigned names following the statement. +* Added `export ^` statement to export all assigning names that begin with a capital letter following the statement. +* `export` can be used before any assignment or class declaration to export just that assignment (or class declaration). +* Argument lists can be broken up over several lines with trailing comma. +* `:hello` is short hand for `hello: hello` inside of table literal. +* Added `..=` for string concatenation. +* `table.insert` no longer used to build accumlated values in comprehensions. + +### The API + +* Added `loadfile`, `loadstring`, and `dofile` functions to `moonscript` module to load/run MoonScript code. +* Added `to_lua` function to `moonscript` module to convert a MoonScript code string to Lua string. + +### The Tools + +* Created [prebuilt MoonScript Windows executables][2]. +* Wrote a [Textmate/Sublime Text bundle][9]. +* Wrote a [SciTE syntax highlighter with scintillua][10]. +* Created a [SciTE package for Windows][11] that has everything configured. +* Created an [online compiler and snippet site][12] using [emscripten][13]. +* Watch mode works on all platforms now. Uses polling if `inotify` is not + available. + +### Standard Library + +I'm now including a small set of useful functions in a single module called `moon`: + +```moon +require "moon" +``` + +Documentation is [available here][3]. + +## Bug Fixes + +* Windows line endings don't break the parser. +* Fixed issues when using `...` within comprehensions when the compiled code uses an intermediate function in the output. +* Names whose first characters happen to be a keyword don't break parser. +* Return statement can have no arguments +* argument names prefixed with `@` in function definitions work outside of classes work with default values. +* Fixed parse issues with the shorthand values within a `with` block. +* Numerous other small fixes. See [commit log][5]. + +## Other Stuff + +Since the first release, I've written one other project in MoonScript (other than the compiler). It's a static site generator called [sitegen][15]. It's what I now use to generate all of my project pages and this blog. + +# MoonScript 0.1.0 (2011-08-12) + +Initial release + + + [1]: http://moonscript.org + [2]: http://moonscript.org/bin/ + [3]: http://moonscript.org/reference/standard_lib.html + [4]: http://moonscript.org/reference/#table_comprehensions + [5]: https://github.com/leafo/moonscript/commits/master + [6]: http://moonscript.org/reference/#function_stubs + [7]: http://moonscript.org/reference/#switch + [8]: http://moonscript.org/reference/#super + [9]: https://github.com/leafo/moonscript-tmbundle + [10]: https://github.com/leafo/moonscript/tree/master/extra/scintillua + [11]: http://moonscript.org/scite/ + [12]: http://moonscript.org/compiler/ + [13]: http://emscripten.org + [14]: http://twitter.com/moonscript + [15]: http://leafo.net/sitegen/ + From ed939f31674659b7770fb1f33a01a16d1f4ea9e9 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 4 Mar 2014 10:48:39 -0800 Subject: [PATCH 026/344] start filling out changelog for new version --- CHANGELOG.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b834c435..2d9d4c33 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,24 @@ +# MoonScript v0.2.4 (TBR) + +## Changes + +Code generation altered. + +* colon stub simplified +* return's statement simplified + +## New Things + +* New code coverage tool built into `moonc` +* New linting tool built into `moonc`, identifies global variable references that don't pass whitelist +* Numbers can have `LL` and `ULL` suffixes for LuaJIT + +## Bugs Fixes + +* Error messages from `moonc` are written to standard error +* Moonloader correctly throws error when moon file can't be parsed, instead of skipping the module + # MoonScript v0.2.4 (2013-07-02) I'm happy to announce [MoonScript][1] version 0.2.4, the CoffeeScript inspired From ba9322901d9b3f47fd736e2c9c7fb62a9fa118ce Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 4 Mar 2014 23:41:07 -0800 Subject: [PATCH 027/344] update changelog for code generation changes --- CHANGELOG.md | 119 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 113 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2d9d4c33..b2d0bd87 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,113 @@ Code generation altered. -* colon stub simplified +Bound functions will avoid creating an anonymous function unless necessary. + +```moonscript +x = hello\world +``` + +**Before:** + +```lua +local x = (function() + local _base_0 = hello + local _fn_0 = _base_0.world + return function(...) + return _fn_0(_base_0, ...) + end +end)() +``` + +**After:** + +```lua +local x +do + local _base_0 = hello + local _fn_0 = _base_0.world + x = function(...) + return _fn_0(_base_0, ...) + end +end +``` + +Explicit return statement now avoids creating anonymous function for statements +where return can be cascaded into the body. + +```moon +-> + if test1 + return [x for x in *y] + + if test2 + return if true + "yes" + else + "no" + + false +``` + + +**Before:** + +```moonscript +local _ +_ = function() + if test1 then + return (function() + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = y + for _index_0 = 1, #_list_0 do + local x = _list_0[_index_0] + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + end + return _accum_0 + end)() + end + if test2 then + return (function() + if true then + return "yes" + else + return "no" + end + end)() + end + return false +end +``` + +**After:** + +```moonscript +local _ +_ = function() + if test1 then + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = y + for _index_0 = 1, #_list_0 do + local x = _list_0[_index_0] + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + end + return _accum_0 + end + if test2 then + if true then + return "yes" + else + return "no" + end + end + return false +end +``` + * return's statement simplified ## New Things @@ -18,6 +124,7 @@ Code generation altered. * Error messages from `moonc` are written to standard error * Moonloader correctly throws error when moon file can't be parsed, instead of skipping the module +* Line number rewriting is no longer incorrectly offset due to multiline strings # MoonScript v0.2.4 (2013-07-02) @@ -29,7 +136,7 @@ language that compiles to Lua. It's been about 5 months since the last release. * The way the subtraction operator works has changed. There was always a little confusion as to the rules regarding whitespace around it and it was recommended to always add whitespace around the operator when doing subtraction. Not anymore. Hopefully it now [works how you would expect](http://moonscript.org/reference/#considerations). (`a-b` compiles to `a - b` and not `a(-b)` anymore). * The `moon` library is no longer sets a global variable and instead returns the module. Your code should now be: -```moon +```moonscript moon = require "moon" ``` @@ -42,7 +149,7 @@ moon = require "moon" * You can now put line breaks inside of string literals. It will be replaced with `\n` in the generated code. -```moon +```moonscript x = "hello world" ``` @@ -50,7 +157,7 @@ world" * Added `moonscript.base` module. It's a way of including the `moonscript` module without automatically installing the moonloader. * You are free to use any whitespace around the name list in an import statement. It has the same rules as an array table, meaning you can delimit names with line breaks. -```moon +```moonscript import a, b c, d from z ``` @@ -80,7 +187,7 @@ change. * For loops when used as expressions will no longer discard nil values when accumulating into an array table. **This is a backwards incompatible change**. Instead you should use the `continue` keyword to filter out iterations you don't want to keep. [Read more here](https://github.com/leafo/moonscript/issues/66). * The `moonscript` module no longer sets a global value for `moonscript` and instead returns it. You should update your code: -```moon +```moonscript moonscript = require "moonscript" ``` @@ -209,7 +316,7 @@ Today I'm proud to release v0.2.0. I've got a handful of new features and bug fi I'm now including a small set of useful functions in a single module called `moon`: -```moon +```moonscript require "moon" ``` From 84e35232df374c7c842748b984ebdb7aba293dd8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Mar 2014 00:28:05 -0800 Subject: [PATCH 028/344] documentation for code coverage and linter --- CHANGELOG.md | 2 - docs/reference.md | 162 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 161 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2d0bd87..d988cd52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -112,8 +112,6 @@ _ = function() end ``` -* return's statement simplified - ## New Things * New code coverage tool built into `moonc` diff --git a/docs/reference.md b/docs/reference.md index 42992670..bb5275a1 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1688,6 +1688,47 @@ If you want to disable [error rewriting](#error_rewriting), you can pass the `-d` flag. A full list of flags can be seen by passing the `-h` or `--help` flag. +### Code Coverage + +`moon` lets you run a MoonScript file while keeping track of which lines +are executed with the `-c` flag. + +For example, consider the following `.moon` file: + +```moononly +-- test.moon +first = -> + print "hello" + +second = -> + print "world" + +first! +``` + +We can execute and get a glance of which lines ran: + +```bash +$ moon -c test.moon +``` + +The following output is produced: + +``` +------| @cool.moon + 1| -- test.moon +* 2| first = -> +* 3| print "hello" + 4| +* 5| second = -> + 6| print "world" + 7| +* 8| first! + 9| +``` + +The star next to the line means that it was executed. Blank lines are not +considered when running so by default they don't get marked as executed. ## `moonc` @@ -1713,9 +1754,128 @@ compiled automatically. A full list of flags can be seen by passing the `-h` or `--help` flag. +### Linter + +`moonc` contains a [lint](http://en.wikipedia.org/wiki/Lint_(software)) tool +for statically detecting potential problems with code. The current linter only +has one test: it detects the use of global variables. + +You can execute the linter with the `-l` flag. When the linting flag is +provided only linting takes place and no compiled code is generated. + +```bash +moonc -l file1.moon file2.moon +``` + +Like when compiling, you can also pass a directory as a command line argument +to recursively process all the `.moon` files. + +#### Global Variable Checking + +It's considered good practice to avoid using global variables and create local +variables for all the values referenced. A good case for not using global +variables is that you can analyize the code ahead of time without the need to +execute it to find references to undeclared variables. + +MoonScript makes it difficult to declare global variables by forcing you to be +explicit with the `export` keyword, so it's a good candidate for doing this +kind of linting. + +Consider the following program with a typo: (`my_number` is spelled wrong as +`my_nmuber` in the function) + +```moononly +-- lint_example.moon +my_number = 1234 + +some_function = -> + -- a contrived example with a small chance to pass + if math.random() < 0.01 + my_nmuber + 10 + +some_function! +``` + +Although there is a bug in this code, it rarely happens during execution. It's +more likely to be missed during development and cause problems in the future. + +Running the linter immediately identifies the problem: + +```bash +$ moonc -l lint_example.moon +``` + +Outputs: + +``` +./lint_example.moon + +line 7: accessing global my_nmuber +================================== +> my_nmuber + 10 + +``` + +##### Global Variable Whitelist + +In most circumstances it's impossible to avoid using some global variables. For +example, to access any of the built in modules or functions you typically +access them globally. + +For this reason a global variable whitelist is used. It's a list of global +variables that are allowed to be used. A default whitelist is provided that +contains all of Lua's built in functions and modules. + +You can create your own entires in the whitelist as well. For example, the +testing framework [Busted](http://olivinelabs.com/busted) uses a collection of +global functions (like `describe`, `before_each`, `setup`) to make writing +tests easy. + +It would be nice if we could allow all of those global functions to be called +for `.moon` files located in the `spec/` directory. We can do that by creating +a `lint_config` file. + +`lint_config` is a regular MoonScript or Lua file that provides configuration +for the linter. One of those settings is `whitelist_globals`. + +To create a configuration for Busted we might do something like this: + +```moononly +-- lint_config.moon +{ + whitelist_globals: { + ["spec/"]: { + "it", "describe", "setup", "teardown", + "before_each", "after_each", "pending" + } + } +} +``` + +Compile the file: + +```bash +$ moonc lint_config.moon +``` + +Then run the linter on your entire project: + +```bash +$ moonc -l . +``` + +The whitelisted global references in `spec/` will no longer raise notices. + +The `whitelist_globals` property of the `lint_config` is a table where the keys +are Lua patterns that match file names, and the values are an array of globals +that are allowed. + +Multiple patterns in `whitelist_globals` can match a single file, the union of +the allowed globals will be used when linting that file. + # License (MIT) - Copyright (C) 2013 by Leaf Corcoran + Copyright (C) 2014 by Leaf Corcoran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 686261c983c5603341f10f325710c8ff6e391b02 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Mar 2014 00:31:34 -0800 Subject: [PATCH 029/344] clean out changelog --- CHANGELOG.md | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d988cd52..367e6f9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -# MoonScript v0.2.4 (TBR) +# MoonScript v0.2.5 (TBR) ## Changes @@ -126,9 +126,6 @@ end # MoonScript v0.2.4 (2013-07-02) -I'm happy to announce [MoonScript][1] version 0.2.4, the CoffeeScript inspired -language that compiles to Lua. It's been about 5 months since the last release. - ## Changes * The way the subtraction operator works has changed. There was always a little confusion as to the rules regarding whitespace around it and it was recommended to always add whitespace around the operator when doing subtraction. Not anymore. Hopefully it now [works how you would expect](http://moonscript.org/reference/#considerations). (`a-b` compiles to `a - b` and not `a(-b)` anymore). @@ -175,11 +172,6 @@ Fixed bug with moonloader not loading anything # MoonScript v0.2.3 (2013-01-24) -Today marks [MoonScript][1] version 0.2.3, the CoffeeScript inspired language -that compiles to Lua. It's been about 3 months since last release. I've got a -couple new features, fixes, Lua 5.2 support and a backwards incompatible -change. - ## Changes * For loops when used as expressions will no longer discard nil values when accumulating into an array table. **This is a backwards incompatible change**. Instead you should use the `continue` keyword to filter out iterations you don't want to keep. [Read more here](https://github.com/leafo/moonscript/issues/66). @@ -210,11 +202,6 @@ MoonScript has [made its way into GitHub](https://github.com/github/linguist/pul # MoonScript v0.2.2 (2012-11-03) -Today marks [MoonScript][1] version 0.2.2, the CoffeeScript inspired language -that compiles to Lua. It's been approximately 11 months since the last release, -and I'd like to apologize for the long gap. Hopefully we'll see more frequent -updates in the future. - ## Changes * Compiled [files will now implicitly return](http://moonscript.org/reference/#implicit_returns_on_files) their last statement. Be careful, this might change what `require` returns. @@ -265,12 +252,6 @@ updates in the future. # MoonScript v0.2.0 (2011-12-12) -Exactly 3 months ago I released [MoonScript][1]. The CoffeeScript inspired -language that compiles to Lua. Since then I've both written a lot of MoonScript -and enhanced the MoonScript compiler. - -Today I'm proud to release v0.2.0. I've got a handful of new features and bug fixes. - ## Changes * `,` is used instead of `:` for delimiting table slice parts. From ea282f23d213a2ad8c784f4f04a907b5085443c0 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Mar 2014 00:33:18 -0800 Subject: [PATCH 030/344] bump version :first_quarter_moon_with_face: --- CHANGELOG.md | 2 +- docs/reference.md | 2 +- docs/standard_lib.md | 2 +- moonscript/version.lua | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 367e6f9c..585cf30d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,5 @@ -# MoonScript v0.2.5 (TBR) +# MoonScript v0.2.5 (2014-3-5) ## Changes diff --git a/docs/reference.md b/docs/reference.md index bb5275a1..8207636d 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1,6 +1,6 @@ target: reference/index template: reference - title: MoonScript v0.2.4 - Language Guide + title: MoonScript v0.2.5 - Language Guide short_name: lang -- MoonScript is a programming language that compiles to diff --git a/docs/standard_lib.md b/docs/standard_lib.md index 95c2a552..d76fe8fb 100644 --- a/docs/standard_lib.md +++ b/docs/standard_lib.md @@ -1,6 +1,6 @@ target: reference/standard_lib template: reference - title: MoonScript v0.2.4 - Standard Library + title: MoonScript v0.2.5 - Standard Library short_name: stdlib -- diff --git a/moonscript/version.lua b/moonscript/version.lua index ce19da42..771bf129 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,7 +1,7 @@ module("moonscript.version", package.seeall) -version = "0.2.4" +version = "0.2.5" function print_version() print("MoonScript version "..version) end From 8851f803fb60b18d4b85217b231999c9dff65b8b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Mar 2014 10:25:49 -0800 Subject: [PATCH 031/344] documentation tweaks, reorder changelog --- CHANGELOG.md | 24 +++++++++--------- docs/reference.md | 63 ++++++++++++++++++++++++++++------------------- 2 files changed, 49 insertions(+), 38 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 585cf30d..4464d817 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,19 @@ # MoonScript v0.2.5 (2014-3-5) -## Changes +## New Things + +* New [code coverage tool](http://moonscript.org/reference/#code_coverage) built into `moonc` +* New [linting tool](http://moonscript.org/reference/#linter) built into `moonc`, identifies global variable references that don't pass whitelist +* Numbers can have `LL` and `ULL` suffixes for LuaJIT + +## Bugs Fixes + +* Error messages from `moonc` are written to standard error +* Moonloader correctly throws error when moon file can't be parsed, instead of skipping the module +* Line number rewriting is no longer incorrectly offset due to multiline strings -Code generation altered. +## Code Generation Bound functions will avoid creating an anonymous function unless necessary. @@ -112,17 +122,7 @@ _ = function() end ``` -## New Things -* New code coverage tool built into `moonc` -* New linting tool built into `moonc`, identifies global variable references that don't pass whitelist -* Numbers can have `LL` and `ULL` suffixes for LuaJIT - -## Bugs Fixes - -* Error messages from `moonc` are written to standard error -* Moonloader correctly throws error when moon file can't be parsed, instead of skipping the module -* Line number rewriting is no longer incorrectly offset due to multiline strings # MoonScript v0.2.4 (2013-07-02) diff --git a/docs/reference.md b/docs/reference.md index 8207636d..d9ec83c8 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1235,6 +1235,19 @@ import \add from my_module print add 22 -- equivalent to calling my_module\get 22 ``` +When handing multiple imports you can substitute the comma with a newline and +any amount of whitespace. When working with a lot of imports you might write +something like this: + +```moon +import + assert_csrf + assert_timezone + not_found + require_login + from require "helpers" +``` + ## With Statement A common pattern involving the creation of an object is calling a series of @@ -1521,25 +1534,27 @@ By default, a file will also implicitly return like a function. This is useful for writing modules, where you can put your module's table as the last statement in the file so it is returned when loaded with `require`. - ### Writing Modules Lua 5.2 has removed the `module` function for creating modules. It is recommended to return a table instead when defining a module. -The `with` statement along with implicit return on a file provides a convenient -way to do this: - +We can cleanly define modules by using the shorthand hash table key/value +syntax: ```moonret --- my_library.moon -with _M = {} - .SOME_CONSTANT = 100 - .some_function = -> print .SOME_CONSTANT +MY_CONSTANT = "hello" +my_function = -> print "the function" +my_second_function = -> print "another function" + +{ :my_function, :my_second_function, :MY_CONSTANT} ``` +If you need to forward declare your values so you can access them regardless of +their written order you can add `local *` to the top of your file. + # MoonScript API ## `moonscript` Module @@ -1714,18 +1729,16 @@ $ moon -c test.moon The following output is produced: -``` -------| @cool.moon - 1| -- test.moon -* 2| first = -> -* 3| print "hello" - 4| -* 5| second = -> - 6| print "world" - 7| -* 8| first! - 9| -``` + ------| @cool.moon + 1| -- test.moon + * 2| first = -> + * 3| print "hello" + 4| + * 5| second = -> + 6| print "world" + 7| + * 8| first! + 9| The star next to the line means that it was executed. Blank lines are not considered when running so by default they don't get marked as executed. @@ -1807,14 +1820,12 @@ $ moonc -l lint_example.moon Outputs: -``` -./lint_example.moon + ./lint_example.moon -line 7: accessing global my_nmuber -================================== -> my_nmuber + 10 + line 7: accessing global my_nmuber + ================================== + > my_nmuber + 10 -``` ##### Global Variable Whitelist From db8f5398fc2d9de842cf535ae10c69e9c6868bdb Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Mar 2014 23:09:43 -0800 Subject: [PATCH 032/344] append avoids creating table for varargs --- moonscript/compile.lua | 40 ++++++++++++++++------------------------ moonscript/compile.moon | 26 ++++++++++++-------------- 2 files changed, 28 insertions(+), 38 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 379c9c91..e5515a56 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -150,38 +150,30 @@ end do local _base_0 = { pos = nil, - _append_single = function(self, item) - if Line == mtype(item) then - if not (self.pos) then - self.pos = item.pos - end - for _index_0 = 1, #item do - local value = item[_index_0] - self:_append_single(value) - end - else - insert(self, item) - end - return nil - end, append_list = function(self, items, delim) for i = 1, #items do - self:_append_single(items[i]) + self:append(items[i]) if i < #items then insert(self, delim) end end return nil end, - append = function(self, ...) - local _list_0 = { - ... - } - for _index_0 = 1, #_list_0 do - local item = _list_0[_index_0] - self:_append_single(item) + append = function(self, first, ...) + if Line == mtype(first) then + if not (self.pos) then + self.pos = first.pos + end + for _index_0 = 1, #first do + local value = first[_index_0] + self:append(value) + end + else + insert(self, first) + end + if ... then + return self:append(...) end - return nil end, render = function(self, buffer) local current = { } @@ -209,7 +201,7 @@ do insert(current, chunk) end end - if #current > 0 then + if current[1] then add_current() end return buffer diff --git a/moonscript/compile.moon b/moonscript/compile.moon index af7242eb..c8b3a549 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -97,24 +97,22 @@ class Lines class Line pos: nil - _append_single: (item) => - if Line == mtype item - -- print "appending line to line", item.pos, item - @pos = item.pos unless @pos -- bubble pos if there isn't one - @_append_single value for value in *item - else - insert self, item - nil - append_list: (items, delim) => for i = 1,#items - @_append_single items[i] + @append items[i] if i < #items then insert self, delim nil - append: (...) => - @_append_single item for item in *{...} - nil + append: (first, ...) => + if Line == mtype first + -- print "appending line to line", first.pos, first + @pos = first.pos unless @pos -- bubble pos if there isn't one + @append value for value in *first + else + insert self, first + + if ... + @append ... -- todo: try to remove concats from here render: (buffer) => @@ -137,7 +135,7 @@ class Line else insert current, chunk - if #current > 0 + if current[1] add_current! buffer From 3b81b84a9a191063b16110c9f1912a903fc3e8a2 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 5 Mar 2014 23:18:15 -0800 Subject: [PATCH 033/344] create two entries in posmap for multi-line lines --- moonscript/compile.lua | 1 + moonscript/compile.moon | 2 ++ 2 files changed, 3 insertions(+) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index e5515a56..1fc05ceb 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -61,6 +61,7 @@ do local _exp_0 = mtype(l) if "string" == _exp_0 or DelayedLine == _exp_0 then line_no = line_no + 1 + out[line_no] = posmap[i] for _ in l:gmatch("\n") do line_no = line_no + 1 end diff --git a/moonscript/compile.moon b/moonscript/compile.moon index c8b3a549..6bcc9491 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -44,6 +44,8 @@ class Lines switch mtype l when "string", DelayedLine line_no += 1 + out[line_no] = posmap[i] + line_no += 1 for _ in l\gmatch"\n" out[line_no] = posmap[i] when Lines From d0cdf220e20b7f04bafc2cf47a6fcfd1408f31c9 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 6 Mar 2014 13:05:17 -0800 Subject: [PATCH 034/344] fix indent and broken lint link --- docs/reference.md | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index d9ec83c8..8bcfacf4 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -90,7 +90,7 @@ without an escape sequence: ```moon some_string = "Here is a string - that has a line break in it." + that has a line break in it." ``` ## Function Literals @@ -1769,7 +1769,7 @@ A full list of flags can be seen by passing the `-h` or `--help` flag. ### Linter -`moonc` contains a [lint](http://en.wikipedia.org/wiki/Lint_(software)) tool +`moonc` contains a [lint][1] tool for statically detecting potential problems with code. The current linter only has one test: it detects the use of global variables. @@ -1802,9 +1802,9 @@ Consider the following program with a typo: (`my_number` is spelled wrong as my_number = 1234 some_function = -> - -- a contrived example with a small chance to pass - if math.random() < 0.01 - my_nmuber + 10 + -- a contrived example with a small chance to pass + if math.random() < 0.01 + my_nmuber + 10 some_function! ``` @@ -1854,12 +1854,12 @@ To create a configuration for Busted we might do something like this: ```moononly -- lint_config.moon { - whitelist_globals: { - ["spec/"]: { - "it", "describe", "setup", "teardown", - "before_each", "after_each", "pending" - } - } + whitelist_globals: { + ["spec/"]: { + "it", "describe", "setup", "teardown", + "before_each", "after_each", "pending" + } + } } ``` @@ -1907,4 +1907,4 @@ the allowed globals will be used when linting that file. THE SOFTWARE. - + [1]: http://en.wikipedia.org/wiki/Lint_(software) From dd7e9924dd7beac2a8c01ea0ed49ebb7d98e5bdf Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 10 Mar 2014 22:24:24 -0700 Subject: [PATCH 035/344] remove left over args --- moonscript/base.lua | 4 ++-- moonscript/base.moon | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/moonscript/base.lua b/moonscript/base.lua index 06b3f6d6..f1bc036c 100644 --- a/moonscript/base.lua +++ b/moonscript/base.lua @@ -33,7 +33,7 @@ to_lua = function(text, options) end if "string" ~= type(text) then local t = type(text) - return nil, "expecting string (got " .. t .. ")", 2 + return nil, "expecting string (got " .. t .. ")" end local tree, err = parse.string(text) if not tree then @@ -41,7 +41,7 @@ to_lua = function(text, options) end local code, ltable, pos = compile.tree(tree, options) if not code then - return nil, compile.format_error(ltable, pos, text), 2 + return nil, compile.format_error(ltable, pos, text) end return code, ltable end diff --git a/moonscript/base.moon b/moonscript/base.moon index 256b0d76..1988d063 100644 --- a/moonscript/base.moon +++ b/moonscript/base.moon @@ -22,7 +22,7 @@ create_moonpath = (package_path) -> to_lua = (text, options={}) -> if "string" != type text t = type text - return nil, "expecting string (got ".. t ..")", 2 + return nil, "expecting string (got ".. t ..")" tree, err = parse.string text if not tree @@ -30,7 +30,7 @@ to_lua = (text, options={}) -> code, ltable, pos = compile.tree tree, options if not code - return nil, compile.format_error(ltable, pos, text), 2 + return nil, compile.format_error(ltable, pos, text) code, ltable From 5c6b298324c62b525fbc1b58ef8c1a1bb3c01560 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 13 Mar 2014 19:16:07 +0400 Subject: [PATCH 036/344] Removed some unused variables --- moonscript/base.lua | 1 - moonscript/base.moon | 2 +- moonscript/compile.lua | 2 -- moonscript/compile.moon | 2 -- moonscript/compile/statement.lua | 1 - moonscript/compile/statement.moon | 1 - moonscript/parse.lua | 36 ++------------------------- moonscript/transform/destructure.lua | 1 - moonscript/transform/destructure.moon | 2 -- 9 files changed, 3 insertions(+), 45 deletions(-) diff --git a/moonscript/base.lua b/moonscript/base.lua index f1bc036c..fd5a8f9c 100644 --- a/moonscript/base.lua +++ b/moonscript/base.lua @@ -128,7 +128,6 @@ return { insert_loader = insert_loader, remove_loader = remove_loader, to_lua = to_lua, - moon_chunk = moon_chunk, moon_loader = moon_loader, dirsep = dirsep, dofile = dofile, diff --git a/moonscript/base.moon b/moonscript/base.moon index 1988d063..7aa39902 100644 --- a/moonscript/base.moon +++ b/moonscript/base.moon @@ -102,7 +102,7 @@ remove_loader = -> { _NAME: "moonscript" - :insert_loader, :remove_loader, :to_lua, :moon_chunk, :moon_loader, :dirsep, + :insert_loader, :remove_loader, :to_lua, :moon_loader, :dirsep, :dofile, :loadfile, :loadstring } diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 1fc05ceb..ccb2e936 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -102,7 +102,6 @@ do end end insert(buffer, "\n") - local last = l elseif Lines == _exp_0 then l:flatten(indent and indent .. indent_char or indent_char, buffer) else @@ -696,7 +695,6 @@ tree = function(tree, options) if not (success) then local error_msg, error_pos if type(err) == "table" then - local error_type = err[1] local _exp_0 = err[1] if "user-error" == _exp_0 or "compile-error" == _exp_0 then error_msg, error_pos = unpack(err, 2) diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 6bcc9491..ac6c4638 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -76,7 +76,6 @@ class Lines insert buffer, ";" insert buffer, "\n" - last = l when Lines l\flatten indent and indent .. indent_char or indent_char, buffer else @@ -452,7 +451,6 @@ tree = (tree, options={}) -> unless success error_msg, error_pos = if type(err) == "table" - error_type = err[1] switch err[1] when "user-error", "compile-error" unpack err, 2 diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index abccd627..7b6ccb0f 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -1,5 +1,4 @@ local util = require("moonscript.util") -local data = require("moonscript.data") local reversed, unpack reversed, unpack = util.reversed, util.unpack local ntype diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index 996b3855..9d0f43a7 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -1,6 +1,5 @@ util = require "moonscript.util" -data = require "moonscript.data" import reversed, unpack from util import ntype from require "moonscript.types" diff --git a/moonscript/parse.lua b/moonscript/parse.lua index f6d84dd5..4d75b2d2 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -10,7 +10,6 @@ local types = require"moonscript.types" local ntype = types.ntype -local dump = util.dump local trim = util.trim local getfenv = util.getfenv @@ -126,7 +125,7 @@ end local function extract_line(str, start_pos) str = str:sub(start_pos) - m = str:match"^(.-)\n" + local m = str:match"^(.-)\n" if m then return m end return str:match"^.-$" end @@ -148,21 +147,6 @@ local function pos(patt) return (lpeg.Cp() * patt) / insert_pos end -local function got(what) - return Cmt("", function(str, pos, ...) - local cap = {...} - print("++ got "..what, "["..extract_line(str, pos).."]") - return true - end) -end - -local function flatten(tbl) - if #tbl == 1 then - return tbl[1] - end - return tbl -end - local function flatten_or_mark(name) return function(tbl) if #tbl == 1 then return tbl[1] end @@ -234,7 +218,7 @@ end local function simple_string(delim, allow_interpolation) local inner = P('\\'..delim) + "\\\\" + (1 - P(delim)) if allow_interpolation then - inter = symx"#{" * V"Exp" * sym"}" + local inter = symx"#{" * V"Exp" * sym"}" inner = (C((inner - inter)^1) + inter / mark"interpolate")^0 else inner = C(inner^0) @@ -280,16 +264,6 @@ local function wrap_decorator(stm, dec) return { "decorated", stm, dec } end --- wrap if statement if there is a conditional decorator -local function wrap_if(stm, cond) - if cond then - local pass, fail = unpack(cond) - if fail then fail = {"else", {fail}} end - return {"if", cond[2], {stm}, fail} - end - return stm -end - local function check_lua_string(str, pos, right, left) return #left == #right end @@ -343,18 +317,12 @@ local build_grammar = wrap_env(function() return true end - local function enable_do(str_pos) - _do_stack:push(true) - return true - end - local function pop_do(str, pos) if nil == _do_stack:pop() then error("unexpected do pop") end return true end local DisableDo = Cmt("", disable_do) - local EnableDo = Cmt("", enable_do) local PopDo = Cmt("", pop_do) local keywords = {} diff --git a/moonscript/transform/destructure.lua b/moonscript/transform/destructure.lua index cb450711..f7a5166c 100644 --- a/moonscript/transform/destructure.lua +++ b/moonscript/transform/destructure.lua @@ -23,7 +23,6 @@ do local _obj_0 = require("moonscript.errors") user_error = _obj_0.user_error end -local util = require("moonscript.util") local join join = function(...) do diff --git a/moonscript/transform/destructure.moon b/moonscript/transform/destructure.moon index 274e6ed3..1547ccfc 100644 --- a/moonscript/transform/destructure.moon +++ b/moonscript/transform/destructure.moon @@ -6,8 +6,6 @@ import unpack from require "moonscript.util" import user_error from require "moonscript.errors" -util = require "moonscript.util" - join = (...) -> with out = {} i = 1 From 592d4b007433aaa6476ba71698025cc816f7f497 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Sat, 22 Mar 2014 10:53:40 +0400 Subject: [PATCH 037/344] Readded got() --- moonscript/parse.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 4d75b2d2..63458592 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -147,6 +147,14 @@ local function pos(patt) return (lpeg.Cp() * patt) / insert_pos end +local function got(what) + return Cmt("", function(str, pos, ...) + local cap = {...} + print("++ got "..what, "["..extract_line(str, pos).."]") + return true + end) +end + local function flatten_or_mark(name) return function(tbl) if #tbl == 1 then return tbl[1] end From 1527a5f696f1dda718f44bdd28b6615f2ab650cf Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 23 Mar 2014 23:45:38 -0700 Subject: [PATCH 038/344] update broken spec --- spec/error_rewriting_spec.moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/error_rewriting_spec.moon b/spec/error_rewriting_spec.moon index 3c6d1280..6493c654 100644 --- a/spec/error_rewriting_spec.moon +++ b/spec/error_rewriting_spec.moon @@ -59,6 +59,6 @@ describe "line map", -> lua_code, posmap = assert to_lua moon_code -- print util.debug_posmap(posmap, moon_code, lua_code) - assert.same {[1]: 7, [7]: 19, [8]: 63}, posmap + assert.same {[1]: 7, [2]: 19, [7]: 19, [8]: 63}, posmap From d6f0e89cdbedaa75ac22689dfe306c223edd68db Mon Sep 17 00:00:00 2001 From: Conor Heine Date: Tue, 27 May 2014 22:33:24 -0700 Subject: [PATCH 039/344] added '-o file' flag to control output name/destination --- bin/moonc | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/bin/moonc b/bin/moonc index dec5ad7c..b273dfdf 100755 --- a/bin/moonc +++ b/bin/moonc @@ -9,7 +9,7 @@ local dump_tree = require"moonscript.dump".tree local alt_getopt = require "alt_getopt" local lfs = require "lfs" -local opts, ind = alt_getopt.get_opts(arg, "lvhwt:pTXb", { +local opts, ind = alt_getopt.get_opts(arg, "lvhwt:o:pTXb", { print = "p", tree = "T", version = "v", help = "h", lint = "l" }) @@ -22,6 +22,7 @@ local help = [[Usage: %s [options] files... -h Print this message -w Watch file/directory -t path Specify where to place compiled files + -o file Write output to file -p Write output to standard out -T Write parse tree instead of code (to stdout) -X Write line rewrite map instead of code (to stdout) @@ -372,7 +373,12 @@ if opts.w then end for fname in protected do - local target = target_dir..convert_path(fname) + local target + if opts.o then + target = opts.o + else + target = target_dir..convert_path(fname) + end local success, err = compile_and_write(fname, target) if not success then io.stderr:write(table.concat({ @@ -399,7 +405,13 @@ elseif opts.l then end else for _, fname in ipairs(files) do - local success, err = compile_and_write(fname, target_dir..convert_path(fname)) + local target + if opts.o then + target = opts.o + else + target = target_dir..convert_path(fname) + end + local success, err = compile_and_write(fname, target) if not success then io.stderr:write(fname .. "\t" .. err .. "\n") os.exit(1) From cb6cb86db89d7a1215266131e29621364073eb02 Mon Sep 17 00:00:00 2001 From: Conor Heine Date: Tue, 27 May 2014 22:58:02 -0700 Subject: [PATCH 040/344] returning a table rather than using module() --- moonscript/version.lua | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/moonscript/version.lua b/moonscript/version.lua index 771bf129..116b2971 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,7 +1,9 @@ -module("moonscript.version", package.seeall) - version = "0.2.5" -function print_version() - print("MoonScript version "..version) -end + +return { + version = version, + print_version = function() + print("MoonScript version "..version) + end +} From 6046b56b07589d5ecb0c6ef8fdbad973bc117973 Mon Sep 17 00:00:00 2001 From: Conor Heine Date: Tue, 27 May 2014 23:41:19 -0700 Subject: [PATCH 041/344] forgot to localize version --- moonscript/version.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moonscript/version.lua b/moonscript/version.lua index 116b2971..bbbf0a82 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,5 +1,5 @@ -version = "0.2.5" +local version = "0.2.5" return { version = version, From 76475da55de421ca0114b29e1dd83793a869d619 Mon Sep 17 00:00:00 2001 From: Conor Heine Date: Thu, 29 May 2014 12:57:47 -0700 Subject: [PATCH 042/344] moved version to moon file --- moonscript/version.lua | 4 +--- moonscript/version.moon | 8 ++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 moonscript/version.moon diff --git a/moonscript/version.lua b/moonscript/version.lua index bbbf0a82..8dda91c7 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,9 +1,7 @@ - local version = "0.2.5" - return { version = version, print_version = function() - print("MoonScript version "..version) + return print("MoonScript version " .. tostring(version)) end } diff --git a/moonscript/version.moon b/moonscript/version.moon new file mode 100644 index 00000000..bf801c36 --- /dev/null +++ b/moonscript/version.moon @@ -0,0 +1,8 @@ + +version = "0.2.5" + +{ + version: version, + print_version: -> + print "MoonScript version #{version}" +} From fa6054f819d5b259a7ebf8655026dd1d4663fe1b Mon Sep 17 00:00:00 2001 From: Conor Heine Date: Thu, 29 May 2014 13:00:04 -0700 Subject: [PATCH 043/344] fixed whitespace --- moonscript/version.moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moonscript/version.moon b/moonscript/version.moon index bf801c36..62886737 100644 --- a/moonscript/version.moon +++ b/moonscript/version.moon @@ -4,5 +4,5 @@ version = "0.2.5" { version: version, print_version: -> - print "MoonScript version #{version}" + print "MoonScript version #{version}" } From c73762e00ee2e9bb9b2ec70c49e1e24f132a6c09 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 6 Jun 2014 14:36:03 -0700 Subject: [PATCH 044/344] prefix file path with @ when loading string --- moonscript/base.lua | 4 ++-- moonscript/base.moon | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/moonscript/base.lua b/moonscript/base.lua index fd5a8f9c..9c74de4b 100644 --- a/moonscript/base.lua +++ b/moonscript/base.lua @@ -60,7 +60,7 @@ moon_loader = function(name) if file then local text = file:read("*a") file:close() - local res, err = loadstring(text, file_path) + local res, err = loadstring(text, "@" .. tostring(file_path)) if not res then error(file_path .. ": " .. err) end @@ -90,7 +90,7 @@ loadfile = function(fname, ...) end local text = assert(file:read("*a")) file:close() - return loadstring(text, fname, ...) + return loadstring(text, "@" .. tostring(fname), ...) end dofile = function(...) local f = assert(loadfile(...)) diff --git a/moonscript/base.moon b/moonscript/base.moon index 7aa39902..371628cc 100644 --- a/moonscript/base.moon +++ b/moonscript/base.moon @@ -46,7 +46,7 @@ moon_loader = (name) -> if file text = file\read "*a" file\close! - res, err = loadstring text, file_path + res, err = loadstring text, "@#{file_path}" if not res error file_path .. ": " .. err @@ -72,7 +72,7 @@ loadfile = (fname, ...) -> return nil, err unless file text = assert file\read "*a" file\close! - loadstring text, fname, ... + loadstring text, "@#{fname}", ... -- throws errros dofile = (...) -> From f92e046d13f98f34ff2bfc8dd0793122ff18706b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 7 Jun 2014 00:04:26 -0700 Subject: [PATCH 045/344] add spec helpers --- spec/helpers.moon | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 spec/helpers.moon diff --git a/spec/helpers.moon b/spec/helpers.moon new file mode 100644 index 00000000..c5ddf9f5 --- /dev/null +++ b/spec/helpers.moon @@ -0,0 +1,9 @@ + +-- remove front indentation from a multiline string, making it suitable to be +-- parsed +unindent = (str) -> + indent = str\match "^%s+" + return str unless indent + (str\gsub("\n#{indent}", "\n")\gsub "%s+$", "") + +{ :unindent } From 01555dc4f4056236a1d7ace42dd8e3a1eca1073e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 7 Jun 2014 11:09:50 -0700 Subject: [PATCH 046/344] update error rewriters for new @ prefix on chunk name for files --- moonscript/errors.lua | 4 ++-- moonscript/errors.moon | 4 ++-- spec/error_rewriting_spec.moon | 5 ++--- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/moonscript/errors.lua b/moonscript/errors.lua index f8d3254a..bdf5811c 100644 --- a/moonscript/errors.lua +++ b/moonscript/errors.lua @@ -78,8 +78,8 @@ rewrite_traceback = function(text, err) local cache = { } local rewrite_single rewrite_single = function(trace) - local fname, line, msg = trace:match('^%[string "(.-)"]:(%d+): (.*)$') - local tbl = line_tables[fname] + local fname, line, msg = trace:match('^(.-):(%d+): (.*)$') + local tbl = line_tables["@" .. tostring(fname)] if fname and tbl then return concat({ fname, diff --git a/moonscript/errors.moon b/moonscript/errors.moon index a5bd94e8..a6543adb 100644 --- a/moonscript/errors.moon +++ b/moonscript/errors.moon @@ -56,8 +56,8 @@ rewrite_traceback = (text, err) -> cache = {} -- loaded file cache rewrite_single = (trace) -> - fname, line, msg = trace\match '^%[string "(.-)"]:(%d+): (.*)$' - tbl = line_tables[fname] + fname, line, msg = trace\match '^(.-):(%d+): (.*)$' + tbl = line_tables["@#{fname}"] if fname and tbl concat { fname, ":" diff --git a/spec/error_rewriting_spec.moon b/spec/error_rewriting_spec.moon index 6493c654..dfaab1b2 100644 --- a/spec/error_rewriting_spec.moon +++ b/spec/error_rewriting_spec.moon @@ -12,12 +12,11 @@ get_rewritten_line_no = (fname) -> success, err = pcall chunk error "`#{fname}` is supposed to have runtime error!" if success - source = tonumber err\match "]:(%d+)" + source = tonumber err\match "^.-:(%d+):" - line_table = require("moonscript.line_tables")[fname] + line_table = assert require("moonscript.line_tables")["@#{fname}"], "missing line table" errors.reverse_line_number fname, line_table, source, {} - -- TODO: check entire stack trace describe "error rewriting", -> tests = { From 5964d06eeeb0bea086cf85aebe26a818b45f4fbf Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 14 Jun 2014 18:49:22 -0700 Subject: [PATCH 047/344] set posmap for declare on assign for functions --- moonscript/compile.lua | 10 ++++++++-- moonscript/compile.moon | 8 +++++--- moonscript/compile/statement.lua | 2 +- moonscript/compile/statement.moon | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index ccb2e936..aaa42c29 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -415,8 +415,14 @@ do }) return name end, - add = function(self, item) - self._lines:add(item) + add = function(self, item, pos) + do + local _with_0 = self._lines + _with_0:add(item) + if pos then + _with_0:mark_pos(pos) + end + end return item end, render = function(self, buffer) diff --git a/moonscript/compile.moon b/moonscript/compile.moon index ac6c4638..af700e10 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -288,9 +288,11 @@ class Block @stm {"assign", {name}, {value}} name - -- add a line object - add: (item) => - @_lines\add item + -- add something to the line buffer + add: (item, pos) => + with @_lines + \add item + \mark_pos pos if pos item -- todo: pass in buffer as argument diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index 7b6ccb0f..2d2a80c8 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -78,7 +78,7 @@ return { _with_0:append(declare) else if #undeclared > 0 then - self:add(declare) + self:add(declare, node[-1]) end _with_0:append_list((function() local _accum_0 = { } diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index 9d0f43a7..e5c57403 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -43,7 +43,7 @@ import concat, insert from table if #undeclared == #names and not has_fndef \append declare else - @add declare if #undeclared > 0 + @add declare, node[-1] if #undeclared > 0 \append_list [@value name for name in *names], ", " \append " = " From 68255378781b647db3d6878f89fa8116b84b930f Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 17 Jun 2014 00:03:13 -0700 Subject: [PATCH 048/344] fix indentation --- bin/moonc | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/bin/moonc b/bin/moonc index b273dfdf..a99ae5a6 100755 --- a/bin/moonc +++ b/bin/moonc @@ -180,7 +180,7 @@ function scan_directory(root, collected) collected = collected or {} for fname in lfs.dir(root) do - if not fname:match("^%.") then + if not fname:match("^%.") then local full_path = root..fname if lfs.attributes(full_path, "mode") == "directory" then @@ -285,7 +285,6 @@ function get_sleep_func() return sleep end - function plural(count, word) if count ~= 1 then word = word .. "s" @@ -373,12 +372,12 @@ if opts.w then end for fname in protected do - local target + local target if opts.o then - target = opts.o + target = opts.o else - target = target_dir..convert_path(fname) - end + target = target_dir..convert_path(fname) + end local success, err = compile_and_write(fname, target) if not success then io.stderr:write(table.concat({ @@ -405,12 +404,12 @@ elseif opts.l then end else for _, fname in ipairs(files) do - local target - if opts.o then - target = opts.o - else - target = target_dir..convert_path(fname) - end + local target + if opts.o then + target = opts.o + else + target = target_dir..convert_path(fname) + end local success, err = compile_and_write(fname, target) if not success then io.stderr:write(fname .. "\t" .. err .. "\n") From 6404767dae508789d16ccc3efd49267de96e6463 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 17 Jun 2014 00:33:52 -0700 Subject: [PATCH 049/344] try travis ci --- .travis.yml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 .travis.yml diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 00000000..74670737 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,8 @@ +language: c + +install: + - sudo apt-get install luarocks + - sudo luarocks install busted + - sudo luarocks make + +script: busted From 31bab1e099c814fa073d88e4db94923c99bf7f77 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 17 Jun 2014 10:45:42 -0700 Subject: [PATCH 050/344] wip --- bin/moonc | 59 ++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 43 insertions(+), 16 deletions(-) diff --git a/bin/moonc b/bin/moonc index a99ae5a6..59872059 100755 --- a/bin/moonc +++ b/bin/moonc @@ -9,6 +9,8 @@ local dump_tree = require"moonscript.dump".tree local alt_getopt = require "alt_getopt" local lfs = require "lfs" +local dirsep = package.config:sub(1,1) + local opts, ind = alt_getopt.get_opts(arg, "lvhwt:o:pTXb", { print = "p", tree = "T", version = "v", help = "h", lint = "l" }) @@ -54,11 +56,11 @@ function print_help(err) end function mkdir(path) - local chunks = util.split(path, "/") + local chunks = util.split(path, dirsep) local accum for _, dir in ipairs(chunks) do - accum = accum and accum.."/"..dir or dir + accum = accum and accum.. dirsep ..dir or dir lfs.mkdir(accum) end @@ -66,16 +68,20 @@ function mkdir(path) end function normalize(path) - return path:match("(.-)/*$").."/" + return path:match("^(.-)" .. dirsep .. "*$")..dirsep end function get_dir(fname) - return fname:match("^(.-)[^/]*$") + return fname:match("^(.-)[^" .. dirsep .. "]*$") end -- convert .moon to .lua function convert_path(path) - return (path:gsub("%.moon$", ".lua")) + local new_path = path:gsub("%.moon$", ".lua") + if new_path == path then + new_path = path .. ".lua" + end + return new_path end function log_msg(...) @@ -196,33 +202,49 @@ function scan_directory(root, collected) return collected end -function append(a, b) - for _, v in ipairs(b) do - table.insert(a, v) - end -end - -function remove_dups(tbl) +function remove_dups(tbl, key_fn) local hash = {} local final = {} for _, v in ipairs(tbl) do - if not hash[v] then + local dup_key = key_fn and key_fn(v) or v + if not hash[dup_key] then table.insert(final, v) - hash[v] = true + hash[dup_key] = true end end return final end +-- creates tuples of input and target function get_files(fname, files) files = files or {} if lfs.attributes(fname, "mode") == "directory" then - append(files, scan_directory(fname)) + local head = fname:match("^(.-)[^" .. dirsep .. "]*" .. dirsep .."?$") + for _, sub_fname in ipairs(scan_directory(fname)) do + local target_fname = convert_path(sub_fname) + if opts.t then + if head then + local start, stop = target_fname:find(head, 1, true) + if start == 1 then + target_fname = target_fname:sub(stop + 1) + end + end + + target_fname = normalize(opts.t) .. target_fname + end + + table.insert(files, {sub_fname, target_fname}) + end else - table.insert(files, "./"..fname) + local target_fname = convert_path(fname) + if opts.t then + target_fname = normalize(opts.t) .. target_fname + end + + table.insert(files, {fname, target_fname}) end return files @@ -268,8 +290,13 @@ for _, input in ipairs(inputs) do get_files(input, files) end +require("moon").p(files) + +do return end + files = remove_dups(files) + function get_sleep_func() local sleep if not pcall(function() From a4585f8b1c9860856218c649e66e5077f0728b80 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 17 Jun 2014 20:27:10 -0700 Subject: [PATCH 051/344] finish converting moonc to have better directory support, remove -t from watch mode temporarily --- bin/moonc | 99 ++++++++++++++++++++++++++++++------------------------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/bin/moonc b/bin/moonc index 59872059..bb9c0d0d 100755 --- a/bin/moonc +++ b/bin/moonc @@ -71,10 +71,15 @@ function normalize(path) return path:match("^(.-)" .. dirsep .. "*$")..dirsep end -function get_dir(fname) +function parse_dir(fname) return fname:match("^(.-)[^" .. dirsep .. "]*$") end +function parse_file(fname) + return fname:match("^.-([^" .. dirsep .. "]*)$") +end + + -- convert .moon to .lua function convert_path(path) local new_path = path:gsub("%.moon$", ".lua") @@ -111,7 +116,7 @@ function write_file(fname, code) if opts.p then if code ~= "" then print(code) end else - mkdir(get_dir(fname)) + mkdir(parse_dir(fname)) local out_f = io.open(fname, "w") if not out_f then return nil, "Failed to write output: "..fname @@ -217,6 +222,15 @@ function remove_dups(tbl, key_fn) return final end +local function is_abs_path(path) + local first = path:sub(1, 1) + if dirsep == "\\" then + return first == "/" or first == "\\" or path:sub(2,1) == ":" + else + return first == dirsep + end +end + -- creates tuples of input and target function get_files(fname, files) files = files or {} @@ -241,7 +255,12 @@ function get_files(fname, files) else local target_fname = convert_path(fname) if opts.t then - target_fname = normalize(opts.t) .. target_fname + local prefix = normalize(opts.t) + + if is_abs_path(target_fname) then + target_fname = parse_file(target_fname) + end + target_fname = prefix .. target_fname end table.insert(files, {fname, target_fname}) @@ -275,27 +294,14 @@ if #inputs == 0 then print_help("No files specified") end -local target_dir = "." -if opts.t then - if mkdir(opts.t) ~= "directory" then - print_help("Invalid target dir") - end - target_dir = opts.t -end - -target_dir = target_dir.."/" - local files = {} for _, input in ipairs(inputs) do get_files(input, files) end -require("moon").p(files) - -do return end - -files = remove_dups(files) - +files = remove_dups(files, function(f) + return f[2] +end) function get_sleep_func() local sleep @@ -330,9 +336,15 @@ function create_watcher(files) if inotify then local dirs = {} - for _, fname in ipairs(files) do - table.insert(dirs, get_dir(fname)) + + for _, tuple in ipairs(files) do + local dir = parse_dir(tuple[1]) + if dir == "" then + dir = "./" + end + table.insert(dirs, dir) end + dirs = remove_dups(dirs) return coroutine.wrap(function() @@ -347,15 +359,20 @@ function create_watcher(files) while true do local events = handle:read() - if events then - for _, ev in ipairs(events) do - local fname = wd_table[ev.wd]..ev.name - if fname:match("%.moon$") then - coroutine.yield(fname) + if not events then + break + end + + for _, ev in ipairs(events) do + local fname = ev.name + if fname:match("%.moon$") then + local dir = wd_table[ev.wd] + if dir ~= "./" then + fname = dir .. fname end + -- TODO: check to make sure the file was in the original set + coroutine.yield(fname) end - else - break end end end) @@ -367,7 +384,8 @@ function create_watcher(files) local mod_time = {} while true do - for _, file in ipairs(files) do + for _, tuple in ipairs(files) do + local file = tuple[1] local time = lfs.attributes(file, "modification") if not mod_time[file] then mod_time[file] = time @@ -390,7 +408,7 @@ if opts.w then local watcher = create_watcher(files) -- catches interrupt error for ctl-c local protected = function() - local status, file = pcall(watcher) + local status, file = true, watcher() if status then return file elseif file ~= "interrupted!" then @@ -399,12 +417,7 @@ if opts.w then end for fname in protected do - local target - if opts.o then - target = opts.o - else - target = target_dir..convert_path(fname) - end + local target = convert_path(fname) local success, err = compile_and_write(fname, target) if not success then io.stderr:write(table.concat({ @@ -420,7 +433,8 @@ if opts.w then io.stderr:write("\nQuitting...\n") elseif opts.l then - for _, fname in pairs(files) do + for _, tuple in pairs(files) do + local fname = tuple[1] lint = require "moonscript.cmd.lint" local res, err = lint.lint_file(fname) if res then @@ -430,19 +444,14 @@ elseif opts.l then end end else - for _, fname in ipairs(files) do - local target - if opts.o then - target = opts.o - else - target = target_dir..convert_path(fname) - end + for _, tuple in ipairs(files) do + local fname, target = unpack(tuple) local success, err = compile_and_write(fname, target) if not success then io.stderr:write(fname .. "\t" .. err .. "\n") os.exit(1) else - log_msg("Built", fname) + log_msg("Built", fname, "->", target) end end end From c60d83c54697328e714fa974fbf46e674a6673cc Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 18 Jun 2014 09:23:41 -0700 Subject: [PATCH 052/344] start moving moonc code to moonscript --- bin/moonc | 128 ++++++++---------------------------- moonscript-dev-1.rockspec | 1 + moonscript/cmd/moonc.lua | 134 ++++++++++++++++++++++++++++++++++++++ moonscript/cmd/moonc.moon | 122 ++++++++++++++++++++++++++++++++++ spec/cmd_spec.moon | 68 +++++++++++++++++++ 5 files changed, 351 insertions(+), 102 deletions(-) create mode 100644 moonscript/cmd/moonc.lua create mode 100644 moonscript/cmd/moonc.moon create mode 100644 spec/cmd_spec.moon diff --git a/bin/moonc b/bin/moonc index bb9c0d0d..f5b11842 100755 --- a/bin/moonc +++ b/bin/moonc @@ -55,62 +55,19 @@ function print_help(err) end end -function mkdir(path) - local chunks = util.split(path, dirsep) - local accum - - for _, dir in ipairs(chunks) do - accum = accum and accum.. dirsep ..dir or dir - lfs.mkdir(accum) - end - - return lfs.attributes(path, "mode") -end - -function normalize(path) - return path:match("^(.-)" .. dirsep .. "*$")..dirsep -end - -function parse_dir(fname) - return fname:match("^(.-)[^" .. dirsep .. "]*$") -end - -function parse_file(fname) - return fname:match("^.-([^" .. dirsep .. "]*)$") -end - - --- convert .moon to .lua -function convert_path(path) - local new_path = path:gsub("%.moon$", ".lua") - if new_path == path then - new_path = path .. ".lua" - end - return new_path -end - function log_msg(...) if not opts.p then io.stderr:write(table.concat({...}, " ") .. "\n") end end -local gettime = nil -if opts.b then - pcall(function() - require "socket" - gettime = socket.gettime - end) - - function format_time(time) - return ("%.3fms"):format(time*1000) - end - if not gettime then - print_help"LuaSocket needed for benchmark" - end -else - gettime = function() return 0 end -end +local moonc = require("moonscript.cmd.moonc") +local mkdir = moonc.mkdir +local normalize_dir = moonc.normalize_dir +local parse_dir = moonc.parse_dir +local parse_file = moonc.parse_file +local convert_path = moonc.convert_path +local compile_file_text = moonc.compile_file_text function write_file(fname, code) if opts.p then @@ -125,50 +82,8 @@ function write_file(fname, code) out_f:write(code.."\n") out_f:close() end - return true -end - -function compile_file(text, fname) - local parse_time = gettime() - local tree, err = parse.string(text) - parse_time = gettime() - parse_time - - if not tree then - return nil, err - end - - if opts.T then - opts.p = true - dump_tree(tree) - return "" - else - local compile_time = gettime() - local code, posmap_or_err, err_pos = compile.tree(tree) - compile_time = gettime() - compile_time - - if not code then - return nil, compile.format_error(posmap_or_err, err_pos, text) - end - if opts.X then - opts.p = true - print("Pos", "Lua", ">>", "Moon") - print(util.debug_posmap(posmap_or_err, text, code)) - return "" - end - - if opts.b then - opts.p = true - return table.concat({ - fname, - "Parse time \t" .. format_time(parse_time), - "Compile time\t" .. format_time(compile_time), - "" - }, "\n") - end - - return code - end + return "built" end function compile_and_write(from, to) @@ -178,16 +93,25 @@ function compile_and_write(from, to) end local text = f:read("*a") - local code, err = compile_file(text, from) - if not code then + local code, err = compile_file_text(text, from, { + benchmark = opts.b, + show_posmap = opts.X, + show_parse_tree = opts.T, + }) + + if not code and err then return nil, err end + if not code then + return true + end + return write_file(to, code) end function scan_directory(root, collected) - root = normalize(root) + root = normalize_dir(root) collected = collected or {} for fname in lfs.dir(root) do @@ -247,7 +171,7 @@ function get_files(fname, files) end end - target_fname = normalize(opts.t) .. target_fname + target_fname = normalize_dir(opts.t) .. target_fname end table.insert(files, {sub_fname, target_fname}) @@ -255,7 +179,7 @@ function get_files(fname, files) else local target_fname = convert_path(fname) if opts.t then - local prefix = normalize(opts.t) + local prefix = normalize_dir(opts.t) if is_abs_path(target_fname) then target_fname = parse_file(target_fname) @@ -426,8 +350,8 @@ if opts.w then err, "\n", }, "\n")) - else - log_msg("Built:", fname, "->", target) + elseif success == "build" then + log_msg("Built", fname, "->", target) end end @@ -450,8 +374,8 @@ else if not success then io.stderr:write(fname .. "\t" .. err .. "\n") os.exit(1) - else - log_msg("Built", fname, "->", target) + elseif success == "build" then + log_msg("Built", fname) end end end diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index 7fd5ef17..f1b4a1c5 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -28,6 +28,7 @@ build = { ["moonscript.base"] = "moonscript/base.lua", ["moonscript.cmd.coverage"] = "moonscript/cmd/coverage.lua", ["moonscript.cmd.lint"] = "moonscript/cmd/lint.lua", + ["moonscript.cmd.moonc"] = "moonscript/cmd/moonc.lua", ["moonscript.compile"] = "moonscript/compile.lua", ["moonscript.compile.statement"] = "moonscript/compile/statement.lua", ["moonscript.compile.value"] = "moonscript/compile/value.lua", diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua new file mode 100644 index 00000000..642e68c4 --- /dev/null +++ b/moonscript/cmd/moonc.lua @@ -0,0 +1,134 @@ +local lfs = require("lfs") +local split +do + local _obj_0 = require("moonscript.util") + split = _obj_0.split +end +local dirsep = package.config:sub(1, 1) +local dirsep_chars +if dirsep == "\\" then + dirsep_chars = "\\/" +else + dirsep_chars = dirsep +end +local mkdir +mkdir = function(path) + local chunks = split(path, dirsep) + local accum + for _index_0 = 1, #chunks do + local dir = chunks[_index_0] + accum = accum and tostring(accum) .. tostring(dirsep) .. tostring(dir) or dir + lfs.mkdir(accum) + end + return lfs.attributes(path, "mode") +end +local normalize_dir +normalize_dir = function(path) + return path:match("^(.-)[" .. tostring(dirsep_chars) .. "]*$") .. dirsep +end +local parse_dir +parse_dir = function(path) + return (path:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*$")) +end +local parse_file +parse_file = function(path) + return (path:match("^.-([^" .. tostring(dirsep_chars) .. "]*)$")) +end +local convert_path +convert_path = function(path) + local new_path = path:gsub("%.moon$", ".lua") + if new_path == path then + new_path = path .. ".lua" + end + return new_path +end +local format_time +format_time = function(time) + return ("%.3fms"):format(time * 1000) +end +local gettime +do + local socket + gettime = function() + if socket == nil then + pcall(function() + socket = require("socket") + end) + if not (socket) then + socket = false + end + end + if socket then + return socket.gettime() + else + return nil, "LuaSocket needed for benchmark" + end + end +end +local compile_file_text +compile_file_text = function(text, fname, opts) + if opts == nil then + opts = { } + end + local parse = require("moonscript.parse") + local compile = require("moonscript.compile") + local parse_time + if opts.benchmark then + parse_time = assert(gettime()) + end + local tree, err = parse.string(text) + if not (tree) then + return nil, err + end + if parse_time then + parse_time = gettime() - parse_time + end + if opts.show_parse_tree then + local dump = require("moonscript.dump") + dump.tree(tree) + return nil + end + local compile_time + if opts.benchmark then + compile_time = gettime() + end + local code, posmap_or_err, err_pos = compile.tree(tree) + if not (code) then + return nil, compile.format_error(posmap_or_err, err_pos, text) + end + if compile_time then + compile_time = gettime() - compile_time + end + if opts.show_posmap then + local debug_posmap + do + local _obj_0 = require("moonscript.util") + debug_posmap = _obj_0.debug_posmap + end + print("Pos", "Lua", ">>", "Moon") + print(debug_posmap(posmap_or_err, text, code)) + return nil + end + if opts.benchmark then + print(table.concat({ + fname, + "Parse time \t" .. format_time(parse_time), + "Compile time\t" .. format_time(compile_time), + "" + }, "\n")) + return nil + end + return code +end +return { + dirsep = dirsep, + mkdir = mkdir, + normalize_dir = normalize_dir, + parse_dir = parse_dir, + parse_file = parse_file, + new_path = new_path, + convert_path = convert_path, + gettime = gettime, + format_time = format_time, + compile_file_text = compile_file_text +} diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon new file mode 100644 index 00000000..79cefe1c --- /dev/null +++ b/moonscript/cmd/moonc.moon @@ -0,0 +1,122 @@ +-- assorted utilities for moonc command line tool + +lfs = require "lfs" + +import split from require "moonscript.util" + +dirsep = package.config\sub 1,1 +dirsep_chars = if dirsep == "\\" + "\\/" -- windows +else + dirsep + + +-- similar to mkdir -p +mkdir = (path) -> + chunks = split path, dirsep + + local accum + for dir in *chunks + accum = accum and "#{accum}#{dirsep}#{dir}" or dir + lfs.mkdir accum + + lfs.attributes path, "mode" + +-- strips excess / and ensures path ends with / +normalize_dir = (path) -> + path\match("^(.-)[#{dirsep_chars}]*$") .. dirsep + +-- parse the directory out of a path +parse_dir = (path) -> + (path\match "^(.-)[^#{dirsep_chars}]*$") + +-- parse the filename out of a path +parse_file = (path) -> + (path\match "^.-([^#{dirsep_chars}]*)$") + +-- converts .moon to a .lua path for calcuating compile target +convert_path = (path) -> + new_path = path\gsub "%.moon$", ".lua" + if new_path == path + new_path = path .. ".lua" + new_path + +format_time = (time) -> + "%.3fms"\format time*1000 + +gettime = do + local socket + -> + if socket == nil + pcall -> + socket = require "socket" + + unless socket + socket = false + + if socket + socket.gettime() + else + nil, "LuaSocket needed for benchmark" + +-- compiles file to lua +-- returns nil, error on error +-- returns just nil if some option handled the output instead +compile_file_text = (text, fname, opts={}) -> + parse = require "moonscript.parse" + compile = require "moonscript.compile" + + parse_time = if opts.benchmark + assert gettime! + + tree, err = parse.string text + return nil, err unless tree + + if parse_time + parse_time = gettime! - parse_time + + if opts.show_parse_tree + dump = require "moonscript.dump" + dump.tree tree + return nil + + compile_time = if opts.benchmark + gettime! + + code, posmap_or_err, err_pos = compile.tree tree + + unless code + return nil, compile.format_error posmap_or_err, err_pos, text + + if compile_time + compile_time = gettime() - compile_time + + if opts.show_posmap + import debug_posmap from require "moonscript.util" + print "Pos", "Lua", ">>", "Moon" + print debug_posmap posmap_or_err, text, code + return nil + + if opts.benchmark + print table.concat { + fname, + "Parse time \t" .. format_time(parse_time), + "Compile time\t" .. format_time(compile_time), + "" + }, "\n" + return nil + + code + +{ + :dirsep + :mkdir + :normalize_dir + :parse_dir + :parse_file + :new_path + :convert_path + :gettime + :format_time + :compile_file_text +} diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon new file mode 100644 index 00000000..cee4bbee --- /dev/null +++ b/spec/cmd_spec.moon @@ -0,0 +1,68 @@ + +moonc = require "moonscript.cmd.moonc" + +-- TODO: add specs for windows equivalents + +describe "moonc", -> + same = (fn, a, b)-> + assert.same b, fn a + + it "should normalize dir", -> + same moonc.normalize_dir, "hello/world/", "hello/world/" + same moonc.normalize_dir, "hello/world//", "hello/world/" + same moonc.normalize_dir, "", "/" -- wrong + same moonc.normalize_dir, "hello", "hello/" + + + it "should parse dir", -> + same moonc.parse_dir, "/hello/world/file", "/hello/world/" + same moonc.parse_dir, "/hello/world/", "/hello/world/" + same moonc.parse_dir, "world", "" + same moonc.parse_dir, "", "" + + it "should parse file", -> + same moonc.parse_file, "/hello/world/file", "file" + same moonc.parse_file, "/hello/world/", "" + same moonc.parse_file, "world", "world" + same moonc.parse_file, "", "" + + it "convert path", -> + same moonc.convert_path, "test.moon", "test.lua" + same moonc.convert_path, "/hello/file.moon", "/hello/file.lua" + same moonc.convert_path, "/hello/world/file", "/hello/world/file.lua" + + it "chould compile file text", -> + assert.same { + [[return print('hello')]] + }, { + moonc.compile_file_text "print'hello'", "test.moon" + } + + describe "stubbed lfs", -> + local dirs + + before_each -> + dirs = {} + package.loaded.lfs = nil + package.loaded["moonscript.cmd.moonc"] = nil + + package.loaded.lfs = { + mkdir: (dir) -> table.insert dirs, dir + attributes: -> "directory" + } + + moonc = require "moonscript.cmd.moonc" + + after_each -> + package.loaded.lfs = nil + package.loaded["moonscript.cmd.moonc"] = nil + moonc = require "moonscript.cmd.moonc" + + it "should make directory", -> + moonc.mkdir "hello/world/directory" + assert.same { + "hello" + "hello/world" + "hello/world/directory" + }, dirs + From a4e03813fc552fe55f7525d86c8ca1cb32bad44a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 18 Jun 2014 09:48:25 -0700 Subject: [PATCH 053/344] more moonc functions to module --- bin/moonc | 55 +++++---------------------------------- moonscript/cmd/moonc.lua | 53 ++++++++++++++++++++++++++++--------- moonscript/cmd/moonc.moon | 46 ++++++++++++++++++++++++++++---- spec/cmd_spec.moon | 2 +- 4 files changed, 89 insertions(+), 67 deletions(-) diff --git a/bin/moonc b/bin/moonc index f5b11842..13a8ef0d 100755 --- a/bin/moonc +++ b/bin/moonc @@ -67,50 +67,9 @@ local normalize_dir = moonc.normalize_dir local parse_dir = moonc.parse_dir local parse_file = moonc.parse_file local convert_path = moonc.convert_path -local compile_file_text = moonc.compile_file_text +local compile_and_write = moonc.compile_and_write -function write_file(fname, code) - if opts.p then - if code ~= "" then print(code) end - else - mkdir(parse_dir(fname)) - local out_f = io.open(fname, "w") - if not out_f then - return nil, "Failed to write output: "..fname - end - - out_f:write(code.."\n") - out_f:close() - end - - return "built" -end - -function compile_and_write(from, to) - local f = io.open(from) - if not f then - return nil, "Can't find file" - end - local text = f:read("*a") - - local code, err = compile_file_text(text, from, { - benchmark = opts.b, - show_posmap = opts.X, - show_parse_tree = opts.T, - }) - - if not code and err then - return nil, err - end - - if not code then - return true - end - - return write_file(to, code) -end - -function scan_directory(root, collected) +local function scan_directory(root, collected) root = normalize_dir(root) collected = collected or {} @@ -131,7 +90,7 @@ function scan_directory(root, collected) return collected end -function remove_dups(tbl, key_fn) +local function remove_dups(tbl, key_fn) local hash = {} local final = {} @@ -156,7 +115,7 @@ local function is_abs_path(path) end -- creates tuples of input and target -function get_files(fname, files) +local function get_files(fname, files) files = files or {} if lfs.attributes(fname, "mode") == "directory" then @@ -227,7 +186,7 @@ files = remove_dups(files, function(f) return f[2] end) -function get_sleep_func() +local function get_sleep_func() local sleep if not pcall(function() require "socket" @@ -242,7 +201,7 @@ function get_sleep_func() return sleep end -function plural(count, word) +local function plural(count, word) if count ~= 1 then word = word .. "s" end @@ -250,7 +209,7 @@ function plural(count, word) end -- returns an iterator that returns files that have been updated -function create_watcher(files) +local function create_watcher(files) local msg = "Starting watch loop (Ctrl-C to exit)" local inotify diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index 642e68c4..d793b329 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -4,14 +4,13 @@ do local _obj_0 = require("moonscript.util") split = _obj_0.split end -local dirsep = package.config:sub(1, 1) -local dirsep_chars +local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write +dirsep = package.config:sub(1, 1) if dirsep == "\\" then dirsep_chars = "\\/" else dirsep_chars = dirsep end -local mkdir mkdir = function(path) local chunks = split(path, dirsep) local accum @@ -22,19 +21,15 @@ mkdir = function(path) end return lfs.attributes(path, "mode") end -local normalize_dir normalize_dir = function(path) return path:match("^(.-)[" .. tostring(dirsep_chars) .. "]*$") .. dirsep end -local parse_dir parse_dir = function(path) return (path:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*$")) end -local parse_file parse_file = function(path) return (path:match("^.-([^" .. tostring(dirsep_chars) .. "]*)$")) end -local convert_path convert_path = function(path) local new_path = path:gsub("%.moon$", ".lua") if new_path == path then @@ -42,11 +37,9 @@ convert_path = function(path) end return new_path end -local format_time format_time = function(time) return ("%.3fms"):format(time * 1000) end -local gettime do local socket gettime = function() @@ -65,7 +58,6 @@ do end end end -local compile_file_text compile_file_text = function(text, fname, opts) if opts == nil then opts = { } @@ -86,7 +78,7 @@ compile_file_text = function(text, fname, opts) if opts.show_parse_tree then local dump = require("moonscript.dump") dump.tree(tree) - return nil + return true end local compile_time if opts.benchmark then @@ -107,7 +99,7 @@ compile_file_text = function(text, fname, opts) end print("Pos", "Lua", ">>", "Moon") print(debug_posmap(posmap_or_err, text, code)) - return nil + return true end if opts.benchmark then print(table.concat({ @@ -120,6 +112,40 @@ compile_file_text = function(text, fname, opts) end return code end +write_file = function(fname, code) + mkdir(parse_dir(fname)) + local f, err = io.open(fname, "w") + if not (f) then + return nil, err + end + assert(f:write(code)) + assert(f:write("\n")) + f:close() + return "build" +end +compile_and_write = function(src, dest, opts) + if opts == nil then + opts = { } + end + local f = io.open(src) + if not (f) then + return nil, "Can't find file" + end + local text = assert(f:read("*a")) + f:close() + local code, err = compile_file_text(text, opts) + if not code then + return nil, err + end + if code == true then + return true + end + if opts.print then + print(text) + return true + end + return write_file(dest, code) +end return { dirsep = dirsep, mkdir = mkdir, @@ -130,5 +156,6 @@ return { convert_path = convert_path, gettime = gettime, format_time = format_time, - compile_file_text = compile_file_text + compile_file_text = compile_file_text, + compile_and_write = compile_and_write } diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon index 79cefe1c..3bb5716f 100644 --- a/moonscript/cmd/moonc.moon +++ b/moonscript/cmd/moonc.moon @@ -4,13 +4,14 @@ lfs = require "lfs" import split from require "moonscript.util" +local * + dirsep = package.config\sub 1,1 dirsep_chars = if dirsep == "\\" "\\/" -- windows else dirsep - -- similar to mkdir -p mkdir = (path) -> chunks = split path, dirsep @@ -59,9 +60,9 @@ gettime = do else nil, "LuaSocket needed for benchmark" --- compiles file to lua +-- compiles file to lua, returns lua code -- returns nil, error on error --- returns just nil if some option handled the output instead +-- returns true if some option handled the output instead compile_file_text = (text, fname, opts={}) -> parse = require "moonscript.parse" compile = require "moonscript.compile" @@ -78,7 +79,7 @@ compile_file_text = (text, fname, opts={}) -> if opts.show_parse_tree dump = require "moonscript.dump" dump.tree tree - return nil + return true compile_time = if opts.benchmark gettime! @@ -95,7 +96,7 @@ compile_file_text = (text, fname, opts={}) -> import debug_posmap from require "moonscript.util" print "Pos", "Lua", ">>", "Moon" print debug_posmap posmap_or_err, text, code - return nil + return true if opts.benchmark print table.concat { @@ -108,6 +109,39 @@ compile_file_text = (text, fname, opts={}) -> code +write_file = (fname, code) -> + mkdir parse_dir fname + f, err = io.open fname, "w" + unless f + return nil, err + + assert f\write code + assert f\write "\n" + f\close! + "build" + +compile_and_write = (src, dest, opts={}) -> + f = io.open src + unless f + return nil, "Can't find file" + + text = assert f\read("*a") + f\close! + + code, err = compile_file_text text, opts + + if not code + return nil, err + + if code == true + return true + + if opts.print + print text + return true + + write_file dest, code + { :dirsep :mkdir @@ -118,5 +152,7 @@ compile_file_text = (text, fname, opts={}) -> :convert_path :gettime :format_time + :compile_file_text + :compile_and_write } diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index cee4bbee..30e3deee 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -31,7 +31,7 @@ describe "moonc", -> same moonc.convert_path, "/hello/file.moon", "/hello/file.lua" same moonc.convert_path, "/hello/world/file", "/hello/world/file.lua" - it "chould compile file text", -> + it "should compile file text", -> assert.same { [[return print('hello')]] }, { From 5904373ab81c7881c7a3fc97c694865a8871e11e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 18 Jun 2014 10:14:26 -0700 Subject: [PATCH 054/344] path_to_target --- bin/moonc | 43 ++++++++------------------------------- moonscript/cmd/moonc.lua | 39 ++++++++++++++++++++++++++++++++++- moonscript/cmd/moonc.moon | 35 +++++++++++++++++++++++++++++++ spec/cmd_spec.moon | 20 ++++++++++++++++++ 4 files changed, 102 insertions(+), 35 deletions(-) diff --git a/bin/moonc b/bin/moonc index 13a8ef0d..3a3ae021 100755 --- a/bin/moonc +++ b/bin/moonc @@ -68,6 +68,7 @@ local parse_dir = moonc.parse_dir local parse_file = moonc.parse_file local convert_path = moonc.convert_path local compile_and_write = moonc.compile_and_write +local path_to_target = moonc.path_to_target local function scan_directory(root, collected) root = normalize_dir(root) @@ -105,48 +106,22 @@ local function remove_dups(tbl, key_fn) return final end -local function is_abs_path(path) - local first = path:sub(1, 1) - if dirsep == "\\" then - return first == "/" or first == "\\" or path:sub(2,1) == ":" - else - return first == dirsep - end -end - -- creates tuples of input and target local function get_files(fname, files) files = files or {} if lfs.attributes(fname, "mode") == "directory" then - local head = fname:match("^(.-)[^" .. dirsep .. "]*" .. dirsep .."?$") for _, sub_fname in ipairs(scan_directory(fname)) do - local target_fname = convert_path(sub_fname) - if opts.t then - if head then - local start, stop = target_fname:find(head, 1, true) - if start == 1 then - target_fname = target_fname:sub(stop + 1) - end - end - - target_fname = normalize_dir(opts.t) .. target_fname - end - - table.insert(files, {sub_fname, target_fname}) + table.insert(files, { + sub_fname, + path_to_target(sub_fname, opts.t, fname) + }) end else - local target_fname = convert_path(fname) - if opts.t then - local prefix = normalize_dir(opts.t) - - if is_abs_path(target_fname) then - target_fname = parse_file(target_fname) - end - target_fname = prefix .. target_fname - end - - table.insert(files, {fname, target_fname}) + table.insert(files, { + fname, + path_to_target(fname, opts.t) + }) end return files diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index d793b329..55009c9b 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -4,7 +4,7 @@ do local _obj_0 = require("moonscript.util") split = _obj_0.split end -local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write +local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write, is_abs_path, path_to_target dirsep = package.config:sub(1, 1) if dirsep == "\\" then dirsep_chars = "\\/" @@ -146,6 +146,42 @@ compile_and_write = function(src, dest, opts) end return write_file(dest, code) end +is_abs_path = function(path) + local first = path:sub(1, 1) + if dirsep == "\\" then + return first == "/" or first == "\\" or path:sub(2, 1) == ":" + else + return first == dirsep + end +end +path_to_target = function(path, target_dir, base_dir) + if target_dir == nil then + target_dir = nil + end + if base_dir == nil then + base_dir = nil + end + local target = convert_path(path) + if target_dir then + target_dir = normalize_dir(target_dir) + end + if base_dir and target_dir then + local head = base_dir:match("^(.-)[^" .. tostring(dirsep_chars) .. "]*[" .. tostring(dirsep_chars) .. "]?$") + if head then + local start, stop = target:find(head, 1, true) + if start == 1 then + target = target:sub(stop + 1) + end + end + end + if target_dir then + if is_abs_path(target) then + target = parse_file(target) + end + target = target_dir .. target + end + return target +end return { dirsep = dirsep, mkdir = mkdir, @@ -156,6 +192,7 @@ return { convert_path = convert_path, gettime = gettime, format_time = format_time, + path_to_target = path_to_target, compile_file_text = compile_file_text, compile_and_write = compile_and_write } diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon index 3bb5716f..8cb2bbb2 100644 --- a/moonscript/cmd/moonc.moon +++ b/moonscript/cmd/moonc.moon @@ -142,6 +142,40 @@ compile_and_write = (src, dest, opts={}) -> write_file dest, code +is_abs_path = (path) -> + first = path\sub 1, 1 + if dirsep == "\\" + first == "/" or first == "\\" or path\sub(2,1) == ":" + else + first == dirsep + + +-- calcuate where a path should be compiled to +-- target_dir: the directory to place the file (optional, from -t flag) +-- base_dir: the directory where the file came from when globbing recursively +path_to_target = (path, target_dir=nil, base_dir=nil) -> + target = convert_path path + + if target_dir + target_dir = normalize_dir target_dir + + if base_dir and target_dir + -- one directory back + head = base_dir\match("^(.-)[^#{dirsep_chars}]*[#{dirsep_chars}]?$") + + if head + start, stop = target\find head, 1, true + if start == 1 + target = target\sub(stop + 1) + + if target_dir + if is_abs_path target + target = parse_file target + + target = target_dir .. target + + target + { :dirsep :mkdir @@ -152,6 +186,7 @@ compile_and_write = (src, dest, opts={}) -> :convert_path :gettime :format_time + :path_to_target :compile_file_text :compile_and_write diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 30e3deee..3ac5dea0 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -31,6 +31,26 @@ describe "moonc", -> same moonc.convert_path, "/hello/file.moon", "/hello/file.lua" same moonc.convert_path, "/hello/world/file", "/hello/world/file.lua" + it "calculate target", -> + p = moonc.path_to_target + + assert.same "test.lua", p "test.moon" + assert.same "hello/world.lua", p "hello/world.moon" + assert.same "compiled/test.lua", p "test.moon", "compiled" + + assert.same "/home/leafo/test.lua", p "/home/leafo/test.moon" + assert.same "compiled/test.lua", p "/home/leafo/test.moon", "compiled" + assert.same "/compiled/test.lua", p "/home/leafo/test.moon", "/compiled/" + + assert.same "moonscript/hello.lua", p "moonscript/hello.moon", nil, "moonscript" + assert.same "out/moonscript/hello.lua", p "moonscript/hello.moon", "out", "moonscript" + + assert.same "out/moonscript/package/hello.lua", + p "moonscript/package/hello.moon", "out", "moonscript/" + + assert.same "/out/moonscript/package/hello.lua", + p "/home/leafo/moonscript/package/hello.moon", "/out", "/home/leafo/moonscript" + it "should compile file text", -> assert.same { [[return print('hello')]] From 6cdfca6ab2befa335eba65de5101a54bf03a193d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 18 Jun 2014 10:26:20 -0700 Subject: [PATCH 055/344] clean up moonc --- bin/moonc | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/bin/moonc b/bin/moonc index 3a3ae021..d68302a9 100755 --- a/bin/moonc +++ b/bin/moonc @@ -1,16 +1,8 @@ #!/usr/bin/env lua -local parse = require "moonscript.parse" -local compile = require "moonscript.compile" -local util = require "moonscript.util" - -local dump_tree = require"moonscript.dump".tree - local alt_getopt = require "alt_getopt" local lfs = require "lfs" -local dirsep = package.config:sub(1,1) - local opts, ind = alt_getopt.get_opts(arg, "lvhwt:o:pTXb", { print = "p", tree = "T", version = "v", help = "h", lint = "l" }) @@ -113,15 +105,15 @@ local function get_files(fname, files) if lfs.attributes(fname, "mode") == "directory" then for _, sub_fname in ipairs(scan_directory(fname)) do table.insert(files, { - sub_fname, - path_to_target(sub_fname, opts.t, fname) - }) + sub_fname, + path_to_target(sub_fname, opts.t, fname) + }) end else table.insert(files, { - fname, - path_to_target(fname, opts.t) - }) + fname, + path_to_target(fname, opts.t) + }) end return files @@ -130,8 +122,12 @@ end if opts.h then print_help() end if read_stdin then + local parse = require "moonscript.parse" + local compile = require "moonscript.compile" + local text = io.stdin:read("*a") local tree, err = parse.string(text) + if not tree then error(err) end local code, err, pos = compile.tree(tree) @@ -158,7 +154,7 @@ for _, input in ipairs(inputs) do end files = remove_dups(files, function(f) - return f[2] + return f[2] end) local function get_sleep_func() @@ -293,7 +289,7 @@ if opts.w then elseif opts.l then for _, tuple in pairs(files) do local fname = tuple[1] - lint = require "moonscript.cmd.lint" + local lint = require "moonscript.cmd.lint" local res, err = lint.lint_file(fname) if res then io.stderr:write(res .. "\n\n") From fec4114adcca82e451e7611efbeafa903aaf3ddd Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 18 Jun 2014 23:45:47 -0700 Subject: [PATCH 056/344] split apart docs into more pages, add some more content --- docs/api.md | 131 +++++++++++++++++ docs/command_line.md | 250 +++++++++++++++++++++++++++++++ docs/reference.md | 343 +------------------------------------------ docs/standard_lib.md | 8 +- 4 files changed, 392 insertions(+), 340 deletions(-) create mode 100644 docs/api.md create mode 100644 docs/command_line.md diff --git a/docs/api.md b/docs/api.md new file mode 100644 index 00000000..367891af --- /dev/null +++ b/docs/api.md @@ -0,0 +1,131 @@ +target: reference/api +template: reference +title: Compiler API +short_name: api +-- + +# MoonScript Compiler API + +## Autocompiling with the `moonscript` Module + +After installing MoonScript, you can include the `moonscript` module to make +any Lua script MoonScript aware. + +```lua +require "moonscript" +``` + +After `moonscript` is required, Lua's package loader is updated to search for +`.moon` files on any subsequent calls to `require`. The search path for `.moon` +files is based on the current `package.path` value in Lua when `moonscript` is +required. Any search paths in `package.path` ending in `.lua` are copied, +rewritten to end in `.moon`, and then inserted in `package.moonpath`. + +The `moonloader` is the function that is responsible for searching +`package.moonpath` for a file available to be included. It is inserted in the +second position of the `package.loaders` table. This means that a matching `.moon` file +will be loaded over a matching `.lua` file that has the same base name. + +For more information on Lua's `package.loaders` see [Lua Reference Manual +— +package.loaders](http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders) + +The `moonloader`, when finding a valid path to a `.moon` file, will parse and +compile the file in memory. The code is then turned into a function using the +built in `load` function, which is run as the module. + +If you are executing MoonScript code with the included `moon` command line tool +then it is not required to include this module before including any other +MoonScript modules. + +## `moonscript.base` Module + +```moononly +moonscript = require "moonscript.base" +``` + +This module contains an assortment of functions for loading and compiling +MoonScript code from within Lua. + +The module provides `load`, `loadfile`, `loadstring` functions, which are +analogous to the similarly named Lua functions. The major difference is that +they load MoonScript code instead of Lua code. + + +```moononly +moonscript = require "moonscript.base" + +fn = moonscript.loadstring 'print "hi!"' +fn! +``` + +All of these functions can take an optional last argument, a table of options. +The only option right now is `implicitly_return_root`. Setting this to `false` +makes it so the file does not implicitly return its last statement. + + +```moononly +moonscript = require "moonscript.base" + +fn = moonscript.loadstring "10" +print fn! -- prints "10" + +fn = moonscript.loadstring "10", implicitly_return_root: false +print fn! -- prints nothing +``` + +One more useful function is provided: `to_lua`. This function takes a string of +MoonScript code and returns the compiled Lua result along with the line mapping +table. If there are any errors then `nil` and the error message are returned. + + +```moononly +import to_lua from require "moonscript.base" + +lua_code, line_tabel = to_lua [[ +x = 124 +print "hello world #{x}" +]] +``` + +Similar to the `load*` functions from above, `to_lua` can take an optional +final argument of a table of options. + +The second return value of `to_lua` is useful if you want to perform line +number reversal. It's a table where the key is a Lua line number and the value +is a character offset from the original MoonScript source. + +## Programmatically Compiling + +If you need finder grained control over the compilation process you can use the +raw parse and compile modules. + +Parsing converts a string of MoonScript into an abstract syntax tree. Compiling +converts an abstract syntax tree into a Lua code string. + +Knowledge of this API may be useful for creating tools to aid the generation of +Lua code from MoonScript code. For example, you could build a macro system by +analyzing and manipulating the abstract syntax tree. Be warned though, the +format of the abstract syntax tree is undocumented and may change in the +future. + +Here is a quick example of how you would compile a MoonScript string to a Lua +String (This is effectively the same as the `to_lua` function described above): + +```moononly +parse = require "moonscript.parse" +compile = require "moonscript.compile" + +moon_code = [[(-> print "hello world")!]] + +tree, err = parse.string moon_code +unless tree + error "Parse error: " .. err + +lua_code, err, pos = compile.tree tree +unless lua_code + error compile.format_error err, pos, moon_code + +-- our code is ready +print lua_code +``` diff --git a/docs/command_line.md b/docs/command_line.md new file mode 100644 index 00000000..a68e3485 --- /dev/null +++ b/docs/command_line.md @@ -0,0 +1,250 @@ +target: reference/command_line +template: reference +title: Command Line Tools +short_name: command_line +-- + +# Command Line Tools + +Two tools are installed with MoonScript, `moon` and `moonc`. + +`moonc` is for compiling MoonScript code to Lua. +`moon` is for running MoonScript code directly. + +## `moon` + +`moon` can be used to run MoonScript files directly from the command line, +without needing a separate compile step. All MoonScript files are compiled in +memory as they are executed. + +```bash +$ moon my_script.moon +``` + +Any MoonScript files that are required will also be compiled on demand as they +are loaded. + +When an error occurs during runtime, the stack trace is rewritten to give line +numbers from the original `.moon` file. + +If you want to disable [error rewriting](#error_rewriting), you can pass the +`-d` flag. A full list of flags can be seen by passing the `-h` or `--help` +flag. + +### Error Rewriting + +Runtime errors are given special attention when running code using the `moon` +command line tool. Because code is written in MoonScript but executed as Lua, +errors that happen during runtime report Lua line numbers. This can make +debugging less than ideal. + +In order to solve this problem MoonScript builds up a table of line number +mappings, allowing the runtime to calculate what line of MoonScript generated +the line of Lua that triggered the error. + +Consider the following file with a bug (note the invalid `z` variable): + +```moon +add_numbers = (x,y) -> x + z -- 1 +print add_numbers 10,0 -- 2 +``` + +The following error is generated: + + moon: scrap.moon:1(3): attempt to perform arithmetic on global 'z' (a nil value) + stack traceback: + scrap.moon:1(3): in function 'add_numbers' + scrap.moon:2(5): in main chunk + + +Notice how next to the file name there are two numbers. The first number is the +rewritten line number. The number in the parentheses is the original Lua line +number. + +The error in this example is being reported on line 1 of the `moon` file, which +corresponds to line 3 of the generated Lua code. The entire stack trace is rewritten in +addition to the error message. + +### Code Coverage + +`moon` lets you run a MoonScript file while keeping track of which lines +are executed with the `-c` flag. + +For example, consider the following `.moon` file: + +```moononly +-- test.moon +first = -> + print "hello" + +second = -> + print "world" + +first! +``` + +We can execute and get a glance of which lines ran: + +```bash +$ moon -c test.moon +``` + +The following output is produced: + + ------| @cool.moon + 1| -- test.moon + * 2| first = -> + * 3| print "hello" + 4| + * 5| second = -> + 6| print "world" + 7| + * 8| first! + 9| + +The star next to the line means that it was executed. Blank lines are not +considered when running so by default they don't get marked as executed. + +## `moonc` + +`moonc` is used for transforming MoonScript files into Lua files. +It takes a list of files, compiles them all, and creates the associated `.lua` +files in the same directories. + +```bash +$ moonc my_script1.moon my_script2.moon ... +``` + +You can control where the compiled files are put using the `-t` flag, followed +by a directory. + +`moonc` can also take a directory as an argument, and it will recursively scan +for all MoonScript files and compile them. + +`moonc` can write to standard out by passing the `-p` flag. + +The `-w` flag can be used to enable watch mode. `moonc` will stay running, and +watch for changes to the input files. If any of them change then they will be +compiled automatically. + +A full list of flags can be seen by passing the `-h` or `--help` flag. + +### Linter + +`moonc` contains a [lint][1] tool +for statically detecting potential problems with code. The current linter only +has one test: it detects the use of global variables. + +You can execute the linter with the `-l` flag. When the linting flag is +provided only linting takes place and no compiled code is generated. + +```bash +moonc -l file1.moon file2.moon +``` + +Like when compiling, you can also pass a directory as a command line argument +to recursively process all the `.moon` files. + +#### Global Variable Checking + +It's considered good practice to avoid using global variables and create local +variables for all the values referenced. A good case for not using global +variables is that you can analyize the code ahead of time without the need to +execute it to find references to undeclared variables. + +MoonScript makes it difficult to declare global variables by forcing you to be +explicit with the `export` keyword, so it's a good candidate for doing this +kind of linting. + +Consider the following program with a typo: (`my_number` is spelled wrong as +`my_nmuber` in the function) + +```moononly +-- lint_example.moon +my_number = 1234 + +some_function = -> + -- a contrived example with a small chance to pass + if math.random() < 0.01 + my_nmuber + 10 + +some_function! +``` + +Although there is a bug in this code, it rarely happens during execution. It's +more likely to be missed during development and cause problems in the future. + +Running the linter immediately identifies the problem: + +```bash +$ moonc -l lint_example.moon +``` + +Outputs: + + ./lint_example.moon + + line 7: accessing global my_nmuber + ================================== + > my_nmuber + 10 + +#### Global Variable Whitelist + +In most circumstances it's impossible to avoid using some global variables. For +example, to access any of the built in modules or functions you typically +access them globally. + +For this reason a global variable whitelist is used. It's a list of global +variables that are allowed to be used. A default whitelist is provided that +contains all of Lua's built in functions and modules. + +You can create your own entires in the whitelist as well. For example, the +testing framework [Busted](http://olivinelabs.com/busted) uses a collection of +global functions (like `describe`, `before_each`, `setup`) to make writing +tests easy. + +It would be nice if we could allow all of those global functions to be called +for `.moon` files located in the `spec/` directory. We can do that by creating +a `lint_config` file. + +`lint_config` is a regular MoonScript or Lua file that provides configuration +for the linter. One of those settings is `whitelist_globals`. + +To create a configuration for Busted we might do something like this: + +```moononly +-- lint_config.moon +{ + whitelist_globals: { + ["spec/"]: { + "it", "describe", "setup", "teardown", + "before_each", "after_each", "pending" + } + } +} +``` + +Compile the file: + +```bash +$ moonc lint_config.moon +``` + +Then run the linter on your entire project: + +```bash +$ moonc -l . +``` + +The whitelisted global references in `spec/` will no longer raise notices. + +The `whitelist_globals` property of the `lint_config` is a table where the keys +are Lua patterns that match file names, and the values are an array of globals +that are allowed. + +Multiple patterns in `whitelist_globals` can match a single file, the union of +the allowed globals will be used when linting that file. + + + [1]: http://en.wikipedia.org/wiki/Lint_(software) + diff --git a/docs/reference.md b/docs/reference.md index 8bcfacf4..a67fd332 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1,8 +1,9 @@ - target: reference/index - template: reference - title: MoonScript v0.2.5 - Language Guide - short_name: lang +target: reference/index +template: reference +title: Language Guide +short_name: lang -- + MoonScript is a programming language that compiles to [Lua](http://www.lua.org). This guide expects the reader to have basic familiarity with Lua. For each code snippet below, the MoonScript is on the @@ -24,8 +25,8 @@ MoonScript is a whitespace sensitive language. This means that instead of using `do` and `end` (or `{` and `}`) to delimit sections of code we use line-breaks and indentation. -This means that how you indent you code is important. Luckily MoonScript -doesn't care how you do it but it's important to be consistent. +This means that how you indent your code is important. Luckily MoonScript +doesn't care how you do it but only requires that you be consistent. An indent must be at least 1 space or 1 tab, but you can use as many as you like. All the code snippets on this page will use two spaces. @@ -1555,335 +1556,6 @@ my_second_function = -> print "another function" If you need to forward declare your values so you can access them regardless of their written order you can add `local *` to the top of your file. -# MoonScript API - -## `moonscript` Module - -Upon installing MoonScript, a `moonscript` module is made available. The best -use of this module is making your Lua's require function MoonScript aware. - -```lua -require "moonscript" -``` - -After `moonscript` is required, Lua's package loader is updated to search for -`.moon` files on any subsequent calls to `require`. The search path for `.moon` -files is based on the current `package.path` value in Lua when `moonscript` is -required. Any search paths in `package.path` ending in `.lua` are copied, -rewritten to end in `.moon`, and then inserted in `package.moonpath`. - -The `moonloader` is the function that is responsible for searching -`package.moonpath` for a file available to be included. It is inserted in the -second position of the `package.loaders` table. This means that a matching `.moon` file -will be loaded over a matching `.lua` file that has the same base name. - -For more information on Lua's `package.loaders` see [Lua Reference Manual -— -package.loaders](http://www.lua.org/manual/5.1/manual.html#pdf-package.loaders) - -The `moonloader`, when finding a valid path to a `.moon` file, will parse and -compile the file in memory. The code is then turned into a function using the -built in `load` function, which is run as the module. - -### Load Functions - -MoonScript provides `moonscript.load`, `moonscript.loadfile`, -`mooonscript.loadstring`, which are analogous to Lua's `load`, `loadfile`, and -`loadstring`. - -The MoonScript functions work the same as their counterparts, except they deal -with MoonScript code instead of Lua Code. - - -```moononly -moonscript = require "moonscript" - -fn = moonscript.loadstring 'print "hi!"' -fn! -``` - -All of these functions can take an optional last argument, a table of options. -The only option right now is `implicitly_return_root`. Setting this to `false` -makes it so the file does not implicitly return its last statement. - - -```moononly -moonscript = require "moonscript" - -fn = moonscript.loadstring "10" -print fn! -- prints "10" - -fn = moonscript.loadstring "10", implicitly_return_root: false -print fn! -- prints nothing -``` - -## Error Rewriting - -Runtime errors are given special attention when running code using the `moon` -binary. Because we start off as MoonScript, but run code as Lua, errors that -happen during runtime report their line numbers as they are in the compiled -file. This can make debugging particularly difficult. - -Consider the following file with a bug (note the invalid `z` variable): - -```moon -add_numbers = (x,y) -> x + z -- 1 -print add_numbers 10,0 -- 2 -``` - -The following error is generated: - - moon: scrap.moon:1(3): attempt to perform arithmetic on global 'z' (a nil value) - stack traceback: - scrap.moon:1(3): in function 'add_numbers' - scrap.moon:2(5): in main chunk - - -Notice how next to the file name there are two numbers. The first number is the -rewritten line number. The number in the parentheses is the original Lua line -number. - -The error in this example is being reported on line 1 of the `moon` file, which -corresponds to line 3 of the generated Lua code. The entire stack trace is rewritten in -addition to the error message. - -## Programmatically Compiling - -The MoonScript module also contains methods for parsing MoonScript text into an -abstract syntax tree, and compiling an instance of a tree into Lua source code. - -Knowledge of this API may be useful for creating tools to aid the generation of -Lua code from MoonScript code. - -Here is a quick example of how you would compile a MoonScript string to a Lua -String: - -```moononly -parse = require "moonscript.parse" -compile = require "moonscript.compile" - -moon_code = [[(-> print "hello world")!]] - -tree, err = parse.string moon_code -if not tree - error "Parse error: " .. err - -lua_code, err, pos = compile.tree tree -if not lua_code - error compile.format_error err, pos, moon_code - --- our code is ready -print lua_code -``` - -# Command Line Use - -Two tools are installed with MoonScript, `moon` and `moonc`. - -`moonc` is for compiling MoonScript code to Lua. -`moon` is for running MoonsScript code directly. - -## `moon` - -`moon` can be used to run MoonsScript files directly from the command line, -without needing a separate compile step. All MoonsScript files are compiled in -memory as they are run. - -```bash -$ moon my_script.moon -``` - -Any MoonScript files that are required will also be compiled and run -automatically. - -When an error occurs during runtime, the stack trace is rewritten to give line -numbers from the original `.moon` file. - -If you want to disable [error rewriting](#error_rewriting), you can pass the -`-d` flag. A full list of flags can be seen by passing the `-h` or `--help` -flag. - -### Code Coverage - -`moon` lets you run a MoonScript file while keeping track of which lines -are executed with the `-c` flag. - -For example, consider the following `.moon` file: - -```moononly --- test.moon -first = -> - print "hello" - -second = -> - print "world" - -first! -``` - -We can execute and get a glance of which lines ran: - -```bash -$ moon -c test.moon -``` - -The following output is produced: - - ------| @cool.moon - 1| -- test.moon - * 2| first = -> - * 3| print "hello" - 4| - * 5| second = -> - 6| print "world" - 7| - * 8| first! - 9| - -The star next to the line means that it was executed. Blank lines are not -considered when running so by default they don't get marked as executed. - -## `moonc` - -`moonc` is used for transforming MoonsScript files into Lua files. -It takes a list of files, compiles them all, and creates the associated `.lua` -files in the same directories. - -```bash -$ moonc my_script1.moon my_script2.moon ... -``` - -You can control where the compiled files are put using the `-t` flag, followed -by a directory. - -`moonc` can also take a directory as an argument, and it will recursively scan -for all MoonScript files and compile them. - -`moonc` can write to standard out by passing the `-p` flag. - -The `-w` flag can be used to enable watch mode. `moonc` will stay running, and -watch for changes to the input files. If any of them change then they will be -compiled automatically. - -A full list of flags can be seen by passing the `-h` or `--help` flag. - -### Linter - -`moonc` contains a [lint][1] tool -for statically detecting potential problems with code. The current linter only -has one test: it detects the use of global variables. - -You can execute the linter with the `-l` flag. When the linting flag is -provided only linting takes place and no compiled code is generated. - -```bash -moonc -l file1.moon file2.moon -``` - -Like when compiling, you can also pass a directory as a command line argument -to recursively process all the `.moon` files. - -#### Global Variable Checking - -It's considered good practice to avoid using global variables and create local -variables for all the values referenced. A good case for not using global -variables is that you can analyize the code ahead of time without the need to -execute it to find references to undeclared variables. - -MoonScript makes it difficult to declare global variables by forcing you to be -explicit with the `export` keyword, so it's a good candidate for doing this -kind of linting. - -Consider the following program with a typo: (`my_number` is spelled wrong as -`my_nmuber` in the function) - -```moononly --- lint_example.moon -my_number = 1234 - -some_function = -> - -- a contrived example with a small chance to pass - if math.random() < 0.01 - my_nmuber + 10 - -some_function! -``` - -Although there is a bug in this code, it rarely happens during execution. It's -more likely to be missed during development and cause problems in the future. - -Running the linter immediately identifies the problem: - -```bash -$ moonc -l lint_example.moon -``` - -Outputs: - - ./lint_example.moon - - line 7: accessing global my_nmuber - ================================== - > my_nmuber + 10 - - -##### Global Variable Whitelist - -In most circumstances it's impossible to avoid using some global variables. For -example, to access any of the built in modules or functions you typically -access them globally. - -For this reason a global variable whitelist is used. It's a list of global -variables that are allowed to be used. A default whitelist is provided that -contains all of Lua's built in functions and modules. - -You can create your own entires in the whitelist as well. For example, the -testing framework [Busted](http://olivinelabs.com/busted) uses a collection of -global functions (like `describe`, `before_each`, `setup`) to make writing -tests easy. - -It would be nice if we could allow all of those global functions to be called -for `.moon` files located in the `spec/` directory. We can do that by creating -a `lint_config` file. - -`lint_config` is a regular MoonScript or Lua file that provides configuration -for the linter. One of those settings is `whitelist_globals`. - -To create a configuration for Busted we might do something like this: - -```moononly --- lint_config.moon -{ - whitelist_globals: { - ["spec/"]: { - "it", "describe", "setup", "teardown", - "before_each", "after_each", "pending" - } - } -} -``` - -Compile the file: - -```bash -$ moonc lint_config.moon -``` - -Then run the linter on your entire project: - -```bash -$ moonc -l . -``` - -The whitelisted global references in `spec/` will no longer raise notices. - -The `whitelist_globals` property of the `lint_config` is a table where the keys -are Lua patterns that match file names, and the values are an array of globals -that are allowed. - -Multiple patterns in `whitelist_globals` can match a single file, the union of -the allowed globals will be used when linting that file. - # License (MIT) Copyright (C) 2014 by Leaf Corcoran @@ -1907,4 +1579,3 @@ the allowed globals will be used when linting that file. THE SOFTWARE. - [1]: http://en.wikipedia.org/wiki/Lint_(software) diff --git a/docs/standard_lib.md b/docs/standard_lib.md index d76fe8fb..f2686081 100644 --- a/docs/standard_lib.md +++ b/docs/standard_lib.md @@ -1,7 +1,7 @@ - target: reference/standard_lib - template: reference - title: MoonScript v0.2.5 - Standard Library - short_name: stdlib +target: reference/standard_lib +template: reference +title: Standard Library +short_name: stdlib -- The MoonScript installation comes with a small kernel of functions that can be From b7f5e79bd329c7d95b6970b66c06f161dafeb5ce Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 18 Jun 2014 23:55:49 -0700 Subject: [PATCH 057/344] update version number, update changelog --- CHANGELOG.md | 9 +++++++++ moonscript/version.moon | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4464d817..00c559fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,13 @@ +# MoonScript v0.2.6 (2014-6-18) + +## Bugs Fixes + +* Fixes to posmap generation for multi-line mappings and variable declarations +* Prefix file name with `@` when loading code so stack traces tread it as file +* Fix bug where `moonc` couldn't work with absolute paths +* Improve target file path generation for `moonc` + # MoonScript v0.2.5 (2014-3-5) ## New Things diff --git a/moonscript/version.moon b/moonscript/version.moon index 62886737..156e240b 100644 --- a/moonscript/version.moon +++ b/moonscript/version.moon @@ -1,5 +1,5 @@ -version = "0.2.5" +version = "0.2.6" { version: version, From 13da7286f8a78360a8a2e1b882212ef44a24c99b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Jun 2014 00:00:10 -0700 Subject: [PATCH 058/344] restore -t to watch mode --- bin/moonc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bin/moonc b/bin/moonc index d68302a9..c408cc81 100755 --- a/bin/moonc +++ b/bin/moonc @@ -58,7 +58,6 @@ local mkdir = moonc.mkdir local normalize_dir = moonc.normalize_dir local parse_dir = moonc.parse_dir local parse_file = moonc.parse_file -local convert_path = moonc.convert_path local compile_and_write = moonc.compile_and_write local path_to_target = moonc.path_to_target @@ -271,7 +270,7 @@ if opts.w then end for fname in protected do - local target = convert_path(fname) + local target = path_to_target(fname, opts.t) local success, err = compile_and_write(fname, target) if not success then io.stderr:write(table.concat({ From fe2984f85d49cee8139813a5307460fb2e7493c4 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Jun 2014 00:04:10 -0700 Subject: [PATCH 059/344] restore -o --- bin/moonc | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index c408cc81..3466d330 100755 --- a/bin/moonc +++ b/bin/moonc @@ -118,7 +118,9 @@ local function get_files(fname, files) return files end -if opts.h then print_help() end +if opts.h then + print_help() +end if read_stdin then local parse = require "moonscript.parse" @@ -156,6 +158,10 @@ files = remove_dups(files, function(f) return f[2] end) +if opts.o and #files > 1 then + print_help("-o can not be used with multiple input files") +end + local function get_sleep_func() local sleep if not pcall(function() @@ -271,6 +277,11 @@ if opts.w then for fname in protected do local target = path_to_target(fname, opts.t) + + if opts.o then + target = opts.o + end + local success, err = compile_and_write(fname, target) if not success then io.stderr:write(table.concat({ @@ -299,6 +310,10 @@ elseif opts.l then else for _, tuple in ipairs(files) do local fname, target = unpack(tuple) + if opts.o then + target = opts.o + end + local success, err = compile_and_write(fname, target) if not success then io.stderr:write(fname .. "\t" .. err .. "\n") From 210fd7cf071f39d1a8ed29e91f64cdf057a8e5da Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Jun 2014 00:24:52 -0700 Subject: [PATCH 060/344] rebuild version --- moonscript/version.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moonscript/version.lua b/moonscript/version.lua index 8dda91c7..ff223d3a 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,4 +1,4 @@ -local version = "0.2.5" +local version = "0.2.6" return { version = version, print_version = function() From de0717027d8ef3b06c0dd7677b694205b36dce13 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Jun 2014 00:41:59 -0700 Subject: [PATCH 061/344] fix typo --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 00c559fb..91e400eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,7 @@ # MoonScript v0.2.6 (2014-6-18) -## Bugs Fixes +## Bug Fixes * Fixes to posmap generation for multi-line mappings and variable declarations * Prefix file name with `@` when loading code so stack traces tread it as file @@ -16,7 +16,7 @@ * New [linting tool](http://moonscript.org/reference/#linter) built into `moonc`, identifies global variable references that don't pass whitelist * Numbers can have `LL` and `ULL` suffixes for LuaJIT -## Bugs Fixes +## Bug Fixes * Error messages from `moonc` are written to standard error * Moonloader correctly throws error when moon file can't be parsed, instead of skipping the module From 6d5dbf7237fa0d7f5f9ccba64714ca0cf26724df Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 8 Jul 2014 23:28:45 -0700 Subject: [PATCH 062/344] fix broken moonc -p --- bin/moonc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index 3466d330..3b276aad 100755 --- a/bin/moonc +++ b/bin/moonc @@ -314,7 +314,9 @@ else target = opts.o end - local success, err = compile_and_write(fname, target) + local success, err = compile_and_write(fname, target, { + print = opts.p + }) if not success then io.stderr:write(fname .. "\t" .. err .. "\n") os.exit(1) From 5c44d987752e779bc6be5b8eebef68e365722f60 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 17 Jun 2014 17:40:24 -0700 Subject: [PATCH 063/344] assert --- moonscript/errors.lua | 2 +- moonscript/errors.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/errors.lua b/moonscript/errors.lua index bdf5811c..d85c3162 100644 --- a/moonscript/errors.lua +++ b/moonscript/errors.lua @@ -18,7 +18,7 @@ local lookup_line lookup_line = function(fname, pos, cache) if not cache[fname] then do - local _with_0 = io.open(fname) + local _with_0 = assert(io.open(fname)) cache[fname] = _with_0:read("*a") _with_0:close() end diff --git a/moonscript/errors.moon b/moonscript/errors.moon index a6543adb..b04c5671 100644 --- a/moonscript/errors.moon +++ b/moonscript/errors.moon @@ -12,7 +12,7 @@ user_error = (...) -> -- find the line number of `pos` chars into fname lookup_line = (fname, pos, cache) -> if not cache[fname] - with io.open fname + with assert io.open(fname) cache[fname] = \read "*a" \close! pos_to_line cache[fname], pos From e191ba9858c9ea933dac5c133fafa1b8c7c66f15 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 18 Jul 2014 18:08:19 -0700 Subject: [PATCH 064/344] fix printing wrong thing --- moonscript/cmd/moonc.lua | 2 +- moonscript/cmd/moonc.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index 55009c9b..dc53cd52 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -141,7 +141,7 @@ compile_and_write = function(src, dest, opts) return true end if opts.print then - print(text) + print(code) return true end return write_file(dest, code) diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon index 8cb2bbb2..6b8b206b 100644 --- a/moonscript/cmd/moonc.moon +++ b/moonscript/cmd/moonc.moon @@ -137,7 +137,7 @@ compile_and_write = (src, dest, opts={}) -> return true if opts.print - print text + print code return true write_file dest, code From 3180dd234bd94e856f312668cec6a818cc2f393f Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 18 Jul 2014 18:10:27 -0700 Subject: [PATCH 065/344] add logo and build status badge --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index abfd3d39..31d2f52d 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,9 @@ # MoonScript +[![MoonScript](http://leafo.net/dump/sailormoonscript.png)](http://moonscript.org) + +[![Build Status](https://travis-ci.org/leafo/moonscript.svg?branch=master)](https://travis-ci.org/leafo/moonscript) + MoonScript is a programmer friendly language that compiles into [Lua](http://www.lua.org/). It gives you the power of the fastest scripting language combined with a rich set of features. It runs on Lua 5.1 and 5.2. From 3d4bdf818d26ee5862b57cbfd92e671566ca6089 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 29 Jul 2014 22:54:11 -0700 Subject: [PATCH 066/344] use older version of busted for travis --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 74670737..2cb435d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: c install: - sudo apt-get install luarocks - - sudo luarocks install busted + - sudo luarocks install https://rocks.moonscript.org/manifests/olivine-labs/busted-1.11.1-1.rockspec - sudo luarocks make script: busted From 64232fef0367cf1888a5d67db8ba09a152b1455a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 29 Jul 2014 23:04:52 -0700 Subject: [PATCH 067/344] update travis to install busted from moonrocks --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2cb435d0..114ffca2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: c install: - sudo apt-get install luarocks - - sudo luarocks install https://rocks.moonscript.org/manifests/olivine-labs/busted-1.11.1-1.rockspec + - sudo luarocks install --server=http://rocks.moonscript.org https://rocks.moonscript.org/manifests/olivine-labs/busted-1.11.1-1.rockspec - sudo luarocks make script: busted From b7de66f45a9fd3227f1f37f615754dac86e835c1 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 29 Jul 2014 23:14:54 -0700 Subject: [PATCH 068/344] travis fix pt. 2 --- .travis.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 114ffca2..346cc94e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,9 @@ language: c install: - sudo apt-get install luarocks - - sudo luarocks install --server=http://rocks.moonscript.org https://rocks.moonscript.org/manifests/olivine-labs/busted-1.11.1-1.rockspec + - mkdir -p ~/.luarocks + - echo 'rocks_servers = { "http://rocks.moonscript.org" }' > ~/.luarocks/config.lua + - sudo luarocks install https://rocks.moonscript.org/manifests/olivine-labs/busted-1.11.1-1.rockspec - sudo luarocks make script: busted From a6e66737ddf5478f4010f857dfe922106e346cf5 Mon Sep 17 00:00:00 2001 From: Jason Tu Date: Sun, 17 Aug 2014 22:29:30 -0400 Subject: [PATCH 069/344] Fix for moonc -w --- bin/moonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index 3b276aad..4dfd2358 100755 --- a/bin/moonc +++ b/bin/moonc @@ -165,7 +165,7 @@ end local function get_sleep_func() local sleep if not pcall(function() - require "socket" + local socket = require "socket" sleep = socket.sleep end) then -- This is set by moonc.c in windows binaries From da6c76b7e436cb2b2f4e44db2022665262bc5d02 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 2 Sep 2014 20:31:08 -0700 Subject: [PATCH 070/344] simplify single name destructures/imports --- moonscript/transform/destructure.lua | 10 +++++++-- moonscript/transform/destructure.moon | 8 +++++-- spec/outputs/class.lua | 5 +---- spec/outputs/destructure.lua | 30 ++++++--------------------- spec/outputs/import.lua | 10 ++------- 5 files changed, 23 insertions(+), 40 deletions(-) diff --git a/moonscript/transform/destructure.lua b/moonscript/transform/destructure.lua index f7a5166c..38e9bc55 100644 --- a/moonscript/transform/destructure.lua +++ b/moonscript/transform/destructure.lua @@ -124,7 +124,7 @@ build_assign = function(scope, destruct_literal, receiver) values } local obj - if scope:is_local(receiver) then + if scope:is_local(receiver) or #extracted_names == 1 then obj = receiver else do @@ -143,7 +143,13 @@ build_assign = function(scope, destruct_literal, receiver) for _index_0 = 1, #extracted_names do local tuple = extracted_names[_index_0] insert(names, tuple[1]) - insert(values, NameProxy.chain(obj, unpack(tuple[2]))) + local chain + if obj then + chain = NameProxy.chain(obj, unpack(tuple[2])) + else + chain = "nil" + end + insert(values, chain) end return build.group({ { diff --git a/moonscript/transform/destructure.moon b/moonscript/transform/destructure.moon index 1547ccfc..ca80bc25 100644 --- a/moonscript/transform/destructure.moon +++ b/moonscript/transform/destructure.moon @@ -59,7 +59,7 @@ build_assign = (scope, destruct_literal, receiver) -> inner = {"assign", names, values} - obj = if scope\is_local receiver + obj = if scope\is_local(receiver) or #extracted_names == 1 receiver else with obj = NameProxy "obj" @@ -70,7 +70,11 @@ build_assign = (scope, destruct_literal, receiver) -> for tuple in *extracted_names insert names, tuple[1] - insert values, NameProxy.chain obj, unpack tuple[2] + chain = if obj + NameProxy.chain obj, unpack tuple[2] + else + "nil" + insert values, chain build.group { {"declare", names} diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index 67227911..35c7153d 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -687,10 +687,7 @@ do _base_0.__class = _class_0 local self = _class_0 val = 23 - do - local _obj_0 = table - insert = _obj_0.insert - end + insert = table.insert Something = _class_0 end do diff --git a/spec/outputs/destructure.lua b/spec/outputs/destructure.lua index 28315438..8254e3c9 100644 --- a/spec/outputs/destructure.lua +++ b/spec/outputs/destructure.lua @@ -26,27 +26,15 @@ do local _obj_0 = yeah a, b, c, d = _obj_0.a, _obj_0.b, _obj_0.c, _obj_0.d end - do - local _obj_0 = one - a = _obj_0[1] - end + a = one[1] local _ = two - do - local _obj_0 = one - b = _obj_0[1] - end + b = one[1] c = nil - do - local _obj_0 = one - d = _obj_0[1] - end + d = one[1] local e = two local x = one local y - do - local _obj_0 = two - y = _obj_0[1] - end + y = two[1] local xx, yy = 1, 2 do local _obj_0 = { @@ -77,18 +65,12 @@ do name, street, city = futurists.poet.name, futurists.poet.address[1], futurists.poet.address[2] end do - do - local _obj_0 = x - self.world = _obj_0[1] - end + self.world = x[1] do local _obj_0 = x a.b, c.y, func().z = _obj_0[1], _obj_0[2], _obj_0[3] end - do - local _obj_0 = x - self.world = _obj_0.world - end + self.world = x.world end do local thing = { diff --git a/spec/outputs/import.lua b/spec/outputs/import.lua index f7467f4f..4c6520eb 100644 --- a/spec/outputs/import.lua +++ b/spec/outputs/import.lua @@ -1,8 +1,5 @@ local hello -do - local _obj_0 = yeah - hello = _obj_0.hello -end +hello = yeah.hello local world do local _obj_0 = table["cool"] @@ -34,10 +31,7 @@ local yumm a, yumm = 3434, "hello" local _table_0 = 232 local something -do - local _obj_0 = a(table) - something = _obj_0.something -end +something = a(table).something if indent then local okay, well do From 163748b8c91f2df226aa8cb81f91c20e1fa1fc62 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 2 Sep 2014 20:32:52 -0700 Subject: [PATCH 071/344] rebuild --- Makefile | 2 +- moonscript/cmd/lint.lua | 15 +++------------ moonscript/cmd/moonc.lua | 10 ++-------- moonscript/compile.lua | 5 +---- moonscript/compile/statement.lua | 5 +---- moonscript/compile/value.lua | 10 ++-------- moonscript/transform.lua | 5 +---- moonscript/transform/destructure.lua | 20 ++++---------------- moonscript/transform/names.lua | 10 ++-------- moonscript/types.lua | 10 ++-------- moonscript/util.lua | 5 +---- 11 files changed, 20 insertions(+), 77 deletions(-) diff --git a/Makefile b/Makefile index 45b90f8a..88085f78 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ global: sudo luarocks make moonscript-dev-1.rockspec compile:: - bin/moonc moon/ moonscript/ + lua5.1 bin/moonc moon/ moonscript/ compile_global: diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index a8c08903..6dfa4c82 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -1,18 +1,9 @@ local insert -do - local _obj_0 = table - insert = _obj_0.insert -end +insert = table.insert local Set -do - local _obj_0 = require("moonscript.data") - Set = _obj_0.Set -end +Set = require("moonscript.data").Set local Block -do - local _obj_0 = require("moonscript.compile") - Block = _obj_0.Block -end +Block = require("moonscript.compile").Block local default_whitelist = Set({ '_G', '_VERSION', diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index dc53cd52..b64c9f1c 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -1,9 +1,6 @@ local lfs = require("lfs") local split -do - local _obj_0 = require("moonscript.util") - split = _obj_0.split -end +split = require("moonscript.util").split local dirsep, dirsep_chars, mkdir, normalize_dir, parse_dir, parse_file, convert_path, format_time, gettime, compile_file_text, write_file, compile_and_write, is_abs_path, path_to_target dirsep = package.config:sub(1, 1) if dirsep == "\\" then @@ -93,10 +90,7 @@ compile_file_text = function(text, fname, opts) end if opts.show_posmap then local debug_posmap - do - local _obj_0 = require("moonscript.util") - debug_posmap = _obj_0.debug_posmap - end + debug_posmap = require("moonscript.util").debug_posmap print("Pos", "Lua", ">>", "Moon") print(debug_posmap(posmap_or_err, text, code)) return true diff --git a/moonscript/compile.lua b/moonscript/compile.lua index aaa42c29..52f39815 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -7,10 +7,7 @@ do NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName end local Set -do - local _obj_0 = require("moonscript.data") - Set = _obj_0.Set -end +Set = require("moonscript.data").Set local ntype, has_value do local _obj_0 = require("moonscript.types") diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index 2d2a80c8..2026a88f 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -2,10 +2,7 @@ local util = require("moonscript.util") local reversed, unpack reversed, unpack = util.reversed, util.unpack local ntype -do - local _obj_0 = require("moonscript.types") - ntype = _obj_0.ntype -end +ntype = require("moonscript.types").ntype local concat, insert do local _obj_0 = table diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 560352ba..8f8e8f23 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -1,15 +1,9 @@ local util = require("moonscript.util") local data = require("moonscript.data") local ntype -do - local _obj_0 = require("moonscript.types") - ntype = _obj_0.ntype -end +ntype = require("moonscript.types").ntype local user_error -do - local _obj_0 = require("moonscript.errors") - user_error = _obj_0.user_error -end +user_error = require("moonscript.errors").user_error local concat, insert do local _obj_0 = table diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 803d5d70..c2499296 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -6,10 +6,7 @@ reversed, unpack = util.reversed, util.unpack local ntype, mtype, build, smart_node, is_slice, value_is_singular ntype, mtype, build, smart_node, is_slice, value_is_singular = types.ntype, types.mtype, types.build, types.smart_node, types.is_slice, types.value_is_singular local insert -do - local _obj_0 = table - insert = _obj_0.insert -end +insert = table.insert local NameProxy, LocalName do local _obj_0 = require("moonscript.transform.names") diff --git a/moonscript/transform/destructure.lua b/moonscript/transform/destructure.lua index 38e9bc55..c0542953 100644 --- a/moonscript/transform/destructure.lua +++ b/moonscript/transform/destructure.lua @@ -4,25 +4,13 @@ do ntype, mtype, build = _obj_0.ntype, _obj_0.mtype, _obj_0.build end local NameProxy -do - local _obj_0 = require("moonscript.transform.names") - NameProxy = _obj_0.NameProxy -end +NameProxy = require("moonscript.transform.names").NameProxy local insert -do - local _obj_0 = table - insert = _obj_0.insert -end +insert = table.insert local unpack -do - local _obj_0 = require("moonscript.util") - unpack = _obj_0.unpack -end +unpack = require("moonscript.util").unpack local user_error -do - local _obj_0 = require("moonscript.errors") - user_error = _obj_0.user_error -end +user_error = require("moonscript.errors").user_error local join join = function(...) do diff --git a/moonscript/transform/names.lua b/moonscript/transform/names.lua index a7584da1..c74a3773 100644 --- a/moonscript/transform/names.lua +++ b/moonscript/transform/names.lua @@ -1,13 +1,7 @@ local build -do - local _obj_0 = require("moonscript.types") - build = _obj_0.build -end +build = require("moonscript.types").build local unpack -do - local _obj_0 = require("moonscript.util") - unpack = _obj_0.unpack -end +unpack = require("moonscript.util").unpack local LocalName do local _base_0 = { diff --git a/moonscript/types.lua b/moonscript/types.lua index aae50c91..ccf1d53a 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -1,14 +1,8 @@ local util = require("moonscript.util") local Set -do - local _obj_0 = require("moonscript.data") - Set = _obj_0.Set -end +Set = require("moonscript.data").Set local insert -do - local _obj_0 = table - insert = _obj_0.insert -end +insert = table.insert local unpack unpack = util.unpack local manual_return = Set({ diff --git a/moonscript/util.lua b/moonscript/util.lua index dfb43557..7eddae37 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -1,8 +1,5 @@ local concat -do - local _obj_0 = table - concat = _obj_0.concat -end +concat = table.concat local unpack = unpack or table.unpack local type = type local moon = { From bf8de1cf5eb7450b8363d173da9c986e66b43481 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 10:30:43 -0800 Subject: [PATCH 072/344] move extract assign name out, add statement compiles as param --- moonscript/compile.lua | 35 ++++++++++++++++++++--------------- moonscript/compile.moon | 39 +++++++++++++++++++++++---------------- 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 52f39815..5eab8e6b 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -256,6 +256,7 @@ do export_all = false, export_proper = false, value_compilers = value_compilers, + statement_compilers = statement_compilers, __tostring = function(self) local h if "string" == type(self.header) then @@ -288,6 +289,22 @@ do end end end, + extract_assign_name = function(self, node) + local is_local = false + local real_name + local _exp_0 = mtype(node) + if LocalName == _exp_0 then + is_local = true + real_name = node:get_name(self) + elseif NameProxy == _exp_0 then + real_name = node:get_name(self) + elseif "table" == _exp_0 then + real_name = node[1] == "ref" and node[2] + elseif "string" == _exp_0 then + real_name = node + end + return real_name, is_local + end, declare = function(self, names) local undeclared do @@ -297,19 +314,7 @@ do local _continue_0 = false repeat local name = names[_index_0] - local is_local = false - local real_name - local _exp_0 = mtype(name) - if LocalName == _exp_0 then - is_local = true - real_name = name:get_name(self) - elseif NameProxy == _exp_0 then - real_name = name:get_name(self) - elseif "table" == _exp_0 then - real_name = name[1] == "ref" and name[2] - elseif "string" == _exp_0 then - real_name = name - end + local real_name, is_local = self:extract_assign_name(name) if not (is_local or real_name and not self:has_name(real_name, true)) then _continue_0 = true break @@ -450,7 +455,7 @@ do end end, is_stm = function(self, node) - return statement_compilers[ntype(node)] ~= nil + return self.statement_compilers[ntype(node)] ~= nil end, is_value = function(self, node) local t = ntype(node) @@ -516,7 +521,7 @@ do node = self.transform.statement(node) local result do - local fn = statement_compilers[ntype(node)] + local fn = self.statement_compilers[ntype(node)] if fn then result = fn(self, node, ...) else diff --git a/moonscript/compile.moon b/moonscript/compile.moon index af700e10..d05a3bb7 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -162,6 +162,7 @@ class Block export_proper: false value_compilers: value_compilers + statement_compilers: statement_compilers __tostring: => h = if "string" == type @header @@ -211,22 +212,28 @@ class Block if fn = @_listeners[name] fn self, ... + extract_assign_name: (node) => + is_local = false + real_name = switch mtype node + when LocalName + is_local = true + node\get_name self + when NameProxy + node\get_name self + when "table" + node[1] == "ref" and node[2] + when "string" + -- TOOD: some legacy transfomers might use string for ref + node + + real_name, is_local + declare: (names) => undeclared = for name in *names - is_local = false - real_name = switch mtype name - when LocalName - is_local = true - name\get_name self - when NameProxy then name\get_name self - when "table" - name[1] == "ref" and name[2] - when "string" - -- TODO: don't use string literal as ref - name - + real_name, is_local = @extract_assign_name name continue unless is_local or real_name and not @has_name real_name, true - -- put exported names so they can be assigned to in deeper scope + -- this also puts exported names so they can be assigned a new value in + -- deeper scope @put_name real_name continue if @name_exported real_name real_name @@ -322,7 +329,7 @@ class Block \append ... is_stm: (node) => - statement_compilers[ntype node] != nil + @statement_compilers[ntype node] != nil is_value: (node) => t = ntype node @@ -369,8 +376,8 @@ class Block return if not node -- skip blank statements node = @transform.statement node - result = if fn = statement_compilers[ntype(node)] - fn self, node, ... + result = if fn = @statement_compilers[ntype(node)] + fn @, node, ... else -- coerce value into statement if has_value node From 65f177f5c5a43149a00feb34363e093ba592a765 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 10:41:13 -0800 Subject: [PATCH 073/344] remove pattern matcher because it no longer works with latest busted --- spec/lang_spec.moon | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/spec/lang_spec.moon b/spec/lang_spec.moon index 7fd1a3f8..f6612dd8 100644 --- a/spec/lang_spec.moon +++ b/spec/lang_spec.moon @@ -90,12 +90,7 @@ describe "input tests", -> for name in *inputs input = input_fname name - fn = if pattern and not input\match pattern - pending - else - it - - fn input .. " #input", -> + it input .. " #input", -> file_str = read_all input_fname name parse_time, tree, err = benchmark -> parse.string file_str From f70e4629fe3fe84697925651eac5aac37be83478 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:05:21 -0800 Subject: [PATCH 074/344] unused variable linter --- moonscript/cmd/lint.lua | 56 +++++++++++++++++++++++++++++++++++++++- moonscript/cmd/lint.moon | 44 ++++++++++++++++++++++++++++++- 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 6dfa4c82..004c2687 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -4,6 +4,8 @@ local Set Set = require("moonscript.data").Set local Block Block = require("moonscript.compile").Block +local mtype +mtype = require("moonscript.util").moon.type local default_whitelist = Set({ '_G', '_VERSION', @@ -52,11 +54,30 @@ local LinterBlock do local _parent_0 = Block local _base_0 = { + lint_check_unused = function(self) + for name, pos in pairs(self.lint_unused_names) do + insert(self:get_root_block().lint_errors, { + "assigned but unused `" .. tostring(name) .. "`", + pos + }) + end + end, + render = function(self, ...) + self:lint_check_unused() + return _parent_0.render(self, ...) + end, block = function(self, ...) + self.get_root_block = self.get_root_block or function() + return self + end do local _with_0 = _parent_0.block(self, ...) _with_0.block = self.block + _with_0.render = self.render + _with_0.get_root_block = self.get_root_block + _with_0.lint_check_unused = self.lint_check_unused _with_0.value_compilers = self.value_compilers + _with_0.statement_compilers = self.statement_compilers return _with_0 end end @@ -76,15 +97,47 @@ do local name = val[2] if not (block:has_name(name) or whitelist_globals[name] or name:match("%.")) then insert(self.lint_errors, { - "accessing global " .. tostring(name), + "accessing global `" .. tostring(name) .. "`", val[-1] }) end + do + local unused = block.lint_unused_names + if unused then + unused[name] = nil + end + end return vc.ref(block, val) end }, { __index = vc }) + local sc = self.statement_compilers + self.statement_compilers = setmetatable({ + assign = function(block, node) + local _, names, values = unpack(node) + for _index_0 = 1, #names do + local _continue_0 = false + repeat + local name = names[_index_0] + local real_name, is_local = block:extract_assign_name(name) + if not (is_local or real_name and not block:has_name(real_name, true)) then + _continue_0 = true + break + end + block.lint_unused_names = block.lint_unused_names or { } + block.lint_unused_names[real_name] = node[-1] or true + _continue_0 = true + until true + if not _continue_0 then + break + end + end + return sc.assign(block, node) + end + }, { + __index = sc + }) end, __base = _base_0, __name = "LinterBlock", @@ -189,6 +242,7 @@ lint_code = function(code, name, whitelist_globals) end local scope = LinterBlock(whitelist_globals) scope:stms(tree) + scope:lint_check_unused() return format_lint(scope.lint_errors, code, name) end local lint_file diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 4add665b..180429cc 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -3,6 +3,8 @@ import insert from table import Set from require "moonscript.data" import Block from require "moonscript.compile" +{type: mtype} = require("moonscript.util").moon + -- globals allowed to be referenced default_whitelist = Set { '_G' @@ -61,17 +63,55 @@ class LinterBlock extends Block name = val[2] unless block\has_name(name) or whitelist_globals[name] or name\match "%." insert @lint_errors, { - "accessing global #{name}" + "accessing global `#{name}`" val[-1] } + if unused = block.lint_unused_names + unused[name] = nil + vc.ref block, val }, __index: vc + sc = @statement_compilers + @statement_compilers = setmetatable { + assign: (block, node) -> + _, names, values = unpack node + -- extract the names to be declared + for name in *names + real_name, is_local = block\extract_assign_name name + -- already defined in some other scope + unless is_local or real_name and not block\has_name real_name, true + continue + + block.lint_unused_names or= {} + block.lint_unused_names[real_name] = node[-1] or true + + sc.assign block, node + }, __index: sc + + + lint_check_unused: => + for name, pos in pairs @lint_unused_names + insert @get_root_block!.lint_errors, { + "assigned but unused `#{name}`" + pos + } + + render: (...) => + @lint_check_unused! + super ... + block: (...) => + @get_root_block or= -> @ + with super ... .block = @block + .render = @render + .get_root_block = @get_root_block + .lint_check_unused = @lint_check_unused .value_compilers = @value_compilers + .statement_compilers = @statement_compilers format_lint = (errors, code, header) -> return unless next errors @@ -127,6 +167,8 @@ lint_code = (code, name="string input", whitelist_globals) -> scope = LinterBlock whitelist_globals scope\stms tree + scope\lint_check_unused! + format_lint scope.lint_errors, code, name lint_file = (fname) -> From 9f7aae7827b4d8ff6dccba87a6e50404f5278d2d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:06:49 -0800 Subject: [PATCH 075/344] clean up makefile --- Makefile | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 88085f78..4781fa3a 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,6 @@ +.PHONY: test local compile watch lint -test:: +test: busted -p "_spec.moon$$" local: compile @@ -8,12 +9,11 @@ local: compile global: sudo luarocks make moonscript-dev-1.rockspec -compile:: +compile: lua5.1 bin/moonc moon/ moonscript/ - -compile_global: - moonc moon/ moonscript/ - watch: moonc moon/ moonscript/ && moonc -w moon/ moonscript/ + +lint: + moonc -l moonscript moon From d19969c2d917d36651993b0a18a763ee4ed5574b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:13:33 -0800 Subject: [PATCH 076/344] don't include autogenerated names and _ in unused linter --- moonscript/cmd/lint.lua | 13 ++++++++++++- moonscript/cmd/lint.moon | 9 ++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 004c2687..af37c201 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -55,6 +55,9 @@ do local _parent_0 = Block local _base_0 = { lint_check_unused = function(self) + if not (self.lint_unused_names) then + return + end for name, pos in pairs(self.lint_unused_names) do insert(self:get_root_block().lint_errors, { "assigned but unused `" .. tostring(name) .. "`", @@ -120,13 +123,21 @@ do local _continue_0 = false repeat local name = names[_index_0] + if type(name) == "table" and name[1] == "temp_name" then + _continue_0 = true + break + end local real_name, is_local = block:extract_assign_name(name) if not (is_local or real_name and not block:has_name(real_name, true)) then _continue_0 = true break end + if real_name == "_" then + _continue_0 = true + break + end block.lint_unused_names = block.lint_unused_names or { } - block.lint_unused_names[real_name] = node[-1] or true + block.lint_unused_names[real_name] = node[-1] or 0 _continue_0 = true until true if not _continue_0 then diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 180429cc..55f93254 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -79,19 +79,26 @@ class LinterBlock extends Block _, names, values = unpack node -- extract the names to be declared for name in *names + -- don't include autogenerated names + if type(name) == "table" and name[1] == "temp_name" + continue + real_name, is_local = block\extract_assign_name name -- already defined in some other scope unless is_local or real_name and not block\has_name real_name, true continue + continue if real_name == "_" + block.lint_unused_names or= {} - block.lint_unused_names[real_name] = node[-1] or true + block.lint_unused_names[real_name] = node[-1] or 0 sc.assign block, node }, __index: sc lint_check_unused: => + return unless @lint_unused_names for name, pos in pairs @lint_unused_names insert @get_root_block!.lint_errors, { "assigned but unused `#{name}`" From 182969dc81591b29095909e8f0c9bbcf240654c9 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:18:21 -0800 Subject: [PATCH 077/344] sort and group unused names --- moonscript/cmd/lint.lua | 38 +++++++++++++++++++++++++++++++++++++- moonscript/cmd/lint.moon | 12 +++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index af37c201..b0f607da 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -58,9 +58,42 @@ do if not (self.lint_unused_names) then return end + local names_by_position = { } for name, pos in pairs(self.lint_unused_names) do + names_by_position[pos] = names_by_position[pos] or { } + insert(names_by_position[pos], name) + end + local tuples + do + local _accum_0 = { } + local _len_0 = 1 + for pos, names in pairs(names_by_position) do + _accum_0[_len_0] = { + pos, + names + } + _len_0 = _len_0 + 1 + end + tuples = _accum_0 + end + table.sort(tuples, function(a, b) + return a[1] < b[1] + end) + for _index_0 = 1, #tuples do + local _des_0 = tuples[_index_0] + local pos, names + pos, names = _des_0[1], _des_0[2] insert(self:get_root_block().lint_errors, { - "assigned but unused `" .. tostring(name) .. "`", + "assigned but unused " .. tostring(table.concat((function() + local _accum_0 = { } + local _len_0 = 1 + for _index_1 = 1, #names do + local n = names[_index_1] + _accum_0[_len_0] = "`" .. tostring(n) .. "`" + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), ", ")), pos }) end @@ -69,6 +102,9 @@ do self:lint_check_unused() return _parent_0.render(self, ...) end, + get_root_block = function(self) + return self + end, block = function(self, ...) self.get_root_block = self.get_root_block or function() return self diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 55f93254..361f15fc 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -99,9 +99,17 @@ class LinterBlock extends Block lint_check_unused: => return unless @lint_unused_names + names_by_position = {} for name, pos in pairs @lint_unused_names + names_by_position[pos] or= {} + insert names_by_position[pos], name + + tuples = [{pos, names} for pos,names in pairs names_by_position] + table.sort tuples, (a,b) -> a[1] < b[1] + + for {pos, names} in *tuples insert @get_root_block!.lint_errors, { - "assigned but unused `#{name}`" + "assigned but unused #{table.concat ["`#{n}`" for n in *names], ", "}" pos } @@ -109,6 +117,8 @@ class LinterBlock extends Block @lint_check_unused! super ... + get_root_block: => @ + block: (...) => @get_root_block or= -> @ From b1ecc7defc6cdac3cf6e75632625944977f02d7e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:22:24 -0800 Subject: [PATCH 078/344] fix get root block for root block --- moonscript/cmd/lint.lua | 11 ++++------- moonscript/cmd/lint.moon | 8 ++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index b0f607da..d4573f72 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -55,7 +55,7 @@ do local _parent_0 = Block local _base_0 = { lint_check_unused = function(self) - if not (self.lint_unused_names) then + if not (self.lint_unused_names and next(self.lint_unused_names)) then return end local names_by_position = { } @@ -102,13 +102,7 @@ do self:lint_check_unused() return _parent_0.render(self, ...) end, - get_root_block = function(self) - return self - end, block = function(self, ...) - self.get_root_block = self.get_root_block or function() - return self - end do local _with_0 = _parent_0.block(self, ...) _with_0.block = self.block @@ -129,6 +123,9 @@ do whitelist_globals = default_whitelist end _parent_0.__init(self, ...) + self.get_root_block = function() + return self + end self.lint_errors = { } local vc = self.value_compilers self.value_compilers = setmetatable({ diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 361f15fc..922e04fa 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -55,6 +55,8 @@ default_whitelist = Set { class LinterBlock extends Block new: (whitelist_globals=default_whitelist, ...) => super ... + @get_root_block = -> @ + @lint_errors = {} vc = @value_compilers @@ -98,7 +100,8 @@ class LinterBlock extends Block lint_check_unused: => - return unless @lint_unused_names + return unless @lint_unused_names and next @lint_unused_names + names_by_position = {} for name, pos in pairs @lint_unused_names names_by_position[pos] or= {} @@ -117,10 +120,7 @@ class LinterBlock extends Block @lint_check_unused! super ... - get_root_block: => @ - block: (...) => - @get_root_block or= -> @ with super ... .block = @block From a79eb19f9e4fcf6a0e9a9efc3e3eab4e521ab443 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:26:37 -0800 Subject: [PATCH 079/344] mark used up the block stack --- moonscript/cmd/lint.lua | 32 ++++++++++++++++++++++++-------- moonscript/cmd/lint.moon | 13 ++++++++++--- 2 files changed, 34 insertions(+), 11 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index d4573f72..1fd80b05 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -54,14 +54,34 @@ local LinterBlock do local _parent_0 = Block local _base_0 = { + lint_mark_used = function(self, name) + if self.lint_unused_names and self.lint_unused_names[name] then + self.lint_unused_names[name] = false + return + end + if self.parent then + return self.parent:lint_mark_used(name) + end + end, lint_check_unused = function(self) if not (self.lint_unused_names and next(self.lint_unused_names)) then return end local names_by_position = { } for name, pos in pairs(self.lint_unused_names) do - names_by_position[pos] = names_by_position[pos] or { } - insert(names_by_position[pos], name) + local _continue_0 = false + repeat + if not (pos) then + _continue_0 = true + break + end + names_by_position[pos] = names_by_position[pos] or { } + insert(names_by_position[pos], name) + _continue_0 = true + until true + if not _continue_0 then + break + end end local tuples do @@ -109,6 +129,7 @@ do _with_0.render = self.render _with_0.get_root_block = self.get_root_block _with_0.lint_check_unused = self.lint_check_unused + _with_0.lint_mark_used = self.lint_mark_used _with_0.value_compilers = self.value_compilers _with_0.statement_compilers = self.statement_compilers return _with_0 @@ -137,12 +158,7 @@ do val[-1] }) end - do - local unused = block.lint_unused_names - if unused then - unused[name] = nil - end - end + block:lint_mark_used(name) return vc.ref(block, val) end }, { diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 922e04fa..8317e5c5 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -69,9 +69,7 @@ class LinterBlock extends Block val[-1] } - if unused = block.lint_unused_names - unused[name] = nil - + block\lint_mark_used name vc.ref block, val }, __index: vc @@ -98,12 +96,20 @@ class LinterBlock extends Block sc.assign block, node }, __index: sc + lint_mark_used: (name) => + if @lint_unused_names and @lint_unused_names[name] + @lint_unused_names[name] = false + return + + if @parent + @parent\lint_mark_used name lint_check_unused: => return unless @lint_unused_names and next @lint_unused_names names_by_position = {} for name, pos in pairs @lint_unused_names + continue unless pos names_by_position[pos] or= {} insert names_by_position[pos], name @@ -127,6 +133,7 @@ class LinterBlock extends Block .render = @render .get_root_block = @get_root_block .lint_check_unused = @lint_check_unused + .lint_mark_used = @lint_mark_used .value_compilers = @value_compilers .statement_compilers = @statement_compilers From 2d5dd107d47e3d86885a87ac3158d23ab9be48fb Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:47:58 -0800 Subject: [PATCH 080/344] fix opts passing to cmd.moonc --- bin/moonc | 7 ++++++- moonscript/cmd/moonc.lua | 4 ++-- moonscript/cmd/moonc.moon | 4 ++-- spec/cmd_spec.moon | 2 +- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/bin/moonc b/bin/moonc index 4dfd2358..bc8ed20b 100755 --- a/bin/moonc +++ b/bin/moonc @@ -315,8 +315,13 @@ else end local success, err = compile_and_write(fname, target, { - print = opts.p + print = opts.p, + fname = fname, + benchmark = opts.b, + show_posmap = opts.X, + show_parse_tree = opts.T, }) + if not success then io.stderr:write(fname .. "\t" .. err .. "\n") os.exit(1) diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index b64c9f1c..c21cbdd8 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -55,7 +55,7 @@ do end end end -compile_file_text = function(text, fname, opts) +compile_file_text = function(text, opts) if opts == nil then opts = { } end @@ -97,7 +97,7 @@ compile_file_text = function(text, fname, opts) end if opts.benchmark then print(table.concat({ - fname, + opts.fname or "stdin", "Parse time \t" .. format_time(parse_time), "Compile time\t" .. format_time(compile_time), "" diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon index 6b8b206b..ee81eb20 100644 --- a/moonscript/cmd/moonc.moon +++ b/moonscript/cmd/moonc.moon @@ -63,7 +63,7 @@ gettime = do -- compiles file to lua, returns lua code -- returns nil, error on error -- returns true if some option handled the output instead -compile_file_text = (text, fname, opts={}) -> +compile_file_text = (text, opts={}) -> parse = require "moonscript.parse" compile = require "moonscript.compile" @@ -100,7 +100,7 @@ compile_file_text = (text, fname, opts={}) -> if opts.benchmark print table.concat { - fname, + opts.fname or "stdin", "Parse time \t" .. format_time(parse_time), "Compile time\t" .. format_time(compile_time), "" diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 3ac5dea0..84012e31 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -55,7 +55,7 @@ describe "moonc", -> assert.same { [[return print('hello')]] }, { - moonc.compile_file_text "print'hello'", "test.moon" + moonc.compile_file_text "print'hello'", fname: "test.moon" } describe "stubbed lfs", -> From 09a2a5c54588dbfa3454509023c5023517a8611a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:52:14 -0800 Subject: [PATCH 081/344] mark position of table self assigns to not confuse linter --- moonscript/cmd/lint.lua | 2 +- moonscript/cmd/lint.moon | 2 +- moonscript/parse.lua | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 1fd80b05..52dba4e0 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -167,7 +167,7 @@ do local sc = self.statement_compilers self.statement_compilers = setmetatable({ assign = function(block, node) - local _, names, values = unpack(node) + local names = node[2] for _index_0 = 1, #names do local _continue_0 = false repeat diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 8317e5c5..103c25ed 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -76,7 +76,7 @@ class LinterBlock extends Block sc = @statement_compilers @statement_compilers = setmetatable { assign: (block, node) -> - _, names, values = unpack node + names = node[2] -- extract the names to be declared for name in *names -- don't include autogenerated names diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 63458592..b26562d7 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -277,8 +277,8 @@ local function check_lua_string(str, pos, right, left) end -- :name in table literal -local function self_assign(name) - return {{"key_literal", name}, name} +local function self_assign(name, pos) + return {{"key_literal", name}, {"ref", name, [-1] = pos}} end local err_msg = "Failed to parse:%s\n [%d] >> %s" @@ -552,7 +552,7 @@ local build_grammar = wrap_env(function() op"*" + op"^" + Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export", - KeyValue = (sym":" * -SomeSpace * Name) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock)), + KeyValue = (sym":" * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock)), KeyValueList = KeyValue * (sym"," * KeyValue)^0, KeyValueLine = CheckIndent * KeyValueList * sym","^-1, From 9a3504afc267fd709e24d003875eba769327a10d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 11:53:47 -0800 Subject: [PATCH 082/344] lint --- moonscript/cmd/moonc.lua | 1 - moonscript/cmd/moonc.moon | 1 - 2 files changed, 2 deletions(-) diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index c21cbdd8..eae86380 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -182,7 +182,6 @@ return { normalize_dir = normalize_dir, parse_dir = parse_dir, parse_file = parse_file, - new_path = new_path, convert_path = convert_path, gettime = gettime, format_time = format_time, diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon index ee81eb20..a680eaa7 100644 --- a/moonscript/cmd/moonc.moon +++ b/moonscript/cmd/moonc.moon @@ -182,7 +182,6 @@ path_to_target = (path, target_dir=nil, base_dir=nil) -> :normalize_dir :parse_dir :parse_file - :new_path :convert_path :gettime :format_time From 64ac22aa7b545b88ec366594b7142d33c6a00385 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 12:42:36 -0800 Subject: [PATCH 083/344] docs for new lint functionality --- docs/command_line.md | 46 ++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/docs/command_line.md b/docs/command_line.md index a68e3485..aac3e7bd 100644 --- a/docs/command_line.md +++ b/docs/command_line.md @@ -131,9 +131,9 @@ A full list of flags can be seen by passing the `-h` or `--help` flag. ### Linter -`moonc` contains a [lint][1] tool -for statically detecting potential problems with code. The current linter only -has one test: it detects the use of global variables. +`moonc` contains a [lint][1] tool for statically detecting potential problems +with code. The linter has two tests: detects accessed global variables, +detect unused declared variables. You can execute the linter with the `-l` flag. When the linting flag is provided only linting takes place and no compiled code is generated. @@ -184,7 +184,7 @@ Outputs: ./lint_example.moon - line 7: accessing global my_nmuber + line 7: accessing global `my_nmuber` ================================== > my_nmuber + 10 @@ -245,6 +245,44 @@ that are allowed. Multiple patterns in `whitelist_globals` can match a single file, the union of the allowed globals will be used when linting that file. +#### Unused Variable Assigns + +Sometimes when debugging, refactoring, or just developing, you might leave +behind stray assignments that aren't actually necessary for the execution of +your code. It's good practice to clean them up to avoid any potential confusion +they might cause. + +The unused assignment detector keeps track of any variables that are assigned, +and if they aren't accessed in within their available scope, they are reported +as an error. + +Given the following code: + +```moononly +a, b = 1, 2 +print "hello", a +``` + +The linter will identify the problem: + + ./lint_example.moon + + line 1: assigned but unused `b` + =============================== + > a, b = 1, 2 + + +Sometimes you need a name to assign to even though you know it will never be +accessed. The linter will treat `_` as a special name that's allowed to be +written to but never accessed: + +The following code would not produce any lint errors: + +```moononly +item = {123, "shoe", "brown", 123} +_, name, _, count = unpack item +print name, count +``` [1]: http://en.wikipedia.org/wiki/Lint_(software) From 72615aa6e226d3acfc9a24e6a7acb860cff62542 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 12:47:21 -0800 Subject: [PATCH 084/344] date and doc misc updates --- README.md | 5 +++-- docs/reference.md | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 31d2f52d..5b2b555c 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,8 @@ MoonScript is a programmer friendly language that compiles into [Lua](http://www.lua.org/). It gives you the power of the fastest scripting -language combined with a rich set of features. It runs on Lua 5.1 and 5.2. +language combined with a rich set of features. It runs on Lua 5.1 and above, +including alternative runtimes like LuaJIT. See . @@ -25,7 +26,7 @@ busted ## License (MIT) -Copyright (C) 2013 by Leaf Corcoran +Copyright (C) 2015 by Leaf Corcoran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/docs/reference.md b/docs/reference.md index a67fd332..3a8e9872 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1558,7 +1558,7 @@ their written order you can add `local *` to the top of your file. # License (MIT) - Copyright (C) 2014 by Leaf Corcoran + Copyright (C) 2015 by Leaf Corcoran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From 8ea934964aecbeba9343129861386daa91d1ee22 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 13:26:29 -0800 Subject: [PATCH 085/344] allow whitespace/newlines after binary operator --- moonscript/parse.lua | 10 +++------- spec/inputs/operators.moon | 33 +++++++++++++++++++++++++++++++++ spec/outputs/operators.lua | 13 +++++++++++++ 3 files changed, 49 insertions(+), 7 deletions(-) create mode 100644 spec/inputs/operators.moon create mode 100644 spec/outputs/operators.lua diff --git a/moonscript/parse.lua b/moonscript/parse.lua index b26562d7..726dda71 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -436,16 +436,12 @@ local build_grammar = wrap_env(function() Assign = sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign", Update = ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update", - -- we can ignore precedence for now + -- we don't need to parse precedence, lua will handle it for us OtherOps = op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + op"<" + op">", + BinaryOperator = (OtherOps + FactorOp + TermOp) * SpaceBreak^0, Assignable = Cmt(DotChain + Chain, check_assignable) + Name + SelfName, - - Exp = Ct(Value * ((OtherOps + FactorOp + TermOp) * Value)^0) / flatten_or_mark"exp", - - -- Exp = Ct(Factor * (OtherOps * Factor)^0) / flatten_or_mark"exp", - -- Factor = Ct(Term * (FactorOp * Term)^0) / flatten_or_mark"exp", - -- Term = Ct(Value * (TermOp * Value)^0) / flatten_or_mark"exp", + Exp = Ct(Value * (BinaryOperator * Value)^0) / flatten_or_mark"exp", SimpleValue = If + Unless + diff --git a/spec/inputs/operators.moon b/spec/inputs/operators.moon new file mode 100644 index 00000000..742ad7f5 --- /dev/null +++ b/spec/inputs/operators.moon @@ -0,0 +1,33 @@ + +-- +x = 1 + 3 + +y = 1 + + 3 + +z = 1 + + 3 + + 4 + +-- + +k = b and c and + g + + +h = thing and + -> + print "hello world" + +-- TODO: should fail, indent still set to previous line so it thinks body is +-- indented +i = thing or + -> + print "hello world" + +p = thing and + -> +print "hello world" + +s = thing or + -> and 234 diff --git a/spec/outputs/operators.lua b/spec/outputs/operators.lua new file mode 100644 index 00000000..2c2a956c --- /dev/null +++ b/spec/outputs/operators.lua @@ -0,0 +1,13 @@ +local x = 1 + 3 +local y = 1 + 3 +local z = 1 + 3 + 4 +local k = b and c and g +local h = thing and function() + return print("hello world") +end +local i = thing or function() + return print("hello world") +end +local p = thing and function() end +print("hello world") +local s = thing or function() end and 234 \ No newline at end of file From 1982b565d86e1fdc2ba2a217c802389c01c170a2 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 13:36:52 -0800 Subject: [PATCH 086/344] refactor operators parsing --- moonscript/parse.lua | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 726dda71..e150c79e 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -59,9 +59,6 @@ local Num = P"0x" * R("09", "af", "AF")^1 * (S"uU"^-1 * S"lL"^2)^-1 + Num = Space * (Num / function(value) return {"number", value} end) -local FactorOp = Space * C(S"+-") -local TermOp = Space * C(S"*/%^") - local Shebang = P"#!" * P(1 - Stop)^0 -- can't have P(false) because it causes preceding patterns not to run @@ -436,9 +433,9 @@ local build_grammar = wrap_env(function() Assign = sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign", Update = ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update", - -- we don't need to parse precedence, lua will handle it for us - OtherOps = op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + op"<" + op">", - BinaryOperator = (OtherOps + FactorOp + TermOp) * SpaceBreak^0, + CharOperators = Space * C(S"+-*/%^><"), + WordOperators = op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op"..", + BinaryOperator = (WordOperators + CharOperators) * SpaceBreak^0, Assignable = Cmt(DotChain + Chain, check_assignable) + Name + SelfName, Exp = Ct(Value * (BinaryOperator * Value)^0) / flatten_or_mark"exp", From e61c2ba29f042d9b1a35b3e7ab736461c7f96bba Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 13:41:38 -0800 Subject: [PATCH 087/344] spacebreak operator tests in more places --- spec/inputs/operators.moon | 17 +++++++++++++++++ spec/outputs/operators.lua | 13 ++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/spec/inputs/operators.moon b/spec/inputs/operators.moon index 742ad7f5..d090865d 100644 --- a/spec/inputs/operators.moon +++ b/spec/inputs/operators.moon @@ -31,3 +31,20 @@ print "hello world" s = thing or -> and 234 + + +-- +u = { + color: 1 and 2 and + 3 + 4 + 4 +} + +v = { + color: 1 and + -> + "yeah" + "great" +} + diff --git a/spec/outputs/operators.lua b/spec/outputs/operators.lua index 2c2a956c..95a10751 100644 --- a/spec/outputs/operators.lua +++ b/spec/outputs/operators.lua @@ -10,4 +10,15 @@ local i = thing or function() end local p = thing and function() end print("hello world") -local s = thing or function() end and 234 \ No newline at end of file +local s = thing or function() end and 234 +local u = { + color = 1 and 2 and 3, + 4, + 4 +} +local v = { + color = 1 and function() + return "yeah" + end, + "great" +} \ No newline at end of file From 7109da7e1db12973a5c19fbfc6a2e4a08b4b1030 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 13:52:09 -0800 Subject: [PATCH 088/344] parens can take spacebreaks on the insides of either side --- moonscript/parse.lua | 5 +++-- spec/inputs/operators.moon | 24 +++++++++++++++++++++++- spec/outputs/operators.lua | 9 +++++++-- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index e150c79e..0da86c62 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -216,6 +216,7 @@ local function sym(chars) return Space * chars end +-- Like sym but doesn't accept whitespace in front local function symx(chars) return chars end @@ -482,9 +483,9 @@ local build_grammar = wrap_env(function() LuaStringClose = "]" * P"="^0 * "]", Callable = pos(Name / mark"ref") + SelfName + VarArg + Parens / mark"parens", - Parens = sym"(" * Exp * sym")", + Parens = sym"(" * SpaceBreak^0 * Exp * SpaceBreak^0 * sym")", - FnArgs = symx"(" * Ct(ExpList^-1) * sym")" + sym"!" * -P"=" * Ct"", + FnArgs = symx"(" * SpaceBreak^0 * Ct(ExpList^-1) * SpaceBreak^0 * sym")" + sym"!" * -P"=" * Ct"", ChainTail = ChainItem^1 * ColonSuffix^-1 + ColonSuffix, diff --git a/spec/inputs/operators.moon b/spec/inputs/operators.moon index d090865d..142ef622 100644 --- a/spec/inputs/operators.moon +++ b/spec/inputs/operators.moon @@ -1,5 +1,5 @@ --- +-- binary ops x = 1 + 3 y = 1 + @@ -46,5 +46,27 @@ v = { -> "yeah" "great" + oksy: 3 ^ +2 } +-- parens + +nno = ( + yeah + 2 ) + +nn = ( + yeah + 2 +) + +n = hello( + b +) -> + +hello a, + ( + yeah + + 2 + ) - + okay + diff --git a/spec/outputs/operators.lua b/spec/outputs/operators.lua index 95a10751..9829ef57 100644 --- a/spec/outputs/operators.lua +++ b/spec/outputs/operators.lua @@ -20,5 +20,10 @@ local v = { color = 1 and function() return "yeah" end, - "great" -} \ No newline at end of file + "great", + oksy = 3 ^ 2 +} +local nno = (yeah + 2) +local nn = (yeah + 2) +local n = hello(b)(function() end) +return hello(a, (yeah + 2) - okay) \ No newline at end of file From 5e162e22a569b5798170c9688d556b0c900880e0 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 14:08:44 -0800 Subject: [PATCH 089/344] spacebreak can come after table colon --- moonscript/parse.lua | 2 +- spec/inputs/tables.moon | 25 +++++++++++++++++++++++++ spec/outputs/tables.lua | 34 +++++++++++++++++++++++++++++++++- 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 0da86c62..2780a67f 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -546,7 +546,7 @@ local build_grammar = wrap_env(function() op"*" + op"^" + Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export", - KeyValue = (sym":" * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock)), + KeyValue = (sym":" * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)), KeyValueList = KeyValue * (sym"," * KeyValue)^0, KeyValueLine = CheckIndent * KeyValueList * sym","^-1, diff --git a/spec/inputs/tables.moon b/spec/inputs/tables.moon index e918556f..0c2a697f 100644 --- a/spec/inputs/tables.moon +++ b/spec/inputs/tables.moon @@ -102,3 +102,28 @@ xam = { } +kam = { + hello: 12 + goodcheese: + "mmm" + + yeah: + 12 + 232 + + lets: + keepit going: true, + okay: "yeah" + + more: + { + 1, [x for x=1,10] + } + + [{"one", "two"}]: + one_thing => +} + +keepit going: true, + okay: "yeah", + workd: "okay" + diff --git a/spec/outputs/tables.lua b/spec/outputs/tables.lua index 7eabec1c..cb6ff9f9 100644 --- a/spec/outputs/tables.lua +++ b/spec/outputs/tables.lua @@ -125,4 +125,36 @@ local xam = { hello = 1234, ["hello"] = 12354, ["hello"] = 12354 -} \ No newline at end of file +} +local kam = { + hello = 12, + goodcheese = "mmm", + yeah = 12 + 232, + lets = keepit({ + going = true + }, { + okay = "yeah" + }), + more = { + 1, + (function() + local _accum_0 = { } + local _len_0 = 1 + for x = 1, 10 do + _accum_0[_len_0] = x + _len_0 = _len_0 + 1 + end + return _accum_0 + end)() + }, + [{ + "one", + "two" + }] = one_thing(function(self) end) +} +return keepit({ + going = true +}, { + okay = "yeah", + workd = "okay" +}) \ No newline at end of file From adccbadf183cbfc0ae29aa2d11d4dbf62dc9d5f9 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 14:10:45 -0800 Subject: [PATCH 090/344] more whitespace sepcs for table --- spec/inputs/tables.moon | 14 ++++++++++++++ spec/outputs/tables.lua | 18 ++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/spec/inputs/tables.moon b/spec/inputs/tables.moon index 0c2a697f..3dad4124 100644 --- a/spec/inputs/tables.moon +++ b/spec/inputs/tables.moon @@ -123,7 +123,21 @@ kam = { one_thing => } +-- TODO: both of these have undesirable output keepit going: true, okay: "yeah", workd: "okay" +thing what: + "great", no: + "more" + okay: 123 + + +-- +thing what: + "great", no: + "more" +okay: 123 -- a anon table + +nil diff --git a/spec/outputs/tables.lua b/spec/outputs/tables.lua index cb6ff9f9..0cad58a5 100644 --- a/spec/outputs/tables.lua +++ b/spec/outputs/tables.lua @@ -152,9 +152,23 @@ local kam = { "two" }] = one_thing(function(self) end) } -return keepit({ +keepit({ going = true }, { okay = "yeah", workd = "okay" -}) \ No newline at end of file +}) +thing({ + what = "great", + no = "more" +}, { + okay = 123 +}) +thing({ + what = "great", + no = "more" +}) +local _ = { + okay = 123 +} +return nil \ No newline at end of file From b9013efcdccfbf862b304fb7723be5a3953e3053 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 14:48:25 -0800 Subject: [PATCH 091/344] parse.literals module --- Makefile | 5 ++- moonscript/parse.lua | 59 +++++++++++++--------------------- moonscript/parse/literals.lua | 34 ++++++++++++++++++++ moonscript/parse/literals.moon | 34 ++++++++++++++++++++ moonscript/util.lua | 11 ++++++- moonscript/util.moon | 8 ++++- 6 files changed, 112 insertions(+), 39 deletions(-) create mode 100644 moonscript/parse/literals.lua create mode 100644 moonscript/parse/literals.moon diff --git a/Makefile b/Makefile index 4781fa3a..9a44cfec 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,11 @@ -.PHONY: test local compile watch lint +.PHONY: test local compile watch lint test_safe test: busted -p "_spec.moon$$" +test_safe: + busted -p "_spec.lua$$" + local: compile luarocks make --local moonscript-dev-1.rockspec diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 2780a67f..427fb857 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -1,15 +1,15 @@ - -local util = require"moonscript.util" +local debug_grammar = false local lpeg = require"lpeg" -local debug_grammar = false +lpeg.setmaxstack(10000) +local util = require"moonscript.util" local data = require"moonscript.data" local types = require"moonscript.types" +local literals = require "moonscript.parse.literals" local ntype = types.ntype - local trim = util.trim local getfenv = util.getfenv @@ -18,6 +18,25 @@ local unpack = util.unpack local Stack = data.Stack +local R, S, V, P = lpeg.R, lpeg.S, lpeg.V, lpeg.P +local C, Ct, Cmt, Cg, Cb, Cc = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb, lpeg.Cc + +local White = literals.White +local Break = literals.Break +local Stop = literals.Stop +local Comment = literals.Comment +local Space = literals.Space +local SomeSpace = literals.SomeSpace +local SpaceBreak = literals.SpaceBreak +local EmptyLine = literals.EmptyLine +local AlphaNum = literals.AlphaNum +local _Name = literals.Name +local Num = literals.Num +local Shebang = literals.Shebang + +local Name = Space * _Name +Num = Space * (Num / function(value) return {"number", value} end) + local function count_indent(str) local sum = 0 for v in str:gmatch("[\t ]") do @@ -27,40 +46,8 @@ local function count_indent(str) return sum end -local R, S, V, P = lpeg.R, lpeg.S, lpeg.V, lpeg.P -local C, Ct, Cmt, Cg, Cb, Cc = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb, lpeg.Cc - -lpeg.setmaxstack(10000) - -local White = S" \t\r\n"^0 -local _Space = S" \t"^0 -local Break = P"\r"^-1 * P"\n" -local Stop = Break + -1 local Indent = C(S"\t "^0) / count_indent -local Comment = P"--" * (1 - S"\r\n")^0 * #Stop -local Space = _Space * Comment^-1 -local SomeSpace = S" \t"^1 * Comment^-1 - -local SpaceBreak = Space * Break -local EmptyLine = SpaceBreak - -local AlphaNum = R("az", "AZ", "09", "__") - -local _Name = C(R("az", "AZ", "__") * AlphaNum^0) -local Name = Space * _Name - -local Num = P"0x" * R("09", "af", "AF")^1 * (S"uU"^-1 * S"lL"^2)^-1 + - R"09"^1 * (S"uU"^-1 * S"lL"^2) + - ( - R"09"^1 * (P"." * R"09"^1)^-1 + - P"." * R"09"^1 - ) * (S"eE" * P"-"^-1 * R"09"^1)^-1 - -Num = Space * (Num / function(value) return {"number", value} end) - -local Shebang = P"#!" * P(1 - Stop)^0 - -- can't have P(false) because it causes preceding patterns not to run local Cut = P(function() return false end) diff --git a/moonscript/parse/literals.lua b/moonscript/parse/literals.lua new file mode 100644 index 00000000..61d6e2b8 --- /dev/null +++ b/moonscript/parse/literals.lua @@ -0,0 +1,34 @@ +local safe_module +safe_module = require("moonscript.util").safe_module +local S, P, R, C +do + local _obj_0 = require("lpeg") + S, P, R, C = _obj_0.S, _obj_0.P, _obj_0.R, _obj_0.C +end +local White = S(" \t\r\n") ^ 0 +local plain_space = S(" \t") ^ 0 +local Break = P("\r") ^ -1 * P("\n") +local Stop = Break + -1 +local Comment = P("--") * (1 - S("\r\n")) ^ 0 * #Stop +local Space = plain_space * Comment ^ -1 +local SomeSpace = S(" \t") ^ 1 * Comment ^ -1 +local SpaceBreak = Space * Break +local EmptyLine = SpaceBreak +local AlphaNum = R("az", "AZ", "09", "__") +local Name = C(R("az", "AZ", "__") * AlphaNum ^ 0) +local Num = P("0x") * R("09", "af", "AF") ^ 1 * (S("uU") ^ -1 * S("lL") ^ 2) ^ -1 + R("09") ^ 1 * (S("uU") ^ -1 * S("lL") ^ 2) + (R("09") ^ 1 * (P(".") * R("09") ^ 1) ^ -1 + P(".") * R("09") ^ 1) * (S("eE") * P("-") ^ -1 * R("09") ^ 1) ^ -1 +local Shebang = P("#!") * P(1 - Stop) ^ 0 +return safe_module("moonscript.parse.literals", { + White = White, + Break = Break, + Stop = Stop, + Comment = Comment, + Space = Space, + SomeSpace = SomeSpace, + SpaceBreak = SpaceBreak, + EmptyLine = EmptyLine, + AlphaNum = AlphaNum, + Name = Name, + Num = Num, + Shebang = Shebang +}) diff --git a/moonscript/parse/literals.moon b/moonscript/parse/literals.moon new file mode 100644 index 00000000..8f0d8cd6 --- /dev/null +++ b/moonscript/parse/literals.moon @@ -0,0 +1,34 @@ +-- non-recursive parsers +import safe_module from require "moonscript.util" +import S, P, R, C from require "lpeg" + +White = S" \t\r\n"^0 +plain_space = S" \t"^0 + +Break = P"\r"^-1 * P"\n" +Stop = Break + -1 + +Comment = P"--" * (1 - S"\r\n")^0 * #Stop +Space = plain_space * Comment^-1 +SomeSpace = S" \t"^1 * Comment^-1 + +SpaceBreak = Space * Break +EmptyLine = SpaceBreak + +AlphaNum = R "az", "AZ", "09", "__" + +Name = C R("az", "AZ", "__") * AlphaNum^0 + +Num = P"0x" * R("09", "af", "AF")^1 * (S"uU"^-1 * S"lL"^2)^-1 + + R"09"^1 * (S"uU"^-1 * S"lL"^2) + + ( + R"09"^1 * (P"." * R"09"^1)^-1 + + P"." * R"09"^1 + ) * (S"eE" * P"-"^-1 * R"09"^1)^-1 + +Shebang = P"#!" * P(1 - Stop)^0 + +safe_module "moonscript.parse.literals", { + :White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine, + :AlphaNum, :Name, :Num, :Shebang +} diff --git a/moonscript/util.lua b/moonscript/util.lua index 7eddae37..cc41ebc3 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -196,6 +196,14 @@ get_options = function(...) return { }, ... end end +local safe_module +safe_module = function(name, tbl) + return setmetatable(tbl, { + __index = function(self, key) + return error("Attempted to import non-existent `" .. tostring(key) .. "` from " .. tostring(name)) + end + }) +end return { moon = moon, pos_to_line = pos_to_line, @@ -209,5 +217,6 @@ return { getfenv = getfenv, setfenv = setfenv, get_options = get_options, - unpack = unpack + unpack = unpack, + safe_module = safe_module } diff --git a/moonscript/util.moon b/moonscript/util.moon index d43d0505..8df3b538 100644 --- a/moonscript/util.moon +++ b/moonscript/util.moon @@ -129,8 +129,14 @@ get_options = (...) -> else {}, ... +safe_module = (name, tbl) -> + setmetatable tbl, { + __index: (key) => + error "Attempted to import non-existent `#{key}` from #{name}" + } + { :moon, :pos_to_line, :get_closest_line, :get_line, :reversed, :trim, :split, - :dump, :debug_posmap, :getfenv, :setfenv, :get_options, :unpack + :dump, :debug_posmap, :getfenv, :setfenv, :get_options, :unpack, :safe_module } From 5b0df1cd623c5a34f092071bd30bdafa2b2a3aa0 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 15:01:28 -0800 Subject: [PATCH 092/344] wrap_env in moonscript, separate module --- moonscript/parse.lua | 58 ++------------------------------ moonscript/parse/env.lua | 70 +++++++++++++++++++++++++++++++++++++++ moonscript/parse/env.moon | 50 ++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+), 55 deletions(-) create mode 100644 moonscript/parse/env.lua create mode 100644 moonscript/parse/env.moon diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 427fb857..f8a1a441 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -12,8 +12,8 @@ local literals = require "moonscript.parse.literals" local ntype = types.ntype local trim = util.trim -local getfenv = util.getfenv -local setfenv = util.setfenv +local wrap_env = require("moonscript.parse.env").wrap_env + local unpack = util.unpack local Stack = data.Stack @@ -55,58 +55,6 @@ local function ensure(patt, finally) return patt * finally + finally * Cut end --- auto declare Proper variables with lpeg.V -local function wrap_env(fn) - local env = getfenv(fn) - local wrap_name = V - - if debug_grammar then - local indent = 0 - local indent_char = " " - - local function iprint(...) - local args = {...} - for i=1,#args do - args[i] = tostring(args[i]) - end - - io.stdout:write(indent_char:rep(indent) .. table.concat(args, ", ") .. "\n") - end - - wrap_name = function(name) - local v = V(name) - v = Cmt("", function() - iprint("* " .. name) - indent = indent + 1 - return true - end) * Cmt(v, function(str, pos, ...) - iprint(name, true) - indent = indent - 1 - return true, ... - end) + Cmt("", function() - iprint(name, false) - indent = indent - 1 - return false - end) - return v - end - end - - return setfenv(fn, setmetatable({}, { - __index = function(self, name) - local value = env[name] - if value ~= nil then return value end - - if name:match"^[A-Z][A-Za-z0-9]*$" then - local v = wrap_name(name) - rawset(self, name, v) - return v - end - error("unknown variable referenced: "..name) - end - })) -end - local function extract_line(str, start_pos) str = str:sub(start_pos) local m = str:match"^(.-)\n" @@ -268,7 +216,7 @@ end local err_msg = "Failed to parse:%s\n [%d] >> %s" -local build_grammar = wrap_env(function() +local build_grammar = wrap_env(debug_grammar, function() local _indent = Stack(0) -- current indent local _do_stack = Stack(0) diff --git a/moonscript/parse/env.lua b/moonscript/parse/env.lua new file mode 100644 index 00000000..4a0adc33 --- /dev/null +++ b/moonscript/parse/env.lua @@ -0,0 +1,70 @@ +local getfenv, setfenv +do + local _obj_0 = require("moonscript.util") + getfenv, setfenv = _obj_0.getfenv, _obj_0.setfenv +end +local wrap_env +wrap_env = function(debug, fn) + local V, Cmt + do + local _obj_0 = require("lpeg") + V, Cmt = _obj_0.V, _obj_0.Cmt + end + local env = getfenv(fn) + local wrap_name = V + if debug then + local indent = 0 + local indent_char = " " + local iprint + iprint = function(...) + local args = table.concat((function(...) + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = { + ... + } + for _index_0 = 1, #_list_0 do + local a = _list_0[_index_0] + _accum_0[_len_0] = tostring(a) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(...), ", ") + return io.stderr:write(tostring(indent_char:rep(indent)) .. tostring(args) .. "\n") + end + wrap_name = function(name) + local v = V(name) + v = Cmt("", function() + iprint("* " .. name) + indent = indent + 1 + return true + end) * Cmt(v, function(str, pos, ...) + iprint(name, true) + indent = indent - 1 + return true, ... + end) + Cmt("", function() + iprint(name, false) + indent = indent - 1 + return false + end) + return v + end + end + return setfenv(fn, setmetatable({ }, { + __index = function(self, name) + local value = env[name] + if value ~= nil then + return value + end + if name:match("^[A-Z][A-Za-z0-9]*$") then + local v = wrap_name(name) + rawset(self, name, v) + return v + end + return error("unknown variable referenced: " .. tostring(name)) + end + })) +end +return { + wrap_env = wrap_env +} diff --git a/moonscript/parse/env.moon b/moonscript/parse/env.moon new file mode 100644 index 00000000..594d3e59 --- /dev/null +++ b/moonscript/parse/env.moon @@ -0,0 +1,50 @@ + +import getfenv, setfenv from require "moonscript.util" + +-- all undefined Proper globals are automaticlly converted into lpeg.V +wrap_env = (debug, fn) -> + import V, Cmt from require "lpeg" + + env = getfenv fn + wrap_name = V + + if debug + indent = 0 + indent_char = " " + + iprint = (...) -> + args = table.concat [tostring a for a in *{...}], ", " + io.stderr\write "#{indent_char\rep(indent)}#{args}\n" + + wrap_name = (name) -> + v = V name + v = Cmt("", -> + iprint "* " .. name + indent += 1 + true + ) * Cmt(v, (str, pos, ...) -> + iprint name, true + indent -= 1 + true, ... + ) + Cmt("", -> + iprint name, false + indent -= 1 + false + ) + + v + + setfenv fn, setmetatable {}, { + __index: (name) => + value = env[name] + return value if value != nil + + if name\match"^[A-Z][A-Za-z0-9]*$" + v = wrap_name name + rawset @, name, v + return v + + error "unknown variable referenced: #{name}" + } + +{ :wrap_env } From 72d496dd667fcb8b8aaa7d5ffb809bf38e2915e9 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 15:12:27 -0800 Subject: [PATCH 093/344] start moving parse helpers to moonscript --- moonscript/parse.lua | 21 ++++----------------- moonscript/parse/util.lua | 31 +++++++++++++++++++++++++++++++ moonscript/parse/util.moon | 23 +++++++++++++++++++++++ 3 files changed, 58 insertions(+), 17 deletions(-) create mode 100644 moonscript/parse/util.lua create mode 100644 moonscript/parse/util.moon diff --git a/moonscript/parse.lua b/moonscript/parse.lua index f8a1a441..4f7ec894 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -8,6 +8,7 @@ local util = require"moonscript.util" local data = require"moonscript.data" local types = require"moonscript.types" local literals = require "moonscript.parse.literals" +local parse_util = require "moonscript.parse.util" local ntype = types.ntype local trim = util.trim @@ -37,23 +38,9 @@ local Shebang = literals.Shebang local Name = Space * _Name Num = Space * (Num / function(value) return {"number", value} end) -local function count_indent(str) - local sum = 0 - for v in str:gmatch("[\t ]") do - if v == ' ' then sum = sum + 1 end - if v == '\t' then sum = sum + 4 end - end - return sum -end - -local Indent = C(S"\t "^0) / count_indent - --- can't have P(false) because it causes preceding patterns not to run -local Cut = P(function() return false end) - -local function ensure(patt, finally) - return patt * finally + finally * Cut -end +local Indent = parse_util.Indent +local Cut = parse_util.Cut +local ensure = parse_util.ensure local function extract_line(str, start_pos) str = str:sub(start_pos) diff --git a/moonscript/parse/util.lua b/moonscript/parse/util.lua new file mode 100644 index 00000000..81a4ca9f --- /dev/null +++ b/moonscript/parse/util.lua @@ -0,0 +1,31 @@ +local P, C, S +do + local _obj_0 = require("lpeg") + P, C, S = _obj_0.P, _obj_0.C, _obj_0.S +end +local Indent = C(S("\t ") ^ 0) / function(str) + do + local sum = 0 + for v in str:gmatch("[\t ]") do + local _exp_0 = v + if " " == _exp_0 then + sum = sum + 1 + elseif "\t" == _exp_0 then + sum = sum + 4 + end + end + return sum + end +end +local Cut = P(function() + return false +end) +local ensure +ensure = function(patt, finally) + return patt * finally + finally * Cut +end +return { + Indent = Indent, + Cut = Cut, + ensure = ensure +} diff --git a/moonscript/parse/util.moon b/moonscript/parse/util.moon new file mode 100644 index 00000000..f10dda99 --- /dev/null +++ b/moonscript/parse/util.moon @@ -0,0 +1,23 @@ + +import P, C, S from require "lpeg" + +-- captures an indentation, returns indent depth +Indent = C(S"\t "^0) / (str) -> + with sum = 0 + for v in str\gmatch "[\t ]" + switch v + when " " + sum += 1 + when "\t" + sum += 4 + + +-- causes pattern in progress to be rejected +-- can't have P(false) because it causes preceding patterns not to run +Cut = P -> false + +-- ensures finally runs regardless of whether pattern fails or passes +ensure = (patt, finally) -> + patt * finally + finally * Cut + +{ :Indent, :Cut, :ensure } From a1d9e39caa4a538cc5558d64f611f63ea430f4c2 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 16:01:52 -0800 Subject: [PATCH 094/344] convert rest of parse helpers to moonscript --- moonscript/parse.lua | 177 +++------------------------ moonscript/parse/util.lua | 242 ++++++++++++++++++++++++++++++++++++- moonscript/parse/util.moon | 150 ++++++++++++++++++++++- 3 files changed, 404 insertions(+), 165 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 4f7ec894..af5398cb 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -41,165 +41,24 @@ Num = Space * (Num / function(value) return {"number", value} end) local Indent = parse_util.Indent local Cut = parse_util.Cut local ensure = parse_util.ensure - -local function extract_line(str, start_pos) - str = str:sub(start_pos) - local m = str:match"^(.-)\n" - if m then return m end - return str:match"^.-$" -end - -local function mark(name) - return function(...) - return {name, ...} - end -end - -local function insert_pos(pos, value) - if type(value) == "table" then - value[-1] = pos - end - return value -end - -local function pos(patt) - return (lpeg.Cp() * patt) / insert_pos -end - -local function got(what) - return Cmt("", function(str, pos, ...) - local cap = {...} - print("++ got "..what, "["..extract_line(str, pos).."]") - return true - end) -end - -local function flatten_or_mark(name) - return function(tbl) - if #tbl == 1 then return tbl[1] end - table.insert(tbl, 1, name) - return tbl - end -end - --- makes sure the last item in a chain is an index -local _chain_assignable = { index = true, dot = true, slice = true } - -local function is_assignable(node) - if node == "..." then - return false - end - - local t = ntype(node) - return t == "ref" or t == "self" or t == "value" or t == "self_class" or - t == "chain" and _chain_assignable[ntype(node[#node])] or - t == "table" -end - -local function check_assignable(str, pos, value) - if is_assignable(value) then - return true, value - end - return false -end - -local flatten_explist = flatten_or_mark"explist" -local function format_assign(lhs_exps, assign) - if not assign then - return flatten_explist(lhs_exps) - end - - for _, assign_exp in ipairs(lhs_exps) do - if not is_assignable(assign_exp) then - error {assign_exp, "left hand expression is not assignable"} - end - end - - local t = ntype(assign) - if t == "assign" then - return {"assign", lhs_exps, unpack(assign, 2)} - elseif t == "update" then - return {"update", lhs_exps[1], unpack(assign, 2)} - end - - error "unknown assign expression" -end - --- the if statement only takes a single lhs, so we wrap in table to git to --- "assign" tuple format -local function format_single_assign(lhs, assign) - if assign then - return format_assign({lhs}, assign) - end - return lhs -end - -local function sym(chars) - return Space * chars -end - --- Like sym but doesn't accept whitespace in front -local function symx(chars) - return chars -end - -local function simple_string(delim, allow_interpolation) - local inner = P('\\'..delim) + "\\\\" + (1 - P(delim)) - if allow_interpolation then - local inter = symx"#{" * V"Exp" * sym"}" - inner = (C((inner - inter)^1) + inter / mark"interpolate")^0 - else - inner = C(inner^0) - end - - return C(symx(delim)) * - inner * sym(delim) / mark"string" -end - -local function wrap_func_arg(value) - return {"call", {value}} -end - --- DOCME -local function flatten_func(callee, args) - if #args == 0 then return callee end - - args = {"call", args} - if ntype(callee) == "chain" then - -- check for colon stub that needs arguments - if ntype(callee[#callee]) == "colon_stub" then - local stub = callee[#callee] - stub[1] = "colon" - table.insert(stub, args) - else - table.insert(callee, args) - end - - return callee - end - - return {"chain", callee, args} -end - -local function flatten_string_chain(str, chain, args) - if not chain then return str end - return flatten_func({"chain", str, unpack(chain)}, args) -end - --- transforms a statement that has a line decorator -local function wrap_decorator(stm, dec) - if not dec then return stm end - return { "decorated", stm, dec } -end - -local function check_lua_string(str, pos, right, left) - return #left == #right -end - --- :name in table literal -local function self_assign(name, pos) - return {{"key_literal", name}, {"ref", name, [-1] = pos}} -end +local extract_line = parse_util.extract_line +local mark = parse_util.mark +local pos = parse_util.pos +local got = parse_util.got +local flatten_or_mark = parse_util.flatten_or_mark +local is_assignable = parse_util.is_assignable +local check_assignable = parse_util.check_assignable +local format_assign = parse_util.format_assign +local format_single_assign = parse_util.format_single_assign +local sym = parse_util.sym +local symx = parse_util.symx +local simple_string = parse_util.simple_string +local wrap_func_arg = parse_util.wrap_func_arg +local flatten_func = parse_util.flatten_func +local flatten_string_chain = parse_util.flatten_string_chain +local wrap_decorator = parse_util.wrap_decorator +local check_lua_string = parse_util.check_lua_string +local self_assign = parse_util.self_assign local err_msg = "Failed to parse:%s\n [%d] >> %s" diff --git a/moonscript/parse/util.lua b/moonscript/parse/util.lua index 81a4ca9f..6db63f06 100644 --- a/moonscript/parse/util.lua +++ b/moonscript/parse/util.lua @@ -1,8 +1,14 @@ -local P, C, S +local unpack +unpack = require("moonscript.util").unpack +local P, C, S, Cp, Cmt, V do local _obj_0 = require("lpeg") - P, C, S = _obj_0.P, _obj_0.C, _obj_0.S + P, C, S, Cp, Cmt, V = _obj_0.P, _obj_0.C, _obj_0.S, _obj_0.Cp, _obj_0.Cmt, _obj_0.V end +local ntype +ntype = require("moonscript.types").ntype +local Space +Space = require("moonscript.parse.literals").Space local Indent = C(S("\t ") ^ 0) / function(str) do local sum = 0 @@ -24,8 +30,238 @@ local ensure ensure = function(patt, finally) return patt * finally + finally * Cut end +local extract_line +extract_line = function(str, start_pos) + str = str:sub(start_pos) + do + local m = str:match("^(.-)\n") + if m then + return m + end + end + return str:match("^.-$") +end +local mark +mark = function(name) + return function(...) + return { + name, + ... + } + end +end +local pos +pos = function(patt) + return (Cp() * patt) / function(pos, value) + if type(value) == "table" then + value[-1] = pos + end + return value + end +end +local got +got = function(what) + return Cmt("", function(str, pos) + print("++ got " .. tostring(what), "[" .. tostring(extract_line(str, pos)) .. "]") + return true + end) +end +local flatten_or_mark +flatten_or_mark = function(name) + return function(tbl) + if #tbl == 1 then + return tbl[1] + end + table.insert(tbl, 1, name) + return tbl + end +end +local is_assignable +do + local chain_assignable = { + index = true, + dot = true, + slice = true + } + is_assignable = function(node) + if node == "..." then + return false + end + local _exp_0 = ntype(node) + if "ref" == _exp_0 or "self" == _exp_0 or "value" == _exp_0 or "self_class" == _exp_0 or "table" == _exp_0 then + return true + elseif "chain" == _exp_0 then + return chain_assignable[ntype(node[#node])] + else + return false + end + end +end +local check_assignable +check_assignable = function(str, pos, value) + if is_assignable(value) then + return true, value + else + return false + end +end +local format_assign +do + local flatten_explist = flatten_or_mark("explist") + format_assign = function(lhs_exps, assign) + if not (assign) then + return flatten_explist(lhs_exps) + end + for _index_0 = 1, #lhs_exps do + local assign_exp = lhs_exps[_index_0] + if not (is_assignable(assign_exp)) then + error({ + assign_exp, + "left hand expression is not assignable" + }) + end + end + local t = ntype(assign) + local _exp_0 = t + if "assign" == _exp_0 then + return { + "assign", + lhs_exps, + unpack(assign, 2) + } + elseif "update" == _exp_0 then + return { + "update", + lhs_exps[1], + unpack(assign, 2) + } + else + return error("unknown assign expression: " .. tostring(t)) + end + end +end +local format_single_assign +format_single_assign = function(lhs, assign) + if assign then + return format_assign({ + lhs + }, assign) + else + return lhs + end +end +local sym +sym = function(chars) + return Space * chars +end +local symx +symx = function(chars) + return chars +end +local simple_string +simple_string = function(delim, allow_interpolation) + local inner = P("\\" .. tostring(delim)) + "\\\\" + (1 - P(delim)) + if allow_interpolation then + local interp = symx('#{') * V("Exp") * sym('}') + inner = (C((inner - interp) ^ 1) + interp / mark("interpolate")) ^ 0 + else + inner = C(inner ^ 0) + end + return C(symx(delim)) * inner * sym(delim) / mark("string") +end +local wrap_func_arg +wrap_func_arg = function(value) + return { + "call", + { + value + } + } +end +local flatten_func +flatten_func = function(callee, args) + if #args == 0 then + return callee + end + args = { + "call", + args + } + if ntype(callee) == "chain" then + local stub = callee[#callee] + if ntype(stub) == "colon_stub" then + stub[1] = "colon" + table.insert(stub, args) + else + table.insert(callee, args) + end + return callee + end + return { + "chain", + callee, + args + } +end +local flatten_string_chain +flatten_string_chain = function(str, chain, args) + if not (chain) then + return str + end + return flatten_func({ + "chain", + str, + unpack(chain) + }, args) +end +local wrap_decorator +wrap_decorator = function(stm, dec) + if not (dec) then + return stm + end + return { + "decorated", + stm, + dec + } +end +local check_lua_string +check_lua_string = function(str, pos, right, left) + return #left == #right +end +local self_assign +self_assign = function(name, pos) + return { + { + "key_literal", + name + }, + { + "ref", + name, + [-1] = pos + } + } +end return { Indent = Indent, Cut = Cut, - ensure = ensure + ensure = ensure, + extract_line = extract_line, + mark = mark, + pos = pos, + flatten_or_mark = flatten_or_mark, + is_assignable = is_assignable, + check_assignable = check_assignable, + format_assign = format_assign, + format_single_assign = format_single_assign, + sym = sym, + symx = symx, + simple_string = simple_string, + wrap_func_arg = wrap_func_arg, + flatten_func = flatten_func, + flatten_string_chain = flatten_string_chain, + wrap_decorator = wrap_decorator, + check_lua_string = check_lua_string, + self_assign = self_assign } diff --git a/moonscript/parse/util.moon b/moonscript/parse/util.moon index f10dda99..fc680d7f 100644 --- a/moonscript/parse/util.moon +++ b/moonscript/parse/util.moon @@ -1,5 +1,8 @@ -import P, C, S from require "lpeg" +import unpack from require "moonscript.util" +import P, C, S, Cp, Cmt, V from require "lpeg" +import ntype from require "moonscript.types" +import Space from require "moonscript.parse.literals" -- captures an indentation, returns indent depth Indent = C(S"\t "^0) / (str) -> @@ -18,6 +21,147 @@ Cut = P -> false -- ensures finally runs regardless of whether pattern fails or passes ensure = (patt, finally) -> - patt * finally + finally * Cut + patt * finally + finally * Cut -{ :Indent, :Cut, :ensure } +-- take rest of line from pos out of str +extract_line = (str, start_pos) -> + str = str\sub start_pos + if m = str\match "^(.-)\n" + return m + + str\match "^.-$" + +-- used to identify a capture with a label +mark = (name) -> + (...) -> {name, ...} + +-- wraps pattern to capture pos into node +-- pos is the character offset from the buffer where the node was parsed from. +-- Used to generate error messages +pos = (patt) -> + (Cp! * patt) / (pos, value) -> + if type(value) == "table" + value[-1] = pos + value + +-- generates a debug pattern that always succeeds and prints out where we are +-- in the buffer with a label +got = (what) -> + Cmt "", (str, pos) -> + print "++ got #{what}", "[#{extract_line str, pos}]" + true + +-- converts 1 element array to its value, otherwise marks it +flatten_or_mark = (name) -> + (tbl) -> + return tbl[1] if #tbl == 1 + table.insert tbl, 1, name + tbl + +-- determines if node is able to be on left side of assignment +is_assignable = do + chain_assignable = { index: true, dot: true, slice: true } + + (node) -> + return false if node == "..." + switch ntype node + when "ref", "self", "value", "self_class", "table" + true + when "chain" + chain_assignable[ntype node[#node]] + else + false + +check_assignable = (str, pos, value) -> + if is_assignable value + true, value + else + false + +-- joins the two parts of an assign parse into a single node +format_assign = do + flatten_explist = flatten_or_mark "explist" + + (lhs_exps, assign) -> + unless assign + return flatten_explist lhs_exps + + for assign_exp in *lhs_exps + unless is_assignable assign_exp + error {assign_exp, "left hand expression is not assignable"} + + t = ntype assign + switch t + when "assign" + {"assign", lhs_exps, unpack assign, 2} + when "update" + {"update", lhs_exps[1], unpack assign, 2} + else + error "unknown assign expression: #{t}" + +-- helper for if statement, which only has single lhs +format_single_assign = (lhs, assign) -> + if assign + format_assign {lhs}, assign + else + lhs + + +-- a symbol +sym = (chars) -> Space * chars +-- a symbole that doesn't accept whitespace before it +symx = (chars) -> chars + +-- a constructor for quote delimited strings +simple_string = (delim, allow_interpolation) -> + inner = P("\\#{delim}") + "\\\\" + (1 - P delim) + + inner = if allow_interpolation + interp = symx'#{' * V"Exp" * sym'}' + (C((inner - interp)^1) + interp / mark"interpolate")^0 + else + C inner^0 + + C(symx(delim)) * inner * sym(delim) / mark"string" + +-- wraps a single value in format needed to be passed as function arguments +wrap_func_arg = (value) -> {"call", {value}} + +-- flatten out the parsed function node +flatten_func = (callee, args) -> + return callee if #args == 0 + args = {"call", args} + + if ntype(callee) == "chain" + -- check for colon stub needing arguments + stub = callee[#callee] + if ntype(stub) == "colon_stub" + stub[1] = "colon" + table.insert stub, args + else + table.insert callee, args + + return callee + + {"chain", callee, args} + +flatten_string_chain = (str, chain, args) -> + return str unless chain + flatten_func {"chain", str, unpack chain}, args + +-- constructor for decorator node +wrap_decorator = (stm, dec) -> + return stm unless dec + {"decorated", stm, dec} + +check_lua_string = (str, pos, right, left) -> + #left == #right + +-- constructor for :name self assignments in table literals +self_assign = (name, pos) -> + {{"key_literal", name}, {"ref", name, [-1]: pos}} + +{ :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, + :is_assignable, :check_assignable, :format_assign, :format_single_assign, + :sym, :symx, :simple_string, :wrap_func_arg, :flatten_func, + :flatten_string_chain, :wrap_decorator, :check_lua_string, :self_assign } From c8e6528ef7cac17d6e139e248c1bede90625615b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 16:55:02 -0800 Subject: [PATCH 095/344] convert parser to moonscript --- moonscript/parse.lua | 635 +++++++++++++++--------------------------- moonscript/parse.moon | 355 +++++++++++++++++++++++ 2 files changed, 583 insertions(+), 407 deletions(-) create mode 100644 moonscript/parse.moon diff --git a/moonscript/parse.lua b/moonscript/parse.lua index af5398cb..12f9062e 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -1,414 +1,235 @@ local debug_grammar = false - -local lpeg = require"lpeg" - +local lpeg = require("lpeg") lpeg.setmaxstack(10000) - -local util = require"moonscript.util" -local data = require"moonscript.data" -local types = require"moonscript.types" -local literals = require "moonscript.parse.literals" -local parse_util = require "moonscript.parse.util" - -local ntype = types.ntype -local trim = util.trim - -local wrap_env = require("moonscript.parse.env").wrap_env - -local unpack = util.unpack - -local Stack = data.Stack - -local R, S, V, P = lpeg.R, lpeg.S, lpeg.V, lpeg.P -local C, Ct, Cmt, Cg, Cb, Cc = lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb, lpeg.Cc - -local White = literals.White -local Break = literals.Break -local Stop = literals.Stop -local Comment = literals.Comment -local Space = literals.Space -local SomeSpace = literals.SomeSpace -local SpaceBreak = literals.SpaceBreak -local EmptyLine = literals.EmptyLine -local AlphaNum = literals.AlphaNum -local _Name = literals.Name -local Num = literals.Num -local Shebang = literals.Shebang - -local Name = Space * _Name -Num = Space * (Num / function(value) return {"number", value} end) - -local Indent = parse_util.Indent -local Cut = parse_util.Cut -local ensure = parse_util.ensure -local extract_line = parse_util.extract_line -local mark = parse_util.mark -local pos = parse_util.pos -local got = parse_util.got -local flatten_or_mark = parse_util.flatten_or_mark -local is_assignable = parse_util.is_assignable -local check_assignable = parse_util.check_assignable -local format_assign = parse_util.format_assign -local format_single_assign = parse_util.format_single_assign -local sym = parse_util.sym -local symx = parse_util.symx -local simple_string = parse_util.simple_string -local wrap_func_arg = parse_util.wrap_func_arg -local flatten_func = parse_util.flatten_func -local flatten_string_chain = parse_util.flatten_string_chain -local wrap_decorator = parse_util.wrap_decorator -local check_lua_string = parse_util.check_lua_string -local self_assign = parse_util.self_assign - local err_msg = "Failed to parse:%s\n [%d] >> %s" - +local Stack +Stack = require("moonscript.data").Stack +local trim, pos_to_line, get_line +do + local _obj_0 = require("moonscript.util") + trim, pos_to_line, get_line = _obj_0.trim, _obj_0.pos_to_line, _obj_0.get_line +end +local unpack +unpack = require("moonscript.util").unpack +local wrap_env +wrap_env = require("moonscript.parse.env").wrap_env +local R, S, V, P, C, Ct, Cmt, Cg, Cb, Cc +R, S, V, P, C, Ct, Cmt, Cg, Cb, Cc = lpeg.R, lpeg.S, lpeg.V, lpeg.P, lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb, lpeg.Cc +local White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, _Name +do + local _obj_0 = require("moonscript.parse.literals") + White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, _Name = _obj_0.White, _obj_0.Break, _obj_0.Stop, _obj_0.Comment, _obj_0.Space, _obj_0.SomeSpace, _obj_0.SpaceBreak, _obj_0.EmptyLine, _obj_0.AlphaNum, _obj_0.Num, _obj_0.Shebang, _obj_0.Name +end +local SpaceName = Space * _Name +Num = Space * (Num / function(v) + return { + "number", + v + } +end) +local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, flatten_func, flatten_string_chain, wrap_decorator, check_lua_string, self_assign +do + local _obj_0 = require("moonscript.parse.util") + Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, flatten_func, flatten_string_chain, wrap_decorator, check_lua_string, self_assign = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.flatten_func, _obj_0.flatten_string_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign +end local build_grammar = wrap_env(debug_grammar, function() - local _indent = Stack(0) -- current indent - local _do_stack = Stack(0) - - local last_pos = 0 -- used to know where to report error - local function check_indent(str, pos, indent) - last_pos = pos - return _indent:top() == indent - end - - local function advance_indent(str, pos, indent) - local top = _indent:top() - if top ~= -1 and indent > _indent:top() then - _indent:push(indent) - return true - end - end - - local function push_indent(str, pos, indent) - _indent:push(indent) - return true - end - - local function pop_indent(str, pos) - if not _indent:pop() then error("unexpected outdent") end - return true - end - - - local function check_do(str, pos, do_node) - local top = _do_stack:top() - if top == nil or top then - return true, do_node - end - return false - end - - local function disable_do(str_pos) - _do_stack:push(false) - return true - end - - local function pop_do(str, pos) - if nil == _do_stack:pop() then error("unexpected do pop") end - return true - end - - local DisableDo = Cmt("", disable_do) - local PopDo = Cmt("", pop_do) - - local keywords = {} - local function key(chars) - keywords[chars] = true - return Space * chars * -AlphaNum - end - - local function op(word) - local patt = Space * C(word) - if word:match("^%w*$") then - keywords[word] = true - patt = patt * -AlphaNum - end - return patt - end - - -- make sure name is not a keyword - local Name = Cmt(Name, function(str, pos, name) - if keywords[name] then return false end - return true - end) / trim - - local SelfName = Space * "@" * ( - "@" * (_Name / mark"self_class" + Cc"self.__class") + - _Name / mark"self" + Cc"self") - - local KeyName = SelfName + Space * _Name / mark"key_literal" - local VarArg = Space * P"..." / trim - - local g = lpeg.P{ - File, - File = Shebang^-1 * (Block + Ct""), - Block = Ct(Line * (Break^1 * Line)^0), - CheckIndent = Cmt(Indent, check_indent), -- validates line is in correct indent - Line = (CheckIndent * Statement + Space * #Stop), - - Statement = pos( - Import + While + With + For + ForEach + Switch + Return + - Local + Export + BreakLoop + - Ct(ExpList) * (Update + Assign)^-1 / format_assign - ) * Space * (( - -- statement decorators - key"if" * Exp * (key"else" * Exp)^-1 * Space / mark"if" + - key"unless" * Exp / mark"unless" + - CompInner / mark"comprehension" - ) * Space)^-1 / wrap_decorator, - - Body = Space^-1 * Break * EmptyLine^0 * InBlock + Ct(Statement), -- either a statement, or an indented block - - Advance = #Cmt(Indent, advance_indent), -- Advances the indent, gives back whitespace for CheckIndent - PushIndent = Cmt(Indent, push_indent), - PreventIndent = Cmt(Cc(-1), push_indent), - PopIndent = Cmt("", pop_indent), - InBlock = Advance * Block * PopIndent, - - Local = key"local" * ((op"*" + op"^") / mark"declare_glob" + Ct(NameList) / mark"declare_with_shadows"), - - Import = key"import" * Ct(ImportNameList) * SpaceBreak^0 * key"from" * Exp / mark"import", - ImportName = (sym"\\" * Ct(Cc"colon_stub" * Name) + Name), - ImportNameList = SpaceBreak^0 * ImportName * ((SpaceBreak^1 + sym"," * SpaceBreak^0) * ImportName)^0, - - BreakLoop = Ct(key"break"/trim) + Ct(key"continue"/trim), - - Return = key"return" * (ExpListLow/mark"explist" + C"") / mark"return", - - WithExp = Ct(ExpList) * Assign^-1 / format_assign, - With = key"with" * DisableDo * ensure(WithExp, PopDo) * key"do"^-1 * Body / mark"with", - - Switch = key"switch" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Space^-1 * Break * SwitchBlock / mark"switch", - - SwitchBlock = EmptyLine^0 * Advance * Ct(SwitchCase * (Break^1 * SwitchCase)^0 * (Break^1 * SwitchElse)^-1) * PopIndent, - SwitchCase = key"when" * Ct(ExpList) * key"then"^-1 * Body / mark"case", - SwitchElse = key"else" * Body / mark"else", - - IfCond = Exp * Assign^-1 / format_single_assign, - - If = key"if" * IfCond * key"then"^-1 * Body * - ((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif")^0 * - ((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if", - - Unless = key"unless" * IfCond * key"then"^-1 * Body * - ((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"unless", - - While = key"while" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Body / mark"while", - - For = key"for" * DisableDo * ensure(Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1), PopDo) * - key"do"^-1 * Body / mark"for", - - ForEach = key"for" * Ct(AssignableNameList) * key"in" * DisableDo * ensure(Ct(sym"*" * Exp / mark"unpack" + ExpList), PopDo) * key"do"^-1 * Body / mark"foreach", - - Do = key"do" * Body / mark"do", - - Comprehension = sym"[" * Exp * CompInner * sym"]" / mark"comprehension", - - TblComprehension = sym"{" * Ct(Exp * (sym"," * Exp)^-1) * CompInner * sym"}" / mark"tblcomprehension", - - CompInner = Ct((CompForEach + CompFor) * CompClause^0), - CompForEach = key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"foreach", - CompFor = key "for" * Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1) / mark"for", - CompClause = CompFor + CompForEach + key"when" * Exp / mark"when", - - Assign = sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign", - Update = ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update", - - CharOperators = Space * C(S"+-*/%^><"), - WordOperators = op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op"..", - BinaryOperator = (WordOperators + CharOperators) * SpaceBreak^0, - - Assignable = Cmt(DotChain + Chain, check_assignable) + Name + SelfName, - Exp = Ct(Value * (BinaryOperator * Value)^0) / flatten_or_mark"exp", - - SimpleValue = - If + Unless + - Switch + - With + - ClassDecl + - ForEach + For + While + - Cmt(Do, check_do) + - sym"-" * -SomeSpace * Exp / mark"minus" + - sym"#" * Exp / mark"length" + - key"not" * Exp / mark"not" + - TblComprehension + - TableLit + - Comprehension + - FunLit + - Num, - - ChainValue = -- a function call or an object access - StringChain + - ((Chain + DotChain + Callable) * Ct(InvokeArgs^-1)) / flatten_func, - - Value = pos( - SimpleValue + - Ct(KeyValueList) / mark"table" + - ChainValue), - - SliceValue = SimpleValue + ChainValue, - - StringChain = String * - (Ct((ColonCall + ColonSuffix) * ChainTail^-1) * Ct(InvokeArgs^-1))^-1 / flatten_string_chain, - - String = Space * DoubleString + Space * SingleString + LuaString, - SingleString = simple_string("'"), - DoubleString = simple_string('"', true), - - LuaString = Cg(LuaStringOpen, "string_open") * Cb"string_open" * Break^-1 * - C((1 - Cmt(C(LuaStringClose) * Cb"string_open", check_lua_string))^0) * - LuaStringClose / mark"string", - - LuaStringOpen = sym"[" * P"="^0 * "[" / trim, - LuaStringClose = "]" * P"="^0 * "]", - - Callable = pos(Name / mark"ref") + SelfName + VarArg + Parens / mark"parens", - Parens = sym"(" * SpaceBreak^0 * Exp * SpaceBreak^0 * sym")", - - FnArgs = symx"(" * SpaceBreak^0 * Ct(ExpList^-1) * SpaceBreak^0 * sym")" + sym"!" * -P"=" * Ct"", - - ChainTail = ChainItem^1 * ColonSuffix^-1 + ColonSuffix, - - -- a list of funcalls and indexes on a callable - Chain = Callable * ChainTail / mark"chain", - - -- shorthand dot call for use in with statement - DotChain = - (sym"." * Cc(-1) * (_Name / mark"dot") * ChainTail^-1) / mark"chain" + - (sym"\\" * Cc(-1) * ( - (_Name * Invoke / mark"colon") * ChainTail^-1 + - (_Name / mark"colon_stub") - )) / mark"chain", - - ChainItem = - Invoke + - Slice + - symx"[" * Exp/mark"index" * sym"]" + - symx"." * _Name/mark"dot" + - ColonCall, - - Slice = symx"[" * (SliceValue + Cc(1)) * sym"," * (SliceValue + Cc"") * - (sym"," * SliceValue)^-1 *sym"]" / mark"slice", - - ColonCall = symx"\\" * (_Name * Invoke) / mark"colon", - ColonSuffix = symx"\\" * _Name / mark"colon_stub", - - Invoke = FnArgs/mark"call" + - SingleString / wrap_func_arg + - DoubleString / wrap_func_arg, - - TableValue = KeyValue + Ct(Exp), - - TableLit = sym"{" * Ct( - TableValueList^-1 * sym","^-1 * - (SpaceBreak * TableLitLine * (sym","^-1 * SpaceBreak * TableLitLine)^0 * sym","^-1)^-1 - ) * White * sym"}" / mark"table", - - TableValueList = TableValue * (sym"," * TableValue)^0, - TableLitLine = PushIndent * ((TableValueList * PopIndent) + (PopIndent * Cut)) + Space, - - -- the unbounded table - TableBlockInner = Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0), - TableBlock = SpaceBreak^1 * Advance * ensure(TableBlockInner, PopIndent) / mark"table", - - ClassDecl = key"class" * -P":" * (Assignable + Cc(nil)) * (key"extends" * PreventIndent * ensure(Exp, PopIndent) + C"")^-1 * (ClassBlock + Ct("")) / mark"class", - - ClassBlock = SpaceBreak^1 * Advance * - Ct(ClassLine * (SpaceBreak^1 * ClassLine)^0) * PopIndent, - ClassLine = CheckIndent * (( - KeyValueList / mark"props" + - Statement / mark"stm" + - Exp / mark"stm" - ) * sym","^-1), - - Export = key"export" * ( - Cc"class" * ClassDecl + - op"*" + op"^" + - Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export", - - KeyValue = (sym":" * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)), - KeyValueList = KeyValue * (sym"," * KeyValue)^0, - KeyValueLine = CheckIndent * KeyValueList * sym","^-1, - - FnArgsDef = sym"(" * Ct(FnArgDefList^-1) * - (key"using" * Ct(NameList + Space * "nil") + Ct"") * - sym")" + Ct"" * Ct"", - - FnArgDefList = FnArgDef * (sym"," * FnArgDef)^0 * (sym"," * Ct(VarArg))^0 + Ct(VarArg), - FnArgDef = Ct((Name + SelfName) * (sym"=" * Exp)^-1), - - FunLit = FnArgsDef * - (sym"->" * Cc"slim" + sym"=>" * Cc"fat") * - (Body + Ct"") / mark"fndef", - - NameList = Name * (sym"," * Name)^0, - NameOrDestructure = Name + TableLit, - AssignableNameList = NameOrDestructure * (sym"," * NameOrDestructure)^0, - - ExpList = Exp * (sym"," * Exp)^0, - ExpListLow = Exp * ((sym"," + sym";") * Exp)^0, - - InvokeArgs = -P"-" * (ExpList * (sym"," * (TableBlock + SpaceBreak * Advance * ArgBlock * TableBlock^-1) + TableBlock)^-1 + TableBlock), - ArgBlock = ArgLine * (sym"," * SpaceBreak * ArgLine)^0 * PopIndent, - ArgLine = CheckIndent * ExpList - } - - return { - _g = White * g * White * -1, - match = function(self, str, ...) - - local pos_to_line = function(pos) - return util.pos_to_line(str, pos) - end - - local get_line = function(num) - return util.get_line(str, num) - end - - local tree - local parse_args = {...} - - local pass, err = xpcall(function() - tree = self._g:match(str, unpack(parse_args)) - end, function(err) - return debug.traceback(err, 2) - end) - - -- regular error, let it bubble up - if type(err) == "string" then - return nil, err - end - - if not tree then - local pos = last_pos - local msg - - if err then - local node - node, msg = unpack(err) - msg = msg and " " .. msg - pos = node[-1] - end - - local line_no = pos_to_line(pos) - local line_str = get_line(line_no) or "" - - return nil, err_msg:format(msg or "", line_no, trim(line_str)) - end - return tree - end - } + local _indent = Stack(0) + local _do_stack = Stack(0) + local last_pos = 0 + local check_indent + check_indent = function(str, pos, indent) + last_pos = pos + return _indent:top() == indent + end + local advance_indent + advance_indent = function(str, pos, indent) + local top = _indent:top() + if top ~= -1 and indent > top then + _indent:push(indent) + return true + end + end + local push_indent + push_indent = function(str, pos, indent) + _indent:push(indent) + return true + end + local pop_indent + pop_indent = function() + assert(_indent:pop(), "unexpected outdent") + return true + end + local check_do + check_do = function(str, pos, do_node) + local top = _do_stack:top() + if top == nil or top then + return true, do_node + end + return false + end + local disable_do + disable_do = function() + _do_stack:push(false) + return true + end + local pop_do + pop_do = function() + assert(_do_stack:pop() ~= nil, "unexpected do pop") + return true + end + local DisableDo = Cmt("", disable_do) + local PopDo = Cmt("", pop_do) + local keywords = { } + local key + key = function(chars) + keywords[chars] = true + return Space * chars * -AlphaNum + end + local op + op = function(chars) + local patt = Space * C(chars) + if chars:match("^%w*$") then + keywords[chars] = true + patt = patt * -AlphaNum + end + return patt + end + local Name = Cmt(SpaceName, function(str, pos, name) + if keywords[name] then + return false + end + return true + end) / trim + local SelfName = Space * "@" * ("@" * (_Name / mark("self_class") + Cc("self.__class")) + _Name / mark("self") + Cc("self")) + local KeyName = SelfName + Space * _Name / mark("key_literal") + local VarArg = Space * P("...") / trim + local g = P({ + File, + File = Shebang ^ -1 * (Block + Ct("")), + Block = Ct(Line * (Break ^ 1 * Line) ^ 0), + CheckIndent = Cmt(Indent, check_indent), + Line = (CheckIndent * Statement + Space * #Stop), + Statement = pos(Import + While + With + For + ForEach + Switch + Return + Local + Export + BreakLoop + Ct(ExpList) * (Update + Assign) ^ -1 / format_assign) * Space * ((key("if") * Exp * (key("else") * Exp) ^ -1 * Space / mark("if") + key("unless") * Exp / mark("unless") + CompInner / mark("comprehension")) * Space) ^ -1 / wrap_decorator, + Body = Space ^ -1 * Break * EmptyLine ^ 0 * InBlock + Ct(Statement), + Advance = #Cmt(Indent, advance_indent), + PushIndent = Cmt(Indent, push_indent), + PreventIndent = Cmt(Cc(-1), push_indent), + PopIndent = Cmt("", pop_indent), + InBlock = Advance * Block * PopIndent, + Local = key("local") * ((op("*") + op("^")) / mark("declare_glob") + Ct(NameList) / mark("declare_with_shadows")), + Import = key("import") * Ct(ImportNameList) * SpaceBreak ^ 0 * key("from") * Exp / mark("import"), + ImportName = (sym("\\") * Ct(Cc("colon_stub") * Name) + Name), + ImportNameList = SpaceBreak ^ 0 * ImportName * ((SpaceBreak ^ 1 + sym(",") * SpaceBreak ^ 0) * ImportName) ^ 0, + BreakLoop = Ct(key("break") / trim) + Ct(key("continue") / trim), + Return = key("return") * (ExpListLow / mark("explist") + C("")) / mark("return"), + WithExp = Ct(ExpList) * Assign ^ -1 / format_assign, + With = key("with") * DisableDo * ensure(WithExp, PopDo) * key("do") ^ -1 * Body / mark("with"), + Switch = key("switch") * DisableDo * ensure(Exp, PopDo) * key("do") ^ -1 * Space ^ -1 * Break * SwitchBlock / mark("switch"), + SwitchBlock = EmptyLine ^ 0 * Advance * Ct(SwitchCase * (Break ^ 1 * SwitchCase) ^ 0 * (Break ^ 1 * SwitchElse) ^ -1) * PopIndent, + SwitchCase = key("when") * Ct(ExpList) * key("then") ^ -1 * Body / mark("case"), + SwitchElse = key("else") * Body / mark("else"), + IfCond = Exp * Assign ^ -1 / format_single_assign, + If = key("if") * IfCond * key("then") ^ -1 * Body * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif")) ^ 0 * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else")) ^ -1 / mark("if"), + Unless = key("unless") * IfCond * key("then") ^ -1 * Body * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else")) ^ -1 / mark("unless"), + While = key("while") * DisableDo * ensure(Exp, PopDo) * key("do") ^ -1 * Body / mark("while"), + For = key("for") * DisableDo * ensure(Name * sym("=") * Ct(Exp * sym(",") * Exp * (sym(",") * Exp) ^ -1), PopDo) * key("do") ^ -1 * Body / mark("for"), + ForEach = key("for") * Ct(AssignableNameList) * key("in") * DisableDo * ensure(Ct(sym("*") * Exp / mark("unpack") + ExpList), PopDo) * key("do") ^ -1 * Body / mark("foreach"), + Do = key("do") * Body / mark("do"), + Comprehension = sym("[") * Exp * CompInner * sym("]") / mark("comprehension"), + TblComprehension = sym("{") * Ct(Exp * (sym(",") * Exp) ^ -1) * CompInner * sym("}") / mark("tblcomprehension"), + CompInner = Ct((CompForEach + CompFor) * CompClause ^ 0), + CompForEach = key("for") * Ct(NameList) * key("in") * (sym("*") * Exp / mark("unpack") + Exp) / mark("foreach"), + CompFor = key("for" * Name * sym("=") * Ct(Exp * sym(",") * Exp * (sym(",") * Exp) ^ -1) / mark("for")), + CompClause = CompFor + CompForEach + key("when") * Exp / mark("when"), + Assign = sym("=") * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark("assign"), + Update = ((sym("..=") + sym("+=") + sym("-=") + sym("*=") + sym("/=") + sym("%=") + sym("or=") + sym("and=")) / trim) * Exp / mark("update"), + CharOperators = Space * C(S("+-*/%^><")), + WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op(".."), + BinaryOperator = (WordOperators + CharOperators) * SpaceBreak ^ 0, + Assignable = Cmt(DotChain + Chain, check_assignable) + Name + SelfName, + Exp = Ct(Value * (BinaryOperator * Value) ^ 0) / flatten_or_mark("exp"), + SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num, + ChainValue = StringChain + ((Chain + DotChain + Callable) * Ct(InvokeArgs ^ -1)) / flatten_func, + Value = pos(SimpleValue + Ct(KeyValueList) / mark("table") + ChainValue), + SliceValue = SimpleValue + ChainValue, + StringChain = String * (Ct((ColonCall + ColonSuffix) * ChainTail ^ -1) * Ct(InvokeArgs ^ -1)) ^ -1 / flatten_string_chain, + String = Space * DoubleString + Space * SingleString + LuaString, + SingleString = simple_string("'"), + DoubleString = simple_string('"', true), + LuaString = Cg(LuaStringOpen, "string_open") * Cb("string_open") * Break ^ -1 * C((1 - Cmt(C(LuaStringClose) * Cb("string_open"), check_lua_string)) ^ 0) * LuaStringClose / mark("string"), + LuaStringOpen = sym("[") * P("=") ^ 0 * "[" / trim, + LuaStringClose = "]" * P("=") ^ 0 * "]", + Callable = pos(Name / mark("ref")) + SelfName + VarArg + Parens / mark("parens"), + Parens = sym("(") * SpaceBreak ^ 0 * Exp * SpaceBreak ^ 0 * sym(")"), + FnArgs = symx("(") * SpaceBreak ^ 0 * Ct(ExpList ^ -1) * SpaceBreak ^ 0 * sym(")") + sym("!") * -P("=") * Ct(""), + ChainTail = ChainItem ^ 1 * ColonSuffix ^ -1 + ColonSuffix, + Chain = Callable * ChainTail / mark("chain"), + DotChain = (sym(".") * Cc(-1) * (_Name / mark("dot")) * ChainTail ^ -1) / mark("chain") + (sym("\\") * Cc(-1) * ((_Name * Invoke / mark("colon")) * ChainTail ^ -1 + (_Name / mark("colon_stub")))) / mark("chain"), + ChainItem = Invoke + Slice + symx("[") * Exp / mark("index") * sym("]") + symx(".") * _Name / mark("dot") + ColonCall, + Slice = symx("[") * (SliceValue + Cc(1)) * sym(",") * (SliceValue + Cc("")) * (sym(",") * SliceValue) ^ -1 * sym("]") / mark("slice"), + ColonCall = symx("\\") * (_Name * Invoke) / mark("colon"), + ColonSuffix = symx("\\") * _Name / mark("colon_stub"), + Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg, + TableValue = KeyValue + Ct(Exp), + TableLit = sym("{") * Ct(TableValueList ^ -1 * sym(",") ^ -1 * (SpaceBreak * TableLitLine * (sym(",") ^ -1 * SpaceBreak * TableLitLine) ^ 0 * sym(",") ^ -1) ^ -1) * White * sym("}") / mark("table"), + TableValueList = TableValue * (sym(",") * TableValue) ^ 0, + TableLitLine = PushIndent * ((TableValueList * PopIndent) + (PopIndent * Cut)) + Space, + TableBlockInner = Ct(KeyValueLine * (SpaceBreak ^ 1 * KeyValueLine) ^ 0), + TableBlock = SpaceBreak ^ 1 * Advance * ensure(TableBlockInner, PopIndent) / mark("table"), + ClassDecl = key("class") * -P(":") * (Assignable + Cc(nil)) * (key("extends") * PreventIndent * ensure(Exp, PopIndent) + C("")) ^ -1 * (ClassBlock + Ct("")) / mark("class"), + ClassBlock = SpaceBreak ^ 1 * Advance * Ct(ClassLine * (SpaceBreak ^ 1 * ClassLine) ^ 0) * PopIndent, + ClassLine = CheckIndent * ((KeyValueList / mark("props") + Statement / mark("stm") + Exp / mark("stm")) * sym(",") ^ -1), + Export = key("export") * (Cc("class") * ClassDecl + op("*") + op("^") + Ct(NameList) * (sym("=") * Ct(ExpListLow)) ^ -1) / mark("export"), + KeyValue = (sym(":") * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym("[") * Exp * sym("]") + DoubleString + SingleString) * symx(":") * (Exp + TableBlock + SpaceBreak ^ 1 * Exp)), + KeyValueList = KeyValue * (sym(",") * KeyValue) ^ 0, + KeyValueLine = CheckIndent * KeyValueList * sym(",") ^ -1, + FnArgsDef = sym("(") * Ct(FnArgDefList ^ -1) * (key("using") * Ct(NameList + Space * "nil") + Ct("")) * sym(")") + Ct("") * Ct(""), + FnArgDefList = FnArgDef * (sym(",") * FnArgDef) ^ 0 * (sym(",") * Ct(VarArg)) ^ 0 + Ct(VarArg), + FnArgDef = Ct((Name + SelfName) * (sym("=") * Exp) ^ -1), + FunLit = FnArgsDef * (sym("->") * Cc("slim") + sym("=>") * Cc("fat")) * (Body + Ct("")) / mark("fndef"), + NameList = Name * (sym(",") * Name) ^ 0, + NameOrDestructure = Name + TableLit, + AssignableNameList = NameOrDestructure * (sym(",") * NameOrDestructure) ^ 0, + ExpList = Exp * (sym(",") * Exp) ^ 0, + ExpListLow = Exp * ((sym(",") + sym(";")) * Exp) ^ 0, + InvokeArgs = -P("-") * (ExpList * (sym(",") * (TableBlock + SpaceBreak * Advance * ArgBlock * TableBlock ^ -1) + TableBlock) ^ -1 + TableBlock), + ArgBlock = ArgLine * (sym(",") * SpaceBreak * ArgLine) ^ 0 * PopIndent, + ArgLine = CheckIndent * ExpList + }) + local file_grammar = White * g * White * -1 + return { + match = function(self, str) + local tree + local _, err = xpcall((function() + tree = file_grammar:match(str) + end), function(err) + return debug.traceback(err, 2) + end) + if type(err) == "string" then + return nil, err + end + if not (tree) then + local msg + pos = last_pos + if err then + local node + node, msg = unpack(err) + if msg then + msg = " " .. msg + end + pos = node[-1] + end + local line_no = pos_to_line(str, pos) + local line_str = get_line(str, line_no) or "" + return nil, err_msg:format(msg or "", line_no, trim(line_str)) + end + return tree + end + } end) - return { - extract_line = extract_line, - - -- parse a string - -- returns tree, or nil and error message - string = function (str) - local g = build_grammar() - return g:match(str) - end + extract_line = extract_line, + string = function(str) + return build_grammar():match(str) + end } - diff --git a/moonscript/parse.moon b/moonscript/parse.moon new file mode 100644 index 00000000..a58d9cb2 --- /dev/null +++ b/moonscript/parse.moon @@ -0,0 +1,355 @@ +debug_grammar = false +lpeg = require "lpeg" + +lpeg.setmaxstack 10000 -- whoa + +err_msg = "Failed to parse:%s\n [%d] >> %s" + +import Stack from require "moonscript.data" +import trim, pos_to_line, get_line from require"moonscript.util" +import unpack from require "moonscript.util" +import wrap_env from require "moonscript.parse.env" + +{ + :R, :S, :V, :P, :C, :Ct, :Cmt, :Cg, :Cb, :Cc +} = lpeg + +{ + :White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine, + :AlphaNum, :Num, :Shebang + Name: _Name +} = require "moonscript.parse.literals" + + +SpaceName = Space * _Name +Num = Space * (Num / (v) -> {"number", v}) + +{ + :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, + :is_assignable, :check_assignable, :format_assign, :format_single_assign, + :sym, :symx, :simple_string, :wrap_func_arg, :flatten_func, + :flatten_string_chain, :wrap_decorator, :check_lua_string, :self_assign +} = require "moonscript.parse.util" + + +build_grammar = wrap_env debug_grammar, -> + _indent = Stack 0 + _do_stack = Stack 0 + + last_pos = 0 -- last pos we saw, used to report error location + + check_indent = (str, pos, indent) -> + last_pos = pos + _indent\top! == indent + + advance_indent = (str, pos, indent) -> + top = _indent\top! + if top != -1 and indent > top + _indent\push indent + true + + push_indent = (str, pos, indent) -> + _indent\push indent + true + + pop_indent = -> + assert _indent\pop!, "unexpected outdent" + true + + check_do = (str, pos, do_node) -> + top = _do_stack\top! + if top == nil or top + return true, do_node + false + + disable_do = -> + _do_stack\push false + true + + pop_do = -> + assert _do_stack\pop! != nil, "unexpected do pop" + true + + DisableDo = Cmt "", disable_do + PopDo = Cmt "", pop_do + + keywords = {} + key = (chars) -> + keywords[chars] = true + Space * chars * -AlphaNum + + op = (chars) -> + patt = Space * C chars + -- it's a word, treat like keyword + if chars\match "^%w*$" + keywords[chars] = true + patt *= -AlphaNum + + patt + + Name = Cmt(SpaceName, (str, pos, name) -> + return false if keywords[name] + true + ) / trim + + SelfName = Space * "@" * ( + "@" * (_Name / mark"self_class" + Cc"self.__class") + + _Name / mark"self" + + Cc"self" -- @ by itself + ) + + KeyName = SelfName + Space * _Name / mark"key_literal" + VarArg = Space * P"..." / trim + + g = P { + File + File: Shebang^-1 * (Block + Ct"") + Block: Ct(Line * (Break^1 * Line)^0) + CheckIndent: Cmt(Indent, check_indent), -- validates line is in correct indent + Line: (CheckIndent * Statement + Space * #Stop) + + Statement: pos( + Import + While + With + For + ForEach + Switch + Return + + Local + Export + BreakLoop + + Ct(ExpList) * (Update + Assign)^-1 / format_assign + ) * Space * (( + -- statement decorators + key"if" * Exp * (key"else" * Exp)^-1 * Space / mark"if" + + key"unless" * Exp / mark"unless" + + CompInner / mark"comprehension" + ) * Space)^-1 / wrap_decorator + + Body: Space^-1 * Break * EmptyLine^0 * InBlock + Ct(Statement), -- either a statement, or an indented block + + Advance: #Cmt(Indent, advance_indent), -- Advances the indent, gives back whitespace for CheckIndent + PushIndent: Cmt(Indent, push_indent) + PreventIndent: Cmt(Cc(-1), push_indent) + PopIndent: Cmt("", pop_indent) + InBlock: Advance * Block * PopIndent + + Local: key"local" * ((op"*" + op"^") / mark"declare_glob" + Ct(NameList) / mark"declare_with_shadows") + + Import: key"import" * Ct(ImportNameList) * SpaceBreak^0 * key"from" * Exp / mark"import" + ImportName: (sym"\\" * Ct(Cc"colon_stub" * Name) + Name) + ImportNameList: SpaceBreak^0 * ImportName * ((SpaceBreak^1 + sym"," * SpaceBreak^0) * ImportName)^0 + + BreakLoop: Ct(key"break"/trim) + Ct(key"continue"/trim) + + Return: key"return" * (ExpListLow/mark"explist" + C"") / mark"return" + + WithExp: Ct(ExpList) * Assign^-1 / format_assign + With: key"with" * DisableDo * ensure(WithExp, PopDo) * key"do"^-1 * Body / mark"with" + + Switch: key"switch" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Space^-1 * Break * SwitchBlock / mark"switch" + + SwitchBlock: EmptyLine^0 * Advance * Ct(SwitchCase * (Break^1 * SwitchCase)^0 * (Break^1 * SwitchElse)^-1) * PopIndent + SwitchCase: key"when" * Ct(ExpList) * key"then"^-1 * Body / mark"case" + SwitchElse: key"else" * Body / mark"else" + + IfCond: Exp * Assign^-1 / format_single_assign + + If: key"if" * IfCond * key"then"^-1 * Body * + ((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif")^0 * + ((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if" + + Unless: key"unless" * IfCond * key"then"^-1 * Body * + ((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"unless" + + While: key"while" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Body / mark"while" + + For: key"for" * DisableDo * ensure(Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1), PopDo) * + key"do"^-1 * Body / mark"for" + + ForEach: key"for" * Ct(AssignableNameList) * key"in" * DisableDo * ensure(Ct(sym"*" * Exp / mark"unpack" + ExpList), PopDo) * key"do"^-1 * Body / mark"foreach" + + Do: key"do" * Body / mark"do" + + Comprehension: sym"[" * Exp * CompInner * sym"]" / mark"comprehension" + + TblComprehension: sym"{" * Ct(Exp * (sym"," * Exp)^-1) * CompInner * sym"}" / mark"tblcomprehension" + + CompInner: Ct((CompForEach + CompFor) * CompClause^0) + CompForEach: key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"foreach" + CompFor: key "for" * Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1) / mark"for" + CompClause: CompFor + CompForEach + key"when" * Exp / mark"when" + + Assign: sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign" + Update: ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update" + + CharOperators: Space * C(S"+-*/%^><") + WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + BinaryOperator: (WordOperators + CharOperators) * SpaceBreak^0 + + Assignable: Cmt(DotChain + Chain, check_assignable) + Name + SelfName + Exp: Ct(Value * (BinaryOperator * Value)^0) / flatten_or_mark"exp" + + SimpleValue: + If + Unless + + Switch + + With + + ClassDecl + + ForEach + For + While + + Cmt(Do, check_do) + + sym"-" * -SomeSpace * Exp / mark"minus" + + sym"#" * Exp / mark"length" + + key"not" * Exp / mark"not" + + TblComprehension + + TableLit + + Comprehension + + FunLit + + Num + + ChainValue: -- a function call or an object access + StringChain + + ((Chain + DotChain + Callable) * Ct(InvokeArgs^-1)) / flatten_func + + Value: pos( + SimpleValue + + Ct(KeyValueList) / mark"table" + + ChainValue) + + SliceValue: SimpleValue + ChainValue + + StringChain: String * + (Ct((ColonCall + ColonSuffix) * ChainTail^-1) * Ct(InvokeArgs^-1))^-1 / flatten_string_chain + + String: Space * DoubleString + Space * SingleString + LuaString + SingleString: simple_string("'") + DoubleString: simple_string('"', true) + + LuaString: Cg(LuaStringOpen, "string_open") * Cb"string_open" * Break^-1 * + C((1 - Cmt(C(LuaStringClose) * Cb"string_open", check_lua_string))^0) * + LuaStringClose / mark"string" + + LuaStringOpen: sym"[" * P"="^0 * "[" / trim + LuaStringClose: "]" * P"="^0 * "]" + + Callable: pos(Name / mark"ref") + SelfName + VarArg + Parens / mark"parens" + Parens: sym"(" * SpaceBreak^0 * Exp * SpaceBreak^0 * sym")" + + FnArgs: symx"(" * SpaceBreak^0 * Ct(ExpList^-1) * SpaceBreak^0 * sym")" + sym"!" * -P"=" * Ct"" + + ChainTail: ChainItem^1 * ColonSuffix^-1 + ColonSuffix + + -- a list of funcalls and indexes on a callable + Chain: Callable * ChainTail / mark"chain" + + -- shorthand dot call for use in with statement + DotChain: + (sym"." * Cc(-1) * (_Name / mark"dot") * ChainTail^-1) / mark"chain" + + (sym"\\" * Cc(-1) * ( + (_Name * Invoke / mark"colon") * ChainTail^-1 + + (_Name / mark"colon_stub") + )) / mark"chain" + + ChainItem: + Invoke + + Slice + + symx"[" * Exp/mark"index" * sym"]" + + symx"." * _Name/mark"dot" + + ColonCall + + Slice: symx"[" * (SliceValue + Cc(1)) * sym"," * (SliceValue + Cc"") * + (sym"," * SliceValue)^-1 *sym"]" / mark"slice" + + ColonCall: symx"\\" * (_Name * Invoke) / mark"colon" + ColonSuffix: symx"\\" * _Name / mark"colon_stub" + + Invoke: FnArgs/mark"call" + + SingleString / wrap_func_arg + + DoubleString / wrap_func_arg + + TableValue: KeyValue + Ct(Exp) + + TableLit: sym"{" * Ct( + TableValueList^-1 * sym","^-1 * + (SpaceBreak * TableLitLine * (sym","^-1 * SpaceBreak * TableLitLine)^0 * sym","^-1)^-1 + ) * White * sym"}" / mark"table" + + TableValueList: TableValue * (sym"," * TableValue)^0 + TableLitLine: PushIndent * ((TableValueList * PopIndent) + (PopIndent * Cut)) + Space + + -- the unbounded table + TableBlockInner: Ct(KeyValueLine * (SpaceBreak^1 * KeyValueLine)^0) + TableBlock: SpaceBreak^1 * Advance * ensure(TableBlockInner, PopIndent) / mark"table" + + ClassDecl: key"class" * -P":" * (Assignable + Cc(nil)) * (key"extends" * PreventIndent * ensure(Exp, PopIndent) + C"")^-1 * (ClassBlock + Ct("")) / mark"class" + + ClassBlock: SpaceBreak^1 * Advance * + Ct(ClassLine * (SpaceBreak^1 * ClassLine)^0) * PopIndent + ClassLine: CheckIndent * (( + KeyValueList / mark"props" + + Statement / mark"stm" + + Exp / mark"stm" + ) * sym","^-1) + + Export: key"export" * ( + Cc"class" * ClassDecl + + op"*" + op"^" + + Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export" + + KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)) + KeyValueList: KeyValue * (sym"," * KeyValue)^0 + KeyValueLine: CheckIndent * KeyValueList * sym","^-1 + + FnArgsDef: sym"(" * Ct(FnArgDefList^-1) * + (key"using" * Ct(NameList + Space * "nil") + Ct"") * + sym")" + Ct"" * Ct"" + + FnArgDefList: FnArgDef * (sym"," * FnArgDef)^0 * (sym"," * Ct(VarArg))^0 + Ct(VarArg) + FnArgDef: Ct((Name + SelfName) * (sym"=" * Exp)^-1) + + FunLit: FnArgsDef * + (sym"->" * Cc"slim" + sym"=>" * Cc"fat") * + (Body + Ct"") / mark"fndef" + + NameList: Name * (sym"," * Name)^0 + NameOrDestructure: Name + TableLit + AssignableNameList: NameOrDestructure * (sym"," * NameOrDestructure)^0 + + ExpList: Exp * (sym"," * Exp)^0 + ExpListLow: Exp * ((sym"," + sym";") * Exp)^0 + + InvokeArgs: -P"-" * (ExpList * (sym"," * (TableBlock + SpaceBreak * Advance * ArgBlock * TableBlock^-1) + TableBlock)^-1 + TableBlock) + ArgBlock: ArgLine * (sym"," * SpaceBreak * ArgLine)^0 * PopIndent + ArgLine: CheckIndent * ExpList + } + + file_grammar = White * g * White * -1 + + { + match: (str) => + local tree + _, err = xpcall (-> + tree = file_grammar\match str + ), (err) -> + debug.traceback err, 2 + + -- regular error, let it bubble up + if type(err) == "string" + return nil, err + + unless tree + local msg + pos = last_pos + + if err + node, msg = unpack err + msg = " " .. msg if msg + pos = node[-1] + + line_no = pos_to_line str, pos + line_str = get_line(str, line_no) or "" + return nil, err_msg\format msg or "", line_no, trim line_str + + tree + } + +{ + extract_line: extract_line, + + -- parse a string + -- returns tree, or nil and error message + string: (str) -> build_grammar!\match str +} + From eb42898b5b42b0150053b93d9b891672b2c7e6ac Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 17:04:43 -0800 Subject: [PATCH 096/344] update rockspec, fixes #161 --- moonscript-dev-1.rockspec | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index f1b4a1c5..00dd0544 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -7,6 +7,7 @@ source = { description = { summary = "A programmer friendly language that compiles to Lua", + detailed = "A programmer friendly language that compiles to Lua", homepage = "http://moonscript.org", maintainer = "Leaf Corcoran ", license = "MIT" @@ -37,6 +38,9 @@ build = { ["moonscript.errors"] = "moonscript/errors.lua", ["moonscript.line_tables"] = "moonscript/line_tables.lua", ["moonscript.parse"] = "moonscript/parse.lua", + ["moonscript.parse.env"] = "moonscript/parse/env.lua", + ["moonscript.parse.literals"] = "moonscript/parse/literals.lua", + ["moonscript.parse.util"] = "moonscript/parse/util.lua", ["moonscript.transform"] = "moonscript/transform.lua", ["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua", ["moonscript.transform.names"] = "moonscript/transform/names.lua", From 9ae7df38dfcf44c3ff4f642e928e67e2f5b8ca65 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 17:07:12 -0800 Subject: [PATCH 097/344] fix typo in docs #164 --- docs/standard_lib.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/standard_lib.md b/docs/standard_lib.md index f2686081..42cb7d4a 100644 --- a/docs/standard_lib.md +++ b/docs/standard_lib.md @@ -11,7 +11,7 @@ The entire library is currently contained in a single object. We can bring this `moon` object into scope by requiring `"moon"`. ```moon -require "moon" +moon = require "moon" -- `moon.p` is the debug printer moon.p { hello: "world" } ``` From f7beade09cfb522a4c7abc6c699899b564b4767e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 17:28:56 -0800 Subject: [PATCH 098/344] remove V cache #150 --- moonscript/parse/env.lua | 1 - moonscript/parse/env.moon | 1 - 2 files changed, 2 deletions(-) diff --git a/moonscript/parse/env.lua b/moonscript/parse/env.lua index 4a0adc33..6dbed84a 100644 --- a/moonscript/parse/env.lua +++ b/moonscript/parse/env.lua @@ -58,7 +58,6 @@ wrap_env = function(debug, fn) end if name:match("^[A-Z][A-Za-z0-9]*$") then local v = wrap_name(name) - rawset(self, name, v) return v end return error("unknown variable referenced: " .. tostring(name)) diff --git a/moonscript/parse/env.moon b/moonscript/parse/env.moon index 594d3e59..25dcc1e4 100644 --- a/moonscript/parse/env.moon +++ b/moonscript/parse/env.moon @@ -41,7 +41,6 @@ wrap_env = (debug, fn) -> if name\match"^[A-Z][A-Za-z0-9]*$" v = wrap_name name - rawset @, name, v return v error "unknown variable referenced: #{name}" From efc4d3dd0f29070ef3b5bd4867f8cf3f444df078 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 18:47:27 -0800 Subject: [PATCH 099/344] update version --- moonscript/version.lua | 2 +- moonscript/version.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/version.lua b/moonscript/version.lua index ff223d3a..0d0b6f0f 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,4 +1,4 @@ -local version = "0.2.6" +local version = "0.3.0" return { version = version, print_version = function() diff --git a/moonscript/version.moon b/moonscript/version.moon index 156e240b..426abbb2 100644 --- a/moonscript/version.moon +++ b/moonscript/version.moon @@ -1,5 +1,5 @@ -version = "0.2.6" +version = "0.3.0" { version: version, From 2c44273b7776b73483277b24eda1e9edb0f388eb Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 18:52:15 -0800 Subject: [PATCH 100/344] update changelog --- CHANGELOG.md | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91e400eb..174b217c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,71 @@ +# MoonScript v0.3.0 (2015-2-28) + +## New Features + +* New [unused assignment linter](http://moonscript.org/reference/command_line.html#unused_variable_assigns) finds assignments that are never referenced after being defined. + +## Parsing Updates + +Whitespace parsing has been relaxed in a handful of locations: + +* You can put unrestricted whitespace/newlines after operator in a binary operator before writing the right hand side. The following are now valid: + +```moonscript +x = really_long_function! + + 2304 + +big_math = 123 / + 12 - + 43 * 17 + + +bool_exp = nice_shirt and cool_shoes or + skateboard and shades +``` + +* You can put unrestricted whitespace/newlines immediately after an opening parenthesis, and immediately before closing parenthesis. The following are now valid: + +```moonscript +hello = 100 + ( + var * 0.23 +) - 15 + + +funcall( + "height", "age", "weight" +) + + +takes_two_functions (-> + print "hello" +), -> + print "world" +``` + +* You can put unrestricted whitespace/newlines immediately after a `:` when defining a table literal. The following is now valid: + +```moonscript +x = { + hello: + call_a_function "football", "hut" +} +``` + +## Code Generation + +* Single value `import`/`destructure` compiles directly into single assignment + +## Bug Fixes + +* Some `moonc` command line flags were being ignored +* Linter would not report global reference when inside self assign in table +* Fixed an issue where parser would crash in Lpeg 0.12 when compiling hundreds of times per process + +## Misc + +* MoonScript parser now written in MoonScript + # MoonScript v0.2.6 (2014-6-18) ## Bug Fixes From f1b7f925074e54761968a12e783685246e7bff1a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 28 Feb 2015 19:13:16 -0800 Subject: [PATCH 101/344] fix codeblock language --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 174b217c..c6b48044 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -142,7 +142,7 @@ where return can be cascaded into the body. **Before:** -```moonscript +```lua local _ _ = function() if test1 then @@ -173,7 +173,7 @@ end **After:** -```moonscript +```lua local _ _ = function() if test1 then From 50bf265e7359a681bd18a7cfcfd57f2b49a6e135 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 6 Mar 2015 09:58:13 -0800 Subject: [PATCH 102/344] bad local variable, fix #177 --- moonscript/parse.lua | 6 +++--- moonscript/parse.moon | 6 +++--- spec/error_rewriting_spec.moon | 8 ++++++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 12f9062e..62044b7a 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -210,16 +210,16 @@ local build_grammar = wrap_env(debug_grammar, function() end if not (tree) then local msg - pos = last_pos + local err_pos = last_pos if err then local node node, msg = unpack(err) if msg then msg = " " .. msg end - pos = node[-1] + err_pos = node[-1] end - local line_no = pos_to_line(str, pos) + local line_no = pos_to_line(str, err_pos) local line_str = get_line(str, line_no) or "" return nil, err_msg:format(msg or "", line_no, trim(line_str)) end diff --git a/moonscript/parse.moon b/moonscript/parse.moon index a58d9cb2..3fb6bfa5 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -331,14 +331,14 @@ build_grammar = wrap_env debug_grammar, -> unless tree local msg - pos = last_pos + err_pos = last_pos if err node, msg = unpack err msg = " " .. msg if msg - pos = node[-1] + err_pos = node[-1] - line_no = pos_to_line str, pos + line_no = pos_to_line str, err_pos line_str = get_line(str, line_no) or "" return nil, err_msg\format msg or "", line_no, trim line_str diff --git a/spec/error_rewriting_spec.moon b/spec/error_rewriting_spec.moon index dfaab1b2..9978d1a2 100644 --- a/spec/error_rewriting_spec.moon +++ b/spec/error_rewriting_spec.moon @@ -60,4 +60,12 @@ describe "line map", -> -- print util.debug_posmap(posmap, moon_code, lua_code) assert.same {[1]: 7, [2]: 19, [7]: 19, [8]: 63}, posmap +describe "error reporting", -> + import to_lua from require "moonscript.base" + it "should compile bad code twice", -> + code, err = to_lua "{b=5}" + assert.truthy err + code, err2 = to_lua "{b=5}" + assert.same err, err2 + From fd278b8ccad6689014ad297f56713f411530fdd8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 7 Mar 2015 08:19:10 -0800 Subject: [PATCH 103/344] version bump --- moonscript/version.lua | 2 +- moonscript/version.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/version.lua b/moonscript/version.lua index 0d0b6f0f..96e2ec83 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,4 +1,4 @@ -local version = "0.3.0" +local version = "0.3.1" return { version = version, print_version = function() diff --git a/moonscript/version.moon b/moonscript/version.moon index 426abbb2..6e909479 100644 --- a/moonscript/version.moon +++ b/moonscript/version.moon @@ -1,5 +1,5 @@ -version = "0.3.0" +version = "0.3.1" { version: version, From 640d30f80d0b6f2a2795f17ccbad1c8ac46dcb3e Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 12 Mar 2015 11:09:17 +0300 Subject: [PATCH 104/344] fix crash in moonc on Lua 5.3 --- bin/moonc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index bc8ed20b..5de57204 100755 --- a/bin/moonc +++ b/bin/moonc @@ -54,6 +54,7 @@ function log_msg(...) end local moonc = require("moonscript.cmd.moonc") +local util = require "moonscript.util" local mkdir = moonc.mkdir local normalize_dir = moonc.normalize_dir local parse_dir = moonc.parse_dir @@ -309,7 +310,7 @@ elseif opts.l then end else for _, tuple in ipairs(files) do - local fname, target = unpack(tuple) + local fname, target = util.unpack(tuple) if opts.o then target = opts.o end From 13dda4e9ba26d44576c26a7043ad6a913be655a4 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 12 Mar 2015 11:22:18 +0300 Subject: [PATCH 105/344] Upgrade to busted 2.0.rc7 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 346cc94e..55a8e747 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ install: - sudo apt-get install luarocks - mkdir -p ~/.luarocks - echo 'rocks_servers = { "http://rocks.moonscript.org" }' > ~/.luarocks/config.lua - - sudo luarocks install https://rocks.moonscript.org/manifests/olivine-labs/busted-1.11.1-1.rockspec + - sudo luarocks install https://rocks.moonscript.org/manifests/olivine-labs/busted-2.0.rc7-0.rockspec - sudo luarocks make script: busted From 28eff692d99057286864e8c217a46c118a56643c Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 16 Mar 2015 21:46:57 -0700 Subject: [PATCH 106/344] update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6b48044..e866ee25 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ +# MoonScript v0.3.1 (2015-3-07) + +## Bug Fixes + +* Fixed a bug where an error from a previous compile would prevent the compiler from running again + # MoonScript v0.3.0 (2015-2-28) ## New Features From f08477d3d6e7d2807c28fb70746351979745c505 Mon Sep 17 00:00:00 2001 From: mpeterv Date: Thu, 12 Mar 2015 20:28:16 +0300 Subject: [PATCH 107/344] fix package.moonpath generation Do not use paths from package.path which do not end in .lua --- moonscript/base.lua | 10 ++++++---- moonscript/base.moon | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/moonscript/base.lua b/moonscript/base.lua index 9c74de4b..379b6bc7 100644 --- a/moonscript/base.lua +++ b/moonscript/base.lua @@ -18,14 +18,16 @@ local dirsep, line_tables, create_moonpath, to_lua, moon_loader, loadstring, loa dirsep = "/" line_tables = require("moonscript.line_tables") create_moonpath = function(package_path) - local paths = split(package_path, ";") - for i, path in ipairs(paths) do + local moonpaths = { } + local _list_0 = split(package.path, ";") + for _index_0 = 1, #_list_0 do + local path = _list_0[_index_0] local p = path:match("^(.-)%.lua$") if p then - paths[i] = p .. ".moon" + insert(moonpaths, p .. ".moon") end end - return concat(paths, ";") + return concat(moonpaths, ";") end to_lua = function(text, options) if options == nil then diff --git a/moonscript/base.moon b/moonscript/base.moon index 371628cc..8cfbfb3f 100644 --- a/moonscript/base.moon +++ b/moonscript/base.moon @@ -13,11 +13,11 @@ line_tables = require "moonscript.line_tables" -- create moon path package from lua package path create_moonpath = (package_path) -> - paths = split package_path, ";" - for i, path in ipairs paths + moonpaths = {} + for path in *split package.path, ";" p = path\match "^(.-)%.lua$" - if p then paths[i] = p..".moon" - concat paths, ";" + if p then insert moonpaths, p..".moon" + concat moonpaths, ";" to_lua = (text, options={}) -> if "string" != type text From 0ccd5e3ca4addb1c722aa3833b27d070808d5bf9 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 16 Mar 2015 23:54:33 -0700 Subject: [PATCH 108/344] restore argument, make more moonscripty, add spec #176 --- moonscript/base.lua | 33 +++++++++++++++++++++++++-------- moonscript/base.moon | 10 +++++----- spec/moonscript_spec.moon | 8 ++++++++ 3 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 spec/moonscript_spec.moon diff --git a/moonscript/base.lua b/moonscript/base.lua index 379b6bc7..e436678a 100644 --- a/moonscript/base.lua +++ b/moonscript/base.lua @@ -18,14 +18,30 @@ local dirsep, line_tables, create_moonpath, to_lua, moon_loader, loadstring, loa dirsep = "/" line_tables = require("moonscript.line_tables") create_moonpath = function(package_path) - local moonpaths = { } - local _list_0 = split(package.path, ";") - for _index_0 = 1, #_list_0 do - local path = _list_0[_index_0] - local p = path:match("^(.-)%.lua$") - if p then - insert(moonpaths, p .. ".moon") + local moonpaths + do + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = split(package_path, ";") + for _index_0 = 1, #_list_0 do + local _continue_0 = false + repeat + local path = _list_0[_index_0] + local prefix = path:match("^(.-)%.lua$") + if not (prefix) then + _continue_0 = true + break + end + local _value_0 = prefix .. ".moon" + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + _continue_0 = true + until true + if not _continue_0 then + break + end end + moonpaths = _accum_0 end return concat(moonpaths, ";") end @@ -134,5 +150,6 @@ return { dirsep = dirsep, dofile = dofile, loadfile = loadfile, - loadstring = loadstring + loadstring = loadstring, + create_moonpath = create_moonpath } diff --git a/moonscript/base.moon b/moonscript/base.moon index 8cfbfb3f..b2068f0b 100644 --- a/moonscript/base.moon +++ b/moonscript/base.moon @@ -13,10 +13,10 @@ line_tables = require "moonscript.line_tables" -- create moon path package from lua package path create_moonpath = (package_path) -> - moonpaths = {} - for path in *split package.path, ";" - p = path\match "^(.-)%.lua$" - if p then insert moonpaths, p..".moon" + moonpaths = for path in *split package_path, ";" + prefix = path\match "^(.-)%.lua$" + continue unless prefix + prefix .. ".moon" concat moonpaths, ";" to_lua = (text, options={}) -> @@ -103,6 +103,6 @@ remove_loader = -> { _NAME: "moonscript" :insert_loader, :remove_loader, :to_lua, :moon_loader, :dirsep, - :dofile, :loadfile, :loadstring + :dofile, :loadfile, :loadstring, :create_moonpath } diff --git a/spec/moonscript_spec.moon b/spec/moonscript_spec.moon new file mode 100644 index 00000000..8db55585 --- /dev/null +++ b/spec/moonscript_spec.moon @@ -0,0 +1,8 @@ +-- moonscript module + +describe "moonscript.base", -> + it "should create moonpath", -> + path = ";./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib/lua/5.1/?.luac;/home/leafo/.luarocks/lua/5.1/?.lua" + import create_moonpath from require "moonscript.base" + assert.same "./?.moon;/usr/share/lua/5.1/?.moon;/usr/share/lua/5.1/?/init.moon;/home/leafo/.luarocks/lua/5.1/?.moon", create_moonpath(path) + From fb791ed4027274937e1af78df084d9b636e1c6e1 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 1 Jun 2015 22:32:36 -0700 Subject: [PATCH 109/344] version bump --- CHANGELOG.md | 6 ++++++ moonscript/version.lua | 2 +- moonscript/version.moon | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e866ee25..b78411af 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,10 @@ +# MoonScript v0.3.2 (2015-6-01) + +## Bug Fixes + +* `package.moonpath` geneator does not use paths that don't end in `lua` + # MoonScript v0.3.1 (2015-3-07) ## Bug Fixes diff --git a/moonscript/version.lua b/moonscript/version.lua index 96e2ec83..352d7a98 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,4 +1,4 @@ -local version = "0.3.1" +local version = "0.3.2" return { version = version, print_version = function() diff --git a/moonscript/version.moon b/moonscript/version.moon index 6e909479..018d1c8c 100644 --- a/moonscript/version.moon +++ b/moonscript/version.moon @@ -1,5 +1,5 @@ -version = "0.3.1" +version = "0.3.2" { version: version, From e46fb70d700a7d77f616d09025bc6e4cf6813626 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 8 Jun 2015 21:07:23 -0700 Subject: [PATCH 110/344] expose api for building custom grammars --- moonscript/parse.lua | 15 ++++++++++----- moonscript/parse.moon | 15 +++++++++------ 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 62044b7a..1ae66f32 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -32,7 +32,7 @@ do local _obj_0 = require("moonscript.parse.util") Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, flatten_func, flatten_string_chain, wrap_decorator, check_lua_string, self_assign = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.flatten_func, _obj_0.flatten_string_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign end -local build_grammar = wrap_env(debug_grammar, function() +local build_grammar = wrap_env(debug_grammar, function(root) local _indent = Stack(0) local _do_stack = Stack(0) local last_pos = 0 @@ -103,8 +103,8 @@ local build_grammar = wrap_env(debug_grammar, function() local SelfName = Space * "@" * ("@" * (_Name / mark("self_class") + Cc("self.__class")) + _Name / mark("self") + Cc("self")) local KeyName = SelfName + Space * _Name / mark("key_literal") local VarArg = Space * P("...") / trim - local g = P({ - File, + return P({ + root or File, File = Shebang ^ -1 * (Block + Ct("")), Block = Ct(Line * (Break ^ 1 * Line) ^ 0), CheckIndent = Cmt(Indent, check_indent), @@ -196,6 +196,10 @@ local build_grammar = wrap_env(debug_grammar, function() ArgBlock = ArgLine * (sym(",") * SpaceBreak * ArgLine) ^ 0 * PopIndent, ArgLine = CheckIndent * ExpList }) +end) +local file_parser +file_parser = function() + local g = build_grammar() local file_grammar = White * g * White * -1 return { match = function(self, str) @@ -226,10 +230,11 @@ local build_grammar = wrap_env(debug_grammar, function() return tree end } -end) +end return { extract_line = extract_line, + build_grammar = build_grammar, string = function(str) - return build_grammar():match(str) + return file_parser():match(str) end } diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 3fb6bfa5..5c0885e8 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -32,7 +32,7 @@ Num = Space * (Num / (v) -> {"number", v}) } = require "moonscript.parse.util" -build_grammar = wrap_env debug_grammar, -> +build_grammar = wrap_env debug_grammar, (root) -> _indent = Stack 0 _do_stack = Stack 0 @@ -101,8 +101,8 @@ build_grammar = wrap_env debug_grammar, -> KeyName = SelfName + Space * _Name / mark"key_literal" VarArg = Space * P"..." / trim - g = P { - File + P { + root or File File: Shebang^-1 * (Block + Ct"") Block: Ct(Line * (Break^1 * Line)^0) CheckIndent: Cmt(Indent, check_indent), -- validates line is in correct indent @@ -315,6 +315,8 @@ build_grammar = wrap_env debug_grammar, -> ArgLine: CheckIndent * ExpList } +file_parser = -> + g = build_grammar! file_grammar = White * g * White * -1 { @@ -346,10 +348,11 @@ build_grammar = wrap_env debug_grammar, -> } { - extract_line: extract_line, + :extract_line + :build_grammar - -- parse a string + -- parse a string as a file -- returns tree, or nil and error message - string: (str) -> build_grammar!\match str + string: (str) -> file_parser!\match str } From 32239c8455611bf8e9d69f87c894bd34427bd3ca Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 8 Jun 2015 21:46:28 -0700 Subject: [PATCH 111/344] rewrite bin/moon in moonscript --- Makefile | 3 + bin/moon | 214 ++++++++++++++++++++++++++------------------------ bin/moon.moon | 116 +++++++++++++++++++++++++++ 3 files changed, 229 insertions(+), 104 deletions(-) create mode 100644 bin/moon.moon diff --git a/Makefile b/Makefile index 9a44cfec..13a1eba8 100644 --- a/Makefile +++ b/Makefile @@ -14,6 +14,9 @@ global: compile: lua5.1 bin/moonc moon/ moonscript/ + echo "#!/usr/bin/env lua" > bin/moon + lua5.1 bin/moonc -p bin/moon.moon >> bin/moon + echo "-- vim: set filetype=lua:" >> bin/moon watch: moonc moon/ moonscript/ && moonc -w moon/ moonscript/ diff --git a/bin/moon b/bin/moon index b6ef27fe..a4892bd3 100755 --- a/bin/moon +++ b/bin/moon @@ -1,16 +1,13 @@ #!/usr/bin/env lua - -require "alt_getopt" -local moonscript = require "moonscript.base" - -local util = require "moonscript.util" -local errors = require "moonscript.errors" - +require("alt_getopt") +local moonscript = require("moonscript.base") +local util = require("moonscript.util") +local errors = require("moonscript.errors") local unpack = util.unpack - --- moonloader and repl -local opts, ind = alt_getopt.get_opts(arg, "cvhd", { version = "v", help = "h" }) - +local opts, ind = alt_getopt.get_opts(arg, "cvhd", { + version = "v", + help = "h" +}) local help = [=[Usage: %s [options] [script [args]] -h Print this message @@ -18,99 +15,108 @@ local help = [=[Usage: %s [options] [script [args]] -c Collect and print code coverage -v Print version ]=] - -local function print_err(...) - local msg = table.concat({...}, "\t") - io.stderr:write(msg .. "\n") -end - -local function print_help(err) - if err then print("Error: "..err) end - print(help:format(arg[0])) - os.exit() -end - -if opts.h then print_help() end - -if opts.v then - local v = require "moonscript.version" - v.print_version() - os.exit() -end - -local script_fname = arg[ind] -if not script_fname then - print_help("repl not yet supported") - return +local print_err +print_err = function(...) + local msg = table.concat((function(...) + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = { + ... + } + for _index_0 = 1, #_list_0 do + local v = _list_0[_index_0] + _accum_0[_len_0] = tostring(v) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(...), "\t") + return io.stderr:write(msg .. "\n") end - -local new_arg = { - [-1] = arg[0], - [0] = arg[ind], - select(ind + 1, unpack(arg)) -} - -local moonscript_chunk, lua_parse_error -local passed, err = pcall(function() - moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { implicitly_return_root = false }) -end) - -if not passed then - print_err(err) - os.exit(1) +local print_help +print_help = function(err) + help = help:format(arg[0]) + if err then + print_err(err) + print_err(help) + else + print(help) + end + return os.exit() end - -if not moonscript_chunk then - if lua_parse_error then - print_err(lua_parse_error) - else - print_err("Can't find file: " .. script_fname) - end - os.exit(1) -end - -util.getfenv(moonscript_chunk).arg = new_arg - -local function run_chunk() - moonscript.insert_loader() - moonscript_chunk(unpack(new_arg)) - moonscript.remove_loader() -end - -if not opts.d then - local err, trace - local cov - - if opts.c then - local coverage = require "moonscript.cmd.coverage" - cov = coverage.CodeCoverage() - cov:start() - end - - xpcall(run_chunk, function(_err) - err = _err - trace = debug.traceback("", 2) - end) - - if err then - local truncated = errors.truncate_traceback(util.trim(trace)) - local rewritten = errors.rewrite_traceback(truncated, err) - - if rewritten then - print_err(rewritten) - else - -- faield to rewrite, show original - print_err(table.concat({ - err, - util.trim(trace) - }, "\n")) - end - else - if cov then - cov:stop() - cov:print_results() - end - end -else - run_chunk() +local run +run = function() + if opts.h then + print_help() + end + if opts.v then + require("moonscript.version").print_version() + os.exit() + end + local script_fname = arg[ind] + if not (script_fname) then + print_help("repl not yet supported") + end + local new_arg = { + [-1] = arg[0], + [0] = arg[ind], + select(ind + 1, unpack(arg)) + } + local moonscript_chunk, lua_parse_error + local passed, err = pcall(function() + moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { + implicitly_return_root = false + }) + end) + if not (passed) then + print_err(err) + os.exit(1) + end + if not (moonscript_chunk) then + if lua_parse_error then + print_err(lua_parse_error) + else + print_err("Can't file file: " .. tostring(script_fname)) + end + os.exit(1) + end + util.getfenv(moonscript_chunk).arg = new_arg + local run_chunk + run_chunk = function() + moonscript.insert_loader() + moonscript_chunk(unpack(new_arg)) + return moonscript.remove_loader() + end + if opts.d then + return run_chunk() + end + local err, trace, cov + if opts.c then + print("starting coverage") + local coverage = require("moonscript.cmd.coverage") + cov = coverage.CodeCoverage() + cov:start() + end + xpcall(run_chunk, function(_err) + err = _err + trace = debug.traceback("", 2) + end) + if err then + local truncated = errors.truncate_traceback(util.trim(trace)) + local rewritten = errors.rewrite_traceback(truncated, err) + if rewritten then + return print_err(rewritten) + else + return print_err(table.concat({ + err, + util.trim(trace) + }, "\n")) + end + else + if cov then + cov:stop() + return cov:print_results() + end + end end +return run() +-- vim: set filetype=lua: diff --git a/bin/moon.moon b/bin/moon.moon new file mode 100644 index 00000000..853cf19b --- /dev/null +++ b/bin/moon.moon @@ -0,0 +1,116 @@ + +require "alt_getopt" + +moonscript = require "moonscript.base" +util = require "moonscript.util" +errors = require "moonscript.errors" + +unpack = util.unpack + +opts, ind = alt_getopt.get_opts arg, "cvhd", { + version: "v" + help: "h" +} + +help = [=[Usage: %s [options] [script [args]] + + -h Print this message + -d Disable stack trace rewriting + -c Collect and print code coverage + -v Print version +]=] + + +print_err = (...) -> + msg = table.concat [tostring v for v in *{...}], "\t" + io.stderr\write msg .. "\n" + +print_help = (err) -> + help = help\format arg[0] + + if err + print_err err + print_err help + else + print help + + os.exit! + +run = -> + if opts.h + print_help! + + if opts.v + require("moonscript.version").print_version! + os.exit! + + script_fname = arg[ind] + + unless script_fname + print_help "repl not yet supported" + + new_arg = { + [-1]: arg[0], + [0]: arg[ind], + select ind + 1, unpack arg + } + + local moonscript_chunk, lua_parse_error + + passed, err = pcall -> + moonscript_chunk, lua_parse_error = moonscript.loadfile script_fname, { + implicitly_return_root: false + } + + unless passed + print_err err + os.exit 1 + + unless moonscript_chunk + if lua_parse_error + print_err lua_parse_error + else + print_err "Can't file file: #{script_fname}" + + os.exit 1 + + util.getfenv(moonscript_chunk).arg = new_arg + + run_chunk = -> + moonscript.insert_loader! + moonscript_chunk unpack new_arg + moonscript.remove_loader! + + if opts.d + return run_chunk! + + local err, trace, cov + + if opts.c + print "starting coverage" + coverage = require "moonscript.cmd.coverage" + cov = coverage.CodeCoverage! + cov\start! + + xpcall run_chunk, (_err) -> + err = _err + trace = debug.traceback "", 2 + + if err + truncated = errors.truncate_traceback util.trim trace + rewritten = errors.rewrite_traceback truncated, err + + if rewritten + print_err rewritten + else + -- failed to rewrite, show original + print_err table.concat { + err, + util.trim trace + }, "\n" + else + if cov + cov\stop! + cov\print_results! + +run! From 1bd8b0336e210a2f06f4d9e48dc5742261a283a3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 8 Jun 2015 21:50:52 -0700 Subject: [PATCH 112/344] pass last pos through state table --- moonscript/parse.lua | 13 ++++++++----- moonscript/parse.moon | 15 ++++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 1ae66f32..1ec4f115 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -35,10 +35,12 @@ end local build_grammar = wrap_env(debug_grammar, function(root) local _indent = Stack(0) local _do_stack = Stack(0) - local last_pos = 0 + local state = { + last_pos = 0 + } local check_indent check_indent = function(str, pos, indent) - last_pos = pos + state.last_pos = pos return _indent:top() == indent end local advance_indent @@ -103,7 +105,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) local SelfName = Space * "@" * ("@" * (_Name / mark("self_class") + Cc("self.__class")) + _Name / mark("self") + Cc("self")) local KeyName = SelfName + Space * _Name / mark("key_literal") local VarArg = Space * P("...") / trim - return P({ + local g = P({ root or File, File = Shebang ^ -1 * (Block + Ct("")), Block = Ct(Line * (Break ^ 1 * Line) ^ 0), @@ -196,10 +198,11 @@ local build_grammar = wrap_env(debug_grammar, function(root) ArgBlock = ArgLine * (sym(",") * SpaceBreak * ArgLine) ^ 0 * PopIndent, ArgLine = CheckIndent * ExpList }) + return g, state end) local file_parser file_parser = function() - local g = build_grammar() + local g, state = build_grammar() local file_grammar = White * g * White * -1 return { match = function(self, str) @@ -214,7 +217,7 @@ file_parser = function() end if not (tree) then local msg - local err_pos = last_pos + local err_pos = state.last_pos if err then local node node, msg = unpack(err) diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 5c0885e8..0dd07a02 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -36,10 +36,13 @@ build_grammar = wrap_env debug_grammar, (root) -> _indent = Stack 0 _do_stack = Stack 0 - last_pos = 0 -- last pos we saw, used to report error location + state = { + -- last pos we saw, used to report error location + last_pos: 0 + } check_indent = (str, pos, indent) -> - last_pos = pos + state.last_pos = pos _indent\top! == indent advance_indent = (str, pos, indent) -> @@ -101,7 +104,7 @@ build_grammar = wrap_env debug_grammar, (root) -> KeyName = SelfName + Space * _Name / mark"key_literal" VarArg = Space * P"..." / trim - P { + g = P { root or File File: Shebang^-1 * (Block + Ct"") Block: Ct(Line * (Break^1 * Line)^0) @@ -315,8 +318,10 @@ build_grammar = wrap_env debug_grammar, (root) -> ArgLine: CheckIndent * ExpList } + g, state + file_parser = -> - g = build_grammar! + g, state = build_grammar! file_grammar = White * g * White * -1 { @@ -333,7 +338,7 @@ file_parser = -> unless tree local msg - err_pos = last_pos + err_pos = state.last_pos if err node, msg = unpack err From 9e98affc1d0fda5ed68a845b083eac3db8ad63f3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 8 Jun 2015 22:03:11 -0700 Subject: [PATCH 113/344] put bin in lint --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 13a1eba8..a374fb3e 100644 --- a/Makefile +++ b/Makefile @@ -22,4 +22,4 @@ watch: moonc moon/ moonscript/ && moonc -w moon/ moonscript/ lint: - moonc -l moonscript moon + moonc -l moonscript moon bin From b0904c170cae82d2b0947f058e7192bce07f0593 Mon Sep 17 00:00:00 2001 From: nymphium Date: Thu, 13 Aug 2015 18:02:30 +0900 Subject: [PATCH 114/344] added Lua5.3's new operators --- moonscript/compile/value.lua | 3 +++ moonscript/compile/value.moon | 3 +++ moonscript/parse.lua | 6 +++--- moonscript/parse.moon | 5 +++-- 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 8f8e8f23..c71a45b3 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -276,6 +276,9 @@ return { number = function(self, node) return node[2] end, + bitnot = function(self, node) + return self:line("~", self:value(node[2])) + end, length = function(self, node) return self:line("#", self:value(node[2])) end, diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 515fed87..d1c9fae1 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -166,6 +166,9 @@ string_chars = { number: (node) => node[2] + bitnot: (node) => + @line "~", @value node[2] + length: (node) => @line "#", @value node[2] diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 1ec4f115..920668bb 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -145,12 +145,12 @@ local build_grammar = wrap_env(debug_grammar, function(root) CompClause = CompFor + CompForEach + key("when") * Exp / mark("when"), Assign = sym("=") * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark("assign"), Update = ((sym("..=") + sym("+=") + sym("-=") + sym("*=") + sym("/=") + sym("%=") + sym("or=") + sym("and=")) / trim) * Exp / mark("update"), - CharOperators = Space * C(S("+-*/%^><")), - WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op(".."), + CharOperators = Space * C(S("+-*/%^><|&")), + WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op("..") + op("<<") + op(">>") + op("//"), BinaryOperator = (WordOperators + CharOperators) * SpaceBreak ^ 0, Assignable = Cmt(DotChain + Chain, check_assignable) + Name + SelfName, Exp = Ct(Value * (BinaryOperator * Value) ^ 0) / flatten_or_mark("exp"), - SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num, + SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + sym("~") * Exp / mark("bitnot") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num, ChainValue = StringChain + ((Chain + DotChain + Callable) * Ct(InvokeArgs ^ -1)) / flatten_func, Value = pos(SimpleValue + Ct(KeyValueList) / mark("table") + ChainValue), SliceValue = SimpleValue + ChainValue, diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 0dd07a02..ce4b221e 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -179,8 +179,8 @@ build_grammar = wrap_env debug_grammar, (root) -> Assign: sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign" Update: ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update" - CharOperators: Space * C(S"+-*/%^><") - WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + CharOperators: Space * C(S"+-*/%^><|&") + WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + op"<<" + op">>" + op"//" BinaryOperator: (WordOperators + CharOperators) * SpaceBreak^0 Assignable: Cmt(DotChain + Chain, check_assignable) + Name + SelfName @@ -195,6 +195,7 @@ build_grammar = wrap_env debug_grammar, (root) -> Cmt(Do, check_do) + sym"-" * -SomeSpace * Exp / mark"minus" + sym"#" * Exp / mark"length" + + sym"~" * Exp / mark"bitnot" + key"not" * Exp / mark"not" + TblComprehension + TableLit + From db523926d3ec9ceddc7eaa4e0d72ec72f41fb7e7 Mon Sep 17 00:00:00 2001 From: nymphium Date: Thu, 13 Aug 2015 18:06:03 +0900 Subject: [PATCH 115/344] make it choice /usr/bin/lua, not lua5.1 --- Makefile | 4 ++-- moon/init.moon | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index a374fb3e..c620a2ef 100644 --- a/Makefile +++ b/Makefile @@ -13,9 +13,9 @@ global: sudo luarocks make moonscript-dev-1.rockspec compile: - lua5.1 bin/moonc moon/ moonscript/ + lua bin/moonc moon/ moonscript/ echo "#!/usr/bin/env lua" > bin/moon - lua5.1 bin/moonc -p bin/moon.moon >> bin/moon + lua bin/moonc -p bin/moon.moon >> bin/moon echo "-- vim: set filetype=lua:" >> bin/moon watch: diff --git a/moon/init.moon b/moon/init.moon index ef07bbaf..5817bc55 100644 --- a/moon/init.moon +++ b/moon/init.moon @@ -41,7 +41,7 @@ debug = setmetatable { -- run a function with scope injected before its function environment run_with_scope = (fn, scope, ...) -> - old_env = getfenv fn + old_env = util.getfenv fn env = setmetatable {}, { __index: (name) => val = scope[name] @@ -50,7 +50,7 @@ run_with_scope = (fn, scope, ...) -> else old_env[name] } - setfenv fn, env + util.setfenv fn, env fn ... -- wrap obj such that calls to methods do not need a reference to self From 0d330c8416614c3337bfb9c321f1482ab5ab8093 Mon Sep 17 00:00:00 2001 From: nymphium Date: Fri, 21 Aug 2015 21:24:31 +0900 Subject: [PATCH 116/344] LUA=lua5.1 as default, and detect luarocks and LUA_{C,}PATH --- Makefile | 25 ++++++++++++++++++++----- moonscript/parse.moon | 2 +- 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index c620a2ef..d4fb0945 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,18 @@ +LUA ?= lua5.1 +LUAROCKS ?= luarocks + +ifneq ($(LUA),lua) + LUA_VERSION = $(shell echo $(LUA) | sed -e "s/lua\(.*\)/\1/") + LUAROCKS = luarocks-$(LUA_VERSION) + LUA_PATH_MAKE = $(shell echo "${LUA_PATH}" | sed -e "s/[0-9]\.[0-9]/$(LUA_VERSION)/g") + LUA_CPATH_MAKE = $(shell echo "${LUA_CPATH}" | sed -e "s/[0-9]\.[0-9]/$(LUA_VERSION)/g") +endif + +ifeq ($(LUA),luajit) + LUAROCKS = luarocks-5.1 +endif + + .PHONY: test local compile watch lint test_safe test: @@ -7,15 +22,15 @@ test_safe: busted -p "_spec.lua$$" local: compile - luarocks make --local moonscript-dev-1.rockspec + LUA_PATH=$(LUA_PATH_MAKE) LUA_CPATH=$(LUA_CPATH_MAKE) $(LUAROCKS) make --local moonscript-dev-1.rockspec global: - sudo luarocks make moonscript-dev-1.rockspec + sudo LUA_PATH=$(LUA_PATH_MAKE) LUA_CPATH=$(LUA_CPATH_MAKE) $(LUAROCKS) make moonscript-dev-1.rockspec -compile: - lua bin/moonc moon/ moonscript/ +compile: init + LUA_PATH=$(LUA_PATH_MAKE) LUA_CPATH=$(LUA_CPATH_MAKE) $(LUA) bin/moonc moon/ moonscript/ echo "#!/usr/bin/env lua" > bin/moon - lua bin/moonc -p bin/moon.moon >> bin/moon + $(LUA) bin/moonc -p bin/moon.moon >> bin/moon echo "-- vim: set filetype=lua:" >> bin/moon watch: diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 0dd07a02..24581a50 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -291,7 +291,7 @@ build_grammar = wrap_env debug_grammar, (root) -> op"*" + op"^" + Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export" - KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)) + KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp!) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)) KeyValueList: KeyValue * (sym"," * KeyValue)^0 KeyValueLine: CheckIndent * KeyValueList * sym","^-1 From b78cdab10b37e6caeffd6d44c48696b30fb58b2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Davidovi=C4=87?= Date: Sat, 5 Sep 2015 16:59:40 +0200 Subject: [PATCH 117/344] Fix bug regarding nested `with` blocks Nested `with` blocks, when the node being used for the block is referenced from the enclosing `with` block using the short-dot syntax, do not produce the correct output. This happens because `scoped_var` is being set at the wrong place. Fixes #187 (and #197, its dupe). --- moonscript/transform.lua | 4 ++-- moonscript/transform.moon | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index c2499296..d81a6100 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -713,11 +713,11 @@ Statement = Transformer({ end scope_name = scope_name or NameProxy("with") return build["do"]({ + copy_scope and build.assign_one(scope_name, exp) or NOOP, + named_assign or NOOP, Run(function(self) return self:set("scope_var", scope_name) end), - copy_scope and build.assign_one(scope_name, exp) or NOOP, - named_assign or NOOP, build.group(block), (function() if ret then diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 059f7d84..8b47c2db 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -418,9 +418,9 @@ Statement = Transformer { scope_name or= NameProxy "with" build.do { - Run => @set "scope_var", scope_name copy_scope and build.assign_one(scope_name, exp) or NOOP named_assign or NOOP + Run => @set "scope_var", scope_name build.group block if ret From 5ae8ffaed74aa9ec5695caaf229af09835b1eb6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Davidovi=C4=87?= Date: Sat, 5 Sep 2015 17:01:28 +0200 Subject: [PATCH 118/344] Add tests for nested `with` blocks Added two tests that test the behavior of nested `with` blocks. When a nested `with` block is started, using a variable referenced from the enclosing one, expected behavior is to change the short-dot syntax scope to the newly-referenced variable. --- spec/inputs/with.moon | 12 +++++++++++- spec/outputs/with.lua | 20 ++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/spec/inputs/with.moon b/spec/inputs/with.moon index 54aa5091..16f3574d 100644 --- a/spec/inputs/with.moon +++ b/spec/inputs/with.moon @@ -93,6 +93,16 @@ do with k.j = "jo" print \upper! +do + with a + print .b + -- nested `with`s should change the scope correctly + with .c + print .d - +do + with a + -- nested `with`s with assignments should change the scope correctly + with .b = 2 + print .c diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua index d1d104a0..62fbb1ca 100644 --- a/spec/outputs/with.lua +++ b/spec/outputs/with.lua @@ -131,6 +131,26 @@ do local _with_0 = "jo" k.j = _with_0 print(_with_0:upper()) + end +end +do + do + local _with_0 = a + print(_with_0.b) + do + local _with_1 = _with_0.c + print(_with_1.d) + end + end +end +do + do + local _with_0 = a + do + local _with_1 = 2 + _with_0.b = _with_1 + print(_with_1.c) + end return _with_0 end end \ No newline at end of file From fb8bfb740c9db2920b0a9b53f8db5cf7e1f0788b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 17:46:21 -0700 Subject: [PATCH 119/344] new transform.statments module, last_stm function --- moonscript/transform/statements.lua | 50 ++++++++++++++++++++++++++++ moonscript/transform/statements.moon | 30 +++++++++++++++++ moonscript/types.moon | 1 + spec/transform_spec.moon | 48 ++++++++++++++++++++++++++ 4 files changed, 129 insertions(+) create mode 100644 moonscript/transform/statements.lua create mode 100644 moonscript/transform/statements.moon create mode 100644 spec/transform_spec.moon diff --git a/moonscript/transform/statements.lua b/moonscript/transform/statements.lua new file mode 100644 index 00000000..afad5c45 --- /dev/null +++ b/moonscript/transform/statements.lua @@ -0,0 +1,50 @@ +local ntype, mtype +do + local _obj_0 = require("moonscript.types") + ntype, mtype = _obj_0.ntype, _obj_0.mtype +end +local Run +do + local _base_0 = { + call = function(self, state) + return self.fn(state) + end + } + _base_0.__index = _base_0 + local _class_0 = setmetatable({ + __init = function(self, fn) + self.fn = fn + self[1] = "run" + end, + __base = _base_0, + __name = "Run" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Run = _class_0 +end +local last_stm +last_stm = function(stms) + local last_exp_id = 0 + for i = #stms, 1, -1 do + local stm = stms[i] + if stm and mtype(stm) ~= Run then + if ntype(stm) == "group" then + return last_stm(stm[2]) + end + last_exp_id = i + break + end + end + return stms[last_exp_id], last_exp_id, stms +end +return { + last_stm = last_stm, + Run = Run +} diff --git a/moonscript/transform/statements.moon b/moonscript/transform/statements.moon new file mode 100644 index 00000000..bf214a6a --- /dev/null +++ b/moonscript/transform/statements.moon @@ -0,0 +1,30 @@ + +import ntype, mtype from require "moonscript.types" + +-- A Run is a special statement node that lets a function run and mutate the +-- state of the compiler +class Run + new: (@fn) => + @[1] = "run" + + call: (state) => + @.fn state + +-- extract the last statment from an array of statements +-- is group aware +-- returns: the last statement, the index, the table it was fetched from +last_stm = (stms) -> + last_exp_id = 0 + for i = #stms, 1, -1 + stm = stms[i] + if stm and mtype(stm) != Run + if ntype(stm) == "group" + return last_stm stm[2] + + last_exp_id = i + break + + stms[last_exp_id], last_exp_id, stms + +{:last_stm, :Run} + diff --git a/moonscript/types.moon b/moonscript/types.moon index 9b415231..d7bf3c55 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -23,6 +23,7 @@ ntype = (node) -> else "value" +-- gets the class of a type if possible mtype = do moon_type = util.moon.type -- lets us check a smart node without throwing an error diff --git a/spec/transform_spec.moon b/spec/transform_spec.moon new file mode 100644 index 00000000..807175fc --- /dev/null +++ b/spec/transform_spec.moon @@ -0,0 +1,48 @@ + +describe "moonscript.transform.statements", -> + describe "last_stm", -> + import last_stm, Run from require "moonscript.transform.statements" + + it "gets last statement from empty list", -> + assert.same nil, (last_stm {}) + + it "gets last statement", -> + stms = { + {"ref", "butt_world"} + {"ref", "hello_world"} + } + + stm, idx, t = last_stm stms + assert stms[2] == stm + assert.same 2, idx + assert stms == t + + it "gets last statement ignoring run", -> + stms = { + {"ref", "butt_world"} + {"ref", "hello_world"} + Run => print "hi" + } + + stm, idx, t = last_stm stms + assert stms[2] == stm + assert.same 2, idx + assert stms == t + + it "gets last from within group", -> + stms = { + {"ref", "butt_world"} + {"group", { + {"ref", "hello_world"} + {"ref", "cool_world"} + }} + } + + last = stms[2][2][2] + + stm, idx, t = last_stm stms + assert stm == last, "should get last" + assert.same 2, idx + assert t == stms[2][2], "should get correct table" + + From c8552b04dc08bcdc1aaba94c1ec4938b573ae6ca Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 17:53:34 -0700 Subject: [PATCH 120/344] transform last stm --- moonscript/transform/statements.lua | 27 ++++++++++++++++++- moonscript/transform/statements.moon | 17 +++++++++++- spec/transform_spec.moon | 39 ++++++++++++++++++++++++++++ 3 files changed, 81 insertions(+), 2 deletions(-) diff --git a/moonscript/transform/statements.lua b/moonscript/transform/statements.lua index afad5c45..f71aede5 100644 --- a/moonscript/transform/statements.lua +++ b/moonscript/transform/statements.lua @@ -44,7 +44,32 @@ last_stm = function(stms) end return stms[last_exp_id], last_exp_id, stms end +local transform_last_stm +transform_last_stm = function(stms, fn) + local _, last_idx, _stms = last_stm(stms) + if _stms ~= stms then + error("cannot transform last node in group") + end + return (function() + local _accum_0 = { } + local _len_0 = 1 + for i, stm in ipairs(stms) do + if i == last_idx then + _accum_0[_len_0] = { + "transform", + stm, + fn + } + else + _accum_0[_len_0] = stm + end + _len_0 = _len_0 + 1 + end + return _accum_0 + end)() +end return { + Run = Run, last_stm = last_stm, - Run = Run + transform_last_stm = transform_last_stm } diff --git a/moonscript/transform/statements.moon b/moonscript/transform/statements.moon index bf214a6a..0e3baeb7 100644 --- a/moonscript/transform/statements.moon +++ b/moonscript/transform/statements.moon @@ -26,5 +26,20 @@ last_stm = (stms) -> stms[last_exp_id], last_exp_id, stms -{:last_stm, :Run} +-- transform the last stm is a list of stms +-- will puke on group +transform_last_stm = (stms, fn) -> + _, last_idx, _stms = last_stm stms + + if _stms != stms + error "cannot transform last node in group" + + return for i, stm in ipairs stms + if i == last_idx + {"transform", stm, fn} + else + stm + + +{:Run, :last_stm, :transform_last_stm} diff --git a/spec/transform_spec.moon b/spec/transform_spec.moon index 807175fc..682561d7 100644 --- a/spec/transform_spec.moon +++ b/spec/transform_spec.moon @@ -45,4 +45,43 @@ describe "moonscript.transform.statements", -> assert.same 2, idx assert t == stms[2][2], "should get correct table" + describe "transform_last_stm", -> + import transform_last_stm, Run from require "moonscript.transform.statements" + + it "transforms empty stms", -> + before = {} + after = transform_last_stm before, (n) -> {"wrapped", n} + + assert.same before, after + assert before != after + + it "transforms stms", -> + before = { + {"ref", "butt_world"} + {"ref", "hello_world"} + } + + transformer = (n) -> n + after = transform_last_stm before, transformer + + assert.same { + {"ref", "butt_world"} + {"transform", {"ref", "hello_world"}, transformer} + }, after + + it "transforms empty stms ignoring runs", -> + before = { + {"ref", "butt_world"} + {"ref", "hello_world"} + Run => print "hi" + } + + transformer = (n) -> n + after = transform_last_stm before, transformer + + assert.same { + {"ref", "butt_world"} + {"transform", {"ref", "hello_world"}, transformer} + before[3] + }, after From e26fc29784a52b9baedc70348845a7f5932bc1c9 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 18:05:00 -0700 Subject: [PATCH 121/344] compile_system --- Makefile | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index a374fb3e..3a4f3dba 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: test local compile watch lint test_safe +.PHONY: test local compile compile_system watch lint test_safe test: busted -p "_spec.moon$$" @@ -18,6 +18,12 @@ compile: lua5.1 bin/moonc -p bin/moon.moon >> bin/moon echo "-- vim: set filetype=lua:" >> bin/moon +compile_system: + moonc moon/ moonscript/ + echo "#!/usr/bin/env lua" > bin/moon + moonc -p bin/moon.moon >> bin/moon + echo "-- vim: set filetype=lua:" >> bin/moon + watch: moonc moon/ moonscript/ && moonc -w moon/ moonscript/ From aeca8864aefcca49a80bcbe09c40d9ee149ba9f8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 19:07:04 -0700 Subject: [PATCH 122/344] make helper to make sure right moonscript code is tested, add to lang spec --- spec/helpers.moon | 35 ++++++++++++++++++++++++++++++++++- spec/lang_spec.moon | 22 +++++++++++++--------- 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/spec/helpers.moon b/spec/helpers.moon index c5ddf9f5..1547d808 100644 --- a/spec/helpers.moon +++ b/spec/helpers.moon @@ -6,4 +6,37 @@ unindent = (str) -> return str unless indent (str\gsub("\n#{indent}", "\n")\gsub "%s+$", "") -{ :unindent } +in_dev = false + +-- this will ensure any moonscript modules included come from the local +-- directory +with_dev = (fn) -> + error "already in dev mode" if in_dev + + -- a package loader that only looks in currect directory + import make_loader from require "loadkit" + loader = make_loader "lua", nil, "./?.lua" + + import setup, teardown from require "busted" + + old_require = _G.require + dev_cache = {} + + setup -> + _G.require = (mod) -> + return dev_cache[mod] if dev_cache[mod] + + if mod\match("moonscript%.") or mod == "moonscript" + dev_cache[mod] = assert(loadfile(assert loader mod))! + return dev_cache[mod] + + old_require mod + + if fn + fn! + + teardown -> + _G.require = old_require + in_dev = false + +{ :unindent, :with_dev } diff --git a/spec/lang_spec.moon b/spec/lang_spec.moon index f6612dd8..b0fa1bab 100644 --- a/spec/lang_spec.moon +++ b/spec/lang_spec.moon @@ -1,12 +1,10 @@ lfs = require "lfs" -parse = require "moonscript.parse" -compile = require "moonscript.compile" -util = require "moonscript.util" +import with_dev from require "spec.helpers" pattern = ... -import unpack from util +unpack = table.unpack or unpack options = { in_dir: "spec/inputs", @@ -20,7 +18,7 @@ options = { tool: "git diff --no-index --color" --color-words" filter: (str) -> -- strip the first four lines - table.concat [line for line in *util.split(str, "\n")[5,]], "\n" + table.concat [l for l in *([line for line in str\gmatch("[^\n]+")])[5,]], "\n" } } @@ -81,12 +79,18 @@ input_fname = (base) -> output_fname = (base) -> options.out_dir .. "/" .. base .. options.output_ext +inputs = for file in lfs.dir options.in_dir + with match = file\match options.input_pattern + continue unless match + +table.sort inputs + describe "input tests", -> - inputs = for file in lfs.dir options.in_dir - with match = file\match options.input_pattern - continue unless match + local parse, compile - table.sort inputs + with_dev -> + parse = require "moonscript.parse" + compile = require "moonscript.compile" for name in *inputs input = input_fname name From 6082a86b11f0ee5e1759097cf33847a8202fb041 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 19:31:01 -0700 Subject: [PATCH 123/344] update all specs to make sure they run with dev code --- spec/cmd_spec.moon | 11 ++- spec/error_rewriting_spec.moon | 140 ++++++++++++++++----------------- spec/helpers.moon | 9 ++- spec/moon_spec.moon | 7 +- spec/moonscript_spec.moon | 4 + spec/transform_spec.moon | 10 ++- 6 files changed, 102 insertions(+), 79 deletions(-) diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 84012e31..5e7b0dce 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -1,9 +1,14 @@ -moonc = require "moonscript.cmd.moonc" +import with_dev from require "spec.helpers" -- TODO: add specs for windows equivalents describe "moonc", -> + local moonc + + dev_loaded = with_dev -> + moonc = require "moonscript.cmd.moonc" + same = (fn, a, b)-> assert.same b, fn a @@ -64,7 +69,7 @@ describe "moonc", -> before_each -> dirs = {} package.loaded.lfs = nil - package.loaded["moonscript.cmd.moonc"] = nil + dev_loaded["moonscript.cmd.moonc"] = nil package.loaded.lfs = { mkdir: (dir) -> table.insert dirs, dir @@ -75,7 +80,7 @@ describe "moonc", -> after_each -> package.loaded.lfs = nil - package.loaded["moonscript.cmd.moonc"] = nil + dev_loaded["moonscript.cmd.moonc"] = nil moonc = require "moonscript.cmd.moonc" it "should make directory", -> diff --git a/spec/error_rewriting_spec.moon b/spec/error_rewriting_spec.moon index 9978d1a2..c3616116 100644 --- a/spec/error_rewriting_spec.moon +++ b/spec/error_rewriting_spec.moon @@ -1,71 +1,71 @@ -moonscript = require "moonscript.base" -errors = require "moonscript.errors" -util = require "moonscript.util" - -import unindent from require "spec.helpers" - -get_rewritten_line_no = (fname) -> - fname = "spec/error_inputs/#{fname}.moon" - chunk = moonscript.loadfile fname - - success, err = pcall chunk - error "`#{fname}` is supposed to have runtime error!" if success - - source = tonumber err\match "^.-:(%d+):" - - line_table = assert require("moonscript.line_tables")["@#{fname}"], "missing line table" - errors.reverse_line_number fname, line_table, source, {} - --- TODO: check entire stack trace -describe "error rewriting", -> - tests = { - "first": 24 - "second": 16 - "third": 11 - } - - for name, expected_no in pairs tests - it "should rewrite line number", -> - assert.same get_rewritten_line_no(name), expected_no - -describe "line map", -> - import to_lua from require "moonscript.base" - - it "should create line table", -> - moon_code = unindent [[ - print "hello world" - if something - print "cats" - ]] - - lua_code, posmap = assert to_lua moon_code - -- print util.debug_posmap(posmap, moon_code, lua_code) - assert.same { 7, 29, 42, 27 }, posmap - - it "should create line table for multiline string", -> - moon_code = unindent [[ - print "one" - x = [==[ - one - two - thre - yes - no - ]==] - print "two" - ]] - - lua_code, posmap = assert to_lua moon_code - -- print util.debug_posmap(posmap, moon_code, lua_code) - assert.same {[1]: 7, [2]: 19, [7]: 19, [8]: 63}, posmap - -describe "error reporting", -> - import to_lua from require "moonscript.base" - it "should compile bad code twice", -> - code, err = to_lua "{b=5}" - assert.truthy err - code, err2 = to_lua "{b=5}" - assert.same err, err2 - - +import unindent, with_dev from require "spec.helpers" + +describe "moonscript.errors", -> + local moonscript, errors, util, to_lua + + -- with_dev -> + moonscript = require "moonscript.base" + errors = require "moonscript.errors" + util = require "moonscript.util" + + {:to_lua} = moonscript + + get_rewritten_line_no = (fname) -> + fname = "spec/error_inputs/#{fname}.moon" + chunk = moonscript.loadfile fname + + success, err = pcall chunk + error "`#{fname}` is supposed to have runtime error!" if success + + source = tonumber err\match "^.-:(%d+):" + + line_table = assert require("moonscript.line_tables")["@#{fname}"], "missing line table" + errors.reverse_line_number fname, line_table, source, {} + + describe "error rewriting", -> + tests = { + "first": 24 + "second": 16 + "third": 11 + } + + for name, expected_no in pairs tests + it "should rewrite line number", -> + assert.same get_rewritten_line_no(name), expected_no + + describe "line map", -> + it "should create line table", -> + moon_code = unindent [[ + print "hello world" + if something + print "cats" + ]] + + lua_code, posmap = assert to_lua moon_code + -- print util.debug_posmap(posmap, moon_code, lua_code) + assert.same { 1, 23, 36, 21 }, posmap + + it "should create line table for multiline string", -> + moon_code = unindent [[ + print "one" + x = [==[ + one + two + thre + yes + no + ]==] + print "two" + ]] + + lua_code, posmap = assert to_lua moon_code + -- print util.debug_posmap(posmap, moon_code, lua_code) + assert.same {[1]: 1, [2]: 13, [7]: 13, [8]: 57}, posmap + + describe "error reporting", -> + it "should compile bad code twice", -> + code, err = to_lua "{b=5}" + assert.truthy err + code, err2 = to_lua "{b=5}" + assert.same err, err2 diff --git a/spec/helpers.moon b/spec/helpers.moon index 1547d808..42d578b3 100644 --- a/spec/helpers.moon +++ b/spec/helpers.moon @@ -4,7 +4,7 @@ unindent = (str) -> indent = str\match "^%s+" return str unless indent - (str\gsub("\n#{indent}", "\n")\gsub "%s+$", "") + (str\gsub("\n#{indent}", "\n")\gsub("%s+$", "")\gsub "^%s+", "") in_dev = false @@ -26,7 +26,10 @@ with_dev = (fn) -> _G.require = (mod) -> return dev_cache[mod] if dev_cache[mod] - if mod\match("moonscript%.") or mod == "moonscript" + testable = mod\match("moonscript%.") or mod == "moonscript" or + mod\match("moon%.") or mod == "moon" + + if testable dev_cache[mod] = assert(loadfile(assert loader mod))! return dev_cache[mod] @@ -39,4 +42,6 @@ with_dev = (fn) -> _G.require = old_require in_dev = false + dev_cache + { :unindent, :with_dev } diff --git a/spec/moon_spec.moon b/spec/moon_spec.moon index 145b5ee1..773e53c0 100644 --- a/spec/moon_spec.moon +++ b/spec/moon_spec.moon @@ -1,8 +1,13 @@ -- test moon library -moon = require "moon" +import with_dev from require "spec.helpers" describe "moon", -> + local moon + + with_dev -> + moon = require "moon" + it "should determine correct type", -> class Test diff --git a/spec/moonscript_spec.moon b/spec/moonscript_spec.moon index 8db55585..b7e5271b 100644 --- a/spec/moonscript_spec.moon +++ b/spec/moonscript_spec.moon @@ -1,6 +1,10 @@ -- moonscript module +import with_dev from require "spec.helpers" + describe "moonscript.base", -> + with_dev! + it "should create moonpath", -> path = ";./?.lua;/usr/share/lua/5.1/?.lua;/usr/share/lua/5.1/?/init.lua;/usr/lib/lua/5.1/?.luac;/home/leafo/.luarocks/lua/5.1/?.lua" import create_moonpath from require "moonscript.base" diff --git a/spec/transform_spec.moon b/spec/transform_spec.moon index 682561d7..b4de4047 100644 --- a/spec/transform_spec.moon +++ b/spec/transform_spec.moon @@ -1,8 +1,13 @@ +import with_dev from require "spec.helpers" + describe "moonscript.transform.statements", -> - describe "last_stm", -> - import last_stm, Run from require "moonscript.transform.statements" + local last_stm, transform_last_stm, Run + with_dev -> + { :last_stm, :transform_last_stm, :Run } = require "moonscript.transform.statements" + + describe "last_stm", -> it "gets last statement from empty list", -> assert.same nil, (last_stm {}) @@ -46,7 +51,6 @@ describe "moonscript.transform.statements", -> assert t == stms[2][2], "should get correct table" describe "transform_last_stm", -> - import transform_last_stm, Run from require "moonscript.transform.statements" it "transforms empty stms", -> before = {} From c451418282949035e73f0570b5cd1df0bc1a6348 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 19:42:28 -0700 Subject: [PATCH 124/344] start a spec writing guide --- README.md | 2 ++ spec/README.md | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 spec/README.md diff --git a/README.md b/README.md index 5b2b555c..bf5359ea 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ To run tests, execute from the root directory: busted ``` +Writing specs is a bit more complicated. Check out [the spec writing guide](spec/README.md). + ## License (MIT) Copyright (C) 2015 by Leaf Corcoran diff --git a/spec/README.md b/spec/README.md new file mode 100644 index 00000000..67a99a01 --- /dev/null +++ b/spec/README.md @@ -0,0 +1,58 @@ + +# MoonScript Spec guide + +Because MoonScript is written in MoonScript, and MoonScript specs are written +in MoonScript, you need to be aware of which copy of MoonScript is actually +executing the specs. + +A system installed version of MoonScript is recommended to run the specs (and +for development). This means that you'll typically have two versions of +MoonScript available in the load path: + +* The system version +* The version in the current directory + +> A system install is recommended because you'll always want a functioning +> version of MoonScript to compile with in case you break your development +> version. + +When developing you want to make ensure the tests are executing your changes in +the current directory, and not testing the system install. + +Code running in Busted will have the system install take precedence over the +loaded version. That means that if you `require "moonscript.base"` for a test, +you won't get the local copy. + +The `with_dev` spec helper will ensure that any require calls within the spec +that ask for MoonScript modules. `with_dev` calls a setup and teardown that +replaces `_G.require` with a custom version. + +You'll use it like this: + +```moonscript +import with_dev from require "spec.helpers" +describe "moonscript.base", -> + local moonscript + + with_dev! + + it "should load code", -> + -- the local version is loaded + moonscript = require "moonscript" + moonscript.load "print 12" +``` + + +`with_dev`'s require function will load the `.lua` files in the local +directory, not the `moon` ones. You're responsible for compiling them first +before running the tests. + +You might do + +```bash +$ make compile_system; busted +``` + +> `make compile_system` is a makefile task included in the repo that will build +> MoonScript in the current directory with the version installed to the system + From 326e4208cb167af08c57a8e1cb71ed811847202e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:05:52 -0700 Subject: [PATCH 125/344] update spec guide --- spec/README.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spec/README.md b/spec/README.md index 67a99a01..37699484 100644 --- a/spec/README.md +++ b/spec/README.md @@ -1,5 +1,5 @@ -# MoonScript Spec guide +# MoonScript spec guide Because MoonScript is written in MoonScript, and MoonScript specs are written in MoonScript, you need to be aware of which copy of MoonScript is actually @@ -32,8 +32,6 @@ You'll use it like this: ```moonscript import with_dev from require "spec.helpers" describe "moonscript.base", -> - local moonscript - with_dev! it "should load code", -> From 9dbc1d5062b80e6de3e8e6b7ecff01c825df2553 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:06:19 -0700 Subject: [PATCH 126/344] misc --- moonscript/base.lua | 4 +--- moonscript/base.moon | 2 +- moonscript/compile.moon | 2 ++ 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/moonscript/base.lua b/moonscript/base.lua index e436678a..6bc0d49e 100644 --- a/moonscript/base.lua +++ b/moonscript/base.lua @@ -66,9 +66,7 @@ end moon_loader = function(name) local name_path = name:gsub("%.", dirsep) local file, file_path - local _list_0 = split(package.moonpath, ";") - for _index_0 = 1, #_list_0 do - local path = _list_0[_index_0] + for path in package.moonpath:gmatch("[^;]+") do file_path = path:gsub("?", name_path) file = io.open(file_path) if file then diff --git a/moonscript/base.moon b/moonscript/base.moon index b2068f0b..3a4502d4 100644 --- a/moonscript/base.moon +++ b/moonscript/base.moon @@ -38,7 +38,7 @@ moon_loader = (name) -> name_path = name\gsub "%.", dirsep local file, file_path - for path in *split package.moonpath, ";" + for path in package.moonpath\gmatch "[^;]+" file_path = path\gsub "?", name_path file = io.open file_path break if file diff --git a/moonscript/compile.moon b/moonscript/compile.moon index d05a3bb7..6206b264 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -406,6 +406,8 @@ class Block nil + -- takes the existing set of lines and replaces them with the result of + -- calling fn on them splice: (fn) => lines = {"lines", @_lines} @_lines = Lines! From ad7be54ff9cface121f02a1fea1b562926b2e64b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:06:41 -0700 Subject: [PATCH 127/344] move some stuff around --- moonscript/transform.lua | 75 ++++++++------------------------------- moonscript/transform.moon | 64 ++++++++++++--------------------- 2 files changed, 36 insertions(+), 103 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index d81a6100..50266e76 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -12,63 +12,16 @@ do local _obj_0 = require("moonscript.transform.names") NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName end +local Run, transform_last_stm +do + local _obj_0 = require("moonscript.transform.statements") + Run, transform_last_stm = _obj_0.Run, _obj_0.transform_last_stm +end local destructure = require("moonscript.transform.destructure") local NOOP = { "noop" } -local Run, apply_to_last, is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value -do - local _base_0 = { - call = function(self, state) - return self.fn(state) - end - } - _base_0.__index = _base_0 - local _class_0 = setmetatable({ - __init = function(self, fn) - self.fn = fn - self[1] = "run" - end, - __base = _base_0, - __name = "Run" - }, { - __index = _base_0, - __call = function(cls, ...) - local _self_0 = setmetatable({}, _base_0) - cls.__init(_self_0, ...) - return _self_0 - end - }) - _base_0.__class = _class_0 - Run = _class_0 -end -apply_to_last = function(stms, fn) - local last_exp_id = 0 - for i = #stms, 1, -1 do - local stm = stms[i] - if stm and mtype(stm) ~= Run then - last_exp_id = i - break - end - end - return (function() - local _accum_0 = { } - local _len_0 = 1 - for i, stm in ipairs(stms) do - if i == last_exp_id then - _accum_0[_len_0] = { - "transform", - stm, - fn - } - else - _accum_0[_len_0] = stm - end - _len_0 = _len_0 + 1 - end - return _accum_0 - end)() -end +local is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value is_singular = function(body) if #body ~= 1 then return false @@ -323,7 +276,7 @@ Statement = Transformer({ return fn(node) end, root_stms = function(self, body) - return apply_to_last(body, implicitly_return(self)) + return transform_last_stm(body, implicitly_return(self)) end, ["return"] = function(self, node) local ret_val = node[2] @@ -338,7 +291,7 @@ Statement = Transformer({ if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" then ret_val = Value:transform_once(self, ret_val) if ntype(ret_val) == "block_exp" then - return build.group(apply_to_last(ret_val[2], function(stm) + return build.group(transform_last_stm(ret_val[2], function(stm) return { "return", stm @@ -565,7 +518,7 @@ Statement = Transformer({ end, ["do"] = function(self, node, ret) if ret then - node[2] = apply_to_last(node[2], ret) + node[2] = transform_last_stm(node[2], ret) end return node end, @@ -676,11 +629,11 @@ Statement = Transformer({ node = expand_elseif_assign(node) if ret then smart_node(node) - node['then'] = apply_to_last(node['then'], ret) + node['then'] = transform_last_stm(node['then'], ret) for i = 4, #node do local case = node[i] local body_idx = #node[i] - case[body_idx] = apply_to_last(case[body_idx], ret) + case[body_idx] = transform_last_stm(case[body_idx], ret) end end return node @@ -861,7 +814,7 @@ Statement = Transformer({ body = case_exps end if ret then - body = apply_to_last(body, ret) + body = transform_last_stm(body, ret) end insert(out, body) return out @@ -1305,7 +1258,7 @@ do body = { } val = single_stm else - body = apply_to_last(body, function(n) + body = transform_last_stm(body, function(n) if types.is_value(n) then return build.assign_one(self.value_name, n) else @@ -1498,7 +1451,7 @@ Value = Transformer({ end, fndef = function(self, node) smart_node(node) - node.body = apply_to_last(node.body, implicitly_return(self)) + node.body = transform_last_stm(node.body, implicitly_return(self)) node.body = { Run(function(self) return self:listen("varargs", function() end) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 8b47c2db..18978760 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -6,37 +6,15 @@ data = require "moonscript.data" import reversed, unpack from util import ntype, mtype, build, smart_node, is_slice, value_is_singular from types import insert from table + import NameProxy, LocalName from require "moonscript.transform.names" +import Run, transform_last_stm from require "moonscript.transform.statements" destructure = require "moonscript.transform.destructure" NOOP = {"noop"} local * -class Run - new: (@fn) => - self[1] = "run" - - call: (state) => - self.fn state - --- transform the last stm is a list of stms --- will puke on group -apply_to_last = (stms, fn) -> - -- find last (real) exp - last_exp_id = 0 - for i = #stms, 1, -1 - stm = stms[i] - if stm and mtype(stm) != Run - last_exp_id = i - break - - return for i, stm in ipairs stms - if i == last_exp_id - {"transform", stm, fn} - else - stm - -- is a body a sindle expression/statement is_singular = (body) -> return false if #body != 1 @@ -82,6 +60,7 @@ constructor_name = "new" with_continue_listener = (body) -> continue_name = nil + { Run => @listen "continue", -> @@ -95,16 +74,17 @@ with_continue_listener = (body) -> Run => return unless continue_name @put_name continue_name, nil - @splice (lines) -> { - {"assign", {continue_name}, {"false"}} - {"repeat", "true", { - lines - {"assign", {continue_name}, {"true"}} - }} - {"if", {"not", continue_name}, { - {"break"} - }} - } + @splice (lines) -> + { + {"assign", {continue_name}, {"false"}} + {"repeat", "true", { + lines + {"assign", {continue_name}, {"true"}} + }} + {"if", {"not", continue_name}, { + {"break"} + }} + } } @@ -173,7 +153,7 @@ Statement = Transformer { fn node root_stms: (body) => - apply_to_last body, implicitly_return @ + transform_last_stm body, implicitly_return @ return: (node) => ret_val = node[2] @@ -190,7 +170,7 @@ Statement = Transformer { if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" ret_val = Value\transform_once @, ret_val if ntype(ret_val) == "block_exp" - return build.group apply_to_last ret_val[2], (stm)-> + return build.group transform_last_stm ret_val[2], (stm)-> {"return", stm} node[2] = ret_val @@ -323,7 +303,7 @@ Statement = Transformer { construct_comprehension action(exp), clauses do: (node, ret) => - node[2] = apply_to_last node[2], ret if ret + node[2] = transform_last_stm node[2], ret if ret node decorated: (node) => @@ -381,11 +361,11 @@ Statement = Transformer { if ret smart_node node -- mutate all the bodies - node['then'] = apply_to_last node['then'], ret + node['then'] = transform_last_stm node['then'], ret for i = 4, #node case = node[i] body_idx = #node[i] - case[body_idx] = apply_to_last case[body_idx], ret + case[body_idx] = transform_last_stm case[body_idx], ret node @@ -519,7 +499,7 @@ Statement = Transformer { body = case_exps if ret - body = apply_to_last body, ret + body = transform_last_stm body, ret insert out, body @@ -786,7 +766,7 @@ class Accumulator body = {} single_stm else - body = apply_to_last body, (n) -> + body = transform_last_stm body, (n) -> if types.is_value n build.assign_one @value_name, n else @@ -905,7 +885,7 @@ Value = Transformer { fndef: (node) => smart_node node - node.body = apply_to_last node.body, implicitly_return self + node.body = transform_last_stm node.body, implicitly_return self node.body = { Run => @listen "varargs", -> -- capture event unpack node.body From 7f423613d726dc3b664799f34f601ea4a89bca4f Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:07:12 -0700 Subject: [PATCH 128/344] update rockspec --- moonscript-dev-1.rockspec | 1 + 1 file changed, 1 insertion(+) diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index 00dd0544..68c82d52 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -44,6 +44,7 @@ build = { ["moonscript.transform"] = "moonscript/transform.lua", ["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua", ["moonscript.transform.names"] = "moonscript/transform/names.lua", + ["moonscript.transform.statements"] = "moonscript/transform/statements.lua", ["moonscript.types"] = "moonscript/types.lua", ["moonscript.util"] = "moonscript/util.lua", ["moonscript.version"] = "moonscript/version.lua", From be0c7d47fcf3f715c77e3d1321cf1b592d6c7450 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:12:52 -0700 Subject: [PATCH 129/344] fix travis? --- .travis.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 55a8e747..14c0f5b5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,8 +3,9 @@ language: c install: - sudo apt-get install luarocks - mkdir -p ~/.luarocks - - echo 'rocks_servers = { "http://rocks.moonscript.org" }' > ~/.luarocks/config.lua - - sudo luarocks install https://rocks.moonscript.org/manifests/olivine-labs/busted-2.0.rc7-0.rockspec + - echo 'rocks_servers = { "http://luarocks.org" }' > ~/.luarocks/config.lua + - sudo luarocks install https://raw.githubusercontent.com/Olivine-Labs/busted/master/busted-2.0.rc10-0.rockspec + - sudo luarocks install https://raw.githubusercontent.com/leafo/loadkit/master/loadkit-dev-1.rockspec - sudo luarocks make script: busted From 70c5c79ed996082310cb0f1547387b025ba34477 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:15:36 -0700 Subject: [PATCH 130/344] woop --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 14c0f5b5..e9e6313d 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ install: - sudo apt-get install luarocks - mkdir -p ~/.luarocks - echo 'rocks_servers = { "http://luarocks.org" }' > ~/.luarocks/config.lua - - sudo luarocks install https://raw.githubusercontent.com/Olivine-Labs/busted/master/busted-2.0.rc10-0.rockspec + - sudo luarocks install https://luarocks.org/manifests/olivine-labs/busted-2.0.rc7-0.rockspec - sudo luarocks install https://raw.githubusercontent.com/leafo/loadkit/master/loadkit-dev-1.rockspec - sudo luarocks make From 87c88feb280b297b7d1eabb751f4e7c659d2971d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:29:39 -0700 Subject: [PATCH 131/344] make sure return/break at end don't interfere with continue logic fixes #215 #190 #183 --- moonscript/transform.lua | 15 +++++++++++++-- moonscript/transform.moon | 8 +++++++- spec/inputs/loops.moon | 9 +++++++++ spec/outputs/loops.lua | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 50266e76..ac548dee 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -12,10 +12,10 @@ do local _obj_0 = require("moonscript.transform.names") NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName end -local Run, transform_last_stm +local Run, transform_last_stm, last_stm do local _obj_0 = require("moonscript.transform.statements") - Run, transform_last_stm = _obj_0.Run, _obj_0.transform_last_stm + Run, transform_last_stm, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.last_stm end local destructure = require("moonscript.transform.destructure") local NOOP = { @@ -115,8 +115,19 @@ with_continue_listener = function(body) if not (continue_name) then return end + local last = last_stm(body) + local t = last and ntype(last) + local enclose_lines = t == "return" or t == "break" self:put_name(continue_name, nil) return self:splice(function(lines) + if enclose_lines then + lines = { + "do", + { + lines + } + } + end return { { "assign", diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 18978760..09e1c074 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -8,7 +8,7 @@ import ntype, mtype, build, smart_node, is_slice, value_is_singular from types import insert from table import NameProxy, LocalName from require "moonscript.transform.names" -import Run, transform_last_stm from require "moonscript.transform.statements" +import Run, transform_last_stm, last_stm from require "moonscript.transform.statements" destructure = require "moonscript.transform.destructure" NOOP = {"noop"} @@ -73,8 +73,14 @@ with_continue_listener = (body) -> Run => return unless continue_name + last = last_stm body + t = last and ntype(last) + enclose_lines = t == "return" or t == "break" + @put_name continue_name, nil @splice (lines) -> + lines = {"do", {lines}} if enclose_lines + { {"assign", {continue_name}, {"false"}} {"repeat", "true", { diff --git a/spec/inputs/loops.moon b/spec/inputs/loops.moon index fbdc6380..a704e562 100644 --- a/spec/inputs/loops.moon +++ b/spec/inputs/loops.moon @@ -114,6 +114,15 @@ for x=1,10 for y = 2,12 continue if y % 3 == 0 + +while true + continue if false + break + +while true + continue if false + return 22 + -- do diff --git a/spec/outputs/loops.lua b/spec/outputs/loops.lua index dcba0bc1..db9aef33 100644 --- a/spec/outputs/loops.lua +++ b/spec/outputs/loops.lua @@ -260,6 +260,38 @@ for x = 1, 10 do break end end +while true do + local _continue_0 = false + repeat + do + if false then + _continue_0 = true + break + end + break + end + _continue_0 = true + until true + if not _continue_0 then + break + end +end +while true do + local _continue_0 = false + repeat + do + if false then + _continue_0 = true + break + end + return 22 + end + _continue_0 = true + until true + if not _continue_0 then + break + end +end do local xxx = { 1, From f273598e37a0da211c6a46e22b48e00c7b396154 Mon Sep 17 00:00:00 2001 From: Kyra 'nonchip' Zimmer Date: Tue, 21 Jul 2015 08:52:02 +0200 Subject: [PATCH 132/344] should fix #200 --- moonscript/parse.moon | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 0dd07a02..7b81bb39 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -260,7 +260,8 @@ build_grammar = wrap_env debug_grammar, (root) -> Invoke: FnArgs/mark"call" + SingleString / wrap_func_arg + - DoubleString / wrap_func_arg + DoubleString / wrap_func_arg + + LuaString / wrap_func_arg TableValue: KeyValue + Ct(Exp) From 36528f1dd019f945269caef0722108ede2f196d6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:59:33 -0700 Subject: [PATCH 133/344] rebuild, specs for new string precedence --- moonscript/parse.lua | 2 +- spec/inputs/syntax.moon | 3 +++ spec/outputs/syntax.lua | 2 ++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 1ec4f115..d3d8c226 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -171,7 +171,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) Slice = symx("[") * (SliceValue + Cc(1)) * sym(",") * (SliceValue + Cc("")) * (sym(",") * SliceValue) ^ -1 * sym("]") / mark("slice"), ColonCall = symx("\\") * (_Name * Invoke) / mark("colon"), ColonSuffix = symx("\\") * _Name / mark("colon_stub"), - Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg, + Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + LuaString / wrap_func_arg, TableValue = KeyValue + Ct(Exp), TableLit = sym("{") * Ct(TableValueList ^ -1 * sym(",") ^ -1 * (SpaceBreak * TableLitLine * (sym(",") ^ -1 * SpaceBreak * TableLitLine) ^ 0 * sym(",") ^ -1) ^ -1) * White * sym("}") / mark("table"), TableValueList = TableValue * (sym(",") * TableValue) ^ 0, diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index 09a5dabd..a1829a5a 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -66,6 +66,9 @@ something 'else', "ya" something'else' something"else" +something[[hey]] * 2 +something[======[hey]======] * 2 + here(we)"go"[12123] -- this runs diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 72d3dab6..2ad418d0 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -53,6 +53,8 @@ end) something('else', "ya") something('else') something("else") +something([[hey]] * 2) +something([======[hey]======] * 2) _ = here(we)("go")[12123] local something = { test = 12323, From 73e9df684184e91d4e010e83ad4bdd98ed73eff8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 20:59:46 -0700 Subject: [PATCH 134/344] clean out some stuff from makefile --- Makefile | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/Makefile b/Makefile index 3a4f3dba..c4d3c8d0 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,11 @@ -.PHONY: test local compile compile_system watch lint test_safe +.PHONY: test local compile compile_system watch lint test: - busted -p "_spec.moon$$" - -test_safe: - busted -p "_spec.lua$$" + busted local: compile luarocks make --local moonscript-dev-1.rockspec -global: - sudo luarocks make moonscript-dev-1.rockspec - compile: lua5.1 bin/moonc moon/ moonscript/ echo "#!/usr/bin/env lua" > bin/moon From 96406947080a38d9897408adcf85c4520a17eb5a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 21:07:22 -0700 Subject: [PATCH 135/344] rebuild spec --- spec/outputs/syntax.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 2ad418d0..3d2b625b 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -53,8 +53,8 @@ end) something('else', "ya") something('else') something("else") -something([[hey]] * 2) -something([======[hey]======] * 2) +_ = something([[hey]]) * 2 +_ = something([======[hey]======]) * 2 _ = here(we)("go")[12123] local something = { test = 12323, From ce396acb155632497f1442b40bee111dc75574fe Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 26 Sep 2015 21:16:02 -0700 Subject: [PATCH 136/344] fixes #208 --- moonscript/transform.lua | 24 +++++++++++++++--------- moonscript/transform.moon | 17 ++++++++++------- moonscript/types.lua | 7 ++++++- moonscript/types.moon | 15 +++++++++++---- spec/inputs/with.moon | 5 +++++ spec/outputs/with.lua | 9 ++++++++- 6 files changed, 55 insertions(+), 22 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index ac548dee..8590ec81 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -116,8 +116,7 @@ with_continue_listener = function(body) return end local last = last_stm(body) - local t = last and ntype(last) - local enclose_lines = t == "return" or t == "break" + local enclose_lines = types.terminating[last and ntype(last)] self:put_name(continue_name, nil) return self:splice(function(lines) if enclose_lines then @@ -653,6 +652,14 @@ Statement = Transformer({ local exp, block = unpack(node, 2) local copy_scope = true local scope_name, named_assign + do + local last = last_stm(block) + if last then + if types.terminating[ntype(last)] then + ret = false + end + end + end if ntype(exp) == "assign" then local names, values = unpack(exp, 2) local first_name = names[1] @@ -676,19 +683,18 @@ Statement = Transformer({ copy_scope = false end scope_name = scope_name or NameProxy("with") - return build["do"]({ + local out = build["do"]({ copy_scope and build.assign_one(scope_name, exp) or NOOP, named_assign or NOOP, Run(function(self) return self:set("scope_var", scope_name) end), - build.group(block), - (function() - if ret then - return ret(scope_name) - end - end)() + unpack(block) }) + if ret then + table.insert(out[2], ret(scope_name)) + end + return out end, foreach = function(self, node, _) smart_node(node) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 09e1c074..4352554c 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -74,8 +74,7 @@ with_continue_listener = (body) -> Run => return unless continue_name last = last_stm body - t = last and ntype(last) - enclose_lines = t == "return" or t == "break" + enclose_lines = types.terminating[last and ntype(last)] @put_name continue_name, nil @splice (lines) -> @@ -381,6 +380,8 @@ Statement = Transformer { copy_scope = true local scope_name, named_assign + if last = last_stm block + ret = false if types.terminating[ntype(last)] if ntype(exp) == "assign" names, values = unpack exp, 2 @@ -403,16 +404,18 @@ Statement = Transformer { scope_name or= NameProxy "with" - build.do { + out = build.do { copy_scope and build.assign_one(scope_name, exp) or NOOP named_assign or NOOP Run => @set "scope_var", scope_name - build.group block - - if ret - ret scope_name + unpack block } + if ret + table.insert out[2], ret scope_name + + out + foreach: (node, _) => smart_node node source = unpack node.iter diff --git a/moonscript/types.lua b/moonscript/types.lua index ccf1d53a..29c24527 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -19,6 +19,10 @@ local cascading = Set({ "class", "do" }) +local terminating = Set({ + "return", + "break" +}) local ntype ntype = function(node) local _exp_0 = type(node) @@ -317,5 +321,6 @@ return { value_is_singular = value_is_singular, comprehension_has_value = comprehension_has_value, has_value = has_value, - mtype = mtype + mtype = mtype, + terminating = terminating } diff --git a/moonscript/types.moon b/moonscript/types.moon index d7bf3c55..ad00a1d0 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -6,12 +6,20 @@ import insert from table import unpack from util -- implicit return does not work on these statements -manual_return = Set{"foreach", "for", "while", "return"} +manual_return = Set { + "foreach", "for", "while", "return" +} -- Assigns and returns are bubbled into their bodies. -- All cascading statement transform functions accept a second arugment that -- is the transformation to apply to the last statement in their body -cascading = Set{ "if", "unless", "with", "switch", "class", "do" } +cascading = Set { + "if", "unless", "with", "switch", "class", "do" +} + +terminating = Set { + "return", "break" +} -- type of node as string ntype = (node) -> @@ -185,7 +193,6 @@ smart_node = (node) -> { :ntype, :smart_node, :build, :is_value, :is_slice, :manual_return, - :cascading, :value_is_singular, :comprehension_has_value, :has_value, - :mtype + :cascading, :value_is_singular, :comprehension_has_value, :has_value, :mtype, :terminating } diff --git a/spec/inputs/with.moon b/spec/inputs/with.moon index 16f3574d..31ce9b5d 100644 --- a/spec/inputs/with.moon +++ b/spec/inputs/with.moon @@ -106,3 +106,8 @@ do with .b = 2 print .c +do + -> + with hi + return .a, .b + diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua index 62fbb1ca..8fcb7aa5 100644 --- a/spec/outputs/with.lua +++ b/spec/outputs/with.lua @@ -151,6 +151,13 @@ do _with_0.b = _with_1 print(_with_1.c) end - return _with_0 + end +end +do + return function() + do + local _with_0 = hi + return _with_0.a, _with_0.b + end end end \ No newline at end of file From c24acf490bf44216b8bf206b0135e230528ce52e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 29 Sep 2015 00:32:46 -0700 Subject: [PATCH 137/344] fix regression from #200 --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 2 +- moonscript/parse/util.moon | 2 +- spec/inputs/syntax.moon | 9 +++++++++ spec/outputs/syntax.lua | 6 ++++++ 5 files changed, 18 insertions(+), 3 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index d3d8c226..026734dc 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -171,7 +171,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) Slice = symx("[") * (SliceValue + Cc(1)) * sym(",") * (SliceValue + Cc("")) * (sym(",") * SliceValue) ^ -1 * sym("]") / mark("slice"), ColonCall = symx("\\") * (_Name * Invoke) / mark("colon"), ColonSuffix = symx("\\") * _Name / mark("colon_stub"), - Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + LuaString / wrap_func_arg, + Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + #P("[") * LuaString / wrap_func_arg, TableValue = KeyValue + Ct(Exp), TableLit = sym("{") * Ct(TableValueList ^ -1 * sym(",") ^ -1 * (SpaceBreak * TableLitLine * (sym(",") ^ -1 * SpaceBreak * TableLitLine) ^ 0 * sym(",") ^ -1) ^ -1) * White * sym("}") / mark("table"), TableValueList = TableValue * (sym(",") * TableValue) ^ 0, diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 7b81bb39..a2245808 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -261,7 +261,7 @@ build_grammar = wrap_env debug_grammar, (root) -> Invoke: FnArgs/mark"call" + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + - LuaString / wrap_func_arg + #P"[" * LuaString / wrap_func_arg TableValue: KeyValue + Ct(Exp) diff --git a/moonscript/parse/util.moon b/moonscript/parse/util.moon index fc680d7f..e12e1f84 100644 --- a/moonscript/parse/util.moon +++ b/moonscript/parse/util.moon @@ -109,7 +109,7 @@ format_single_assign = (lhs, assign) -> -- a symbol sym = (chars) -> Space * chars --- a symbole that doesn't accept whitespace before it +-- a symbol that doesn't accept whitespace before it symx = (chars) -> chars -- a constructor for quote delimited strings diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index a1829a5a..05ce0671 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -69,6 +69,15 @@ something"else" something[[hey]] * 2 something[======[hey]======] * 2 + +something'else', 2 +something"else", 2 +something[[else]], 2 + +something 'else', 2 +something "else", 2 +something [[else]], 2 + here(we)"go"[12123] -- this runs diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 3d2b625b..f19a3e69 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -55,6 +55,12 @@ something('else') something("else") _ = something([[hey]]) * 2 _ = something([======[hey]======]) * 2 +_ = something('else'), 2 +_ = something("else"), 2 +_ = something([[else]]), 2 +something('else', 2) +something("else", 2) +something([[else]], 2) _ = here(we)("go")[12123] local something = { test = 12323, From b9efd31af7fb5cf520e7352dde83aaa5900b3e3b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 30 Sep 2015 00:57:08 -0700 Subject: [PATCH 138/344] add another super test --- spec/inputs/class.moon | 1 + spec/outputs/class.lua | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/spec/inputs/class.moon b/spec/inputs/class.moon index 1d27e5e5..ebcca20e 100644 --- a/spec/inputs/class.moon +++ b/spec/inputs/class.moon @@ -77,6 +77,7 @@ class CoolSuper super\yeah"world".okay hi, hi, hi something.super super.super.super.super + super\hello nil diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index 35c7153d..b16ba179 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -319,6 +319,13 @@ do _parent_0.yeah(self, "world").okay(hi, hi, hi) _ = something.super _ = _parent_0.super.super.super + do + local _base_1 = _parent_0 + local _fn_0 = _base_1.hello + _ = function(...) + return _fn_0(self, ...) + end + end return nil end } From 8db100691658ac60c95fc473dda56494c6f6b69e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 27 Sep 2015 00:39:31 -0700 Subject: [PATCH 139/344] start simplifying chain ast --- moonscript/compile/value.lua | 2 +- moonscript/compile/value.moon | 2 +- moonscript/parse.lua | 21 ++++++-------- moonscript/parse.moon | 41 ++++++++++------------------ moonscript/parse/util.lua | 16 ++++------- moonscript/parse/util.moon | 18 ++++-------- moonscript/transform.lua | 12 ++++---- moonscript/transform.moon | 11 +++----- moonscript/transform/statements.lua | 8 +++++- moonscript/transform/statements.moon | 5 +++- 10 files changed, 58 insertions(+), 78 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 8f8e8f23..07220247 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -95,7 +95,7 @@ return { elseif t == "dot" then return ".", tostring(arg) elseif t == "colon" then - return ":", arg, chain_item(node[3]) + return ":", tostring(arg) elseif t == "colon_stub" then return user_error("Uncalled colon stub") else diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 515fed87..c8f9891b 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -64,7 +64,7 @@ string_chars = { elseif t == "dot" ".", tostring arg elseif t == "colon" - ":", arg, chain_item(node[3]) + ":", tostring arg elseif t == "colon_stub" user_error "Uncalled colon stub" else diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 026734dc..13fb8aa8 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -27,10 +27,10 @@ Num = Space * (Num / function(v) v } end) -local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, flatten_func, flatten_string_chain, wrap_decorator, check_lua_string, self_assign +local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, flatten_string_chain, wrap_decorator, check_lua_string, self_assign do local _obj_0 = require("moonscript.parse.util") - Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, flatten_func, flatten_string_chain, wrap_decorator, check_lua_string, self_assign = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.flatten_func, _obj_0.flatten_string_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign + Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, flatten_string_chain, wrap_decorator, check_lua_string, self_assign = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.join_chain, _obj_0.flatten_string_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign end local build_grammar = wrap_env(debug_grammar, function(root) local _indent = Stack(0) @@ -148,13 +148,12 @@ local build_grammar = wrap_env(debug_grammar, function(root) CharOperators = Space * C(S("+-*/%^><")), WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op(".."), BinaryOperator = (WordOperators + CharOperators) * SpaceBreak ^ 0, - Assignable = Cmt(DotChain + Chain, check_assignable) + Name + SelfName, + Assignable = Cmt(Chain, check_assignable) + Name + SelfName, Exp = Ct(Value * (BinaryOperator * Value) ^ 0) / flatten_or_mark("exp"), SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num, - ChainValue = StringChain + ((Chain + DotChain + Callable) * Ct(InvokeArgs ^ -1)) / flatten_func, - Value = pos(SimpleValue + Ct(KeyValueList) / mark("table") + ChainValue), + ChainValue = (Chain + Callable) * Ct(InvokeArgs ^ -1) / join_chain, + Value = pos(SimpleValue + Ct(KeyValueList) / mark("table") + ChainValue + String), SliceValue = SimpleValue + ChainValue, - StringChain = String * (Ct((ColonCall + ColonSuffix) * ChainTail ^ -1) * Ct(InvokeArgs ^ -1)) ^ -1 / flatten_string_chain, String = Space * DoubleString + Space * SingleString + LuaString, SingleString = simple_string("'"), DoubleString = simple_string('"', true), @@ -164,13 +163,11 @@ local build_grammar = wrap_env(debug_grammar, function(root) Callable = pos(Name / mark("ref")) + SelfName + VarArg + Parens / mark("parens"), Parens = sym("(") * SpaceBreak ^ 0 * Exp * SpaceBreak ^ 0 * sym(")"), FnArgs = symx("(") * SpaceBreak ^ 0 * Ct(ExpList ^ -1) * SpaceBreak ^ 0 * sym(")") + sym("!") * -P("=") * Ct(""), - ChainTail = ChainItem ^ 1 * ColonSuffix ^ -1 + ColonSuffix, - Chain = Callable * ChainTail / mark("chain"), - DotChain = (sym(".") * Cc(-1) * (_Name / mark("dot")) * ChainTail ^ -1) / mark("chain") + (sym("\\") * Cc(-1) * ((_Name * Invoke / mark("colon")) * ChainTail ^ -1 + (_Name / mark("colon_stub")))) / mark("chain"), - ChainItem = Invoke + Slice + symx("[") * Exp / mark("index") * sym("]") + symx(".") * _Name / mark("dot") + ColonCall, + Chain = (Callable + String + -S(".\\")) * ChainItems / mark("chain"), + ChainItems = ChainItem ^ 1 * ColonChainItem ^ -1 + ColonChainItem, + ChainItem = Invoke + symx(".") * _Name / mark("dot") + Slice + symx("[") * Exp / mark("index") * sym("]"), + ColonChainItem = symx("\\") * _Name / mark("colon") * (Invoke * ChainItems ^ -1) ^ -1, Slice = symx("[") * (SliceValue + Cc(1)) * sym(",") * (SliceValue + Cc("")) * (sym(",") * SliceValue) ^ -1 * sym("]") / mark("slice"), - ColonCall = symx("\\") * (_Name * Invoke) / mark("colon"), - ColonSuffix = symx("\\") * _Name / mark("colon_stub"), Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + #P("[") * LuaString / wrap_func_arg, TableValue = KeyValue + Ct(Exp), TableLit = sym("{") * Ct(TableValueList ^ -1 * sym(",") ^ -1 * (SpaceBreak * TableLitLine * (sym(",") ^ -1 * SpaceBreak * TableLitLine) ^ 0 * sym(",") ^ -1) ^ -1) * White * sym("}") / mark("table"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index a2245808..dac6b813 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -3,6 +3,7 @@ lpeg = require "lpeg" lpeg.setmaxstack 10000 -- whoa + err_msg = "Failed to parse:%s\n [%d] >> %s" import Stack from require "moonscript.data" @@ -27,7 +28,7 @@ Num = Space * (Num / (v) -> {"number", v}) { :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, :is_assignable, :check_assignable, :format_assign, :format_single_assign, - :sym, :symx, :simple_string, :wrap_func_arg, :flatten_func, + :sym, :symx, :simple_string, :wrap_func_arg, :join_chain, :flatten_string_chain, :wrap_decorator, :check_lua_string, :self_assign } = require "moonscript.parse.util" @@ -183,7 +184,7 @@ build_grammar = wrap_env debug_grammar, (root) -> WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." BinaryOperator: (WordOperators + CharOperators) * SpaceBreak^0 - Assignable: Cmt(DotChain + Chain, check_assignable) + Name + SelfName + Assignable: Cmt(Chain, check_assignable) + Name + SelfName Exp: Ct(Value * (BinaryOperator * Value)^0) / flatten_or_mark"exp" SimpleValue: @@ -202,20 +203,17 @@ build_grammar = wrap_env debug_grammar, (root) -> FunLit + Num - ChainValue: -- a function call or an object access - StringChain + - ((Chain + DotChain + Callable) * Ct(InvokeArgs^-1)) / flatten_func + -- a function call or an object access + ChainValue: (Chain + Callable) * Ct(InvokeArgs^-1) / join_chain Value: pos( SimpleValue + Ct(KeyValueList) / mark"table" + - ChainValue) + ChainValue + + String) SliceValue: SimpleValue + ChainValue - StringChain: String * - (Ct((ColonCall + ColonSuffix) * ChainTail^-1) * Ct(InvokeArgs^-1))^-1 / flatten_string_chain - String: Space * DoubleString + Space * SingleString + LuaString SingleString: simple_string("'") DoubleString: simple_string('"', true) @@ -232,33 +230,23 @@ build_grammar = wrap_env debug_grammar, (root) -> FnArgs: symx"(" * SpaceBreak^0 * Ct(ExpList^-1) * SpaceBreak^0 * sym")" + sym"!" * -P"=" * Ct"" - ChainTail: ChainItem^1 * ColonSuffix^-1 + ColonSuffix - -- a list of funcalls and indexes on a callable - Chain: Callable * ChainTail / mark"chain" + Chain: (Callable + String + -S".\\") * ChainItems / mark"chain" - -- shorthand dot call for use in with statement - DotChain: - (sym"." * Cc(-1) * (_Name / mark"dot") * ChainTail^-1) / mark"chain" + - (sym"\\" * Cc(-1) * ( - (_Name * Invoke / mark"colon") * ChainTail^-1 + - (_Name / mark"colon_stub") - )) / mark"chain" + ChainItems: ChainItem^1 * ColonChainItem^-1 + ColonChainItem ChainItem: Invoke + - Slice + - symx"[" * Exp/mark"index" * sym"]" + symx"." * _Name/mark"dot" + - ColonCall + Slice + + symx"[" * Exp/mark"index" * sym"]" + + ColonChainItem: symx"\\" * _Name / mark"colon" * (Invoke * ChainItems^-1)^-1 Slice: symx"[" * (SliceValue + Cc(1)) * sym"," * (SliceValue + Cc"") * (sym"," * SliceValue)^-1 *sym"]" / mark"slice" - ColonCall: symx"\\" * (_Name * Invoke) / mark"colon" - ColonSuffix: symx"\\" * _Name / mark"colon_stub" - - Invoke: FnArgs/mark"call" + + Invoke: FnArgs / mark"call" + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + #P"[" * LuaString / wrap_func_arg @@ -314,6 +302,7 @@ build_grammar = wrap_env debug_grammar, (root) -> ExpList: Exp * (sym"," * Exp)^0 ExpListLow: Exp * ((sym"," + sym";") * Exp)^0 + -- open args InvokeArgs: -P"-" * (ExpList * (sym"," * (TableBlock + SpaceBreak * Advance * ArgBlock * TableBlock^-1) + TableBlock)^-1 + TableBlock) ArgBlock: ArgLine * (sym"," * SpaceBreak * ArgLine)^0 * PopIndent ArgLine: CheckIndent * ExpList diff --git a/moonscript/parse/util.lua b/moonscript/parse/util.lua index 6db63f06..a93665dc 100644 --- a/moonscript/parse/util.lua +++ b/moonscript/parse/util.lua @@ -178,8 +178,8 @@ wrap_func_arg = function(value) } } end -local flatten_func -flatten_func = function(callee, args) +local join_chain +join_chain = function(callee, args) if #args == 0 then return callee end @@ -188,13 +188,7 @@ flatten_func = function(callee, args) args } if ntype(callee) == "chain" then - local stub = callee[#callee] - if ntype(stub) == "colon_stub" then - stub[1] = "colon" - table.insert(stub, args) - else - table.insert(callee, args) - end + table.insert(callee, args) return callee end return { @@ -208,7 +202,7 @@ flatten_string_chain = function(str, chain, args) if not (chain) then return str end - return flatten_func({ + return flatten_chain({ "chain", str, unpack(chain) @@ -259,7 +253,7 @@ return { symx = symx, simple_string = simple_string, wrap_func_arg = wrap_func_arg, - flatten_func = flatten_func, + join_chain = join_chain, flatten_string_chain = flatten_string_chain, wrap_decorator = wrap_decorator, check_lua_string = check_lua_string, diff --git a/moonscript/parse/util.moon b/moonscript/parse/util.moon index e12e1f84..2039e3bd 100644 --- a/moonscript/parse/util.moon +++ b/moonscript/parse/util.moon @@ -127,27 +127,21 @@ simple_string = (delim, allow_interpolation) -> -- wraps a single value in format needed to be passed as function arguments wrap_func_arg = (value) -> {"call", {value}} --- flatten out the parsed function node -flatten_func = (callee, args) -> +-- chains are parsed in two captures, the chain and then the open arguments +-- if there are open arguments, then append them to the end of the chain as a call +join_chain = (callee, args) -> return callee if #args == 0 args = {"call", args} if ntype(callee) == "chain" - -- check for colon stub needing arguments - stub = callee[#callee] - if ntype(stub) == "colon_stub" - stub[1] = "colon" - table.insert stub, args - else - table.insert callee, args - + table.insert callee, args return callee {"chain", callee, args} flatten_string_chain = (str, chain, args) -> return str unless chain - flatten_func {"chain", str, unpack chain}, args + flatten_chain {"chain", str, unpack chain}, args -- constructor for decorator node wrap_decorator = (stm, dec) -> @@ -163,5 +157,5 @@ self_assign = (name, pos) -> { :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, :is_assignable, :check_assignable, :format_assign, :format_single_assign, - :sym, :symx, :simple_string, :wrap_func_arg, :flatten_func, + :sym, :symx, :simple_string, :wrap_func_arg, :join_chain, :flatten_string_chain, :wrap_decorator, :check_lua_string, :self_assign } diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 8590ec81..0b1e1d0b 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -12,10 +12,10 @@ do local _obj_0 = require("moonscript.transform.names") NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName end -local Run, transform_last_stm, last_stm +local Run, transform_last_stm, last_stm, chain_is_stub do local _obj_0 = require("moonscript.transform.statements") - Run, transform_last_stm, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.last_stm + Run, transform_last_stm, last_stm, chain_is_stub = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.last_stm, _obj_0.chain_is_stub end local destructure = require("moonscript.transform.destructure") local NOOP = { @@ -1498,7 +1498,6 @@ Value = Transformer({ }) end, chain = function(self, node) - local stub = node[#node] for i = 3, #node do local part = node[i] if ntype(part) == "dot" and data.lua_keywords[part[2]] then @@ -1517,10 +1516,11 @@ Value = Transformer({ "parens", node[2] } - elseif type(stub) == "table" and stub[1] == "colon_stub" then - table.remove(node, #node) + end + if chain_is_stub(node) then local base_name = NameProxy("base") local fn_name = NameProxy("fn") + local colon = table.remove(node) local is_super = ntype(node[2]) == "ref" and node[2][2] == "super" return build.block_exp({ build.assign({ @@ -1540,7 +1540,7 @@ Value = Transformer({ base = base_name, { "dot", - stub[2] + colon[2] } }) } diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 4352554c..9be3292c 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -8,7 +8,7 @@ import ntype, mtype, build, smart_node, is_slice, value_is_singular from types import insert from table import NameProxy, LocalName from require "moonscript.transform.names" -import Run, transform_last_stm, last_stm from require "moonscript.transform.statements" +import Run, transform_last_stm, last_stm, chain_is_stub from require "moonscript.transform.statements" destructure = require "moonscript.transform.destructure" NOOP = {"noop"} @@ -910,8 +910,6 @@ Value = Transformer { -- pull out colon chain chain: (node) => - stub = node[#node] - -- escape lua keywords used in dot accessors for i=3,#node part = node[i] @@ -921,12 +919,11 @@ Value = Transformer { if ntype(node[2]) == "string" -- add parens if callee is raw string node[2] = {"parens", node[2] } - elseif type(stub) == "table" and stub[1] == "colon_stub" - -- convert colon stub into code - table.remove node, #node + if chain_is_stub node base_name = NameProxy "base" fn_name = NameProxy "fn" + colon = table.remove node is_super = ntype(node[2]) == "ref" and node[2][2] == "super" build.block_exp { @@ -938,7 +935,7 @@ Value = Transformer { build.assign { names: {fn_name} values: { - build.chain { base: base_name, {"dot", stub[2]} } + build.chain { base: base_name, {"dot", colon[2]} } } } diff --git a/moonscript/transform/statements.lua b/moonscript/transform/statements.lua index f71aede5..169590ea 100644 --- a/moonscript/transform/statements.lua +++ b/moonscript/transform/statements.lua @@ -68,8 +68,14 @@ transform_last_stm = function(stms, fn) return _accum_0 end)() end +local chain_is_stub +chain_is_stub = function(chain) + local stub = chain[#chain] + return stub and ntype(stub) == "colon" +end return { Run = Run, last_stm = last_stm, - transform_last_stm = transform_last_stm + transform_last_stm = transform_last_stm, + chain_is_stub = chain_is_stub } diff --git a/moonscript/transform/statements.moon b/moonscript/transform/statements.moon index 0e3baeb7..59d8e364 100644 --- a/moonscript/transform/statements.moon +++ b/moonscript/transform/statements.moon @@ -40,6 +40,9 @@ transform_last_stm = (stms, fn) -> else stm +chain_is_stub = (chain) -> + stub = chain[#chain] + stub and ntype(stub) == "colon" -{:Run, :last_stm, :transform_last_stm} +{:Run, :last_stm, :transform_last_stm, :chain_is_stub} From 6a057b6b5605f5965ebdc511d648840206ce9015 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 29 Sep 2015 02:17:40 -0700 Subject: [PATCH 140/344] show preview of buffer on non-terminal debugger --- moonscript/parse/env.lua | 5 +++-- moonscript/parse/env.moon | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/moonscript/parse/env.lua b/moonscript/parse/env.lua index 6dbed84a..f805211b 100644 --- a/moonscript/parse/env.lua +++ b/moonscript/parse/env.lua @@ -34,8 +34,9 @@ wrap_env = function(debug, fn) end wrap_name = function(name) local v = V(name) - v = Cmt("", function() - iprint("* " .. name) + v = Cmt("", function(str, pos) + local rest = str:sub(pos, -1):match("^([^\n]*)") + iprint("* " .. tostring(name) .. " (" .. tostring(rest) .. ")") indent = indent + 1 return true end) * Cmt(v, function(str, pos, ...) diff --git a/moonscript/parse/env.moon b/moonscript/parse/env.moon index 25dcc1e4..ea71c3df 100644 --- a/moonscript/parse/env.moon +++ b/moonscript/parse/env.moon @@ -18,8 +18,10 @@ wrap_env = (debug, fn) -> wrap_name = (name) -> v = V name - v = Cmt("", -> - iprint "* " .. name + v = Cmt("", (str, pos) -> + rest = str\sub(pos, -1)\match "^([^\n]*)" + + iprint "* #{name} (#{rest})" indent += 1 true ) * Cmt(v, (str, pos, ...) -> From e94494721a2c63050ca44546845e719855c8caba Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 29 Sep 2015 02:18:58 -0700 Subject: [PATCH 141/344] bring back dot/colon prefix chain --- moonscript/compile/value.lua | 8 +++++--- moonscript/compile/value.moon | 9 ++++++--- moonscript/parse.lua | 10 ++++++---- moonscript/parse.moon | 12 +++++++----- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 07220247..3641cc9d 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -71,11 +71,13 @@ return { chain = function(self, node) local callee = node[2] local callee_type = ntype(callee) - if callee == -1 then + local item_offset = 3 + if callee_type == "dot" or callee_type == "colon" then callee = self:get("scope_var") - if not callee then + if not (callee) then user_error("Short-dot syntax must be called within a with block") end + item_offset = 2 end if callee_type == "ref" and callee[2] == "super" or callee == "super" then do @@ -112,7 +114,7 @@ return { local actions do local _with_0 = self:line() - for _index_0 = 3, #node do + for _index_0 = item_offset, #node do local action = node[_index_0] _with_0:append(chain_item(action)) end diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index c8f9891b..bbe0d230 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -44,10 +44,13 @@ string_chars = { chain: (node) => callee = node[2] callee_type = ntype callee + item_offset = 3 - if callee == -1 + if callee_type == "dot" or callee_type == "colon" callee = @get "scope_var" - if not callee then user_error "Short-dot syntax must be called within a with block" + unless callee + user_error "Short-dot syntax must be called within a with block" + item_offset = 2 -- TODO: don't use string literals as ref if callee_type == "ref" and callee[2] == "super" or callee == "super" @@ -77,7 +80,7 @@ string_chars = { callee_value = @line "(", callee_value, ")" if ntype(callee) == "exp" actions = with @line! - \append chain_item action for action in *node[3,] + \append chain_item action for action in *node[item_offset,] @line callee_value, actions diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 13fb8aa8..30846db7 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -163,10 +163,12 @@ local build_grammar = wrap_env(debug_grammar, function(root) Callable = pos(Name / mark("ref")) + SelfName + VarArg + Parens / mark("parens"), Parens = sym("(") * SpaceBreak ^ 0 * Exp * SpaceBreak ^ 0 * sym(")"), FnArgs = symx("(") * SpaceBreak ^ 0 * Ct(ExpList ^ -1) * SpaceBreak ^ 0 * sym(")") + sym("!") * -P("=") * Ct(""), - Chain = (Callable + String + -S(".\\")) * ChainItems / mark("chain"), - ChainItems = ChainItem ^ 1 * ColonChainItem ^ -1 + ColonChainItem, - ChainItem = Invoke + symx(".") * _Name / mark("dot") + Slice + symx("[") * Exp / mark("index") * sym("]"), - ColonChainItem = symx("\\") * _Name / mark("colon") * (Invoke * ChainItems ^ -1) ^ -1, + Chain = (Callable + String + -S(".\\")) * ChainItems / mark("chain") + Space * (DotChainItem * ChainItems ^ -1 + ColonChain) / mark("chain"), + ChainItems = ChainItem ^ 1 * ColonChain ^ -1 + ColonChain, + ChainItem = Invoke + DotChainItem + Slice + symx("[") * Exp / mark("index") * sym("]"), + DotChainItem = symx(".") * _Name / mark("dot"), + ColonChainItem = symx("\\") * _Name / mark("colon"), + ColonChain = ColonChainItem * (Invoke * ChainItems ^ -1) ^ -1, Slice = symx("[") * (SliceValue + Cc(1)) * sym(",") * (SliceValue + Cc("")) * (sym(",") * SliceValue) ^ -1 * sym("]") / mark("slice"), Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + #P("[") * LuaString / wrap_func_arg, TableValue = KeyValue + Ct(Exp), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index dac6b813..ca275570 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -230,18 +230,20 @@ build_grammar = wrap_env debug_grammar, (root) -> FnArgs: symx"(" * SpaceBreak^0 * Ct(ExpList^-1) * SpaceBreak^0 * sym")" + sym"!" * -P"=" * Ct"" - -- a list of funcalls and indexes on a callable - Chain: (Callable + String + -S".\\") * ChainItems / mark"chain" + Chain: (Callable + String + -S".\\") * ChainItems / mark"chain" + + Space * (DotChainItem * ChainItems^-1 + ColonChain) / mark"chain" - ChainItems: ChainItem^1 * ColonChainItem^-1 + ColonChainItem + ChainItems: ChainItem^1 * ColonChain^-1 + ColonChain ChainItem: Invoke + - symx"." * _Name/mark"dot" + + DotChainItem + Slice + symx"[" * Exp/mark"index" * sym"]" - ColonChainItem: symx"\\" * _Name / mark"colon" * (Invoke * ChainItems^-1)^-1 + DotChainItem: symx"." * _Name/mark"dot" + ColonChainItem: symx"\\" * _Name / mark"colon" + ColonChain: ColonChainItem * (Invoke * ChainItems^-1)^-1 Slice: symx"[" * (SliceValue + Cc(1)) * sym"," * (SliceValue + Cc"") * (sym"," * SliceValue)^-1 *sym"]" / mark"slice" From 02a20ea69545307865a0c273449d072569042119 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 29 Sep 2015 20:47:27 -0700 Subject: [PATCH 142/344] add some specs for name extract --- moonscript/transform/destructure.lua | 3 +- moonscript/transform/destructure.moon | 2 +- spec/transform_spec.moon | 49 +++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/moonscript/transform/destructure.lua b/moonscript/transform/destructure.lua index c0542953..6798c611 100644 --- a/moonscript/transform/destructure.lua +++ b/moonscript/transform/destructure.lua @@ -228,5 +228,6 @@ end return { has_destructure = has_destructure, split_assign = split_assign, - build_assign = build_assign + build_assign = build_assign, + extract_assign_names = extract_assign_names } diff --git a/moonscript/transform/destructure.moon b/moonscript/transform/destructure.moon index ca80bc25..eef2852e 100644 --- a/moonscript/transform/destructure.moon +++ b/moonscript/transform/destructure.moon @@ -123,4 +123,4 @@ split_assign = (scope, assign) -> build.group g -{ :has_destructure, :split_assign, :build_assign } +{ :has_destructure, :split_assign, :build_assign, :extract_assign_names } diff --git a/spec/transform_spec.moon b/spec/transform_spec.moon index b4de4047..dba44d04 100644 --- a/spec/transform_spec.moon +++ b/spec/transform_spec.moon @@ -1,6 +1,55 @@ import with_dev from require "spec.helpers" +describe "moonscript.transform.destructure", -> + local extract_assign_names + + with_dev -> + { :extract_assign_names } = require "moonscript.transform.destructure" + + it "extracts names from table destructure", -> + des = { + "table" + { + {{"key_literal", "hi"}, {"ref", "hi"}} + {{"key_literal", "world"}, {"ref", "world"}} + } + } + + assert.same { + { + {"ref", "hi"} -- target + { + {"dot", "hi"} + } -- chain suffix + } + + { + {"ref", "world"} + { + {"dot", "world"} + } + } + + }, extract_assign_names des + + it "extracts names from array destructure", -> + des = { + "table" + { + {{"ref", "hi"}} + } + } + + assert.same { + { + {"ref", "hi"} + { + {"index", {"number", 1}} + } + } + }, extract_assign_names des + describe "moonscript.transform.statements", -> local last_stm, transform_last_stm, Run From 2f71fbc13f18d1c2c6628aa6470b9adaa8da72e4 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 29 Sep 2015 21:06:51 -0700 Subject: [PATCH 143/344] use colon instead of colon stub for bound import --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 2 +- moonscript/transform.lua | 10 +++++----- moonscript/transform.moon | 4 ++-- moonscript/transform/destructure.lua | 2 +- moonscript/transform/destructure.moon | 4 +++- 6 files changed, 13 insertions(+), 11 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 30846db7..53c12f05 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -120,7 +120,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) InBlock = Advance * Block * PopIndent, Local = key("local") * ((op("*") + op("^")) / mark("declare_glob") + Ct(NameList) / mark("declare_with_shadows")), Import = key("import") * Ct(ImportNameList) * SpaceBreak ^ 0 * key("from") * Exp / mark("import"), - ImportName = (sym("\\") * Ct(Cc("colon_stub") * Name) + Name), + ImportName = (sym("\\") * Ct(Cc("colon") * Name) + Name), ImportNameList = SpaceBreak ^ 0 * ImportName * ((SpaceBreak ^ 1 + sym(",") * SpaceBreak ^ 0) * ImportName) ^ 0, BreakLoop = Ct(key("break") / trim) + Ct(key("continue") / trim), Return = key("return") * (ExpListLow / mark("explist") + C("")) / mark("return"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index ca275570..96c26e5f 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -134,7 +134,7 @@ build_grammar = wrap_env debug_grammar, (root) -> Local: key"local" * ((op"*" + op"^") / mark"declare_glob" + Ct(NameList) / mark"declare_with_shadows") Import: key"import" * Ct(ImportNameList) * SpaceBreak^0 * key"from" * Exp / mark"import" - ImportName: (sym"\\" * Ct(Cc"colon_stub" * Name) + Name) + ImportName: (sym"\\" * Ct(Cc"colon" * Name) + Name) ImportNameList: SpaceBreak^0 * ImportName * ((SpaceBreak^1 + sym"," * SpaceBreak^0) * ImportName)^0 BreakLoop: Ct(key"break"/trim) + Ct(key"continue"/trim) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 0b1e1d0b..a378674d 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -484,18 +484,18 @@ Statement = Transformer({ local _len_0 = 1 for _index_0 = 1, #names do local name = names[_index_0] - local dest_val - if ntype(name) == "colon_stub" then - dest_val = name[2] + local dest_name + if ntype(name) == "colon" then + dest_name = name[2] else - dest_val = name + dest_name = name end local _value_0 = { { "key_literal", name }, - dest_val + dest_name } _accum_0[_len_0] = _value_0 _len_0 = _len_0 + 1 diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 9be3292c..accc1530 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -291,12 +291,12 @@ Statement = Transformer { import: (node) => _, names, source = unpack node table_values = for name in *names - dest_val = if ntype(name) == "colon_stub" + dest_name = if ntype(name) == "colon" name[2] else name - {{"key_literal", name}, dest_val} + {{"key_literal", name}, dest_name} dest = { "table", table_values } { "assign", {dest}, {source}, [-1]: node[-1] } diff --git a/moonscript/transform/destructure.lua b/moonscript/transform/destructure.lua index 6798c611..2ec46bef 100644 --- a/moonscript/transform/destructure.lua +++ b/moonscript/transform/destructure.lua @@ -68,7 +68,7 @@ extract_assign_names = function(name, accum, prefix) local s if ntype(key) == "key_literal" then local key_name = key[2] - if ntype(key_name) == "colon_stub" then + if ntype(key_name) == "colon" then s = key_name else s = { diff --git a/moonscript/transform/destructure.moon b/moonscript/transform/destructure.moon index eef2852e..5b22247a 100644 --- a/moonscript/transform/destructure.moon +++ b/moonscript/transform/destructure.moon @@ -20,6 +20,7 @@ has_destructure = (names) -> false extract_assign_names = (name, accum={}, prefix={}) -> + i = 1 for tuple in *name[2] value, suffix = if #tuple == 1 @@ -28,9 +29,10 @@ extract_assign_names = (name, accum={}, prefix={}) -> tuple[1], s else key = tuple[1] + s = if ntype(key) == "key_literal" key_name = key[2] - if ntype(key_name) == "colon_stub" + if ntype(key_name) == "colon" key_name else {"dot", key_name} From e4c4e8e0eead566dacbb40b99cc876b30c90d804 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 30 Sep 2015 01:05:20 -0700 Subject: [PATCH 144/344] update super for new chain ast --- moonscript/transform.lua | 46 +++++++++++++++++---------------------- moonscript/transform.moon | 28 +++++++++++++++++------- 2 files changed, 40 insertions(+), 34 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index a378674d..7ff298b8 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -1099,29 +1099,21 @@ Statement = Transformer({ end return self:set("super", function(block, chain) if chain then - local slice - do - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 3, #chain do - local item = chain[_index_0] - _accum_0[_len_0] = item - _len_0 = _len_0 + 1 - end - slice = _accum_0 - end + local chain_tail = { + unpack(chain, 3) + } local new_chain = { "chain", parent_cls_name } - local head = slice[1] + local head = chain_tail[1] if head == nil then return parent_cls_name end local _exp_1 = head[1] if "call" == _exp_1 then local calling_name = block:get("current_block") - slice[1] = { + chain_tail[1] = { "call", { "self", @@ -1140,21 +1132,23 @@ Statement = Transformer({ }) end elseif "colon" == _exp_1 then - local call = head[3] - insert(new_chain, { - "dot", - head[2] - }) - slice[1] = { - "call", - { - "self", - unpack(call[2]) + local call = chain_tail[2] + if call and call[1] == "call" then + chain_tail[1] = { + "dot", + head[2] } - } + chain_tail[2] = { + "call", + { + "self", + unpack(call[2]) + } + } + end end - for _index_0 = 1, #slice do - local item = slice[_index_0] + for _index_0 = 1, #chain_tail do + local item = chain_tail[_index_0] insert(new_chain, item) end return new_chain diff --git a/moonscript/transform.moon b/moonscript/transform.moon index accc1530..528a5189 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -661,10 +661,10 @@ Statement = Transformer { @set "super", (block, chain) -> if chain - slice = [item for item in *chain[3,]] + chain_tail = { unpack chain, 3 } new_chain = {"chain", parent_cls_name} - head = slice[1] + head = chain_tail[1] if head == nil return parent_cls_name @@ -673,7 +673,7 @@ Statement = Transformer { -- calling super, inject calling name and self into chain when "call" calling_name = block\get"current_block" - slice[1] = {"call", {"self", unpack head[2]}} + chain_tail[1] = {"call", {"self", unpack head[2]}} if ntype(calling_name) == "key_literal" insert new_chain, {"dot", calling_name[2]} @@ -682,11 +682,23 @@ Statement = Transformer { -- colon call on super, replace class with self as first arg when "colon" - call = head[3] - insert new_chain, {"dot", head[2]} - slice[1] = { "call", { "self", unpack call[2] } } - - insert new_chain, item for item in *slice + call = chain_tail[2] + -- calling chain tail + if call and call[1] == "call" + chain_tail[1] = { + "dot" + head[2] + } + + chain_tail[2] = { + "call" + { + "self" + unpack call[2] + } + } + + insert new_chain, item for item in *chain_tail new_chain else From c4ad47c1123c303eeec1ebd3e00973f2297b6da8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 5 Oct 2015 20:19:36 -0700 Subject: [PATCH 145/344] replace has_value with functional value_can_be_statement --- moonscript/compile.lua | 10 +++++----- moonscript/compile.moon | 10 +++++----- moonscript/types.lua | 14 ++++++-------- moonscript/types.moon | 15 +++++++-------- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 5eab8e6b..98045ce2 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -8,10 +8,10 @@ do end local Set Set = require("moonscript.data").Set -local ntype, has_value +local ntype, value_can_be_statement do local _obj_0 = require("moonscript.types") - ntype, has_value = _obj_0.ntype, _obj_0.has_value + ntype, value_can_be_statement = _obj_0.ntype, _obj_0.value_can_be_statement end local statement_compilers = require("moonscript.compile.statement") local value_compilers = require("moonscript.compile.value") @@ -525,7 +525,9 @@ do if fn then result = fn(self, node, ...) else - if has_value(node) then + if value_can_be_statement(node) then + result = self:value(node) + else result = self:stm({ "assign", { @@ -535,8 +537,6 @@ do node } }) - else - result = self:value(node) end end end diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 6206b264..a2b17fee 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -5,7 +5,7 @@ transform = require "moonscript.transform" import NameProxy, LocalName from require "moonscript.transform.names" import Set from require "moonscript.data" -import ntype, has_value from require "moonscript.types" +import ntype, value_can_be_statement from require "moonscript.types" statement_compilers = require "moonscript.compile.statement" value_compilers = require "moonscript.compile.value" @@ -379,11 +379,11 @@ class Block result = if fn = @statement_compilers[ntype(node)] fn @, node, ... else - -- coerce value into statement - if has_value node - @stm {"assign", {"_"}, {node}} - else + if value_can_be_statement node @value node + else + -- coerce value into statement + @stm {"assign", {"_"}, {node}} if result if type(node) == "table" and type(result) == "table" and node[-1] diff --git a/moonscript/types.lua b/moonscript/types.lua index 29c24527..a8bd6c70 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -45,14 +45,12 @@ do return moon_type(val) end end -local has_value -has_value = function(node) - if ntype(node) == "chain" then - local ctype = ntype(node[#node]) - return ctype ~= "call" and ctype ~= "colon" - else - return true +local value_can_be_statement +value_can_be_statement = function(node) + if not (ntype(node) == "chain") then + return false end + return ntype(node[#node]) == "call" end local is_value is_value = function(stm) @@ -320,7 +318,7 @@ return { cascading = cascading, value_is_singular = value_is_singular, comprehension_has_value = comprehension_has_value, - has_value = has_value, + value_can_be_statement = value_can_be_statement, mtype = mtype, terminating = terminating } diff --git a/moonscript/types.moon b/moonscript/types.moon index ad00a1d0..abac3110 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -40,13 +40,11 @@ mtype = do return "table" if mt and mt.smart_node moon_type val --- does this always return a value -has_value = (node) -> - if ntype(node) == "chain" - ctype = ntype(node[#node]) - ctype != "call" and ctype != "colon" - else - true +-- can this value be compiled in a line by itself +value_can_be_statement = (node) -> + return false unless ntype(node) == "chain" + -- it's a function call + ntype(node[#node]) == "call" is_value = (stm) -> compile = require "moonscript.compile" @@ -193,6 +191,7 @@ smart_node = (node) -> { :ntype, :smart_node, :build, :is_value, :is_slice, :manual_return, - :cascading, :value_is_singular, :comprehension_has_value, :has_value, :mtype, :terminating + :cascading, :value_is_singular, :comprehension_has_value, + :value_can_be_statement, :mtype, :terminating } From 24b3a86c68c505ec31040e6c8529bd1e0e8b690e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 5 Oct 2015 20:53:54 -0700 Subject: [PATCH 146/344] correct escape new chain format for lua keywords --- moonscript/compile/value.lua | 2 +- moonscript/compile/value.moon | 2 +- moonscript/transform.lua | 2 +- moonscript/transform.moon | 2 +- spec/inputs/with.moon | 5 +++++ spec/outputs/with.lua | 11 ++++++++++- 6 files changed, 19 insertions(+), 5 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 3641cc9d..82cc7304 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -72,7 +72,7 @@ return { local callee = node[2] local callee_type = ntype(callee) local item_offset = 3 - if callee_type == "dot" or callee_type == "colon" then + if callee_type == "dot" or callee_type == "colon" or callee_type == "index" then callee = self:get("scope_var") if not (callee) then user_error("Short-dot syntax must be called within a with block") diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index bbe0d230..fb5c2253 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -46,7 +46,7 @@ string_chars = { callee_type = ntype callee item_offset = 3 - if callee_type == "dot" or callee_type == "colon" + if callee_type == "dot" or callee_type == "colon" or callee_type == "index" callee = @get "scope_var" unless callee user_error "Short-dot syntax must be called within a with block" diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 7ff298b8..7a2d18d4 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -1492,7 +1492,7 @@ Value = Transformer({ }) end, chain = function(self, node) - for i = 3, #node do + for i = 2, #node do local part = node[i] if ntype(part) == "dot" and data.lua_keywords[part[2]] then node[i] = { diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 528a5189..4ad3b66c 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -923,7 +923,7 @@ Value = Transformer { -- pull out colon chain chain: (node) => -- escape lua keywords used in dot accessors - for i=3,#node + for i=2,#node part = node[i] if ntype(part) == "dot" and data.lua_keywords[part[2]] node[i] = { "index", {"string", '"', part[2]} } diff --git a/spec/inputs/with.moon b/spec/inputs/with.moon index 31ce9b5d..ae3c8c05 100644 --- a/spec/inputs/with.moon +++ b/spec/inputs/with.moon @@ -111,3 +111,8 @@ do with hi return .a, .b + +do + with dad + .if "yes" + y = .end.of.function diff --git a/spec/outputs/with.lua b/spec/outputs/with.lua index 8fcb7aa5..3b2eec3b 100644 --- a/spec/outputs/with.lua +++ b/spec/outputs/with.lua @@ -154,10 +154,19 @@ do end end do - return function() + local _ + _ = function() do local _with_0 = hi return _with_0.a, _with_0.b end end +end +do + do + local _with_0 = dad + _with_0["if"]("yes") + local y = _with_0["end"].of["function"] + return _with_0 + end end \ No newline at end of file From 11f0b022c49cc0cd7e1b0e44c98153042dcac9b0 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Nov 2015 18:01:22 -0800 Subject: [PATCH 147/344] use relative reference to parent class instead of closure --- moonscript/compile.lua | 7 ++-- moonscript/transform.moon | 35 ++++++++++++++++--- spec/class_spec.moon | 71 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 102 insertions(+), 11 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 98045ce2..dad4b79a 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -638,7 +638,7 @@ do __init = function(self, options) self.options = options self.root = self - return _parent_0.__init(self) + return self.__class.__parent.__init(self) end, __base = _base_0, __name = "RootBlock", @@ -647,7 +647,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 4ad3b66c..5db3b5d6 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -612,7 +612,25 @@ Statement = Transformer { class_lookup = build["if"] { cond: { "exp", {"ref", "val"}, "==", "nil" } then: { - parent_cls_name\index"name" + build.assign_one LocalName"parent", build.chain { + base: "rawget" + { + "call", { + {"ref", "cls"} + {"string", '"', "__parent"} + } + } + } + + build.if { + cond: LocalName "parent" + then: { + build.chain { + base: LocalName "parent" + {"index", "name"} + } + } + } } } insert class_lookup, {"else", {"val"}} @@ -660,14 +678,21 @@ Statement = Transformer { @put_name name if name @set "super", (block, chain) -> + relative_parent = { + "chain", + "self" + {"dot", "__class"} + {"dot", "__parent"} + } + if chain chain_tail = { unpack chain, 3 } - new_chain = {"chain", parent_cls_name} - head = chain_tail[1] if head == nil - return parent_cls_name + return relative_parent + + new_chain = relative_parent switch head[1] -- calling super, inject calling name and self into chain @@ -702,7 +727,7 @@ Statement = Transformer { new_chain else - parent_cls_name + relative_parent {"declare_glob", "*"} diff --git a/spec/class_spec.moon b/spec/class_spec.moon index c46b4415..7b1dbd6e 100644 --- a/spec/class_spec.moon +++ b/spec/class_spec.moon @@ -19,8 +19,7 @@ describe "class", -> instance = Thing! assert.same instance\get_color!, "blue" - - it "should have class property", -> + it "should have base properies from class", -> class Thing color: "blue" get_color: => @color @@ -54,7 +53,6 @@ describe "class", -> assert.same instance.property, "name" assert.same instance.name, "the_thing" - it "should call super method", -> class Base _count: 111 @@ -66,6 +64,18 @@ describe "class", -> instance = Thing! assert.same instance\counter!, "00000111" + it "should call other method from super", -> + class Base + _count: 111 + counter: => + @_count + + class Thing extends Base + other_method: => super\counter! + + instance = Thing! + assert.same instance\other_method!, 111 + it "should get super class", -> class Base class Thing extends Base @@ -102,7 +112,6 @@ describe "class", -> Thing = class assert.same Thing.__name, "Thing" - it "should not expose class properties on instance", -> class Thing @height: 10 @@ -135,3 +144,57 @@ describe "class", -> instance = Thing! instance\go! + it "should have class properies take precedence over base properties", -> + class Thing + @prop: "hello" + prop: "world" + + assert.same "hello", Thing.prop + + it "class properties take precedence in super class over base", -> + class Thing + @prop: "hello" + prop: "world" + + class OtherThing extends Thing + + assert.same "hello", OtherThing.prop + + it "gets value from base in super class", -> + class Thing + prop: "world" + + class OtherThing extends Thing + assert.same "world", OtherThing.prop + + it "should let parent be replaced on class", -> + class A + @prop: "yeah" + cool: => 1234 + plain: => "a" + + class B + @prop: "okay" + cool: => 9999 + plain: => "b" + + class Thing extends A + cool: => + super! + 1 + + get_super: => + super + + instance = Thing! + + assert.same "a", instance\plain! + assert.same 1235, instance\cool! + assert A == instance\get_super!, "expected super to be B" + + Thing.__parent = B + setmetatable Thing.__base, B.__base + + assert.same "b", instance\plain! + assert.same 10000, instance\cool! + assert B == instance\get_super!, "expected super to be B" + From 6aea71f16c772844cc1c92d431c6e4a1bc19b80e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Nov 2015 18:15:05 -0800 Subject: [PATCH 148/344] rebuild --- moonscript/cmd/lint.lua | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 52dba4e0..3a109a3d 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -120,11 +120,11 @@ do end, render = function(self, ...) self:lint_check_unused() - return _parent_0.render(self, ...) + return self.__class.__parent.render(self, ...) end, block = function(self, ...) do - local _with_0 = _parent_0.block(self, ...) + local _with_0 = self.__class.__parent.block(self, ...) _with_0.block = self.block _with_0.render = self.render _with_0.get_root_block = self.get_root_block @@ -143,7 +143,7 @@ do if whitelist_globals == nil then whitelist_globals = default_whitelist end - _parent_0.__init(self, ...) + self.__class.__parent.__init(self, ...) self.get_root_block = function() return self end @@ -206,7 +206,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end From b4c802fadfe438e1b86d6893ae3c8f141bc6154b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Nov 2015 18:15:51 -0800 Subject: [PATCH 149/344] rebuild tests for new compiled syntax --- spec/outputs/class.lua | 71 +++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 25 deletions(-) diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index b16ba179..b9e6c41c 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -70,7 +70,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end @@ -118,14 +121,14 @@ do local _parent_0 = Hi local _base_0 = { cool = function(self) - return _parent_0.cool(self, 120302) + return self.__class.__parent.cool(self, 120302) end } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) local _class_0 = setmetatable({ __init = function(self) - return _parent_0.__init(self, "man") + return self.__class.__parent.__init(self, "man") end, __base = _base_0, __name = "Simple", @@ -134,7 +137,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end @@ -180,16 +186,16 @@ do local _parent_0 = Okay local _base_0 = { something = function(self) - _parent_0.something(self, 1, 2, 3, 4) - _parent_0.something(another_self, 1, 2, 3, 4) - return assert(_parent_0 == Okay) + self.__class.__parent.something(self, 1, 2, 3, 4) + self.__class.__parent.something(another_self, 1, 2, 3, 4) + return assert(self.__class.__parent == Okay) end } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) local _class_0 = setmetatable({ __init = function(self, ...) - return _parent_0.__init(self, ...) + return self.__class.__parent.__init(self, ...) end, __base = _base_0, __name = "Biggie", @@ -198,7 +204,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end @@ -219,7 +228,7 @@ local Yeah do local _base_0 = { okay = function(self) - return _parent_0.something(self, 1, 2, 3, 4) + return self.__class.__parent.something(self, 1, 2, 3, 4) end } _base_0.__index = _base_0 @@ -266,7 +275,7 @@ do local _base_0 = { val = 2323, something = function(self) - local _base_1 = _parent_0 + local _base_1 = self.__class.__parent local _fn_0 = _base_1.something return function(...) return _fn_0(self, ...) @@ -277,7 +286,7 @@ do setmetatable(_base_0, _parent_0.__base) local _class_0 = setmetatable({ __init = function(self, ...) - return _parent_0.__init(self, ...) + return self.__class.__parent.__init(self, ...) end, __base = _base_0, __name = "Hello", @@ -286,7 +295,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end @@ -313,14 +325,14 @@ local CoolSuper do local _base_0 = { hi = function(self) - _parent_0.hi(self, 1, 2, 3, 4)(1, 2, 3, 4) - _parent_0.something(1, 2, 3, 4) - local _ = _parent_0.something(1, 2, 3, 4).world - _parent_0.yeah(self, "world").okay(hi, hi, hi) + self.__class.__parent.hi(self, 1, 2, 3, 4)(1, 2, 3, 4) + self.__class.__parent.something(1, 2, 3, 4) + local _ = self.__class.__parent.something(1, 2, 3, 4).world + self.__class.__parent.yeah(self, "world").okay(hi, hi, hi) _ = something.super - _ = _parent_0.super.super.super + _ = self.__class.__parent.super.super.super do - local _base_1 = _parent_0 + local _base_1 = self.__class.__parent local _fn_0 = _base_1.hello _ = function(...) return _fn_0(self, ...) @@ -492,7 +504,7 @@ do setmetatable(_base_0, _parent_0.__base) local _class_0 = setmetatable({ __init = function(self, ...) - return _parent_0.__init(self, ...) + return self.__class.__parent.__init(self, ...) end, __base = _base_0, __name = "Something", @@ -501,7 +513,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end @@ -570,7 +585,7 @@ do setmetatable(_base_0, _parent_0.__base) local _class_0 = setmetatable({ __init = function(self, ...) - return _parent_0.__init(self, ...) + return self.__class.__parent.__init(self, ...) end, __base = _base_0, __name = "Something", @@ -579,7 +594,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end @@ -605,7 +623,7 @@ do setmetatable(_base_0, _parent_0.__base) local _class_0 = setmetatable({ __init = function(self, ...) - return _parent_0.__init(self, ...) + return self.__class.__parent.__init(self, ...) end, __base = _base_0, __name = "d", @@ -614,7 +632,10 @@ do __index = function(cls, name) local val = rawget(_base_0, name) if val == nil then - return _parent_0[name] + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end else return val end From 40b7ed61350d9e28518ec5dccce337b5bd673447 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Nov 2015 22:01:48 -0800 Subject: [PATCH 150/344] specs for repeated supers --- spec/class_spec.moon | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/spec/class_spec.moon b/spec/class_spec.moon index 7b1dbd6e..d21e8bff 100644 --- a/spec/class_spec.moon +++ b/spec/class_spec.moon @@ -198,3 +198,39 @@ describe "class", -> assert.same 10000, instance\cool! assert B == instance\get_super!, "expected super to be B" + it "should resolve many levels of super", -> + class One + a: => + 1 + + class Two extends One + a: => + super! + 2 + + class Three extends Two + a: => + super! + 3 + + i = Three! + + assert.same 6, i\a! + + + it "should resolve many levels of super with a gap", -> + class One + a: => + 1 + + class Two extends One + + class Three extends Two + a: => + super! + 3 + + class Four extends Three + a: => + super! + 4 + + i = Four! + + assert.same 8, i\a! From 66d80ed217eca5e18f78cc064fdb3da3a23ceb3a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Nov 2015 22:02:19 -0800 Subject: [PATCH 151/344] use cls_name for super access, rebuild --- moonscript/cmd/coverage.lua | 3 ++- moonscript/cmd/lint.lua | 9 +++++---- moonscript/compile.lua | 17 +++++++++++------ moonscript/data.lua | 3 ++- moonscript/transform.moon | 4 ++-- moonscript/transform/names.lua | 6 ++++-- moonscript/transform/statements.lua | 3 ++- 7 files changed, 28 insertions(+), 17 deletions(-) diff --git a/moonscript/cmd/coverage.lua b/moonscript/cmd/coverage.lua index 8c585e83..4fd36806 100644 --- a/moonscript/cmd/coverage.lua +++ b/moonscript/cmd/coverage.lua @@ -58,6 +58,7 @@ format_file = function(fname, positions) end local CodeCoverage do + local _class_0 local _base_0 = { reset = function(self) self.line_counts = create_counter() @@ -120,7 +121,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self) return self:reset() end, diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 3a109a3d..50509e57 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -52,6 +52,7 @@ local default_whitelist = Set({ }) local LinterBlock do + local _class_0 local _parent_0 = Block local _base_0 = { lint_mark_used = function(self, name) @@ -120,11 +121,11 @@ do end, render = function(self, ...) self:lint_check_unused() - return self.__class.__parent.render(self, ...) + return _class_0.__parent.render(self, ...) end, block = function(self, ...) do - local _with_0 = self.__class.__parent.block(self, ...) + local _with_0 = _class_0.__parent.block(self, ...) _with_0.block = self.block _with_0.render = self.render _with_0.get_root_block = self.get_root_block @@ -138,12 +139,12 @@ do } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, whitelist_globals, ...) if whitelist_globals == nil then whitelist_globals = default_whitelist end - self.__class.__parent.__init(self, ...) + _class_0.__parent.__init(self, ...) self.get_root_block = function() return self end diff --git a/moonscript/compile.lua b/moonscript/compile.lua index dad4b79a..0715b862 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -26,6 +26,7 @@ local mtype = util.moon.type local indent_char = " " local Line, DelayedLine, Lines, Block, RootBlock do + local _class_0 local _base_0 = { mark_pos = function(self, pos, line) if line == nil then @@ -127,7 +128,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self) self.posmap = { } end, @@ -145,6 +146,7 @@ do Lines = _class_0 end do + local _class_0 local _base_0 = { pos = nil, append_list = function(self, items, delim) @@ -208,7 +210,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Line" @@ -224,6 +226,7 @@ do Line = _class_0 end do + local _class_0 local _base_0 = { prepare = function() end, render = function(self) @@ -232,7 +235,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, fn) self.prepare = fn end, @@ -250,6 +253,7 @@ do DelayedLine = _class_0 end do + local _class_0 local _base_0 = { header = "do", footer = "end", @@ -573,7 +577,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, parent, header, footer) self.parent, self.header, self.footer = parent, header, footer self._lines = Lines() @@ -613,6 +617,7 @@ do Block = _class_0 end do + local _class_0 local _parent_0 = Block local _base_0 = { __tostring = function(self) @@ -634,11 +639,11 @@ do } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, options) self.options = options self.root = self - return self.__class.__parent.__init(self) + return _class_0.__parent.__init(self) end, __base = _base_0, __name = "RootBlock", diff --git a/moonscript/data.lua b/moonscript/data.lua index 2e159c83..f33f708d 100644 --- a/moonscript/data.lua +++ b/moonscript/data.lua @@ -14,6 +14,7 @@ Set = function(items) end local Stack do + local _class_0 local _base_0 = { __tostring = function(self) return "" @@ -34,7 +35,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, ...) self:push(...) return nil diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 5db3b5d6..5cd9ec1e 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -680,8 +680,7 @@ Statement = Transformer { @set "super", (block, chain) -> relative_parent = { "chain", - "self" - {"dot", "__class"} + cls_name {"dot", "__parent"} } @@ -729,6 +728,7 @@ Statement = Transformer { else relative_parent + {"declare", { cls_name }} {"declare_glob", "*"} parent_val and .assign_one(parent_cls_name, parent_val) or NOOP diff --git a/moonscript/transform/names.lua b/moonscript/transform/names.lua index c74a3773..b498036e 100644 --- a/moonscript/transform/names.lua +++ b/moonscript/transform/names.lua @@ -4,13 +4,14 @@ local unpack unpack = require("moonscript.util").unpack local LocalName do + local _class_0 local _base_0 = { get_name = function(self) return self.name end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, name) self.name = name self[1] = "temp_name" @@ -30,6 +31,7 @@ do end local NameProxy do + local _class_0 local _base_0 = { get_name = function(self, scope, dont_put) if dont_put == nil then @@ -81,7 +83,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, prefix) self.prefix = prefix self[1] = "temp_name" diff --git a/moonscript/transform/statements.lua b/moonscript/transform/statements.lua index 169590ea..994df895 100644 --- a/moonscript/transform/statements.lua +++ b/moonscript/transform/statements.lua @@ -5,13 +5,14 @@ do end local Run do + local _class_0 local _base_0 = { call = function(self, state) return self.fn(state) end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, fn) self.fn = fn self[1] = "run" From ae2558ab67d8227b870cee1597a608c98727a924 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 19 Nov 2015 22:47:06 -0800 Subject: [PATCH 152/344] rebuild specs and code --- moonscript/transform.lua | 59 +++++++++++++++++---- spec/outputs/class.lua | 111 ++++++++++++++++++++++++--------------- spec/outputs/export.lua | 3 +- 3 files changed, 120 insertions(+), 53 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 7a2d18d4..a88b1d2b 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -171,6 +171,7 @@ with_continue_listener = function(body) } end do + local _class_0 local _base_0 = { transform_once = function(self, scope, node, ...) if self.seen_nodes[node] then @@ -217,7 +218,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, transformers) self.transformers = transformers self.seen_nodes = setmetatable({ }, { @@ -1000,7 +1001,35 @@ Statement = Transformer({ "nil" }, ["then"] = { - parent_cls_name:index("name") + build.assign_one(LocalName("parent"), build.chain({ + base = "rawget", + { + "call", + { + { + "ref", + "cls" + }, + { + "string", + '"', + "__parent" + } + } + } + })), + build["if"]({ + cond = LocalName("parent"), + ["then"] = { + build.chain({ + base = LocalName("parent"), + { + "index", + "name" + } + }) + } + }) } }) insert(class_lookup, { @@ -1098,18 +1127,23 @@ Statement = Transformer({ self:put_name(name) end return self:set("super", function(block, chain) + local relative_parent = { + "chain", + cls_name, + { + "dot", + "__parent" + } + } if chain then local chain_tail = { unpack(chain, 3) } - local new_chain = { - "chain", - parent_cls_name - } local head = chain_tail[1] if head == nil then - return parent_cls_name + return relative_parent end + local new_chain = relative_parent local _exp_1 = head[1] if "call" == _exp_1 then local calling_name = block:get("current_block") @@ -1153,10 +1187,16 @@ Statement = Transformer({ end return new_chain else - return parent_cls_name + return relative_parent end end) end), + { + "declare", + { + cls_name + } + }, { "declare_glob", "*" @@ -1240,6 +1280,7 @@ Statement = Transformer({ end }) do + local _class_0 local _base_0 = { body_idx = { ["for"] = 4, @@ -1300,7 +1341,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, accum_name) self.accum_name = NameProxy("accum") self.value_name = NameProxy("value") diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index b9e6c41c..30180620 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -1,5 +1,6 @@ local Hello do + local _class_0 local _base_0 = { hello = function(self) return print(self.test, self.world) @@ -9,7 +10,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, test, world) self.test, self.world = test, world return print("creating object..") @@ -32,13 +33,14 @@ x:hello() print(x) local Simple do + local _class_0 local _base_0 = { cool = function(self) return print("cool") end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Simple" @@ -55,11 +57,12 @@ do end local Yikes do + local _class_0 local _parent_0 = Simple local _base_0 = { } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self) return print("created hello") end, @@ -94,13 +97,14 @@ x = Yikes() x:cool() local Hi do + local _class_0 local _base_0 = { cool = function(self, num) return print("num", num) end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, arg) return print("init arg", arg) end, @@ -118,17 +122,18 @@ do Hi = _class_0 end do + local _class_0 local _parent_0 = Hi local _base_0 = { cool = function(self) - return self.__class.__parent.cool(self, 120302) + return _class_0.__parent.cool(self, 120302) end } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self) - return self.__class.__parent.__init(self, "man") + return _class_0.__parent.__init(self, "man") end, __base = _base_0, __name = "Simple", @@ -162,11 +167,12 @@ x:cool() print(x.__class == Simple) local Okay do + local _class_0 local _base_0 = { something = 20323 } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Okay" @@ -183,19 +189,20 @@ do end local Biggie do + local _class_0 local _parent_0 = Okay local _base_0 = { something = function(self) - self.__class.__parent.something(self, 1, 2, 3, 4) - self.__class.__parent.something(another_self, 1, 2, 3, 4) - return assert(self.__class.__parent == Okay) + _class_0.__parent.something(self, 1, 2, 3, 4) + _class_0.__parent.something(another_self, 1, 2, 3, 4) + return assert(_class_0.__parent == Okay) end } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, ...) - return self.__class.__parent.__init(self, ...) + return _class_0.__parent.__init(self, ...) end, __base = _base_0, __name = "Biggie", @@ -226,13 +233,14 @@ do end local Yeah do + local _class_0 local _base_0 = { okay = function(self) - return self.__class.__parent.something(self, 1, 2, 3, 4) + return _class_0.__parent.something(self, 1, 2, 3, 4) end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Yeah" @@ -249,13 +257,14 @@ do end local What do + local _class_0 local _base_0 = { something = function(self) return print("val:", self.val) end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "What" @@ -271,11 +280,12 @@ do What = _class_0 end do + local _class_0 local _parent_0 = What local _base_0 = { val = 2323, something = function(self) - local _base_1 = self.__class.__parent + local _base_1 = _class_0.__parent local _fn_0 = _base_1.something return function(...) return _fn_0(self, ...) @@ -284,9 +294,9 @@ do } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, ...) - return self.__class.__parent.__init(self, ...) + return _class_0.__parent.__init(self, ...) end, __base = _base_0, __name = "Hello", @@ -323,16 +333,17 @@ do end local CoolSuper do + local _class_0 local _base_0 = { hi = function(self) - self.__class.__parent.hi(self, 1, 2, 3, 4)(1, 2, 3, 4) - self.__class.__parent.something(1, 2, 3, 4) - local _ = self.__class.__parent.something(1, 2, 3, 4).world - self.__class.__parent.yeah(self, "world").okay(hi, hi, hi) + _class_0.__parent.hi(self, 1, 2, 3, 4)(1, 2, 3, 4) + _class_0.__parent.something(1, 2, 3, 4) + local _ = _class_0.__parent.something(1, 2, 3, 4).world + _class_0.__parent.yeah(self, "world").okay(hi, hi, hi) _ = something.super - _ = self.__class.__parent.super.super.super + _ = _class_0.__parent.super.super.super do - local _base_1 = self.__class.__parent + local _base_1 = _class_0.__parent local _fn_0 = _base_1.hello _ = function(...) return _fn_0(self, ...) @@ -342,7 +353,7 @@ do end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "CoolSuper" @@ -368,12 +379,13 @@ xx = function(hello, world, cool) end local ClassMan do + local _class_0 local _base_0 = { blue = function(self) end, green = function(self) end } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "ClassMan" @@ -403,10 +415,11 @@ self.hello(2, 3, 4) local _ = hello[self].world local Whacko do + local _class_0 local hello local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Whacko" @@ -436,9 +449,10 @@ local yyy yyy = function() local Cool do + local _class_0 local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Cool" @@ -458,9 +472,10 @@ yyy = function() end end do + local _class_0 local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "D" @@ -478,9 +493,10 @@ do a.b.c.D = _class_0 end do + local _class_0 local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "hello" @@ -498,13 +514,14 @@ do a.b["hello"] = _class_0 end do + local _class_0 local _parent_0 = Hello.World local _base_0 = { } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, ...) - return self.__class.__parent.__init(self, ...) + return _class_0.__parent.__init(self, ...) end, __base = _base_0, __name = "Something", @@ -539,9 +556,10 @@ do end local a do + local _class_0 local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "a" @@ -559,9 +577,10 @@ end local b local Something do + local _class_0 local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Something" @@ -579,13 +598,14 @@ do end local c do + local _class_0 local _parent_0 = Hello local _base_0 = { } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, ...) - return self.__class.__parent.__init(self, ...) + return _class_0.__parent.__init(self, ...) end, __base = _base_0, __name = "Something", @@ -617,13 +637,14 @@ do end local d do + local _class_0 local _parent_0 = World local _base_0 = { } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self, ...) - return self.__class.__parent.__init(self, ...) + return _class_0.__parent.__init(self, ...) end, __base = _base_0, __name = "d", @@ -655,9 +676,10 @@ end print(((function() local WhatsUp do + local _class_0 local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "WhatsUp" @@ -675,9 +697,10 @@ print(((function() end end)()).__name) do + local _class_0 local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Something" @@ -695,10 +718,11 @@ do Something = _class_0 end do + local _class_0 local val, insert local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function(self) return print(insert, val) end, @@ -719,9 +743,10 @@ do Something = _class_0 end do + local _class_0 local _base_0 = { } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = hi, __base = _base_0, __name = "X" diff --git a/spec/outputs/export.lua b/spec/outputs/export.lua index 72ebadea..08fb4ce8 100644 --- a/spec/outputs/export.lua +++ b/spec/outputs/export.lua @@ -4,11 +4,12 @@ do end do do + local _class_0 local _base_0 = { umm = "cool" } _base_0.__index = _base_0 - local _class_0 = setmetatable({ + _class_0 = setmetatable({ __init = function() end, __base = _base_0, __name = "Something" From d91fbbe4962f9a9e70457804665f931d98502c92 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 5 Dec 2015 21:34:32 -0800 Subject: [PATCH 153/344] count --- Makefile | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index c4d3c8d0..8ddcd12c 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -.PHONY: test local compile compile_system watch lint +.PHONY: test local compile compile_system watch lint count test: busted @@ -23,3 +23,6 @@ watch: lint: moonc -l moonscript moon bin + +count: + wc -l $$(git ls-files | grep 'moon$$') | sort -n | tail From d73c0a1d3fecef6cab8045c54a9a3d22a302bc35 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 5 Dec 2015 21:38:08 -0800 Subject: [PATCH 154/344] unindent super code --- moonscript/transform.moon | 93 +++++++++++++++++++++------------------ 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 5cd9ec1e..d04a236f 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -684,49 +684,48 @@ Statement = Transformer { {"dot", "__parent"} } - if chain - chain_tail = { unpack chain, 3 } - head = chain_tail[1] - - if head == nil - return relative_parent - - new_chain = relative_parent - - switch head[1] - -- calling super, inject calling name and self into chain - when "call" - calling_name = block\get"current_block" - chain_tail[1] = {"call", {"self", unpack head[2]}} - - if ntype(calling_name) == "key_literal" - insert new_chain, {"dot", calling_name[2]} - else - insert new_chain, {"index", calling_name} - - -- colon call on super, replace class with self as first arg - when "colon" - call = chain_tail[2] - -- calling chain tail - if call and call[1] == "call" - chain_tail[1] = { - "dot" - head[2] + return relative_parent unless chain + + chain_tail = { unpack chain, 3 } + head = chain_tail[1] + + if head == nil + return relative_parent + + new_chain = relative_parent + + switch head[1] + -- calling super, inject calling name and self into chain + when "call" + calling_name = block\get"current_block" + assert calling_name, "missing calling name" + chain_tail[1] = {"call", {"self", unpack head[2]}} + + if ntype(calling_name) == "key_literal" + insert new_chain, {"dot", calling_name[2]} + else + insert new_chain, {"index", calling_name} + + -- colon call on super, replace class with self as first arg + when "colon" + call = chain_tail[2] + -- calling chain tail + if call and call[1] == "call" + chain_tail[1] = { + "dot" + head[2] + } + + chain_tail[2] = { + "call" + { + "self" + unpack call[2] } + } - chain_tail[2] = { - "call" - { - "self" - unpack call[2] - } - } - - insert new_chain, item for item in *chain_tail - - new_chain - else - relative_parent + insert new_chain, item for item in *chain_tail + new_chain {"declare", { cls_name }} {"declare_glob", "*"} @@ -939,9 +938,15 @@ Value = Transformer { node - if: (node) => build.block_exp { node } - unless: (node) =>build.block_exp { node } - with: (node) => build.block_exp { node } + if: (node) => + build.block_exp { node } + + unless: (node) => + build.block_exp { node } + + with: (node) => + build.block_exp { node } + switch: (node) => build.block_exp { node } From 53ef9fe5045e3e5996224ac28df684e65a679ced Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 5 Dec 2015 21:38:29 -0800 Subject: [PATCH 155/344] move transformer to separate class --- moonscript/transform.lua | 160 +++++++++++--------------------------- moonscript/transform.moon | 40 +--------- 2 files changed, 49 insertions(+), 151 deletions(-) diff --git a/moonscript/transform.lua b/moonscript/transform.lua index a88b1d2b..922bef9a 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -17,11 +17,13 @@ do local _obj_0 = require("moonscript.transform.statements") Run, transform_last_stm, last_stm, chain_is_stub = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.last_stm, _obj_0.chain_is_stub end +local Transformer +Transformer = require("moonscript.transform.transformer").Transformer local destructure = require("moonscript.transform.destructure") local NOOP = { "noop" } -local is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value +local is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value is_singular = function(body) if #body ~= 1 then return false @@ -170,74 +172,6 @@ with_continue_listener = function(body) end) } end -do - local _class_0 - local _base_0 = { - transform_once = function(self, scope, node, ...) - if self.seen_nodes[node] then - return node - end - self.seen_nodes[node] = true - local transformer = self.transformers[ntype(node)] - if transformer then - return transformer(scope, node, ...) or node - else - return node - end - end, - transform = function(self, scope, node, ...) - if self.seen_nodes[node] then - return node - end - self.seen_nodes[node] = true - while true do - local transformer = self.transformers[ntype(node)] - local res - if transformer then - res = transformer(scope, node, ...) or node - else - res = node - end - if res == node then - return node - end - node = res - end - return node - end, - bind = function(self, scope) - return function(...) - return self:transform(scope, ...) - end - end, - __call = function(self, ...) - return self:transform(...) - end, - can_transform = function(self, node) - return self.transformers[ntype(node)] ~= nil - end - } - _base_0.__index = _base_0 - _class_0 = setmetatable({ - __init = function(self, transformers) - self.transformers = transformers - self.seen_nodes = setmetatable({ }, { - __mode = "k" - }) - end, - __base = _base_0, - __name = "Transformer" - }, { - __index = _base_0, - __call = function(cls, ...) - local _self_0 = setmetatable({}, _base_0) - cls.__init(_self_0, ...) - return _self_0 - end - }) - _base_0.__class = _class_0 - Transformer = _class_0 -end construct_comprehension = function(inner, clauses) local current_stms = inner for _, clause in reversed(clauses) do @@ -1135,60 +1069,60 @@ Statement = Transformer({ "__parent" } } - if chain then - local chain_tail = { - unpack(chain, 3) + if not (chain) then + return relative_parent + end + local chain_tail = { + unpack(chain, 3) + } + local head = chain_tail[1] + if head == nil then + return relative_parent + end + local new_chain = relative_parent + local _exp_1 = head[1] + if "call" == _exp_1 then + local calling_name = block:get("current_block") + assert(calling_name, "missing calling name") + chain_tail[1] = { + "call", + { + "self", + unpack(head[2]) + } } - local head = chain_tail[1] - if head == nil then - return relative_parent + if ntype(calling_name) == "key_literal" then + insert(new_chain, { + "dot", + calling_name[2] + }) + else + insert(new_chain, { + "index", + calling_name + }) end - local new_chain = relative_parent - local _exp_1 = head[1] - if "call" == _exp_1 then - local calling_name = block:get("current_block") + elseif "colon" == _exp_1 then + local call = chain_tail[2] + if call and call[1] == "call" then chain_tail[1] = { + "dot", + head[2] + } + chain_tail[2] = { "call", { "self", - unpack(head[2]) + unpack(call[2]) } } - if ntype(calling_name) == "key_literal" then - insert(new_chain, { - "dot", - calling_name[2] - }) - else - insert(new_chain, { - "index", - calling_name - }) - end - elseif "colon" == _exp_1 then - local call = chain_tail[2] - if call and call[1] == "call" then - chain_tail[1] = { - "dot", - head[2] - } - chain_tail[2] = { - "call", - { - "self", - unpack(call[2]) - } - } - end end - for _index_0 = 1, #chain_tail do - local item = chain_tail[_index_0] - insert(new_chain, item) - end - return new_chain - else - return relative_parent end + for _index_0 = 1, #chain_tail do + local item = chain_tail[_index_0] + insert(new_chain, item) + end + return new_chain end) end), { diff --git a/moonscript/transform.moon b/moonscript/transform.moon index d04a236f..27127d2d 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -10,6 +10,8 @@ import insert from table import NameProxy, LocalName from require "moonscript.transform.names" import Run, transform_last_stm, last_stm, chain_is_stub from require "moonscript.transform.statements" +import Transformer from require "moonscript.transform.transformer" + destructure = require "moonscript.transform.destructure" NOOP = {"noop"} @@ -93,44 +95,6 @@ with_continue_listener = (body) -> } -class Transformer - new: (@transformers) => - @seen_nodes = setmetatable {}, __mode: "k" - - transform_once: (scope, node, ...) => - return node if @seen_nodes[node] - @seen_nodes[node] = true - - transformer = @transformers[ntype node] - if transformer - transformer(scope, node, ...) or node - else - node - - transform: (scope, node, ...) => - return node if @seen_nodes[node] - - @seen_nodes[node] = true - while true - transformer = @transformers[ntype node] - res = if transformer - transformer(scope, node, ...) or node - else - node - - return node if res == node - node = res - - node - - bind: (scope) => - (...) -> @transform scope, ... - - __call: (...) => @transform ... - - can_transform: (node) => - @transformers[ntype node] != nil - construct_comprehension = (inner, clauses) -> current_stms = inner for _, clause in reversed clauses From 76af9964fe7e1186e3080d7a18f4db65b529b115 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 5 Dec 2015 23:07:42 -0800 Subject: [PATCH 156/344] refactor apart transfromers --- moonscript/cmd/lint.lua | 3 + moonscript/cmd/lint.moon | 1 + moonscript/parse.lua | 4 +- moonscript/parse.moon | 2 +- moonscript/parse/util.lua | 12 - moonscript/parse/util.moon | 8 +- moonscript/transform.lua | 1573 +------------------------ moonscript/transform.moon | 978 +-------------- moonscript/transform/accumulator.lua | 110 ++ moonscript/transform/accumulator.moon | 71 ++ moonscript/transform/statement.lua | 1161 ++++++++++++++++++ moonscript/transform/statement.moon | 720 +++++++++++ moonscript/transform/statements.lua | 89 +- moonscript/transform/statements.moon | 56 +- moonscript/transform/transformer.lua | 74 ++ moonscript/transform/transformer.moon | 42 + moonscript/transform/value.lua | 261 ++++ moonscript/transform/value.moon | 162 +++ moonscript/types.lua | 6 +- moonscript/types.moon | 3 + 20 files changed, 2760 insertions(+), 2576 deletions(-) create mode 100644 moonscript/transform/accumulator.lua create mode 100644 moonscript/transform/accumulator.moon create mode 100644 moonscript/transform/statement.lua create mode 100644 moonscript/transform/statement.moon create mode 100644 moonscript/transform/transformer.lua create mode 100644 moonscript/transform/transformer.moon create mode 100644 moonscript/transform/value.lua create mode 100644 moonscript/transform/value.moon diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 50509e57..55f018b5 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -65,6 +65,9 @@ do end end, lint_check_unused = function(self) + do + return + end if not (self.lint_unused_names and next(self.lint_unused_names)) then return end diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 103c25ed..853bab59 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -105,6 +105,7 @@ class LinterBlock extends Block @parent\lint_mark_used name lint_check_unused: => + do return return unless @lint_unused_names and next @lint_unused_names names_by_position = {} diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 53c12f05..49b673dd 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -27,10 +27,10 @@ Num = Space * (Num / function(v) v } end) -local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, flatten_string_chain, wrap_decorator, check_lua_string, self_assign +local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign do local _obj_0 = require("moonscript.parse.util") - Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, flatten_string_chain, wrap_decorator, check_lua_string, self_assign = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.join_chain, _obj_0.flatten_string_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign + Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.join_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign end local build_grammar = wrap_env(debug_grammar, function(root) local _indent = Stack(0) diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 96c26e5f..0c6bc823 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -29,7 +29,7 @@ Num = Space * (Num / (v) -> {"number", v}) :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, :is_assignable, :check_assignable, :format_assign, :format_single_assign, :sym, :symx, :simple_string, :wrap_func_arg, :join_chain, - :flatten_string_chain, :wrap_decorator, :check_lua_string, :self_assign + :wrap_decorator, :check_lua_string, :self_assign } = require "moonscript.parse.util" diff --git a/moonscript/parse/util.lua b/moonscript/parse/util.lua index a93665dc..e9d4554b 100644 --- a/moonscript/parse/util.lua +++ b/moonscript/parse/util.lua @@ -197,17 +197,6 @@ join_chain = function(callee, args) args } end -local flatten_string_chain -flatten_string_chain = function(str, chain, args) - if not (chain) then - return str - end - return flatten_chain({ - "chain", - str, - unpack(chain) - }, args) -end local wrap_decorator wrap_decorator = function(stm, dec) if not (dec) then @@ -254,7 +243,6 @@ return { simple_string = simple_string, wrap_func_arg = wrap_func_arg, join_chain = join_chain, - flatten_string_chain = flatten_string_chain, wrap_decorator = wrap_decorator, check_lua_string = check_lua_string, self_assign = self_assign diff --git a/moonscript/parse/util.moon b/moonscript/parse/util.moon index 2039e3bd..af33279e 100644 --- a/moonscript/parse/util.moon +++ b/moonscript/parse/util.moon @@ -139,10 +139,6 @@ join_chain = (callee, args) -> {"chain", callee, args} -flatten_string_chain = (str, chain, args) -> - return str unless chain - flatten_chain {"chain", str, unpack chain}, args - -- constructor for decorator node wrap_decorator = (stm, dec) -> return stm unless dec @@ -157,5 +153,5 @@ self_assign = (name, pos) -> { :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, :is_assignable, :check_assignable, :format_assign, :format_single_assign, - :sym, :symx, :simple_string, :wrap_func_arg, :join_chain, - :flatten_string_chain, :wrap_decorator, :check_lua_string, :self_assign } + :sym, :symx, :simple_string, :wrap_func_arg, :join_chain, :wrap_decorator, + :check_lua_string, :self_assign } diff --git a/moonscript/transform.lua b/moonscript/transform.lua index 922bef9a..c95f14d3 100644 --- a/moonscript/transform.lua +++ b/moonscript/transform.lua @@ -1,1573 +1,4 @@ -local types = require("moonscript.types") -local util = require("moonscript.util") -local data = require("moonscript.data") -local reversed, unpack -reversed, unpack = util.reversed, util.unpack -local ntype, mtype, build, smart_node, is_slice, value_is_singular -ntype, mtype, build, smart_node, is_slice, value_is_singular = types.ntype, types.mtype, types.build, types.smart_node, types.is_slice, types.value_is_singular -local insert -insert = table.insert -local NameProxy, LocalName -do - local _obj_0 = require("moonscript.transform.names") - NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName -end -local Run, transform_last_stm, last_stm, chain_is_stub -do - local _obj_0 = require("moonscript.transform.statements") - Run, transform_last_stm, last_stm, chain_is_stub = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.last_stm, _obj_0.chain_is_stub -end -local Transformer -Transformer = require("moonscript.transform.transformer").Transformer -local destructure = require("moonscript.transform.destructure") -local NOOP = { - "noop" -} -local is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value -is_singular = function(body) - if #body ~= 1 then - return false - end - if "group" == ntype(body) then - return is_singular(body[2]) - else - return body[1] - end -end -extract_declarations = function(self, body, start, out) - if body == nil then - body = self.current_stms - end - if start == nil then - start = self.current_stm_i + 1 - end - if out == nil then - out = { } - end - for i = start, #body do - local _continue_0 = false - repeat - local stm = body[i] - if stm == nil then - _continue_0 = true - break - end - stm = self.transform.statement(stm) - body[i] = stm - local _exp_0 = stm[1] - if "assign" == _exp_0 or "declare" == _exp_0 then - local _list_0 = stm[2] - for _index_0 = 1, #_list_0 do - local name = _list_0[_index_0] - if ntype(name) == "ref" then - insert(out, name) - elseif type(name) == "string" then - insert(out, name) - end - end - elseif "group" == _exp_0 then - extract_declarations(self, stm[2], 1, out) - end - _continue_0 = true - until true - if not _continue_0 then - break - end - end - return out -end -expand_elseif_assign = function(ifstm) - for i = 4, #ifstm do - local case = ifstm[i] - if ntype(case) == "elseif" and ntype(case[2]) == "assign" then - local split = { - unpack(ifstm, 1, i - 1) - } - insert(split, { - "else", - { - { - "if", - case[2], - case[3], - unpack(ifstm, i + 1) - } - } - }) - return split - end - end - return ifstm -end -constructor_name = "new" -with_continue_listener = function(body) - local continue_name = nil - return { - Run(function(self) - return self:listen("continue", function() - if not (continue_name) then - continue_name = NameProxy("continue") - self:put_name(continue_name) - end - return continue_name - end) - end), - build.group(body), - Run(function(self) - if not (continue_name) then - return - end - local last = last_stm(body) - local enclose_lines = types.terminating[last and ntype(last)] - self:put_name(continue_name, nil) - return self:splice(function(lines) - if enclose_lines then - lines = { - "do", - { - lines - } - } - end - return { - { - "assign", - { - continue_name - }, - { - "false" - } - }, - { - "repeat", - "true", - { - lines, - { - "assign", - { - continue_name - }, - { - "true" - } - } - } - }, - { - "if", - { - "not", - continue_name - }, - { - { - "break" - } - } - } - } - end) - end) - } -end -construct_comprehension = function(inner, clauses) - local current_stms = inner - for _, clause in reversed(clauses) do - local t = clause[1] - local _exp_0 = t - if "for" == _exp_0 then - local name, bounds - _, name, bounds = clause[1], clause[2], clause[3] - current_stms = { - "for", - name, - bounds, - current_stms - } - elseif "foreach" == _exp_0 then - local names, iter - _, names, iter = clause[1], clause[2], clause[3] - current_stms = { - "foreach", - names, - { - iter - }, - current_stms - } - elseif "when" == _exp_0 then - local cond - _, cond = clause[1], clause[2] - current_stms = { - "if", - cond, - current_stms - } - else - current_stms = error("Unknown comprehension clause: " .. t) - end - current_stms = { - current_stms - } - end - return current_stms[1] -end -Statement = Transformer({ - transform = function(self, tuple) - local _, node, fn - _, node, fn = tuple[1], tuple[2], tuple[3] - return fn(node) - end, - root_stms = function(self, body) - return transform_last_stm(body, implicitly_return(self)) - end, - ["return"] = function(self, node) - local ret_val = node[2] - local ret_val_type = ntype(ret_val) - if ret_val_type == "explist" and #ret_val == 2 then - ret_val = ret_val[2] - ret_val_type = ntype(ret_val) - end - if types.cascading[ret_val_type] then - return implicitly_return(self)(ret_val) - end - if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" then - ret_val = Value:transform_once(self, ret_val) - if ntype(ret_val) == "block_exp" then - return build.group(transform_last_stm(ret_val[2], function(stm) - return { - "return", - stm - } - end)) - end - end - node[2] = ret_val - return node - end, - declare_glob = function(self, node) - local names = extract_declarations(self) - if node[2] == "^" then - do - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #names do - local _continue_0 = false - repeat - local name = names[_index_0] - if not (name[2]:match("^%u")) then - _continue_0 = true - break - end - local _value_0 = name - _accum_0[_len_0] = _value_0 - _len_0 = _len_0 + 1 - _continue_0 = true - until true - if not _continue_0 then - break - end - end - names = _accum_0 - end - end - return { - "declare", - names - } - end, - assign = function(self, node) - local names, values = unpack(node, 2) - local num_values = #values - local num_names = #values - if num_names == 1 and num_values == 1 then - local first_value = values[1] - local first_name = names[1] - local first_type = ntype(first_value) - if first_type == "chain" then - first_value = Value:transform_once(self, first_value) - first_type = ntype(first_value) - end - local _exp_0 = ntype(first_value) - if "block_exp" == _exp_0 then - local block_body = first_value[2] - local idx = #block_body - block_body[idx] = build.assign_one(first_name, block_body[idx]) - return build.group({ - { - "declare", - { - first_name - } - }, - { - "do", - block_body - } - }) - elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 or "foreach" == _exp_0 or "for" == _exp_0 or "while" == _exp_0 then - return build.assign_one(first_name, Value:transform_once(self, first_value)) - else - values[1] = first_value - end - end - local transformed - if num_values == 1 then - local value = values[1] - local t = ntype(value) - if t == "decorated" then - value = self.transform.statement(value) - t = ntype(value) - end - if types.cascading[t] then - local ret - ret = function(stm) - if types.is_value(stm) then - return { - "assign", - names, - { - stm - } - } - else - return stm - end - end - transformed = build.group({ - { - "declare", - names - }, - self.transform.statement(value, ret, node) - }) - end - end - node = transformed or node - if destructure.has_destructure(names) then - return destructure.split_assign(self, node) - end - return node - end, - continue = function(self, node) - local continue_name = self:send("continue") - if not (continue_name) then - error("continue must be inside of a loop") - end - return build.group({ - build.assign_one(continue_name, "true"), - { - "break" - } - }) - end, - export = function(self, node) - if #node > 2 then - if node[2] == "class" then - local cls = smart_node(node[3]) - return build.group({ - { - "export", - { - cls.name - } - }, - cls - }) - else - return build.group({ - { - "export", - node[2] - }, - build.assign({ - names = node[2], - values = node[3] - }) - }) - end - else - return nil - end - end, - update = function(self, node) - local _, name, op, exp = unpack(node) - local op_final = op:match("^(.+)=$") - if not op_final then - error("Unknown op: " .. op) - end - if not (value_is_singular(exp)) then - exp = { - "parens", - exp - } - end - return build.assign_one(name, { - "exp", - name, - op_final, - exp - }) - end, - import = function(self, node) - local _, names, source = unpack(node) - local table_values - do - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #names do - local name = names[_index_0] - local dest_name - if ntype(name) == "colon" then - dest_name = name[2] - else - dest_name = name - end - local _value_0 = { - { - "key_literal", - name - }, - dest_name - } - _accum_0[_len_0] = _value_0 - _len_0 = _len_0 + 1 - end - table_values = _accum_0 - end - local dest = { - "table", - table_values - } - return { - "assign", - { - dest - }, - { - source - }, - [-1] = node[-1] - } - end, - comprehension = function(self, node, action) - local _, exp, clauses = unpack(node) - action = action or function(exp) - return { - exp - } - end - return construct_comprehension(action(exp), clauses) - end, - ["do"] = function(self, node, ret) - if ret then - node[2] = transform_last_stm(node[2], ret) - end - return node - end, - decorated = function(self, node) - local stm, dec = unpack(node, 2) - local wrapped - local _exp_0 = dec[1] - if "if" == _exp_0 then - local cond, fail = unpack(dec, 2) - if fail then - fail = { - "else", - { - fail - } - } - end - wrapped = { - "if", - cond, - { - stm - }, - fail - } - elseif "unless" == _exp_0 then - wrapped = { - "unless", - dec[2], - { - stm - } - } - elseif "comprehension" == _exp_0 then - wrapped = { - "comprehension", - stm, - dec[2] - } - else - wrapped = error("Unknown decorator " .. dec[1]) - end - if ntype(stm) == "assign" then - wrapped = build.group({ - build.declare({ - names = (function() - local _accum_0 = { } - local _len_0 = 1 - local _list_0 = stm[2] - for _index_0 = 1, #_list_0 do - local name = _list_0[_index_0] - if ntype(name) == "ref" then - _accum_0[_len_0] = name - _len_0 = _len_0 + 1 - end - end - return _accum_0 - end)() - }), - wrapped - }) - end - return wrapped - end, - unless = function(self, node) - return { - "if", - { - "not", - { - "parens", - node[2] - } - }, - unpack(node, 3) - } - end, - ["if"] = function(self, node, ret) - if ntype(node[2]) == "assign" then - local _, assign, body = unpack(node) - if destructure.has_destructure(assign[2]) then - local name = NameProxy("des") - body = { - destructure.build_assign(self, assign[2][1], name), - build.group(node[3]) - } - return build["do"]({ - build.assign_one(name, assign[3][1]), - { - "if", - name, - body, - unpack(node, 4) - } - }) - else - local name = assign[2][1] - return build["do"]({ - assign, - { - "if", - name, - unpack(node, 3) - } - }) - end - end - node = expand_elseif_assign(node) - if ret then - smart_node(node) - node['then'] = transform_last_stm(node['then'], ret) - for i = 4, #node do - local case = node[i] - local body_idx = #node[i] - case[body_idx] = transform_last_stm(case[body_idx], ret) - end - end - return node - end, - with = function(self, node, ret) - local exp, block = unpack(node, 2) - local copy_scope = true - local scope_name, named_assign - do - local last = last_stm(block) - if last then - if types.terminating[ntype(last)] then - ret = false - end - end - end - if ntype(exp) == "assign" then - local names, values = unpack(exp, 2) - local first_name = names[1] - if ntype(first_name) == "ref" then - scope_name = first_name - named_assign = exp - exp = values[1] - copy_scope = false - else - scope_name = NameProxy("with") - exp = values[1] - values[1] = scope_name - named_assign = { - "assign", - names, - values - } - end - elseif self:is_local(exp) then - scope_name = exp - copy_scope = false - end - scope_name = scope_name or NameProxy("with") - local out = build["do"]({ - copy_scope and build.assign_one(scope_name, exp) or NOOP, - named_assign or NOOP, - Run(function(self) - return self:set("scope_var", scope_name) - end), - unpack(block) - }) - if ret then - table.insert(out[2], ret(scope_name)) - end - return out - end, - foreach = function(self, node, _) - smart_node(node) - local source = unpack(node.iter) - local destructures = { } - do - local _accum_0 = { } - local _len_0 = 1 - for i, name in ipairs(node.names) do - if ntype(name) == "table" then - do - local proxy = NameProxy("des") - insert(destructures, destructure.build_assign(self, name, proxy)) - _accum_0[_len_0] = proxy - end - else - _accum_0[_len_0] = name - end - _len_0 = _len_0 + 1 - end - node.names = _accum_0 - end - if next(destructures) then - insert(destructures, build.group(node.body)) - node.body = destructures - end - if ntype(source) == "unpack" then - local list = source[2] - local index_name = NameProxy("index") - local list_name = self:is_local(list) and list or NameProxy("list") - local slice_var = nil - local bounds - if is_slice(list) then - local slice = list[#list] - table.remove(list) - table.remove(slice, 1) - if self:is_local(list) then - list_name = list - end - if slice[2] and slice[2] ~= "" then - local max_tmp_name = NameProxy("max") - slice_var = build.assign_one(max_tmp_name, slice[2]) - slice[2] = { - "exp", - max_tmp_name, - "<", - 0, - "and", - { - "length", - list_name - }, - "+", - max_tmp_name, - "or", - max_tmp_name - } - else - slice[2] = { - "length", - list_name - } - end - bounds = slice - else - bounds = { - 1, - { - "length", - list_name - } - } - end - return build.group({ - list_name ~= list and build.assign_one(list_name, list) or NOOP, - slice_var or NOOP, - build["for"]({ - name = index_name, - bounds = bounds, - body = { - { - "assign", - node.names, - { - NameProxy.index(list_name, index_name) - } - }, - build.group(node.body) - } - }) - }) - end - node.body = with_continue_listener(node.body) - end, - ["while"] = function(self, node) - smart_node(node) - node.body = with_continue_listener(node.body) - end, - ["for"] = function(self, node) - smart_node(node) - node.body = with_continue_listener(node.body) - end, - switch = function(self, node, ret) - local _, exp, conds = unpack(node) - local exp_name = NameProxy("exp") - local convert_cond - convert_cond = function(cond) - local t, case_exps, body = unpack(cond) - local out = { } - insert(out, t == "case" and "elseif" or "else") - if t ~= "else" then - local cond_exp = { } - for i, case in ipairs(case_exps) do - if i == 1 then - insert(cond_exp, "exp") - else - insert(cond_exp, "or") - end - if not (value_is_singular(case)) then - case = { - "parens", - case - } - end - insert(cond_exp, { - "exp", - case, - "==", - exp_name - }) - end - insert(out, cond_exp) - else - body = case_exps - end - if ret then - body = transform_last_stm(body, ret) - end - insert(out, body) - return out - end - local first = true - local if_stm = { - "if" - } - for _index_0 = 1, #conds do - local cond = conds[_index_0] - local if_cond = convert_cond(cond) - if first then - first = false - insert(if_stm, if_cond[2]) - insert(if_stm, if_cond[3]) - else - insert(if_stm, if_cond) - end - end - return build.group({ - build.assign_one(exp_name, exp), - if_stm - }) - end, - class = function(self, node, ret, parent_assign) - local _, name, parent_val, body = unpack(node) - if parent_val == "" then - parent_val = nil - end - local statements = { } - local properties = { } - for _index_0 = 1, #body do - local item = body[_index_0] - local _exp_0 = item[1] - if "stm" == _exp_0 then - insert(statements, item[2]) - elseif "props" == _exp_0 then - for _index_1 = 2, #item do - local tuple = item[_index_1] - if ntype(tuple[1]) == "self" then - insert(statements, build.assign_one(unpack(tuple))) - else - insert(properties, tuple) - end - end - end - end - local constructor - do - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #properties do - local _continue_0 = false - repeat - local tuple = properties[_index_0] - local key = tuple[1] - local _value_0 - if key[1] == "key_literal" and key[2] == constructor_name then - constructor = tuple[2] - _continue_0 = true - break - else - _value_0 = tuple - end - _accum_0[_len_0] = _value_0 - _len_0 = _len_0 + 1 - _continue_0 = true - until true - if not _continue_0 then - break - end - end - properties = _accum_0 - end - local parent_cls_name = NameProxy("parent") - local base_name = NameProxy("base") - local self_name = NameProxy("self") - local cls_name = NameProxy("class") - if not (constructor) then - if parent_val then - constructor = build.fndef({ - args = { - { - "..." - } - }, - arrow = "fat", - body = { - build.chain({ - base = "super", - { - "call", - { - "..." - } - } - }) - } - }) - else - constructor = build.fndef() - end - end - local real_name = name or parent_assign and parent_assign[2][1] - local _exp_0 = ntype(real_name) - if "chain" == _exp_0 then - local last = real_name[#real_name] - local _exp_1 = ntype(last) - if "dot" == _exp_1 then - real_name = { - "string", - '"', - last[2] - } - elseif "index" == _exp_1 then - real_name = last[2] - else - real_name = "nil" - end - elseif "nil" == _exp_0 then - real_name = "nil" - else - local name_t = type(real_name) - local flattened_name - if name_t == "string" then - flattened_name = real_name - elseif name_t == "table" and real_name[1] == "ref" then - flattened_name = real_name[2] - else - flattened_name = error("don't know how to extract name from " .. tostring(name_t)) - end - real_name = { - "string", - '"', - flattened_name - } - end - local cls = build.table({ - { - "__init", - constructor - }, - { - "__base", - base_name - }, - { - "__name", - real_name - }, - parent_val and { - "__parent", - parent_cls_name - } or nil - }) - local class_index - if parent_val then - local class_lookup = build["if"]({ - cond = { - "exp", - { - "ref", - "val" - }, - "==", - "nil" - }, - ["then"] = { - build.assign_one(LocalName("parent"), build.chain({ - base = "rawget", - { - "call", - { - { - "ref", - "cls" - }, - { - "string", - '"', - "__parent" - } - } - } - })), - build["if"]({ - cond = LocalName("parent"), - ["then"] = { - build.chain({ - base = LocalName("parent"), - { - "index", - "name" - } - }) - } - }) - } - }) - insert(class_lookup, { - "else", - { - "val" - } - }) - class_index = build.fndef({ - args = { - { - "cls" - }, - { - "name" - } - }, - body = { - build.assign_one(LocalName("val"), build.chain({ - base = "rawget", - { - "call", - { - base_name, - { - "ref", - "name" - } - } - } - })), - class_lookup - } - }) - else - class_index = base_name - end - local cls_mt = build.table({ - { - "__index", - class_index - }, - { - "__call", - build.fndef({ - args = { - { - "cls" - }, - { - "..." - } - }, - body = { - build.assign_one(self_name, build.chain({ - base = "setmetatable", - { - "call", - { - "{}", - base_name - } - } - })), - build.chain({ - base = "cls.__init", - { - "call", - { - self_name, - "..." - } - } - }), - self_name - } - }) - } - }) - cls = build.chain({ - base = "setmetatable", - { - "call", - { - cls, - cls_mt - } - } - }) - local value = nil - do - local out_body = { - Run(function(self) - if name then - self:put_name(name) - end - return self:set("super", function(block, chain) - local relative_parent = { - "chain", - cls_name, - { - "dot", - "__parent" - } - } - if not (chain) then - return relative_parent - end - local chain_tail = { - unpack(chain, 3) - } - local head = chain_tail[1] - if head == nil then - return relative_parent - end - local new_chain = relative_parent - local _exp_1 = head[1] - if "call" == _exp_1 then - local calling_name = block:get("current_block") - assert(calling_name, "missing calling name") - chain_tail[1] = { - "call", - { - "self", - unpack(head[2]) - } - } - if ntype(calling_name) == "key_literal" then - insert(new_chain, { - "dot", - calling_name[2] - }) - else - insert(new_chain, { - "index", - calling_name - }) - end - elseif "colon" == _exp_1 then - local call = chain_tail[2] - if call and call[1] == "call" then - chain_tail[1] = { - "dot", - head[2] - } - chain_tail[2] = { - "call", - { - "self", - unpack(call[2]) - } - } - end - end - for _index_0 = 1, #chain_tail do - local item = chain_tail[_index_0] - insert(new_chain, item) - end - return new_chain - end) - end), - { - "declare", - { - cls_name - } - }, - { - "declare_glob", - "*" - }, - parent_val and build.assign_one(parent_cls_name, parent_val) or NOOP, - build.assign_one(base_name, { - "table", - properties - }), - build.assign_one(base_name:chain("__index"), base_name), - parent_val and build.chain({ - base = "setmetatable", - { - "call", - { - base_name, - build.chain({ - base = parent_cls_name, - { - "dot", - "__base" - } - }) - } - } - }) or NOOP, - build.assign_one(cls_name, cls), - build.assign_one(base_name:chain("__class"), cls_name), - build.group((function() - if #statements > 0 then - return { - build.assign_one(LocalName("self"), cls_name), - build.group(statements) - } - end - end)()), - parent_val and build["if"]({ - cond = { - "exp", - parent_cls_name:chain("__inherited") - }, - ["then"] = { - parent_cls_name:chain("__inherited", { - "call", - { - parent_cls_name, - cls_name - } - }) - } - }) or NOOP, - build.group((function() - if name then - return { - build.assign_one(name, cls_name) - } - end - end)()), - (function() - if ret then - return ret(cls_name) - end - end)() - } - value = build.group({ - build.group((function() - if ntype(name) == "value" then - return { - build.declare({ - names = { - name - } - }) - } - end - end)()), - build["do"](out_body) - }) - end - return value - end -}) -do - local _class_0 - local _base_0 = { - body_idx = { - ["for"] = 4, - ["while"] = 3, - foreach = 4 - }, - convert = function(self, node) - local index = self.body_idx[ntype(node)] - node[index] = self:mutate_body(node[index]) - return self:wrap(node) - end, - wrap = function(self, node, group_type) - if group_type == nil then - group_type = "block_exp" - end - return build[group_type]({ - build.assign_one(self.accum_name, build.table()), - build.assign_one(self.len_name, 1), - node, - group_type == "block_exp" and self.accum_name or NOOP - }) - end, - mutate_body = function(self, body) - local single_stm = is_singular(body) - local val - if single_stm and types.is_value(single_stm) then - body = { } - val = single_stm - else - body = transform_last_stm(body, function(n) - if types.is_value(n) then - return build.assign_one(self.value_name, n) - else - return build.group({ - { - "declare", - { - self.value_name - } - }, - n - }) - end - end) - val = self.value_name - end - local update = { - build.assign_one(NameProxy.index(self.accum_name, self.len_name), val), - { - "update", - self.len_name, - "+=", - 1 - } - } - insert(body, build.group(update)) - return body - end - } - _base_0.__index = _base_0 - _class_0 = setmetatable({ - __init = function(self, accum_name) - self.accum_name = NameProxy("accum") - self.value_name = NameProxy("value") - self.len_name = NameProxy("len") - end, - __base = _base_0, - __name = "Accumulator" - }, { - __index = _base_0, - __call = function(cls, ...) - local _self_0 = setmetatable({}, _base_0) - cls.__init(_self_0, ...) - return _self_0 - end - }) - _base_0.__class = _class_0 - Accumulator = _class_0 -end -default_accumulator = function(self, node) - return Accumulator():convert(node) -end -implicitly_return = function(scope) - local is_top = true - local fn - fn = function(stm) - local t = ntype(stm) - if t == "decorated" then - stm = scope.transform.statement(stm) - t = ntype(stm) - end - if types.cascading[t] then - is_top = false - return scope.transform.statement(stm, fn) - elseif types.manual_return[t] or not types.is_value(stm) then - if is_top and t == "return" and stm[2] == "" then - return NOOP - else - return stm - end - else - if t == "comprehension" and not types.comprehension_has_value(stm) then - return stm - else - return { - "return", - stm - } - end - end - end - return fn -end -Value = Transformer({ - ["for"] = default_accumulator, - ["while"] = default_accumulator, - foreach = default_accumulator, - ["do"] = function(self, node) - return build.block_exp(node[2]) - end, - decorated = function(self, node) - return self.transform.statement(node) - end, - class = function(self, node) - return build.block_exp({ - node - }) - end, - string = function(self, node) - local delim = node[2] - local convert_part - convert_part = function(part) - if type(part) == "string" or part == nil then - return { - "string", - delim, - part or "" - } - else - return build.chain({ - base = "tostring", - { - "call", - { - part[2] - } - } - }) - end - end - if #node <= 3 then - if type(node[3]) == "string" then - return node - else - return convert_part(node[3]) - end - end - local e = { - "exp", - convert_part(node[3]) - } - for i = 4, #node do - insert(e, "..") - insert(e, convert_part(node[i])) - end - return e - end, - comprehension = function(self, node) - local a = Accumulator() - node = self.transform.statement(node, function(exp) - return a:mutate_body({ - exp - }) - end) - return a:wrap(node) - end, - tblcomprehension = function(self, node) - local _, explist, clauses = unpack(node) - local key_exp, value_exp = unpack(explist) - local accum = NameProxy("tbl") - local inner - if value_exp then - local dest = build.chain({ - base = accum, - { - "index", - key_exp - } - }) - inner = { - build.assign_one(dest, value_exp) - } - else - local key_name, val_name = NameProxy("key"), NameProxy("val") - local dest = build.chain({ - base = accum, - { - "index", - key_name - } - }) - inner = { - build.assign({ - names = { - key_name, - val_name - }, - values = { - key_exp - } - }), - build.assign_one(dest, val_name) - } - end - return build.block_exp({ - build.assign_one(accum, build.table()), - construct_comprehension(inner, clauses), - accum - }) - end, - fndef = function(self, node) - smart_node(node) - node.body = transform_last_stm(node.body, implicitly_return(self)) - node.body = { - Run(function(self) - return self:listen("varargs", function() end) - end), - unpack(node.body) - } - return node - end, - ["if"] = function(self, node) - return build.block_exp({ - node - }) - end, - unless = function(self, node) - return build.block_exp({ - node - }) - end, - with = function(self, node) - return build.block_exp({ - node - }) - end, - switch = function(self, node) - return build.block_exp({ - node - }) - end, - chain = function(self, node) - for i = 2, #node do - local part = node[i] - if ntype(part) == "dot" and data.lua_keywords[part[2]] then - node[i] = { - "index", - { - "string", - '"', - part[2] - } - } - end - end - if ntype(node[2]) == "string" then - node[2] = { - "parens", - node[2] - } - end - if chain_is_stub(node) then - local base_name = NameProxy("base") - local fn_name = NameProxy("fn") - local colon = table.remove(node) - local is_super = ntype(node[2]) == "ref" and node[2][2] == "super" - return build.block_exp({ - build.assign({ - names = { - base_name - }, - values = { - node - } - }), - build.assign({ - names = { - fn_name - }, - values = { - build.chain({ - base = base_name, - { - "dot", - colon[2] - } - }) - } - }), - build.fndef({ - args = { - { - "..." - } - }, - body = { - build.chain({ - base = fn_name, - { - "call", - { - is_super and "self" or base_name, - "..." - } - } - }) - } - }) - }) - end - end, - block_exp = function(self, node) - local _, body = unpack(node) - local fn = nil - local arg_list = { } - fn = smart_node(build.fndef({ - body = { - Run(function(self) - return self:listen("varargs", function() - insert(arg_list, "...") - insert(fn.args, { - "..." - }) - return self:unlisten("varargs") - end) - end), - unpack(body) - } - })) - return build.chain({ - base = { - "parens", - fn - }, - { - "call", - arg_list - } - }) - end -}) return { - Statement = Statement, - Value = Value, - Run = Run + Statement = require("moonscript.transform.statement"), + Value = require("moonscript.transform.value") } diff --git a/moonscript/transform.moon b/moonscript/transform.moon index 27127d2d..29a93ad2 100644 --- a/moonscript/transform.moon +++ b/moonscript/transform.moon @@ -1,977 +1,5 @@ -types = require "moonscript.types" -util = require "moonscript.util" -data = require "moonscript.data" - -import reversed, unpack from util -import ntype, mtype, build, smart_node, is_slice, value_is_singular from types -import insert from table - -import NameProxy, LocalName from require "moonscript.transform.names" -import Run, transform_last_stm, last_stm, chain_is_stub from require "moonscript.transform.statements" - -import Transformer from require "moonscript.transform.transformer" - -destructure = require "moonscript.transform.destructure" -NOOP = {"noop"} - -local * - --- is a body a sindle expression/statement -is_singular = (body) -> - return false if #body != 1 - if "group" == ntype body - is_singular body[2] - else - body[1] - --- this mutates body searching for assigns -extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) => - for i=start,#body - stm = body[i] - continue if stm == nil - stm = @transform.statement stm - body[i] = stm - switch stm[1] - when "assign", "declare" - for name in *stm[2] - if ntype(name) == "ref" - insert out, name - elseif type(name) == "string" - -- TODO: don't use string literal as ref - insert out, name - when "group" - extract_declarations @, stm[2], 1, out - out - -expand_elseif_assign = (ifstm) -> - for i = 4, #ifstm - case = ifstm[i] - if ntype(case) == "elseif" and ntype(case[2]) == "assign" - split = { unpack ifstm, 1, i - 1 } - insert split, { - "else", { - {"if", case[2], case[3], unpack ifstm, i + 1} - } - } - return split - - ifstm - -constructor_name = "new" - -with_continue_listener = (body) -> - continue_name = nil - - { - Run => - @listen "continue", -> - unless continue_name - continue_name = NameProxy"continue" - @put_name continue_name - continue_name - - build.group body - - Run => - return unless continue_name - last = last_stm body - enclose_lines = types.terminating[last and ntype(last)] - - @put_name continue_name, nil - @splice (lines) -> - lines = {"do", {lines}} if enclose_lines - - { - {"assign", {continue_name}, {"false"}} - {"repeat", "true", { - lines - {"assign", {continue_name}, {"true"}} - }} - {"if", {"not", continue_name}, { - {"break"} - }} - } - } - - -construct_comprehension = (inner, clauses) -> - current_stms = inner - for _, clause in reversed clauses - t = clause[1] - current_stms = switch t - when "for" - {_, name, bounds} = clause - {"for", name, bounds, current_stms} - when "foreach" - {_, names, iter} = clause - {"foreach", names, {iter}, current_stms} - when "when" - {_, cond} = clause - {"if", cond, current_stms} - else - error "Unknown comprehension clause: "..t - - current_stms = {current_stms} - - current_stms[1] - -Statement = Transformer { - transform: (tuple) => - {_, node, fn} = tuple - fn node - - root_stms: (body) => - transform_last_stm body, implicitly_return @ - - return: (node) => - ret_val = node[2] - ret_val_type = ntype ret_val - - if ret_val_type == "explist" and #ret_val == 2 - ret_val = ret_val[2] - ret_val_type = ntype ret_val - - if types.cascading[ret_val_type] - return implicitly_return(@) ret_val - - -- flatten things that create block exp - if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" - ret_val = Value\transform_once @, ret_val - if ntype(ret_val) == "block_exp" - return build.group transform_last_stm ret_val[2], (stm)-> - {"return", stm} - - node[2] = ret_val - node - - declare_glob: (node) => - names = extract_declarations @ - - if node[2] == "^" - names = for name in *names - continue unless name[2]\match "^%u" - name - - {"declare", names} - - assign: (node) => - names, values = unpack node, 2 - - num_values = #values - num_names = #values - - -- special code simplifications for single assigns - if num_names == 1 and num_values == 1 - first_value = values[1] - first_name = names[1] - first_type = ntype first_value - - -- reduce colon stub chain to block exp - if first_type == "chain" - first_value = Value\transform_once @, first_value - first_type = ntype first_value - - switch ntype first_value - when "block_exp" - block_body = first_value[2] - idx = #block_body - block_body[idx] = build.assign_one first_name, block_body[idx] - - return build.group { - {"declare", {first_name}} - {"do", block_body} - } - - when "comprehension", "tblcomprehension", "foreach", "for", "while" - return build.assign_one first_name, Value\transform_once @, first_value - else - values[1] = first_value - - -- bubble cascading assigns - transformed = if num_values == 1 - value = values[1] - t = ntype value - - if t == "decorated" - value = @transform.statement value - t = ntype value - - if types.cascading[t] - ret = (stm) -> - if types.is_value stm - {"assign", names, {stm}} - else - stm - - build.group { - {"declare", names} - @transform.statement value, ret, node - } - - node = transformed or node - - if destructure.has_destructure names - return destructure.split_assign @, node - - node - - continue: (node) => - continue_name = @send "continue" - error "continue must be inside of a loop" unless continue_name - build.group { - build.assign_one continue_name, "true" - {"break"} - } - - export: (node) => - -- assign values if they are included - if #node > 2 - if node[2] == "class" - cls = smart_node node[3] - build.group { - {"export", {cls.name}} - cls - } - else - -- pull out vawlues and assign them after the export - build.group { - { "export", node[2] } - build.assign { - names: node[2] - values: node[3] - } - } - else - nil - - update: (node) => - _, name, op, exp = unpack node - op_final = op\match "^(.+)=$" - error "Unknown op: "..op if not op_final - exp = {"parens", exp} unless value_is_singular exp - build.assign_one name, {"exp", name, op_final, exp} - - import: (node) => - _, names, source = unpack node - table_values = for name in *names - dest_name = if ntype(name) == "colon" - name[2] - else - name - - {{"key_literal", name}, dest_name} - - dest = { "table", table_values } - { "assign", {dest}, {source}, [-1]: node[-1] } - - comprehension: (node, action) => - _, exp, clauses = unpack node - - action = action or (exp) -> {exp} - construct_comprehension action(exp), clauses - - do: (node, ret) => - node[2] = transform_last_stm node[2], ret if ret - node - - decorated: (node) => - stm, dec = unpack node, 2 - - wrapped = switch dec[1] - when "if" - cond, fail = unpack dec, 2 - fail = { "else", { fail } } if fail - { "if", cond, { stm }, fail } - when "unless" - { "unless", dec[2], { stm } } - when "comprehension" - { "comprehension", stm, dec[2] } - else - error "Unknown decorator " .. dec[1] - - if ntype(stm) == "assign" - wrapped = build.group { - build.declare names: [name for name in *stm[2] when ntype(name) == "ref"] - wrapped - } - - wrapped - - unless: (node) => - { "if", {"not", {"parens", node[2]}}, unpack node, 3 } - - if: (node, ret) => - -- expand assign in cond - if ntype(node[2]) == "assign" - _, assign, body = unpack node - if destructure.has_destructure assign[2] - name = NameProxy "des" - - body = { - destructure.build_assign @, assign[2][1], name - build.group node[3] - } - - return build.do { - build.assign_one name, assign[3][1] - {"if", name, body, unpack node, 4} - } - else - name = assign[2][1] - return build["do"] { - assign - {"if", name, unpack node, 3} - } - - node = expand_elseif_assign node - - -- apply cascading return decorator - if ret - smart_node node - -- mutate all the bodies - node['then'] = transform_last_stm node['then'], ret - for i = 4, #node - case = node[i] - body_idx = #node[i] - case[body_idx] = transform_last_stm case[body_idx], ret - - node - - with: (node, ret) => - exp, block = unpack node, 2 - - copy_scope = true - local scope_name, named_assign - - if last = last_stm block - ret = false if types.terminating[ntype(last)] - - if ntype(exp) == "assign" - names, values = unpack exp, 2 - first_name = names[1] - - if ntype(first_name) == "ref" - scope_name = first_name - named_assign = exp - exp = values[1] - copy_scope = false - else - scope_name = NameProxy "with" - exp = values[1] - values[1] = scope_name - named_assign = {"assign", names, values} - - elseif @is_local exp - scope_name = exp - copy_scope = false - - scope_name or= NameProxy "with" - - out = build.do { - copy_scope and build.assign_one(scope_name, exp) or NOOP - named_assign or NOOP - Run => @set "scope_var", scope_name - unpack block - } - - if ret - table.insert out[2], ret scope_name - - out - - foreach: (node, _) => - smart_node node - source = unpack node.iter - - destructures = {} - node.names = for i, name in ipairs node.names - if ntype(name) == "table" - with proxy = NameProxy "des" - insert destructures, destructure.build_assign @, name, proxy - else - name - - if next destructures - insert destructures, build.group node.body - node.body = destructures - - if ntype(source) == "unpack" - list = source[2] - - index_name = NameProxy "index" - - list_name = @is_local(list) and list or NameProxy "list" - - slice_var = nil - bounds = if is_slice list - slice = list[#list] - table.remove list - table.remove slice, 1 - - list_name = list if @is_local list - - slice[2] = if slice[2] and slice[2] != "" - max_tmp_name = NameProxy "max" - slice_var = build.assign_one max_tmp_name, slice[2] - {"exp", max_tmp_name, "<", 0 - "and", {"length", list_name}, "+", max_tmp_name - "or", max_tmp_name } - else - {"length", list_name} - - slice - else - {1, {"length", list_name}} - - return build.group { - list_name != list and build.assign_one(list_name, list) or NOOP - slice_var or NOOP - build["for"] { - name: index_name - bounds: bounds - body: { - {"assign", node.names, { NameProxy.index list_name, index_name }} - build.group node.body - } - } - } - - node.body = with_continue_listener node.body - - while: (node) => - smart_node node - node.body = with_continue_listener node.body - - for: (node) => - smart_node node - node.body = with_continue_listener node.body - - switch: (node, ret) => - _, exp, conds = unpack node - exp_name = NameProxy "exp" - - -- convert switch conds into if statment conds - convert_cond = (cond) -> - t, case_exps, body = unpack cond - out = {} - insert out, t == "case" and "elseif" or "else" - if t != "else" - cond_exp = {} - for i, case in ipairs case_exps - if i == 1 - insert cond_exp, "exp" - else - insert cond_exp, "or" - - case = {"parens", case} unless value_is_singular case - insert cond_exp, {"exp", case, "==", exp_name} - - insert out, cond_exp - else - body = case_exps - - if ret - body = transform_last_stm body, ret - - insert out, body - - out - - first = true - if_stm = {"if"} - for cond in *conds - if_cond = convert_cond cond - if first - first = false - insert if_stm, if_cond[2] - insert if_stm, if_cond[3] - else - insert if_stm, if_cond - - build.group { - build.assign_one exp_name, exp - if_stm - } - - class: (node, ret, parent_assign) => - _, name, parent_val, body = unpack node - parent_val = nil if parent_val == "" - - -- split apart properties and statements - statements = {} - properties = {} - for item in *body - switch item[1] - when "stm" - insert statements, item[2] - when "props" - for tuple in *item[2,] - if ntype(tuple[1]) == "self" - insert statements, build.assign_one unpack tuple - else - insert properties, tuple - - -- find constructor - local constructor - properties = for tuple in *properties - key = tuple[1] - if key[1] == "key_literal" and key[2] == constructor_name - constructor = tuple[2] - continue - else - tuple - - parent_cls_name = NameProxy "parent" - base_name = NameProxy "base" - self_name = NameProxy "self" - cls_name = NameProxy "class" - - unless constructor - constructor = if parent_val - build.fndef { - args: {{"..."}} - arrow: "fat" - body: { - build.chain { base: "super", {"call", {"..."}} } - } - } - else - build.fndef! - - real_name = name or parent_assign and parent_assign[2][1] - real_name = switch ntype real_name - when "chain" - last = real_name[#real_name] - switch ntype last - when "dot" - {"string", '"', last[2]} - when "index" - last[2] - else - "nil" - when "nil" - "nil" - else - name_t = type real_name - -- TODO: don't use string literal as ref - flattened_name = if name_t == "string" - real_name - elseif name_t == "table" and real_name[1] == "ref" - real_name[2] - else - error "don't know how to extract name from #{name_t}" - - {"string", '"', flattened_name} - - cls = build.table { - {"__init", constructor} - {"__base", base_name} - {"__name", real_name} -- "quote the string" - parent_val and {"__parent", parent_cls_name} or nil - } - - -- looking up a name in the class object - class_index = if parent_val - class_lookup = build["if"] { - cond: { "exp", {"ref", "val"}, "==", "nil" } - then: { - build.assign_one LocalName"parent", build.chain { - base: "rawget" - { - "call", { - {"ref", "cls"} - {"string", '"', "__parent"} - } - } - } - - build.if { - cond: LocalName "parent" - then: { - build.chain { - base: LocalName "parent" - {"index", "name"} - } - } - } - } - } - insert class_lookup, {"else", {"val"}} - - build.fndef { - args: {{"cls"}, {"name"}} - body: { - build.assign_one LocalName"val", build.chain { - base: "rawget", {"call", {base_name, {"ref", "name"}}} - } - class_lookup - } - } - else - base_name - - cls_mt = build.table { - {"__index", class_index} - {"__call", build.fndef { - args: {{"cls"}, {"..."}} - body: { - build.assign_one self_name, build.chain { - base: "setmetatable" - {"call", {"{}", base_name}} - } - build.chain { - base: "cls.__init" - {"call", {self_name, "..."}} - } - self_name - } - }} - } - - cls = build.chain { - base: "setmetatable" - {"call", {cls, cls_mt}} - } - - value = nil - with build - out_body = { - Run => - -- make sure we don't assign the class to a local inside the do - @put_name name if name - - @set "super", (block, chain) -> - relative_parent = { - "chain", - cls_name - {"dot", "__parent"} - } - - return relative_parent unless chain - - chain_tail = { unpack chain, 3 } - head = chain_tail[1] - - if head == nil - return relative_parent - - new_chain = relative_parent - - switch head[1] - -- calling super, inject calling name and self into chain - when "call" - calling_name = block\get"current_block" - assert calling_name, "missing calling name" - chain_tail[1] = {"call", {"self", unpack head[2]}} - - if ntype(calling_name) == "key_literal" - insert new_chain, {"dot", calling_name[2]} - else - insert new_chain, {"index", calling_name} - - -- colon call on super, replace class with self as first arg - when "colon" - call = chain_tail[2] - -- calling chain tail - if call and call[1] == "call" - chain_tail[1] = { - "dot" - head[2] - } - - chain_tail[2] = { - "call" - { - "self" - unpack call[2] - } - } - - insert new_chain, item for item in *chain_tail - new_chain - - {"declare", { cls_name }} - {"declare_glob", "*"} - - parent_val and .assign_one(parent_cls_name, parent_val) or NOOP - - .assign_one base_name, {"table", properties} - .assign_one base_name\chain"__index", base_name - - parent_val and .chain({ - base: "setmetatable" - {"call", { - base_name, - .chain { base: parent_cls_name, {"dot", "__base"}} - }} - }) or NOOP - - .assign_one cls_name, cls - .assign_one base_name\chain"__class", cls_name - - .group if #statements > 0 then { - .assign_one LocalName"self", cls_name - .group statements - } - - -- run the inherited callback - parent_val and .if({ - cond: {"exp", parent_cls_name\chain "__inherited" } - then: { - parent_cls_name\chain "__inherited", {"call", { - parent_cls_name, cls_name - }} - } - }) or NOOP - - .group if name then { - .assign_one name, cls_name - } - - if ret - ret cls_name - } - - value = .group { - .group if ntype(name) == "value" then { - .declare names: {name} - } - - .do out_body - } - - value +{ + Statement: require "moonscript.transform.statement" + Value: require "moonscript.transform.value" } - -class Accumulator - body_idx: { for: 4, while: 3, foreach: 4 } - - new: (accum_name) => - @accum_name = NameProxy "accum" - @value_name = NameProxy "value" - @len_name = NameProxy "len" - - -- wraps node and mutates body - convert: (node) => - index = @body_idx[ntype node] - node[index] = @mutate_body node[index] - @wrap node - - -- wrap the node into a block_exp - wrap: (node, group_type="block_exp") => - build[group_type] { - build.assign_one @accum_name, build.table! - build.assign_one @len_name, 1 - node - group_type == "block_exp" and @accum_name or NOOP - } - - -- mutates the body of a loop construct to save last value into accumulator - mutate_body: (body) => - -- shortcut to write simpler code if body is a single expression - single_stm = is_singular body - val = if single_stm and types.is_value single_stm - body = {} - single_stm - else - body = transform_last_stm body, (n) -> - if types.is_value n - build.assign_one @value_name, n - else - -- just ignore it - build.group { - {"declare", {@value_name}} - n - } - @value_name - - update = { - build.assign_one NameProxy.index(@accum_name, @len_name), val - {"update", @len_name, "+=", 1} - } - - insert body, build.group update - body - -default_accumulator = (node) => - Accumulator!\convert node - -implicitly_return = (scope) -> - is_top = true - fn = (stm) -> - t = ntype stm - - -- expand decorated - if t == "decorated" - stm = scope.transform.statement stm - t = ntype stm - - if types.cascading[t] - is_top = false - scope.transform.statement stm, fn - elseif types.manual_return[t] or not types.is_value stm - -- remove blank return statement - if is_top and t == "return" and stm[2] == "" - NOOP - else - stm - else - if t == "comprehension" and not types.comprehension_has_value stm - stm - else - {"return", stm} - - fn - -Value = Transformer { - for: default_accumulator - while: default_accumulator - foreach: default_accumulator - - do: (node) => - build.block_exp node[2] - - decorated: (node) => - @transform.statement node - - class: (node) => - build.block_exp { node } - - string: (node) => - delim = node[2] - - convert_part = (part) -> - if type(part) == "string" or part == nil - {"string", delim, part or ""} - else - build.chain { base: "tostring", {"call", {part[2]}} } - - -- reduced to single item - if #node <= 3 - return if type(node[3]) == "string" - node - else - convert_part node[3] - - e = {"exp", convert_part node[3]} - - for i=4, #node - insert e, ".." - insert e, convert_part node[i] - e - - comprehension: (node) => - a = Accumulator! - node = @transform.statement node, (exp) -> - a\mutate_body {exp} - a\wrap node - - tblcomprehension: (node) => - _, explist, clauses = unpack node - key_exp, value_exp = unpack explist - - accum = NameProxy "tbl" - - inner = if value_exp - dest = build.chain { base: accum, {"index", key_exp} } - { build.assign_one dest, value_exp } - else - -- If we only have single expression then - -- unpack the result into key and value - key_name, val_name = NameProxy"key", NameProxy"val" - dest = build.chain { base: accum, {"index", key_name} } - { - build.assign names: {key_name, val_name}, values: {key_exp} - build.assign_one dest, val_name - } - - build.block_exp { - build.assign_one accum, build.table! - construct_comprehension inner, clauses - accum - } - - fndef: (node) => - smart_node node - node.body = transform_last_stm node.body, implicitly_return self - node.body = { - Run => @listen "varargs", -> -- capture event - unpack node.body - } - - node - - if: (node) => - build.block_exp { node } - - unless: (node) => - build.block_exp { node } - - with: (node) => - build.block_exp { node } - - switch: (node) => - build.block_exp { node } - - -- pull out colon chain - chain: (node) => - -- escape lua keywords used in dot accessors - for i=2,#node - part = node[i] - if ntype(part) == "dot" and data.lua_keywords[part[2]] - node[i] = { "index", {"string", '"', part[2]} } - - if ntype(node[2]) == "string" - -- add parens if callee is raw string - node[2] = {"parens", node[2] } - - if chain_is_stub node - base_name = NameProxy "base" - fn_name = NameProxy "fn" - colon = table.remove node - - is_super = ntype(node[2]) == "ref" and node[2][2] == "super" - build.block_exp { - build.assign { - names: {base_name} - values: {node} - } - - build.assign { - names: {fn_name} - values: { - build.chain { base: base_name, {"dot", colon[2]} } - } - } - - build.fndef { - args: {{"..."}} - body: { - build.chain { - base: fn_name, {"call", {is_super and "self" or base_name, "..."}} - } - } - } - } - - block_exp: (node) => - _, body = unpack node - - fn = nil - arg_list = {} - - fn = smart_node build.fndef body: { - Run => - @listen "varargs", -> - insert arg_list, "..." - insert fn.args, {"..."} - @unlisten "varargs" - - unpack body - } - - build.chain { base: {"parens", fn}, {"call", arg_list} } -} - -{ :Statement, :Value, :Run } diff --git a/moonscript/transform/accumulator.lua b/moonscript/transform/accumulator.lua new file mode 100644 index 00000000..539c65fd --- /dev/null +++ b/moonscript/transform/accumulator.lua @@ -0,0 +1,110 @@ +local types = require("moonscript.types") +local build, ntype, NOOP +build, ntype, NOOP = types.build, types.ntype, types.NOOP +local NameProxy +NameProxy = require("moonscript.transform.names").NameProxy +local insert +insert = table.insert +local is_singular +is_singular = function(body) + if #body ~= 1 then + return false + end + if "group" == ntype(body) then + return is_singular(body[2]) + else + return body[1] + end +end +local transform_last_stm +transform_last_stm = require("moonscript.transform.statements").transform_last_stm +local Accumulator +do + local _class_0 + local _base_0 = { + body_idx = { + ["for"] = 4, + ["while"] = 3, + foreach = 4 + }, + convert = function(self, node) + local index = self.body_idx[ntype(node)] + node[index] = self:mutate_body(node[index]) + return self:wrap(node) + end, + wrap = function(self, node, group_type) + if group_type == nil then + group_type = "block_exp" + end + return build[group_type]({ + build.assign_one(self.accum_name, build.table()), + build.assign_one(self.len_name, 1), + node, + group_type == "block_exp" and self.accum_name or NOOP + }) + end, + mutate_body = function(self, body) + local single_stm = is_singular(body) + local val + if single_stm and types.is_value(single_stm) then + body = { } + val = single_stm + else + body = transform_last_stm(body, function(n) + if types.is_value(n) then + return build.assign_one(self.value_name, n) + else + return build.group({ + { + "declare", + { + self.value_name + } + }, + n + }) + end + end) + val = self.value_name + end + local update = { + build.assign_one(NameProxy.index(self.accum_name, self.len_name), val), + { + "update", + self.len_name, + "+=", + 1 + } + } + insert(body, build.group(update)) + return body + end + } + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function(self, accum_name) + self.accum_name = NameProxy("accum") + self.value_name = NameProxy("value") + self.len_name = NameProxy("len") + end, + __base = _base_0, + __name = "Accumulator" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Accumulator = _class_0 +end +local default_accumulator +default_accumulator = function(self, node) + return Accumulator():convert(node) +end +return { + Accumulator = Accumulator, + default_accumulator = default_accumulator +} diff --git a/moonscript/transform/accumulator.moon b/moonscript/transform/accumulator.moon new file mode 100644 index 00000000..62ff015c --- /dev/null +++ b/moonscript/transform/accumulator.moon @@ -0,0 +1,71 @@ +types = require "moonscript.types" + +import build, ntype, NOOP from types +import NameProxy from require "moonscript.transform.names" + +import insert from table + +-- is a body a single expression/statement +is_singular = (body) -> + return false if #body != 1 + if "group" == ntype body + is_singular body[2] + else + body[1] + +import transform_last_stm from require "moonscript.transform.statements" + +class Accumulator + body_idx: { for: 4, while: 3, foreach: 4 } + + new: (accum_name) => + @accum_name = NameProxy "accum" + @value_name = NameProxy "value" + @len_name = NameProxy "len" + + -- wraps node and mutates body + convert: (node) => + index = @body_idx[ntype node] + node[index] = @mutate_body node[index] + @wrap node + + -- wrap the node into a block_exp + wrap: (node, group_type="block_exp") => + build[group_type] { + build.assign_one @accum_name, build.table! + build.assign_one @len_name, 1 + node + group_type == "block_exp" and @accum_name or NOOP + } + + -- mutates the body of a loop construct to save last value into accumulator + mutate_body: (body) => + -- shortcut to write simpler code if body is a single expression + single_stm = is_singular body + val = if single_stm and types.is_value single_stm + body = {} + single_stm + else + body = transform_last_stm body, (n) -> + if types.is_value n + build.assign_one @value_name, n + else + -- just ignore it + build.group { + {"declare", {@value_name}} + n + } + @value_name + + update = { + build.assign_one NameProxy.index(@accum_name, @len_name), val + {"update", @len_name, "+=", 1} + } + + insert body, build.group update + body + +default_accumulator = (node) => + Accumulator!\convert node + +{ :Accumulator, :default_accumulator } diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua new file mode 100644 index 00000000..683fb90c --- /dev/null +++ b/moonscript/transform/statement.lua @@ -0,0 +1,1161 @@ +local Transformer +Transformer = require("moonscript.transform.transformer").Transformer +local NameProxy, LocalName +do + local _obj_0 = require("moonscript.transform.names") + NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName +end +local Run, transform_last_stm, implicitly_return, construct_comprehension, last_stm +do + local _obj_0 = require("moonscript.transform.statements") + Run, transform_last_stm, implicitly_return, construct_comprehension, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.construct_comprehension, _obj_0.last_stm +end +local types = require("moonscript.types") +local build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP +build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP = types.build, types.ntype, types.is_value, types.smart_node, types.value_is_singular, types.is_slice, types.NOOP +local insert +insert = table.insert +local destructure = require("moonscript.transform.destructure") +local CONSTRUCTOR_NAME = "new" +local with_continue_listener +with_continue_listener = function(body) + local continue_name = nil + return { + Run(function(self) + return self:listen("continue", function() + if not (continue_name) then + continue_name = NameProxy("continue") + self:put_name(continue_name) + end + return continue_name + end) + end), + build.group(body), + Run(function(self) + if not (continue_name) then + return + end + local last = last_stm(body) + local enclose_lines = types.terminating[last and ntype(last)] + self:put_name(continue_name, nil) + return self:splice(function(lines) + if enclose_lines then + lines = { + "do", + { + lines + } + } + end + return { + { + "assign", + { + continue_name + }, + { + "false" + } + }, + { + "repeat", + "true", + { + lines, + { + "assign", + { + continue_name + }, + { + "true" + } + } + } + }, + { + "if", + { + "not", + continue_name + }, + { + { + "break" + } + } + } + } + end) + end) + } +end +local extract_declarations +extract_declarations = function(self, body, start, out) + if body == nil then + body = self.current_stms + end + if start == nil then + start = self.current_stm_i + 1 + end + if out == nil then + out = { } + end + for i = start, #body do + local _continue_0 = false + repeat + local stm = body[i] + if stm == nil then + _continue_0 = true + break + end + stm = self.transform.statement(stm) + body[i] = stm + local _exp_0 = stm[1] + if "assign" == _exp_0 or "declare" == _exp_0 then + local _list_0 = stm[2] + for _index_0 = 1, #_list_0 do + local name = _list_0[_index_0] + if ntype(name) == "ref" then + insert(out, name) + elseif type(name) == "string" then + insert(out, name) + end + end + elseif "group" == _exp_0 then + extract_declarations(self, stm[2], 1, out) + end + _continue_0 = true + until true + if not _continue_0 then + break + end + end + return out +end +local expand_elseif_assign +expand_elseif_assign = function(ifstm) + for i = 4, #ifstm do + local case = ifstm[i] + if ntype(case) == "elseif" and ntype(case[2]) == "assign" then + local split = { + unpack(ifstm, 1, i - 1) + } + insert(split, { + "else", + { + { + "if", + case[2], + case[3], + unpack(ifstm, i + 1) + } + } + }) + return split + end + end + return ifstm +end +return Transformer({ + transform = function(self, tuple) + local _, node, fn + _, node, fn = tuple[1], tuple[2], tuple[3] + return fn(node) + end, + root_stms = function(self, body) + return transform_last_stm(body, implicitly_return(self)) + end, + ["return"] = function(self, node) + local ret_val = node[2] + local ret_val_type = ntype(ret_val) + if ret_val_type == "explist" and #ret_val == 2 then + ret_val = ret_val[2] + ret_val_type = ntype(ret_val) + end + if types.cascading[ret_val_type] then + return implicitly_return(self)(ret_val) + end + if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" then + local Value = require("moonscript.transform.value") + ret_val = Value:transform_once(self, ret_val) + if ntype(ret_val) == "block_exp" then + return build.group(transform_last_stm(ret_val[2], function(stm) + return { + "return", + stm + } + end)) + end + end + node[2] = ret_val + return node + end, + declare_glob = function(self, node) + local names = extract_declarations(self) + if node[2] == "^" then + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #names do + local _continue_0 = false + repeat + local name = names[_index_0] + if not (name[2]:match("^%u")) then + _continue_0 = true + break + end + local _value_0 = name + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + _continue_0 = true + until true + if not _continue_0 then + break + end + end + names = _accum_0 + end + end + return { + "declare", + names + } + end, + assign = function(self, node) + local names, values = unpack(node, 2) + local num_values = #values + local num_names = #values + if num_names == 1 and num_values == 1 then + local first_value = values[1] + local first_name = names[1] + local first_type = ntype(first_value) + if first_type == "chain" then + local Value = require("moonscript.transform.value") + first_value = Value:transform_once(self, first_value) + first_type = ntype(first_value) + end + local _exp_0 = ntype(first_value) + if "block_exp" == _exp_0 then + local block_body = first_value[2] + local idx = #block_body + block_body[idx] = build.assign_one(first_name, block_body[idx]) + return build.group({ + { + "declare", + { + first_name + } + }, + { + "do", + block_body + } + }) + elseif "comprehension" == _exp_0 or "tblcomprehension" == _exp_0 or "foreach" == _exp_0 or "for" == _exp_0 or "while" == _exp_0 then + local Value = require("moonscript.transform.value") + return build.assign_one(first_name, Value:transform_once(self, first_value)) + else + values[1] = first_value + end + end + local transformed + if num_values == 1 then + local value = values[1] + local t = ntype(value) + if t == "decorated" then + value = self.transform.statement(value) + t = ntype(value) + end + if types.cascading[t] then + local ret + ret = function(stm) + if is_value(stm) then + return { + "assign", + names, + { + stm + } + } + else + return stm + end + end + transformed = build.group({ + { + "declare", + names + }, + self.transform.statement(value, ret, node) + }) + end + end + node = transformed or node + if destructure.has_destructure(names) then + return destructure.split_assign(self, node) + end + return node + end, + continue = function(self, node) + local continue_name = self:send("continue") + if not (continue_name) then + error("continue must be inside of a loop") + end + return build.group({ + build.assign_one(continue_name, "true"), + { + "break" + } + }) + end, + export = function(self, node) + if #node > 2 then + if node[2] == "class" then + local cls = smart_node(node[3]) + return build.group({ + { + "export", + { + cls.name + } + }, + cls + }) + else + return build.group({ + { + "export", + node[2] + }, + build.assign({ + names = node[2], + values = node[3] + }) + }) + end + else + return nil + end + end, + update = function(self, node) + local _, name, op, exp = unpack(node) + local op_final = op:match("^(.+)=$") + if not op_final then + error("Unknown op: " .. op) + end + if not (value_is_singular(exp)) then + exp = { + "parens", + exp + } + end + return build.assign_one(name, { + "exp", + name, + op_final, + exp + }) + end, + import = function(self, node) + local _, names, source = unpack(node) + local table_values + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #names do + local name = names[_index_0] + local dest_name + if ntype(name) == "colon" then + dest_name = name[2] + else + dest_name = name + end + local _value_0 = { + { + "key_literal", + name + }, + dest_name + } + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + end + table_values = _accum_0 + end + local dest = { + "table", + table_values + } + return { + "assign", + { + dest + }, + { + source + }, + [-1] = node[-1] + } + end, + comprehension = function(self, node, action) + local _, exp, clauses = unpack(node) + action = action or function(exp) + return { + exp + } + end + return construct_comprehension(action(exp), clauses) + end, + ["do"] = function(self, node, ret) + if ret then + node[2] = transform_last_stm(node[2], ret) + end + return node + end, + decorated = function(self, node) + local stm, dec = unpack(node, 2) + local wrapped + local _exp_0 = dec[1] + if "if" == _exp_0 then + local cond, fail = unpack(dec, 2) + if fail then + fail = { + "else", + { + fail + } + } + end + wrapped = { + "if", + cond, + { + stm + }, + fail + } + elseif "unless" == _exp_0 then + wrapped = { + "unless", + dec[2], + { + stm + } + } + elseif "comprehension" == _exp_0 then + wrapped = { + "comprehension", + stm, + dec[2] + } + else + wrapped = error("Unknown decorator " .. dec[1]) + end + if ntype(stm) == "assign" then + wrapped = build.group({ + build.declare({ + names = (function() + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = stm[2] + for _index_0 = 1, #_list_0 do + local name = _list_0[_index_0] + if ntype(name) == "ref" then + _accum_0[_len_0] = name + _len_0 = _len_0 + 1 + end + end + return _accum_0 + end)() + }), + wrapped + }) + end + return wrapped + end, + unless = function(self, node) + return { + "if", + { + "not", + { + "parens", + node[2] + } + }, + unpack(node, 3) + } + end, + ["if"] = function(self, node, ret) + if ntype(node[2]) == "assign" then + local _, assign, body = unpack(node) + if destructure.has_destructure(assign[2]) then + local name = NameProxy("des") + body = { + destructure.build_assign(self, assign[2][1], name), + build.group(node[3]) + } + return build["do"]({ + build.assign_one(name, assign[3][1]), + { + "if", + name, + body, + unpack(node, 4) + } + }) + else + local name = assign[2][1] + return build["do"]({ + assign, + { + "if", + name, + unpack(node, 3) + } + }) + end + end + node = expand_elseif_assign(node) + if ret then + smart_node(node) + node['then'] = transform_last_stm(node['then'], ret) + for i = 4, #node do + local case = node[i] + local body_idx = #node[i] + case[body_idx] = transform_last_stm(case[body_idx], ret) + end + end + return node + end, + with = function(self, node, ret) + local exp, block = unpack(node, 2) + local copy_scope = true + local scope_name, named_assign + do + local last = last_stm(block) + if last then + if types.terminating[ntype(last)] then + ret = false + end + end + end + if ntype(exp) == "assign" then + local names, values = unpack(exp, 2) + local first_name = names[1] + if ntype(first_name) == "ref" then + scope_name = first_name + named_assign = exp + exp = values[1] + copy_scope = false + else + scope_name = NameProxy("with") + exp = values[1] + values[1] = scope_name + named_assign = { + "assign", + names, + values + } + end + elseif self:is_local(exp) then + scope_name = exp + copy_scope = false + end + scope_name = scope_name or NameProxy("with") + local out = build["do"]({ + copy_scope and build.assign_one(scope_name, exp) or NOOP, + named_assign or NOOP, + Run(function(self) + return self:set("scope_var", scope_name) + end), + unpack(block) + }) + if ret then + table.insert(out[2], ret(scope_name)) + end + return out + end, + foreach = function(self, node, _) + smart_node(node) + local source = unpack(node.iter) + local destructures = { } + do + local _accum_0 = { } + local _len_0 = 1 + for i, name in ipairs(node.names) do + if ntype(name) == "table" then + do + local proxy = NameProxy("des") + insert(destructures, destructure.build_assign(self, name, proxy)) + _accum_0[_len_0] = proxy + end + else + _accum_0[_len_0] = name + end + _len_0 = _len_0 + 1 + end + node.names = _accum_0 + end + if next(destructures) then + insert(destructures, build.group(node.body)) + node.body = destructures + end + if ntype(source) == "unpack" then + local list = source[2] + local index_name = NameProxy("index") + local list_name = self:is_local(list) and list or NameProxy("list") + local slice_var = nil + local bounds + if is_slice(list) then + local slice = list[#list] + table.remove(list) + table.remove(slice, 1) + if self:is_local(list) then + list_name = list + end + if slice[2] and slice[2] ~= "" then + local max_tmp_name = NameProxy("max") + slice_var = build.assign_one(max_tmp_name, slice[2]) + slice[2] = { + "exp", + max_tmp_name, + "<", + 0, + "and", + { + "length", + list_name + }, + "+", + max_tmp_name, + "or", + max_tmp_name + } + else + slice[2] = { + "length", + list_name + } + end + bounds = slice + else + bounds = { + 1, + { + "length", + list_name + } + } + end + return build.group({ + list_name ~= list and build.assign_one(list_name, list) or NOOP, + slice_var or NOOP, + build["for"]({ + name = index_name, + bounds = bounds, + body = { + { + "assign", + node.names, + { + NameProxy.index(list_name, index_name) + } + }, + build.group(node.body) + } + }) + }) + end + node.body = with_continue_listener(node.body) + end, + ["while"] = function(self, node) + smart_node(node) + node.body = with_continue_listener(node.body) + end, + ["for"] = function(self, node) + smart_node(node) + node.body = with_continue_listener(node.body) + end, + switch = function(self, node, ret) + local _, exp, conds = unpack(node) + local exp_name = NameProxy("exp") + local convert_cond + convert_cond = function(cond) + local t, case_exps, body = unpack(cond) + local out = { } + insert(out, t == "case" and "elseif" or "else") + if t ~= "else" then + local cond_exp = { } + for i, case in ipairs(case_exps) do + if i == 1 then + insert(cond_exp, "exp") + else + insert(cond_exp, "or") + end + if not (value_is_singular(case)) then + case = { + "parens", + case + } + end + insert(cond_exp, { + "exp", + case, + "==", + exp_name + }) + end + insert(out, cond_exp) + else + body = case_exps + end + if ret then + body = transform_last_stm(body, ret) + end + insert(out, body) + return out + end + local first = true + local if_stm = { + "if" + } + for _index_0 = 1, #conds do + local cond = conds[_index_0] + local if_cond = convert_cond(cond) + if first then + first = false + insert(if_stm, if_cond[2]) + insert(if_stm, if_cond[3]) + else + insert(if_stm, if_cond) + end + end + return build.group({ + build.assign_one(exp_name, exp), + if_stm + }) + end, + class = function(self, node, ret, parent_assign) + local _, name, parent_val, body = unpack(node) + if parent_val == "" then + parent_val = nil + end + local statements = { } + local properties = { } + for _index_0 = 1, #body do + local item = body[_index_0] + local _exp_0 = item[1] + if "stm" == _exp_0 then + insert(statements, item[2]) + elseif "props" == _exp_0 then + for _index_1 = 2, #item do + local tuple = item[_index_1] + if ntype(tuple[1]) == "self" then + insert(statements, build.assign_one(unpack(tuple))) + else + insert(properties, tuple) + end + end + end + end + local constructor + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #properties do + local _continue_0 = false + repeat + local tuple = properties[_index_0] + local key = tuple[1] + local _value_0 + if key[1] == "key_literal" and key[2] == CONSTRUCTOR_NAME then + constructor = tuple[2] + _continue_0 = true + break + else + _value_0 = tuple + end + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + _continue_0 = true + until true + if not _continue_0 then + break + end + end + properties = _accum_0 + end + local parent_cls_name = NameProxy("parent") + local base_name = NameProxy("base") + local self_name = NameProxy("self") + local cls_name = NameProxy("class") + if not (constructor) then + if parent_val then + constructor = build.fndef({ + args = { + { + "..." + } + }, + arrow = "fat", + body = { + build.chain({ + base = "super", + { + "call", + { + "..." + } + } + }) + } + }) + else + constructor = build.fndef() + end + end + local real_name = name or parent_assign and parent_assign[2][1] + local _exp_0 = ntype(real_name) + if "chain" == _exp_0 then + local last = real_name[#real_name] + local _exp_1 = ntype(last) + if "dot" == _exp_1 then + real_name = { + "string", + '"', + last[2] + } + elseif "index" == _exp_1 then + real_name = last[2] + else + real_name = "nil" + end + elseif "nil" == _exp_0 then + real_name = "nil" + else + local name_t = type(real_name) + local flattened_name + if name_t == "string" then + flattened_name = real_name + elseif name_t == "table" and real_name[1] == "ref" then + flattened_name = real_name[2] + else + flattened_name = error("don't know how to extract name from " .. tostring(name_t)) + end + real_name = { + "string", + '"', + flattened_name + } + end + local cls = build.table({ + { + "__init", + constructor + }, + { + "__base", + base_name + }, + { + "__name", + real_name + }, + parent_val and { + "__parent", + parent_cls_name + } or nil + }) + local class_index + if parent_val then + local class_lookup = build["if"]({ + cond = { + "exp", + { + "ref", + "val" + }, + "==", + "nil" + }, + ["then"] = { + build.assign_one(LocalName("parent"), build.chain({ + base = "rawget", + { + "call", + { + { + "ref", + "cls" + }, + { + "string", + '"', + "__parent" + } + } + } + })), + build["if"]({ + cond = LocalName("parent"), + ["then"] = { + build.chain({ + base = LocalName("parent"), + { + "index", + "name" + } + }) + } + }) + } + }) + insert(class_lookup, { + "else", + { + "val" + } + }) + class_index = build.fndef({ + args = { + { + "cls" + }, + { + "name" + } + }, + body = { + build.assign_one(LocalName("val"), build.chain({ + base = "rawget", + { + "call", + { + base_name, + { + "ref", + "name" + } + } + } + })), + class_lookup + } + }) + else + class_index = base_name + end + local cls_mt = build.table({ + { + "__index", + class_index + }, + { + "__call", + build.fndef({ + args = { + { + "cls" + }, + { + "..." + } + }, + body = { + build.assign_one(self_name, build.chain({ + base = "setmetatable", + { + "call", + { + "{}", + base_name + } + } + })), + build.chain({ + base = "cls.__init", + { + "call", + { + self_name, + "..." + } + } + }), + self_name + } + }) + } + }) + cls = build.chain({ + base = "setmetatable", + { + "call", + { + cls, + cls_mt + } + } + }) + local value = nil + do + local out_body = { + Run(function(self) + if name then + self:put_name(name) + end + return self:set("super", function(block, chain) + local relative_parent = { + "chain", + cls_name, + { + "dot", + "__parent" + } + } + if not (chain) then + return relative_parent + end + local chain_tail = { + unpack(chain, 3) + } + local head = chain_tail[1] + if head == nil then + return relative_parent + end + local new_chain = relative_parent + local _exp_1 = head[1] + if "call" == _exp_1 then + local calling_name = block:get("current_block") + assert(calling_name, "missing calling name") + chain_tail[1] = { + "call", + { + "self", + unpack(head[2]) + } + } + if ntype(calling_name) == "key_literal" then + insert(new_chain, { + "dot", + calling_name[2] + }) + else + insert(new_chain, { + "index", + calling_name + }) + end + elseif "colon" == _exp_1 then + local call = chain_tail[2] + if call and call[1] == "call" then + chain_tail[1] = { + "dot", + head[2] + } + chain_tail[2] = { + "call", + { + "self", + unpack(call[2]) + } + } + end + end + for _index_0 = 1, #chain_tail do + local item = chain_tail[_index_0] + insert(new_chain, item) + end + return new_chain + end) + end), + { + "declare", + { + cls_name + } + }, + { + "declare_glob", + "*" + }, + parent_val and build.assign_one(parent_cls_name, parent_val) or NOOP, + build.assign_one(base_name, { + "table", + properties + }), + build.assign_one(base_name:chain("__index"), base_name), + parent_val and build.chain({ + base = "setmetatable", + { + "call", + { + base_name, + build.chain({ + base = parent_cls_name, + { + "dot", + "__base" + } + }) + } + } + }) or NOOP, + build.assign_one(cls_name, cls), + build.assign_one(base_name:chain("__class"), cls_name), + build.group((function() + if #statements > 0 then + return { + build.assign_one(LocalName("self"), cls_name), + build.group(statements) + } + end + end)()), + parent_val and build["if"]({ + cond = { + "exp", + parent_cls_name:chain("__inherited") + }, + ["then"] = { + parent_cls_name:chain("__inherited", { + "call", + { + parent_cls_name, + cls_name + } + }) + } + }) or NOOP, + build.group((function() + if name then + return { + build.assign_one(name, cls_name) + } + end + end)()), + (function() + if ret then + return ret(cls_name) + end + end)() + } + value = build.group({ + build.group((function() + if ntype(name) == "value" then + return { + build.declare({ + names = { + name + } + }) + } + end + end)()), + build["do"](out_body) + }) + end + return value + end +}) diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon new file mode 100644 index 00000000..646403f9 --- /dev/null +++ b/moonscript/transform/statement.moon @@ -0,0 +1,720 @@ +import Transformer from require "moonscript.transform.transformer" + +import NameProxy, LocalName from require "moonscript.transform.names" + +import Run, transform_last_stm, implicitly_return, construct_comprehension, + last_stm from require "moonscript.transform.statements" + +types = require "moonscript.types" + +import build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP + from types + +import insert from table + +destructure = require "moonscript.transform.destructure" + +CONSTRUCTOR_NAME = "new" + +with_continue_listener = (body) -> + continue_name = nil + + { + Run => + @listen "continue", -> + unless continue_name + continue_name = NameProxy"continue" + @put_name continue_name + continue_name + + build.group body + + Run => + return unless continue_name + last = last_stm body + enclose_lines = types.terminating[last and ntype(last)] + + @put_name continue_name, nil + @splice (lines) -> + lines = {"do", {lines}} if enclose_lines + + { + {"assign", {continue_name}, {"false"}} + {"repeat", "true", { + lines + {"assign", {continue_name}, {"true"}} + }} + {"if", {"not", continue_name}, { + {"break"} + }} + } + } + + +-- this mutates body searching for assigns +extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) => + for i=start,#body + stm = body[i] + continue if stm == nil + stm = @transform.statement stm + body[i] = stm + switch stm[1] + when "assign", "declare" + for name in *stm[2] + if ntype(name) == "ref" + insert out, name + elseif type(name) == "string" + -- TODO: don't use string literal as ref + insert out, name + when "group" + extract_declarations @, stm[2], 1, out + out + +expand_elseif_assign = (ifstm) -> + for i = 4, #ifstm + case = ifstm[i] + if ntype(case) == "elseif" and ntype(case[2]) == "assign" + split = { unpack ifstm, 1, i - 1 } + insert split, { + "else", { + {"if", case[2], case[3], unpack ifstm, i + 1} + } + } + return split + + ifstm + + +Transformer { + transform: (tuple) => + {_, node, fn} = tuple + fn node + + root_stms: (body) => + transform_last_stm body, implicitly_return @ + + return: (node) => + ret_val = node[2] + ret_val_type = ntype ret_val + + if ret_val_type == "explist" and #ret_val == 2 + ret_val = ret_val[2] + ret_val_type = ntype ret_val + + if types.cascading[ret_val_type] + return implicitly_return(@) ret_val + + -- flatten things that create block exp + if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" + -- TODO: clean this up + Value = require "moonscript.transform.value" + ret_val = Value\transform_once @, ret_val + if ntype(ret_val) == "block_exp" + return build.group transform_last_stm ret_val[2], (stm)-> + {"return", stm} + + node[2] = ret_val + node + + declare_glob: (node) => + names = extract_declarations @ + + if node[2] == "^" + names = for name in *names + continue unless name[2]\match "^%u" + name + + {"declare", names} + + assign: (node) => + names, values = unpack node, 2 + + num_values = #values + num_names = #values + + -- special code simplifications for single assigns + if num_names == 1 and num_values == 1 + first_value = values[1] + first_name = names[1] + first_type = ntype first_value + + -- reduce colon stub chain to block exp + if first_type == "chain" + -- TODO: clean this up + Value = require "moonscript.transform.value" + first_value = Value\transform_once @, first_value + first_type = ntype first_value + + switch ntype first_value + when "block_exp" + block_body = first_value[2] + idx = #block_body + block_body[idx] = build.assign_one first_name, block_body[idx] + + return build.group { + {"declare", {first_name}} + {"do", block_body} + } + + when "comprehension", "tblcomprehension", "foreach", "for", "while" + -- TODO: clean this up + Value = require "moonscript.transform.value" + return build.assign_one first_name, Value\transform_once @, first_value + else + values[1] = first_value + + -- bubble cascading assigns + transformed = if num_values == 1 + value = values[1] + t = ntype value + + if t == "decorated" + value = @transform.statement value + t = ntype value + + if types.cascading[t] + ret = (stm) -> + if is_value stm + {"assign", names, {stm}} + else + stm + + build.group { + {"declare", names} + @transform.statement value, ret, node + } + + node = transformed or node + + if destructure.has_destructure names + return destructure.split_assign @, node + + node + + continue: (node) => + continue_name = @send "continue" + error "continue must be inside of a loop" unless continue_name + build.group { + build.assign_one continue_name, "true" + {"break"} + } + + export: (node) => + -- assign values if they are included + if #node > 2 + if node[2] == "class" + cls = smart_node node[3] + build.group { + {"export", {cls.name}} + cls + } + else + -- pull out vawlues and assign them after the export + build.group { + { "export", node[2] } + build.assign { + names: node[2] + values: node[3] + } + } + else + nil + + update: (node) => + _, name, op, exp = unpack node + op_final = op\match "^(.+)=$" + error "Unknown op: "..op if not op_final + exp = {"parens", exp} unless value_is_singular exp + build.assign_one name, {"exp", name, op_final, exp} + + import: (node) => + _, names, source = unpack node + table_values = for name in *names + dest_name = if ntype(name) == "colon" + name[2] + else + name + + {{"key_literal", name}, dest_name} + + dest = { "table", table_values } + { "assign", {dest}, {source}, [-1]: node[-1] } + + comprehension: (node, action) => + _, exp, clauses = unpack node + + action = action or (exp) -> {exp} + construct_comprehension action(exp), clauses + + do: (node, ret) => + node[2] = transform_last_stm node[2], ret if ret + node + + decorated: (node) => + stm, dec = unpack node, 2 + + wrapped = switch dec[1] + when "if" + cond, fail = unpack dec, 2 + fail = { "else", { fail } } if fail + { "if", cond, { stm }, fail } + when "unless" + { "unless", dec[2], { stm } } + when "comprehension" + { "comprehension", stm, dec[2] } + else + error "Unknown decorator " .. dec[1] + + if ntype(stm) == "assign" + wrapped = build.group { + build.declare names: [name for name in *stm[2] when ntype(name) == "ref"] + wrapped + } + + wrapped + + unless: (node) => + { "if", {"not", {"parens", node[2]}}, unpack node, 3 } + + if: (node, ret) => + -- expand assign in cond + if ntype(node[2]) == "assign" + _, assign, body = unpack node + if destructure.has_destructure assign[2] + name = NameProxy "des" + + body = { + destructure.build_assign @, assign[2][1], name + build.group node[3] + } + + return build.do { + build.assign_one name, assign[3][1] + {"if", name, body, unpack node, 4} + } + else + name = assign[2][1] + return build["do"] { + assign + {"if", name, unpack node, 3} + } + + node = expand_elseif_assign node + + -- apply cascading return decorator + if ret + smart_node node + -- mutate all the bodies + node['then'] = transform_last_stm node['then'], ret + for i = 4, #node + case = node[i] + body_idx = #node[i] + case[body_idx] = transform_last_stm case[body_idx], ret + + node + + with: (node, ret) => + exp, block = unpack node, 2 + + copy_scope = true + local scope_name, named_assign + + if last = last_stm block + ret = false if types.terminating[ntype(last)] + + if ntype(exp) == "assign" + names, values = unpack exp, 2 + first_name = names[1] + + if ntype(first_name) == "ref" + scope_name = first_name + named_assign = exp + exp = values[1] + copy_scope = false + else + scope_name = NameProxy "with" + exp = values[1] + values[1] = scope_name + named_assign = {"assign", names, values} + + elseif @is_local exp + scope_name = exp + copy_scope = false + + scope_name or= NameProxy "with" + + out = build.do { + copy_scope and build.assign_one(scope_name, exp) or NOOP + named_assign or NOOP + Run => @set "scope_var", scope_name + unpack block + } + + if ret + table.insert out[2], ret scope_name + + out + + foreach: (node, _) => + smart_node node + source = unpack node.iter + + destructures = {} + node.names = for i, name in ipairs node.names + if ntype(name) == "table" + with proxy = NameProxy "des" + insert destructures, destructure.build_assign @, name, proxy + else + name + + if next destructures + insert destructures, build.group node.body + node.body = destructures + + if ntype(source) == "unpack" + list = source[2] + + index_name = NameProxy "index" + + list_name = @is_local(list) and list or NameProxy "list" + + slice_var = nil + bounds = if is_slice list + slice = list[#list] + table.remove list + table.remove slice, 1 + + list_name = list if @is_local list + + slice[2] = if slice[2] and slice[2] != "" + max_tmp_name = NameProxy "max" + slice_var = build.assign_one max_tmp_name, slice[2] + {"exp", max_tmp_name, "<", 0 + "and", {"length", list_name}, "+", max_tmp_name + "or", max_tmp_name } + else + {"length", list_name} + + slice + else + {1, {"length", list_name}} + + return build.group { + list_name != list and build.assign_one(list_name, list) or NOOP + slice_var or NOOP + build["for"] { + name: index_name + bounds: bounds + body: { + {"assign", node.names, { NameProxy.index list_name, index_name }} + build.group node.body + } + } + } + + node.body = with_continue_listener node.body + + while: (node) => + smart_node node + node.body = with_continue_listener node.body + + for: (node) => + smart_node node + node.body = with_continue_listener node.body + + switch: (node, ret) => + _, exp, conds = unpack node + exp_name = NameProxy "exp" + + -- convert switch conds into if statment conds + convert_cond = (cond) -> + t, case_exps, body = unpack cond + out = {} + insert out, t == "case" and "elseif" or "else" + if t != "else" + cond_exp = {} + for i, case in ipairs case_exps + if i == 1 + insert cond_exp, "exp" + else + insert cond_exp, "or" + + case = {"parens", case} unless value_is_singular case + insert cond_exp, {"exp", case, "==", exp_name} + + insert out, cond_exp + else + body = case_exps + + if ret + body = transform_last_stm body, ret + + insert out, body + + out + + first = true + if_stm = {"if"} + for cond in *conds + if_cond = convert_cond cond + if first + first = false + insert if_stm, if_cond[2] + insert if_stm, if_cond[3] + else + insert if_stm, if_cond + + build.group { + build.assign_one exp_name, exp + if_stm + } + + class: (node, ret, parent_assign) => + _, name, parent_val, body = unpack node + parent_val = nil if parent_val == "" + + -- split apart properties and statements + statements = {} + properties = {} + for item in *body + switch item[1] + when "stm" + insert statements, item[2] + when "props" + for tuple in *item[2,] + if ntype(tuple[1]) == "self" + insert statements, build.assign_one unpack tuple + else + insert properties, tuple + + -- find constructor + local constructor + properties = for tuple in *properties + key = tuple[1] + if key[1] == "key_literal" and key[2] == CONSTRUCTOR_NAME + constructor = tuple[2] + continue + else + tuple + + parent_cls_name = NameProxy "parent" + base_name = NameProxy "base" + self_name = NameProxy "self" + cls_name = NameProxy "class" + + unless constructor + constructor = if parent_val + build.fndef { + args: {{"..."}} + arrow: "fat" + body: { + build.chain { base: "super", {"call", {"..."}} } + } + } + else + build.fndef! + + real_name = name or parent_assign and parent_assign[2][1] + real_name = switch ntype real_name + when "chain" + last = real_name[#real_name] + switch ntype last + when "dot" + {"string", '"', last[2]} + when "index" + last[2] + else + "nil" + when "nil" + "nil" + else + name_t = type real_name + -- TODO: don't use string literal as ref + flattened_name = if name_t == "string" + real_name + elseif name_t == "table" and real_name[1] == "ref" + real_name[2] + else + error "don't know how to extract name from #{name_t}" + + {"string", '"', flattened_name} + + cls = build.table { + {"__init", constructor} + {"__base", base_name} + {"__name", real_name} -- "quote the string" + parent_val and {"__parent", parent_cls_name} or nil + } + + -- looking up a name in the class object + class_index = if parent_val + class_lookup = build["if"] { + cond: { "exp", {"ref", "val"}, "==", "nil" } + then: { + build.assign_one LocalName"parent", build.chain { + base: "rawget" + { + "call", { + {"ref", "cls"} + {"string", '"', "__parent"} + } + } + } + + build.if { + cond: LocalName "parent" + then: { + build.chain { + base: LocalName "parent" + {"index", "name"} + } + } + } + } + } + insert class_lookup, {"else", {"val"}} + + build.fndef { + args: {{"cls"}, {"name"}} + body: { + build.assign_one LocalName"val", build.chain { + base: "rawget", {"call", {base_name, {"ref", "name"}}} + } + class_lookup + } + } + else + base_name + + cls_mt = build.table { + {"__index", class_index} + {"__call", build.fndef { + args: {{"cls"}, {"..."}} + body: { + build.assign_one self_name, build.chain { + base: "setmetatable" + {"call", {"{}", base_name}} + } + build.chain { + base: "cls.__init" + {"call", {self_name, "..."}} + } + self_name + } + }} + } + + cls = build.chain { + base: "setmetatable" + {"call", {cls, cls_mt}} + } + + value = nil + with build + out_body = { + Run => + -- make sure we don't assign the class to a local inside the do + @put_name name if name + + @set "super", (block, chain) -> + relative_parent = { + "chain", + cls_name + {"dot", "__parent"} + } + + return relative_parent unless chain + + chain_tail = { unpack chain, 3 } + head = chain_tail[1] + + if head == nil + return relative_parent + + new_chain = relative_parent + + switch head[1] + -- calling super, inject calling name and self into chain + when "call" + calling_name = block\get"current_block" + assert calling_name, "missing calling name" + chain_tail[1] = {"call", {"self", unpack head[2]}} + + if ntype(calling_name) == "key_literal" + insert new_chain, {"dot", calling_name[2]} + else + insert new_chain, {"index", calling_name} + + -- colon call on super, replace class with self as first arg + when "colon" + call = chain_tail[2] + -- calling chain tail + if call and call[1] == "call" + chain_tail[1] = { + "dot" + head[2] + } + + chain_tail[2] = { + "call" + { + "self" + unpack call[2] + } + } + + insert new_chain, item for item in *chain_tail + new_chain + + {"declare", { cls_name }} + {"declare_glob", "*"} + + parent_val and .assign_one(parent_cls_name, parent_val) or NOOP + + .assign_one base_name, {"table", properties} + .assign_one base_name\chain"__index", base_name + + parent_val and .chain({ + base: "setmetatable" + {"call", { + base_name, + .chain { base: parent_cls_name, {"dot", "__base"}} + }} + }) or NOOP + + .assign_one cls_name, cls + .assign_one base_name\chain"__class", cls_name + + .group if #statements > 0 then { + .assign_one LocalName"self", cls_name + .group statements + } + + -- run the inherited callback + parent_val and .if({ + cond: {"exp", parent_cls_name\chain "__inherited" } + then: { + parent_cls_name\chain "__inherited", {"call", { + parent_cls_name, cls_name + }} + } + }) or NOOP + + .group if name then { + .assign_one name, cls_name + } + + if ret + ret cls_name + } + + value = .group { + .group if ntype(name) == "value" then { + .declare names: {name} + } + + .do out_body + } + + value +} diff --git a/moonscript/transform/statements.lua b/moonscript/transform/statements.lua index 994df895..dc562a5d 100644 --- a/moonscript/transform/statements.lua +++ b/moonscript/transform/statements.lua @@ -1,8 +1,6 @@ -local ntype, mtype -do - local _obj_0 = require("moonscript.types") - ntype, mtype = _obj_0.ntype, _obj_0.mtype -end +local types = require("moonscript.types") +local ntype, mtype, is_value, NOOP +ntype, mtype, is_value, NOOP = types.ntype, types.mtype, types.is_value, types.NOOP local Run do local _class_0 @@ -74,9 +72,88 @@ chain_is_stub = function(chain) local stub = chain[#chain] return stub and ntype(stub) == "colon" end +local implicitly_return +implicitly_return = function(scope) + local is_top = true + local fn + fn = function(stm) + local t = ntype(stm) + if t == "decorated" then + stm = scope.transform.statement(stm) + t = ntype(stm) + end + if types.cascading[t] then + is_top = false + return scope.transform.statement(stm, fn) + elseif types.manual_return[t] or not is_value(stm) then + if is_top and t == "return" and stm[2] == "" then + return NOOP + else + return stm + end + else + if t == "comprehension" and not types.comprehension_has_value(stm) then + return stm + else + return { + "return", + stm + } + end + end + end + return fn +end +local reversed +reversed = require("moonscript.util").reversed +local construct_comprehension +construct_comprehension = function(inner, clauses) + local current_stms = inner + for _, clause in reversed(clauses) do + local t = clause[1] + local _exp_0 = t + if "for" == _exp_0 then + local name, bounds + _, name, bounds = clause[1], clause[2], clause[3] + current_stms = { + "for", + name, + bounds, + current_stms + } + elseif "foreach" == _exp_0 then + local names, iter + _, names, iter = clause[1], clause[2], clause[3] + current_stms = { + "foreach", + names, + { + iter + }, + current_stms + } + elseif "when" == _exp_0 then + local cond + _, cond = clause[1], clause[2] + current_stms = { + "if", + cond, + current_stms + } + else + current_stms = error("Unknown comprehension clause: " .. t) + end + current_stms = { + current_stms + } + end + return current_stms[1] +end return { Run = Run, last_stm = last_stm, transform_last_stm = transform_last_stm, - chain_is_stub = chain_is_stub + chain_is_stub = chain_is_stub, + implicitly_return = implicitly_return, + construct_comprehension = construct_comprehension } diff --git a/moonscript/transform/statements.moon b/moonscript/transform/statements.moon index 59d8e364..3edb0dec 100644 --- a/moonscript/transform/statements.moon +++ b/moonscript/transform/statements.moon @@ -1,5 +1,6 @@ -import ntype, mtype from require "moonscript.types" +types = require "moonscript.types" +import ntype, mtype, is_value, NOOP from types -- A Run is a special statement node that lets a function run and mutate the -- state of the compiler @@ -44,5 +45,56 @@ chain_is_stub = (chain) -> stub = chain[#chain] stub and ntype(stub) == "colon" -{:Run, :last_stm, :transform_last_stm, :chain_is_stub} +implicitly_return = (scope) -> + is_top = true + fn = (stm) -> + t = ntype stm + + -- expand decorated + if t == "decorated" + stm = scope.transform.statement stm + t = ntype stm + + if types.cascading[t] + is_top = false + scope.transform.statement stm, fn + elseif types.manual_return[t] or not is_value stm + -- remove blank return statement + if is_top and t == "return" and stm[2] == "" + NOOP + else + stm + else + if t == "comprehension" and not types.comprehension_has_value stm + stm + else + {"return", stm} + + fn + +-- TODO: reversed unecessary +import reversed from require "moonscript.util" +construct_comprehension = (inner, clauses) -> + current_stms = inner + for _, clause in reversed clauses + t = clause[1] + current_stms = switch t + when "for" + {_, name, bounds} = clause + {"for", name, bounds, current_stms} + when "foreach" + {_, names, iter} = clause + {"foreach", names, {iter}, current_stms} + when "when" + {_, cond} = clause + {"if", cond, current_stms} + else + error "Unknown comprehension clause: "..t + + current_stms = {current_stms} + + current_stms[1] + +{:Run, :last_stm, :transform_last_stm, :chain_is_stub, :implicitly_return, + :construct_comprehension} diff --git a/moonscript/transform/transformer.lua b/moonscript/transform/transformer.lua new file mode 100644 index 00000000..4ea16959 --- /dev/null +++ b/moonscript/transform/transformer.lua @@ -0,0 +1,74 @@ +local ntype +ntype = require("moonscript.types").ntype +local Transformer +do + local _class_0 + local _base_0 = { + transform_once = function(self, scope, node, ...) + if self.seen_nodes[node] then + return node + end + self.seen_nodes[node] = true + local transformer = self.transformers[ntype(node)] + if transformer then + return transformer(scope, node, ...) or node + else + return node + end + end, + transform = function(self, scope, node, ...) + if self.seen_nodes[node] then + return node + end + self.seen_nodes[node] = true + while true do + local transformer = self.transformers[ntype(node)] + local res + if transformer then + res = transformer(scope, node, ...) or node + else + res = node + end + if res == node then + return node + end + node = res + end + return node + end, + bind = function(self, scope) + return function(...) + return self:transform(scope, ...) + end + end, + __call = function(self, ...) + return self:transform(...) + end, + can_transform = function(self, node) + return self.transformers[ntype(node)] ~= nil + end + } + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function(self, transformers) + self.transformers = transformers + self.seen_nodes = setmetatable({ }, { + __mode = "k" + }) + end, + __base = _base_0, + __name = "Transformer" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Transformer = _class_0 +end +return { + Transformer = Transformer +} diff --git a/moonscript/transform/transformer.moon b/moonscript/transform/transformer.moon new file mode 100644 index 00000000..8a40c3c7 --- /dev/null +++ b/moonscript/transform/transformer.moon @@ -0,0 +1,42 @@ +import ntype from require "moonscript.types" + +class Transformer + new: (@transformers) => + @seen_nodes = setmetatable {}, __mode: "k" + + transform_once: (scope, node, ...) => + return node if @seen_nodes[node] + @seen_nodes[node] = true + + transformer = @transformers[ntype node] + if transformer + transformer(scope, node, ...) or node + else + node + + transform: (scope, node, ...) => + return node if @seen_nodes[node] + + @seen_nodes[node] = true + while true + transformer = @transformers[ntype node] + res = if transformer + transformer(scope, node, ...) or node + else + node + + return node if res == node + node = res + + node + + bind: (scope) => + (...) -> @transform scope, ... + + __call: (...) => @transform ... + + can_transform: (node) => + @transformers[ntype node] != nil + + +{ :Transformer } diff --git a/moonscript/transform/value.lua b/moonscript/transform/value.lua new file mode 100644 index 00000000..6b56a98e --- /dev/null +++ b/moonscript/transform/value.lua @@ -0,0 +1,261 @@ +local Transformer +Transformer = require("moonscript.transform.transformer").Transformer +local build, ntype, smart_node +do + local _obj_0 = require("moonscript.types") + build, ntype, smart_node = _obj_0.build, _obj_0.ntype, _obj_0.smart_node +end +local NameProxy +NameProxy = require("moonscript.transform.names").NameProxy +local Accumulator, default_accumulator +do + local _obj_0 = require("moonscript.transform.accumulator") + Accumulator, default_accumulator = _obj_0.Accumulator, _obj_0.default_accumulator +end +local lua_keywords +lua_keywords = require("moonscript.data").lua_keywords +local Run, transform_last_stm, implicitly_return, chain_is_stub, construct_comprehension +do + local _obj_0 = require("moonscript.transform.statements") + Run, transform_last_stm, implicitly_return, chain_is_stub, construct_comprehension = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.chain_is_stub, _obj_0.construct_comprehension +end +local insert +insert = table.insert +return Transformer({ + ["for"] = default_accumulator, + ["while"] = default_accumulator, + foreach = default_accumulator, + ["do"] = function(self, node) + return build.block_exp(node[2]) + end, + decorated = function(self, node) + return self.transform.statement(node) + end, + class = function(self, node) + return build.block_exp({ + node + }) + end, + string = function(self, node) + local delim = node[2] + local convert_part + convert_part = function(part) + if type(part) == "string" or part == nil then + return { + "string", + delim, + part or "" + } + else + return build.chain({ + base = "tostring", + { + "call", + { + part[2] + } + } + }) + end + end + if #node <= 3 then + if type(node[3]) == "string" then + return node + else + return convert_part(node[3]) + end + end + local e = { + "exp", + convert_part(node[3]) + } + for i = 4, #node do + insert(e, "..") + insert(e, convert_part(node[i])) + end + return e + end, + comprehension = function(self, node) + local a = Accumulator() + node = self.transform.statement(node, function(exp) + return a:mutate_body({ + exp + }) + end) + return a:wrap(node) + end, + tblcomprehension = function(self, node) + local _, explist, clauses = unpack(node) + local key_exp, value_exp = unpack(explist) + local accum = NameProxy("tbl") + local inner + if value_exp then + local dest = build.chain({ + base = accum, + { + "index", + key_exp + } + }) + inner = { + build.assign_one(dest, value_exp) + } + else + local key_name, val_name = NameProxy("key"), NameProxy("val") + local dest = build.chain({ + base = accum, + { + "index", + key_name + } + }) + inner = { + build.assign({ + names = { + key_name, + val_name + }, + values = { + key_exp + } + }), + build.assign_one(dest, val_name) + } + end + return build.block_exp({ + build.assign_one(accum, build.table()), + construct_comprehension(inner, clauses), + accum + }) + end, + fndef = function(self, node) + smart_node(node) + node.body = transform_last_stm(node.body, implicitly_return(self)) + node.body = { + Run(function(self) + return self:listen("varargs", function() end) + end), + unpack(node.body) + } + return node + end, + ["if"] = function(self, node) + return build.block_exp({ + node + }) + end, + unless = function(self, node) + return build.block_exp({ + node + }) + end, + with = function(self, node) + return build.block_exp({ + node + }) + end, + switch = function(self, node) + return build.block_exp({ + node + }) + end, + chain = function(self, node) + for i = 2, #node do + local part = node[i] + if ntype(part) == "dot" and lua_keywords[part[2]] then + node[i] = { + "index", + { + "string", + '"', + part[2] + } + } + end + end + if ntype(node[2]) == "string" then + node[2] = { + "parens", + node[2] + } + end + if chain_is_stub(node) then + local base_name = NameProxy("base") + local fn_name = NameProxy("fn") + local colon = table.remove(node) + local is_super = ntype(node[2]) == "ref" and node[2][2] == "super" + return build.block_exp({ + build.assign({ + names = { + base_name + }, + values = { + node + } + }), + build.assign({ + names = { + fn_name + }, + values = { + build.chain({ + base = base_name, + { + "dot", + colon[2] + } + }) + } + }), + build.fndef({ + args = { + { + "..." + } + }, + body = { + build.chain({ + base = fn_name, + { + "call", + { + is_super and "self" or base_name, + "..." + } + } + }) + } + }) + }) + end + end, + block_exp = function(self, node) + local _, body = unpack(node) + local fn = nil + local arg_list = { } + fn = smart_node(build.fndef({ + body = { + Run(function(self) + return self:listen("varargs", function() + insert(arg_list, "...") + insert(fn.args, { + "..." + }) + return self:unlisten("varargs") + end) + end), + unpack(body) + } + })) + return build.chain({ + base = { + "parens", + fn + }, + { + "call", + arg_list + } + }) + end +}) diff --git a/moonscript/transform/value.moon b/moonscript/transform/value.moon new file mode 100644 index 00000000..2b3491e6 --- /dev/null +++ b/moonscript/transform/value.moon @@ -0,0 +1,162 @@ +import Transformer from require "moonscript.transform.transformer" +import build, ntype, smart_node from require "moonscript.types" + +import NameProxy from require "moonscript.transform.names" +import Accumulator, default_accumulator from require "moonscript.transform.accumulator" +import lua_keywords from require "moonscript.data" + +import Run, transform_last_stm, implicitly_return, chain_is_stub, + construct_comprehension from require "moonscript.transform.statements" + +import insert from table + +Transformer { + for: default_accumulator + while: default_accumulator + foreach: default_accumulator + + do: (node) => + build.block_exp node[2] + + decorated: (node) => + @transform.statement node + + class: (node) => + build.block_exp { node } + + string: (node) => + delim = node[2] + + convert_part = (part) -> + if type(part) == "string" or part == nil + {"string", delim, part or ""} + else + build.chain { base: "tostring", {"call", {part[2]}} } + + -- reduced to single item + if #node <= 3 + return if type(node[3]) == "string" + node + else + convert_part node[3] + + e = {"exp", convert_part node[3]} + + for i=4, #node + insert e, ".." + insert e, convert_part node[i] + e + + comprehension: (node) => + a = Accumulator! + node = @transform.statement node, (exp) -> + a\mutate_body {exp} + a\wrap node + + tblcomprehension: (node) => + _, explist, clauses = unpack node + key_exp, value_exp = unpack explist + + accum = NameProxy "tbl" + + inner = if value_exp + dest = build.chain { base: accum, {"index", key_exp} } + { build.assign_one dest, value_exp } + else + -- If we only have single expression then + -- unpack the result into key and value + key_name, val_name = NameProxy"key", NameProxy"val" + dest = build.chain { base: accum, {"index", key_name} } + { + build.assign names: {key_name, val_name}, values: {key_exp} + build.assign_one dest, val_name + } + + build.block_exp { + build.assign_one accum, build.table! + construct_comprehension inner, clauses + accum + } + + fndef: (node) => + smart_node node + node.body = transform_last_stm node.body, implicitly_return self + node.body = { + Run => @listen "varargs", -> -- capture event + unpack node.body + } + + node + + if: (node) => + build.block_exp { node } + + unless: (node) => + build.block_exp { node } + + with: (node) => + build.block_exp { node } + + switch: (node) => + build.block_exp { node } + + -- pull out colon chain + chain: (node) => + -- escape lua keywords used in dot accessors + for i=2,#node + part = node[i] + if ntype(part) == "dot" and lua_keywords[part[2]] + node[i] = { "index", {"string", '"', part[2]} } + + if ntype(node[2]) == "string" + -- add parens if callee is raw string + node[2] = {"parens", node[2] } + + if chain_is_stub node + base_name = NameProxy "base" + fn_name = NameProxy "fn" + colon = table.remove node + + is_super = ntype(node[2]) == "ref" and node[2][2] == "super" + build.block_exp { + build.assign { + names: {base_name} + values: {node} + } + + build.assign { + names: {fn_name} + values: { + build.chain { base: base_name, {"dot", colon[2]} } + } + } + + build.fndef { + args: {{"..."}} + body: { + build.chain { + base: fn_name, {"call", {is_super and "self" or base_name, "..."}} + } + } + } + } + + block_exp: (node) => + _, body = unpack node + + fn = nil + arg_list = {} + + fn = smart_node build.fndef body: { + Run => + @listen "varargs", -> + insert arg_list, "..." + insert fn.args, {"..."} + @unlisten "varargs" + + unpack body + } + + build.chain { base: {"parens", fn}, {"call", arg_list} } +} + diff --git a/moonscript/types.lua b/moonscript/types.lua index a8bd6c70..64ea3651 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -308,6 +308,9 @@ local smart_node smart_node = function(node) return setmetatable(node, smart_node_mt[ntype(node)]) end +local NOOP = { + "noop" +} return { ntype = ntype, smart_node = smart_node, @@ -320,5 +323,6 @@ return { comprehension_has_value = comprehension_has_value, value_can_be_statement = value_can_be_statement, mtype = mtype, - terminating = terminating + terminating = terminating, + NOOP = NOOP } diff --git a/moonscript/types.moon b/moonscript/types.moon index abac3110..a2360330 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -189,9 +189,12 @@ smart_node_mt = setmetatable {}, { smart_node = (node) -> setmetatable node, smart_node_mt[ntype node] +NOOP = {"noop"} + { :ntype, :smart_node, :build, :is_value, :is_slice, :manual_return, :cascading, :value_is_singular, :comprehension_has_value, :value_can_be_statement, :mtype, :terminating + :NOOP } From 292337ee2bdc1a1b75a03e4648899b69a8c5fc9b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 5 Dec 2015 23:09:17 -0800 Subject: [PATCH 157/344] update rockpsec (unbreak lint) --- moonscript-dev-1.rockspec | 4 ++++ moonscript/cmd/lint.lua | 3 --- moonscript/cmd/lint.moon | 1 - 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index 68c82d52..94631056 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -42,9 +42,13 @@ build = { ["moonscript.parse.literals"] = "moonscript/parse/literals.lua", ["moonscript.parse.util"] = "moonscript/parse/util.lua", ["moonscript.transform"] = "moonscript/transform.lua", + ["moonscript.transform.accumulator"] = "moonscript/transform/accumulator.lua", ["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua", ["moonscript.transform.names"] = "moonscript/transform/names.lua", + ["moonscript.transform.statement"] = "moonscript/transform/statement.lua", ["moonscript.transform.statements"] = "moonscript/transform/statements.lua", + ["moonscript.transform.transformer"] = "moonscript/transform/transformer.lua", + ["moonscript.transform.value"] = "moonscript/transform/value.lua", ["moonscript.types"] = "moonscript/types.lua", ["moonscript.util"] = "moonscript/util.lua", ["moonscript.version"] = "moonscript/version.lua", diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 55f018b5..50509e57 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -65,9 +65,6 @@ do end end, lint_check_unused = function(self) - do - return - end if not (self.lint_unused_names and next(self.lint_unused_names)) then return end diff --git a/moonscript/cmd/lint.moon b/moonscript/cmd/lint.moon index 853bab59..103c25ed 100644 --- a/moonscript/cmd/lint.moon +++ b/moonscript/cmd/lint.moon @@ -105,7 +105,6 @@ class LinterBlock extends Block @parent\lint_mark_used name lint_check_unused: => - do return return unless @lint_unused_names and next @lint_unused_names names_by_position = {} From 5a0c5d1e5577ab4feb00ea5f8dea1e091b0f755e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 00:02:55 -0800 Subject: [PATCH 158/344] move comprehension to new module --- moonscript-dev-1.rockspec | 1 + moonscript/transform/comprehension.lua | 55 +++++++++++++++++++++++++ moonscript/transform/comprehension.moon | 30 ++++++++++++++ moonscript/transform/statement.lua | 6 ++- moonscript/transform/statement.moon | 5 ++- moonscript/transform/statements.lua | 52 ++--------------------- moonscript/transform/statements.moon | 30 ++------------ moonscript/transform/value.lua | 6 ++- moonscript/transform/value.moon | 5 ++- moonscript/types.lua | 5 --- moonscript/types.moon | 5 +-- 11 files changed, 109 insertions(+), 91 deletions(-) create mode 100644 moonscript/transform/comprehension.lua create mode 100644 moonscript/transform/comprehension.moon diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index 94631056..734ac3e1 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -43,6 +43,7 @@ build = { ["moonscript.parse.util"] = "moonscript/parse/util.lua", ["moonscript.transform"] = "moonscript/transform.lua", ["moonscript.transform.accumulator"] = "moonscript/transform/accumulator.lua", + ["moonscript.transform.comprehension"] = "moonscript/transform/comprehension.lua", ["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua", ["moonscript.transform.names"] = "moonscript/transform/names.lua", ["moonscript.transform.statement"] = "moonscript/transform/statement.lua", diff --git a/moonscript/transform/comprehension.lua b/moonscript/transform/comprehension.lua new file mode 100644 index 00000000..1c1fb5f8 --- /dev/null +++ b/moonscript/transform/comprehension.lua @@ -0,0 +1,55 @@ +local is_value +is_value = require("moonscript.types").is_value +local reversed +reversed = require("moonscript.util").reversed +local construct_comprehension +construct_comprehension = function(inner, clauses) + local current_stms = inner + for _, clause in reversed(clauses) do + local t = clause[1] + local _exp_0 = t + if "for" == _exp_0 then + local name, bounds + _, name, bounds = clause[1], clause[2], clause[3] + current_stms = { + "for", + name, + bounds, + current_stms + } + elseif "foreach" == _exp_0 then + local names, iter + _, names, iter = clause[1], clause[2], clause[3] + current_stms = { + "foreach", + names, + { + iter + }, + current_stms + } + elseif "when" == _exp_0 then + local cond + _, cond = clause[1], clause[2] + current_stms = { + "if", + cond, + current_stms + } + else + current_stms = error("Unknown comprehension clause: " .. t) + end + current_stms = { + current_stms + } + end + return current_stms[1] +end +local comprehension_has_value +comprehension_has_value = function(comp) + return is_value(comp[2]) +end +return { + construct_comprehension = construct_comprehension, + comprehension_has_value = comprehension_has_value +} diff --git a/moonscript/transform/comprehension.moon b/moonscript/transform/comprehension.moon new file mode 100644 index 00000000..61f9696f --- /dev/null +++ b/moonscript/transform/comprehension.moon @@ -0,0 +1,30 @@ + +import is_value from require "moonscript.types" + +-- TODO: reversed unecessary +import reversed from require "moonscript.util" +construct_comprehension = (inner, clauses) -> + current_stms = inner + for _, clause in reversed clauses + t = clause[1] + current_stms = switch t + when "for" + {_, name, bounds} = clause + {"for", name, bounds, current_stms} + when "foreach" + {_, names, iter} = clause + {"foreach", names, {iter}, current_stms} + when "when" + {_, cond} = clause + {"if", cond, current_stms} + else + error "Unknown comprehension clause: "..t + + current_stms = {current_stms} + + current_stms[1] + +comprehension_has_value = (comp) -> + is_value comp[2] + +{:construct_comprehension, :comprehension_has_value} diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index 683fb90c..77d0d3ec 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -5,10 +5,10 @@ do local _obj_0 = require("moonscript.transform.names") NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName end -local Run, transform_last_stm, implicitly_return, construct_comprehension, last_stm +local Run, transform_last_stm, implicitly_return, last_stm do local _obj_0 = require("moonscript.transform.statements") - Run, transform_last_stm, implicitly_return, construct_comprehension, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.construct_comprehension, _obj_0.last_stm + Run, transform_last_stm, implicitly_return, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.last_stm end local types = require("moonscript.types") local build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP @@ -16,6 +16,8 @@ build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP = types.bu local insert insert = table.insert local destructure = require("moonscript.transform.destructure") +local construct_comprehension +construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension local CONSTRUCTOR_NAME = "new" local with_continue_listener with_continue_listener = function(body) diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index 646403f9..6f4e1691 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -2,8 +2,8 @@ import Transformer from require "moonscript.transform.transformer" import NameProxy, LocalName from require "moonscript.transform.names" -import Run, transform_last_stm, implicitly_return, construct_comprehension, - last_stm from require "moonscript.transform.statements" +import Run, transform_last_stm, implicitly_return, last_stm + from require "moonscript.transform.statements" types = require "moonscript.types" @@ -13,6 +13,7 @@ import build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP import insert from table destructure = require "moonscript.transform.destructure" +import construct_comprehension from require "moonscript.transform.comprehension" CONSTRUCTOR_NAME = "new" diff --git a/moonscript/transform/statements.lua b/moonscript/transform/statements.lua index dc562a5d..6a49b77e 100644 --- a/moonscript/transform/statements.lua +++ b/moonscript/transform/statements.lua @@ -1,6 +1,8 @@ local types = require("moonscript.types") local ntype, mtype, is_value, NOOP ntype, mtype, is_value, NOOP = types.ntype, types.mtype, types.is_value, types.NOOP +local comprehension_has_value +comprehension_has_value = require("moonscript.transform.comprehension").comprehension_has_value local Run do local _class_0 @@ -92,7 +94,7 @@ implicitly_return = function(scope) return stm end else - if t == "comprehension" and not types.comprehension_has_value(stm) then + if t == "comprehension" and not comprehension_has_value(stm) then return stm else return { @@ -104,56 +106,10 @@ implicitly_return = function(scope) end return fn end -local reversed -reversed = require("moonscript.util").reversed -local construct_comprehension -construct_comprehension = function(inner, clauses) - local current_stms = inner - for _, clause in reversed(clauses) do - local t = clause[1] - local _exp_0 = t - if "for" == _exp_0 then - local name, bounds - _, name, bounds = clause[1], clause[2], clause[3] - current_stms = { - "for", - name, - bounds, - current_stms - } - elseif "foreach" == _exp_0 then - local names, iter - _, names, iter = clause[1], clause[2], clause[3] - current_stms = { - "foreach", - names, - { - iter - }, - current_stms - } - elseif "when" == _exp_0 then - local cond - _, cond = clause[1], clause[2] - current_stms = { - "if", - cond, - current_stms - } - else - current_stms = error("Unknown comprehension clause: " .. t) - end - current_stms = { - current_stms - } - end - return current_stms[1] -end return { Run = Run, last_stm = last_stm, transform_last_stm = transform_last_stm, chain_is_stub = chain_is_stub, - implicitly_return = implicitly_return, - construct_comprehension = construct_comprehension + implicitly_return = implicitly_return } diff --git a/moonscript/transform/statements.moon b/moonscript/transform/statements.moon index 3edb0dec..164e6b58 100644 --- a/moonscript/transform/statements.moon +++ b/moonscript/transform/statements.moon @@ -2,6 +2,8 @@ types = require "moonscript.types" import ntype, mtype, is_value, NOOP from types +import comprehension_has_value from require "moonscript.transform.comprehension" + -- A Run is a special statement node that lets a function run and mutate the -- state of the compiler class Run @@ -65,36 +67,12 @@ implicitly_return = (scope) -> else stm else - if t == "comprehension" and not types.comprehension_has_value stm + if t == "comprehension" and not comprehension_has_value stm stm else {"return", stm} fn --- TODO: reversed unecessary -import reversed from require "moonscript.util" -construct_comprehension = (inner, clauses) -> - current_stms = inner - for _, clause in reversed clauses - t = clause[1] - current_stms = switch t - when "for" - {_, name, bounds} = clause - {"for", name, bounds, current_stms} - when "foreach" - {_, names, iter} = clause - {"foreach", names, {iter}, current_stms} - when "when" - {_, cond} = clause - {"if", cond, current_stms} - else - error "Unknown comprehension clause: "..t - - current_stms = {current_stms} - - current_stms[1] - -{:Run, :last_stm, :transform_last_stm, :chain_is_stub, :implicitly_return, - :construct_comprehension} +{:Run, :last_stm, :transform_last_stm, :chain_is_stub, :implicitly_return } diff --git a/moonscript/transform/value.lua b/moonscript/transform/value.lua index 6b56a98e..159ae52a 100644 --- a/moonscript/transform/value.lua +++ b/moonscript/transform/value.lua @@ -14,11 +14,13 @@ do end local lua_keywords lua_keywords = require("moonscript.data").lua_keywords -local Run, transform_last_stm, implicitly_return, chain_is_stub, construct_comprehension +local Run, transform_last_stm, implicitly_return, chain_is_stub do local _obj_0 = require("moonscript.transform.statements") - Run, transform_last_stm, implicitly_return, chain_is_stub, construct_comprehension = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.chain_is_stub, _obj_0.construct_comprehension + Run, transform_last_stm, implicitly_return, chain_is_stub = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.chain_is_stub end +local construct_comprehension +construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension local insert insert = table.insert return Transformer({ diff --git a/moonscript/transform/value.moon b/moonscript/transform/value.moon index 2b3491e6..7b415d6f 100644 --- a/moonscript/transform/value.moon +++ b/moonscript/transform/value.moon @@ -5,8 +5,9 @@ import NameProxy from require "moonscript.transform.names" import Accumulator, default_accumulator from require "moonscript.transform.accumulator" import lua_keywords from require "moonscript.data" -import Run, transform_last_stm, implicitly_return, chain_is_stub, - construct_comprehension from require "moonscript.transform.statements" +import Run, transform_last_stm, implicitly_return, chain_is_stub from require "moonscript.transform.statements" + +import construct_comprehension from require "moonscript.transform.comprehension" import insert from table diff --git a/moonscript/types.lua b/moonscript/types.lua index 64ea3651..e8efc409 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -58,10 +58,6 @@ is_value = function(stm) local transform = require("moonscript.transform") return compile.Block:is_value(stm) or transform.Value:can_transform(stm) end -local comprehension_has_value -comprehension_has_value = function(comp) - return is_value(comp[2]) -end local value_is_singular value_is_singular = function(node) return type(node) ~= "table" or node[1] ~= "exp" or #node == 2 @@ -320,7 +316,6 @@ return { manual_return = manual_return, cascading = cascading, value_is_singular = value_is_singular, - comprehension_has_value = comprehension_has_value, value_can_be_statement = value_can_be_statement, mtype = mtype, terminating = terminating, diff --git a/moonscript/types.moon b/moonscript/types.moon index a2360330..23f4ed91 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -52,9 +52,6 @@ is_value = (stm) -> compile.Block\is_value(stm) or transform.Value\can_transform stm -comprehension_has_value = (comp) -> - is_value comp[2] - value_is_singular = (node) -> type(node) != "table" or node[1] != "exp" or #node == 2 @@ -193,7 +190,7 @@ NOOP = {"noop"} { :ntype, :smart_node, :build, :is_value, :is_slice, :manual_return, - :cascading, :value_is_singular, :comprehension_has_value, + :cascading, :value_is_singular, :value_can_be_statement, :mtype, :terminating :NOOP } From ca21c216ea94a3795dbe47019617b9222b526218 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 00:10:36 -0800 Subject: [PATCH 159/344] move class transformer to own module --- moonscript/transform/class.lua | 436 ++++++++++++++++++++++++++++ moonscript/transform/class.moon | 256 ++++++++++++++++ moonscript/transform/statement.lua | 430 +-------------------------- moonscript/transform/statement.moon | 254 +--------------- 4 files changed, 698 insertions(+), 678 deletions(-) create mode 100644 moonscript/transform/class.lua create mode 100644 moonscript/transform/class.moon diff --git a/moonscript/transform/class.lua b/moonscript/transform/class.lua new file mode 100644 index 00000000..a8ad78b8 --- /dev/null +++ b/moonscript/transform/class.lua @@ -0,0 +1,436 @@ +local NameProxy, LocalName +do + local _obj_0 = require("moonscript.transform.names") + NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName +end +local Run +Run = require("moonscript.transform.statements").Run +local CONSTRUCTOR_NAME = "new" +local insert +insert = table.insert +local build, ntype, NOOP +do + local _obj_0 = require("moonscript.types") + build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP +end +return function(self, node, ret, parent_assign) + local _, name, parent_val, body = unpack(node) + if parent_val == "" then + parent_val = nil + end + local statements = { } + local properties = { } + for _index_0 = 1, #body do + local item = body[_index_0] + local _exp_0 = item[1] + if "stm" == _exp_0 then + insert(statements, item[2]) + elseif "props" == _exp_0 then + for _index_1 = 2, #item do + local tuple = item[_index_1] + if ntype(tuple[1]) == "self" then + insert(statements, build.assign_one(unpack(tuple))) + else + insert(properties, tuple) + end + end + end + end + local constructor + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #properties do + local _continue_0 = false + repeat + local tuple = properties[_index_0] + local key = tuple[1] + local _value_0 + if key[1] == "key_literal" and key[2] == CONSTRUCTOR_NAME then + constructor = tuple[2] + _continue_0 = true + break + else + _value_0 = tuple + end + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + _continue_0 = true + until true + if not _continue_0 then + break + end + end + properties = _accum_0 + end + local parent_cls_name = NameProxy("parent") + local base_name = NameProxy("base") + local self_name = NameProxy("self") + local cls_name = NameProxy("class") + if not (constructor) then + if parent_val then + constructor = build.fndef({ + args = { + { + "..." + } + }, + arrow = "fat", + body = { + build.chain({ + base = "super", + { + "call", + { + "..." + } + } + }) + } + }) + else + constructor = build.fndef() + end + end + local real_name = name or parent_assign and parent_assign[2][1] + local _exp_0 = ntype(real_name) + if "chain" == _exp_0 then + local last = real_name[#real_name] + local _exp_1 = ntype(last) + if "dot" == _exp_1 then + real_name = { + "string", + '"', + last[2] + } + elseif "index" == _exp_1 then + real_name = last[2] + else + real_name = "nil" + end + elseif "nil" == _exp_0 then + real_name = "nil" + else + local name_t = type(real_name) + local flattened_name + if name_t == "string" then + flattened_name = real_name + elseif name_t == "table" and real_name[1] == "ref" then + flattened_name = real_name[2] + else + flattened_name = error("don't know how to extract name from " .. tostring(name_t)) + end + real_name = { + "string", + '"', + flattened_name + } + end + local cls = build.table({ + { + "__init", + constructor + }, + { + "__base", + base_name + }, + { + "__name", + real_name + }, + parent_val and { + "__parent", + parent_cls_name + } or nil + }) + local class_index + if parent_val then + local class_lookup = build["if"]({ + cond = { + "exp", + { + "ref", + "val" + }, + "==", + "nil" + }, + ["then"] = { + build.assign_one(LocalName("parent"), build.chain({ + base = "rawget", + { + "call", + { + { + "ref", + "cls" + }, + { + "string", + '"', + "__parent" + } + } + } + })), + build["if"]({ + cond = LocalName("parent"), + ["then"] = { + build.chain({ + base = LocalName("parent"), + { + "index", + "name" + } + }) + } + }) + } + }) + insert(class_lookup, { + "else", + { + "val" + } + }) + class_index = build.fndef({ + args = { + { + "cls" + }, + { + "name" + } + }, + body = { + build.assign_one(LocalName("val"), build.chain({ + base = "rawget", + { + "call", + { + base_name, + { + "ref", + "name" + } + } + } + })), + class_lookup + } + }) + else + class_index = base_name + end + local cls_mt = build.table({ + { + "__index", + class_index + }, + { + "__call", + build.fndef({ + args = { + { + "cls" + }, + { + "..." + } + }, + body = { + build.assign_one(self_name, build.chain({ + base = "setmetatable", + { + "call", + { + "{}", + base_name + } + } + })), + build.chain({ + base = "cls.__init", + { + "call", + { + self_name, + "..." + } + } + }), + self_name + } + }) + } + }) + cls = build.chain({ + base = "setmetatable", + { + "call", + { + cls, + cls_mt + } + } + }) + local value = nil + do + local out_body = { + Run(function(self) + if name then + self:put_name(name) + end + return self:set("super", function(block, chain) + local relative_parent = { + "chain", + cls_name, + { + "dot", + "__parent" + } + } + if not (chain) then + return relative_parent + end + local chain_tail = { + unpack(chain, 3) + } + local head = chain_tail[1] + if head == nil then + return relative_parent + end + local new_chain = relative_parent + local _exp_1 = head[1] + if "call" == _exp_1 then + local calling_name = block:get("current_block") + assert(calling_name, "missing calling name") + chain_tail[1] = { + "call", + { + "self", + unpack(head[2]) + } + } + if ntype(calling_name) == "key_literal" then + insert(new_chain, { + "dot", + calling_name[2] + }) + else + insert(new_chain, { + "index", + calling_name + }) + end + elseif "colon" == _exp_1 then + local call = chain_tail[2] + if call and call[1] == "call" then + chain_tail[1] = { + "dot", + head[2] + } + chain_tail[2] = { + "call", + { + "self", + unpack(call[2]) + } + } + end + end + for _index_0 = 1, #chain_tail do + local item = chain_tail[_index_0] + insert(new_chain, item) + end + return new_chain + end) + end), + { + "declare", + { + cls_name + } + }, + { + "declare_glob", + "*" + }, + parent_val and build.assign_one(parent_cls_name, parent_val) or NOOP, + build.assign_one(base_name, { + "table", + properties + }), + build.assign_one(base_name:chain("__index"), base_name), + parent_val and build.chain({ + base = "setmetatable", + { + "call", + { + base_name, + build.chain({ + base = parent_cls_name, + { + "dot", + "__base" + } + }) + } + } + }) or NOOP, + build.assign_one(cls_name, cls), + build.assign_one(base_name:chain("__class"), cls_name), + build.group((function() + if #statements > 0 then + return { + build.assign_one(LocalName("self"), cls_name), + build.group(statements) + } + end + end)()), + parent_val and build["if"]({ + cond = { + "exp", + parent_cls_name:chain("__inherited") + }, + ["then"] = { + parent_cls_name:chain("__inherited", { + "call", + { + parent_cls_name, + cls_name + } + }) + } + }) or NOOP, + build.group((function() + if name then + return { + build.assign_one(name, cls_name) + } + end + end)()), + (function() + if ret then + return ret(cls_name) + end + end)() + } + value = build.group({ + build.group((function() + if ntype(name) == "value" then + return { + build.declare({ + names = { + name + } + }) + } + end + end)()), + build["do"](out_body) + }) + end + return value +end diff --git a/moonscript/transform/class.moon b/moonscript/transform/class.moon new file mode 100644 index 00000000..007baced --- /dev/null +++ b/moonscript/transform/class.moon @@ -0,0 +1,256 @@ +import NameProxy, LocalName from require "moonscript.transform.names" +import Run from require "moonscript.transform.statements" + +CONSTRUCTOR_NAME = "new" + +import insert from table +import build, ntype, NOOP from require "moonscript.types" + +(node, ret, parent_assign) => + _, name, parent_val, body = unpack node + parent_val = nil if parent_val == "" + + -- split apart properties and statements + statements = {} + properties = {} + for item in *body + switch item[1] + when "stm" + insert statements, item[2] + when "props" + for tuple in *item[2,] + if ntype(tuple[1]) == "self" + insert statements, build.assign_one unpack tuple + else + insert properties, tuple + + -- find constructor + local constructor + properties = for tuple in *properties + key = tuple[1] + if key[1] == "key_literal" and key[2] == CONSTRUCTOR_NAME + constructor = tuple[2] + continue + else + tuple + + parent_cls_name = NameProxy "parent" + base_name = NameProxy "base" + self_name = NameProxy "self" + cls_name = NameProxy "class" + + unless constructor + constructor = if parent_val + build.fndef { + args: {{"..."}} + arrow: "fat" + body: { + build.chain { base: "super", {"call", {"..."}} } + } + } + else + build.fndef! + + real_name = name or parent_assign and parent_assign[2][1] + real_name = switch ntype real_name + when "chain" + last = real_name[#real_name] + switch ntype last + when "dot" + {"string", '"', last[2]} + when "index" + last[2] + else + "nil" + when "nil" + "nil" + else + name_t = type real_name + -- TODO: don't use string literal as ref + flattened_name = if name_t == "string" + real_name + elseif name_t == "table" and real_name[1] == "ref" + real_name[2] + else + error "don't know how to extract name from #{name_t}" + + {"string", '"', flattened_name} + + cls = build.table { + {"__init", constructor} + {"__base", base_name} + {"__name", real_name} -- "quote the string" + parent_val and {"__parent", parent_cls_name} or nil + } + + -- looking up a name in the class object + class_index = if parent_val + class_lookup = build["if"] { + cond: { "exp", {"ref", "val"}, "==", "nil" } + then: { + build.assign_one LocalName"parent", build.chain { + base: "rawget" + { + "call", { + {"ref", "cls"} + {"string", '"', "__parent"} + } + } + } + + build.if { + cond: LocalName "parent" + then: { + build.chain { + base: LocalName "parent" + {"index", "name"} + } + } + } + } + } + insert class_lookup, {"else", {"val"}} + + build.fndef { + args: {{"cls"}, {"name"}} + body: { + build.assign_one LocalName"val", build.chain { + base: "rawget", {"call", {base_name, {"ref", "name"}}} + } + class_lookup + } + } + else + base_name + + cls_mt = build.table { + {"__index", class_index} + {"__call", build.fndef { + args: {{"cls"}, {"..."}} + body: { + build.assign_one self_name, build.chain { + base: "setmetatable" + {"call", {"{}", base_name}} + } + build.chain { + base: "cls.__init" + {"call", {self_name, "..."}} + } + self_name + } + }} + } + + cls = build.chain { + base: "setmetatable" + {"call", {cls, cls_mt}} + } + + value = nil + with build + out_body = { + Run => + -- make sure we don't assign the class to a local inside the do + @put_name name if name + + @set "super", (block, chain) -> + relative_parent = { + "chain", + cls_name + {"dot", "__parent"} + } + + return relative_parent unless chain + + chain_tail = { unpack chain, 3 } + head = chain_tail[1] + + if head == nil + return relative_parent + + new_chain = relative_parent + + switch head[1] + -- calling super, inject calling name and self into chain + when "call" + calling_name = block\get"current_block" + assert calling_name, "missing calling name" + chain_tail[1] = {"call", {"self", unpack head[2]}} + + if ntype(calling_name) == "key_literal" + insert new_chain, {"dot", calling_name[2]} + else + insert new_chain, {"index", calling_name} + + -- colon call on super, replace class with self as first arg + when "colon" + call = chain_tail[2] + -- calling chain tail + if call and call[1] == "call" + chain_tail[1] = { + "dot" + head[2] + } + + chain_tail[2] = { + "call" + { + "self" + unpack call[2] + } + } + + insert new_chain, item for item in *chain_tail + new_chain + + {"declare", { cls_name }} + {"declare_glob", "*"} + + parent_val and .assign_one(parent_cls_name, parent_val) or NOOP + + .assign_one base_name, {"table", properties} + .assign_one base_name\chain"__index", base_name + + parent_val and .chain({ + base: "setmetatable" + {"call", { + base_name, + .chain { base: parent_cls_name, {"dot", "__base"}} + }} + }) or NOOP + + .assign_one cls_name, cls + .assign_one base_name\chain"__class", cls_name + + .group if #statements > 0 then { + .assign_one LocalName"self", cls_name + .group statements + } + + -- run the inherited callback + parent_val and .if({ + cond: {"exp", parent_cls_name\chain "__inherited" } + then: { + parent_cls_name\chain "__inherited", {"call", { + parent_cls_name, cls_name + }} + } + }) or NOOP + + .group if name then { + .assign_one name, cls_name + } + + if ret + ret cls_name + } + + value = .group { + .group if ntype(name) == "value" then { + .declare names: {name} + } + + .do out_body + } + + value diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index 77d0d3ec..f37a9d6f 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -1,10 +1,7 @@ local Transformer Transformer = require("moonscript.transform.transformer").Transformer -local NameProxy, LocalName -do - local _obj_0 = require("moonscript.transform.names") - NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName -end +local NameProxy +NameProxy = require("moonscript.transform.names").NameProxy local Run, transform_last_stm, implicitly_return, last_stm do local _obj_0 = require("moonscript.transform.statements") @@ -18,7 +15,6 @@ insert = table.insert local destructure = require("moonscript.transform.destructure") local construct_comprehension construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension -local CONSTRUCTOR_NAME = "new" local with_continue_listener with_continue_listener = function(body) local continue_name = nil @@ -739,425 +735,5 @@ return Transformer({ if_stm }) end, - class = function(self, node, ret, parent_assign) - local _, name, parent_val, body = unpack(node) - if parent_val == "" then - parent_val = nil - end - local statements = { } - local properties = { } - for _index_0 = 1, #body do - local item = body[_index_0] - local _exp_0 = item[1] - if "stm" == _exp_0 then - insert(statements, item[2]) - elseif "props" == _exp_0 then - for _index_1 = 2, #item do - local tuple = item[_index_1] - if ntype(tuple[1]) == "self" then - insert(statements, build.assign_one(unpack(tuple))) - else - insert(properties, tuple) - end - end - end - end - local constructor - do - local _accum_0 = { } - local _len_0 = 1 - for _index_0 = 1, #properties do - local _continue_0 = false - repeat - local tuple = properties[_index_0] - local key = tuple[1] - local _value_0 - if key[1] == "key_literal" and key[2] == CONSTRUCTOR_NAME then - constructor = tuple[2] - _continue_0 = true - break - else - _value_0 = tuple - end - _accum_0[_len_0] = _value_0 - _len_0 = _len_0 + 1 - _continue_0 = true - until true - if not _continue_0 then - break - end - end - properties = _accum_0 - end - local parent_cls_name = NameProxy("parent") - local base_name = NameProxy("base") - local self_name = NameProxy("self") - local cls_name = NameProxy("class") - if not (constructor) then - if parent_val then - constructor = build.fndef({ - args = { - { - "..." - } - }, - arrow = "fat", - body = { - build.chain({ - base = "super", - { - "call", - { - "..." - } - } - }) - } - }) - else - constructor = build.fndef() - end - end - local real_name = name or parent_assign and parent_assign[2][1] - local _exp_0 = ntype(real_name) - if "chain" == _exp_0 then - local last = real_name[#real_name] - local _exp_1 = ntype(last) - if "dot" == _exp_1 then - real_name = { - "string", - '"', - last[2] - } - elseif "index" == _exp_1 then - real_name = last[2] - else - real_name = "nil" - end - elseif "nil" == _exp_0 then - real_name = "nil" - else - local name_t = type(real_name) - local flattened_name - if name_t == "string" then - flattened_name = real_name - elseif name_t == "table" and real_name[1] == "ref" then - flattened_name = real_name[2] - else - flattened_name = error("don't know how to extract name from " .. tostring(name_t)) - end - real_name = { - "string", - '"', - flattened_name - } - end - local cls = build.table({ - { - "__init", - constructor - }, - { - "__base", - base_name - }, - { - "__name", - real_name - }, - parent_val and { - "__parent", - parent_cls_name - } or nil - }) - local class_index - if parent_val then - local class_lookup = build["if"]({ - cond = { - "exp", - { - "ref", - "val" - }, - "==", - "nil" - }, - ["then"] = { - build.assign_one(LocalName("parent"), build.chain({ - base = "rawget", - { - "call", - { - { - "ref", - "cls" - }, - { - "string", - '"', - "__parent" - } - } - } - })), - build["if"]({ - cond = LocalName("parent"), - ["then"] = { - build.chain({ - base = LocalName("parent"), - { - "index", - "name" - } - }) - } - }) - } - }) - insert(class_lookup, { - "else", - { - "val" - } - }) - class_index = build.fndef({ - args = { - { - "cls" - }, - { - "name" - } - }, - body = { - build.assign_one(LocalName("val"), build.chain({ - base = "rawget", - { - "call", - { - base_name, - { - "ref", - "name" - } - } - } - })), - class_lookup - } - }) - else - class_index = base_name - end - local cls_mt = build.table({ - { - "__index", - class_index - }, - { - "__call", - build.fndef({ - args = { - { - "cls" - }, - { - "..." - } - }, - body = { - build.assign_one(self_name, build.chain({ - base = "setmetatable", - { - "call", - { - "{}", - base_name - } - } - })), - build.chain({ - base = "cls.__init", - { - "call", - { - self_name, - "..." - } - } - }), - self_name - } - }) - } - }) - cls = build.chain({ - base = "setmetatable", - { - "call", - { - cls, - cls_mt - } - } - }) - local value = nil - do - local out_body = { - Run(function(self) - if name then - self:put_name(name) - end - return self:set("super", function(block, chain) - local relative_parent = { - "chain", - cls_name, - { - "dot", - "__parent" - } - } - if not (chain) then - return relative_parent - end - local chain_tail = { - unpack(chain, 3) - } - local head = chain_tail[1] - if head == nil then - return relative_parent - end - local new_chain = relative_parent - local _exp_1 = head[1] - if "call" == _exp_1 then - local calling_name = block:get("current_block") - assert(calling_name, "missing calling name") - chain_tail[1] = { - "call", - { - "self", - unpack(head[2]) - } - } - if ntype(calling_name) == "key_literal" then - insert(new_chain, { - "dot", - calling_name[2] - }) - else - insert(new_chain, { - "index", - calling_name - }) - end - elseif "colon" == _exp_1 then - local call = chain_tail[2] - if call and call[1] == "call" then - chain_tail[1] = { - "dot", - head[2] - } - chain_tail[2] = { - "call", - { - "self", - unpack(call[2]) - } - } - end - end - for _index_0 = 1, #chain_tail do - local item = chain_tail[_index_0] - insert(new_chain, item) - end - return new_chain - end) - end), - { - "declare", - { - cls_name - } - }, - { - "declare_glob", - "*" - }, - parent_val and build.assign_one(parent_cls_name, parent_val) or NOOP, - build.assign_one(base_name, { - "table", - properties - }), - build.assign_one(base_name:chain("__index"), base_name), - parent_val and build.chain({ - base = "setmetatable", - { - "call", - { - base_name, - build.chain({ - base = parent_cls_name, - { - "dot", - "__base" - } - }) - } - } - }) or NOOP, - build.assign_one(cls_name, cls), - build.assign_one(base_name:chain("__class"), cls_name), - build.group((function() - if #statements > 0 then - return { - build.assign_one(LocalName("self"), cls_name), - build.group(statements) - } - end - end)()), - parent_val and build["if"]({ - cond = { - "exp", - parent_cls_name:chain("__inherited") - }, - ["then"] = { - parent_cls_name:chain("__inherited", { - "call", - { - parent_cls_name, - cls_name - } - }) - } - }) or NOOP, - build.group((function() - if name then - return { - build.assign_one(name, cls_name) - } - end - end)()), - (function() - if ret then - return ret(cls_name) - end - end)() - } - value = build.group({ - build.group((function() - if ntype(name) == "value" then - return { - build.declare({ - names = { - name - } - }) - } - end - end)()), - build["do"](out_body) - }) - end - return value - end + class = require("moonscript.transform.class") }) diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index 6f4e1691..7933c5fe 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -1,6 +1,6 @@ import Transformer from require "moonscript.transform.transformer" -import NameProxy, LocalName from require "moonscript.transform.names" +import NameProxy from require "moonscript.transform.names" import Run, transform_last_stm, implicitly_return, last_stm from require "moonscript.transform.statements" @@ -15,8 +15,6 @@ import insert from table destructure = require "moonscript.transform.destructure" import construct_comprehension from require "moonscript.transform.comprehension" -CONSTRUCTOR_NAME = "new" - with_continue_listener = (body) -> continue_name = nil @@ -470,252 +468,6 @@ Transformer { if_stm } - class: (node, ret, parent_assign) => - _, name, parent_val, body = unpack node - parent_val = nil if parent_val == "" - - -- split apart properties and statements - statements = {} - properties = {} - for item in *body - switch item[1] - when "stm" - insert statements, item[2] - when "props" - for tuple in *item[2,] - if ntype(tuple[1]) == "self" - insert statements, build.assign_one unpack tuple - else - insert properties, tuple - - -- find constructor - local constructor - properties = for tuple in *properties - key = tuple[1] - if key[1] == "key_literal" and key[2] == CONSTRUCTOR_NAME - constructor = tuple[2] - continue - else - tuple - - parent_cls_name = NameProxy "parent" - base_name = NameProxy "base" - self_name = NameProxy "self" - cls_name = NameProxy "class" - - unless constructor - constructor = if parent_val - build.fndef { - args: {{"..."}} - arrow: "fat" - body: { - build.chain { base: "super", {"call", {"..."}} } - } - } - else - build.fndef! - - real_name = name or parent_assign and parent_assign[2][1] - real_name = switch ntype real_name - when "chain" - last = real_name[#real_name] - switch ntype last - when "dot" - {"string", '"', last[2]} - when "index" - last[2] - else - "nil" - when "nil" - "nil" - else - name_t = type real_name - -- TODO: don't use string literal as ref - flattened_name = if name_t == "string" - real_name - elseif name_t == "table" and real_name[1] == "ref" - real_name[2] - else - error "don't know how to extract name from #{name_t}" - - {"string", '"', flattened_name} - - cls = build.table { - {"__init", constructor} - {"__base", base_name} - {"__name", real_name} -- "quote the string" - parent_val and {"__parent", parent_cls_name} or nil - } - - -- looking up a name in the class object - class_index = if parent_val - class_lookup = build["if"] { - cond: { "exp", {"ref", "val"}, "==", "nil" } - then: { - build.assign_one LocalName"parent", build.chain { - base: "rawget" - { - "call", { - {"ref", "cls"} - {"string", '"', "__parent"} - } - } - } - - build.if { - cond: LocalName "parent" - then: { - build.chain { - base: LocalName "parent" - {"index", "name"} - } - } - } - } - } - insert class_lookup, {"else", {"val"}} - - build.fndef { - args: {{"cls"}, {"name"}} - body: { - build.assign_one LocalName"val", build.chain { - base: "rawget", {"call", {base_name, {"ref", "name"}}} - } - class_lookup - } - } - else - base_name - - cls_mt = build.table { - {"__index", class_index} - {"__call", build.fndef { - args: {{"cls"}, {"..."}} - body: { - build.assign_one self_name, build.chain { - base: "setmetatable" - {"call", {"{}", base_name}} - } - build.chain { - base: "cls.__init" - {"call", {self_name, "..."}} - } - self_name - } - }} - } - - cls = build.chain { - base: "setmetatable" - {"call", {cls, cls_mt}} - } - - value = nil - with build - out_body = { - Run => - -- make sure we don't assign the class to a local inside the do - @put_name name if name - - @set "super", (block, chain) -> - relative_parent = { - "chain", - cls_name - {"dot", "__parent"} - } - - return relative_parent unless chain - - chain_tail = { unpack chain, 3 } - head = chain_tail[1] - - if head == nil - return relative_parent - - new_chain = relative_parent - - switch head[1] - -- calling super, inject calling name and self into chain - when "call" - calling_name = block\get"current_block" - assert calling_name, "missing calling name" - chain_tail[1] = {"call", {"self", unpack head[2]}} - - if ntype(calling_name) == "key_literal" - insert new_chain, {"dot", calling_name[2]} - else - insert new_chain, {"index", calling_name} - - -- colon call on super, replace class with self as first arg - when "colon" - call = chain_tail[2] - -- calling chain tail - if call and call[1] == "call" - chain_tail[1] = { - "dot" - head[2] - } - - chain_tail[2] = { - "call" - { - "self" - unpack call[2] - } - } - - insert new_chain, item for item in *chain_tail - new_chain - - {"declare", { cls_name }} - {"declare_glob", "*"} - - parent_val and .assign_one(parent_cls_name, parent_val) or NOOP - - .assign_one base_name, {"table", properties} - .assign_one base_name\chain"__index", base_name - - parent_val and .chain({ - base: "setmetatable" - {"call", { - base_name, - .chain { base: parent_cls_name, {"dot", "__base"}} - }} - }) or NOOP - - .assign_one cls_name, cls - .assign_one base_name\chain"__class", cls_name - - .group if #statements > 0 then { - .assign_one LocalName"self", cls_name - .group statements - } - - -- run the inherited callback - parent_val and .if({ - cond: {"exp", parent_cls_name\chain "__inherited" } - then: { - parent_cls_name\chain "__inherited", {"call", { - parent_cls_name, cls_name - }} - } - }) or NOOP - - .group if name then { - .assign_one name, cls_name - } - - if ret - ret cls_name - } - - value = .group { - .group if ntype(name) == "value" then { - .declare names: {name} - } - - .do out_body - } - - value + class: require "moonscript.transform.class" + } From 07c639ad3db985a67c3bf62c04eb39386f8be65a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 00:14:47 -0800 Subject: [PATCH 160/344] remove reversed --- moonscript/compile/statement.lua | 3 --- moonscript/compile/statement.moon | 3 --- moonscript/transform/comprehension.lua | 11 +++++------ moonscript/transform/comprehension.moon | 6 +++--- moonscript/util.lua | 9 --------- moonscript/util.moon | 9 ++------- 6 files changed, 10 insertions(+), 31 deletions(-) diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index 2026a88f..eebb24a9 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -1,6 +1,3 @@ -local util = require("moonscript.util") -local reversed, unpack -reversed, unpack = util.reversed, util.unpack local ntype ntype = require("moonscript.types").ntype local concat, insert diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index e5c57403..45e9a489 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -1,7 +1,4 @@ -util = require "moonscript.util" - -import reversed, unpack from util import ntype from require "moonscript.types" import concat, insert from table diff --git a/moonscript/transform/comprehension.lua b/moonscript/transform/comprehension.lua index 1c1fb5f8..da33892d 100644 --- a/moonscript/transform/comprehension.lua +++ b/moonscript/transform/comprehension.lua @@ -1,15 +1,14 @@ local is_value is_value = require("moonscript.types").is_value -local reversed -reversed = require("moonscript.util").reversed local construct_comprehension construct_comprehension = function(inner, clauses) local current_stms = inner - for _, clause in reversed(clauses) do + for i = #clauses, 1, -1 do + local clause = clauses[i] local t = clause[1] local _exp_0 = t if "for" == _exp_0 then - local name, bounds + local _, name, bounds _, name, bounds = clause[1], clause[2], clause[3] current_stms = { "for", @@ -18,7 +17,7 @@ construct_comprehension = function(inner, clauses) current_stms } elseif "foreach" == _exp_0 then - local names, iter + local _, names, iter _, names, iter = clause[1], clause[2], clause[3] current_stms = { "foreach", @@ -29,7 +28,7 @@ construct_comprehension = function(inner, clauses) current_stms } elseif "when" == _exp_0 then - local cond + local _, cond _, cond = clause[1], clause[2] current_stms = { "if", diff --git a/moonscript/transform/comprehension.moon b/moonscript/transform/comprehension.moon index 61f9696f..a181a891 100644 --- a/moonscript/transform/comprehension.moon +++ b/moonscript/transform/comprehension.moon @@ -1,12 +1,12 @@ import is_value from require "moonscript.types" --- TODO: reversed unecessary -import reversed from require "moonscript.util" construct_comprehension = (inner, clauses) -> current_stms = inner - for _, clause in reversed clauses + for i=#clauses,1,-1 + clause = clauses[i] t = clause[1] + current_stms = switch t when "for" {_, name, bounds} = clause diff --git a/moonscript/util.lua b/moonscript/util.lua index cc41ebc3..bde0d576 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -60,14 +60,6 @@ get_closest_line = function(str, line_num) return line, line_num end end -local reversed -reversed = function(seq) - return coroutine.wrap(function() - for i = #seq, 1, -1 do - coroutine.yield(i, seq[i]) - end - end) -end local split split = function(str, delim) if str == "" then @@ -209,7 +201,6 @@ return { pos_to_line = pos_to_line, get_closest_line = get_closest_line, get_line = get_line, - reversed = reversed, trim = trim, split = split, dump = dump, diff --git a/moonscript/util.moon b/moonscript/util.moon index 8df3b538..ea7c8a5b 100644 --- a/moonscript/util.moon +++ b/moonscript/util.moon @@ -48,11 +48,6 @@ get_closest_line = (str, line_num) -> else line, line_num -reversed = (seq) -> - coroutine.wrap -> - for i=#seq,1,-1 - coroutine.yield i, seq[i] - split = (str, delim) -> return {} if str == "" str ..= delim @@ -136,7 +131,7 @@ safe_module = (name, tbl) -> } { - :moon, :pos_to_line, :get_closest_line, :get_line, :reversed, :trim, :split, - :dump, :debug_posmap, :getfenv, :setfenv, :get_options, :unpack, :safe_module + :moon, :pos_to_line, :get_closest_line, :get_line, :trim, :split, :dump, + :debug_posmap, :getfenv, :setfenv, :get_options, :unpack, :safe_module } From 76454f61ae005c7dceca3c04ef77b279da04ecab Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 00:16:49 -0800 Subject: [PATCH 161/344] update rockspec --- moonscript-dev-1.rockspec | 1 + 1 file changed, 1 insertion(+) diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index 734ac3e1..b3ff2432 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -43,6 +43,7 @@ build = { ["moonscript.parse.util"] = "moonscript/parse/util.lua", ["moonscript.transform"] = "moonscript/transform.lua", ["moonscript.transform.accumulator"] = "moonscript/transform/accumulator.lua", + ["moonscript.transform.class"] = "moonscript/transform/class.lua", ["moonscript.transform.comprehension"] = "moonscript/transform/comprehension.lua", ["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua", ["moonscript.transform.names"] = "moonscript/transform/names.lua", From 4b26091c9f476e9e12270501701c7139a4008e82 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 02:05:47 -0800 Subject: [PATCH 162/344] change how super method name is set --- moonscript/compile/statement.moon | 2 +- moonscript/compile/value.lua | 12 ++++++++++-- moonscript/compile/value.moon | 8 ++++++-- moonscript/transform/class.lua | 29 ++++++++++++++++++++++++++--- moonscript/transform/class.moon | 20 +++++++++++++++++--- 5 files changed, 60 insertions(+), 11 deletions(-) diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index 45e9a489..bb80e969 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -27,7 +27,7 @@ import concat, insert from table _, names, values = unpack node undeclared = @declare names - declare = "local "..concat(undeclared, ", ") + declare = "local " .. concat(undeclared, ", ") has_fndef = false i = 1 diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 82cc7304..af7320ea 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -17,6 +17,16 @@ local string_chars = { ["\n"] = "\\n" } return { + scoped = function(self, node) + local _, before, value, after + _, before, value, after = node[1], node[2], node[3], node[4] + _ = before and before:call(self) + do + local _with_0 = self:value(value) + _ = after and after:call(self) + return _with_0 + end + end, exp = function(self, node) local _comp _comp = function(i, value) @@ -248,9 +258,7 @@ return { else assign = self:line("[", _with_0:value(key), "]") end - _with_0:set("current_block", key) local out = self:line(assign, " = ", _with_0:value(value)) - _with_0:set("current_block", nil) return out else return self:line(_with_0:value(tuple[1])) diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index fb5c2253..193a4b5e 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -15,6 +15,12 @@ string_chars = { } { + scoped: (node) => + {_, before, value, after} = node + before and before\call @ + with @value value + after and after\call @ + -- list of values separated by binary operators exp: (node) => _comp = (i, value) -> @@ -146,9 +152,7 @@ string_chars = { else @line "[", \value(key), "]" - \set "current_block", key out = @line assign, " = ", \value(value) - \set "current_block", nil out else @line \value tuple[1] diff --git a/moonscript/transform/class.lua b/moonscript/transform/class.lua index a8ad78b8..1079dcdc 100644 --- a/moonscript/transform/class.lua +++ b/moonscript/transform/class.lua @@ -13,6 +13,21 @@ do local _obj_0 = require("moonscript.types") build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP end +local super_scope +super_scope = function(value, key) + local prev_method + return { + "scoped", + Run(function(self) + prev_method = self:get("current_method") + return self:set("current_method", key) + end), + value, + Run(function(self) + return self:set("current_method", prev_method) + end) + } +end return function(self, node, ret, parent_assign) local _, name, parent_val, body = unpack(node) if parent_val == "" then @@ -51,7 +66,12 @@ return function(self, node, ret, parent_assign) _continue_0 = true break else - _value_0 = tuple + local val + key, val = tuple[1], tuple[2] + _value_0 = { + key, + super_scope(val, key) + } end _accum_0[_len_0] = _value_0 _len_0 = _len_0 + 1 @@ -129,7 +149,10 @@ return function(self, node, ret, parent_assign) local cls = build.table({ { "__init", - constructor + super_scope(constructor, { + "key_literal", + "__init" + }) }, { "__base", @@ -304,7 +327,7 @@ return function(self, node, ret, parent_assign) local new_chain = relative_parent local _exp_1 = head[1] if "call" == _exp_1 then - local calling_name = block:get("current_block") + local calling_name = block:get("current_method") assert(calling_name, "missing calling name") chain_tail[1] = { "call", diff --git a/moonscript/transform/class.moon b/moonscript/transform/class.moon index 007baced..f75e4e5e 100644 --- a/moonscript/transform/class.moon +++ b/moonscript/transform/class.moon @@ -6,6 +6,19 @@ CONSTRUCTOR_NAME = "new" import insert from table import build, ntype, NOOP from require "moonscript.types" +super_scope = (value, key) -> + local prev_method + + { + "scoped", + Run => + prev_method = @get "current_method" + @set "current_method", key + value + Run => + @set "current_method", prev_method + } + (node, ret, parent_assign) => _, name, parent_val, body = unpack node parent_val = nil if parent_val == "" @@ -32,7 +45,8 @@ import build, ntype, NOOP from require "moonscript.types" constructor = tuple[2] continue else - tuple + {key, val} = tuple + {key, super_scope val, key} parent_cls_name = NameProxy "parent" base_name = NameProxy "base" @@ -77,7 +91,7 @@ import build, ntype, NOOP from require "moonscript.types" {"string", '"', flattened_name} cls = build.table { - {"__init", constructor} + {"__init", super_scope constructor, {"key_literal", "__init"}} {"__base", base_name} {"__name", real_name} -- "quote the string" parent_val and {"__parent", parent_cls_name} or nil @@ -173,7 +187,7 @@ import build, ntype, NOOP from require "moonscript.types" switch head[1] -- calling super, inject calling name and self into chain when "call" - calling_name = block\get"current_block" + calling_name = block\get "current_method" assert calling_name, "missing calling name" chain_tail[1] = {"call", {"self", unpack head[2]}} From 8f647cc743a90ceac8124e44aa02551159901bf8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 02:07:41 -0800 Subject: [PATCH 163/344] spec for testing super scope when inside table/function --- spec/inputs/class.moon | 10 +++++++++ spec/outputs/class.lua | 48 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/spec/inputs/class.moon b/spec/inputs/class.moon index ebcca20e..87eb866c 100644 --- a/spec/inputs/class.moon +++ b/spec/inputs/class.moon @@ -176,4 +176,14 @@ class Something class X new: hi + +-- + +class Cool extends Thing + dang: => + { + hello: -> super! + world: -> super.one + } + nil diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index 30180620..6f361f82 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -761,4 +761,52 @@ do _base_0.__class = _class_0 X = _class_0 end +do + local _class_0 + local _parent_0 = Thing + local _base_0 = { + dang = function(self) + return { + hello = function() + return _class_0.__parent.dang(self) + end, + world = function() + return _class_0.__parent.one + end + } + end + } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, ...) + return _class_0.__parent.__init(self, ...) + end, + __base = _base_0, + __name = "Cool", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + Cool = _class_0 +end return nil \ No newline at end of file From 3be7be0166a31e24618efc25bb7c983111319c91 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 10:42:21 -0800 Subject: [PATCH 164/344] extract super transform out --- moonscript/transform/class.lua | 152 +++++++++++++++++--------------- moonscript/transform/class.moon | 119 +++++++++++++------------ 2 files changed, 141 insertions(+), 130 deletions(-) diff --git a/moonscript/transform/class.lua b/moonscript/transform/class.lua index 1079dcdc..a7a6273f 100644 --- a/moonscript/transform/class.lua +++ b/moonscript/transform/class.lua @@ -13,14 +13,80 @@ do local _obj_0 = require("moonscript.types") build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP end +local transform_super +transform_super = function(cls_name, block, chain) + local relative_parent = { + "chain", + cls_name, + { + "dot", + "__parent" + } + } + if not (chain) then + return relative_parent + end + local chain_tail = { + unpack(chain, 3) + } + local head = chain_tail[1] + if head == nil then + return relative_parent + end + local new_chain = relative_parent + local _exp_0 = head[1] + if "call" == _exp_0 then + local calling_name = block:get("current_method") + assert(calling_name, "missing calling name") + chain_tail[1] = { + "call", + { + "self", + unpack(head[2]) + } + } + if ntype(calling_name) == "key_literal" then + insert(new_chain, { + "dot", + calling_name[2] + }) + else + insert(new_chain, { + "index", + calling_name + }) + end + elseif "colon" == _exp_0 then + local call = chain_tail[2] + if call and call[1] == "call" then + chain_tail[1] = { + "dot", + head[2] + } + chain_tail[2] = { + "call", + { + "self", + unpack(call[2]) + } + } + end + end + for _index_0 = 1, #chain_tail do + local item = chain_tail[_index_0] + insert(new_chain, item) + end + return new_chain +end local super_scope -super_scope = function(value, key) +super_scope = function(value, t, key) local prev_method return { "scoped", Run(function(self) prev_method = self:get("current_method") - return self:set("current_method", key) + self:set("current_method", key) + return self:set("super", t) end), value, Run(function(self) @@ -33,6 +99,14 @@ return function(self, node, ret, parent_assign) if parent_val == "" then parent_val = nil end + local parent_cls_name = NameProxy("parent") + local base_name = NameProxy("base") + local self_name = NameProxy("self") + local cls_name = NameProxy("class") + local cls_super + cls_super = function(...) + return transform_super(cls_name, ...) + end local statements = { } local properties = { } for _index_0 = 1, #body do @@ -70,7 +144,7 @@ return function(self, node, ret, parent_assign) key, val = tuple[1], tuple[2] _value_0 = { key, - super_scope(val, key) + super_scope(val, cls_super, key) } end _accum_0[_len_0] = _value_0 @@ -83,10 +157,6 @@ return function(self, node, ret, parent_assign) end properties = _accum_0 end - local parent_cls_name = NameProxy("parent") - local base_name = NameProxy("base") - local self_name = NameProxy("self") - local cls_name = NameProxy("class") if not (constructor) then if parent_val then constructor = build.fndef({ @@ -149,7 +219,7 @@ return function(self, node, ret, parent_assign) local cls = build.table({ { "__init", - super_scope(constructor, { + super_scope(constructor, cls_super, { "key_literal", "__init" }) @@ -303,72 +373,8 @@ return function(self, node, ret, parent_assign) local out_body = { Run(function(self) if name then - self:put_name(name) + return self:put_name(name) end - return self:set("super", function(block, chain) - local relative_parent = { - "chain", - cls_name, - { - "dot", - "__parent" - } - } - if not (chain) then - return relative_parent - end - local chain_tail = { - unpack(chain, 3) - } - local head = chain_tail[1] - if head == nil then - return relative_parent - end - local new_chain = relative_parent - local _exp_1 = head[1] - if "call" == _exp_1 then - local calling_name = block:get("current_method") - assert(calling_name, "missing calling name") - chain_tail[1] = { - "call", - { - "self", - unpack(head[2]) - } - } - if ntype(calling_name) == "key_literal" then - insert(new_chain, { - "dot", - calling_name[2] - }) - else - insert(new_chain, { - "index", - calling_name - }) - end - elseif "colon" == _exp_1 then - local call = chain_tail[2] - if call and call[1] == "call" then - chain_tail[1] = { - "dot", - head[2] - } - chain_tail[2] = { - "call", - { - "self", - unpack(call[2]) - } - } - end - end - for _index_0 = 1, #chain_tail do - local item = chain_tail[_index_0] - insert(new_chain, item) - end - return new_chain - end) end), { "declare", diff --git a/moonscript/transform/class.moon b/moonscript/transform/class.moon index f75e4e5e..6dd7099b 100644 --- a/moonscript/transform/class.moon +++ b/moonscript/transform/class.moon @@ -6,7 +6,58 @@ CONSTRUCTOR_NAME = "new" import insert from table import build, ntype, NOOP from require "moonscript.types" -super_scope = (value, key) -> +transform_super = (cls_name, block, chain) -> + relative_parent = { + "chain", + cls_name + {"dot", "__parent"} + } + + return relative_parent unless chain + + chain_tail = { unpack chain, 3 } + head = chain_tail[1] + + if head == nil + return relative_parent + + new_chain = relative_parent + + switch head[1] + -- calling super, inject calling name and self into chain + when "call" + calling_name = block\get "current_method" + assert calling_name, "missing calling name" + chain_tail[1] = {"call", {"self", unpack head[2]}} + + if ntype(calling_name) == "key_literal" + insert new_chain, {"dot", calling_name[2]} + else + insert new_chain, {"index", calling_name} + + -- colon call on super, replace class with self as first arg + when "colon" + call = chain_tail[2] + -- calling chain tail + if call and call[1] == "call" + chain_tail[1] = { + "dot" + head[2] + } + + chain_tail[2] = { + "call" + { + "self" + unpack call[2] + } + } + + insert new_chain, item for item in *chain_tail + new_chain + + +super_scope = (value, t, key) -> local prev_method { @@ -14,6 +65,7 @@ super_scope = (value, key) -> Run => prev_method = @get "current_method" @set "current_method", key + @set "super", t value Run => @set "current_method", prev_method @@ -23,6 +75,13 @@ super_scope = (value, key) -> _, name, parent_val, body = unpack node parent_val = nil if parent_val == "" + parent_cls_name = NameProxy "parent" + base_name = NameProxy "base" + self_name = NameProxy "self" + cls_name = NameProxy "class" + + cls_super = (...) -> transform_super cls_name, ... + -- split apart properties and statements statements = {} properties = {} @@ -46,12 +105,8 @@ super_scope = (value, key) -> continue else {key, val} = tuple - {key, super_scope val, key} + {key, super_scope val, cls_super, key} - parent_cls_name = NameProxy "parent" - base_name = NameProxy "base" - self_name = NameProxy "self" - cls_name = NameProxy "class" unless constructor constructor = if parent_val @@ -91,7 +146,7 @@ super_scope = (value, key) -> {"string", '"', flattened_name} cls = build.table { - {"__init", super_scope constructor, {"key_literal", "__init"}} + {"__init", super_scope constructor, cls_super, {"key_literal", "__init"}} {"__base", base_name} {"__name", real_name} -- "quote the string" parent_val and {"__parent", parent_cls_name} or nil @@ -167,56 +222,6 @@ super_scope = (value, key) -> -- make sure we don't assign the class to a local inside the do @put_name name if name - @set "super", (block, chain) -> - relative_parent = { - "chain", - cls_name - {"dot", "__parent"} - } - - return relative_parent unless chain - - chain_tail = { unpack chain, 3 } - head = chain_tail[1] - - if head == nil - return relative_parent - - new_chain = relative_parent - - switch head[1] - -- calling super, inject calling name and self into chain - when "call" - calling_name = block\get "current_method" - assert calling_name, "missing calling name" - chain_tail[1] = {"call", {"self", unpack head[2]}} - - if ntype(calling_name) == "key_literal" - insert new_chain, {"dot", calling_name[2]} - else - insert new_chain, {"index", calling_name} - - -- colon call on super, replace class with self as first arg - when "colon" - call = chain_tail[2] - -- calling chain tail - if call and call[1] == "call" - chain_tail[1] = { - "dot" - head[2] - } - - chain_tail[2] = { - "call" - { - "self" - unpack call[2] - } - } - - insert new_chain, item for item in *chain_tail - new_chain - {"declare", { cls_name }} {"declare_glob", "*"} From a9bb80159f68d1f54b7b6c7f50a4244bdaaa40ea Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 11:05:34 -0800 Subject: [PATCH 165/344] super works inside class methods --- moonscript/transform/class.lua | 8 +++++++- moonscript/transform/class.moon | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/moonscript/transform/class.lua b/moonscript/transform/class.lua index a7a6273f..3e480c26 100644 --- a/moonscript/transform/class.lua +++ b/moonscript/transform/class.lua @@ -118,7 +118,13 @@ return function(self, node, ret, parent_assign) for _index_1 = 2, #item do local tuple = item[_index_1] if ntype(tuple[1]) == "self" then - insert(statements, build.assign_one(unpack(tuple))) + local k, v + k, v = tuple[1], tuple[2] + v = super_scope(v, cls_super, { + "key_literal", + k[2] + }) + insert(statements, build.assign_one(k, v)) else insert(properties, tuple) end diff --git a/moonscript/transform/class.moon b/moonscript/transform/class.moon index 6dd7099b..729c47a9 100644 --- a/moonscript/transform/class.moon +++ b/moonscript/transform/class.moon @@ -92,7 +92,9 @@ super_scope = (value, t, key) -> when "props" for tuple in *item[2,] if ntype(tuple[1]) == "self" - insert statements, build.assign_one unpack tuple + {k,v} = tuple + v = super_scope v, cls_super, {"key_literal", k[2]} + insert statements, build.assign_one k, v else insert properties, tuple From 1bdcf26b08e9929a55ceb02e3b91fb02df8f93cc Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 11:06:35 -0800 Subject: [PATCH 166/344] super scope test --- spec/inputs/class.moon | 9 +++++++++ spec/outputs/class.lua | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/spec/inputs/class.moon b/spec/inputs/class.moon index 87eb866c..27fde65b 100644 --- a/spec/inputs/class.moon +++ b/spec/inputs/class.moon @@ -186,4 +186,13 @@ class Cool extends Thing world: -> super.one } + +-- + +class Cool extends Thing + dang: do_something => + super! + + + nil diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index 6f361f82..e9ce06b6 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -809,4 +809,45 @@ do end Cool = _class_0 end +do + local _class_0 + local _parent_0 = Thing + local _base_0 = { + dang = do_something(function(self) + return _class_0.__parent.dang(self) + end) + } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, ...) + return _class_0.__parent.__init(self, ...) + end, + __base = _base_0, + __name = "Cool", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + Cool = _class_0 +end return nil \ No newline at end of file From 7eb269b35ab55de10be054756dc4709244d07988 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 11:22:40 -0800 Subject: [PATCH 167/344] add distinct class and instance super transformers --- moonscript/transform/class.lua | 21 ++++++++++++++++++--- moonscript/transform/class.moon | 14 +++++++++++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/moonscript/transform/class.lua b/moonscript/transform/class.lua index 3e480c26..f55dce00 100644 --- a/moonscript/transform/class.lua +++ b/moonscript/transform/class.lua @@ -13,8 +13,13 @@ do local _obj_0 = require("moonscript.types") build, ntype, NOOP = _obj_0.build, _obj_0.ntype, _obj_0.NOOP end +local unpack +unpack = require("moonscript.util").unpack local transform_super -transform_super = function(cls_name, block, chain) +transform_super = function(cls_name, on_base, block, chain) + if on_base == nil then + on_base = true + end local relative_parent = { "chain", cls_name, @@ -36,6 +41,12 @@ transform_super = function(cls_name, block, chain) local new_chain = relative_parent local _exp_0 = head[1] if "call" == _exp_0 then + if on_base then + insert(new_chain, { + "dot", + "__base" + }) + end local calling_name = block:get("current_method") assert(calling_name, "missing calling name") chain_tail[1] = { @@ -103,9 +114,13 @@ return function(self, node, ret, parent_assign) local base_name = NameProxy("base") local self_name = NameProxy("self") local cls_name = NameProxy("class") + local cls_instance_super + cls_instance_super = function(...) + return transform_super(cls_name, true, ...) + end local cls_super cls_super = function(...) - return transform_super(cls_name, ...) + return transform_super(cls_name, false, ...) end local statements = { } local properties = { } @@ -150,7 +165,7 @@ return function(self, node, ret, parent_assign) key, val = tuple[1], tuple[2] _value_0 = { key, - super_scope(val, cls_super, key) + super_scope(val, cls_instance_super, key) } end _accum_0[_len_0] = _value_0 diff --git a/moonscript/transform/class.moon b/moonscript/transform/class.moon index 729c47a9..2a952657 100644 --- a/moonscript/transform/class.moon +++ b/moonscript/transform/class.moon @@ -5,8 +5,9 @@ CONSTRUCTOR_NAME = "new" import insert from table import build, ntype, NOOP from require "moonscript.types" +import unpack from require "moonscript.util" -transform_super = (cls_name, block, chain) -> +transform_super = (cls_name, on_base=true, block, chain) -> relative_parent = { "chain", cls_name @@ -26,6 +27,9 @@ transform_super = (cls_name, block, chain) -> switch head[1] -- calling super, inject calling name and self into chain when "call" + if on_base + insert new_chain, {"dot", "__base"} + calling_name = block\get "current_method" assert calling_name, "missing calling name" chain_tail[1] = {"call", {"self", unpack head[2]}} @@ -80,7 +84,11 @@ super_scope = (value, t, key) -> self_name = NameProxy "self" cls_name = NameProxy "class" - cls_super = (...) -> transform_super cls_name, ... + -- super call on instance + cls_instance_super = (...) -> transform_super cls_name, true, ... + + -- super call on parent class + cls_super = (...) -> transform_super cls_name, false, ... -- split apart properties and statements statements = {} @@ -107,7 +115,7 @@ super_scope = (value, t, key) -> continue else {key, val} = tuple - {key, super_scope val, cls_super, key} + {key, super_scope val, cls_instance_super, key} unless constructor From 7aa63b2b61c90c26f1f742356fc28269978923eb Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 11:24:42 -0800 Subject: [PATCH 168/344] update specs for new class super --- spec/inputs/class.moon | 19 +++++++++-- spec/outputs/class.lua | 74 ++++++++++++++++++++++++++++++++++++++---- 2 files changed, 84 insertions(+), 9 deletions(-) diff --git a/spec/inputs/class.moon b/spec/inputs/class.moon index 27fde65b..af03994b 100644 --- a/spec/inputs/class.moon +++ b/spec/inputs/class.moon @@ -186,13 +186,28 @@ class Cool extends Thing world: -> super.one } - -- -class Cool extends Thing +class Whack extends Thing dang: do_something => super! +--- + +class Wowha extends Thing + @butt: -> + super! + super.hello + super\hello! + super\hello + + @butt: cool { + -> + super! + super.hello + super\hello! + super\hello + } nil diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index e9ce06b6..aebacbda 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -126,7 +126,7 @@ do local _parent_0 = Hi local _base_0 = { cool = function(self) - return _class_0.__parent.cool(self, 120302) + return _class_0.__parent.__base.cool(self, 120302) end } _base_0.__index = _base_0 @@ -193,7 +193,7 @@ do local _parent_0 = Okay local _base_0 = { something = function(self) - _class_0.__parent.something(self, 1, 2, 3, 4) + _class_0.__parent.__base.something(self, 1, 2, 3, 4) _class_0.__parent.something(another_self, 1, 2, 3, 4) return assert(_class_0.__parent == Okay) end @@ -336,7 +336,7 @@ do local _class_0 local _base_0 = { hi = function(self) - _class_0.__parent.hi(self, 1, 2, 3, 4)(1, 2, 3, 4) + _class_0.__parent.__base.hi(self, 1, 2, 3, 4)(1, 2, 3, 4) _class_0.__parent.something(1, 2, 3, 4) local _ = _class_0.__parent.something(1, 2, 3, 4).world _class_0.__parent.yeah(self, "world").okay(hi, hi, hi) @@ -768,7 +768,7 @@ do dang = function(self) return { hello = function() - return _class_0.__parent.dang(self) + return _class_0.__parent.__base.dang(self) end, world = function() return _class_0.__parent.one @@ -814,7 +814,7 @@ do local _parent_0 = Thing local _base_0 = { dang = do_something(function(self) - return _class_0.__parent.dang(self) + return _class_0.__parent.__base.dang(self) end) } _base_0.__index = _base_0 @@ -824,7 +824,7 @@ do return _class_0.__parent.__init(self, ...) end, __base = _base_0, - __name = "Cool", + __name = "Whack", __parent = _parent_0 }, { __index = function(cls, name) @@ -848,6 +848,66 @@ do if _parent_0.__inherited then _parent_0.__inherited(_parent_0, _class_0) end - Cool = _class_0 + Whack = _class_0 +end +do + local _class_0 + local _parent_0 = Thing + local _base_0 = { } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, ...) + return _class_0.__parent.__init(self, ...) + end, + __base = _base_0, + __name = "Wowha", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + local self = _class_0 + self.butt = function() + _class_0.__parent.butt(self) + _ = _class_0.__parent.hello + _class_0.__parent.hello(self) + local _base_1 = _class_0.__parent + local _fn_0 = _base_1.hello + return function(...) + return _fn_0(self, ...) + end + end + self.butt = cool({ + function() + _class_0.__parent.butt(self) + _ = _class_0.__parent.hello + _class_0.__parent.hello(self) + local _base_1 = _class_0.__parent + local _fn_0 = _base_1.hello + return function(...) + return _fn_0(self, ...) + end + end + }) + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + Wowha = _class_0 end return nil \ No newline at end of file From 92932584b4b367cc6e28c244e794f1c4d8d8c743 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 11:31:40 -0800 Subject: [PATCH 169/344] add more super class specs --- moonscript/cmd/lint.lua | 4 +- spec/class_spec.moon | 282 ++++++++++++++++++++++++---------------- spec/inputs/class.moon | 2 +- spec/outputs/class.lua | 4 +- 4 files changed, 173 insertions(+), 119 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 50509e57..8f036ff1 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -121,11 +121,11 @@ do end, render = function(self, ...) self:lint_check_unused() - return _class_0.__parent.render(self, ...) + return _class_0.__parent.__base.render(self, ...) end, block = function(self, ...) do - local _with_0 = _class_0.__parent.block(self, ...) + local _with_0 = _class_0.__parent.__base.block(self, ...) _with_0.block = self.block _with_0.render = self.render _with_0.get_root_block = self.get_root_block diff --git a/spec/class_spec.moon b/spec/class_spec.moon index d21e8bff..e66eefac 100644 --- a/spec/class_spec.moon +++ b/spec/class_spec.moon @@ -40,61 +40,6 @@ describe "class", -> instance = Thing "color" assert.same instance\get_property!, "green" - it "should call super constructor", -> - class Base - new: (@property) => - - class Thing extends Base - new: (@name) => - super "name" - - instance = Thing "the_thing" - - assert.same instance.property, "name" - assert.same instance.name, "the_thing" - - it "should call super method", -> - class Base - _count: 111 - counter: => @_count - - class Thing extends Base - counter: => "%08d"\format super! - - instance = Thing! - assert.same instance\counter!, "00000111" - - it "should call other method from super", -> - class Base - _count: 111 - counter: => - @_count - - class Thing extends Base - other_method: => super\counter! - - instance = Thing! - assert.same instance\other_method!, 111 - - it "should get super class", -> - class Base - class Thing extends Base - get_super: => super - - instance = Thing! - assert.is_true instance\get_super! == Base - - it "should get a bound method from super", -> - class Base - count: 1 - get_count: => @count - - class Thing extends Base - get_count: => "this is wrong" - get_method: => super\get_count - - instance = Thing! - assert.same instance\get_method!!, 1 it "should have class properties", -> class Base @@ -151,86 +96,195 @@ describe "class", -> assert.same "hello", Thing.prop - it "class properties take precedence in super class over base", -> - class Thing - @prop: "hello" - prop: "world" + describe "super", -> + it "should call super constructor", -> + class Base + new: (@property) => - class OtherThing extends Thing + class Thing extends Base + new: (@name) => + super "name" - assert.same "hello", OtherThing.prop + instance = Thing "the_thing" - it "gets value from base in super class", -> - class Thing - prop: "world" + assert.same instance.property, "name" + assert.same instance.name, "the_thing" - class OtherThing extends Thing - assert.same "world", OtherThing.prop + it "should call super method", -> + class Base + _count: 111 + counter: => @_count - it "should let parent be replaced on class", -> - class A - @prop: "yeah" - cool: => 1234 - plain: => "a" + class Thing extends Base + counter: => "%08d"\format super! - class B - @prop: "okay" - cool: => 9999 - plain: => "b" + instance = Thing! + assert.same instance\counter!, "00000111" - class Thing extends A - cool: => - super! + 1 + it "should call other method from super", -> + class Base + _count: 111 + counter: => + @_count - get_super: => - super + class Thing extends Base + other_method: => super\counter! - instance = Thing! + instance = Thing! + assert.same instance\other_method!, 111 + + it "should get super class", -> + class Base + class Thing extends Base + get_super: => super + + instance = Thing! + assert.is_true instance\get_super! == Base + + it "should get a bound method from super", -> + class Base + count: 1 + get_count: => @count + + class Thing extends Base + get_count: => "this is wrong" + get_method: => super\get_count + + instance = Thing! + assert.same instance\get_method!!, 1 + + it "class properties take precedence in super class over base", -> + class Thing + @prop: "hello" + prop: "world" + + class OtherThing extends Thing + + assert.same "hello", OtherThing.prop + + it "gets value from base in super class", -> + class Thing + prop: "world" + + class OtherThing extends Thing + assert.same "world", OtherThing.prop + + it "should let parent be replaced on class", -> + class A + @prop: "yeah" + cool: => 1234 + plain: => "a" + + class B + @prop: "okay" + cool: => 9999 + plain: => "b" + + class Thing extends A + cool: => + super! + 1 + + get_super: => + super + + instance = Thing! + + assert.same "a", instance\plain! + assert.same 1235, instance\cool! + assert A == instance\get_super!, "expected super to be B" + + Thing.__parent = B + setmetatable Thing.__base, B.__base + + assert.same "b", instance\plain! + assert.same 10000, instance\cool! + assert B == instance\get_super!, "expected super to be B" + + it "should resolve many levels of super", -> + class One + a: => + 1 + + class Two extends One + a: => + super! + 2 + + class Three extends Two + a: => + super! + 3 + + i = Three! + + assert.same 6, i\a! + + + it "should resolve many levels of super with a gap", -> + class One + a: => + 1 + + class Two extends One + + class Three extends Two + a: => + super! + 3 + + class Four extends Three + a: => + super! + 4 + + i = Four! + + assert.same 8, i\a! + + + it "should call correct class/instance super methods", -> + class Base + doit: => + "instance" - assert.same "a", instance\plain! - assert.same 1235, instance\cool! - assert A == instance\get_super!, "expected super to be B" + @doit: => + "class" - Thing.__parent = B - setmetatable Thing.__base, B.__base + class One extends Base + doit: => super! + @doit: => super! - assert.same "b", instance\plain! - assert.same 10000, instance\cool! - assert B == instance\get_super!, "expected super to be B" + assert.same "instance", One!\doit! + assert.same "class", One\doit! - it "should resolve many levels of super", -> - class One - a: => - 1 - class Two extends One - a: => - super! + 2 + it "should resolve many levels of super on class methods", -> + class One + @a: => + 1 - class Three extends Two - a: => - super! + 3 + class Two extends One - i = Three! + class Three extends Two + @a: => + super! + 3 - assert.same 6, i\a! + class Four extends Three + @a: => + super! + 4 + assert.same 8, Four\a! - it "should resolve many levels of super with a gap", -> - class One - a: => - 1 + it "super should still work when method wrapped", -> + add_some = (opts) -> + => opts.amount + opts[1] @ - class Two extends One + class Base + value: => 1 - class Three extends Two - a: => - super! + 3 + class Sub extends Base + value: add_some { + amount: 12 + => + super! + 100 + } - class Four extends Three - a: => - super! + 4 + assert.same 1 + 100 + 12, Sub!\value! - i = Four! - assert.same 8, i\a! diff --git a/spec/inputs/class.moon b/spec/inputs/class.moon index af03994b..9a980557 100644 --- a/spec/inputs/class.moon +++ b/spec/inputs/class.moon @@ -202,7 +202,7 @@ class Wowha extends Thing super\hello - @butt: cool { + @zone: cool { -> super! super.hello diff --git a/spec/outputs/class.lua b/spec/outputs/class.lua index aebacbda..018760a0 100644 --- a/spec/outputs/class.lua +++ b/spec/outputs/class.lua @@ -893,9 +893,9 @@ do return _fn_0(self, ...) end end - self.butt = cool({ + self.zone = cool({ function() - _class_0.__parent.butt(self) + _class_0.__parent.zone(self) _ = _class_0.__parent.hello _class_0.__parent.hello(self) local _base_1 = _class_0.__parent From 232bad8fc312141fda7ee2565fad098f097e285a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 11:34:04 -0800 Subject: [PATCH 170/344] add ref to unpack everywhere --- moonscript/compile/statement.lua | 2 ++ moonscript/compile/statement.moon | 2 ++ moonscript/transform/statement.lua | 2 ++ moonscript/transform/statement.moon | 2 ++ moonscript/transform/value.lua | 2 ++ moonscript/transform/value.moon | 1 + 6 files changed, 11 insertions(+) diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index eebb24a9..8b250d54 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -5,6 +5,8 @@ do local _obj_0 = table concat, insert = _obj_0.concat, _obj_0.insert end +local unpack +unpack = require("moonscript.util").unpack return { raw = function(self, node) return self:add(node[2]) diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index bb80e969..bddd3dca 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -2,6 +2,8 @@ import ntype from require "moonscript.types" import concat, insert from table +import unpack from require "moonscript.util" + { raw: (node) => @add node[2] diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index f37a9d6f..efc0e723 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -15,6 +15,8 @@ insert = table.insert local destructure = require("moonscript.transform.destructure") local construct_comprehension construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension +local unpack +unpack = require("moonscript.util").unpack local with_continue_listener with_continue_listener = function(body) local continue_name = nil diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index 7933c5fe..1d11560b 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -15,6 +15,8 @@ import insert from table destructure = require "moonscript.transform.destructure" import construct_comprehension from require "moonscript.transform.comprehension" +import unpack from require "moonscript.util" + with_continue_listener = (body) -> continue_name = nil diff --git a/moonscript/transform/value.lua b/moonscript/transform/value.lua index 159ae52a..50a49041 100644 --- a/moonscript/transform/value.lua +++ b/moonscript/transform/value.lua @@ -23,6 +23,8 @@ local construct_comprehension construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension local insert insert = table.insert +local unpack +unpack = require("moonscript.util").unpack return Transformer({ ["for"] = default_accumulator, ["while"] = default_accumulator, diff --git a/moonscript/transform/value.moon b/moonscript/transform/value.moon index 7b415d6f..18d97a3c 100644 --- a/moonscript/transform/value.moon +++ b/moonscript/transform/value.moon @@ -10,6 +10,7 @@ import Run, transform_last_stm, implicitly_return, chain_is_stub from require "m import construct_comprehension from require "moonscript.transform.comprehension" import insert from table +import unpack from require "moonscript.util" Transformer { for: default_accumulator From caa724d0989958e0d20a19e51c68e4cfb00bb164 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 12:03:19 -0800 Subject: [PATCH 171/344] more spec --- spec/class_spec.moon | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/spec/class_spec.moon b/spec/class_spec.moon index e66eefac..394c59b1 100644 --- a/spec/class_spec.moon +++ b/spec/class_spec.moon @@ -285,6 +285,13 @@ describe "class", -> super! + 100 } + class OtherSub extends Base + value: if true + => 5 + super! + else + => 2 + super! + assert.same 1 + 100 + 12, Sub!\value! + assert.same 6, OtherSub!\value! From 352130cdaed47045a76c8d2b4f55b67a9d47d26e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 12:03:23 -0800 Subject: [PATCH 172/344] start writing changelog --- CHANGELOG.md | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b78411af..c6a8d0ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,82 @@ +# MoonScript v0.4.0 (2015-12-06) + +## Changes to `super` + +`super` now looks up the parent method via the class reference, instead of a +(fixed) closure to the parent class. + +Given the following code: + +```moonscript +class MyThing extends OtherThing + the_method: => + super! +``` + +In the past `super` would compile to something like this: + +```lua +_parent_0.the_method(self) +``` + +Where `_parent_0` was an internal local variable that contains a reference to +the parent class. Because the reference to parent is an internal local +variable, you could never swap out the parent unless resorting to the debug +library. + +This version will compile to: + +```lua +_class_0.__parent.__base.the_method(self) +``` + +Where `_class_0` is an internal local variable that contains the current class (`MyThing`). + +Another difference is that the instance method is looked up on `__base` instead +of the class. The old variation would trigger the metamethod for looking up on +the instance, but a class method of the same name could conflict, take +precedence, and be retuned instead. By referencing `__base` directly we avoid +this issue. + +### Super on class methods + +`super` can now be used on class methods. It works exactly as you would expect. + +```moonscript +class MyThing extends OtherThing + @static_method: => + print super! +``` + +Calling `super` will compile to: + +```moonscript +_class_0.__parent.static_method(self) +``` + +### Improved scoping for super + +The scoping of super is more intelligent. You can warp your methods in other +code and `super` will still generate correctly. For example, syntax like this +will now work as expected: + +```moonscript +class Sub extends Base + value: if debugging + => super! + 100 + else + => super! + 10 + + other_value: some_decorator { + the_func: => + super! + } +``` + +`super` will refer to the lexically closest class declaration to find the name +of the method it should call on the parent. + # MoonScript v0.3.2 (2015-6-01) ## Bug Fixes From 23cd6f5571b6f326eb706d15cadbecc7cc9e7094 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 12:04:00 -0800 Subject: [PATCH 173/344] increment version :fish: --- moonscript/version.lua | 2 +- moonscript/version.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/version.lua b/moonscript/version.lua index 352d7a98..45fa3c50 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,4 +1,4 @@ -local version = "0.3.2" +local version = "0.4.0" return { version = version, print_version = function() diff --git a/moonscript/version.moon b/moonscript/version.moon index 018d1c8c..6f665e26 100644 --- a/moonscript/version.moon +++ b/moonscript/version.moon @@ -1,5 +1,5 @@ -version = "0.3.2" +version = "0.4.0" { version: version, From 36e7eb1846c368d047ab26ad166f20fce558b07b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 12:18:16 -0800 Subject: [PATCH 174/344] more changelog updates --- CHANGELOG.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c6a8d0ac..d2c2cdb7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -77,6 +77,20 @@ class Sub extends Base `super` will refer to the lexically closest class declaration to find the name of the method it should call on the parent. +## Bug Fixes + +* Nested `with` blocks used incorrect ref (#214 by @geomaster) +* Lua quote string literals had wrong precedence (#200 by @nonchip) +* Returning from `with` block would generate two `return` statements (#208) +* Including `return` or `break` in a `continue` wrapped block would generate invalid code (#215 #190 #183) + +## Other + +* Refactor transformer out into multiple files +* `moon` command line script rewritten in MoonScript +* `moonscript.parse.build_grammar` function for getting new instance of parser grammar +* Chain AST updated to be simpler + # MoonScript v0.3.2 (2015-6-01) ## Bug Fixes From 3aac81533a1abfc9629c932da7326845d222b643 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 17:24:44 -0800 Subject: [PATCH 175/344] add new build status image --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index bf5359ea..871b86aa 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ [![MoonScript](http://leafo.net/dump/sailormoonscript.png)](http://moonscript.org) -[![Build Status](https://travis-ci.org/leafo/moonscript.svg?branch=master)](https://travis-ci.org/leafo/moonscript) +[![Build Status](https://travis-ci.org/leafo/moonscript.svg?branch=master)](https://travis-ci.org/leafo/moonscript) [![Build status](https://ci.appveyor.com/api/projects/status/f5prpi4wvytul290/branch/binaries?svg=true)](https://ci.appveyor.com/project/leafo/moonscript/branch/binaries) + MoonScript is a programmer friendly language that compiles into [Lua](http://www.lua.org/). It gives you the power of the fastest scripting From b129b00daf7cff3e45cd9ef3a87ed5bf02bf0ea3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 17:33:35 -0800 Subject: [PATCH 176/344] update to travis build scripts from https://github.com/moteus/lua-travis-example no sudo oops forgot to install lua busted stable? we will get there.. use stable loadkit --- .travis.yml | 23 +++++--- .travis/platform.sh | 15 ++++++ .travis/setenv_lua.sh | 3 ++ .travis/setup_lua.sh | 121 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 .travis/platform.sh create mode 100644 .travis/setenv_lua.sh create mode 100644 .travis/setup_lua.sh diff --git a/.travis.yml b/.travis.yml index e9e6313d..48d84c8c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,11 +1,22 @@ language: c +sudo: false + +env: + global: + - LUAROCKS=2.2.2 + matrix: + - LUA=lua5.1 + - LUA=lua5.2 + - LUA=lua5.3 + - LUA=luajit2.1 # current head of 2.1 branch + +before_install: + - source .travis/setenv_lua.sh + install: - - sudo apt-get install luarocks - - mkdir -p ~/.luarocks - - echo 'rocks_servers = { "http://luarocks.org" }' > ~/.luarocks/config.lua - - sudo luarocks install https://luarocks.org/manifests/olivine-labs/busted-2.0.rc7-0.rockspec - - sudo luarocks install https://raw.githubusercontent.com/leafo/loadkit/master/loadkit-dev-1.rockspec - - sudo luarocks make + - luarocks install https://luarocks.org/manifests/olivine-labs/busted-2.0.rc11-0.rockspec + - luarocks install loadkit + - luarocks make script: busted diff --git a/.travis/platform.sh b/.travis/platform.sh new file mode 100644 index 00000000..7259a7d6 --- /dev/null +++ b/.travis/platform.sh @@ -0,0 +1,15 @@ +if [ -z "${PLATFORM:-}" ]; then + PLATFORM=$TRAVIS_OS_NAME; +fi + +if [ "$PLATFORM" == "osx" ]; then + PLATFORM="macosx"; +fi + +if [ -z "$PLATFORM" ]; then + if [ "$(uname)" == "Linux" ]; then + PLATFORM="linux"; + else + PLATFORM="macosx"; + fi; +fi diff --git a/.travis/setenv_lua.sh b/.travis/setenv_lua.sh new file mode 100644 index 00000000..8d8c8255 --- /dev/null +++ b/.travis/setenv_lua.sh @@ -0,0 +1,3 @@ +export PATH=${PATH}:$HOME/.lua:$HOME/.local/bin:${TRAVIS_BUILD_DIR}/install/luarocks/bin +bash .travis/setup_lua.sh +eval `$HOME/.lua/luarocks path` diff --git a/.travis/setup_lua.sh b/.travis/setup_lua.sh new file mode 100644 index 00000000..275b0ec5 --- /dev/null +++ b/.travis/setup_lua.sh @@ -0,0 +1,121 @@ +#! /bin/bash + +# A script for setting up environment for travis-ci testing. +# Sets up Lua and Luarocks. +# LUA must be "lua5.1", "lua5.2" or "luajit". +# luajit2.0 - master v2.0 +# luajit2.1 - master v2.1 + +set -eufo pipefail + +LUAJIT_BASE="LuaJIT-2.0.4" + +source .travis/platform.sh + +LUA_HOME_DIR=$TRAVIS_BUILD_DIR/install/lua + +LR_HOME_DIR=$TRAVIS_BUILD_DIR/install/luarocks + +mkdir $HOME/.lua + +LUAJIT="no" + +if [ "$PLATFORM" == "macosx" ]; then + if [ "$LUA" == "luajit" ]; then + LUAJIT="yes"; + fi + if [ "$LUA" == "luajit2.0" ]; then + LUAJIT="yes"; + fi + if [ "$LUA" == "luajit2.1" ]; then + LUAJIT="yes"; + fi; +elif [ "$(expr substr $LUA 1 6)" == "luajit" ]; then + LUAJIT="yes"; +fi + +mkdir -p "$LUA_HOME_DIR" + +if [ "$LUAJIT" == "yes" ]; then + + if [ "$LUA" == "luajit" ]; then + curl http://luajit.org/download/$LUAJIT_BASE.tar.gz | tar xz; + else + git clone http://luajit.org/git/luajit-2.0.git $LUAJIT_BASE; + fi + + cd $LUAJIT_BASE + + if [ "$LUA" == "luajit2.1" ]; then + git checkout v2.1; + # force the INSTALL_TNAME to be luajit + perl -i -pe 's/INSTALL_TNAME=.+/INSTALL_TNAME= luajit/' Makefile + fi + + make && make install PREFIX="$LUA_HOME_DIR" + + ln -s $LUA_HOME_DIR/bin/luajit $HOME/.lua/luajit + ln -s $LUA_HOME_DIR/bin/luajit $HOME/.lua/lua; + +else + + if [ "$LUA" == "lua5.1" ]; then + curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz + cd lua-5.1.5; + elif [ "$LUA" == "lua5.2" ]; then + curl http://www.lua.org/ftp/lua-5.2.4.tar.gz | tar xz + cd lua-5.2.4; + elif [ "$LUA" == "lua5.3" ]; then + curl http://www.lua.org/ftp/lua-5.3.2.tar.gz | tar xz + cd lua-5.3.2; + fi + + # Build Lua without backwards compatibility for testing + perl -i -pe 's/-DLUA_COMPAT_(ALL|5_2)//' src/Makefile + make $PLATFORM + make INSTALL_TOP="$LUA_HOME_DIR" install; + + ln -s $LUA_HOME_DIR/bin/lua $HOME/.lua/lua + ln -s $LUA_HOME_DIR/bin/luac $HOME/.lua/luac; + +fi + +cd $TRAVIS_BUILD_DIR + +lua -v + +LUAROCKS_BASE=luarocks-$LUAROCKS + +curl --location http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz | tar xz + +cd $LUAROCKS_BASE + +if [ "$LUA" == "luajit" ]; then + ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.0" --prefix="$LR_HOME_DIR"; +elif [ "$LUA" == "luajit2.0" ]; then + ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.0" --prefix="$LR_HOME_DIR"; +elif [ "$LUA" == "luajit2.1" ]; then + ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.1" --prefix="$LR_HOME_DIR"; +else + ./configure --with-lua="$LUA_HOME_DIR" --prefix="$LR_HOME_DIR" +fi + +make build && make install + +ln -s $LR_HOME_DIR/bin/luarocks $HOME/.lua/luarocks + +cd $TRAVIS_BUILD_DIR + +luarocks --version + +rm -rf $LUAROCKS_BASE + +if [ "$LUAJIT" == "yes" ]; then + rm -rf $LUAJIT_BASE; +elif [ "$LUA" == "lua5.1" ]; then + rm -rf lua-5.1.5; +elif [ "$LUA" == "lua5.2" ]; then + rm -rf lua-5.2.4; +elif [ "$LUA" == "lua5.3" ]; then + rm -rf lua-5.3.2; +fi From 9e606623656057b50d464259d4b7acdbada5aebd Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 6 Dec 2015 18:06:06 -0800 Subject: [PATCH 177/344] spec fixes for greater versions of lua --- moon/init.lua | 9 ++++++--- moon/init.moon | 4 +--- spec/comprehension_spec.moon | 2 ++ 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/moon/init.lua b/moon/init.lua index 0ca497c6..d4c24903 100644 --- a/moon/init.lua +++ b/moon/init.lua @@ -1,10 +1,13 @@ -local util = require("moonscript.util") local lua = { debug = debug, type = type } -local dump, p, is_object, type, debug, run_with_scope, bind_methods, defaultbl, extend, copy, mixin, mixin_object, mixin_table, fold -dump = util.dump +local getfenv, setfenv, dump +do + local _obj_0 = require("moonscript.util") + getfenv, setfenv, dump = _obj_0.getfenv, _obj_0.setfenv, _obj_0.dump +end +local p, is_object, type, debug, run_with_scope, bind_methods, defaultbl, extend, copy, mixin, mixin_object, mixin_table, fold p = function(...) return print(dump(...)) end diff --git a/moon/init.moon b/moon/init.moon index ef07bbaf..658d8867 100644 --- a/moon/init.moon +++ b/moon/init.moon @@ -1,11 +1,9 @@ -util = require "moonscript.util" lua = { :debug, :type } +import getfenv, setfenv, dump from require "moonscript.util" local * -dump = util.dump - p = (...) -> print dump ... diff --git a/spec/comprehension_spec.moon b/spec/comprehension_spec.moon index 2f07939d..d8823728 100644 --- a/spec/comprehension_spec.moon +++ b/spec/comprehension_spec.moon @@ -1,4 +1,6 @@ +import unpack from require "moonscript.util" + describe "comprehension", -> it "should double every number", -> input = {1,2,3,4,5,6} From a569774536a9e010ef50d7ab424a480b1fc51a46 Mon Sep 17 00:00:00 2001 From: nymphium Date: Wed, 9 Dec 2015 03:46:25 +0900 Subject: [PATCH 178/344] fix #191 benchmark failed --- moonscript/cmd/moonc.lua | 2 +- moonscript/cmd/moonc.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index eae86380..54600998 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -102,7 +102,7 @@ compile_file_text = function(text, opts) "Compile time\t" .. format_time(compile_time), "" }, "\n")) - return nil + return true end return code end diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon index a680eaa7..b2e92e63 100644 --- a/moonscript/cmd/moonc.moon +++ b/moonscript/cmd/moonc.moon @@ -105,7 +105,7 @@ compile_file_text = (text, opts={}) -> "Compile time\t" .. format_time(compile_time), "" }, "\n" - return nil + return true code From a4bc557724cde78b0362f419ce6a0aaf708aa71d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 27 Dec 2015 20:47:19 -0800 Subject: [PATCH 179/344] fix travis --- .travis/setup_lua.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis/setup_lua.sh b/.travis/setup_lua.sh index 275b0ec5..6dcc0c6e 100644 --- a/.travis/setup_lua.sh +++ b/.travis/setup_lua.sh @@ -8,7 +8,8 @@ set -eufo pipefail -LUAJIT_BASE="LuaJIT-2.0.4" +LUAJIT_VERSION="2.0.4" +LUAJIT_BASE="LuaJIT-$LUAJIT_VERSION" source .travis/platform.sh @@ -39,9 +40,9 @@ mkdir -p "$LUA_HOME_DIR" if [ "$LUAJIT" == "yes" ]; then if [ "$LUA" == "luajit" ]; then - curl http://luajit.org/download/$LUAJIT_BASE.tar.gz | tar xz; + curl --location https://github.com/LuaJIT/LuaJIT/archive/v$LUAJIT_VERSION.tar.gz | tar xz; else - git clone http://luajit.org/git/luajit-2.0.git $LUAJIT_BASE; + git clone https://github.com/LuaJIT/LuaJIT.git $LUAJIT_BASE; fi cd $LUAJIT_BASE From a9e0874a7aac427cb6ced5d39232e16ecef5c2f8 Mon Sep 17 00:00:00 2001 From: AlissaSquared Date: Sun, 27 Dec 2015 22:36:54 -0600 Subject: [PATCH 180/344] add lint option to watcher --- bin/moonc | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/bin/moonc b/bin/moonc index 5de57204..2d707678 100755 --- a/bin/moonc +++ b/bin/moonc @@ -264,6 +264,15 @@ local function create_watcher(files) end end +-- build function to check for lint or compile in watch +local check_compile_or_lint +if opts.l then + local lint = require "moonscript.cmd.lint" + check_compile_or_lint = lint.lint_file +else + check_compile_or_lint = compile_and_write +end + if opts.w then local watcher = create_watcher(files) -- catches interrupt error for ctl-c @@ -283,8 +292,14 @@ if opts.w then target = opts.o end - local success, err = compile_and_write(fname, target) - if not success then + local success, err = check_compile_or_lint(fname, target) + if opts.l then + if success then + io.stderr:write(success .. "\n\n") + elseif err then + io.stderr:write(fname .. "\n" .. err .. "\n\n") + end + elseif not success then io.stderr:write(table.concat({ "", "Error: " .. fname, @@ -300,8 +315,7 @@ if opts.w then elseif opts.l then for _, tuple in pairs(files) do local fname = tuple[1] - local lint = require "moonscript.cmd.lint" - local res, err = lint.lint_file(fname) + local res, err = check_compile_or_lint(fname) if res then io.stderr:write(res .. "\n\n") elseif err then From 47d94d5bb64aebd11b15c15f4e068cbd21992e33 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 27 Dec 2015 21:02:57 -0800 Subject: [PATCH 181/344] update preamble syntax --- docs/api.md | 11 ++++++----- docs/command_line.md | 11 ++++++----- docs/reference.md | 11 ++++++----- docs/standard_lib.md | 11 ++++++----- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/docs/api.md b/docs/api.md index 367891af..a0aa7f55 100644 --- a/docs/api.md +++ b/docs/api.md @@ -1,8 +1,9 @@ -target: reference/api -template: reference -title: Compiler API -short_name: api --- +{ + target: "reference/api" + template: "reference" + title: "Compiler API" + short_name: "api" +} # MoonScript Compiler API diff --git a/docs/command_line.md b/docs/command_line.md index aac3e7bd..047a0ddd 100644 --- a/docs/command_line.md +++ b/docs/command_line.md @@ -1,8 +1,9 @@ -target: reference/command_line -template: reference -title: Command Line Tools -short_name: command_line --- +{ + target: "reference/command_line" + template: "reference" + title: "Command Line Tools" + short_name: "command_line" +} # Command Line Tools diff --git a/docs/reference.md b/docs/reference.md index 3a8e9872..960930a0 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1,8 +1,9 @@ -target: reference/index -template: reference -title: Language Guide -short_name: lang --- +{ + target: "reference/index" + template: "reference" + title: "Language Guide" + short_name: "lang" +} MoonScript is a programming language that compiles to [Lua](http://www.lua.org). This guide expects the reader to have basic diff --git a/docs/standard_lib.md b/docs/standard_lib.md index 42cb7d4a..3a86a7b8 100644 --- a/docs/standard_lib.md +++ b/docs/standard_lib.md @@ -1,8 +1,9 @@ -target: reference/standard_lib -template: reference -title: Standard Library -short_name: stdlib --- +{ + target: "reference/standard_lib" + template: "reference" + title: "Standard Library" + short_name: "stdlib" +} The MoonScript installation comes with a small kernel of functions that can be used to perform various common things. From bbf5af654f63f052630ac700bba4f3d266feee90 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 27 Dec 2015 22:14:55 -0800 Subject: [PATCH 182/344] some style changes --- bin/moonc | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/bin/moonc b/bin/moonc index 2d707678..68a0284c 100755 --- a/bin/moonc +++ b/bin/moonc @@ -264,16 +264,17 @@ local function create_watcher(files) end end --- build function to check for lint or compile in watch -local check_compile_or_lint -if opts.l then - local lint = require "moonscript.cmd.lint" - check_compile_or_lint = lint.lint_file -else - check_compile_or_lint = compile_and_write -end if opts.w then + -- build function to check for lint or compile in watch + local handle_file + if opts.l then + local lint = require "moonscript.cmd.lint" + handle_file = lint.lint_file + else + handle_file = compile_and_write + end + local watcher = create_watcher(files) -- catches interrupt error for ctl-c local protected = function() @@ -292,7 +293,7 @@ if opts.w then target = opts.o end - local success, err = check_compile_or_lint(fname, target) + local success, err = handle_file(fname, target) if opts.l then if success then io.stderr:write(success .. "\n\n") @@ -313,9 +314,10 @@ if opts.w then io.stderr:write("\nQuitting...\n") elseif opts.l then + local lint = require "moonscript.cmd.lint" for _, tuple in pairs(files) do local fname = tuple[1] - local res, err = check_compile_or_lint(fname) + local res, err = lint.lint_file(fname) if res then io.stderr:write(res .. "\n\n") elseif err then From c5917c78b93fb7dfec5364cafd60ab66f7525d19 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 27 Dec 2015 22:30:37 -0800 Subject: [PATCH 183/344] misc --- moonscript/parse.moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 0c6bc823..556f81da 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -282,7 +282,7 @@ build_grammar = wrap_env debug_grammar, (root) -> op"*" + op"^" + Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export" - KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)) + KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp!) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)) KeyValueList: KeyValue * (sym"," * KeyValue)^0 KeyValueLine: CheckIndent * KeyValueList * sym","^-1 From fc77603156f75682cb059e3e244d58ab3c550175 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 27 Dec 2015 23:02:06 -0800 Subject: [PATCH 184/344] parser supports lulpeg, fixes #109 --- moonscript/parse.lua | 10 +++++----- moonscript/parse.moon | 14 ++++++-------- moonscript/parse/literals.lua | 7 ++++++- moonscript/parse/literals.moon | 6 +++++- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 072e4ef4..6e75c297 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -15,10 +15,10 @@ local wrap_env wrap_env = require("moonscript.parse.env").wrap_env local R, S, V, P, C, Ct, Cmt, Cg, Cb, Cc R, S, V, P, C, Ct, Cmt, Cg, Cb, Cc = lpeg.R, lpeg.S, lpeg.V, lpeg.P, lpeg.C, lpeg.Ct, lpeg.Cmt, lpeg.Cg, lpeg.Cb, lpeg.Cc -local White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, _Name +local White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, L, _Name do local _obj_0 = require("moonscript.parse.literals") - White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, _Name = _obj_0.White, _obj_0.Break, _obj_0.Stop, _obj_0.Comment, _obj_0.Space, _obj_0.SomeSpace, _obj_0.SpaceBreak, _obj_0.EmptyLine, _obj_0.AlphaNum, _obj_0.Num, _obj_0.Shebang, _obj_0.Name + White, Break, Stop, Comment, Space, SomeSpace, SpaceBreak, EmptyLine, AlphaNum, Num, Shebang, L, _Name = _obj_0.White, _obj_0.Break, _obj_0.Stop, _obj_0.Comment, _obj_0.Space, _obj_0.SomeSpace, _obj_0.SpaceBreak, _obj_0.EmptyLine, _obj_0.AlphaNum, _obj_0.Num, _obj_0.Shebang, _obj_0.L, _obj_0.Name end local SpaceName = Space * _Name Num = Space * (Num / function(v) @@ -110,10 +110,10 @@ local build_grammar = wrap_env(debug_grammar, function(root) File = Shebang ^ -1 * (Block + Ct("")), Block = Ct(Line * (Break ^ 1 * Line) ^ 0), CheckIndent = Cmt(Indent, check_indent), - Line = (CheckIndent * Statement + Space * #Stop), + Line = (CheckIndent * Statement + Space * L(Stop)), Statement = pos(Import + While + With + For + ForEach + Switch + Return + Local + Export + BreakLoop + Ct(ExpList) * (Update + Assign) ^ -1 / format_assign) * Space * ((key("if") * Exp * (key("else") * Exp) ^ -1 * Space / mark("if") + key("unless") * Exp / mark("unless") + CompInner / mark("comprehension")) * Space) ^ -1 / wrap_decorator, Body = Space ^ -1 * Break * EmptyLine ^ 0 * InBlock + Ct(Statement), - Advance = #Cmt(Indent, advance_indent), + Advance = L(Cmt(Indent, advance_indent)), PushIndent = Cmt(Indent, push_indent), PreventIndent = Cmt(Cc(-1), push_indent), PopIndent = Cmt("", pop_indent), @@ -170,7 +170,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) ColonChainItem = symx("\\") * _Name / mark("colon"), ColonChain = ColonChainItem * (Invoke * ChainItems ^ -1) ^ -1, Slice = symx("[") * (SliceValue + Cc(1)) * sym(",") * (SliceValue + Cc("")) * (sym(",") * SliceValue) ^ -1 * sym("]") / mark("slice"), - Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + #P("[") * LuaString / wrap_func_arg, + Invoke = FnArgs / mark("call") + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + L(P("[")) * LuaString / wrap_func_arg, TableValue = KeyValue + Ct(Exp), TableLit = sym("{") * Ct(TableValueList ^ -1 * sym(",") ^ -1 * (SpaceBreak * TableLitLine * (sym(",") ^ -1 * SpaceBreak * TableLitLine) ^ 0 * sym(",") ^ -1) ^ -1) * White * sym("}") / mark("table"), TableValueList = TableValue * (sym(",") * TableValue) ^ 0, diff --git a/moonscript/parse.moon b/moonscript/parse.moon index c87cffad..87225a4c 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -3,11 +3,10 @@ lpeg = require "lpeg" lpeg.setmaxstack 10000 -- whoa - err_msg = "Failed to parse:%s\n [%d] >> %s" import Stack from require "moonscript.data" -import trim, pos_to_line, get_line from require"moonscript.util" +import trim, pos_to_line, get_line from require "moonscript.util" import unpack from require "moonscript.util" import wrap_env from require "moonscript.parse.env" @@ -17,11 +16,10 @@ import wrap_env from require "moonscript.parse.env" { :White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine, - :AlphaNum, :Num, :Shebang + :AlphaNum, :Num, :Shebang, :L Name: _Name } = require "moonscript.parse.literals" - SpaceName = Space * _Name Num = Space * (Num / (v) -> {"number", v}) @@ -110,7 +108,7 @@ build_grammar = wrap_env debug_grammar, (root) -> File: Shebang^-1 * (Block + Ct"") Block: Ct(Line * (Break^1 * Line)^0) CheckIndent: Cmt(Indent, check_indent), -- validates line is in correct indent - Line: (CheckIndent * Statement + Space * #Stop) + Line: (CheckIndent * Statement + Space * L(Stop)) Statement: pos( Import + While + With + For + ForEach + Switch + Return + @@ -123,9 +121,9 @@ build_grammar = wrap_env debug_grammar, (root) -> CompInner / mark"comprehension" ) * Space)^-1 / wrap_decorator - Body: Space^-1 * Break * EmptyLine^0 * InBlock + Ct(Statement), -- either a statement, or an indented block + Body: Space^-1 * Break * EmptyLine^0 * InBlock + Ct(Statement) -- either a statement, or an indented block - Advance: #Cmt(Indent, advance_indent), -- Advances the indent, gives back whitespace for CheckIndent + Advance: L Cmt(Indent, advance_indent) -- Advances the indent, gives back whitespace for CheckIndent PushIndent: Cmt(Indent, push_indent) PreventIndent: Cmt(Cc(-1), push_indent) PopIndent: Cmt("", pop_indent) @@ -252,7 +250,7 @@ build_grammar = wrap_env debug_grammar, (root) -> Invoke: FnArgs / mark"call" + SingleString / wrap_func_arg + DoubleString / wrap_func_arg + - #P"[" * LuaString / wrap_func_arg + L(P"[") * LuaString / wrap_func_arg TableValue: KeyValue + Ct(Exp) diff --git a/moonscript/parse/literals.lua b/moonscript/parse/literals.lua index 61d6e2b8..1ad5aeb5 100644 --- a/moonscript/parse/literals.lua +++ b/moonscript/parse/literals.lua @@ -5,11 +5,15 @@ do local _obj_0 = require("lpeg") S, P, R, C = _obj_0.S, _obj_0.P, _obj_0.R, _obj_0.C end +local lpeg = require("lpeg") +local L = lpeg.luversion and lpeg.L or function(v) + return #v +end local White = S(" \t\r\n") ^ 0 local plain_space = S(" \t") ^ 0 local Break = P("\r") ^ -1 * P("\n") local Stop = Break + -1 -local Comment = P("--") * (1 - S("\r\n")) ^ 0 * #Stop +local Comment = P("--") * (1 - S("\r\n")) ^ 0 * L(Stop) local Space = plain_space * Comment ^ -1 local SomeSpace = S(" \t") ^ 1 * Comment ^ -1 local SpaceBreak = Space * Break @@ -19,6 +23,7 @@ local Name = C(R("az", "AZ", "__") * AlphaNum ^ 0) local Num = P("0x") * R("09", "af", "AF") ^ 1 * (S("uU") ^ -1 * S("lL") ^ 2) ^ -1 + R("09") ^ 1 * (S("uU") ^ -1 * S("lL") ^ 2) + (R("09") ^ 1 * (P(".") * R("09") ^ 1) ^ -1 + P(".") * R("09") ^ 1) * (S("eE") * P("-") ^ -1 * R("09") ^ 1) ^ -1 local Shebang = P("#!") * P(1 - Stop) ^ 0 return safe_module("moonscript.parse.literals", { + L = L, White = White, Break = Break, Stop = Stop, diff --git a/moonscript/parse/literals.moon b/moonscript/parse/literals.moon index 8f0d8cd6..5a4980f3 100644 --- a/moonscript/parse/literals.moon +++ b/moonscript/parse/literals.moon @@ -2,13 +2,16 @@ import safe_module from require "moonscript.util" import S, P, R, C from require "lpeg" +lpeg = require "lpeg" +L = lpeg.luversion and lpeg.L or (v) -> #v + White = S" \t\r\n"^0 plain_space = S" \t"^0 Break = P"\r"^-1 * P"\n" Stop = Break + -1 -Comment = P"--" * (1 - S"\r\n")^0 * #Stop +Comment = P"--" * (1 - S"\r\n")^0 * L(Stop) Space = plain_space * Comment^-1 SomeSpace = S" \t"^1 * Comment^-1 @@ -29,6 +32,7 @@ Num = P"0x" * R("09", "af", "AF")^1 * (S"uU"^-1 * S"lL"^2)^-1 + Shebang = P"#!" * P(1 - Stop)^0 safe_module "moonscript.parse.literals", { + :L :White, :Break, :Stop, :Comment, :Space, :SomeSpace, :SpaceBreak, :EmptyLine, :AlphaNum, :Name, :Num, :Shebang } From 1058593a3ee258296d976c10b82a26e0a449b37a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 28 Dec 2015 02:12:50 -0800 Subject: [PATCH 185/344] remove scite extras, link to edit plugins fixes #198 --- README.md | 7 ++ extra/scintillua/README.md | 36 ------- extra/scintillua/lexers/moonscript.lua | 124 ------------------------ extra/scintillua/lexers/themes/moon.lua | 61 ------------ extra/scintillua/moonscript.properties | 11 --- 5 files changed, 7 insertions(+), 232 deletions(-) delete mode 100644 extra/scintillua/README.md delete mode 100644 extra/scintillua/lexers/moonscript.lua delete mode 100644 extra/scintillua/lexers/themes/moon.lua delete mode 100644 extra/scintillua/moonscript.properties diff --git a/README.md b/README.md index 871b86aa..b282f75d 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,13 @@ busted Writing specs is a bit more complicated. Check out [the spec writing guide](spec/README.md). + +## Editor Support + +* [Vim](https://github.com/leafo/moonscript-vim) +* [Textadept](https://github.com/leafo/moonscript-textadept) +* [Sublime/Textmate](https://github.com/leafo/moonscript-tmbundle) + ## License (MIT) Copyright (C) 2015 by Leaf Corcoran diff --git a/extra/scintillua/README.md b/extra/scintillua/README.md deleted file mode 100644 index ffeb9800..00000000 --- a/extra/scintillua/README.md +++ /dev/null @@ -1,36 +0,0 @@ -# MoonScript for [scintillua][1] - -MoonScript syntax file for [SciTE][2] written in Lua for [scintillua][1]. - -## Windows Binary - -Windows users can get a all-included package ready for MoonScript Development: - - - -If you already have a ScITE installation, or are on another platform, follow -the directions below. - -## Installation - -Install SciTE, then [install scintillua][1]. - -Put `moonscript.properties` in in your ScITE installation folder or user -properties folder. - -Copy the entire contents of the `lexers` folder in this repository into your -scintillua `lexers` folder. - -In your `lexers` folder edit `lpeg.properties`, add to the end: - - file.patterns.moonscript=*.moon - lexer.$(file.patterns.moonscript)=lpeg_moonscript - -Optionally, enable the Moon theme, find `lexer.peg.color.theme` in the same -file and change it to: - - lexer.lpeg.color.theme=moon - - [1]: http://foicica.com/scintillua/ "scintillua" - [2]: http://www.scintilla.org/SciTE.html "SciTE" - diff --git a/extra/scintillua/lexers/moonscript.lua b/extra/scintillua/lexers/moonscript.lua deleted file mode 100644 index 7a9e54b8..00000000 --- a/extra/scintillua/lexers/moonscript.lua +++ /dev/null @@ -1,124 +0,0 @@ --- Copyright 2006-2011 Mitchell mitchellcaladbolg.net. See LICENSE. --- Moonscript lexer by leaf corcoran - -local l = lexer -local token, word_match = l.token, l.word_match -local P, S, R = lpeg.P, lpeg.S, lpeg.R - -local M = { _NAME = 'moonscript' } - --- Whitespace. -local ws = token(l.WHITESPACE, l.space^1) - -local longstring = #('[[' + ('[' * P('=')^0 * '[')) -local longstring = longstring * P(function(input, index) - local level = input:match('^%[(=*)%[', index) - if level then - local _, stop = input:find(']'..level..']', index, true) - return stop and stop + 1 or #input + 1 - end -end) - --- Comments. -local line_comment = '--' * l.nonnewline^0 -local block_comment = '--' * longstring -local comment = token(l.COMMENT, block_comment + line_comment) - --- Strings. -local sq_str = l.delimited_range("'", '\\', true) -local dq_str = l.delimited_range('"', '\\', true) -local string = token(l.STRING, sq_str + dq_str + longstring) - --- Numbers. -local number = token(l.NUMBER, l.float + l.integer) - --- Keywords. -local keyword = token(l.KEYWORD, word_match { - 'return', 'break', 'for', 'while', - 'if', 'else', 'elseif', 'then', 'export', - 'import', 'from', 'with', 'in', 'and', - 'or', 'not', 'class', 'extends', 'super', 'do', - 'using', 'switch', 'when', -}) - -local special = token("special", word_match { "true", "false", "nil" }) - --- Functions. -local builtin = token(l.FUNCTION, word_match({ - "_G","_VERSION","assert","collectgarbage","dofile","error","getfenv","getmetatable","ipairs","load", - "loadfile","loadstring","module","next","pairs","pcall","print","rawequal","rawget","rawset","require", - "select","setfenv","setmetatable","tonumber","tostring","type","unpack","xpcall", - - "coroutine.create","coroutine.resume","coroutine.running","coroutine.status","coroutine.wrap","coroutine.yield", - - "debug.debug","debug.getfenv","debug.gethook","debug.getinfo","debug.getlocal","debug.getmetatable", - "debug.getregistry","debug.getupvalue","debug.setfenv","debug.sethook","debug.setlocal","debug.setmetatable", - "debug.setupvalue","debug.traceback", - - "io.close","io.flush","io.input","io.lines","io.open","io.output","io.popen","io.read","io.stderr","io.stdin", - "io.stdout","io.tmpfile","io.type","io.write", - - "math.abs","math.acos","math.asin","math.atan","math.atan2","math.ceil","math.cos","math.cosh","math.deg", - "math.exp","math.floor","math.fmod","math.frexp","math.huge","math.ldexp","math.log","math.log10","math.max", - "math.min","math.modf","math.pi","math.pow","math.rad","math.random","math.randomseed","math.sin","math.sinh", - "math.sqrt","math.tan","math.tanh", - - "os.clock","os.date","os.difftime","os.execute","os.exit","os.getenv","os.remove","os.rename","os.setlocale", - "os.time","os.tmpname", - - "package.cpath","package.loaded","package.loaders","package.loadlib","package.path","package.preload", - "package.seeall", - - "string.byte","string.char","string.dump","string.find","string.format","string.gmatch","string.gsub", - "string.len","string.lower","string.match","string.rep","string.reverse","string.sub","string.upper", - - "table.concat","table.insert","table.maxn","table.remove","table.sort" -}, "%.")) - --- Identifiers. -local identifier = token(l.IDENTIFIER, l.word) - -local fndef = token("fndef", P"->" + P"=>") -local err = token(l.ERROR, word_match { "function", "end" }) - --- Operators. -local symbol = token("symbol", S("(){}[]")) -local operator = token(l.OPERATOR, '~=' + S('+-*!\\/%^#=<>;:,.')) - --- self ref -local self_var = token("self_ref", "@" * l.word + "self") - -local proper_ident = token("proper_ident", R("AZ") * l.word) - -local tbl_key = token("tbl_key", l.word * ":" + ":" * l.word ) - -M._rules = { - { 'whitespace', ws }, - { 'error', err }, - { 'self', self_var }, - { 'special', special }, - { 'keyword', keyword }, - { 'builtin', builtin }, - { 'identifier', proper_ident + tbl_key + identifier }, - { 'comment', comment }, - { 'number', number }, - { 'string', string }, - { 'fndef', fndef }, - { 'symbol', symbol }, - { 'operator', operator }, - { 'any_char', l.any_char }, -} - -local style_special = { fore = l.colors.light_blue } -local style_fndef = { fore = l.colors.green } - -M._tokenstyles = { - { 'self_ref', style_special }, - { 'proper_ident', l.style_class }, - { 'fndef', style_fndef }, - { 'symbol', style_fndef }, - { 'special', style_special }, - { 'tbl_key', { fore = l.colors.red } }, -} - -return M diff --git a/extra/scintillua/lexers/themes/moon.lua b/extra/scintillua/lexers/themes/moon.lua deleted file mode 100644 index db8d7b26..00000000 --- a/extra/scintillua/lexers/themes/moon.lua +++ /dev/null @@ -1,61 +0,0 @@ --- Copyright 2006-2011 Mitchell mitchellcaladbolg.net. See LICENSE. --- moon lexer theme for Scintillua. - -module('lexer', package.seeall) - -colors = { - green = color('9F', 'FF', '98'), -- - blue = color('94', '95', 'FF'), -- - light_blue = color('98', 'D9', 'FF'), -- - red = color('FF', '98', '98'), -- - bright_red = color("F9", "26", "32"), -- - yellow = color('FF', 'E8', '98'), -- - teal = color('4D', '99', '99'), - white = color('FF', 'FF', 'FF'), -- - black = color('2E', '2E', '2E'), -- - grey = color('92', '92', '92'), -- - purple = color('CB', '98', 'FF'), -- - orange = color('FF', '92', '00'), -- - pink = color("ED", "4E", "78"), -- -} - -style_nothing = style { } -style_char = style { fore = colors.red, bold = true } -style_class = style { fore = colors.light_blue, bold = true } -style_comment = style { fore = colors.grey, } -style_constant = style { fore = colors.teal, bold = true } -style_definition = style { fore = colors.red, bold = true } -style_error = style { fore = colors.white, back = colors.bright_red, bold = true} -style_function = style { fore = colors.orange, bold = true } -style_keyword = style { fore = colors.purple, bold = true } -style_number = style { fore = colors.blue } -style_operator = style { fore = colors.red, bold = true } -style_string = style { fore = colors.yellow, bold = true } -style_preproc = style { fore = colors.light_blue } -style_tag = style { fore = colors.teal, bold = true } -style_type = style { fore = colors.green } -style_variable = style { fore = colors.white, italic = true } -style_embedded = style_tag..{ back = color('44', '44', '44') } -style_identifier = style_nothing - --- Default styles. -local font_face = '!Bitstream Vera Sans Mono' -local font_size = 12 -if WIN32 then - font_face = not GTK and 'Courier New' or '!Courier New' -elseif OSX then - font_face = '!Monaco' - font_size = 12 -end -style_default = style{ - font = font_face, - size = font_size, - fore = colors.white, - back = colors.black -} -style_line_number = style { fore = colors.black, back = colors.grey } -style_bracelight = style { fore = color('66', '99', 'FF'), bold = true } -style_bracebad = style { fore = color('FF', '66', '99'), bold = true } -style_controlchar = style_nothing -style_indentguide = style { fore = colors.grey, back = colors.white } -style_calltip = style { fore = colors.white, back = color('44', '44', '44') } diff --git a/extra/scintillua/moonscript.properties b/extra/scintillua/moonscript.properties deleted file mode 100644 index feff9333..00000000 --- a/extra/scintillua/moonscript.properties +++ /dev/null @@ -1,11 +0,0 @@ - -file.patterns.moon=*.moon -shbang.moon=moon -filter.moon=MoonScript (moon)|$(file.patterns.moon)| - -command.compile.*.moon=moonc "$(FileNameExt)" -command.go.*.moon=moon "$(FileNameExt)" - -tabsize=2 -indent.size=2 -use.tabs=0 \ No newline at end of file From 0262243136fdbb365c4b685202dba42b5452c828 Mon Sep 17 00:00:00 2001 From: AlissaSquared Date: Thu, 7 Jan 2016 17:37:47 -0600 Subject: [PATCH 186/344] Exit with status 1 on linting error --- bin/moonc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bin/moonc b/bin/moonc index 2d707678..99c056b4 100755 --- a/bin/moonc +++ b/bin/moonc @@ -313,15 +313,21 @@ if opts.w then io.stderr:write("\nQuitting...\n") elseif opts.l then + local has_linted_with_error; for _, tuple in pairs(files) do local fname = tuple[1] local res, err = check_compile_or_lint(fname) if res then + has_linted_with_error = true io.stderr:write(res .. "\n\n") elseif err then + has_linted_with_error = true io.stderr:write(fname .. "\n" .. err.. "\n\n") end end + if has_linted_with_error then + os.exit(1) + end else for _, tuple in ipairs(files) do local fname, target = util.unpack(tuple) From f4a2c6c46f0b185b4c188cc48b7346b663ace0c8 Mon Sep 17 00:00:00 2001 From: AlissaSquared Date: Sat, 9 Jan 2016 22:19:32 -0600 Subject: [PATCH 187/344] Added linting documentation --- docs/command_line.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/command_line.md b/docs/command_line.md index 047a0ddd..18743b23 100644 --- a/docs/command_line.md +++ b/docs/command_line.md @@ -134,11 +134,14 @@ A full list of flags can be seen by passing the `-h` or `--help` flag. `moonc` contains a [lint][1] tool for statically detecting potential problems with code. The linter has two tests: detects accessed global variables, -detect unused declared variables. +detect unused declared variables. If the linter detects any issues with a file, +the program will exit with a status of `1`. You can execute the linter with the `-l` flag. When the linting flag is provided only linting takes place and no compiled code is generated. +The linter is compatible with the watch mode (see above) for automatic linting + ```bash moonc -l file1.moon file2.moon ``` From 7758114e00c1269ef6741d6449184c0825043212 Mon Sep 17 00:00:00 2001 From: AlissaSquared Date: Sat, 9 Jan 2016 22:21:10 -0600 Subject: [PATCH 188/344] --- docs/command_line.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/command_line.md b/docs/command_line.md index 18743b23..31fe48ee 100644 --- a/docs/command_line.md +++ b/docs/command_line.md @@ -140,7 +140,7 @@ the program will exit with a status of `1`. You can execute the linter with the `-l` flag. When the linting flag is provided only linting takes place and no compiled code is generated. -The linter is compatible with the watch mode (see above) for automatic linting +The linter is compatible with the watch mode (see above) for automatic linting. ```bash moonc -l file1.moon file2.moon From b3a13435414a2592fc3d336508e89287437c54cd Mon Sep 17 00:00:00 2001 From: nymphium Date: Wed, 20 Jan 2016 02:26:50 +0900 Subject: [PATCH 189/344] refine makefile --- Makefile | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 4435f99a..c63fba5a 100644 --- a/Makefile +++ b/Makefile @@ -1,16 +1,8 @@ -LUA ?= lua5.1 -LUAROCKS ?= luarocks - -ifneq ($(LUA),lua) - LUA_VERSION = $(shell echo $(LUA) | sed -e "s/lua\(.*\)/\1/") - LUAROCKS = luarocks-$(LUA_VERSION) - LUA_PATH_MAKE = $(shell echo "$$LUA_PATH" | sed -e "s/[0-9]\.[0-9]/$(LUA_VERSION)/g") - LUA_CPATH_MAKE = $(shell echo "$$LUA_CPATH" | sed -e "s/[0-9]\.[0-9]/$(LUA_VERSION)/g") -endif - -ifeq ($(LUA),luajit) - LUAROCKS = luarocks-5.1 -endif +LUA ?= lua5.1 +LUA_VERSION = $(shell $(LUA) -e 'print(_VERSION:match("%d%.%d"))') +LUAROCKS = luarocks-$(LUA_VERSION) +LUA_PATH_MAKE = $(shell $(LUAROCKS) path --lr-path) +LUA_CPATH_MAKE = $(shell $(LUAROCKS) path --lr-cpath) .PHONY: test local compile compile_system watch lint count show From e0c37ecb1516b5f381eb8c2937cfaf86d853fb15 Mon Sep 17 00:00:00 2001 From: nymphium Date: Wed, 20 Jan 2016 02:34:41 +0900 Subject: [PATCH 190/344] update for newest master branch From 2dc328bf5c1f0626c06146099a3b29236dbf49a8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 28 Mar 2016 21:18:31 -0700 Subject: [PATCH 191/344] fix typo fixes --- docs/reference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference.md b/docs/reference.md index 960930a0..37e7871e 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1234,7 +1234,7 @@ my_module = import \add from my_module -print add 22 -- equivalent to calling my_module\get 22 +print add 22 -- equivalent to calling my_module\add 22 ``` When handing multiple imports you can substitute the comma with a newline and From d8273d59855ec415718b5617f27beadb0ba322bd Mon Sep 17 00:00:00 2001 From: nymphium Date: Thu, 31 Mar 2016 00:16:51 +0900 Subject: [PATCH 192/344] Fix #255 : add unless - elseif --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 6e75c297..757519d6 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -132,7 +132,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) SwitchElse = key("else") * Body / mark("else"), IfCond = Exp * Assign ^ -1 / format_single_assign, If = key("if") * IfCond * key("then") ^ -1 * Body * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif")) ^ 0 * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else")) ^ -1 / mark("if"), - Unless = key("unless") * IfCond * key("then") ^ -1 * Body * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else")) ^ -1 / mark("unless"), + Unless = key("unless") * IfCond * key("then") ^ -1 * Body * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif")) ^ 0 * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else")) ^ -1 / mark("unless"), While = key("while") * DisableDo * ensure(Exp, PopDo) * key("do") ^ -1 * Body / mark("while"), For = key("for") * DisableDo * ensure(Name * sym("=") * Ct(Exp * sym(",") * Exp * (sym(",") * Exp) ^ -1), PopDo) * key("do") ^ -1 * Body / mark("for"), ForEach = key("for") * Ct(AssignableNameList) * key("in") * DisableDo * ensure(Ct(sym("*") * Exp / mark("unpack") + ExpList), PopDo) * key("do") ^ -1 * Body / mark("foreach"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 87225a4c..c7c836b5 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -155,6 +155,7 @@ build_grammar = wrap_env debug_grammar, (root) -> ((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if" Unless: key"unless" * IfCond * key"then"^-1 * Body * + ((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif")^0 * ((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"unless" While: key"while" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Body / mark"while" From ca33e55ff3b20296a306135690e01f2d09cc7a19 Mon Sep 17 00:00:00 2001 From: nymphium Date: Thu, 31 Mar 2016 05:26:53 +0900 Subject: [PATCH 193/344] solve LUA_PATH overwrite problem --- Makefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index c63fba5a..d1c2b1f7 100644 --- a/Makefile +++ b/Makefile @@ -1,8 +1,8 @@ LUA ?= lua5.1 LUA_VERSION = $(shell $(LUA) -e 'print(_VERSION:match("%d%.%d"))') LUAROCKS = luarocks-$(LUA_VERSION) -LUA_PATH_MAKE = $(shell $(LUAROCKS) path --lr-path) -LUA_CPATH_MAKE = $(shell $(LUAROCKS) path --lr-cpath) +LUA_PATH_MAKE = $(shell $(LUAROCKS) path --lr-path);./?.lua;./?/init.lua +LUA_CPATH_MAKE = $(shell $(LUAROCKS) path --lr-cpath);./?.so .PHONY: test local compile compile_system watch lint count show From a841de51ab326101c6314b6b395e0cd1f70e85a3 Mon Sep 17 00:00:00 2001 From: nymphium Date: Thu, 31 Mar 2016 18:38:56 +0900 Subject: [PATCH 194/344] add spec --- spec/inputs/unless_else.moon | 5 +++++ spec/outputs/unless_else.lua | 7 +++++++ 2 files changed, 12 insertions(+) create mode 100644 spec/inputs/unless_else.moon create mode 100644 spec/outputs/unless_else.lua diff --git a/spec/inputs/unless_else.moon b/spec/inputs/unless_else.moon new file mode 100644 index 00000000..b421d4d0 --- /dev/null +++ b/spec/inputs/unless_else.moon @@ -0,0 +1,5 @@ +if a + unless b + print "hi" + elseif c + print "not hi" diff --git a/spec/outputs/unless_else.lua b/spec/outputs/unless_else.lua new file mode 100644 index 00000000..f05e7517 --- /dev/null +++ b/spec/outputs/unless_else.lua @@ -0,0 +1,7 @@ +if a then + if not (b) then + return print("hi") + elseif c then + return print("not hi") + end +end \ No newline at end of file From d85973411f7187da788191127d943fd681ff4813 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 7 Apr 2016 20:40:46 -0700 Subject: [PATCH 195/344] don't let unpack names reuse ones in scope, fixes #211, fixes #256 --- moonscript/transform/names.lua | 13 ++++++++++++- moonscript/transform/names.moon | 8 +++++++- moonscript/transform/statement.lua | 21 ++++++++++++++++++--- moonscript/transform/statement.moon | 6 ++++-- 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/moonscript/transform/names.lua b/moonscript/transform/names.lua index b498036e..f6af4c46 100644 --- a/moonscript/transform/names.lua +++ b/moonscript/transform/names.lua @@ -101,7 +101,18 @@ do _base_0.__class = _class_0 NameProxy = _class_0 end +local is_name_proxy +is_name_proxy = function(v) + if not (type(v) == "table") then + return false + end + local _exp_0 = v.__class + if LocalName == _exp_0 or NameProxy == _exp_0 then + return true + end +end return { NameProxy = NameProxy, - LocalName = LocalName + LocalName = LocalName, + is_name_proxy = is_name_proxy } diff --git a/moonscript/transform/names.moon b/moonscript/transform/names.moon index 220d862d..4af6aa0d 100644 --- a/moonscript/transform/names.moon +++ b/moonscript/transform/names.moon @@ -41,5 +41,11 @@ class NameProxy else ("name")\format @prefix +is_name_proxy = (v) -> + return false unless type(v) == "table" -{ :NameProxy, :LocalName } + switch v.__class + when LocalName, NameProxy + true + +{ :NameProxy, :LocalName, :is_name_proxy } diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index efc0e723..3e5986f4 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -1,7 +1,10 @@ local Transformer Transformer = require("moonscript.transform.transformer").Transformer -local NameProxy -NameProxy = require("moonscript.transform.names").NameProxy +local NameProxy, LocalName, is_name_proxy +do + local _obj_0 = require("moonscript.transform.names") + NameProxy, LocalName, is_name_proxy = _obj_0.NameProxy, _obj_0.LocalName, _obj_0.is_name_proxy +end local Run, transform_last_stm, implicitly_return, last_stm do local _obj_0 = require("moonscript.transform.statements") @@ -649,6 +652,18 @@ return Transformer({ } } end + local names + do + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = node.names + for _index_0 = 1, #_list_0 do + local n = _list_0[_index_0] + _accum_0[_len_0] = is_name_proxy(n) and n or LocalName(n) or n + _len_0 = _len_0 + 1 + end + names = _accum_0 + end return build.group({ list_name ~= list and build.assign_one(list_name, list) or NOOP, slice_var or NOOP, @@ -658,7 +673,7 @@ return Transformer({ body = { { "assign", - node.names, + names, { NameProxy.index(list_name, index_name) } diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index 1d11560b..50de0e0d 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -1,6 +1,6 @@ import Transformer from require "moonscript.transform.transformer" -import NameProxy from require "moonscript.transform.names" +import NameProxy, LocalName, is_name_proxy from require "moonscript.transform.names" import Run, transform_last_stm, implicitly_return, last_stm from require "moonscript.transform.statements" @@ -400,6 +400,8 @@ Transformer { else {1, {"length", list_name}} + names = [is_name_proxy(n) and n or LocalName(n) or n for n in *node.names] + return build.group { list_name != list and build.assign_one(list_name, list) or NOOP slice_var or NOOP @@ -407,7 +409,7 @@ Transformer { name: index_name bounds: bounds body: { - {"assign", node.names, { NameProxy.index list_name, index_name }} + {"assign", names, { NameProxy.index list_name, index_name }} build.group node.body } } From 716f2d53d621e2034ef3ea4ad6c6a89e0b6a6dd6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 7 Apr 2016 20:41:47 -0700 Subject: [PATCH 196/344] rebuild specs --- spec/outputs/bubbling.lua | 2 +- spec/outputs/comprehension.lua | 4 ++-- spec/outputs/lists.lua | 8 ++++---- spec/outputs/loops.lua | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spec/outputs/bubbling.lua b/spec/outputs/bubbling.lua index 247ff1c2..036d5fb7 100644 --- a/spec/outputs/bubbling.lua +++ b/spec/outputs/bubbling.lua @@ -78,7 +78,7 @@ do ... } for _index_0 = 1, #_list_0 do - x = _list_0[_index_0] + local x = _list_0[_index_0] _accum_0[_len_0] = x _len_0 = _len_0 + 1 end diff --git a/spec/outputs/comprehension.lua b/spec/outputs/comprehension.lua index 7512c5f3..ec3569a1 100644 --- a/spec/outputs/comprehension.lua +++ b/spec/outputs/comprehension.lua @@ -41,7 +41,7 @@ do local _tbl_0 = { } local _list_0 = yes for _index_0 = 1, #_list_0 do - x = _list_0[_index_0] + local x = _list_0[_index_0] local _key_0, _val_0 = unpack(x) _tbl_0[_key_0] = _val_0 end @@ -68,7 +68,7 @@ do } } for _index_0 = 1, #_list_0 do - x = _list_0[_index_0] + local x = _list_0[_index_0] local _key_0, _val_0 = unpack((function() local _accum_0 = { } local _len_0 = 1 diff --git a/spec/outputs/lists.lua b/spec/outputs/lists.lua index 4905ee0c..abb167eb 100644 --- a/spec/outputs/lists.lua +++ b/spec/outputs/lists.lua @@ -161,14 +161,14 @@ do local _accum_0 = { } local _len_0 = 1 for _index_0 = 1, #items do - x = items[_index_0] + local x = items[_index_0] _accum_0[_len_0] = x * 2 _len_0 = _len_0 + 1 end double = _accum_0 end for _index_0 = 1, #double do - x = double[_index_0] + local x = double[_index_0] print(x) end local cut @@ -176,7 +176,7 @@ do local _accum_0 = { } local _len_0 = 1 for _index_0 = 1, #items do - x = items[_index_0] + local x = items[_index_0] if x > 3 then _accum_0[_len_0] = x _len_0 = _len_0 + 1 @@ -189,7 +189,7 @@ do local _accum_0 = { } local _len_0 = 1 for _index_0 = 1, #items do - x = items[_index_0] + local x = items[_index_0] for _index_1 = 1, #items do local y = items[_index_1] _accum_0[_len_0] = x + y diff --git a/spec/outputs/loops.lua b/spec/outputs/loops.lua index db9aef33..57fe40be 100644 --- a/spec/outputs/loops.lua +++ b/spec/outputs/loops.lua @@ -62,7 +62,7 @@ do end x = function() for _index_0 = 1, #hello do - x = hello[_index_0] + local x = hello[_index_0] local _ = y end end From 33dfdad81e451eca659505d11ca3b4ada73e2040 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 7 Apr 2016 20:55:36 -0700 Subject: [PATCH 197/344] remove some duplicated rules #257 --- moonscript/parse.lua | 6 ++++-- moonscript/parse.moon | 10 ++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 757519d6..2ba504a3 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -131,8 +131,10 @@ local build_grammar = wrap_env(debug_grammar, function(root) SwitchCase = key("when") * Ct(ExpList) * key("then") ^ -1 * Body / mark("case"), SwitchElse = key("else") * Body / mark("else"), IfCond = Exp * Assign ^ -1 / format_single_assign, - If = key("if") * IfCond * key("then") ^ -1 * Body * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif")) ^ 0 * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else")) ^ -1 / mark("if"), - Unless = key("unless") * IfCond * key("then") ^ -1 * Body * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif")) ^ 0 * ((Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else")) ^ -1 / mark("unless"), + IfElse = (Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else"), + IfElseIf = (Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif"), + If = key("if") * IfCond * key("then") ^ -1 * Body * IfElseIf ^ 0 * IfElse ^ -1 / mark("if"), + Unless = key("unless") * IfCond * key("then") ^ -1 * Body * IfElseIf ^ 0 * IfElse ^ -1 / mark("unless"), While = key("while") * DisableDo * ensure(Exp, PopDo) * key("do") ^ -1 * Body / mark("while"), For = key("for") * DisableDo * ensure(Name * sym("=") * Ct(Exp * sym(",") * Exp * (sym(",") * Exp) ^ -1), PopDo) * key("do") ^ -1 * Body / mark("for"), ForEach = key("for") * Ct(AssignableNameList) * key("in") * DisableDo * ensure(Ct(sym("*") * Exp / mark("unpack") + ExpList), PopDo) * key("do") ^ -1 * Body / mark("foreach"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index c7c836b5..f7d89268 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -150,13 +150,11 @@ build_grammar = wrap_env debug_grammar, (root) -> IfCond: Exp * Assign^-1 / format_single_assign - If: key"if" * IfCond * key"then"^-1 * Body * - ((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif")^0 * - ((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"if" + IfElse: (Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else" + IfElseIf: (Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif" - Unless: key"unless" * IfCond * key"then"^-1 * Body * - ((Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif")^0 * - ((Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else")^-1 / mark"unless" + If: key"if" * IfCond * key"then"^-1 * Body * IfElseIf^0 * IfElse^-1 / mark"if" + Unless: key"unless" * IfCond * key"then"^-1 * Body * IfElseIf^0 * IfElse^-1 / mark"unless" While: key"while" * DisableDo * ensure(Exp, PopDo) * key"do"^-1 * Body / mark"while" From 4aa8603cf2da440b2017edd349af5324a82ca99f Mon Sep 17 00:00:00 2001 From: Ryan Rion Date: Wed, 13 Apr 2016 18:53:49 -0500 Subject: [PATCH 198/344] bin/moonc: fixes #259 when not using inotify --- bin/moonc | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/moonc b/bin/moonc index 26c6873a..357a0f7b 100755 --- a/bin/moonc +++ b/bin/moonc @@ -247,8 +247,10 @@ local function create_watcher(files) for _, tuple in ipairs(files) do local file = tuple[1] local time = lfs.attributes(file, "modification") - if not mod_time[file] then - mod_time[file] = time + if not time then + mod_time[file] = nil -- file doesn't exist + elseif not mod_time[file] then + mod_time[file] = time -- new file created else if time ~= mod_time[file] then if time > mod_time[file] then From d573ba1953cac34db5548524de0b07c0697f40fe Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 13 Apr 2016 19:30:03 -0700 Subject: [PATCH 199/344] start moving watcher code out --- moonscript/cmd/watcher.lua | 149 ++++++++++++++++++++++++++++++++++++ moonscript/cmd/watcher.moon | 29 +++++++ spec/cmd_spec.moon | 18 ++++- 3 files changed, 194 insertions(+), 2 deletions(-) create mode 100644 moonscript/cmd/watcher.lua create mode 100644 moonscript/cmd/watcher.moon diff --git a/moonscript/cmd/watcher.lua b/moonscript/cmd/watcher.lua new file mode 100644 index 00000000..52be1d54 --- /dev/null +++ b/moonscript/cmd/watcher.lua @@ -0,0 +1,149 @@ +local remove_dupes +remove_dupes = function(list, key_fn) + local seen = { } + return (function() + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #list do + local _continue_0 = false + repeat + local item = list[_index_0] + local key + if key_fn then + key = key_fn(item) + else + key = item + end + if seen[key] then + _continue_0 = true + break + end + seen[key] = true + local _value_0 = item + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + _continue_0 = true + until true + if not _continue_0 then + break + end + end + return _accum_0 + end)() +end +local Watcher +do + local _class_0 + local _base_0 = { } + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function(self, file_list) + self.file_list = file_list + end, + __base = _base_0, + __name = "Watcher" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + Watcher = _class_0 +end +local InotifyWacher +do + local _class_0 + local _parent_0 = Watcher + local _base_0 = { + get_dirs = function(self) + local parse_dir + parse_dir = require("moonscript.cmd.moonc").parse_dir + local dirs + do + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = self.file_list + for _index_0 = 1, #_list_0 do + local _des_0 = _list_0[_index_0] + local file_path + file_path = _des_0[1] + local dir = parse_dir(file_path) + if dir == "" then + dir = "./" + end + local _value_0 = dir + _accum_0[_len_0] = _value_0 + _len_0 = _len_0 + 1 + end + dirs = _accum_0 + end + return remove_dupes(dirs) + end + } + _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) + _class_0 = setmetatable({ + __init = function(self, ...) + return _class_0.__parent.__init(self, ...) + end, + __base = _base_0, + __name = "InotifyWacher", + __parent = _parent_0 + }, { + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + local self = _class_0 + self.available = function(self) + return pcall(function() + return require("inotify") + end) + end + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end + InotifyWacher = _class_0 +end +local SleepWatcher +do + local _class_0 + local _base_0 = { } + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "SleepWatcher" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + SleepWatcher = _class_0 +end +return { + Watcher = Watcher, + SleepWatcher = SleepWatcher, + InotifyWacher = InotifyWacher +} diff --git a/moonscript/cmd/watcher.moon b/moonscript/cmd/watcher.moon new file mode 100644 index 00000000..afb5dd7c --- /dev/null +++ b/moonscript/cmd/watcher.moon @@ -0,0 +1,29 @@ +remove_dupes = (list, key_fn) -> + seen = {} + return for item in *list + key = if key_fn then key_fn item else item + continue if seen[key] + seen[key] = true + item + +-- files is a list of tuples, {source, target} +class Watcher + new: (@file_list) => + + +class InotifyWacher extends Watcher + @available: => + pcall -> require "inotify" + + get_dirs: => + import parse_dir from require "moonscript.cmd.moonc" + dirs = for {file_path} in *@file_list + dir = parse_dir file_path + dir = "./" if dir == "" + dir + + remove_dupes dirs + +class SleepWatcher + +{:Watcher, :SleepWatcher, :InotifyWacher} diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 5e7b0dce..2ab51999 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -9,7 +9,7 @@ describe "moonc", -> dev_loaded = with_dev -> moonc = require "moonscript.cmd.moonc" - same = (fn, a, b)-> + same = (fn, a, b) -> assert.same b, fn a it "should normalize dir", -> @@ -18,7 +18,6 @@ describe "moonc", -> same moonc.normalize_dir, "", "/" -- wrong same moonc.normalize_dir, "hello", "hello/" - it "should parse dir", -> same moonc.parse_dir, "/hello/world/file", "/hello/world/" same moonc.parse_dir, "/hello/world/", "/hello/world/" @@ -63,6 +62,21 @@ describe "moonc", -> moonc.compile_file_text "print'hello'", fname: "test.moon" } + describe "watcher", -> + describe "inotify watcher", -> + it "gets dirs", -> + import InotifyWacher from require "moonscript.cmd.watcher" + watcher = InotifyWacher { + {"hello.moon", "hello.lua"} + {"cool/no.moon", "cool/no.lua"} + } + + assert.same { + "./" + "cool/" + }, watcher\get_dirs! + + describe "stubbed lfs", -> local dirs From c5cbf3315ef6b413bfac7c132e7d04b6b472b0ed Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 13 Apr 2016 19:30:11 -0700 Subject: [PATCH 200/344] rebuild --- moonscript/compile/statement.lua | 2 +- moonscript/dump.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index 8b250d54..d2e9dee3 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -136,7 +136,7 @@ return { current = next end for _index_0 = 4, #node do - cond = node[_index_0] + local cond = node[_index_0] add_clause(cond) end return root diff --git a/moonscript/dump.lua b/moonscript/dump.lua index 8b69ec93..1496ed43 100644 --- a/moonscript/dump.lua +++ b/moonscript/dump.lua @@ -31,7 +31,7 @@ local tree tree = function(block) local _list_0 = block for _index_0 = 1, #_list_0 do - value = _list_0[_index_0] + local value = _list_0[_index_0] print(flat_value(value)) end end From fdfd314ebc38b7034a7fe956e8854fb35da8cd02 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 13 Apr 2016 19:47:27 -0700 Subject: [PATCH 201/344] move inotify watcher to library --- bin/moonc | 90 ++++++-------------- moonscript/cmd/watcher.moon | 29 ------- moonscript/cmd/{watcher.lua => watchers.lua} | 72 +++++++++++++++- moonscript/cmd/watchers.moon | 61 +++++++++++++ 4 files changed, 155 insertions(+), 97 deletions(-) delete mode 100644 moonscript/cmd/watcher.moon rename moonscript/cmd/{watcher.lua => watchers.lua} (61%) create mode 100644 moonscript/cmd/watchers.moon diff --git a/bin/moonc b/bin/moonc index 357a0f7b..bc246747 100755 --- a/bin/moonc +++ b/bin/moonc @@ -194,76 +194,38 @@ local function create_watcher(files) inotify = require "inotify" end) - if inotify then - local dirs = {} + local watchers = require("moonscript.cmd.watchers") - for _, tuple in ipairs(files) do - local dir = parse_dir(tuple[1]) - if dir == "" then - dir = "./" - end - table.insert(dirs, dir) - end - - dirs = remove_dups(dirs) - - return coroutine.wrap(function() - io.stderr:write(("%s with inotify [%s]"):format(msg, plural(#dirs, "dir")) .. "\n") - - local wd_table = {} - local handle = inotify.init() - for _, dir in ipairs(dirs) do - local wd = handle:addwatch(dir, inotify.IN_CLOSE_WRITE, inotify.IN_MOVED_TO) - wd_table[wd] = dir - end - - while true do - local events = handle:read() - if not events then - break - end + if watchers.InotifyWacher:available() then + return watchers.InotifyWacher(files):each_update() + end - for _, ev in ipairs(events) do - local fname = ev.name - if fname:match("%.moon$") then - local dir = wd_table[ev.wd] - if dir ~= "./" then - fname = dir .. fname + -- poll the filesystem instead + local sleep = get_sleep_func() + return coroutine.wrap(function() + io.stderr:write(("%s with polling [%s]"):format(msg, plural(#files, "file")) .. "\n") + + local mod_time = {} + while true do + for _, tuple in ipairs(files) do + local file = tuple[1] + local time = lfs.attributes(file, "modification") + if not time then + mod_time[file] = nil -- file doesn't exist + elseif not mod_time[file] then + mod_time[file] = time -- new file created + else + if time ~= mod_time[file] then + if time > mod_time[file] then + coroutine.yield(file) + mod_time[file] = time end - -- TODO: check to make sure the file was in the original set - coroutine.yield(fname) end end end - end) - else - -- poll the filesystem instead - local sleep = get_sleep_func() - return coroutine.wrap(function() - io.stderr:write(("%s with polling [%s]"):format(msg, plural(#files, "file")) .. "\n") - - local mod_time = {} - while true do - for _, tuple in ipairs(files) do - local file = tuple[1] - local time = lfs.attributes(file, "modification") - if not time then - mod_time[file] = nil -- file doesn't exist - elseif not mod_time[file] then - mod_time[file] = time -- new file created - else - if time ~= mod_time[file] then - if time > mod_time[file] then - coroutine.yield(file) - mod_time[file] = time - end - end - end - end - sleep(polling_rate) - end - end) - end + sleep(polling_rate) + end + end) end diff --git a/moonscript/cmd/watcher.moon b/moonscript/cmd/watcher.moon deleted file mode 100644 index afb5dd7c..00000000 --- a/moonscript/cmd/watcher.moon +++ /dev/null @@ -1,29 +0,0 @@ -remove_dupes = (list, key_fn) -> - seen = {} - return for item in *list - key = if key_fn then key_fn item else item - continue if seen[key] - seen[key] = true - item - --- files is a list of tuples, {source, target} -class Watcher - new: (@file_list) => - - -class InotifyWacher extends Watcher - @available: => - pcall -> require "inotify" - - get_dirs: => - import parse_dir from require "moonscript.cmd.moonc" - dirs = for {file_path} in *@file_list - dir = parse_dir file_path - dir = "./" if dir == "" - dir - - remove_dupes dirs - -class SleepWatcher - -{:Watcher, :SleepWatcher, :InotifyWacher} diff --git a/moonscript/cmd/watcher.lua b/moonscript/cmd/watchers.lua similarity index 61% rename from moonscript/cmd/watcher.lua rename to moonscript/cmd/watchers.lua index 52be1d54..21279baa 100644 --- a/moonscript/cmd/watcher.lua +++ b/moonscript/cmd/watchers.lua @@ -31,10 +31,16 @@ remove_dupes = function(list, key_fn) return _accum_0 end)() end +local plural +plural = function(count, word) + return tostring(count) .. " " .. tostring(word) .. tostring(count == 1 and "" or "s") +end local Watcher do local _class_0 - local _base_0 = { } + local _base_0 = { + start_msg = "Starting watch loop (Ctrl-C to exit)" + } _base_0.__index = _base_0 _class_0 = setmetatable({ __init = function(self, file_list) @@ -81,6 +87,46 @@ do dirs = _accum_0 end return remove_dupes(dirs) + end, + each_update = function(self) + return coroutine.wrap(function() + local dirs = self:get_dirs() + io.stderr:write(tostring(self.start_msg) .. " with inotify [" .. tostring(plural(#dirs, "dir")) .. "]\n") + local wd_table = { } + local inotify = require("inotify") + local handle = inotify.init() + for _index_0 = 1, #dirs do + local dir = dirs[_index_0] + local wd = handle:addwatch(dir, inotify.IN_CLOSE_WRITE, inotify.IN_MOVED_TO) + wd_table[wd] = dir + end + while true do + local events = handle:read() + if not (events) then + break + end + for _index_0 = 1, #events do + local _continue_0 = false + repeat + local ev = events[_index_0] + local fname = ev.name + if not (fname:match("%.moon$")) then + _continue_0 = true + break + end + local dir = wd_table[ev.wd] + if dir ~= "./" then + fname = dir .. fname + end + coroutine.yield(fname) + _continue_0 = true + until true + if not _continue_0 then + break + end + end + end + end) end } _base_0.__index = _base_0 @@ -125,14 +171,29 @@ end local SleepWatcher do local _class_0 + local _parent_0 = Watcher local _base_0 = { } _base_0.__index = _base_0 + setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ - __init = function() end, + __init = function(self, ...) + return _class_0.__parent.__init(self, ...) + end, __base = _base_0, - __name = "SleepWatcher" + __name = "SleepWatcher", + __parent = _parent_0 }, { - __index = _base_0, + __index = function(cls, name) + local val = rawget(_base_0, name) + if val == nil then + local parent = rawget(cls, "__parent") + if parent then + return parent[name] + end + else + return val + end + end, __call = function(cls, ...) local _self_0 = setmetatable({}, _base_0) cls.__init(_self_0, ...) @@ -140,6 +201,9 @@ do end }) _base_0.__class = _class_0 + if _parent_0.__inherited then + _parent_0.__inherited(_parent_0, _class_0) + end SleepWatcher = _class_0 end return { diff --git a/moonscript/cmd/watchers.moon b/moonscript/cmd/watchers.moon new file mode 100644 index 00000000..6b91aa97 --- /dev/null +++ b/moonscript/cmd/watchers.moon @@ -0,0 +1,61 @@ +remove_dupes = (list, key_fn) -> + seen = {} + return for item in *list + key = if key_fn then key_fn item else item + continue if seen[key] + seen[key] = true + item + +plural = (count, word) -> + "#{count} #{word}#{count == 1 and "" or "s"}" + +-- files is a list of tuples, {source, target} +class Watcher + start_msg: "Starting watch loop (Ctrl-C to exit)" + new: (@file_list) => + +class InotifyWacher extends Watcher + @available: => + pcall -> require "inotify" + + get_dirs: => + import parse_dir from require "moonscript.cmd.moonc" + dirs = for {file_path} in *@file_list + dir = parse_dir file_path + dir = "./" if dir == "" + dir + + remove_dupes dirs + + -- creates an iterator that yields a file every time it's updated + -- TODO: detect when new files are added to directories + each_update: => + coroutine.wrap -> + dirs = @get_dirs! + + io.stderr\write "#{@start_msg} with inotify [#{plural #dirs, "dir"}]\n" + wd_table = {} + + inotify = require "inotify" + handle = inotify.init! + + for dir in *dirs + wd = handle\addwatch dir, inotify.IN_CLOSE_WRITE, inotify.IN_MOVED_TO + wd_table[wd] = dir + + while true + events = handle\read! + break unless events -- error? + + for ev in *events + fname = ev.name + continue unless fname\match "%.moon$" + dir = wd_table[ev.wd] + fname = dir .. fname if dir != "./" + + -- TODO: check to make sure the file was in the original set + coroutine.yield(fname) + +class SleepWatcher extends Watcher + +{:Watcher, :SleepWatcher, :InotifyWacher} From 1a776e30e0afbbe2b7f780d64c1405057c75b607 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 13 Apr 2016 19:47:37 -0700 Subject: [PATCH 202/344] make error message from loader more useful --- spec/helpers.moon | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/helpers.moon b/spec/helpers.moon index 42d578b3..45545b91 100644 --- a/spec/helpers.moon +++ b/spec/helpers.moon @@ -30,7 +30,8 @@ with_dev = (fn) -> mod\match("moon%.") or mod == "moon" if testable - dev_cache[mod] = assert(loadfile(assert loader mod))! + fname = assert loader(mod), "failed to find module: #{mod}" + dev_cache[mod] = assert(loadfile fname)! return dev_cache[mod] old_require mod From e251fa4a0241cce5220666c44daec80479c89f12 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 13 Apr 2016 19:49:16 -0700 Subject: [PATCH 203/344] misc --- moonscript/cmd/watchers.moon | 2 +- spec/cmd_spec.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/cmd/watchers.moon b/moonscript/cmd/watchers.moon index 6b91aa97..b9146bc6 100644 --- a/moonscript/cmd/watchers.moon +++ b/moonscript/cmd/watchers.moon @@ -54,7 +54,7 @@ class InotifyWacher extends Watcher fname = dir .. fname if dir != "./" -- TODO: check to make sure the file was in the original set - coroutine.yield(fname) + coroutine.yield fname class SleepWatcher extends Watcher diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 2ab51999..63e88e35 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -65,7 +65,7 @@ describe "moonc", -> describe "watcher", -> describe "inotify watcher", -> it "gets dirs", -> - import InotifyWacher from require "moonscript.cmd.watcher" + import InotifyWacher from require "moonscript.cmd.watchers" watcher = InotifyWacher { {"hello.moon", "hello.lua"} {"cool/no.moon", "cool/no.lua"} From 4733598583c7a3f09da493d813028b054ffb7349 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 13 Apr 2016 20:05:51 -0700 Subject: [PATCH 204/344] move polling watcher --- bin/moonc | 59 +--------------------------------- moonscript-dev-1.rockspec | 1 + moonscript/cmd/watchers.lua | 62 ++++++++++++++++++++++++++++++++++-- moonscript/cmd/watchers.moon | 45 +++++++++++++++++++++++++- 4 files changed, 105 insertions(+), 62 deletions(-) diff --git a/bin/moonc b/bin/moonc index bc246747..30469377 100755 --- a/bin/moonc +++ b/bin/moonc @@ -9,8 +9,6 @@ local opts, ind = alt_getopt.get_opts(arg, "lvhwt:o:pTXb", { local read_stdin = arg[1] == "--" -local polling_rate = 1.0 - local help = [[Usage: %s [options] files... -h Print this message @@ -163,72 +161,17 @@ if opts.o and #files > 1 then print_help("-o can not be used with multiple input files") end -local function get_sleep_func() - local sleep - if not pcall(function() - local socket = require "socket" - sleep = socket.sleep - end) then - -- This is set by moonc.c in windows binaries - sleep = require("moonscript")._sleep - end - if not sleep then - error("Missing sleep function; install LuaSocket") - end - return sleep -end - -local function plural(count, word) - if count ~= 1 then - word = word .. "s" - end - return table.concat({count, word}, " ") -end - -- returns an iterator that returns files that have been updated local function create_watcher(files) - local msg = "Starting watch loop (Ctrl-C to exit)" - - local inotify - pcall(function() - inotify = require "inotify" - end) - local watchers = require("moonscript.cmd.watchers") if watchers.InotifyWacher:available() then return watchers.InotifyWacher(files):each_update() end - -- poll the filesystem instead - local sleep = get_sleep_func() - return coroutine.wrap(function() - io.stderr:write(("%s with polling [%s]"):format(msg, plural(#files, "file")) .. "\n") - - local mod_time = {} - while true do - for _, tuple in ipairs(files) do - local file = tuple[1] - local time = lfs.attributes(file, "modification") - if not time then - mod_time[file] = nil -- file doesn't exist - elseif not mod_time[file] then - mod_time[file] = time -- new file created - else - if time ~= mod_time[file] then - if time > mod_time[file] then - coroutine.yield(file) - mod_time[file] = time - end - end - end - end - sleep(polling_rate) - end - end) + return watchers.SleepWatcher(files):each_update() end - if opts.w then -- build function to check for lint or compile in watch local handle_file diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index b3ff2432..c4388d0a 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -30,6 +30,7 @@ build = { ["moonscript.cmd.coverage"] = "moonscript/cmd/coverage.lua", ["moonscript.cmd.lint"] = "moonscript/cmd/lint.lua", ["moonscript.cmd.moonc"] = "moonscript/cmd/moonc.lua", + ["moonscript.cmd.watchers"] = "moonscript/cmd/watchers.lua", ["moonscript.compile"] = "moonscript/compile.lua", ["moonscript.compile.statement"] = "moonscript/compile/statement.lua", ["moonscript.compile.value"] = "moonscript/compile/value.lua", diff --git a/moonscript/cmd/watchers.lua b/moonscript/cmd/watchers.lua index 21279baa..651011b2 100644 --- a/moonscript/cmd/watchers.lua +++ b/moonscript/cmd/watchers.lua @@ -39,7 +39,10 @@ local Watcher do local _class_0 local _base_0 = { - start_msg = "Starting watch loop (Ctrl-C to exit)" + start_msg = "Starting watch loop (Ctrl-C to exit)", + print_start = function(self, mode, misc) + return io.stderr:write(tostring(self.start_msg) .. " with " .. tostring(mode) .. " [" .. tostring(misc) .. "]\n") + end } _base_0.__index = _base_0 _class_0 = setmetatable({ @@ -91,7 +94,7 @@ do each_update = function(self) return coroutine.wrap(function() local dirs = self:get_dirs() - io.stderr:write(tostring(self.start_msg) .. " with inotify [" .. tostring(plural(#dirs, "dir")) .. "]\n") + self:print_start("inotify", plural(#dirs, "dir")) local wd_table = { } local inotify = require("inotify") local handle = inotify.init() @@ -172,7 +175,60 @@ local SleepWatcher do local _class_0 local _parent_0 = Watcher - local _base_0 = { } + local _base_0 = { + polling_rate = 1.0, + get_sleep_func = function(self) + local sleep + pcall(function() + sleep = require("socket").sleep + end) + sleep = sleep or require("moonscript")._sleep + if not (sleep) then + error("Missing sleep function; install LuaSocket") + end + return sleep + end, + each_update = function(self) + return coroutine.wrap(function() + local lfs = require("lfs") + local sleep = self:get_sleep_func() + self:print_start("polling", plural(#self.file_list, "files")) + local mod_time = { } + while true do + local _list_0 = self.file_list + for _index_0 = 1, #_list_0 do + local _continue_0 = false + repeat + local _des_0 = _list_0[_index_0] + local file + file = _des_0[1] + local time = lfs.attributes(file, "modification") + print(file, time) + if not (time) then + mod_time[file] = nil + _continue_0 = true + break + end + if not (mod_time[file]) then + mod_time[file] = time + _continue_0 = true + break + end + if time > mod_time[file] then + mod_time[file] = time + coroutine.yield(file) + end + _continue_0 = true + until true + if not _continue_0 then + break + end + end + sleep(self.polling_rate) + end + end) + end + } _base_0.__index = _base_0 setmetatable(_base_0, _parent_0.__base) _class_0 = setmetatable({ diff --git a/moonscript/cmd/watchers.moon b/moonscript/cmd/watchers.moon index b9146bc6..b021e80e 100644 --- a/moonscript/cmd/watchers.moon +++ b/moonscript/cmd/watchers.moon @@ -14,6 +14,9 @@ class Watcher start_msg: "Starting watch loop (Ctrl-C to exit)" new: (@file_list) => + print_start: (mode, misc) => + io.stderr\write "#{@start_msg} with #{mode} [#{misc}]\n" + class InotifyWacher extends Watcher @available: => pcall -> require "inotify" @@ -33,7 +36,8 @@ class InotifyWacher extends Watcher coroutine.wrap -> dirs = @get_dirs! - io.stderr\write "#{@start_msg} with inotify [#{plural #dirs, "dir"}]\n" + @print_start "inotify", plural #dirs, "dir" + wd_table = {} inotify = require "inotify" @@ -57,5 +61,44 @@ class InotifyWacher extends Watcher coroutine.yield fname class SleepWatcher extends Watcher + polling_rate: 1.0 + + -- the windows mooonscript binaries provide their own sleep function + get_sleep_func: => + local sleep + + pcall -> + sleep = require("socket").sleep + + -- TODO: this is also loading moonloader, which isn't intentional + sleep or= require("moonscript")._sleep + error "Missing sleep function; install LuaSocket" unless sleep + sleep + + each_update: => + coroutine.wrap -> + lfs = require "lfs" + sleep = @get_sleep_func! + + @print_start "polling", plural #@file_list, "files" + mod_time = {} + + while true + for {file} in *@file_list + time = lfs.attributes file, "modification" + print file, time + unless time -- file no longer exists + mod_time[file] = nil + continue + + unless mod_time[file] -- file time scanned + mod_time[file] = time + continue + + if time > mod_time[file] + mod_time[file] = time + coroutine.yield file + + sleep @polling_rate {:Watcher, :SleepWatcher, :InotifyWacher} From 94e5a32369b0cf3b7f943dcd92ef61aae329eb98 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 15 Apr 2016 23:51:27 -0700 Subject: [PATCH 205/344] start writing arg parser --- moonscript/cmd/args.lua | 43 ++++++++++++++++++++++++++++++++++++++++ moonscript/cmd/args.moon | 27 +++++++++++++++++++++++++ spec/cmd_spec.moon | 13 ++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 moonscript/cmd/args.lua create mode 100644 moonscript/cmd/args.moon diff --git a/moonscript/cmd/args.lua b/moonscript/cmd/args.lua new file mode 100644 index 00000000..812b1ae6 --- /dev/null +++ b/moonscript/cmd/args.lua @@ -0,0 +1,43 @@ +local parse_arguments +parse_arguments = function(spec, args) + local out = { } + local remaining = { } + local last_flag = nil + for _index_0 = 1, #args do + local _continue_0 = false + repeat + local arg = args[_index_0] + if last_flag then + out[last_flag] = arg + _continue_0 = true + break + end + do + local flag = arg:match("-(%w+)") + if flag then + do + local short_name = spec[flag] + if short_name then + out[short_name] = true + else + for char in flag:gmatch(".") do + out[char] = true + end + end + end + _continue_0 = true + break + end + end + table.insert(remaining, arg) + _continue_0 = true + until true + if not _continue_0 then + break + end + end + return out, remaining +end +return { + parse_arguments = parse_arguments +} diff --git a/moonscript/cmd/args.moon b/moonscript/cmd/args.moon new file mode 100644 index 00000000..8ad0a9ca --- /dev/null +++ b/moonscript/cmd/args.moon @@ -0,0 +1,27 @@ + +parse_arguments = (spec, args) -> + out = {} + + remaining = {} + last_flag = nil + + for arg in *args + if last_flag + out[last_flag] = arg + continue + + if flag = arg\match "-(%w+)" + if short_name = spec[flag] + out[short_name] = true + else + for char in flag\gmatch "." + out[char] = true + continue + + table.insert remaining, arg + + out, remaining + + + +{:parse_arguments} diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 63e88e35..6a117580 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -76,6 +76,19 @@ describe "moonc", -> "cool/" }, watcher\get_dirs! + describe "parse args", -> + import parse_arguments from require "moonscript.cmd.args" + + it "parses arguments", -> + out, res = parse_arguments { + print: "p" + }, {"hello", "word", "-gap"} + + assert.same { + g: true + a: true + p: true + }, out describe "stubbed lfs", -> local dirs From 7c17aefbc7e9dc13a03c1ef03400cfeff749cb1b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 16 Apr 2016 23:58:36 -0700 Subject: [PATCH 206/344] parse spec --- moonscript/cmd/args.lua | 26 +++++++++++++++++++++++++- moonscript/cmd/args.moon | 22 +++++++++++++++++++++- spec/cmd_spec.moon | 12 +++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/moonscript/cmd/args.lua b/moonscript/cmd/args.lua index 812b1ae6..1f760000 100644 --- a/moonscript/cmd/args.lua +++ b/moonscript/cmd/args.lua @@ -1,5 +1,27 @@ +local parse_spec +parse_spec = function(spec) + local flags, words + if type(spec) == "table" then + flags, words = unpack(spec), spec + else + flags, words = spec, { } + end + assert("no flags for arguments") + local out = { } + for part in flags:gmatch("%w:?") do + if part:match(":$") then + out[part:sub(1, 1)] = { + value = true + } + else + out[part] = { } + end + end + return out +end local parse_arguments parse_arguments = function(spec, args) + spec = parse_spec(spec) local out = { } local remaining = { } local last_flag = nil @@ -7,6 +29,7 @@ parse_arguments = function(spec, args) local _continue_0 = false repeat local arg = args[_index_0] + local group = { } if last_flag then out[last_flag] = arg _continue_0 = true @@ -39,5 +62,6 @@ parse_arguments = function(spec, args) return out, remaining end return { - parse_arguments = parse_arguments + parse_arguments = parse_arguments, + parse_spec = parse_spec } diff --git a/moonscript/cmd/args.moon b/moonscript/cmd/args.moon index 8ad0a9ca..5bcbe37d 100644 --- a/moonscript/cmd/args.moon +++ b/moonscript/cmd/args.moon @@ -1,11 +1,31 @@ +parse_spec = (spec) -> + flags, words = if type(spec) == "table" + unpack(spec), spec + else + spec, {} + + assert "no flags for arguments" + + out = {} + for part in flags\gmatch "%w:?" + if part\match ":$" + out[part\sub 1,1] = { value: true } + else + out[part] = {} + + out + parse_arguments = (spec, args) -> + spec = parse_spec spec + out = {} remaining = {} last_flag = nil for arg in *args + group = {} if last_flag out[last_flag] = arg continue @@ -24,4 +44,4 @@ parse_arguments = (spec, args) -> -{:parse_arguments} +{ :parse_arguments, :parse_spec } diff --git a/spec/cmd_spec.moon b/spec/cmd_spec.moon index 6a117580..039973d5 100644 --- a/spec/cmd_spec.moon +++ b/spec/cmd_spec.moon @@ -77,10 +77,20 @@ describe "moonc", -> }, watcher\get_dirs! describe "parse args", -> - import parse_arguments from require "moonscript.cmd.args" + it "parses spec", -> + import parse_spec from require "moonscript.cmd.args" + spec = parse_spec "lt:o:X" + assert.same { + X: {} + o: {value: true} + t: {value: true} + l: {} + }, spec it "parses arguments", -> + import parse_arguments from require "moonscript.cmd.args" out, res = parse_arguments { + "ga:p" print: "p" }, {"hello", "word", "-gap"} From b2df8e6fc0fdc0ae7d07de5eba93f64cbb5c18f1 Mon Sep 17 00:00:00 2001 From: Ryan Rion Date: Sat, 30 Apr 2016 16:17:38 -0500 Subject: [PATCH 207/344] docs/api.md: Fix typo (finder => finer) (#268) --- docs/api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index a0aa7f55..c59f6f70 100644 --- a/docs/api.md +++ b/docs/api.md @@ -98,7 +98,7 @@ is a character offset from the original MoonScript source. ## Programmatically Compiling -If you need finder grained control over the compilation process you can use the +If you need finer grained control over the compilation process you can use the raw parse and compile modules. Parsing converts a string of MoonScript into an abstract syntax tree. Compiling From 9302ef965724f77d05d7bc9eb21cea2d21b15094 Mon Sep 17 00:00:00 2001 From: Ryan Rion Date: Mon, 2 May 2016 19:55:15 -0500 Subject: [PATCH 208/344] moonscript/cmd/args.moon: fix `unpack()` Lua 5.2+ (#269) --- moonscript/cmd/args.lua | 2 ++ moonscript/cmd/args.moon | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/moonscript/cmd/args.lua b/moonscript/cmd/args.lua index 1f760000..68b06fdc 100644 --- a/moonscript/cmd/args.lua +++ b/moonscript/cmd/args.lua @@ -1,3 +1,5 @@ +local unpack +unpack = require("moonscript.util").unpack local parse_spec parse_spec = function(spec) local flags, words diff --git a/moonscript/cmd/args.moon b/moonscript/cmd/args.moon index 5bcbe37d..ac65c0e3 100644 --- a/moonscript/cmd/args.moon +++ b/moonscript/cmd/args.moon @@ -1,4 +1,4 @@ - +import unpack from require "moonscript.util" parse_spec = (spec) -> flags, words = if type(spec) == "table" unpack(spec), spec From 2d96e11ed344fbfc304acb7e42a011994833b9e7 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 13 Jul 2016 10:19:30 -0700 Subject: [PATCH 209/344] use most recent busted fixes #278 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 48d84c8c..11808bda 100644 --- a/.travis.yml +++ b/.travis.yml @@ -15,7 +15,7 @@ before_install: - source .travis/setenv_lua.sh install: - - luarocks install https://luarocks.org/manifests/olivine-labs/busted-2.0.rc11-0.rockspec + - luarocks install busted - luarocks install loadkit - luarocks make From c49fdca7aa7165040a6a3e94f0a1721c618a498d Mon Sep 17 00:00:00 2001 From: Gskartwii Date: Sun, 25 Sep 2016 20:18:42 +0300 Subject: [PATCH 210/344] Ignore directories named *.moon (#290) --- bin/moonc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bin/moonc b/bin/moonc index 30469377..dfd56969 100755 --- a/bin/moonc +++ b/bin/moonc @@ -70,9 +70,7 @@ local function scan_directory(root, collected) if lfs.attributes(full_path, "mode") == "directory" then scan_directory(full_path, collected) - end - - if fname:match("%.moon$") then + elseif fname:match("%.moon$") then table.insert(collected, full_path) end end From 32d650a5ff91409e5b7ec0991db228857016f7e9 Mon Sep 17 00:00:00 2001 From: Jon Allen Date: Sun, 25 Sep 2016 12:21:34 -0500 Subject: [PATCH 211/344] switched the alt_getopt module import to work with newer versions of lua (#289) --- bin/moon | 2 +- bin/moon.moon | 2 +- bin/splat.moon | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/bin/moon b/bin/moon index a4892bd3..9b7593ba 100755 --- a/bin/moon +++ b/bin/moon @@ -1,5 +1,5 @@ #!/usr/bin/env lua -require("alt_getopt") +local alt_getopt = require("alt_getopt") local moonscript = require("moonscript.base") local util = require("moonscript.util") local errors = require("moonscript.errors") diff --git a/bin/moon.moon b/bin/moon.moon index 853cf19b..eb36eab5 100644 --- a/bin/moon.moon +++ b/bin/moon.moon @@ -1,5 +1,5 @@ -require "alt_getopt" +alt_getopt = require "alt_getopt" moonscript = require "moonscript.base" util = require "moonscript.util" diff --git a/bin/splat.moon b/bin/splat.moon index 506a54ae..df523c14 100755 --- a/bin/splat.moon +++ b/bin/splat.moon @@ -2,8 +2,8 @@ -- concatenate a collection of lua modules into one -require "lfs" -require "alt_getopt" +lfs = require "lfs" +alt_getopt = require "alt_getopt" import insert, concat from table import dump, split from require "moonscript.util" From 28849bc8782df355ec70615b67a5716947d387b8 Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 25 Sep 2016 12:22:26 -0500 Subject: [PATCH 212/344] moonscript/: remove unneeded variables on stack (#271) --- moonscript/compile/statement.lua | 10 +++++----- moonscript/compile/statement.moon | 10 +++++----- moonscript/compile/value.lua | 6 +++--- moonscript/compile/value.moon | 6 +++--- moonscript/transform/class.lua | 2 +- moonscript/transform/class.moon | 2 +- moonscript/transform/statement.lua | 10 +++++----- moonscript/transform/statement.moon | 10 +++++----- moonscript/transform/value.lua | 4 ++-- moonscript/transform/value.moon | 4 ++-- 10 files changed, 32 insertions(+), 32 deletions(-) diff --git a/moonscript/compile/statement.lua b/moonscript/compile/statement.lua index d2e9dee3..7f1c9063 100644 --- a/moonscript/compile/statement.lua +++ b/moonscript/compile/statement.lua @@ -57,7 +57,7 @@ return { end end, assign = function(self, node) - local _, names, values = unpack(node) + local names, values = unpack(node, 2) local undeclared = self:declare(names) local declare = "local " .. concat(undeclared, ", ") local has_fndef = false @@ -150,7 +150,7 @@ return { end end, ["while"] = function(self, node) - local _, cond, block = unpack(node) + local cond, block = unpack(node, 2) do local _with_0 = self:block(self:line("while ", self:value(cond), " do")) _with_0:stms(block) @@ -158,7 +158,7 @@ return { end end, ["for"] = function(self, node) - local _, name, bounds, block = unpack(node) + local name, bounds, block = unpack(node, 2) local loop = self:line("for ", self:name(name), " = ", self:value({ "explist", unpack(bounds) @@ -173,7 +173,7 @@ return { end end, foreach = function(self, node) - local _, names, exps, block = unpack(node) + local names, exps, block = unpack(node, 2) local loop do local _with_0 = self:line() @@ -210,7 +210,7 @@ return { end end, export = function(self, node) - local _, names = unpack(node) + local names = unpack(node, 2) if type(names) == "string" then if names == "*" then self.export_all = true diff --git a/moonscript/compile/statement.moon b/moonscript/compile/statement.moon index bddd3dca..25f53fd0 100644 --- a/moonscript/compile/statement.moon +++ b/moonscript/compile/statement.moon @@ -26,7 +26,7 @@ import unpack from require "moonscript.util" \append_list [@name name for name in *names], ", " assign: (node) => - _, names, values = unpack node + names, values = unpack node, 2 undeclared = @declare names declare = "local " .. concat(undeclared, ", ") @@ -83,12 +83,12 @@ import unpack from require "moonscript.util" \stms block while: (node) => - _, cond, block = unpack node + cond, block = unpack node, 2 with @block @line "while ", @value(cond), " do" \stms block for: (node) => - _, name, bounds, block = unpack node + name, bounds, block = unpack node, 2 loop = @line "for ", @name(name), " = ", @value({"explist", unpack bounds}), " do" with @block loop \declare {name} @@ -97,7 +97,7 @@ import unpack from require "moonscript.util" -- for x in y ... -- {"foreach", {names...}, {exp...}, body} foreach: (node) => - _, names, exps, block = unpack node + names, exps, block = unpack node, 2 loop = with @line! \append "for " @@ -112,7 +112,7 @@ import unpack from require "moonscript.util" \stms block export: (node) => - _, names = unpack node + names = unpack node, 2 if type(names) == "string" if names == "*" @export_all = true diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 2c457dc5..ce20dadd 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -71,7 +71,7 @@ return { return self:line("(", self:value(node[2]), ")") end, string = function(self, node) - local _, delim, inner = unpack(node) + local delim, inner = unpack(node, 2) local end_delim = delim:gsub("%[", "]") if delim == "'" or delim == '"' then inner = inner:gsub("[\r\n]", string_chars) @@ -133,7 +133,7 @@ return { return self:line(callee_value, actions) end, fndef = function(self, node) - local _, args, whitelist, arrow, block = unpack(node) + local args, whitelist, arrow, block = unpack(node, 2) local default_args = { } local self_args = { } local arg_names @@ -238,7 +238,7 @@ return { end end, table = function(self, node) - local _, items = unpack(node) + local items = unpack(node, 2) do local _with_0 = self:block("{", "}") local format_line diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 02ae94c8..7cfdda4e 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -40,7 +40,7 @@ string_chars = { @line "(", @value(node[2]), ")" string: (node) => - _, delim, inner = unpack node + delim, inner = unpack node, 2 end_delim = delim\gsub "%[", "]" if delim == "'" or delim == '"' inner = inner\gsub "[\r\n]", string_chars @@ -91,7 +91,7 @@ string_chars = { @line callee_value, actions fndef: (node) => - _, args, whitelist, arrow, block = unpack node + args, whitelist, arrow, block = unpack node, 2 default_args = {} self_args = {} @@ -137,7 +137,7 @@ string_chars = { .header = "function("..concat(arg_names, ", ")..")" table: (node) => - _, items = unpack node + items = unpack node, 2 with @block "{", "}" format_line = (tuple) -> if #tuple == 2 diff --git a/moonscript/transform/class.lua b/moonscript/transform/class.lua index f55dce00..74f93c59 100644 --- a/moonscript/transform/class.lua +++ b/moonscript/transform/class.lua @@ -106,7 +106,7 @@ super_scope = function(value, t, key) } end return function(self, node, ret, parent_assign) - local _, name, parent_val, body = unpack(node) + local name, parent_val, body = unpack(node, 2) if parent_val == "" then parent_val = nil end diff --git a/moonscript/transform/class.moon b/moonscript/transform/class.moon index 2a952657..7451375b 100644 --- a/moonscript/transform/class.moon +++ b/moonscript/transform/class.moon @@ -76,7 +76,7 @@ super_scope = (value, t, key) -> } (node, ret, parent_assign) => - _, name, parent_val, body = unpack node + name, parent_val, body = unpack node, 2 parent_val = nil if parent_val == "" parent_cls_name = NameProxy "parent" diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index 3e5986f4..6c8a60bf 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -342,7 +342,7 @@ return Transformer({ end end, update = function(self, node) - local _, name, op, exp = unpack(node) + local name, op, exp = unpack(node, 2) local op_final = op:match("^(.+)=$") if not op_final then error("Unknown op: " .. op) @@ -361,7 +361,7 @@ return Transformer({ }) end, import = function(self, node) - local _, names, source = unpack(node) + local names, source = unpack(node, 2) local table_values do local _accum_0 = { } @@ -402,7 +402,7 @@ return Transformer({ } end, comprehension = function(self, node, action) - local _, exp, clauses = unpack(node) + local exp, clauses = unpack(node, 2) action = action or function(exp) return { exp @@ -492,7 +492,7 @@ return Transformer({ end, ["if"] = function(self, node, ret) if ntype(node[2]) == "assign" then - local _, assign, body = unpack(node) + local assign, body = unpack(node, 2) if destructure.has_destructure(assign[2]) then local name = NameProxy("des") body = { @@ -694,7 +694,7 @@ return Transformer({ node.body = with_continue_listener(node.body) end, switch = function(self, node, ret) - local _, exp, conds = unpack(node) + local exp, conds = unpack(node, 2) local exp_name = NameProxy("exp") local convert_cond convert_cond = function(cond) diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index 50de0e0d..2d3a3455 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -222,14 +222,14 @@ Transformer { nil update: (node) => - _, name, op, exp = unpack node + name, op, exp = unpack node, 2 op_final = op\match "^(.+)=$" error "Unknown op: "..op if not op_final exp = {"parens", exp} unless value_is_singular exp build.assign_one name, {"exp", name, op_final, exp} import: (node) => - _, names, source = unpack node + names, source = unpack node, 2 table_values = for name in *names dest_name = if ntype(name) == "colon" name[2] @@ -242,7 +242,7 @@ Transformer { { "assign", {dest}, {source}, [-1]: node[-1] } comprehension: (node, action) => - _, exp, clauses = unpack node + exp, clauses = unpack node, 2 action = action or (exp) -> {exp} construct_comprehension action(exp), clauses @@ -280,7 +280,7 @@ Transformer { if: (node, ret) => -- expand assign in cond if ntype(node[2]) == "assign" - _, assign, body = unpack node + assign, body = unpack node, 2 if destructure.has_destructure assign[2] name = NameProxy "des" @@ -426,7 +426,7 @@ Transformer { node.body = with_continue_listener node.body switch: (node, ret) => - _, exp, conds = unpack node + exp, conds = unpack node, 2 exp_name = NameProxy "exp" -- convert switch conds into if statment conds diff --git a/moonscript/transform/value.lua b/moonscript/transform/value.lua index 50a49041..33aaef99 100644 --- a/moonscript/transform/value.lua +++ b/moonscript/transform/value.lua @@ -89,7 +89,7 @@ return Transformer({ return a:wrap(node) end, tblcomprehension = function(self, node) - local _, explist, clauses = unpack(node) + local explist, clauses = unpack(node, 2) local key_exp, value_exp = unpack(explist) local accum = NameProxy("tbl") local inner @@ -234,7 +234,7 @@ return Transformer({ end end, block_exp = function(self, node) - local _, body = unpack(node) + local body = unpack(node, 2) local fn = nil local arg_list = { } fn = smart_node(build.fndef({ diff --git a/moonscript/transform/value.moon b/moonscript/transform/value.moon index 18d97a3c..04e16ae2 100644 --- a/moonscript/transform/value.moon +++ b/moonscript/transform/value.moon @@ -56,7 +56,7 @@ Transformer { a\wrap node tblcomprehension: (node) => - _, explist, clauses = unpack node + explist, clauses = unpack node, 2 key_exp, value_exp = unpack explist accum = NameProxy "tbl" @@ -144,7 +144,7 @@ Transformer { } block_exp: (node) => - _, body = unpack node + body = unpack node, 2 fn = nil arg_list = {} From 619db4ba7b52cb7e23e05f5dfac59be899ea487d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 10:44:09 -0700 Subject: [PATCH 213/344] fixes #265 fixes #264 --- moonscript/cmd/coverage.lua | 1 + moonscript/cmd/coverage.moon | 3 +++ 2 files changed, 4 insertions(+) diff --git a/moonscript/cmd/coverage.lua b/moonscript/cmd/coverage.lua index 4fd36806..bed561f8 100644 --- a/moonscript/cmd/coverage.lua +++ b/moonscript/cmd/coverage.lua @@ -42,6 +42,7 @@ position_to_lines = function(file_content, positions) end local format_file format_file = function(fname, positions) + fname = fname:gsub("^@", "") local file = assert(io.open(fname)) local content = file:read("*a") file:close() diff --git a/moonscript/cmd/coverage.moon b/moonscript/cmd/coverage.moon index dd1d1fa0..281c12aa 100644 --- a/moonscript/cmd/coverage.moon +++ b/moonscript/cmd/coverage.moon @@ -23,6 +23,9 @@ position_to_lines = (file_content, positions) -> lines format_file = (fname, positions) -> + -- sources have @ in front of file names + fname = fname\gsub "^@", "" + file = assert io.open fname content = file\read "*a" file\close! From e0c16a3b6abb405ab613438cfc76f13a5a780865 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 11:25:38 -0700 Subject: [PATCH 214/344] add coverage handler spec --- spec/coverage_output_handler.moon | 51 +++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 spec/coverage_output_handler.moon diff --git a/spec/coverage_output_handler.moon b/spec/coverage_output_handler.moon new file mode 100644 index 00000000..0043e462 --- /dev/null +++ b/spec/coverage_output_handler.moon @@ -0,0 +1,51 @@ + +load_line_table = (chunk_name) -> + import to_lua from require "moonscript.base" + + return unless chunk_name\match "^@" + fname = chunk_name\sub 2 + + file = assert io.open fname + code = file\read "*a" + file\close! + + c, ltable = to_lua code + + return nil, ltable unless c + + line_tables = require "moonscript.line_tables" + line_tables[chunk_name] = ltable + true + +(options) -> + busted = require "busted" + handler = require("busted.outputHandlers.utfTerminal") options + + local spec_name + + coverage = require "moonscript.cmd.coverage" + cov = coverage.CodeCoverage! + + busted.subscribe { "test", "start" }, (context) -> + cov\start! + + busted.subscribe { "test", "end" }, -> + cov\stop! + + busted.subscribe { "suite", "end" }, (context) -> + line_counts = {} + + for chunk_name, counts in pairs cov.line_counts + continue unless chunk_name\match("^@$./") or chunk_name\match "@[^/]" + continue if chunk_name\match "^@spec/" + + if chunk_name\match "%.lua$" + chunk_name = chunk_name\gsub "lua$", "moon" + continue unless load_line_table chunk_name + + line_counts[chunk_name] = counts + + cov.line_counts = line_counts + cov\format_results! + + handler From f2cb2f506c247bced566080cd7a0bfb60c9c3f7b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 11:28:56 -0700 Subject: [PATCH 215/344] try adding coverage build step --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 11808bda..4e96f64e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,6 +10,7 @@ env: - LUA=lua5.2 - LUA=lua5.3 - LUA=luajit2.1 # current head of 2.1 branch + - LUA=luajit2.1 BUSTED="-o spec/coverage_output_handler.moon" before_install: - source .travis/setenv_lua.sh @@ -19,4 +20,4 @@ install: - luarocks install loadkit - luarocks make -script: busted +script: busted $BUSTED From 330f5491d688163310e99658b5788067d0ee3e8c Mon Sep 17 00:00:00 2001 From: Kawahara Satoru Date: Mon, 26 Sep 2016 03:36:07 +0900 Subject: [PATCH 216/344] add bitwise update assignment operators (#277) --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 2 +- spec/inputs/syntax.moon | 2 ++ spec/outputs/syntax.lua | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 2ba504a3..21fecb84 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -146,7 +146,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) CompFor = key("for" * Name * sym("=") * Ct(Exp * sym(",") * Exp * (sym(",") * Exp) ^ -1) / mark("for")), CompClause = CompFor + CompForEach + key("when") * Exp / mark("when"), Assign = sym("=") * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark("assign"), - Update = ((sym("..=") + sym("+=") + sym("-=") + sym("*=") + sym("/=") + sym("%=") + sym("or=") + sym("and=")) / trim) * Exp / mark("update"), + Update = ((sym("..=") + sym("+=") + sym("-=") + sym("*=") + sym("/=") + sym("%=") + sym("or=") + sym("and=") + sym("&=") + sym("|=") + sym(">>=") + sym("<<=")) / trim) * Exp / mark("update"), CharOperators = Space * C(S("+-*/%^><|&")), WordOperators = op("or") + op("and") + op("<=") + op(">=") + op("~=") + op("!=") + op("==") + op("..") + op("<<") + op(">>") + op("//"), BinaryOperator = (WordOperators + CharOperators) * SpaceBreak ^ 0, diff --git a/moonscript/parse.moon b/moonscript/parse.moon index f7d89268..558d4c82 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -175,7 +175,7 @@ build_grammar = wrap_env debug_grammar, (root) -> CompClause: CompFor + CompForEach + key"when" * Exp / mark"when" Assign: sym"=" * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark"assign" - Update: ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=") / trim) * Exp / mark"update" + Update: ((sym"..=" + sym"+=" + sym"-=" + sym"*=" + sym"/=" + sym"%=" + sym"or=" + sym"and=" + sym"&=" + sym"|=" + sym">>=" + sym"<<=") / trim) * Exp / mark"update" CharOperators: Space * C(S"+-*/%^><|&") WordOperators: op"or" + op"and" + op"<=" + op">=" + op"~=" + op"!=" + op"==" + op".." + op"<<" + op">>" + op"//" diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index 05ce0671..7b49d04b 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -243,6 +243,8 @@ another hello, one, a += 3 - 5 a *= 3 + 5 a *= 3 +a >>= 3 +a <<= 3 a /= func "cool" --- diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index f19a3e69..bf323195 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -241,6 +241,8 @@ another(hello, one, two, three, four, { a = a + (3 - 5) a = a * (3 + 5) a = a * 3 +a = a >> 3 +a = a << 3 a = a / func("cool") x["then"] = "hello" x["while"]["true"] = "hello" From 92b269c0cc1e760022b9da9642859498de5f6fa4 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 11:59:08 -0700 Subject: [PATCH 217/344] fixes #286 --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 8 +++++++- spec/inputs/tables.moon | 18 ++++++++++++++++++ spec/outputs/tables.lua | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 21fecb84..240041b3 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -183,7 +183,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) ClassBlock = SpaceBreak ^ 1 * Advance * Ct(ClassLine * (SpaceBreak ^ 1 * ClassLine) ^ 0) * PopIndent, ClassLine = CheckIndent * ((KeyValueList / mark("props") + Statement / mark("stm") + Exp / mark("stm")) * sym(",") ^ -1), Export = key("export") * (Cc("class") * ClassDecl + op("*") + op("^") + Ct(NameList) * (sym("=") * Ct(ExpListLow)) ^ -1) / mark("export"), - KeyValue = (sym(":") * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym("[") * Exp * sym("]") + DoubleString + SingleString) * symx(":") * (Exp + TableBlock + SpaceBreak ^ 1 * Exp)), + KeyValue = (sym(":") * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym("[") * Exp * sym("]") + Space * DoubleString + Space * SingleString) * symx(":") * (Exp + TableBlock + SpaceBreak ^ 1 * Exp)), KeyValueList = KeyValue * (sym(",") * KeyValue) ^ 0, KeyValueLine = CheckIndent * KeyValueList * sym(",") ^ -1, FnArgsDef = sym("(") * Ct(FnArgDefList ^ -1) * (key("using") * Ct(NameList + Space * "nil") + Ct("")) * sym(")") + Ct("") * Ct(""), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 558d4c82..b309de3d 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -280,7 +280,13 @@ build_grammar = wrap_env debug_grammar, (root) -> op"*" + op"^" + Ct(NameList) * (sym"=" * Ct(ExpListLow))^-1) / mark"export" - KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp!) / self_assign + Ct((KeyName + sym"[" * Exp * sym"]" + DoubleString + SingleString) * symx":" * (Exp + TableBlock + SpaceBreak^1 * Exp)) + KeyValue: (sym":" * -SomeSpace * Name * lpeg.Cp!) / self_assign + + Ct( + (KeyName + sym"[" * Exp * sym"]" +Space * DoubleString + Space * SingleString) * + symx":" * + (Exp + TableBlock + SpaceBreak^1 * Exp) + ) + KeyValueList: KeyValue * (sym"," * KeyValue)^0 KeyValueLine: CheckIndent * KeyValueList * sym","^-1 diff --git a/spec/inputs/tables.moon b/spec/inputs/tables.moon index 3dad4124..2bf66d70 100644 --- a/spec/inputs/tables.moon +++ b/spec/inputs/tables.moon @@ -140,4 +140,22 @@ thing what: "more" okay: 123 -- a anon table + +-- + +k = { "hello": "world" } +k = { 'hello': 'world' } +k = { "hello": 'world', "hat": "zat" } + +please "hello": "world" +k = "hello": "world", "one": "zone" + +f = "one", "two": three, "four" +f = "two": three, "four" +f = { "one", "two": three, "four" } + + +j = "one", "two": three, "four": five, 6, 7 + + nil diff --git a/spec/outputs/tables.lua b/spec/outputs/tables.lua index 0cad58a5..b6a4f587 100644 --- a/spec/outputs/tables.lua +++ b/spec/outputs/tables.lua @@ -171,4 +171,36 @@ thing({ local _ = { okay = 123 } +local k = { + ["hello"] = "world" +} +k = { + ['hello'] = 'world' +} +k = { + ["hello"] = 'world', + ["hat"] = "zat" +} +please({ + ["hello"] = "world" +}) +k = { + ["hello"] = "world", + ["one"] = "zone" +} +local f = "one", { + ["two"] = three +}, "four" +f = { + ["two"] = three +}, "four" +f = { + "one", + ["two"] = three, + "four" +} +local j = "one", { + ["two"] = three, + ["four"] = five +}, 6, 7 return nil \ No newline at end of file From bf437b2cd242a882a200f10cc9b93192ebba0088 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 12:08:10 -0700 Subject: [PATCH 218/344] class names aren't refs, fixes #287 --- moonscript/transform/statement.lua | 8 +++- moonscript/transform/statement.moon | 7 ++- spec/inputs/local.moon | 11 +++++ spec/outputs/local.lua | 67 +++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index 6c8a60bf..47dd0de7 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -204,7 +204,13 @@ return Transformer({ local _continue_0 = false repeat local name = names[_index_0] - if not (name[2]:match("^%u")) then + local str_name + if ntype(name) == "ref" then + str_name = name[2] + else + str_name = name + end + if not (str_name:match("^%u")) then _continue_0 = true break end diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index 2d3a3455..42c3ec79 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -122,7 +122,12 @@ Transformer { if node[2] == "^" names = for name in *names - continue unless name[2]\match "^%u" + str_name = if ntype(name) == "ref" + name[2] + else + name + + continue unless str_name\match "^%u" name {"declare", names} diff --git a/spec/inputs/local.moon b/spec/inputs/local.moon index d4a616ca..fec78b18 100644 --- a/spec/inputs/local.moon +++ b/spec/inputs/local.moon @@ -75,6 +75,17 @@ do d = 2323 +do + local ^ + lowercase = 5 + Uppercase = 3 + + class One + Five = 6 + + class Two + class No + do local * -- this generates a nil value in the body diff --git a/spec/outputs/local.lua b/spec/outputs/local.lua index db19697a..3ee8623e 100644 --- a/spec/outputs/local.lua +++ b/spec/outputs/local.lua @@ -70,6 +70,73 @@ do d = 200 d = 2323 end +do + local Uppercase, One, Two + local lowercase = 5 + Uppercase = 3 + do + local _class_0 + local Five + local _base_0 = { } + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "One" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + local self = _class_0 + Five = 6 + One = _class_0 + end + do + local _class_0 + local No + local _base_0 = { } + _base_0.__index = _base_0 + _class_0 = setmetatable({ + __init = function() end, + __base = _base_0, + __name = "Two" + }, { + __index = _base_0, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_0) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_0.__class = _class_0 + local self = _class_0 + do + local _class_1 + local _base_1 = { } + _base_1.__index = _base_1 + _class_1 = setmetatable({ + __init = function() end, + __base = _base_1, + __name = "No" + }, { + __index = _base_1, + __call = function(cls, ...) + local _self_0 = setmetatable({}, _base_1) + cls.__init(_self_0, ...) + return _self_0 + end + }) + _base_1.__class = _class_1 + No = _class_1 + end + Two = _class_0 + end +end do local _list_0 = { } for _index_0 = 1, #_list_0 do From f2363f53cdcd917a4d16ba74fe426cd09929e400 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 12:53:05 -0700 Subject: [PATCH 219/344] show_line_position --- moonscript/parse/util.lua | 63 ++++++++++++++++++++++++++++++++++++-- moonscript/parse/util.moon | 46 ++++++++++++++++++++++++++-- 2 files changed, 103 insertions(+), 6 deletions(-) diff --git a/moonscript/parse/util.lua b/moonscript/parse/util.lua index e9d4554b..2687384b 100644 --- a/moonscript/parse/util.lua +++ b/moonscript/parse/util.lua @@ -41,6 +41,58 @@ extract_line = function(str, start_pos) end return str:match("^.-$") end +local show_line_position +show_line_position = function(str, pos, context) + if context == nil then + context = true + end + local lines = { + { } + } + for c in str:gmatch(".") do + lines[#lines] = lines[#lines] or { } + table.insert(lines[#lines], c) + if c == "\n" then + lines[#lines + 1] = { } + end + end + for i, line in ipairs(lines) do + lines[i] = table.concat(line) + end + local out + local remaining = pos - 1 + for k, line in ipairs(lines) do + if remaining < #line then + local left = line:sub(1, remaining) + local right = line:sub(remaining + 1) + out = { + tostring(left) .. "â—‰" .. tostring(right) + } + if context then + do + local before = lines[k - 1] + if before then + table.insert(out, 1, before) + end + end + do + local after = lines[k + 1] + if after then + table.insert(out, after) + end + end + end + break + else + remaining = remaining - #line + end + end + if not (out) then + return "-" + end + out = table.concat(out) + return (out:gsub("\n*$", "")) +end local mark mark = function(name) return function(...) @@ -60,9 +112,12 @@ pos = function(patt) end end local got -got = function(what) +got = function(what, context) + if context == nil then + context = true + end return Cmt("", function(str, pos) - print("++ got " .. tostring(what), "[" .. tostring(extract_line(str, pos)) .. "]") + print("++ got " .. tostring(what), "[" .. tostring(show_line_position(str, pos, context)) .. "]") return true end) end @@ -245,5 +300,7 @@ return { join_chain = join_chain, wrap_decorator = wrap_decorator, check_lua_string = check_lua_string, - self_assign = self_assign + self_assign = self_assign, + got = got, + show_line_position = show_line_position } diff --git a/moonscript/parse/util.moon b/moonscript/parse/util.moon index af33279e..17adf77a 100644 --- a/moonscript/parse/util.moon +++ b/moonscript/parse/util.moon @@ -31,6 +31,46 @@ extract_line = (str, start_pos) -> str\match "^.-$" +-- print the line with a token showing the position +show_line_position = (str, pos, context=true) -> + lines = { {} } + for c in str\gmatch "." + lines[#lines] or= {} + table.insert lines[#lines], c + if c == "\n" + lines[#lines + 1] = {} + + for i, line in ipairs lines + lines[i] = table.concat line + + local out + + remaining = pos - 1 + for k, line in ipairs lines + if remaining < #line + left = line\sub 1, remaining + right = line\sub remaining + 1 + out = { + "#{left}â—‰#{right}" + } + + if context + if before = lines[k - 1] + table.insert out, 1, before + + if after = lines[k + 1] + table.insert out, after + + break + else + remaining -= #line + + + return "-" unless out + + out = table.concat out + (out\gsub "\n*$", "") + -- used to identify a capture with a label mark = (name) -> (...) -> {name, ...} @@ -46,9 +86,9 @@ pos = (patt) -> -- generates a debug pattern that always succeeds and prints out where we are -- in the buffer with a label -got = (what) -> +got = (what, context=true) -> Cmt "", (str, pos) -> - print "++ got #{what}", "[#{extract_line str, pos}]" + print "++ got #{what}", "[#{show_line_position str, pos, context}]" true -- converts 1 element array to its value, otherwise marks it @@ -154,4 +194,4 @@ self_assign = (name, pos) -> { :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, :is_assignable, :check_assignable, :format_assign, :format_single_assign, :sym, :symx, :simple_string, :wrap_func_arg, :join_chain, :wrap_decorator, - :check_lua_string, :self_assign } + :check_lua_string, :self_assign, :got, :show_line_position } From 2f1dd92012afd89ce22e0babf774749a4d77d245 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 12:57:54 -0700 Subject: [PATCH 220/344] fix empty line consumption for else, fixes #276 --- moonscript/parse.lua | 8 ++++---- moonscript/parse.moon | 7 ++++--- spec/inputs/cond.moon | 26 ++++++++++++++++++++++++++ spec/outputs/cond.lua | 19 ++++++++++++++++++- 4 files changed, 52 insertions(+), 8 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 240041b3..10643a2b 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -27,10 +27,10 @@ Num = Space * (Num / function(v) v } end) -local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign +local Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign, got do local _obj_0 = require("moonscript.parse.util") - Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.join_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign + Indent, Cut, ensure, extract_line, mark, pos, flatten_or_mark, is_assignable, check_assignable, format_assign, format_single_assign, sym, symx, simple_string, wrap_func_arg, join_chain, wrap_decorator, check_lua_string, self_assign, got = _obj_0.Indent, _obj_0.Cut, _obj_0.ensure, _obj_0.extract_line, _obj_0.mark, _obj_0.pos, _obj_0.flatten_or_mark, _obj_0.is_assignable, _obj_0.check_assignable, _obj_0.format_assign, _obj_0.format_single_assign, _obj_0.sym, _obj_0.symx, _obj_0.simple_string, _obj_0.wrap_func_arg, _obj_0.join_chain, _obj_0.wrap_decorator, _obj_0.check_lua_string, _obj_0.self_assign, _obj_0.got end local build_grammar = wrap_env(debug_grammar, function(root) local _indent = Stack(0) @@ -131,8 +131,8 @@ local build_grammar = wrap_env(debug_grammar, function(root) SwitchCase = key("when") * Ct(ExpList) * key("then") ^ -1 * Body / mark("case"), SwitchElse = key("else") * Body / mark("else"), IfCond = Exp * Assign ^ -1 / format_single_assign, - IfElse = (Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("else") * Body / mark("else"), - IfElseIf = (Break * CheckIndent) ^ -1 * EmptyLine ^ 0 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif"), + IfElse = (Break * EmptyLine ^ 0 * CheckIndent) ^ -1 * key("else") * Body / mark("else"), + IfElseIf = (Break * EmptyLine ^ 0 * CheckIndent) ^ -1 * key("elseif") * pos(IfCond) * key("then") ^ -1 * Body / mark("elseif"), If = key("if") * IfCond * key("then") ^ -1 * Body * IfElseIf ^ 0 * IfElse ^ -1 / mark("if"), Unless = key("unless") * IfCond * key("then") ^ -1 * Body * IfElseIf ^ 0 * IfElse ^ -1 / mark("unless"), While = key("while") * DisableDo * ensure(Exp, PopDo) * key("do") ^ -1 * Body / mark("while"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index b309de3d..6daf9eec 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -27,7 +27,8 @@ Num = Space * (Num / (v) -> {"number", v}) :Indent, :Cut, :ensure, :extract_line, :mark, :pos, :flatten_or_mark, :is_assignable, :check_assignable, :format_assign, :format_single_assign, :sym, :symx, :simple_string, :wrap_func_arg, :join_chain, - :wrap_decorator, :check_lua_string, :self_assign + :wrap_decorator, :check_lua_string, :self_assign, :got + } = require "moonscript.parse.util" @@ -150,8 +151,8 @@ build_grammar = wrap_env debug_grammar, (root) -> IfCond: Exp * Assign^-1 / format_single_assign - IfElse: (Break * CheckIndent)^-1 * EmptyLine^0 * key"else" * Body / mark"else" - IfElseIf: (Break * CheckIndent)^-1 * EmptyLine^0 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif" + IfElse: (Break * EmptyLine^0 * CheckIndent)^-1 * key"else" * Body / mark"else" + IfElseIf: (Break * EmptyLine^0 * CheckIndent)^-1 * key"elseif" * pos(IfCond) * key"then"^-1 * Body / mark"elseif" If: key"if" * IfCond * key"then"^-1 * Body * IfElseIf^0 * IfElse^-1 / mark"if" Unless: key"unless" * IfCond * key"then"^-1 * Body * IfElseIf^0 * IfElse^-1 / mark"unless" diff --git a/spec/inputs/cond.moon b/spec/inputs/cond.moon index 89097216..3bf966fb 100644 --- a/spec/inputs/cond.moon +++ b/spec/inputs/cond.moon @@ -154,3 +154,29 @@ a,c,b = "cool" if something +--- + +j = if 1 + if 2 + 3 +else 6 + + +m = if 1 + + + + if 2 + + + 3 + + +else 6 + + + +nil + + + diff --git a/spec/outputs/cond.lua b/spec/outputs/cond.lua index 036f5469..30e87626 100644 --- a/spec/outputs/cond.lua +++ b/spec/outputs/cond.lua @@ -250,4 +250,21 @@ local a = 12 local c, b if something then a, c, b = "cool" -end \ No newline at end of file +end +local j +if 1 then + if 2 then + j = 3 + end +else + j = 6 +end +local m +if 1 then + if 2 then + m = 3 + end +else + m = 6 +end +return nil \ No newline at end of file From dfc4f7b0167274c8ed0e11e2ac8ec03e6052487b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 13:11:39 -0700 Subject: [PATCH 221/344] add note in readme about windows builds --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/README.md b/README.md index b282f75d..fb79f111 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,15 @@ busted Writing specs is a bit more complicated. Check out [the spec writing guide](spec/README.md). +## Binaries + +Precompiled versions of MoonScript are provided for Windows. You can find them +in the [GitHub releases page](https://github.com/leafo/moonscript/releases). +(Scroll down to the `win32-` tags. + +The build code can be found in the [`binaries` +branch](https://github.com/leafo/moonscript/tree/binaries) + ## Editor Support * [Vim](https://github.com/leafo/moonscript-vim) From c19a7fd2c32c225d411f460f20420681cab2f9f2 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 14:07:35 -0700 Subject: [PATCH 222/344] allow assignment in `unless` block, fixes #251 --- moonscript/transform/statement.lua | 36 +++++++++++++++++++++-------- moonscript/transform/statement.moon | 16 +++++++++++-- spec/inputs/cond.moon | 8 +++++++ spec/outputs/cond.lua | 9 ++++++++ 4 files changed, 58 insertions(+), 11 deletions(-) diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index 47dd0de7..7303666a 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -484,17 +484,35 @@ return Transformer({ return wrapped end, unless = function(self, node) - return { - "if", - { - "not", + local clause = node[2] + if ntype(clause) == "assign" then + if destructure.has_destructure(clause[2]) then + error("destructure not allowed in unless assignment") + end + return build["do"]({ + clause, { - "parens", - node[2] + "if", + { + "not", + clause[2][1] + }, + unpack(node, 3) } - }, - unpack(node, 3) - } + }) + else + return { + "if", + { + "not", + { + "parens", + clause + } + }, + unpack(node, 3) + } + end end, ["if"] = function(self, node, ret) if ntype(node[2]) == "assign" then diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index 42c3ec79..f5cf3a54 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -280,7 +280,19 @@ Transformer { wrapped unless: (node) => - { "if", {"not", {"parens", node[2]}}, unpack node, 3 } + clause = node[2] + + if ntype(clause) == "assign" + if destructure.has_destructure clause[2] + error "destructure not allowed in unless assignment" + + build.do { + clause + { "if", {"not", clause[2][1]}, unpack node, 3 } + } + + else + { "if", {"not", {"parens", clause}}, unpack node, 3 } if: (node, ret) => -- expand assign in cond @@ -300,7 +312,7 @@ Transformer { } else name = assign[2][1] - return build["do"] { + return build.do { assign {"if", name, unpack node, 3} } diff --git a/spec/inputs/cond.moon b/spec/inputs/cond.moon index 3bf966fb..18e42b91 100644 --- a/spec/inputs/cond.moon +++ b/spec/inputs/cond.moon @@ -147,6 +147,14 @@ print "hello" unless value dddd = {1,2,3} unless value + +-- + +do + j = 100 + unless j = hi! + error "not j!" + ---------------- a = 12 diff --git a/spec/outputs/cond.lua b/spec/outputs/cond.lua index 30e87626..35e7ac56 100644 --- a/spec/outputs/cond.lua +++ b/spec/outputs/cond.lua @@ -246,6 +246,15 @@ if not (value) then 3 } end +do + local j = 100 + do + j = hi() + if not j then + error("not j!") + end + end +end local a = 12 local c, b if something then From c024c83975c369b60be3f3070c33edc2409e8240 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 14:07:41 -0700 Subject: [PATCH 223/344] spec indentation --- spec/inputs/unless_else.moon | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spec/inputs/unless_else.moon b/spec/inputs/unless_else.moon index b421d4d0..fe96c0bd 100644 --- a/spec/inputs/unless_else.moon +++ b/spec/inputs/unless_else.moon @@ -1,5 +1,5 @@ if a - unless b - print "hi" - elseif c - print "not hi" + unless b + print "hi" + elseif c + print "not hi" From e0da63c50107584fd8255d13ffce5b08fb5a0f26 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 14:20:57 -0700 Subject: [PATCH 224/344] assignable name list in comprehension/decorator for in loop, fixes #236 --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 2 +- spec/inputs/comprehension.moon | 13 ++++++ spec/outputs/comprehension.lua | 79 ++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 2 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 10643a2b..be2c1529 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -142,7 +142,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) Comprehension = sym("[") * Exp * CompInner * sym("]") / mark("comprehension"), TblComprehension = sym("{") * Ct(Exp * (sym(",") * Exp) ^ -1) * CompInner * sym("}") / mark("tblcomprehension"), CompInner = Ct((CompForEach + CompFor) * CompClause ^ 0), - CompForEach = key("for") * Ct(NameList) * key("in") * (sym("*") * Exp / mark("unpack") + Exp) / mark("foreach"), + CompForEach = key("for") * Ct(AssignableNameList) * key("in") * (sym("*") * Exp / mark("unpack") + Exp) / mark("foreach"), CompFor = key("for" * Name * sym("=") * Ct(Exp * sym(",") * Exp * (sym(",") * Exp) ^ -1) / mark("for")), CompClause = CompFor + CompForEach + key("when") * Exp / mark("when"), Assign = sym("=") * (Ct(With + If + Switch) + Ct(TableBlock + ExpListLow)) / mark("assign"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 6daf9eec..708f7c91 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -171,7 +171,7 @@ build_grammar = wrap_env debug_grammar, (root) -> TblComprehension: sym"{" * Ct(Exp * (sym"," * Exp)^-1) * CompInner * sym"}" / mark"tblcomprehension" CompInner: Ct((CompForEach + CompFor) * CompClause^0) - CompForEach: key"for" * Ct(NameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"foreach" + CompForEach: key"for" * Ct(AssignableNameList) * key"in" * (sym"*" * Exp / mark"unpack" + Exp) / mark"foreach" CompFor: key "for" * Name * sym"=" * Ct(Exp * sym"," * Exp * (sym"," * Exp)^-1) / mark"for" CompClause: CompFor + CompForEach + key"when" * Exp / mark"when" diff --git a/spec/inputs/comprehension.moon b/spec/inputs/comprehension.moon index 345d5b70..51b39857 100644 --- a/spec/inputs/comprehension.moon +++ b/spec/inputs/comprehension.moon @@ -30,4 +30,17 @@ dd = [y for i=1,10 when cool for thing in y when x > 3 when c + 3] {"hello", "world" for i=1,10} +-- + +j = [a for {a,b,c} in things] +k = [a for {a,b,c} in *things] +i = [hello for {:hello, :world} in *things] + +hj = {a,c for {a,b,c} in things} +hk = {a,c for {a,b,c} in *things} +hi = {hello,world for {:hello, :world} in *things} + +ok(a,b,c) for {a,b,c} in things + + nil diff --git a/spec/outputs/comprehension.lua b/spec/outputs/comprehension.lua index ec3569a1..c53290ea 100644 --- a/spec/outputs/comprehension.lua +++ b/spec/outputs/comprehension.lua @@ -168,4 +168,83 @@ do end _ = _tbl_0 end +local j +do + local _accum_0 = { } + local _len_0 = 1 + for _des_0 in things do + local a, b, c + a, b, c = _des_0[1], _des_0[2], _des_0[3] + _accum_0[_len_0] = a + _len_0 = _len_0 + 1 + end + j = _accum_0 +end +local k +do + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = things + for _index_0 = 1, #_list_0 do + local _des_0 = _list_0[_index_0] + local a, b, c + a, b, c = _des_0[1], _des_0[2], _des_0[3] + _accum_0[_len_0] = a + _len_0 = _len_0 + 1 + end + k = _accum_0 +end +local i +do + local _accum_0 = { } + local _len_0 = 1 + local _list_0 = things + for _index_0 = 1, #_list_0 do + local _des_0 = _list_0[_index_0] + local hello, world + hello, world = _des_0.hello, _des_0.world + _accum_0[_len_0] = hello + _len_0 = _len_0 + 1 + end + i = _accum_0 +end +local hj +do + local _tbl_0 = { } + for _des_0 in things do + local a, b, c + a, b, c = _des_0[1], _des_0[2], _des_0[3] + _tbl_0[a] = c + end + hj = _tbl_0 +end +local hk +do + local _tbl_0 = { } + local _list_0 = things + for _index_0 = 1, #_list_0 do + local _des_0 = _list_0[_index_0] + local a, b, c + a, b, c = _des_0[1], _des_0[2], _des_0[3] + _tbl_0[a] = c + end + hk = _tbl_0 +end +local hi +do + local _tbl_0 = { } + local _list_0 = things + for _index_0 = 1, #_list_0 do + local _des_0 = _list_0[_index_0] + local hello, world + hello, world = _des_0.hello, _des_0.world + _tbl_0[hello] = world + end + hi = _tbl_0 +end +for _des_0 in things do + local a, b, c + a, b, c = _des_0[1], _des_0[2], _des_0[3] + ok(a, b, c) +end return nil \ No newline at end of file From d8830b17f3dafcdd6b0357d584bd296730ce5ea6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 14:33:55 -0700 Subject: [PATCH 225/344] allow whitespace around function arguments when in parens, fixes #274 --- moonscript/parse.lua | 3 ++- moonscript/parse.moon | 3 ++- spec/inputs/whitespace.moon | 19 +++++++++++++++++++ spec/outputs/whitespace.lua | 8 ++++++-- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index be2c1529..16d94313 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -164,7 +164,8 @@ local build_grammar = wrap_env(debug_grammar, function(root) LuaStringClose = "]" * P("=") ^ 0 * "]", Callable = pos(Name / mark("ref")) + SelfName + VarArg + Parens / mark("parens"), Parens = sym("(") * SpaceBreak ^ 0 * Exp * SpaceBreak ^ 0 * sym(")"), - FnArgs = symx("(") * SpaceBreak ^ 0 * Ct(ExpList ^ -1) * SpaceBreak ^ 0 * sym(")") + sym("!") * -P("=") * Ct(""), + FnArgs = symx("(") * SpaceBreak ^ 0 * Ct(FnArgsExpList ^ -1) * SpaceBreak ^ 0 * sym(")") + sym("!") * -P("=") * Ct(""), + FnArgsExpList = Exp * (sym(",") * White * Exp) ^ 0, Chain = (Callable + String + -S(".\\")) * ChainItems / mark("chain") + Space * (DotChainItem * ChainItems ^ -1 + ColonChain) / mark("chain"), ChainItems = ChainItem ^ 1 * ColonChain ^ -1 + ColonChain, ChainItem = Invoke + DotChainItem + Slice + symx("[") * Exp / mark("index") * sym("]"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 708f7c91..baef3d08 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -227,7 +227,8 @@ build_grammar = wrap_env debug_grammar, (root) -> Callable: pos(Name / mark"ref") + SelfName + VarArg + Parens / mark"parens" Parens: sym"(" * SpaceBreak^0 * Exp * SpaceBreak^0 * sym")" - FnArgs: symx"(" * SpaceBreak^0 * Ct(ExpList^-1) * SpaceBreak^0 * sym")" + sym"!" * -P"=" * Ct"" + FnArgs: symx"(" * SpaceBreak^0 * Ct(FnArgsExpList^-1) * SpaceBreak^0 * sym")" + sym"!" * -P"=" * Ct"" + FnArgsExpList: Exp * (sym"," * White * Exp)^0 Chain: (Callable + String + -S".\\") * ChainItems / mark"chain" + Space * (DotChainItem * ChainItems^-1 + ColonChain) / mark"chain" diff --git a/spec/inputs/whitespace.moon b/spec/inputs/whitespace.moon index a8242228..4a2ff1fa 100644 --- a/spec/inputs/whitespace.moon +++ b/spec/inputs/whitespace.moon @@ -81,3 +81,22 @@ if hello 1,2,3, print "hello" +-- + +a( + one, two, three +) + +b( + one, + two, + three +) + + +c(one, two, + three, four) + +-- + +nil diff --git a/spec/outputs/whitespace.lua b/spec/outputs/whitespace.lua index ce2d7920..c112a055 100644 --- a/spec/outputs/whitespace.lua +++ b/spec/outputs/whitespace.lua @@ -72,5 +72,9 @@ if hello(1, 2, 3, world, world) then print("hello") end if hello(1, 2, 3, world, world) then - return print("hello") -end \ No newline at end of file + print("hello") +end +a(one, two, three) +b(one, two, three) +c(one, two, three, four) +return nil \ No newline at end of file From 0a2399ef396aab822ad942eda2ea6da00eba5c8d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 15:53:40 -0700 Subject: [PATCH 226/344] let newlines break arguments in fn call with parens --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 2 +- spec/inputs/funcs.moon | 38 ++++++++++++++++++++++++++++++++++++++ spec/outputs/funcs.lua | 17 +++++++++++++++-- 4 files changed, 55 insertions(+), 4 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 16d94313..3b9b832b 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -165,7 +165,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) Callable = pos(Name / mark("ref")) + SelfName + VarArg + Parens / mark("parens"), Parens = sym("(") * SpaceBreak ^ 0 * Exp * SpaceBreak ^ 0 * sym(")"), FnArgs = symx("(") * SpaceBreak ^ 0 * Ct(FnArgsExpList ^ -1) * SpaceBreak ^ 0 * sym(")") + sym("!") * -P("=") * Ct(""), - FnArgsExpList = Exp * (sym(",") * White * Exp) ^ 0, + FnArgsExpList = Exp * ((Break + sym(",")) * White * Exp) ^ 0, Chain = (Callable + String + -S(".\\")) * ChainItems / mark("chain") + Space * (DotChainItem * ChainItems ^ -1 + ColonChain) / mark("chain"), ChainItems = ChainItem ^ 1 * ColonChain ^ -1 + ColonChain, ChainItem = Invoke + DotChainItem + Slice + symx("[") * Exp / mark("index") * sym("]"), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index baef3d08..5ffc1f0d 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -228,7 +228,7 @@ build_grammar = wrap_env debug_grammar, (root) -> Parens: sym"(" * SpaceBreak^0 * Exp * SpaceBreak^0 * sym")" FnArgs: symx"(" * SpaceBreak^0 * Ct(FnArgsExpList^-1) * SpaceBreak^0 * sym")" + sym"!" * -P"=" * Ct"" - FnArgsExpList: Exp * (sym"," * White * Exp)^0 + FnArgsExpList: Exp * ((Break + sym",") * White * Exp)^0 Chain: (Callable + String + -S".\\") * ChainItems / mark"chain" + Space * (DotChainItem * ChainItems^-1 + ColonChain) / mark"chain" diff --git a/spec/inputs/funcs.moon b/spec/inputs/funcs.moon index 8c17439b..a6812255 100644 --- a/spec/inputs/funcs.moon +++ b/spec/inputs/funcs.moon @@ -59,3 +59,41 @@ k -> if yes then return else return -> real_name if something +-- + +d( + -> + print "hello world" + 10 +) + + + +d( + 1,2,3 + 4 + 5 + 6 + + if something + print "okay" + 10 + + 10,20 +) + + +f( + + )( + + )( + what + )(-> + print "srue" + 123) + +-- + + +nil diff --git a/spec/outputs/funcs.lua b/spec/outputs/funcs.lua index 35dee928..02f4f756 100644 --- a/spec/outputs/funcs.lua +++ b/spec/outputs/funcs.lua @@ -108,8 +108,21 @@ k(function() return end end) -return function() +_ = function() if something then return real_name end -end \ No newline at end of file +end +d(function() + return print("hello world") +end, 10) +d(1, 2, 3, 4, 5, 6, (function() + if something then + print("okay") + return 10 + end +end)(), 10, 20) +f()()(what)(function() + return print("srue") +end, 123) +return nil \ No newline at end of file From 2e7d5153f193ca19e7de9941ae09a1ad7bf00962 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 17:34:59 -0700 Subject: [PATCH 227/344] expression can be used inside slide boundaries, fixes #233 --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 2 +- spec/inputs/comprehension.moon | 6 ++++++ spec/outputs/comprehension.lua | 10 ++++++++++ 4 files changed, 18 insertions(+), 2 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 3b9b832b..0ed7e4e5 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -155,7 +155,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) SimpleValue = If + Unless + Switch + With + ClassDecl + ForEach + For + While + Cmt(Do, check_do) + sym("-") * -SomeSpace * Exp / mark("minus") + sym("#") * Exp / mark("length") + sym("~") * Exp / mark("bitnot") + key("not") * Exp / mark("not") + TblComprehension + TableLit + Comprehension + FunLit + Num, ChainValue = (Chain + Callable) * Ct(InvokeArgs ^ -1) / join_chain, Value = pos(SimpleValue + Ct(KeyValueList) / mark("table") + ChainValue + String), - SliceValue = SimpleValue + ChainValue, + SliceValue = Exp, String = Space * DoubleString + Space * SingleString + LuaString, SingleString = simple_string("'"), DoubleString = simple_string('"', true), diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 5ffc1f0d..0403f333 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -211,7 +211,7 @@ build_grammar = wrap_env debug_grammar, (root) -> ChainValue + String) - SliceValue: SimpleValue + ChainValue + SliceValue: Exp String: Space * DoubleString + Space * SingleString + LuaString SingleString: simple_string("'") diff --git a/spec/inputs/comprehension.moon b/spec/inputs/comprehension.moon index 51b39857..1609d79e 100644 --- a/spec/inputs/comprehension.moon +++ b/spec/inputs/comprehension.moon @@ -42,5 +42,11 @@ hi = {hello,world for {:hello, :world} in *things} ok(a,b,c) for {a,b,c} in things +-- + +[item for item in *items[1 + 2,3+4]] +[item for item in *items[hello! * 4, 2 - thing[4]]] + + nil diff --git a/spec/outputs/comprehension.lua b/spec/outputs/comprehension.lua index c53290ea..15a93890 100644 --- a/spec/outputs/comprehension.lua +++ b/spec/outputs/comprehension.lua @@ -247,4 +247,14 @@ for _des_0 in things do a, b, c = _des_0[1], _des_0[2], _des_0[3] ok(a, b, c) end +local _max_0 = 3 + 4 +for _index_0 = 1 + 2, _max_0 < 0 and #items + _max_0 or _max_0 do + local item = items[_index_0] + _ = item +end +local _max_1 = 2 - thing[4] +for _index_0 = hello() * 4, _max_1 < 0 and #items + _max_1 or _max_1 do + local item = items[_index_0] + _ = item +end return nil \ No newline at end of file From 5d074c9e62442081b8c3edb57e23470ed892eed3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 17:45:50 -0700 Subject: [PATCH 228/344] allow whitespace around function argument declaration, fixes #227 --- moonscript/parse.lua | 4 ++-- moonscript/parse.moon | 6 +++--- spec/inputs/funcs.moon | 21 +++++++++++++++++++++ spec/outputs/funcs.lua | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index 0ed7e4e5..f3fda489 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -187,8 +187,8 @@ local build_grammar = wrap_env(debug_grammar, function(root) KeyValue = (sym(":") * -SomeSpace * Name * lpeg.Cp()) / self_assign + Ct((KeyName + sym("[") * Exp * sym("]") + Space * DoubleString + Space * SingleString) * symx(":") * (Exp + TableBlock + SpaceBreak ^ 1 * Exp)), KeyValueList = KeyValue * (sym(",") * KeyValue) ^ 0, KeyValueLine = CheckIndent * KeyValueList * sym(",") ^ -1, - FnArgsDef = sym("(") * Ct(FnArgDefList ^ -1) * (key("using") * Ct(NameList + Space * "nil") + Ct("")) * sym(")") + Ct("") * Ct(""), - FnArgDefList = FnArgDef * (sym(",") * FnArgDef) ^ 0 * (sym(",") * Ct(VarArg)) ^ 0 + Ct(VarArg), + FnArgsDef = sym("(") * White * Ct(FnArgDefList ^ -1) * (key("using") * Ct(NameList + Space * "nil") + Ct("")) * White * sym(")") + Ct("") * Ct(""), + FnArgDefList = FnArgDef * (sym(",") * White * FnArgDef) ^ 0 * (sym(",") * White * Ct(VarArg)) ^ 0 + Ct(VarArg), FnArgDef = Ct((Name + SelfName) * (sym("=") * Exp) ^ -1), FunLit = FnArgsDef * (sym("->") * Cc("slim") + sym("=>") * Cc("fat")) * (Body + Ct("")) / mark("fndef"), NameList = Name * (sym(",") * Name) ^ 0, diff --git a/moonscript/parse.moon b/moonscript/parse.moon index 0403f333..d1a5fff1 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -292,11 +292,11 @@ build_grammar = wrap_env debug_grammar, (root) -> KeyValueList: KeyValue * (sym"," * KeyValue)^0 KeyValueLine: CheckIndent * KeyValueList * sym","^-1 - FnArgsDef: sym"(" * Ct(FnArgDefList^-1) * + FnArgsDef: sym"(" * White * Ct(FnArgDefList^-1) * (key"using" * Ct(NameList + Space * "nil") + Ct"") * - sym")" + Ct"" * Ct"" + White * sym")" + Ct"" * Ct"" - FnArgDefList: FnArgDef * (sym"," * FnArgDef)^0 * (sym"," * Ct(VarArg))^0 + Ct(VarArg) + FnArgDefList: FnArgDef * (sym"," * White * FnArgDef)^0 * (sym"," * White * Ct(VarArg))^0 + Ct(VarArg) FnArgDef: Ct((Name + SelfName) * (sym"=" * Exp)^-1) FunLit: FnArgsDef * diff --git a/spec/inputs/funcs.moon b/spec/inputs/funcs.moon index a6812255..faab1aff 100644 --- a/spec/inputs/funcs.moon +++ b/spec/inputs/funcs.moon @@ -95,5 +95,26 @@ f( -- +x = (a, + b) -> + print "what" + + +y = (a="hi", + b=23) -> + print "what" + +z = ( + a="hi", + b=23) -> + print "what" + + +j = (f,g,m, + a="hi", + b=23 +) -> + print "what" + nil diff --git a/spec/outputs/funcs.lua b/spec/outputs/funcs.lua index 02f4f756..1ce333dd 100644 --- a/spec/outputs/funcs.lua +++ b/spec/outputs/funcs.lua @@ -125,4 +125,37 @@ end)(), 10, 20) f()()(what)(function() return print("srue") end, 123) +x = function(a, b) + return print("what") +end +local y +y = function(a, b) + if a == nil then + a = "hi" + end + if b == nil then + b = 23 + end + return print("what") +end +local z +z = function(a, b) + if a == nil then + a = "hi" + end + if b == nil then + b = 23 + end + return print("what") +end +local j +j = function(f, g, m, a, b) + if a == nil then + a = "hi" + end + if b == nil then + b = 23 + end + return print("what") +end return nil \ No newline at end of file From a5abd2fe1ec6566f00d6660d4a27c070989af15a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 17:47:44 -0700 Subject: [PATCH 229/344] tests for ... in whitepace function def --- spec/inputs/funcs.moon | 16 ++++++++++++++++ spec/outputs/funcs.lua | 18 ++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/spec/inputs/funcs.moon b/spec/inputs/funcs.moon index faab1aff..37387f84 100644 --- a/spec/inputs/funcs.moon +++ b/spec/inputs/funcs.moon @@ -117,4 +117,20 @@ j = (f,g,m, print "what" +y = (a="hi", + b=23, + ...) -> + print "what" + + +y = (a="hi", + b=23, + ... +) -> + print "what" + + + + + nil diff --git a/spec/outputs/funcs.lua b/spec/outputs/funcs.lua index 1ce333dd..43cb9342 100644 --- a/spec/outputs/funcs.lua +++ b/spec/outputs/funcs.lua @@ -158,4 +158,22 @@ j = function(f, g, m, a, b) end return print("what") end +y = function(a, b, ...) + if a == nil then + a = "hi" + end + if b == nil then + b = 23 + end + return print("what") +end +y = function(a, b, ...) + if a == nil then + a = "hi" + end + if b == nil then + b = 23 + end + return print("what") +end return nil \ No newline at end of file From e698e180acbc774fdff6ea86388a2b82cbebc832 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 17:52:05 -0700 Subject: [PATCH 230/344] newlines can be used to separte function argument declarations --- moonscript/parse.lua | 2 +- moonscript/parse.moon | 2 +- spec/inputs/funcs.moon | 22 ++++++++++++++++++++++ spec/outputs/funcs.lua | 31 +++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/moonscript/parse.lua b/moonscript/parse.lua index f3fda489..da6731e7 100644 --- a/moonscript/parse.lua +++ b/moonscript/parse.lua @@ -188,7 +188,7 @@ local build_grammar = wrap_env(debug_grammar, function(root) KeyValueList = KeyValue * (sym(",") * KeyValue) ^ 0, KeyValueLine = CheckIndent * KeyValueList * sym(",") ^ -1, FnArgsDef = sym("(") * White * Ct(FnArgDefList ^ -1) * (key("using") * Ct(NameList + Space * "nil") + Ct("")) * White * sym(")") + Ct("") * Ct(""), - FnArgDefList = FnArgDef * (sym(",") * White * FnArgDef) ^ 0 * (sym(",") * White * Ct(VarArg)) ^ 0 + Ct(VarArg), + FnArgDefList = FnArgDef * ((sym(",") + Break) * White * FnArgDef) ^ 0 * ((sym(",") + Break) * White * Ct(VarArg)) ^ 0 + Ct(VarArg), FnArgDef = Ct((Name + SelfName) * (sym("=") * Exp) ^ -1), FunLit = FnArgsDef * (sym("->") * Cc("slim") + sym("=>") * Cc("fat")) * (Body + Ct("")) / mark("fndef"), NameList = Name * (sym(",") * Name) ^ 0, diff --git a/moonscript/parse.moon b/moonscript/parse.moon index d1a5fff1..7fd5de6f 100644 --- a/moonscript/parse.moon +++ b/moonscript/parse.moon @@ -296,7 +296,7 @@ build_grammar = wrap_env debug_grammar, (root) -> (key"using" * Ct(NameList + Space * "nil") + Ct"") * White * sym")" + Ct"" * Ct"" - FnArgDefList: FnArgDef * (sym"," * White * FnArgDef)^0 * (sym"," * White * Ct(VarArg))^0 + Ct(VarArg) + FnArgDefList: FnArgDef * ((sym"," + Break) * White * FnArgDef)^0 * ((sym"," + Break) * White * Ct(VarArg))^0 + Ct(VarArg) FnArgDef: Ct((Name + SelfName) * (sym"=" * Exp)^-1) FunLit: FnArgsDef * diff --git a/spec/inputs/funcs.moon b/spec/inputs/funcs.moon index 37387f84..08a29b64 100644 --- a/spec/inputs/funcs.moon +++ b/spec/inputs/funcs.moon @@ -129,6 +129,28 @@ y = (a="hi", ) -> print "what" +-- + +args = (a + b) -> + print "what" + + +args = (a="hi" + b=23) -> + print "what" + +args = ( + a="hi" + b=23) -> + print "what" + + +args = (f,g,m + a="hi" + b=23 +) -> + print "what" diff --git a/spec/outputs/funcs.lua b/spec/outputs/funcs.lua index 43cb9342..f5e2cf3c 100644 --- a/spec/outputs/funcs.lua +++ b/spec/outputs/funcs.lua @@ -176,4 +176,35 @@ y = function(a, b, ...) end return print("what") end +local args +args = function(a, b) + return print("what") +end +args = function(a, b) + if a == nil then + a = "hi" + end + if b == nil then + b = 23 + end + return print("what") +end +args = function(a, b) + if a == nil then + a = "hi" + end + if b == nil then + b = 23 + end + return print("what") +end +args = function(f, g, m, a, b) + if a == nil then + a = "hi" + end + if b == nil then + b = 23 + end + return print("what") +end return nil \ No newline at end of file From b3e2aabf14c2407a2054579c830ea3c1c518221c Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 18:10:53 -0700 Subject: [PATCH 231/344] start writing changelog --- CHANGELOG.md | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d2c2cdb7..e59823fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,64 @@ + +# MoonScript v0.5.0 (2016-X-XX) + +## Syntax updates + +### Function calls + +Function calls with parentheses can not have free whitespace around the +arguments. Additionally, a line break may be used in place of a comma: + +```moonscript +my_func( + "first arg" + => + print "some func" + + "third arg", "fourth arg" +) +``` + +### Function argument definitions + +Just like the function all update, function argument definitions have no +whitespace restrictions between arguments, and line breaks can be used to +separate arguments: + + +```moonscript +some_func = ( + name + type + action="print" +) => + print name, type, action +``` + +## Additions + +* `elseif` can be used part of an `unless` block (nymphium) +* `unless` conditional expression can contain an assignment like an `if` statement (#251) +* Lua 5.3 bitwise operator support (nymphium) (Kawahara Satoru) +* Makefile is Lua version agnostic (nymphium) +* Lint flag can be used with `moonc` watch mode (ChickenNuggers) +* Lint exits with status 1 if there was a problem detected (ChickenNuggers) +* Compiler can be used with lulpeg + +## Bug Fixes + +* Slice boundaries can be full expressions (#233) +* Destructure works when used as loop variable in comprehension (#236) +* Proper name local hoisting works for classes again (#287) +* Quoted table key literals can now be parsed when table declaration is in single line (#286) +* Fix an issue where `else` could get attached to wrong `if` statement (#276) +* Loop variables will no longer overwrite variables of the same name in the same scope (egonSchiele) +* A file being deleted will not crash polling watch mode (ChickenNuggers) +* The compiler will not try to compile a directory ending in `.moon` (Gskartwii) +* alt_getopt import works with modern version (Jon Allen) +* Code coverage not being able to find file from chunk name + + # MoonScript v0.4.0 (2015-12-06) ## Changes to `super` From c3f4e511c21afa7d2f9b6257f9d3805da65b0313 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 18:19:40 -0700 Subject: [PATCH 232/344] version bump :full_moon_with_face: --- moonscript/version.lua | 2 +- moonscript/version.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/version.lua b/moonscript/version.lua index 45fa3c50..91f885aa 100644 --- a/moonscript/version.lua +++ b/moonscript/version.lua @@ -1,4 +1,4 @@ -local version = "0.4.0" +local version = "0.5.0" return { version = version, print_version = function() diff --git a/moonscript/version.moon b/moonscript/version.moon index 6f665e26..68c483e4 100644 --- a/moonscript/version.moon +++ b/moonscript/version.moon @@ -1,5 +1,5 @@ -version = "0.4.0" +version = "0.5.0" { version: version, From b8244cb0937dc62a87c0bcd573b7319bdf7eef89 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 25 Sep 2016 18:26:27 -0700 Subject: [PATCH 233/344] fix typo, add date --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e59823fb..dd9c43dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,12 +1,12 @@ -# MoonScript v0.5.0 (2016-X-XX) +# MoonScript v0.5.0 (2016-9-25) ## Syntax updates ### Function calls -Function calls with parentheses can not have free whitespace around the +Function calls with parentheses can now have free whitespace around the arguments. Additionally, a line break may be used in place of a comma: ```moonscript From 2a9f16377b08f6e2ec51cd21a88b38cfac2d98f8 Mon Sep 17 00:00:00 2001 From: ryan Date: Mon, 26 Sep 2016 01:53:43 -0700 Subject: [PATCH 234/344] document new bitwise update assignment operators (#291) --- docs/reference.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/docs/reference.md b/docs/reference.md index 37e7871e..ce34721b 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -55,9 +55,9 @@ variable, or shadow an existing one. ## Update Assignment -`+=`, `-=`, `/=`, `*=`, `%=`, `..=`, `or=`, `and=` operators have been added -for updating and assigning at the same time. They are aliases for their -expanded equivalents. +`+=`, `-=`, `/=`, `*=`, `%=`, `..=`, `or=`, `and=`, `&=`, `|=`, `>>=`, and +`<<=` operators have been added for updating and assigning at the same time. +They are aliases for their expanded equivalents. ```moon x = 0 @@ -68,6 +68,12 @@ s ..= "world" b = false b and= true or false + +p = 50 +p &= 5 +p |= 3 +p >>= 3 +p <<= 3 ``` ## Comments From 669ca059633465753ba2ec1d11f76e4cbe03299a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 3 Oct 2016 15:40:30 -0700 Subject: [PATCH 235/344] remove accidental print, fixes #295 --- moonscript/cmd/watchers.lua | 1 - moonscript/cmd/watchers.moon | 1 - 2 files changed, 2 deletions(-) diff --git a/moonscript/cmd/watchers.lua b/moonscript/cmd/watchers.lua index 651011b2..7e266b8d 100644 --- a/moonscript/cmd/watchers.lua +++ b/moonscript/cmd/watchers.lua @@ -203,7 +203,6 @@ do local file file = _des_0[1] local time = lfs.attributes(file, "modification") - print(file, time) if not (time) then mod_time[file] = nil _continue_0 = true diff --git a/moonscript/cmd/watchers.moon b/moonscript/cmd/watchers.moon index b021e80e..c147fc5e 100644 --- a/moonscript/cmd/watchers.moon +++ b/moonscript/cmd/watchers.moon @@ -86,7 +86,6 @@ class SleepWatcher extends Watcher while true for {file} in *@file_list time = lfs.attributes file, "modification" - print file, time unless time -- file no longer exists mod_time[file] = nil continue From baf44fd7f79dbdf9261c349dae61267c7df45ff5 Mon Sep 17 00:00:00 2001 From: Lassi Kortela Date: Sat, 8 Oct 2016 20:49:36 +0300 Subject: [PATCH 236/344] mention emacs mode in readme (#297) --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index fb79f111..8dd57d42 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ branch](https://github.com/leafo/moonscript/tree/binaries) * [Vim](https://github.com/leafo/moonscript-vim) * [Textadept](https://github.com/leafo/moonscript-textadept) * [Sublime/Textmate](https://github.com/leafo/moonscript-tmbundle) +* [Emacs](https://github.com/k2052/moonscript-mode) ## License (MIT) From 9e1ad7fe53b6eb24da84f3f11f23c6739b3ec970 Mon Sep 17 00:00:00 2001 From: LudwikJaniuk Date: Sun, 23 Oct 2016 02:33:25 +0200 Subject: [PATCH 237/344] Specify that directories are also accepted (#294) Small change in wording to make it a bit clearer that you can provide directory names to moonc, not just file names. --- bin/moonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index dfd56969..61416327 100755 --- a/bin/moonc +++ b/bin/moonc @@ -9,7 +9,7 @@ local opts, ind = alt_getopt.get_opts(arg, "lvhwt:o:pTXb", { local read_stdin = arg[1] == "--" -local help = [[Usage: %s [options] files... +local help = [[Usage: %s [options] files/directories... -h Print this message -w Watch file/directory From 0b4a42437898edb1f252c3092b3e5ec2459a9aa2 Mon Sep 17 00:00:00 2001 From: "Lord Niels N. Horn" Date: Sun, 23 Oct 2016 22:50:57 +0200 Subject: [PATCH 238/344] Correct 'repl' to 'REPL' (#301) --- bin/moon.moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moon.moon b/bin/moon.moon index eb36eab5..4f02431a 100644 --- a/bin/moon.moon +++ b/bin/moon.moon @@ -47,7 +47,7 @@ run = -> script_fname = arg[ind] unless script_fname - print_help "repl not yet supported" + print_help "REPL not yet supported" new_arg = { [-1]: arg[0], From 0998331a5d717ab7ae26f4d99cbdfa824f9d7e56 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 23 Oct 2016 13:51:31 -0700 Subject: [PATCH 239/344] rebuild #301 --- bin/moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moon b/bin/moon index 9b7593ba..e69d7a06 100755 --- a/bin/moon +++ b/bin/moon @@ -54,7 +54,7 @@ run = function() end local script_fname = arg[ind] if not (script_fname) then - print_help("repl not yet supported") + print_help("REPL not yet supported") end local new_arg = { [-1] = arg[0], From fc741a3c1e4712dea0ffd5c9c508b48c0f89ac04 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 22 Nov 2016 23:50:48 -0800 Subject: [PATCH 240/344] start building ast spec --- spec/compiler_spec.moon | 36 ++++++++++++++++++++++++++++++++++++ spec/factory.moon | 9 +++++++++ 2 files changed, 45 insertions(+) create mode 100644 spec/compiler_spec.moon create mode 100644 spec/factory.moon diff --git a/spec/compiler_spec.moon b/spec/compiler_spec.moon new file mode 100644 index 00000000..01233538 --- /dev/null +++ b/spec/compiler_spec.moon @@ -0,0 +1,36 @@ +import Block from require "moonscript.compile" +import ref from require "spec.factory" + +-- no transform step +class SimpleBlock extends Block + new: (...) => + super ... + @transform = { + value: (...) -> ... + statement: (...) -> ... + } + +value = require "moonscript.compile.value" + +describe "moonscript.compile", -> + compile_node = (node) -> + block = SimpleBlock! + block\add block\value node + lines = block._lines\flatten! + lines[#lines] = nil if lines[#lines] == "\n" + table.concat lines + + -- compiling lua ast + describe "value", -> + for {name, node, expected} in *{ + { + "ref" + -> {"ref", "hello_world"} + "hello_world" + } + } + it "compiles #{name}", -> + node = node! + assert.same expected, compile_node(node) + + diff --git a/spec/factory.moon b/spec/factory.moon new file mode 100644 index 00000000..5a1c4989 --- /dev/null +++ b/spec/factory.moon @@ -0,0 +1,9 @@ + +-- ast factory + +ref = (name="val") -> + {"ref", name} + +{ + :var +} From e75fceff68ff4167b051390eaa4ec5c6d842aae6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 23 Nov 2016 00:04:31 -0800 Subject: [PATCH 241/344] more ast specs --- spec/compiler_spec.moon | 73 ++++++++++++++++++++++++++++++++++++++++- spec/factory.moon | 6 +++- 2 files changed, 77 insertions(+), 2 deletions(-) diff --git a/spec/compiler_spec.moon b/spec/compiler_spec.moon index 01233538..c1c9577d 100644 --- a/spec/compiler_spec.moon +++ b/spec/compiler_spec.moon @@ -1,5 +1,6 @@ import Block from require "moonscript.compile" -import ref from require "spec.factory" + +import ref, str from require "spec.factory" -- no transform step class SimpleBlock extends Block @@ -28,6 +29,76 @@ describe "moonscript.compile", -> -> {"ref", "hello_world"} "hello_world" } + + { + "explist" + -> { "explist", ref("a"), ref("b"), ref("c")} + "a, b, c" + } + + { + "parens" + -> { "parens", ref! } + "(val)" + } + + { + "string (single quote)" + -> {"string", "'", "Hello\\'s world"} + "'Hello\\'s world'" + } + + { + "string (double quote)" + -> {"string", '"', "Hello's world"} + [["Hello's world"]] + + } + + { + "string (lua)" + -> {"string", '[==[', "Hello's world"} + "[==[Hello's world]==]" + } + + { + "chain (single)" + -> {"chain", ref!} + "val" + } + + { + "chain (dot)" + -> {"chain", ref!, {"dot", "zone"} } + "val.zone" + } + + { + "chain (index)" + -> {"chain", ref!, {"index", ref("x") } } + "val[x]" + } + + + { + "chain (call)" + -> {"chain", ref!, {"call", { ref("arg") }} } + "val(arg)" + } + + { + "chain" + -> { + "chain" + ref! + {"dot", "one"} + {"index", str!} + {"colon", "two"} + {"call", { ref("arg") }} + } + 'val.one["dogzone"]:two(arg)' + } + } it "compiles #{name}", -> node = node! diff --git a/spec/factory.moon b/spec/factory.moon index 5a1c4989..a6f3d76c 100644 --- a/spec/factory.moon +++ b/spec/factory.moon @@ -4,6 +4,10 @@ ref = (name="val") -> {"ref", name} +str = (contents="dogzone", delim='"') -> + {"string", delim, contents} + { - :var + :ref + :str } From b367f7ed514967ced8d852df2658016827c3628d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 23 Nov 2016 08:42:24 -0800 Subject: [PATCH 242/344] more ast --- spec/compiler_spec.moon | 90 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 83 insertions(+), 7 deletions(-) diff --git a/spec/compiler_spec.moon b/spec/compiler_spec.moon index c1c9577d..696e6bb9 100644 --- a/spec/compiler_spec.moon +++ b/spec/compiler_spec.moon @@ -30,12 +30,30 @@ describe "moonscript.compile", -> "hello_world" } + { + "number" + -> {"number", "14"} + "14" + } + + { + "minus" + -> {"minus", ref!} + "-val" + } + { "explist" -> { "explist", ref("a"), ref("b"), ref("c")} "a, b, c" } + { + "exp" + -> {"exp", ref("a"), "+", ref("b"), "!=", ref("c")} + "a + b ~= c" + } + { "parens" -> { "parens", ref! } @@ -61,6 +79,48 @@ describe "moonscript.compile", -> "[==[Hello's world]==]" } + { + "self" + -> {"self", ref!} + "self.val" + } + + { + "self_class" + -> {"self_class", ref!} + "self.__class.val" + } + + { + "self_class_colon" + -> {"self_class_colon", ref!} + "self.__class:val" + } + + { + "not" + -> {"not", ref!} + "not val" + } + + { + "length" + -> {"length", ref!} + "#val" + } + + { + "length" + -> {"length", ref!} + "#val" + } + + { + "bitnot" + -> {"bitnot", ref!} + "~val" + } + { "chain (single)" -> {"chain", ref!} @@ -89,16 +149,32 @@ describe "moonscript.compile", -> { "chain" -> { - "chain" - ref! - {"dot", "one"} - {"index", str!} - {"colon", "two"} - {"call", { ref("arg") }} - } + "chain" + ref! + {"dot", "one"} + {"index", str!} + {"colon", "two"} + {"call", { ref("arg") }} + } 'val.one["dogzone"]:two(arg)' } + { + "chain (self receiver)" + -> { + "chain" + {"self", ref!} + {"call", {ref "arg"} } + } + "self:val(arg)" + } + + { + "fndef (empty)" + -> {"fndef", {}, {}, "slim", {}} + "function() end" + } + } it "compiles #{name}", -> node = node! From 5ff168f9d9c4aa9c39f2082fb14d7950bae4dd5e Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Sat, 7 Jan 2017 01:52:05 +0400 Subject: [PATCH 243/344] Mention Loadkit requirement for tests in README.md (#315) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8dd57d42..029ad633 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Online demo/compiler at . ## Running Tests Tests are written in MoonScript and use [Busted](http://olivinelabs.com/busted/). -In order to run the tests you must have MoonScript installed. +In order to run the tests you must have MoonScript and [Loadkit](https://github.com/leafo/loadkit) installed. To run tests, execute from the root directory: From 39cff4ece3372ac9da62a5e6115006709159434b Mon Sep 17 00:00:00 2001 From: Ryan Tongol Date: Fri, 6 Jan 2017 15:43:33 -0800 Subject: [PATCH 244/344] update copyright years (#316) --- README.md | 4 ++-- docs/reference.md | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 029ad633..535525b1 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ branch](https://github.com/leafo/moonscript/tree/binaries) ## License (MIT) -Copyright (C) 2015 by Leaf Corcoran +Copyright (C) 2017 by Leaf Corcoran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -64,4 +64,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. \ No newline at end of file +THE SOFTWARE. diff --git a/docs/reference.md b/docs/reference.md index ce34721b..9782f2ba 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1565,7 +1565,7 @@ their written order you can add `local *` to the top of your file. # License (MIT) - Copyright (C) 2015 by Leaf Corcoran + Copyright (C) 2017 by Leaf Corcoran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From dfef936a0cbda897d2f9512acdfe511e2f8a927b Mon Sep 17 00:00:00 2001 From: RyanSquared Date: Tue, 30 May 2017 00:14:03 -0500 Subject: [PATCH 245/344] bin/moon.moon: add support for argparse, remove support for alt-getopt --- bin/moon | 76 +++++++++++++++++++++++---------------------------- bin/moon.moon | 60 ++++++++++++++-------------------------- 2 files changed, 55 insertions(+), 81 deletions(-) diff --git a/bin/moon b/bin/moon index e69d7a06..ef3c2f2f 100755 --- a/bin/moon +++ b/bin/moon @@ -1,30 +1,39 @@ -#!/usr/bin/env lua -local alt_getopt = require("alt_getopt") +local argparse = require("argparse") local moonscript = require("moonscript.base") local util = require("moonscript.util") local errors = require("moonscript.errors") local unpack = util.unpack -local opts, ind = alt_getopt.get_opts(arg, "cvhd", { - version = "v", - help = "h" +local argparser = argparse()({ + name = "moon" }) -local help = [=[Usage: %s [options] [script [args]] - - -h Print this message - -d Disable stack trace rewriting - -c Collect and print code coverage - -v Print version -]=] +argparser:argument("script") +argparser:argument("args"):args("*") +argparser:option("-c --coverage", "Collect and print code coverage") +argparser:option("-d", "Disable stack trace rewriting") +argparser:option("-v --version", "Print version information") +local base = 0 +local _list_0 = arg +for _index_0 = 1, #_list_0 do + local flag = _list_0[_index_0] + base = base + 1 + if flag:sub(1, 1) ~= "-" then + break + end +end +local args = { + unpack(arg, 1, base) +} +local opts = argparser:parse(args) local print_err print_err = function(...) local msg = table.concat((function(...) local _accum_0 = { } local _len_0 = 1 - local _list_0 = { + local _list_1 = { ... } - for _index_0 = 1, #_list_0 do - local v = _list_0[_index_0] + for _index_0 = 1, #_list_1 do + local v = _list_1[_index_0] _accum_0[_len_0] = tostring(v) _len_0 = _len_0 + 1 end @@ -32,35 +41,18 @@ print_err = function(...) end)(...), "\t") return io.stderr:write(msg .. "\n") end -local print_help -print_help = function(err) - help = help:format(arg[0]) - if err then - print_err(err) - print_err(help) - else - print(help) - end - return os.exit() -end local run run = function() - if opts.h then - print_help() - end - if opts.v then + if opts.version then require("moonscript.version").print_version() os.exit() end - local script_fname = arg[ind] - if not (script_fname) then - print_help("REPL not yet supported") - end - local new_arg = { - [-1] = arg[0], - [0] = arg[ind], - select(ind + 1, unpack(arg)) + local script_fname = opts.script + args = { + unpack(arg, base + 1) } + args[-1] = arg[0] + args[0] = opts.script local moonscript_chunk, lua_parse_error local passed, err = pcall(function() moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { @@ -79,18 +71,19 @@ run = function() end os.exit(1) end - util.getfenv(moonscript_chunk).arg = new_arg + util.getfenv(moonscript_chunk).arg = args + print(args[-1], args[0], args[1]) local run_chunk run_chunk = function() moonscript.insert_loader() - moonscript_chunk(unpack(new_arg)) + moonscript_chunk(unpack(args)) return moonscript.remove_loader() end if opts.d then return run_chunk() end local err, trace, cov - if opts.c then + if opts.coverage then print("starting coverage") local coverage = require("moonscript.cmd.coverage") cov = coverage.CodeCoverage() @@ -119,4 +112,3 @@ run = function() end end return run() --- vim: set filetype=lua: diff --git a/bin/moon.moon b/bin/moon.moon index 4f02431a..71bdafb0 100644 --- a/bin/moon.moon +++ b/bin/moon.moon @@ -1,5 +1,4 @@ - -alt_getopt = require "alt_getopt" +argparse = require "argparse" moonscript = require "moonscript.base" util = require "moonscript.util" @@ -7,53 +6,36 @@ errors = require "moonscript.errors" unpack = util.unpack -opts, ind = alt_getopt.get_opts arg, "cvhd", { - version: "v" - help: "h" -} - -help = [=[Usage: %s [options] [script [args]] +argparser = argparse! name: "moon" - -h Print this message - -d Disable stack trace rewriting - -c Collect and print code coverage - -v Print version -]=] +argparser\argument "script" +argparser\argument("args")\args "*" +argparser\option "-c --coverage", "Collect and print code coverage" +argparser\option "-d", "Disable stack trace rewriting" +argparser\option "-v --version", "Print version information" +base = 0 +for flag in *arg + base += 1 + break if flag\sub(1, 1) != "-" +args = {unpack arg, 1, base} +opts = argparser\parse args print_err = (...) -> msg = table.concat [tostring v for v in *{...}], "\t" io.stderr\write msg .. "\n" -print_help = (err) -> - help = help\format arg[0] - - if err - print_err err - print_err help - else - print help - - os.exit! - run = -> - if opts.h - print_help! - if opts.v + if opts.version require("moonscript.version").print_version! os.exit! - script_fname = arg[ind] - - unless script_fname - print_help "REPL not yet supported" + script_fname = opts.script - new_arg = { - [-1]: arg[0], - [0]: arg[ind], - select ind + 1, unpack arg - } + args = {unpack arg, base + 1} + args[-1] = arg[0] + args[0] = opts.script local moonscript_chunk, lua_parse_error @@ -74,11 +56,11 @@ run = -> os.exit 1 - util.getfenv(moonscript_chunk).arg = new_arg + util.getfenv(moonscript_chunk).arg = args run_chunk = -> moonscript.insert_loader! - moonscript_chunk unpack new_arg + moonscript_chunk unpack args moonscript.remove_loader! if opts.d @@ -86,7 +68,7 @@ run = -> local err, trace, cov - if opts.c + if opts.coverage print "starting coverage" coverage = require "moonscript.cmd.coverage" cov = coverage.CodeCoverage! From dd9661db93d3e6328af18e277a15cca0ae44ad1d Mon Sep 17 00:00:00 2001 From: RyanSquared Date: Tue, 30 May 2017 00:14:20 -0500 Subject: [PATCH 246/344] bin/moonc: add support for argparse, remove support for alt-getopt --- bin/moonc | 92 +++++++++++++++++++------------------------------------ 1 file changed, 31 insertions(+), 61 deletions(-) diff --git a/bin/moonc b/bin/moonc index 61416327..f3aad57a 100755 --- a/bin/moonc +++ b/bin/moonc @@ -1,50 +1,38 @@ #!/usr/bin/env lua -local alt_getopt = require "alt_getopt" +local argparse = require "argparse" local lfs = require "lfs" -local opts, ind = alt_getopt.get_opts(arg, "lvhwt:o:pTXb", { - print = "p", tree = "T", version = "v", help = "h", lint = "l" -}) - -local read_stdin = arg[1] == "--" - -local help = [[Usage: %s [options] files/directories... - - -h Print this message - -w Watch file/directory - -t path Specify where to place compiled files - -o file Write output to file - -p Write output to standard out - -T Write parse tree instead of code (to stdout) - -X Write line rewrite map instead of code (to stdout) - -l Perform lint on the file instead of compiling - -b Dump parse and compile time (doesn't write output) - -v Print version +local parser = argparse() + +parser:flag("-l --lint", "Perform a lint on the file instead of compiling") +parser:flag("-v --version", "Print version") +parser:flag("-w --watch", "Watch file/directory for updates") +parser:mutex( + parser:flag("-t --output-to", "Specify where to place compiled files"), + parser:flag("-o", "Write output to file"):args(1), + parser:flag("-p", "Write output to standard output"), + parser:flag("-T", "Write parse tree instead of code (to stdout)"), + parser:flag("-b", "Write parse and compile time instead of code(to stdout)"), + parser:flag("-X", "Write line rewrite map instead of code (to stdout)") +) +parser:flag("-", + "Read from standard in, print to standard out (Must be only argument)") + +local read_stdin = arg[1] == "--" -- luacheck: ignore 113 + +if not read_stdin then + parser:argument("file/directory"):args("+") +end - -- Read from standard in, print to standard out - (Must be first and only argument) -]] +local opts = parser:parse() -if opts.v then +if opts.version then local v = require "moonscript.version" v.print_version() os.exit() end -function print_help(err) - local help_msg = help:format(arg[0]) - - if err then - io.stderr:write("Error: ".. err .. "\n") - io.stderr:write(help_msg .. "\n") - os.exit(1) - else - print(help_msg) - os.exit(0) - end -end - function log_msg(...) if not opts.p then io.stderr:write(table.concat({...}, " ") .. "\n") @@ -53,10 +41,7 @@ end local moonc = require("moonscript.cmd.moonc") local util = require "moonscript.util" -local mkdir = moonc.mkdir local normalize_dir = moonc.normalize_dir -local parse_dir = moonc.parse_dir -local parse_file = moonc.parse_file local compile_and_write = moonc.compile_and_write local path_to_target = moonc.path_to_target @@ -102,23 +87,19 @@ local function get_files(fname, files) for _, sub_fname in ipairs(scan_directory(fname)) do table.insert(files, { sub_fname, - path_to_target(sub_fname, opts.t, fname) + path_to_target(sub_fname, opts.output_to, fname) }) end else table.insert(files, { fname, - path_to_target(fname, opts.t) + path_to_target(fname, opts.output_to) }) end return files end -if opts.h then - print_help() -end - if read_stdin then local parse = require "moonscript.parse" local compile = require "moonscript.compile" @@ -137,14 +118,7 @@ if read_stdin then os.exit() end -local inputs = {} -for i = ind, #arg do - table.insert(inputs, arg[i]) -end - -if #inputs == 0 then - print_help("No files specified") -end +local inputs = opts["file/directory"] local files = {} for _, input in ipairs(inputs) do @@ -155,10 +129,6 @@ files = remove_dups(files, function(f) return f[2] end) -if opts.o and #files > 1 then - print_help("-o can not be used with multiple input files") -end - -- returns an iterator that returns files that have been updated local function create_watcher(files) local watchers = require("moonscript.cmd.watchers") @@ -170,10 +140,10 @@ local function create_watcher(files) return watchers.SleepWatcher(files):each_update() end -if opts.w then +if opts.watch then -- build function to check for lint or compile in watch local handle_file - if opts.l then + if opts.lint then local lint = require "moonscript.cmd.lint" handle_file = lint.lint_file else @@ -199,7 +169,7 @@ if opts.w then end local success, err = handle_file(fname, target) - if opts.l then + if opts.lint then if success then io.stderr:write(success .. "\n\n") elseif err then @@ -218,7 +188,7 @@ if opts.w then end io.stderr:write("\nQuitting...\n") -elseif opts.l then +elseif opts.lint then local has_linted_with_error; local lint = require "moonscript.cmd.lint" for _, tuple in pairs(files) do From 6f332a45592073a794087cd5a8b8e92f7d66ceb2 Mon Sep 17 00:00:00 2001 From: RyanSquared Date: Tue, 30 May 2017 00:15:46 -0500 Subject: [PATCH 247/344] moonscript-dev-1.rockspec: replace alt-getopt with argparse --- moonscript-dev-1.rockspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index c4388d0a..8970e306 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -16,7 +16,7 @@ description = { dependencies = { "lua >= 5.1", "lpeg >= 0.10, ~= 0.11", - "alt-getopt >= 0.7", + "argparse >= 0.5", "luafilesystem >= 1.5" } From fcc937ad39a0649eb782b1de0ebe3272362fd310 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 16 Oct 2017 16:19:08 -0700 Subject: [PATCH 248/344] rebuild moon --- bin/moon | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/moon b/bin/moon index ef3c2f2f..5e5e4251 100755 --- a/bin/moon +++ b/bin/moon @@ -1,3 +1,4 @@ +#!/usr/bin/env lua local argparse = require("argparse") local moonscript = require("moonscript.base") local util = require("moonscript.util") @@ -72,7 +73,6 @@ run = function() os.exit(1) end util.getfenv(moonscript_chunk).arg = args - print(args[-1], args[0], args[1]) local run_chunk run_chunk = function() moonscript.insert_loader() @@ -112,3 +112,4 @@ run = function() end end return run() +-- vim: set filetype=lua: From 9e36baa435edfaffb11ec8084aa439b7426ba275 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 16 Oct 2017 17:13:55 -0700 Subject: [PATCH 249/344] lift expressions from chain indexes on update operator, fixes #185 --- moonscript/transform/statement.lua | 69 ++++++++++++++++++++++++++++- moonscript/transform/statement.moon | 30 ++++++++++++- spec/inputs/syntax.moon | 4 ++ spec/outputs/syntax.lua | 6 +++ 4 files changed, 107 insertions(+), 2 deletions(-) diff --git a/moonscript/transform/statement.lua b/moonscript/transform/statement.lua index 7303666a..edeab697 100644 --- a/moonscript/transform/statement.lua +++ b/moonscript/transform/statement.lua @@ -353,18 +353,85 @@ return Transformer({ if not op_final then error("Unknown op: " .. op) end + local lifted + if ntype(name) == "chain" then + lifted = { } + local new_chain + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 3, #name do + local part = name[_index_0] + if ntype(part) == "index" then + local proxy = NameProxy("update") + table.insert(lifted, { + proxy, + part[2] + }) + _accum_0[_len_0] = { + "index", + proxy + } + else + _accum_0[_len_0] = part + end + _len_0 = _len_0 + 1 + end + new_chain = _accum_0 + end + if next(lifted) then + name = { + name[1], + name[2], + unpack(new_chain) + } + end + end if not (value_is_singular(exp)) then exp = { "parens", exp } end - return build.assign_one(name, { + local out = build.assign_one(name, { "exp", name, op_final, exp }) + if lifted and next(lifted) then + local names + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #lifted do + local l = lifted[_index_0] + _accum_0[_len_0] = l[1] + _len_0 = _len_0 + 1 + end + names = _accum_0 + end + local values + do + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #lifted do + local l = lifted[_index_0] + _accum_0[_len_0] = l[2] + _len_0 = _len_0 + 1 + end + values = _accum_0 + end + out = build.group({ + { + "assign", + names, + values + }, + out + }) + end + return out end, import = function(self, node) local names, source = unpack(node, 2) diff --git a/moonscript/transform/statement.moon b/moonscript/transform/statement.moon index f5cf3a54..2e4b8e83 100644 --- a/moonscript/transform/statement.moon +++ b/moonscript/transform/statement.moon @@ -229,9 +229,37 @@ Transformer { update: (node) => name, op, exp = unpack node, 2 op_final = op\match "^(.+)=$" + error "Unknown op: "..op if not op_final + + local lifted + + if ntype(name) == "chain" + lifted = {} + new_chain = for part in *name[3,] + if ntype(part) == "index" + proxy = NameProxy "update" + table.insert lifted, { proxy, part[2] } + { "index", proxy } + else + part + + if next lifted + name = {name[1], name[2], unpack new_chain} + exp = {"parens", exp} unless value_is_singular exp - build.assign_one name, {"exp", name, op_final, exp} + out = build.assign_one name, {"exp", name, op_final, exp} + + if lifted and next lifted + names = [l[1] for l in *lifted] + values = [l[2] for l in *lifted] + + out = build.group { + {"assign", names, values} + out + } + + out import: (node) => names, source = unpack node, 2 diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index 7b49d04b..854f6297 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -158,6 +158,10 @@ hello ..= "world" @@something += 10 @something += 10 +a["hello"] += 10 +a["hello#{tostring ff}"] += 10 +a[four].x += 10 + x = 0 (if ntype(v) == "fndef" then x += 1) for v in *values diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index bf323195..fef1a739 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -162,6 +162,12 @@ local m = m % 2 local hello = hello .. "world" self.__class.something = self.__class.something + 10 self.something = self.something + 10 +local _update_0 = "hello" +a[_update_0] = a[_update_0] + 10 +local _update_1 = "hello" .. tostring(tostring(ff)) +a[_update_1] = a[_update_1] + 10 +local _update_2 = four +a[_update_2].x = a[_update_2].x + 10 x = 0 local _list_0 = values for _index_0 = 1, #_list_0 do From 491f2352a238c4cbfe141c6aa80b3eb4cc9713f5 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 2 Feb 2018 21:55:21 -0800 Subject: [PATCH 250/344] add suppor tfor transform module --- bin/moonc | 4 +++- moonscript/cmd/moonc.lua | 6 ++++++ moonscript/cmd/moonc.moon | 3 +++ 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index f3aad57a..e8086703 100755 --- a/bin/moonc +++ b/bin/moonc @@ -8,9 +8,10 @@ local parser = argparse() parser:flag("-l --lint", "Perform a lint on the file instead of compiling") parser:flag("-v --version", "Print version") parser:flag("-w --watch", "Watch file/directory for updates") +parser:option("--transform", "Transform syntax tree with module") parser:mutex( parser:flag("-t --output-to", "Specify where to place compiled files"), - parser:flag("-o", "Write output to file"):args(1), + parser:option("-o", "Write output to file"), parser:flag("-p", "Write output to standard output"), parser:flag("-T", "Write parse tree instead of code (to stdout)"), parser:flag("-b", "Write parse and compile time instead of code(to stdout)"), @@ -218,6 +219,7 @@ else benchmark = opts.b, show_posmap = opts.X, show_parse_tree = opts.T, + transform_module = opts.transform }) if not success then diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index 54600998..09ac3c78 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -81,6 +81,12 @@ compile_file_text = function(text, opts) if opts.benchmark then compile_time = gettime() end + do + local mod = opts.transform_module + if mod then + tree = assert(require(mod)(tree)) + end + end local code, posmap_or_err, err_pos = compile.tree(tree) if not (code) then return nil, compile.format_error(posmap_or_err, err_pos, text) diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon index b2e92e63..ba59b06c 100644 --- a/moonscript/cmd/moonc.moon +++ b/moonscript/cmd/moonc.moon @@ -84,6 +84,9 @@ compile_file_text = (text, opts={}) -> compile_time = if opts.benchmark gettime! + if mod = opts.transform_module + tree = assert require(mod) tree + code, posmap_or_err, err_pos = compile.tree tree unless code From 9cba69b114c77028db365edfae02fe818dcd7f8b Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 13 Apr 2018 11:09:19 -0700 Subject: [PATCH 251/344] remove built printout when running comipiler on single file --- bin/moonc | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bin/moonc b/bin/moonc index e8086703..50c3c331 100755 --- a/bin/moonc +++ b/bin/moonc @@ -225,9 +225,7 @@ else if not success then io.stderr:write(fname .. "\t" .. err .. "\n") os.exit(1) - elseif success == "build" then - log_msg("Built", fname) - end + else end end From 57e4f984cbff45137ffe9bbecbd3dfabb7a0d871 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 30 Apr 2018 18:20:51 -0700 Subject: [PATCH 252/344] woops --- bin/moonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index 50c3c331..195112c5 100755 --- a/bin/moonc +++ b/bin/moonc @@ -225,7 +225,7 @@ else if not success then io.stderr:write(fname .. "\t" .. err .. "\n") os.exit(1) - else + end end end From 61b825fa5a8614782c7b4808678ae877da70ff6e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 30 Apr 2018 18:21:40 -0700 Subject: [PATCH 253/344] dump tree doesn't print by default --- moonscript/cmd/moonc.lua | 6 ++++-- moonscript/cmd/moonc.moon | 6 ++++-- moonscript/dump.lua | 2 +- moonscript/dump.moon | 2 +- 4 files changed, 10 insertions(+), 6 deletions(-) diff --git a/moonscript/cmd/moonc.lua b/moonscript/cmd/moonc.lua index 09ac3c78..e7618ef8 100644 --- a/moonscript/cmd/moonc.lua +++ b/moonscript/cmd/moonc.lua @@ -74,7 +74,7 @@ compile_file_text = function(text, opts) end if opts.show_parse_tree then local dump = require("moonscript.dump") - dump.tree(tree) + print(dump.tree(tree)) return true end local compile_time @@ -84,7 +84,9 @@ compile_file_text = function(text, opts) do local mod = opts.transform_module if mod then - tree = assert(require(mod)(tree)) + local file = assert(loadfile(mod)) + local fn = assert(file()) + tree = assert(fn(tree)) end end local code, posmap_or_err, err_pos = compile.tree(tree) diff --git a/moonscript/cmd/moonc.moon b/moonscript/cmd/moonc.moon index ba59b06c..3e05bb07 100644 --- a/moonscript/cmd/moonc.moon +++ b/moonscript/cmd/moonc.moon @@ -78,14 +78,16 @@ compile_file_text = (text, opts={}) -> if opts.show_parse_tree dump = require "moonscript.dump" - dump.tree tree + print dump.tree tree return true compile_time = if opts.benchmark gettime! if mod = opts.transform_module - tree = assert require(mod) tree + file = assert loadfile mod + fn = assert file! + tree = assert fn tree code, posmap_or_err, err_pos = compile.tree tree diff --git a/moonscript/dump.lua b/moonscript/dump.lua index 1496ed43..1622f38c 100644 --- a/moonscript/dump.lua +++ b/moonscript/dump.lua @@ -32,7 +32,7 @@ tree = function(block) local _list_0 = block for _index_0 = 1, #_list_0 do local value = _list_0[_index_0] - print(flat_value(value)) + flat_value(value) end end return { diff --git a/moonscript/dump.moon b/moonscript/dump.moon index a6373daa..e651c22c 100644 --- a/moonscript/dump.moon +++ b/moonscript/dump.moon @@ -12,7 +12,7 @@ value = (op) -> flat_value op tree = (block) -> - print flat_value value for value in *block + flat_value value for value in *block { :value, :tree } From ff437509f1fd63b58ea3c00fb51a10be5666dc23 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 30 Apr 2018 18:24:51 -0700 Subject: [PATCH 254/344] maybe actually make this work correctly --- moonscript/dump.lua | 15 ++++++++++----- moonscript/dump.moon | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/moonscript/dump.lua b/moonscript/dump.lua index 1622f38c..26cdd7a3 100644 --- a/moonscript/dump.lua +++ b/moonscript/dump.lua @@ -29,11 +29,16 @@ value = function(op) end local tree tree = function(block) - local _list_0 = block - for _index_0 = 1, #_list_0 do - local value = _list_0[_index_0] - flat_value(value) - end + return table.concat((function() + local _accum_0 = { } + local _len_0 = 1 + for _index_0 = 1, #block do + local value = block[_index_0] + _accum_0[_len_0] = flat_value(value) + _len_0 = _len_0 + 1 + end + return _accum_0 + end)(), "\n") end return { value = value, diff --git a/moonscript/dump.moon b/moonscript/dump.moon index e651c22c..6fbeb375 100644 --- a/moonscript/dump.moon +++ b/moonscript/dump.moon @@ -12,7 +12,7 @@ value = (op) -> flat_value op tree = (block) -> - flat_value value for value in *block + table.concat [flat_value value for value in *block], "\n" { :value, :tree } From dd2d041b5e35ac89bee450e84ed5f58dfd6dbe39 Mon Sep 17 00:00:00 2001 From: leaf Date: Thu, 7 Jun 2018 21:43:07 -0700 Subject: [PATCH 255/344] add link to discord --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 535525b1..8f7e42b0 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,10 @@ See . Online demo/compiler at . +## Join Our Community + +We just created a Discord for those interested in MoonScript. You can join us here: + ## Running Tests Tests are written in MoonScript and use [Busted](http://olivinelabs.com/busted/). From bf16c518ebb2c67eee05dc64e8e7e2bb7ab9e2ae Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 25 Jan 2019 18:45:27 -0800 Subject: [PATCH 256/344] add docs for syntax tranformer --- docs/command_line.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/docs/command_line.md b/docs/command_line.md index 31fe48ee..f0e5d4b2 100644 --- a/docs/command_line.md +++ b/docs/command_line.md @@ -130,6 +130,23 @@ compiled automatically. A full list of flags can be seen by passing the `-h` or `--help` flag. +### Syntax Transformer + +A syntax transformer is a function that manipulates MoonScript code before +compiling to Lua. It operates on the parsed AST (Abstract Syntax Tree). It can +be used to implement macros, change syntax, optimize code, among other things. +You specify the name of a Lua module. This module must return a single function +(or callable object) that takes AST as an argument, and returns the new AST. + +```bash +moonc --transform my_transfomer my_script.moon +``` + +The transform can fail by returning `nil` and an error message. MoonScript AST +is currently undocumented, so you'll have to experiment by printing out the AST +to see how to make changes. MoonScript AST is made up of standard Lua tables +that are nested. + ### Linter `moonc` contains a [lint][1] tool for statically detecting potential problems From d64467d070c3ebf4168e546c967f864d2e7871ef Mon Sep 17 00:00:00 2001 From: Shemar McLean <16985079+Shemar32@users.noreply.github.com> Date: Sun, 17 Feb 2019 14:12:28 -0500 Subject: [PATCH 257/344] "-t --output-to" should be an option not a flag --- bin/moonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index 195112c5..015979ad 100755 --- a/bin/moonc +++ b/bin/moonc @@ -10,7 +10,7 @@ parser:flag("-v --version", "Print version") parser:flag("-w --watch", "Watch file/directory for updates") parser:option("--transform", "Transform syntax tree with module") parser:mutex( - parser:flag("-t --output-to", "Specify where to place compiled files"), + parser:option("-t --output-to", "Specify where to place compiled files"), parser:option("-o", "Write output to file"), parser:flag("-p", "Write output to standard output"), parser:flag("-T", "Write parse tree instead of code (to stdout)"), From 29a780ecc4ea874f9593e51c228ed86dadc91ceb Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 16 Sep 2019 11:51:00 -0700 Subject: [PATCH 258/344] add twitch link --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8f7e42b0..74396dfd 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,8 @@ [![Build Status](https://travis-ci.org/leafo/moonscript.svg?branch=master)](https://travis-ci.org/leafo/moonscript) [![Build status](https://ci.appveyor.com/api/projects/status/f5prpi4wvytul290/branch/binaries?svg=true)](https://ci.appveyor.com/project/leafo/moonscript/branch/binaries) +[![](http://leafo.net/dump/twitch-banner.svg)](https://www.twitch.tv/moonscript) + MoonScript is a programmer friendly language that compiles into [Lua](http://www.lua.org/). It gives you the power of the fastest scripting language combined with a rich set of features. It runs on Lua 5.1 and above, From dba4b10a4559e0efaaabeee4d82a34eab5311e88 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 26 Sep 2019 10:25:01 -0700 Subject: [PATCH 259/344] exit with error code when code run with moon fails --- bin/moon | 5 +++-- bin/moon.moon | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bin/moon b/bin/moon index 5e5e4251..75f2156d 100755 --- a/bin/moon +++ b/bin/moon @@ -97,13 +97,14 @@ run = function() local truncated = errors.truncate_traceback(util.trim(trace)) local rewritten = errors.rewrite_traceback(truncated, err) if rewritten then - return print_err(rewritten) + print_err(rewritten) else - return print_err(table.concat({ + print_err(table.concat({ err, util.trim(trace) }, "\n")) end + return os.exit(1) else if cov then cov:stop() diff --git a/bin/moon.moon b/bin/moon.moon index 71bdafb0..94601d57 100644 --- a/bin/moon.moon +++ b/bin/moon.moon @@ -90,6 +90,7 @@ run = -> err, util.trim trace }, "\n" + os.exit 1 else if cov cov\stop! From 8c8f0874dfac549e47f95263a8c29b96ccc90ef5 Mon Sep 17 00:00:00 2001 From: jakbyte Date: Sun, 2 Feb 2020 13:57:54 -0500 Subject: [PATCH 260/344] README: Update links to be HTTPS (#393) * README: Update links to be HTTPS * Update Twitch link to use HTTPS --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 74396dfd..973a143c 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,20 @@ # MoonScript -[![MoonScript](http://leafo.net/dump/sailormoonscript.png)](http://moonscript.org) +[![MoonScript](https://leafo.net/dump/sailormoonscript.png)](https://moonscript.org) [![Build Status](https://travis-ci.org/leafo/moonscript.svg?branch=master)](https://travis-ci.org/leafo/moonscript) [![Build status](https://ci.appveyor.com/api/projects/status/f5prpi4wvytul290/branch/binaries?svg=true)](https://ci.appveyor.com/project/leafo/moonscript/branch/binaries) -[![](http://leafo.net/dump/twitch-banner.svg)](https://www.twitch.tv/moonscript) +[![](https://leafo.net/dump/twitch-banner.svg)](https://www.twitch.tv/moonscript) MoonScript is a programmer friendly language that compiles into -[Lua](http://www.lua.org/). It gives you the power of the fastest scripting +[Lua](https://www.lua.org/). It gives you the power of the fastest scripting language combined with a rich set of features. It runs on Lua 5.1 and above, including alternative runtimes like LuaJIT. -See . +See . -Online demo/compiler at . +Online demo/compiler at . ## Join Our Community @@ -22,7 +22,7 @@ We just created a Discord for those interested in MoonScript. You can join us he ## Running Tests -Tests are written in MoonScript and use [Busted](http://olivinelabs.com/busted/). +Tests are written in MoonScript and use [Busted](https://olivinelabs.com/busted/). In order to run the tests you must have MoonScript and [Loadkit](https://github.com/leafo/loadkit) installed. To run tests, execute from the root directory: From 39a904d419bffe9aa206935b067c40ae724c4d99 Mon Sep 17 00:00:00 2001 From: Jaxson Van Doorn Date: Sun, 22 Jul 2018 00:43:49 -0700 Subject: [PATCH 261/344] Fix typo in argument defaults section --- docs/reference.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/reference.md b/docs/reference.md index 9782f2ba..48302fb7 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -195,7 +195,7 @@ func = (num) => @value + num It is possible to provide default values for the arguments of a function. An argument is determined to be empty if its value is `nil`. Any `nil` arguments -that have a default value will be replace before the body of the function is run. +that have a default value will be replaced before the body of the function is run. ```moon my_function = (name="something", height=100) -> From 532200dc136f3a1cd5ae59a76fcab47bdce82330 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 2 Feb 2020 10:56:20 -0800 Subject: [PATCH 262/344] update copyright year, closes #394 --- README.md | 2 +- docs/reference.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 973a143c..0c247ba8 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ branch](https://github.com/leafo/moonscript/tree/binaries) ## License (MIT) -Copyright (C) 2017 by Leaf Corcoran +Copyright (C) 2020 by Leaf Corcoran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/docs/reference.md b/docs/reference.md index 48302fb7..b95e8faf 100644 --- a/docs/reference.md +++ b/docs/reference.md @@ -1565,7 +1565,7 @@ their written order you can add `local *` to the top of your file. # License (MIT) - Copyright (C) 2017 by Leaf Corcoran + Copyright (C) 2020 by Leaf Corcoran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From aad0d2a0f6dfda14ee6db12136a55ec0dfbe11b5 Mon Sep 17 00:00:00 2001 From: Qais Patankar Date: Thu, 30 Apr 2020 17:10:01 +0100 Subject: [PATCH 263/344] docs: fix `line_tabel` typo --- docs/api.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/api.md b/docs/api.md index c59f6f70..56425264 100644 --- a/docs/api.md +++ b/docs/api.md @@ -83,7 +83,7 @@ table. If there are any errors then `nil` and the error message are returned. ```moononly import to_lua from require "moonscript.base" -lua_code, line_tabel = to_lua [[ +lua_code, line_table = to_lua [[ x = 124 print "hello world #{x}" ]] From 494387236eb4c0cad1338cfb3c6e63610d657691 Mon Sep 17 00:00:00 2001 From: Nathan DECHER Date: Thu, 30 Apr 2020 23:35:24 +0200 Subject: [PATCH 264/344] Fixed moonscript generating ambiguous lua after `import` when the next line starts with `(` --- moonscript/compile.lua | 3 +-- moonscript/compile.moon | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 0715b862..58390c2a 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -94,8 +94,7 @@ do end insert(buffer, l) if "string" == type(self[i + 1]) then - local lc = l:sub(-1) - if (lc == ")" or lc == "]") and self[i + 1]:sub(1, 1) == "(" then + if l:sub(-1) ~= ',' and l:sub(-3) ~= 'end' and self[i + 1]:sub(1, 1) == "(" then insert(buffer, ";") end end diff --git a/moonscript/compile.moon b/moonscript/compile.moon index a2b17fee..716c2c4e 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -71,8 +71,7 @@ class Lines -- insert breaks between ambiguous statements if "string" == type @[i + 1] - lc = l\sub(-1) - if (lc == ")" or lc == "]") and @[i + 1]\sub(1,1) == "(" + if l\sub(-1)!=',' and l\sub(-3)!='end' and @[i + 1]\sub(1,1) == "(" insert buffer, ";" insert buffer, "\n" From 4ff4077a21a8b296cf3bf8fd7a522febc67c2165 Mon Sep 17 00:00:00 2001 From: Nathan DECHER Date: Thu, 30 Apr 2020 23:37:36 +0200 Subject: [PATCH 265/344] Fixed tests to accommodate for the new semicolons --- spec/outputs/string.lua | 2 +- spec/outputs/syntax.lua | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/outputs/string.lua b/spec/outputs/string.lua index 2d751d81..a928c5d3 100644 --- a/spec/outputs/string.lua +++ b/spec/outputs/string.lua @@ -26,7 +26,7 @@ local f = [[hello #{world} world]] a = 'hello #{hello} hello' b = '#{hello} hello' c = 'hello #{hello}' -local _ = "hello" +local _ = "hello"; ("hello"):format(1); ("hello"):format(1, 2, 3); ("hello"):format(1, 2, 3)(1, 2, 3); diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index fef1a739..52c5d88f 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -192,7 +192,7 @@ _ = 5 + what(wack) what(whack + 5) _ = 5 - what(wack) what(whack - 5) -x = hello - world - something +x = hello - world - something; (function(something) if something == nil then do From 0ac82e3af5bbb299f8e2b4d542cb3a0daafaba6c Mon Sep 17 00:00:00 2001 From: Nathan DECHER Date: Thu, 30 Apr 2020 23:54:00 +0200 Subject: [PATCH 266/344] Added tests for the semicolon insertion --- spec/inputs/ambiguous.moon | 8 ++++++++ spec/inputs/ambiguous_tables.moon | 6 ++++++ spec/outputs/ambiguous.lua | 9 +++++++++ spec/outputs/ambiguous_tables.lua | 6 ++++++ 4 files changed, 29 insertions(+) create mode 100644 spec/inputs/ambiguous.moon create mode 100644 spec/inputs/ambiguous_tables.moon create mode 100644 spec/outputs/ambiguous.lua create mode 100644 spec/outputs/ambiguous_tables.lua diff --git a/spec/inputs/ambiguous.moon b/spec/inputs/ambiguous.moon new file mode 100644 index 00000000..66109c11 --- /dev/null +++ b/spec/inputs/ambiguous.moon @@ -0,0 +1,8 @@ +a = 'b' +c = d +(a b) c d +import c from d +(a b) c d +(c d) a b +a, b = c, d +(d a) c diff --git a/spec/inputs/ambiguous_tables.moon b/spec/inputs/ambiguous_tables.moon new file mode 100644 index 00000000..c2c7ea8c --- /dev/null +++ b/spec/inputs/ambiguous_tables.moon @@ -0,0 +1,6 @@ +x = { +hello +(one) +(two) +three() +} diff --git a/spec/outputs/ambiguous.lua b/spec/outputs/ambiguous.lua new file mode 100644 index 00000000..2330da40 --- /dev/null +++ b/spec/outputs/ambiguous.lua @@ -0,0 +1,9 @@ +local a = 'b' +local c = d; +(a(b))(c(d)) +c = d.c; +(a(b))(c(d)); +(c(d))(a(b)) +local b +a, b = c, d +return (d(a))(c) \ No newline at end of file diff --git a/spec/outputs/ambiguous_tables.lua b/spec/outputs/ambiguous_tables.lua new file mode 100644 index 00000000..8af0aaab --- /dev/null +++ b/spec/outputs/ambiguous_tables.lua @@ -0,0 +1,6 @@ +local x = { + hello, + (one), + (two), + three() +} \ No newline at end of file From df7b10335d8f78df6a9f062c3b32c3d278d3984d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 16:24:11 -0800 Subject: [PATCH 267/344] recompile --- moonscript/cmd/coverage.lua | 6 ++++-- moonscript/compile.lua | 3 ++- moonscript/parse/util.lua | 3 ++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/moonscript/cmd/coverage.lua b/moonscript/cmd/coverage.lua index bed561f8..43456301 100644 --- a/moonscript/cmd/coverage.lua +++ b/moonscript/cmd/coverage.lua @@ -82,7 +82,8 @@ do process_line = function(self, _, line_no) local debug_data = debug.getinfo(2, "S") local source = debug_data.source - self.line_counts[source][line_no] = self.line_counts[source][line_no] + 1 + local _update_0, _update_1 = source, line_no + self.line_counts[_update_0][_update_1] = self.line_counts[_update_0][_update_1] + 1 end, format_results = function(self) local line_table = require("moonscript.line_tables") @@ -103,7 +104,8 @@ do _continue_1 = true break end - positions[file][position] = positions[file][position] + count + local _update_0, _update_1 = file, position + positions[_update_0][_update_1] = positions[_update_0][_update_1] + count _continue_1 = true until true if not _continue_1 then diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 58390c2a..66f4df59 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -438,7 +438,8 @@ do self.next:render(buffer) else if #self._lines == 0 and "string" == type(buffer[#buffer]) then - buffer[#buffer] = buffer[#buffer] .. (" " .. (unpack(Lines():add(self.footer)))) + local _update_0 = #buffer + buffer[_update_0] = buffer[_update_0] .. (" " .. (unpack(Lines():add(self.footer)))) else buffer:add(self._lines) buffer:add(self.footer) diff --git a/moonscript/parse/util.lua b/moonscript/parse/util.lua index 2687384b..3f02c8d5 100644 --- a/moonscript/parse/util.lua +++ b/moonscript/parse/util.lua @@ -50,7 +50,8 @@ show_line_position = function(str, pos, context) { } } for c in str:gmatch(".") do - lines[#lines] = lines[#lines] or { } + local _update_0 = #lines + lines[_update_0] = lines[_update_0] or { } table.insert(lines[#lines], c) if c == "\n" then lines[#lines + 1] = { } From 91b483323c1fa6b99d50f34bbe2180239b9e0d78 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 16:27:50 -0800 Subject: [PATCH 268/344] add github actions workflow --- .github/workflows/spec.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 .github/workflows/spec.yml diff --git a/.github/workflows/spec.yml b/.github/workflows/spec.yml new file mode 100644 index 00000000..7ddab019 --- /dev/null +++ b/.github/workflows/spec.yml @@ -0,0 +1,31 @@ +name: "spec" + +on: [push] + +jobs: + test: + strategy: + fail-fast: false + matrix: + luaVersion: ["5.1", "5.2", "5.3", "5.4", "luajit", "luajit-openresty"] + + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@master + + - uses: leafo/gh-actions-lua@master + with: + luaVersion: ${{ matrix.luaVersion }} + + - uses: leafo/gh-actions-luarocks@master + + - name: build + run: | + luarocks install busted + luarocks install loadkit + luarocks make + + - name: test + run: | + busted -o utfTerminal From 2d4732981cac9c874d37233ef6ed8c8be1e8acf1 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 16:38:49 -0800 Subject: [PATCH 269/344] test windows workflow --- .github/workflows/windows_exe.yml | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 .github/workflows/windows_exe.yml diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml new file mode 100644 index 00000000..137e8934 --- /dev/null +++ b/.github/workflows/windows_exe.yml @@ -0,0 +1,11 @@ +name: "windows_exe" + +on: [push] + +jobs: + build: + runs-on: windows-latest + + steps: + - name: hello + run: Get-ChildItem From 6cdfba0add95fd96c32afdfa746515668816af89 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 16:41:13 -0800 Subject: [PATCH 270/344] check out the code.. --- .github/workflows/windows_exe.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 137e8934..8332cb18 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -7,5 +7,6 @@ jobs: runs-on: windows-latest steps: - - name: hello - run: Get-ChildItem + - uses: actions/checkout@master + - name: hello + run: Get-ChildItem From e869c408e48a83bd149a772d58f891a90c2f6abd Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 16:43:43 -0800 Subject: [PATCH 271/344] update badge to github workflow --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c247ba8..967b84f6 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ [![MoonScript](https://leafo.net/dump/sailormoonscript.png)](https://moonscript.org) -[![Build Status](https://travis-ci.org/leafo/moonscript.svg?branch=master)](https://travis-ci.org/leafo/moonscript) [![Build status](https://ci.appveyor.com/api/projects/status/f5prpi4wvytul290/branch/binaries?svg=true)](https://ci.appveyor.com/project/leafo/moonscript/branch/binaries) + +[![spec](https://github.com/leafo/moonscript/workflows/spec/badge.svg)](https://github.com/leafo/moonscript/actions?query=workflow%3Aspec) [![Build status](https://ci.appveyor.com/api/projects/status/f5prpi4wvytul290/branch/binaries?svg=true)](https://ci.appveyor.com/project/leafo/moonscript/branch/binaries) [![](https://leafo.net/dump/twitch-banner.svg)](https://www.twitch.tv/moonscript) From 81b6394329625df6c475b42d7fd65853f55ec5b8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 16:44:28 -0800 Subject: [PATCH 272/344] remove travis --- .travis.yml | 23 -------- .travis/platform.sh | 15 ------ .travis/setenv_lua.sh | 3 -- .travis/setup_lua.sh | 122 ------------------------------------------ 4 files changed, 163 deletions(-) delete mode 100644 .travis.yml delete mode 100644 .travis/platform.sh delete mode 100644 .travis/setenv_lua.sh delete mode 100644 .travis/setup_lua.sh diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4e96f64e..00000000 --- a/.travis.yml +++ /dev/null @@ -1,23 +0,0 @@ -language: c - -sudo: false - -env: - global: - - LUAROCKS=2.2.2 - matrix: - - LUA=lua5.1 - - LUA=lua5.2 - - LUA=lua5.3 - - LUA=luajit2.1 # current head of 2.1 branch - - LUA=luajit2.1 BUSTED="-o spec/coverage_output_handler.moon" - -before_install: - - source .travis/setenv_lua.sh - -install: - - luarocks install busted - - luarocks install loadkit - - luarocks make - -script: busted $BUSTED diff --git a/.travis/platform.sh b/.travis/platform.sh deleted file mode 100644 index 7259a7d6..00000000 --- a/.travis/platform.sh +++ /dev/null @@ -1,15 +0,0 @@ -if [ -z "${PLATFORM:-}" ]; then - PLATFORM=$TRAVIS_OS_NAME; -fi - -if [ "$PLATFORM" == "osx" ]; then - PLATFORM="macosx"; -fi - -if [ -z "$PLATFORM" ]; then - if [ "$(uname)" == "Linux" ]; then - PLATFORM="linux"; - else - PLATFORM="macosx"; - fi; -fi diff --git a/.travis/setenv_lua.sh b/.travis/setenv_lua.sh deleted file mode 100644 index 8d8c8255..00000000 --- a/.travis/setenv_lua.sh +++ /dev/null @@ -1,3 +0,0 @@ -export PATH=${PATH}:$HOME/.lua:$HOME/.local/bin:${TRAVIS_BUILD_DIR}/install/luarocks/bin -bash .travis/setup_lua.sh -eval `$HOME/.lua/luarocks path` diff --git a/.travis/setup_lua.sh b/.travis/setup_lua.sh deleted file mode 100644 index 6dcc0c6e..00000000 --- a/.travis/setup_lua.sh +++ /dev/null @@ -1,122 +0,0 @@ -#! /bin/bash - -# A script for setting up environment for travis-ci testing. -# Sets up Lua and Luarocks. -# LUA must be "lua5.1", "lua5.2" or "luajit". -# luajit2.0 - master v2.0 -# luajit2.1 - master v2.1 - -set -eufo pipefail - -LUAJIT_VERSION="2.0.4" -LUAJIT_BASE="LuaJIT-$LUAJIT_VERSION" - -source .travis/platform.sh - -LUA_HOME_DIR=$TRAVIS_BUILD_DIR/install/lua - -LR_HOME_DIR=$TRAVIS_BUILD_DIR/install/luarocks - -mkdir $HOME/.lua - -LUAJIT="no" - -if [ "$PLATFORM" == "macosx" ]; then - if [ "$LUA" == "luajit" ]; then - LUAJIT="yes"; - fi - if [ "$LUA" == "luajit2.0" ]; then - LUAJIT="yes"; - fi - if [ "$LUA" == "luajit2.1" ]; then - LUAJIT="yes"; - fi; -elif [ "$(expr substr $LUA 1 6)" == "luajit" ]; then - LUAJIT="yes"; -fi - -mkdir -p "$LUA_HOME_DIR" - -if [ "$LUAJIT" == "yes" ]; then - - if [ "$LUA" == "luajit" ]; then - curl --location https://github.com/LuaJIT/LuaJIT/archive/v$LUAJIT_VERSION.tar.gz | tar xz; - else - git clone https://github.com/LuaJIT/LuaJIT.git $LUAJIT_BASE; - fi - - cd $LUAJIT_BASE - - if [ "$LUA" == "luajit2.1" ]; then - git checkout v2.1; - # force the INSTALL_TNAME to be luajit - perl -i -pe 's/INSTALL_TNAME=.+/INSTALL_TNAME= luajit/' Makefile - fi - - make && make install PREFIX="$LUA_HOME_DIR" - - ln -s $LUA_HOME_DIR/bin/luajit $HOME/.lua/luajit - ln -s $LUA_HOME_DIR/bin/luajit $HOME/.lua/lua; - -else - - if [ "$LUA" == "lua5.1" ]; then - curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz - cd lua-5.1.5; - elif [ "$LUA" == "lua5.2" ]; then - curl http://www.lua.org/ftp/lua-5.2.4.tar.gz | tar xz - cd lua-5.2.4; - elif [ "$LUA" == "lua5.3" ]; then - curl http://www.lua.org/ftp/lua-5.3.2.tar.gz | tar xz - cd lua-5.3.2; - fi - - # Build Lua without backwards compatibility for testing - perl -i -pe 's/-DLUA_COMPAT_(ALL|5_2)//' src/Makefile - make $PLATFORM - make INSTALL_TOP="$LUA_HOME_DIR" install; - - ln -s $LUA_HOME_DIR/bin/lua $HOME/.lua/lua - ln -s $LUA_HOME_DIR/bin/luac $HOME/.lua/luac; - -fi - -cd $TRAVIS_BUILD_DIR - -lua -v - -LUAROCKS_BASE=luarocks-$LUAROCKS - -curl --location http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz | tar xz - -cd $LUAROCKS_BASE - -if [ "$LUA" == "luajit" ]; then - ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.0" --prefix="$LR_HOME_DIR"; -elif [ "$LUA" == "luajit2.0" ]; then - ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.0" --prefix="$LR_HOME_DIR"; -elif [ "$LUA" == "luajit2.1" ]; then - ./configure --lua-suffix=jit --with-lua-include="$LUA_HOME_DIR/include/luajit-2.1" --prefix="$LR_HOME_DIR"; -else - ./configure --with-lua="$LUA_HOME_DIR" --prefix="$LR_HOME_DIR" -fi - -make build && make install - -ln -s $LR_HOME_DIR/bin/luarocks $HOME/.lua/luarocks - -cd $TRAVIS_BUILD_DIR - -luarocks --version - -rm -rf $LUAROCKS_BASE - -if [ "$LUAJIT" == "yes" ]; then - rm -rf $LUAJIT_BASE; -elif [ "$LUA" == "lua5.1" ]; then - rm -rf lua-5.1.5; -elif [ "$LUA" == "lua5.2" ]; then - rm -rf lua-5.2.4; -elif [ "$LUA" == "lua5.3" ]; then - rm -rf lua-5.3.2; -fi From 804b32acde91549a6216ee170958c8cc48e65ebc Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 16:48:18 -0800 Subject: [PATCH 273/344] more testing --- .github/workflows/windows_exe.yml | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 8332cb18..fef2b0f0 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -10,3 +10,12 @@ jobs: - uses: actions/checkout@master - name: hello run: Get-ChildItem + + - uses: msys2/setup-msys2@v2 + with: + install: gcc make + + - run: gcc -v + + + From d1b717d28aa96765e575b70bb258d8ddbe247ce7 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 16:59:19 -0800 Subject: [PATCH 274/344] more windows test --- .github/workflows/windows_exe.yml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index fef2b0f0..3a05fa4f 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -8,14 +8,20 @@ jobs: steps: - uses: actions/checkout@master - - name: hello - run: Get-ChildItem + - uses: msys2/setup-msys2@v2 with: - install: gcc make + install: gcc make curl - run: gcc -v + - name: Setup Lua + - run: | + curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz + tar -xZf lua-5.1.5.tar.gz + + - name: list files + run: Get-ChildItem From 3d5b9ce007454ae358ec9eb6a859e9d30c924624 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 17:01:02 -0800 Subject: [PATCH 275/344] syntax fix --- .github/workflows/windows_exe.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 3a05fa4f..44b45168 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -14,14 +14,15 @@ jobs: with: install: gcc make curl - - run: gcc -v + - name: Show GCC + run: gcc -v - name: Setup Lua - - run: | + run: | curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xZf lua-5.1.5.tar.gz - - name: list files + - name: List Files run: Get-ChildItem From 4c6eb918eb1423d26a945192ba2102c2ae394d0e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 17:04:21 -0800 Subject: [PATCH 276/344] try to build lua --- .github/workflows/windows_exe.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 44b45168..88e92821 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -21,6 +21,7 @@ jobs: run: | curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xZf lua-5.1.5.tar.gz + cd lua-5.1.5; make PLAT=mingw - name: List Files run: Get-ChildItem From 9cad63ab8b5b906a50258e99c0a6f25fbad0821d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 17:10:22 -0800 Subject: [PATCH 277/344] recurse --- .github/workflows/windows_exe.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 88e92821..f499747a 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -24,6 +24,6 @@ jobs: cd lua-5.1.5; make PLAT=mingw - name: List Files - run: Get-ChildItem + run: Get-ChildItem -Recurse From 8ae336bd05a92b5bec0a57d7ed3a7785a119585e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 17:38:50 -0800 Subject: [PATCH 278/344] test running lua --- .github/workflows/windows_exe.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index f499747a..e170ebf1 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -26,4 +26,6 @@ jobs: - name: List Files run: Get-ChildItem -Recurse + - name: Run Lua + run: lua-5.1.5/src/lua.exe -v From da1b28b1da08be616f4a97993e802e5163b1676e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 17:45:53 -0800 Subject: [PATCH 279/344] add some node transform specs --- spec/transform_spec.moon | 100 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 98 insertions(+), 2 deletions(-) diff --git a/spec/transform_spec.moon b/spec/transform_spec.moon index dba44d04..3952fdee 100644 --- a/spec/transform_spec.moon +++ b/spec/transform_spec.moon @@ -1,11 +1,107 @@ import with_dev from require "spec.helpers" + describe "moonscript.transform.destructure", -> - local extract_assign_names + local extract_assign_names, split_assign, Block with_dev -> - { :extract_assign_names } = require "moonscript.transform.destructure" + { :extract_assign_names, :split_assign } = require "moonscript.transform.destructure" + {:Block} = require "moonscript.compile" + + describe "split_assign #fff", -> + -- {:hello} = world + it "simple assignment", -> + node = { + "assign" + { + { "table", { + {{"key_literal", "hello"}, {"ref", "hello"}} + } + } + } + { + {"ref", "world"} + } + } + + out = split_assign Block!, node + + assert.same { + "group", { + { + "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "world"}, {"dot", "hello"}} } } + } + } + } + }, out + + -- a, {:hello} = one, two + it "multiple assigns", -> + node = { + "assign" + { + {"ref", "a"} + { "table", { + {{"key_literal", "hello"}, {"ref", "hello"}} + } + } + } + { + {"ref", "one"} + {"ref", "two"} + } + } + + out = split_assign Block!, node + + assert.same { + "group", { + {"assign", { {"ref", "a"} }, { {"ref", "one"} }} + + { + "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "two"}, {"dot", "hello"}} } } + } + } + } + }, out + + -- {:hello}, a = one, two + it "multiple assigns swapped #ddd", -> + node = { + "assign" + { + { "table", { + {{"key_literal", "hello"}, {"ref", "hello"}} + } + } + {"ref", "a"} + } + { + {"ref", "one"} + {"ref", "two"} + } + } + + out = split_assign Block!, node + + assert.same { + "group", { + { + "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "one"}, {"dot", "hello"}} } } + } + } + + {"assign", { {"ref", "a"} }, { {"ref", "two"} }} + } + }, out + it "extracts names from table destructure", -> des = { From ce3a3f8bd98144477e4dd1d0601211f29d923138 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 17:56:09 -0800 Subject: [PATCH 280/344] fetch binary deps --- .github/workflows/windows_exe.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index e170ebf1..97579be5 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -23,6 +23,16 @@ jobs: tar -xZf lua-5.1.5.tar.gz cd lua-5.1.5; make PLAT=mingw + - name: Get LPeg + run: | + curl -o lpeg.tar.gz http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz + tar -xZf lpeg.tar.gz + + - name: Get Luafilesystem + run: | + curl -o luafilesystem.tar.gz https://github.com/keplerproject/luafilesystem/archive/v1_8_0.tar.gz + tar -xZf luafilesystem.tar.gz + - name: List Files run: Get-ChildItem -Recurse From 13735f711da77f99b719606e88a731b12dde4f7a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 18:04:33 -0800 Subject: [PATCH 281/344] reformat things, add complex value temporary storage spec --- spec/transform_spec.moon | 81 +++++++++++++++++++++++++--------------- 1 file changed, 51 insertions(+), 30 deletions(-) diff --git a/spec/transform_spec.moon b/spec/transform_spec.moon index 3952fdee..9bb06a92 100644 --- a/spec/transform_spec.moon +++ b/spec/transform_spec.moon @@ -27,16 +27,45 @@ describe "moonscript.transform.destructure", -> out = split_assign Block!, node - assert.same { - "group", { - { - "group", { - { "declare", { {"ref", "hello"} } } - { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "world"}, {"dot", "hello"}} } } + assert.same { "group", { + { "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "world"}, {"dot", "hello"}} } } + }} + }}, out + + -- {:a, :b} = world! + -- a complex value should never be repeated to avoid double execution + it "complex value", -> + node = { + "assign" + { + { "table", { + {{"key_literal", "a"}, {"ref", "a"}} + {{"key_literal", "b"}, {"ref", "b"}} } } } - }, out + { + {"chain", {"ref", "world"}, {"call", {}}} + } + } + + out = split_assign Block!, node + + -- the temp name the result is stored into + tmp = {"temp_name", prefix: "obj"} + + assert.same { "group", { + { "group", { + { "declare", { {"ref", "a"}, {"ref", "b"} } } + + { "do", { + {"assign", { tmp }, { {"chain", {"ref", "world"}, {"call", {}}} } } + {"assign", { {"ref", "a"}, {"ref", "b"} }, { {"chain", tmp, {"dot", "a"}}, {"chain", tmp, {"dot", "b"}} } } + }} + }} + }}, out -- a, {:hello} = one, two it "multiple assigns", -> @@ -57,21 +86,17 @@ describe "moonscript.transform.destructure", -> out = split_assign Block!, node - assert.same { - "group", { - {"assign", { {"ref", "a"} }, { {"ref", "one"} }} + assert.same { "group", { + {"assign", { {"ref", "a"} }, { {"ref", "one"} }} - { - "group", { - { "declare", { {"ref", "hello"} } } - { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "two"}, {"dot", "hello"}} } } - } - } - } - }, out + { "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "two"}, {"dot", "hello"}} } } + }} + }}, out -- {:hello}, a = one, two - it "multiple assigns swapped #ddd", -> + it "multiple assigns swapped", -> node = { "assign" { @@ -89,18 +114,14 @@ describe "moonscript.transform.destructure", -> out = split_assign Block!, node - assert.same { - "group", { - { - "group", { - { "declare", { {"ref", "hello"} } } - { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "one"}, {"dot", "hello"}} } } - } - } + assert.same { "group", { + { "group", { + { "declare", { {"ref", "hello"} } } + { "assign", { {"ref", "hello"} }, { {"chain", {"ref", "one"}, {"dot", "hello"}} } } + }} - {"assign", { {"ref", "a"} }, { {"ref", "two"} }} - } - }, out + {"assign", { {"ref", "a"} }, { {"ref", "two"} }} + }}, out it "extracts names from table destructure", -> From e2a44b040cc33e2d4f2d259aa16dac316b281bfc Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 18:10:09 -0800 Subject: [PATCH 282/344] follow redirect --- .github/workflows/windows_exe.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 97579be5..6b7ef1be 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -30,7 +30,7 @@ jobs: - name: Get Luafilesystem run: | - curl -o luafilesystem.tar.gz https://github.com/keplerproject/luafilesystem/archive/v1_8_0.tar.gz + curl -L -o luafilesystem.tar.gz https://github.com/keplerproject/luafilesystem/archive/v1_8_0.tar.gz tar -xZf luafilesystem.tar.gz - name: List Files From 21226eb5e291fad1b28b1605bb63892cd96fc8e3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 18:22:22 -0800 Subject: [PATCH 283/344] add compiler error when building invalid descructure --- moonscript/transform/destructure.lua | 1 + moonscript/transform/destructure.moon | 2 ++ 2 files changed, 3 insertions(+) diff --git a/moonscript/transform/destructure.lua b/moonscript/transform/destructure.lua index 2ec46bef..1e370b7d 100644 --- a/moonscript/transform/destructure.lua +++ b/moonscript/transform/destructure.lua @@ -103,6 +103,7 @@ extract_assign_names = function(name, accum, prefix) end local build_assign build_assign = function(scope, destruct_literal, receiver) + assert(receiver, "attempting to build destructure assign with no receiver") local extracted_names = extract_assign_names(destruct_literal) local names = { } local values = { } diff --git a/moonscript/transform/destructure.moon b/moonscript/transform/destructure.moon index 5b22247a..b0413b2d 100644 --- a/moonscript/transform/destructure.moon +++ b/moonscript/transform/destructure.moon @@ -54,6 +54,8 @@ extract_assign_names = (name, accum={}, prefix={}) -> accum build_assign = (scope, destruct_literal, receiver) -> + assert receiver, "attempting to build destructure assign with no receiver" + extracted_names = extract_assign_names destruct_literal names = {} From ef0e0f6ad2446e5eba8d816ef1e495f9541774e4 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 18:26:59 -0800 Subject: [PATCH 284/344] build a c file --- .github/workflows/windows_exe.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 6b7ef1be..fd0ee859 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -39,3 +39,7 @@ jobs: - name: Run Lua run: lua-5.1.5/src/lua.exe -v + - name: Build + run: gcc -o moon.exe -c bin/binaries/moon.c + + From d88e2e2dcf4f19805d905831f295d956ba83bcbb Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 18:29:03 -0800 Subject: [PATCH 285/344] check in missing file :) --- bin/binaries/moon.c | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 bin/binaries/moon.c diff --git a/bin/binaries/moon.c b/bin/binaries/moon.c new file mode 100644 index 00000000..ea668ed9 --- /dev/null +++ b/bin/binaries/moon.c @@ -0,0 +1,4 @@ + +// this does nothing until we get a build working for all deps +int main(int argc, char **argv) { +} From 62c92ac2dca76b1b0437aaaabc1f623ceab728f4 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 18:38:31 -0800 Subject: [PATCH 286/344] try building lpeg with bin --- .github/workflows/windows_exe.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index fd0ee859..f68c65bd 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -40,6 +40,8 @@ jobs: run: lua-5.1.5/src/lua.exe -v - name: Build - run: gcc -o moon.exe -c bin/binaries/moon.c + run: gcc -o moon.exe -ilua-5.1.5/src/ -c bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + + From 36071113dff4ce265945e974223a06003a189c13 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 22 Nov 2020 18:41:23 -0800 Subject: [PATCH 287/344] quote arg? --- .github/workflows/windows_exe.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index f68c65bd..52d18cf5 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -40,7 +40,7 @@ jobs: run: lua-5.1.5/src/lua.exe -v - name: Build - run: gcc -o moon.exe -ilua-5.1.5/src/ -c bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + run: gcc -o moon.exe '-ilua-5.1.5/src/' -c bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c From 03bdcf668a1a17b07414d64594363420f5419af0 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 23 Nov 2020 11:23:55 -0800 Subject: [PATCH 288/344] fix arg --- .github/workflows/windows_exe.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 52d18cf5..9fdd7abc 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -40,7 +40,7 @@ jobs: run: lua-5.1.5/src/lua.exe -v - name: Build - run: gcc -o moon.exe '-ilua-5.1.5/src/' -c bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + run: gcc -o moon.exe '-Ilua-5.1.5/src/' -c bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c From e445bb51e63a7b6dc369c157f4cba15ac0c5c1be Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 23 Nov 2020 11:41:33 -0800 Subject: [PATCH 289/344] remove extraneous arg --- .github/workflows/windows_exe.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/windows_exe.yml index 9fdd7abc..fd23805d 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/windows_exe.yml @@ -40,7 +40,7 @@ jobs: run: lua-5.1.5/src/lua.exe -v - name: Build - run: gcc -o moon.exe '-Ilua-5.1.5/src/' -c bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + run: gcc -o moon.exe '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c From 29d8ccc53f8c49f4cd166444bd8a0721487297d6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 25 Nov 2020 14:46:25 -0800 Subject: [PATCH 290/344] run tests on pull request --- .github/workflows/spec.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/spec.yml b/.github/workflows/spec.yml index 7ddab019..91707d8c 100644 --- a/.github/workflows/spec.yml +++ b/.github/workflows/spec.yml @@ -1,6 +1,6 @@ name: "spec" -on: [push] +on: [push, pull_request] jobs: test: From ca9ccfe4301c4c78fbd4e737e952604e5fb810b1 Mon Sep 17 00:00:00 2001 From: codinget Date: Tue, 29 Dec 2020 17:09:33 +0100 Subject: [PATCH 291/344] Theoretically fix `-` flag for `moonc` `moonc` currently tells argparse to expect `-`, but actually checks for `--` on the cli, which breaks `moonc -`; this should in theory fix this --- bin/moonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index 015979ad..e462d83b 100755 --- a/bin/moonc +++ b/bin/moonc @@ -20,7 +20,7 @@ parser:mutex( parser:flag("-", "Read from standard in, print to standard out (Must be only argument)") -local read_stdin = arg[1] == "--" -- luacheck: ignore 113 +local read_stdin = arg[1] == "-" -- luacheck: ignore 113 if not read_stdin then parser:argument("file/directory"):args("+") From f04ae47efbfdf0d2e92b60b6f5565a694d8c79a4 Mon Sep 17 00:00:00 2001 From: Codinget Date: Tue, 29 Dec 2020 17:14:52 +0100 Subject: [PATCH 292/344] fix moonc - --- bin/moonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/moonc b/bin/moonc index e462d83b..98e8eda0 100755 --- a/bin/moonc +++ b/bin/moonc @@ -26,7 +26,7 @@ if not read_stdin then parser:argument("file/directory"):args("+") end -local opts = parser:parse() +local opts = read_stdin and {} or parser:parse() if opts.version then local v = require "moonscript.version" From 7de31c59d3c8509032b7fa2d611c03f74bd9c176 Mon Sep 17 00:00:00 2001 From: Codinget Date: Tue, 29 Dec 2020 17:16:44 +0100 Subject: [PATCH 293/344] force - to be the only argument --- bin/moonc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/bin/moonc b/bin/moonc index 98e8eda0..0d8dd4a9 100755 --- a/bin/moonc +++ b/bin/moonc @@ -24,6 +24,11 @@ local read_stdin = arg[1] == "-" -- luacheck: ignore 113 if not read_stdin then parser:argument("file/directory"):args("+") +else + if arg[2] ~= nil then + io.stderr:write("- must be the only argument\n") + os.exit(1) + end end local opts = read_stdin and {} or parser:parse() From b7efcd131046ed921ae1075d7c0f6a3b64a570f7 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 18 Mar 2021 11:51:52 -0700 Subject: [PATCH 294/344] show class name when dumping in front of { --- moonscript/util.lua | 6 +++++- moonscript/util.moon | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/moonscript/util.lua b/moonscript/util.lua index bde0d576..714767ed 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -102,7 +102,11 @@ dump = function(what) lines = _accum_0 end seen[what] = false - return "{\n" .. concat(lines) .. (" "):rep((depth - 1) * 4) .. "}\n" + local class_name + if what.__class then + class_name = "<" .. tostring(what.__class.__name) .. ">" + end + return tostring(class_name or "") .. "{\n" .. concat(lines) .. (" "):rep((depth - 1) * 4) .. "}\n" else return tostring(what) .. "\n" end diff --git a/moonscript/util.moon b/moonscript/util.moon index ea7c8a5b..9516c8bc 100644 --- a/moonscript/util.moon +++ b/moonscript/util.moon @@ -70,7 +70,10 @@ dump = (what) -> seen[what] = false - "{\n" .. concat(lines) .. (" ")\rep((depth - 1)*4) .. "}\n" + class_name = if what.__class + "<#{what.__class.__name}>" + + "#{class_name or ""}{\n" .. concat(lines) .. (" ")\rep((depth - 1)*4) .. "}\n" else tostring(what).."\n" From b3dfdc9cad86d09e7f13de79c8638a9525c163d6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 2 Nov 2022 14:45:36 -0700 Subject: [PATCH 295/344] fix indentation in file --- bin/moonc | 323 +++++++++++++++++++++++++++--------------------------- 1 file changed, 163 insertions(+), 160 deletions(-) diff --git a/bin/moonc b/bin/moonc index 015979ad..09785d08 100755 --- a/bin/moonc +++ b/bin/moonc @@ -6,38 +6,41 @@ local lfs = require "lfs" local parser = argparse() parser:flag("-l --lint", "Perform a lint on the file instead of compiling") + parser:flag("-v --version", "Print version") parser:flag("-w --watch", "Watch file/directory for updates") parser:option("--transform", "Transform syntax tree with module") + parser:mutex( - parser:option("-t --output-to", "Specify where to place compiled files"), - parser:option("-o", "Write output to file"), - parser:flag("-p", "Write output to standard output"), - parser:flag("-T", "Write parse tree instead of code (to stdout)"), - parser:flag("-b", "Write parse and compile time instead of code(to stdout)"), - parser:flag("-X", "Write line rewrite map instead of code (to stdout)") + parser:option("-t --output-to", "Specify where to place compiled files"), + parser:option("-o", "Write output to file"), + parser:flag("-p", "Write output to standard output"), + parser:flag("-T", "Write parse tree instead of code (to stdout)"), + parser:flag("-b", "Write parse and compile time instead of code(to stdout)"), + parser:flag("-X", "Write line rewrite map instead of code (to stdout)") ) + parser:flag("-", - "Read from standard in, print to standard out (Must be only argument)") + "Read from standard in, print to standard out (Must be only argument)") local read_stdin = arg[1] == "--" -- luacheck: ignore 113 if not read_stdin then - parser:argument("file/directory"):args("+") + parser:argument("file/directory"):args("+") end local opts = parser:parse() if opts.version then - local v = require "moonscript.version" - v.print_version() - os.exit() + local v = require "moonscript.version" + v.print_version() + os.exit() end function log_msg(...) - if not opts.p then - io.stderr:write(table.concat({...}, " ") .. "\n") - end + if not opts.p then + io.stderr:write(table.concat({...}, " ") .. "\n") + end end local moonc = require("moonscript.cmd.moonc") @@ -47,186 +50,186 @@ local compile_and_write = moonc.compile_and_write local path_to_target = moonc.path_to_target local function scan_directory(root, collected) - root = normalize_dir(root) - collected = collected or {} - - for fname in lfs.dir(root) do - if not fname:match("^%.") then - local full_path = root..fname - - if lfs.attributes(full_path, "mode") == "directory" then - scan_directory(full_path, collected) - elseif fname:match("%.moon$") then - table.insert(collected, full_path) - end - end - end - - return collected + root = normalize_dir(root) + collected = collected or {} + + for fname in lfs.dir(root) do + if not fname:match("^%.") then + local full_path = root..fname + + if lfs.attributes(full_path, "mode") == "directory" then + scan_directory(full_path, collected) + elseif fname:match("%.moon$") then + table.insert(collected, full_path) + end + end + end + + return collected end local function remove_dups(tbl, key_fn) - local hash = {} - local final = {} - - for _, v in ipairs(tbl) do - local dup_key = key_fn and key_fn(v) or v - if not hash[dup_key] then - table.insert(final, v) - hash[dup_key] = true - end - end - - return final + local hash = {} + local final = {} + + for _, v in ipairs(tbl) do + local dup_key = key_fn and key_fn(v) or v + if not hash[dup_key] then + table.insert(final, v) + hash[dup_key] = true + end + end + + return final end -- creates tuples of input and target local function get_files(fname, files) - files = files or {} - - if lfs.attributes(fname, "mode") == "directory" then - for _, sub_fname in ipairs(scan_directory(fname)) do - table.insert(files, { - sub_fname, - path_to_target(sub_fname, opts.output_to, fname) - }) - end - else - table.insert(files, { - fname, - path_to_target(fname, opts.output_to) - }) - end - - return files + files = files or {} + + if lfs.attributes(fname, "mode") == "directory" then + for _, sub_fname in ipairs(scan_directory(fname)) do + table.insert(files, { + sub_fname, + path_to_target(sub_fname, opts.output_to, fname) + }) + end + else + table.insert(files, { + fname, + path_to_target(fname, opts.output_to) + }) + end + + return files end if read_stdin then - local parse = require "moonscript.parse" - local compile = require "moonscript.compile" + local parse = require "moonscript.parse" + local compile = require "moonscript.compile" - local text = io.stdin:read("*a") - local tree, err = parse.string(text) + local text = io.stdin:read("*a") + local tree, err = parse.string(text) - if not tree then error(err) end - local code, err, pos = compile.tree(tree) + if not tree then error(err) end + local code, err, pos = compile.tree(tree) - if not code then - error(compile.format_error(err, pos, text)) - end + if not code then + error(compile.format_error(err, pos, text)) + end - print(code) - os.exit() + print(code) + os.exit() end local inputs = opts["file/directory"] local files = {} for _, input in ipairs(inputs) do - get_files(input, files) + get_files(input, files) end files = remove_dups(files, function(f) - return f[2] + return f[2] end) -- returns an iterator that returns files that have been updated local function create_watcher(files) - local watchers = require("moonscript.cmd.watchers") + local watchers = require("moonscript.cmd.watchers") - if watchers.InotifyWacher:available() then - return watchers.InotifyWacher(files):each_update() - end + if watchers.InotifyWacher:available() then + return watchers.InotifyWacher(files):each_update() + end - return watchers.SleepWatcher(files):each_update() + return watchers.SleepWatcher(files):each_update() end if opts.watch then - -- build function to check for lint or compile in watch - local handle_file - if opts.lint then - local lint = require "moonscript.cmd.lint" - handle_file = lint.lint_file - else - handle_file = compile_and_write - end - - local watcher = create_watcher(files) - -- catches interrupt error for ctl-c - local protected = function() - local status, file = true, watcher() - if status then - return file - elseif file ~= "interrupted!" then - error(file) - end - end - - for fname in protected do - local target = path_to_target(fname, opts.t) - - if opts.o then - target = opts.o - end - - local success, err = handle_file(fname, target) - if opts.lint then - if success then - io.stderr:write(success .. "\n\n") - elseif err then - io.stderr:write(fname .. "\n" .. err .. "\n\n") - end - elseif not success then - io.stderr:write(table.concat({ - "", - "Error: " .. fname, - err, - "\n", - }, "\n")) - elseif success == "build" then - log_msg("Built", fname, "->", target) - end - end - - io.stderr:write("\nQuitting...\n") + -- build function to check for lint or compile in watch + local handle_file + if opts.lint then + local lint = require "moonscript.cmd.lint" + handle_file = lint.lint_file + else + handle_file = compile_and_write + end + + local watcher = create_watcher(files) + -- catches interrupt error for ctl-c + local protected = function() + local status, file = true, watcher() + if status then + return file + elseif file ~= "interrupted!" then + error(file) + end + end + + for fname in protected do + local target = path_to_target(fname, opts.t) + + if opts.o then + target = opts.o + end + + local success, err = handle_file(fname, target) + if opts.lint then + if success then + io.stderr:write(success .. "\n\n") + elseif err then + io.stderr:write(fname .. "\n" .. err .. "\n\n") + end + elseif not success then + io.stderr:write(table.concat({ + "", + "Error: " .. fname, + err, + "\n", + }, "\n")) + elseif success == "build" then + log_msg("Built", fname, "->", target) + end + end + + io.stderr:write("\nQuitting...\n") elseif opts.lint then - local has_linted_with_error; - local lint = require "moonscript.cmd.lint" - for _, tuple in pairs(files) do - local fname = tuple[1] - local res, err = lint.lint_file(fname) - if res then - has_linted_with_error = true - io.stderr:write(res .. "\n\n") - elseif err then - has_linted_with_error = true - io.stderr:write(fname .. "\n" .. err.. "\n\n") - end - end - if has_linted_with_error then - os.exit(1) - end + local has_linted_with_error; + local lint = require "moonscript.cmd.lint" + for _, tuple in pairs(files) do + local fname = tuple[1] + local res, err = lint.lint_file(fname) + if res then + has_linted_with_error = true + io.stderr:write(res .. "\n\n") + elseif err then + has_linted_with_error = true + io.stderr:write(fname .. "\n" .. err.. "\n\n") + end + end + if has_linted_with_error then + os.exit(1) + end else - for _, tuple in ipairs(files) do - local fname, target = util.unpack(tuple) - if opts.o then - target = opts.o - end - - local success, err = compile_and_write(fname, target, { - print = opts.p, - fname = fname, - benchmark = opts.b, - show_posmap = opts.X, - show_parse_tree = opts.T, - transform_module = opts.transform - }) - - if not success then - io.stderr:write(fname .. "\t" .. err .. "\n") - os.exit(1) - end - end + for _, tuple in ipairs(files) do + local fname, target = util.unpack(tuple) + if opts.o then + target = opts.o + end + + local success, err = compile_and_write(fname, target, { + print = opts.p, + fname = fname, + benchmark = opts.b, + show_posmap = opts.X, + show_parse_tree = opts.T, + transform_module = opts.transform + }) + + if not success then + io.stderr:write(fname .. "\t" .. err .. "\n") + os.exit(1) + end + end end From 87fa9e8da828594bf03f871fbdde31b8f6dcfb03 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 12:49:30 -0700 Subject: [PATCH 296/344] clean up the makefile --- Makefile | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Makefile b/Makefile index d1c2b1f7..5f29047f 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ LUA ?= lua5.1 LUA_VERSION = $(shell $(LUA) -e 'print(_VERSION:match("%d%.%d"))') -LUAROCKS = luarocks-$(LUA_VERSION) +LUAROCKS = luarocks --lua-version=$(LUA_VERSION) LUA_PATH_MAKE = $(shell $(LUAROCKS) path --lr-path);./?.lua;./?/init.lua LUA_CPATH_MAKE = $(shell $(LUAROCKS) path --lr-cpath);./?.so @@ -25,12 +25,6 @@ compile: $(LUA) bin/moonc -p bin/moon.moon >> bin/moon echo "-- vim: set filetype=lua:" >> bin/moon -compile_system: - moonc moon/ moonscript/ - echo "#!/usr/bin/env lua" > bin/moon - moonc -p bin/moon.moon >> bin/moon - echo "-- vim: set filetype=lua:" >> bin/moon - watch: moonc moon/ moonscript/ && moonc -w moon/ moonscript/ From 8bd7dc27b725faa3250ce572cab5b611bd2a398f Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:04:11 -0700 Subject: [PATCH 297/344] more misc makefile changes --- Makefile | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 5f29047f..0582488e 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,13 @@ LUAROCKS = luarocks --lua-version=$(LUA_VERSION) LUA_PATH_MAKE = $(shell $(LUAROCKS) path --lr-path);./?.lua;./?/init.lua LUA_CPATH_MAKE = $(shell $(LUAROCKS) path --lr-cpath);./?.so -.PHONY: test local compile compile_system watch lint count show +.PHONY: test local build watch lint count show -test: - busted +build: + LUA_PATH='$(LUA_PATH_MAKE)' LUA_CPATH='$(LUA_CPATH_MAKE)' $(LUA) bin/moonc moon/ moonscript/ + echo "#!/usr/bin/env lua" > bin/moon + $(LUA) bin/moonc -p bin/moon.moon >> bin/moon + echo "-- vim: set filetype=lua:" >> bin/moon show: # LUA $(LUA) @@ -16,14 +19,11 @@ show: # LUA_PATH_MAKE $(LUA_PATH_MAKE) # LUA_CPATH_MAKE $(LUA_CPATH_MAKE) -local: compile - LUA_PATH='$(LUA_PATH_MAKE)' LUA_CPATH='$(LUA_CPATH_MAKE)' $(LUAROCKS) make --local moonscript-dev-1.rockspec +test: build + busted -compile: - LUA_PATH='$(LUA_PATH_MAKE)' LUA_CPATH='$(LUA_CPATH_MAKE)' $(LUA) bin/moonc moon/ moonscript/ - echo "#!/usr/bin/env lua" > bin/moon - $(LUA) bin/moonc -p bin/moon.moon >> bin/moon - echo "-- vim: set filetype=lua:" >> bin/moon +local: build + LUA_PATH='$(LUA_PATH_MAKE)' LUA_CPATH='$(LUA_CPATH_MAKE)' $(LUAROCKS) make --local moonscript-dev-1.rockspec watch: moonc moon/ moonscript/ && moonc -w moon/ moonscript/ From 0af9a8df8c283d643e5001ab665cf42dd26208a2 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:06:37 -0700 Subject: [PATCH 298/344] bump up argparse dep --- moonscript-dev-1.rockspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/moonscript-dev-1.rockspec b/moonscript-dev-1.rockspec index 8970e306..b094bfaf 100644 --- a/moonscript-dev-1.rockspec +++ b/moonscript-dev-1.rockspec @@ -16,7 +16,7 @@ description = { dependencies = { "lua >= 5.1", "lpeg >= 0.10, ~= 0.11", - "argparse >= 0.5", + "argparse >= 0.7", "luafilesystem >= 1.5" } From 1ba34563f99e3e8a9d6685a0c84c3970a4885ede Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:07:16 -0700 Subject: [PATCH 299/344] allow lua keyword property access on self #410 --- moonscript/cmd/lint.lua | 3 ++- moonscript/compile/value.lua | 38 +++++++++++++++++++++++++++++++++-- moonscript/compile/value.moon | 14 +++++++++++-- spec/inputs/syntax.moon | 3 +++ spec/outputs/syntax.lua | 2 ++ 5 files changed, 55 insertions(+), 5 deletions(-) diff --git a/moonscript/cmd/lint.lua b/moonscript/cmd/lint.lua index 8f036ff1..68053ca5 100644 --- a/moonscript/cmd/lint.lua +++ b/moonscript/cmd/lint.lua @@ -76,7 +76,8 @@ do _continue_0 = true break end - names_by_position[pos] = names_by_position[pos] or { } + local _update_0 = pos + names_by_position[_update_0] = names_by_position[_update_0] or { } insert(names_by_position[pos], name) _continue_0 = true until true diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index ce20dadd..913f9653 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -296,10 +296,44 @@ return { return self:line("not ", self:value(node[2])) end, self = function(self, node) - return "self." .. self:name(node[2]) + if data.lua_keywords[node[2]] then + return self:value({ + "chain", + "self", + { + "index", + { + "string", + '"', + node[2] + } + } + }) + else + return "self." .. self:name(node[2]) + end end, self_class = function(self, node) - return "self.__class." .. self:name(node[2]) + if data.lua_keywords[node[2]] then + return self:value({ + "chain", + "self", + { + "dot", + "__class" + }, + { + "index", + { + "string", + '"', + node[2] + } + } + }) + else + return "self.__class." .. self:name(node[2]) + end end, self_colon = function(self, node) return "self:" .. self:name(node[2]) diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 7cfdda4e..0c07898d 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -183,10 +183,20 @@ string_chars = { @line "not ", @value node[2] self: (node) => - "self."..@name node[2] + if data.lua_keywords[node[2]] + @value {"chain", "self", {"index", { + "string", '"', node[2] + }}} + else + "self."..@name node[2] self_class: (node) => - "self.__class."..@name node[2] + if data.lua_keywords[node[2]] + @value {"chain", "self", {"dot", "__class"}, {"index", { + "string", '"', node[2] + }}} + else + "self.__class."..@name node[2] self_colon: (node) => "self:"..@name node[2] diff --git a/spec/inputs/syntax.moon b/spec/inputs/syntax.moon index 854f6297..b5b92a84 100644 --- a/spec/inputs/syntax.moon +++ b/spec/inputs/syntax.moon @@ -158,6 +158,9 @@ hello ..= "world" @@something += 10 @something += 10 +@@then += 10 +@then += 10 + a["hello"] += 10 a["hello#{tostring ff}"] += 10 a[four].x += 10 diff --git a/spec/outputs/syntax.lua b/spec/outputs/syntax.lua index 52c5d88f..8667f348 100644 --- a/spec/outputs/syntax.lua +++ b/spec/outputs/syntax.lua @@ -162,6 +162,8 @@ local m = m % 2 local hello = hello .. "world" self.__class.something = self.__class.something + 10 self.something = self.something + 10 +self.__class["then"] = self.__class["then"] + 10 +self["then"] = self["then"] + 10 local _update_0 = "hello" a[_update_0] = a[_update_0] + 10 local _update_1 = "hello" .. tostring(tostring(ff)) From 66cc505f94de2724f4d73a6c6faf86ae08a9b7c6 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:12:10 -0700 Subject: [PATCH 300/344] minor makefile/spec tweaks --- Makefile | 4 ++++ spec/README.md | 10 ---------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 0582488e..4df5d776 100644 --- a/Makefile +++ b/Makefile @@ -22,6 +22,10 @@ show: test: build busted + +build_test_outputs: build + BUILD=1 busted spec/lang_spec.moon + local: build LUA_PATH='$(LUA_PATH_MAKE)' LUA_CPATH='$(LUA_CPATH_MAKE)' $(LUAROCKS) make --local moonscript-dev-1.rockspec diff --git a/spec/README.md b/spec/README.md index 37699484..bdffb54e 100644 --- a/spec/README.md +++ b/spec/README.md @@ -44,13 +44,3 @@ describe "moonscript.base", -> `with_dev`'s require function will load the `.lua` files in the local directory, not the `moon` ones. You're responsible for compiling them first before running the tests. - -You might do - -```bash -$ make compile_system; busted -``` - -> `make compile_system` is a makefile task included in the repo that will build -> MoonScript in the current directory with the version installed to the system - From 75aa95d8f36154b68f051e9e664d77a95044f6b1 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:22:28 -0700 Subject: [PATCH 301/344] update readme about building the syntax tests --- spec/README.md | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/spec/README.md b/spec/README.md index bdffb54e..b5ba9594 100644 --- a/spec/README.md +++ b/spec/README.md @@ -1,6 +1,8 @@ # MoonScript spec guide +## Testing the right code + Because MoonScript is written in MoonScript, and MoonScript specs are written in MoonScript, you need to be aware of which copy of MoonScript is actually executing the specs. @@ -19,9 +21,14 @@ MoonScript available in the load path: When developing you want to make ensure the tests are executing your changes in the current directory, and not testing the system install. -Code running in Busted will have the system install take precedence over the -loaded version. That means that if you `require "moonscript.base"` for a test, -you won't get the local copy. +Busted itself is MoonScript aware, so it means it should have a functional +MoonScript compiler in order to load the `.moon` test files. This should be the +system install. After booting your specs though, you would like to use the +current directory version of MoonScript to the test + +Because by default Busted will have the system install take precedence over the +loaded version, running `require "moonscript.base"` within a test you won't get +the working directory version of the code that you should be testing. The `with_dev` spec helper will ensure that any require calls within the spec that ask for MoonScript modules. `with_dev` calls a setup and teardown that @@ -40,7 +47,33 @@ describe "moonscript.base", -> moonscript.load "print 12" ``` - -`with_dev`'s require function will load the `.lua` files in the local -directory, not the `moon` ones. You're responsible for compiling them first +Note that `with_dev`'s `require` function will not use the MoonLoader, it will +only load the `.lua` files in the working directory directory, not the `moon` +ones. This means you must compile the working directory version of MoonScript before running the tests. + +There is a make task to conveniently do all of this: + +``` +make test +``` + +## Building syntax tests + +The test suite has a series of *syntax* tests (`spec/lang_spec.moon`) that +consist of a bunch of `.moon` files and their expected output. These files +should capture a large range of syntax that can be verified to have the correct +output when you make changes to the language. + +If you are adding new syntax, or changing the expected output, then these tests +will fail until you rebuild the expected outputs. You can do this by running +the syntax test suite with the `BUILD` environment variable set. + +There is a make task to conveniently do this: + +``` +make build_test_outputs +``` + + + From 791e015bbea05038764c30ef46103546ba4dbb74 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:22:43 -0700 Subject: [PATCH 302/344] bring back build from system --- Makefile | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 4df5d776..ed924438 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,14 @@ build: $(LUA) bin/moonc -p bin/moon.moon >> bin/moon echo "-- vim: set filetype=lua:" >> bin/moon + +# This will rebuild MoonScript from the (hopefully working) system installation of moonc +build_from_system: + moonc moon/ moonscript/ + echo "#!/usr/bin/env lua" > bin/moon + moonc -p bin/moon.moon >> bin/moon + echo "-- vim: set filetype=lua:" >> bin/moon + show: # LUA $(LUA) # LUA_VERSION $(LUA_VERSION) @@ -22,7 +30,6 @@ show: test: build busted - build_test_outputs: build BUILD=1 busted spec/lang_spec.moon From c7be4dcb387af640aa226969486e556058361319 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:25:24 -0700 Subject: [PATCH 303/344] use name here --- moonscript/compile/value.lua | 4 ++-- moonscript/compile/value.moon | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 913f9653..068035d0 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -305,7 +305,7 @@ return { { "string", '"', - node[2] + self:name(node[2]) } } }) @@ -327,7 +327,7 @@ return { { "string", '"', - node[2] + self:name(node[2]) } } }) diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 0c07898d..677c08cc 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -185,7 +185,7 @@ string_chars = { self: (node) => if data.lua_keywords[node[2]] @value {"chain", "self", {"index", { - "string", '"', node[2] + "string", '"', @name node[2] }}} else "self."..@name node[2] @@ -193,7 +193,7 @@ string_chars = { self_class: (node) => if data.lua_keywords[node[2]] @value {"chain", "self", {"dot", "__class"}, {"index", { - "string", '"', node[2] + "string", '"', @name node[2] }}} else "self.__class."..@name node[2] From a1265cf7a11c137d8caa0eff89fddb46c24d9903 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:29:46 -0700 Subject: [PATCH 304/344] misc cleanups --- moonscript/compile/value.lua | 18 ++++++++++-------- moonscript/compile/value.moon | 19 +++++++++++-------- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/moonscript/compile/value.lua b/moonscript/compile/value.lua index 068035d0..ca6e5041 100644 --- a/moonscript/compile/value.lua +++ b/moonscript/compile/value.lua @@ -296,7 +296,8 @@ return { return self:line("not ", self:value(node[2])) end, self = function(self, node) - if data.lua_keywords[node[2]] then + local field_name = self:name(node[2]) + if data.lua_keywords[field_name] then return self:value({ "chain", "self", @@ -305,16 +306,17 @@ return { { "string", '"', - self:name(node[2]) + field_name } } }) else - return "self." .. self:name(node[2]) + return "self." .. tostring(field_name) end end, self_class = function(self, node) - if data.lua_keywords[node[2]] then + local field_name = self:name(node[2]) + if data.lua_keywords[field_name] then return self:value({ "chain", "self", @@ -327,19 +329,19 @@ return { { "string", '"', - self:name(node[2]) + field_name } } }) else - return "self.__class." .. self:name(node[2]) + return "self.__class." .. tostring(field_name) end end, self_colon = function(self, node) - return "self:" .. self:name(node[2]) + return "self:" .. tostring(self:name(node[2])) end, self_class_colon = function(self, node) - return "self.__class:" .. self:name(node[2]) + return "self.__class:" .. tostring(self:name(node[2])) end, ref = function(self, value) do diff --git a/moonscript/compile/value.moon b/moonscript/compile/value.moon index 677c08cc..1bfa754b 100644 --- a/moonscript/compile/value.moon +++ b/moonscript/compile/value.moon @@ -183,26 +183,29 @@ string_chars = { @line "not ", @value node[2] self: (node) => - if data.lua_keywords[node[2]] + field_name = @name node[2] + if data.lua_keywords[field_name] @value {"chain", "self", {"index", { - "string", '"', @name node[2] + "string", '"', field_name }}} else - "self."..@name node[2] + "self.#{field_name}" self_class: (node) => - if data.lua_keywords[node[2]] + field_name = @name node[2] + + if data.lua_keywords[field_name] @value {"chain", "self", {"dot", "__class"}, {"index", { - "string", '"', @name node[2] + "string", '"', field_name }}} else - "self.__class."..@name node[2] + "self.__class.#{field_name}" self_colon: (node) => - "self:"..@name node[2] + "self:#{@name node[2]}" self_class_colon: (node) => - "self.__class:"..@name node[2] + "self.__class:#{@name node[2]}" -- a variable reference ref: (value) => From a0108328373d5f3f1aefb98341aa895dd75a1b2a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 4 Nov 2022 13:38:05 -0700 Subject: [PATCH 305/344] mention TIME mode in language spec --- spec/README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/spec/README.md b/spec/README.md index b5ba9594..3ca43503 100644 --- a/spec/README.md +++ b/spec/README.md @@ -75,5 +75,17 @@ There is a make task to conveniently do this: make build_test_outputs ``` +## Performance timing + +The syntax specs have performance timing collection built in. To get these +times run the test suite with the `TIME` environment variable set. + +``` +TIME=1 busted spec/lang_spec.moon +``` + +Any changes to the compiler should not introduce any substantial performance +decreases. + From 0259c835fe4a09a127d694b22d30d2357d5feb26 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 22 Mar 2023 15:40:31 -0700 Subject: [PATCH 306/344] add util script for generating header for binaries --- bin/util/file_to_header.lua | 42 +++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 bin/util/file_to_header.lua diff --git a/bin/util/file_to_header.lua b/bin/util/file_to_header.lua new file mode 100644 index 00000000..71378367 --- /dev/null +++ b/bin/util/file_to_header.lua @@ -0,0 +1,42 @@ +-- this script is used to convert a source input file into a C header to embed +-- that file as a string. Works the same as xxd -i + +local input = ... + +local function read_file(file_path) + local file = assert(io.open(file_path, "rb")) + local content = file:read("*a") + file:close() + return content +end + +local function generate_c_header(input_file) + local function byte_to_hex(byte) + return string.format("0x%02x", byte:byte()) + end + + local function sanitize_name(name) + return (name:gsub("[^%w_]", "_")) + end + + local data = read_file(input_file) + local name = sanitize_name(input_file) + local header = {} + + table.insert(header, string.format("unsigned char %s[] = {", name)) + for i = 1, #data do + if i % 16 == 1 then + table.insert(header, "\n ") + end + table.insert(header, byte_to_hex(data:sub(i, i))) + if i ~= #data then + table.insert(header, ", ") + end + end + table.insert(header, "\n};\n") + table.insert(header, string.format("unsigned int %s_len = %d;\n", name, #data)) + + return table.concat(header) +end + +print(generate_c_header(input)) From fd9d0eee6d0dc9f6dbcea579e92ced6e69072e32 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 22 Mar 2023 15:43:33 -0700 Subject: [PATCH 307/344] rewrite binaries script to to build for both windows & linux --- .../{windows_exe.yml => binaries.yml} | 32 +++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) rename .github/workflows/{windows_exe.yml => binaries.yml} (55%) diff --git a/.github/workflows/windows_exe.yml b/.github/workflows/binaries.yml similarity index 55% rename from .github/workflows/windows_exe.yml rename to .github/workflows/binaries.yml index fd23805d..bbb07a0a 100644 --- a/.github/workflows/windows_exe.yml +++ b/.github/workflows/binaries.yml @@ -3,12 +3,40 @@ name: "windows_exe" on: [push] jobs: - build: - runs-on: windows-latest + linux: + runs-on: ubuntu-latest steps: - uses: actions/checkout@master + - name: Show GCC + run: gcc -v + + - name: Setup Lua + run: | + curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz + tar -xZf lua-5.1.5.tar.gz + cd lua-5.1.5; make PLAT=mingw + + - name: Get LPeg + run: | + curl -o lpeg.tar.gz http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz + tar -xZf lpeg.tar.gz + + - name: Get Luafilesystem + run: | + curl -L -o luafilesystem.tar.gz https://github.com/keplerproject/luafilesystem/archive/v1_8_0.tar.gz + tar -xZf luafilesystem.tar.gz + + - name: Build + run: gcc -o moon '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + + + windows: + runs-on: windows-latest + + steps: + - uses: actions/checkout@master - uses: msys2/setup-msys2@v2 with: From 1affa0abf6f2764862ed4825c6c5253ad3793590 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 22 Mar 2023 15:44:15 -0700 Subject: [PATCH 308/344] rename action --- .github/workflows/binaries.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index bbb07a0a..01b7f707 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -1,4 +1,4 @@ -name: "windows_exe" +name: "binaries" on: [push] From fa104985a6edb0890495e93515bca017031bef87 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 22 Mar 2023 15:48:51 -0700 Subject: [PATCH 309/344] fix invalid platform type on linux build --- .github/workflows/binaries.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 01b7f707..c4a70626 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -16,7 +16,7 @@ jobs: run: | curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xZf lua-5.1.5.tar.gz - cd lua-5.1.5; make PLAT=mingw + cd lua-5.1.5; make - name: Get LPeg run: | From 00397fd64b14ce85a4b5d0198bd702d6fb69dcb7 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 22 Jun 2023 15:33:50 -0700 Subject: [PATCH 310/344] add moon-tags script for generating tag file for moonscript classes --- bin/moon-tags | 123 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100755 bin/moon-tags diff --git a/bin/moon-tags b/bin/moon-tags new file mode 100755 index 00000000..88c7e854 --- /dev/null +++ b/bin/moon-tags @@ -0,0 +1,123 @@ +#!/usr/bin/env moon + +HEADER = [[ +!_TAG_FILE_FORMAT 2 /extended format/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_PROGRAM_AUTHOR leaf corcoran /leafot@gmail.com/ +!_TAG_PROGRAM_NAME MoonTags // +!_TAG_PROGRAM_URL https://github.com/leafo/moonscript /GitHub repository/ +!_TAG_PROGRAM_VERSION 0.0.1 // +]] + +-- see `ctags --list-kinds` for examples of kinds +-- see `ctags --list-fields` + +argparse = require "argparse" + +parser = argparse "moon-tags", "Generate ctags style tags file for MoonScript files" +parser\argument("files", "MoonScript files to generate tags for")\args "+" +parser\flag "--include-line", "Include line number field for each tag" + +args = parser\parse [v for _, v in ipairs _G.arg] + +TAGS = {} -- the final output of tags + +literals = require "moonscript.parse.literals" +import Indent from require "moonscript.parse.util" + +import P, S, C, Cc, Cg, Cb, Ct, Cs, V from require "lpeg" + +-- consome the rest of the file +until_end = (1 - literals.Stop)^0 +whitespace = S"\t " -- not including newline +ignore_line = Ct until_end -- tag it for empty line + +-- we have to do this double Ct to capture both the full line and the grouped captures +Line = (p) -> Ct C Ct Cg(Indent, "indent") * p +Type = (name) -> Cg Cc(name), "type" + +class_line = Line P"class" * whitespace^1 * Cg(literals.Name, "tag") * until_end * Type "class" +-- TODO: support lapis style routes +-- class_property = Line P("@")^-1 * Cg(literals.Name, "tag") * P":" * until_end * Type "property" + +method = P { P"=>" + P(1 - literals.Stop) * V(1) } +class_method = Line P("@")^-1 * Cg(literals.Name, "tag") * P":" * method * until_end * Type "method" + +parse_lines = Ct P { + (class_line + class_method + ignore_line) * (P(-1) + literals.Break * V(1)) +} + +escape_tagaddress = (line_text) -> + replacements = P([[\]]) / [[\\]] + P([[/]]) / [[\/]] + P("\t") / [[\t]] + P("\r") / [[\r]] + P("\n") / [[\n]] + Cs((replacements + 1)^0)\match line_text + +for fname in *args.files + file = assert io.open fname + contents = assert file\read "*a" + lines = assert parse_lines\match contents + + class_stack = {} + + push_class = (cls) -> + assert cls.type == "class", "not a class match" + -- remove classes that are longer in scope due to indentation + for i=#class_stack,1,-1 + top = class_stack[i] + + if cls.indent <= top.indent + table.remove class_stack, i + else + break + + table.insert class_stack, cls + + -- find the class this property is associated with based on change in indent + -- the expeted indent is written to `step` on the first proprety + find_class = (property) -> + for i=#class_stack,1,-1 + top = class_stack[i] + step = property.indent - top.indent + + if step > 0 + if top.step == nil + top.step = step + + if step == top.step + return top + + for line_no, line in ipairs lines + continue unless next line + + {line_text, properties} = line + + fields = {"language:moon"} + if args.include_line + table.insert fields, 1, "line:#{line_no}" + + switch properties.type + when "method" + if cls = find_class properties + table.insert fields, "class:#{cls.tag}" + + table.insert TAGS, { + properties.tag + fname + "/^#{escape_tagaddress line_text}$/;\"" + "f" + table.concat fields, " " + } + when "class" + push_class properties + + table.insert TAGS, { + properties.tag + fname + "/^#{escape_tagaddress line_text}$/;\"" + "c" + table.concat fields, " " + } + +print HEADER +tag_lines = [table.concat(t, "\t") for t in *TAGS] +table.sort tag_lines +print table.concat tag_lines, "\n" From 02222d3410b05476c1f376e0fdb1dde28e10d05e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 22 Jun 2023 16:28:33 -0700 Subject: [PATCH 311/344] extract export list and top level function definitions --- bin/moon-tags | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/bin/moon-tags b/bin/moon-tags index 88c7e854..9d365da8 100755 --- a/bin/moon-tags +++ b/bin/moon-tags @@ -33,18 +33,30 @@ whitespace = S"\t " -- not including newline ignore_line = Ct until_end -- tag it for empty line -- we have to do this double Ct to capture both the full line and the grouped captures -Line = (p) -> Ct C Ct Cg(Indent, "indent") * p Type = (name) -> Cg Cc(name), "type" +Line = (type_name, p) -> Ct C Ct Cg(Indent, "indent") * p * Type type_name + +class_line = Line "class", P"class" * whitespace^1 * Cg(literals.Name, "tag") * until_end -class_line = Line P"class" * whitespace^1 * Cg(literals.Name, "tag") * until_end * Type "class" -- TODO: support lapis style routes -- class_property = Line P("@")^-1 * Cg(literals.Name, "tag") * P":" * until_end * Type "property" method = P { P"=>" + P(1 - literals.Stop) * V(1) } -class_method = Line P("@")^-1 * Cg(literals.Name, "tag") * P":" * method * until_end * Type "method" +func = P { P"->" + P"=>" + P(1 - literals.Stop) * V(1) } + +-- this matches end-of-file return table convention for module files to figure +-- out what names are exported +export_list = Ct P"{" * P { + P"}" + ((P":" * literals.Name) + (P(1) - P"}")) * V(1) +} + +eof_exports = P { export_list * S(" \t\r\n")^0 * P(-1) + P(1) * V(1) } + +class_method = Line("method", P("@")^-1 * Cg(literals.Name, "tag") * P":" * method) * until_end +function_def = Line("function", Cg(literals.Name, "tag") * whitespace^0 * P"=" * func) * until_end parse_lines = Ct P { - (class_line + class_method + ignore_line) * (P(-1) + literals.Break * V(1)) + (class_line + class_method + function_def + ignore_line) * (P(-1) + literals.Break * V(1)) } escape_tagaddress = (line_text) -> @@ -54,6 +66,8 @@ escape_tagaddress = (line_text) -> for fname in *args.files file = assert io.open fname contents = assert file\read "*a" + exports = {e, true for e in *eof_exports\match(contents) or {}} + lines = assert parse_lines\match contents class_stack = {} @@ -95,6 +109,17 @@ for fname in *args.files table.insert fields, 1, "line:#{line_no}" switch properties.type + when "function" + if exports[properties.tag] and properties.indent == 0 + table.insert TAGS, { + properties.tag + fname + -- note we don't use $ here + "/^#{escape_tagaddress line_text}$/;\"" + "f" + table.concat fields, " " + } + when "method" if cls = find_class properties table.insert fields, "class:#{cls.tag}" @@ -102,6 +127,7 @@ for fname in *args.files table.insert TAGS, { properties.tag fname + -- note we don't use $ here "/^#{escape_tagaddress line_text}$/;\"" "f" table.concat fields, " " From 524d64ffd386f619a32850552dd760df6c29acc5 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 22 Jun 2023 19:45:34 -0700 Subject: [PATCH 312/344] use prefix mach for tag address for fucntions, since they are truncated --- bin/moon-tags | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bin/moon-tags b/bin/moon-tags index 9d365da8..2452a01e 100755 --- a/bin/moon-tags +++ b/bin/moon-tags @@ -60,7 +60,7 @@ parse_lines = Ct P { } escape_tagaddress = (line_text) -> - replacements = P([[\]]) / [[\\]] + P([[/]]) / [[\/]] + P("\t") / [[\t]] + P("\r") / [[\r]] + P("\n") / [[\n]] + replacements = S([[\/.$^]]) / [[\%0]]+ P("\t") / [[\t]] + P("\r") / [[\r]] + P("\n") / [[\n]] Cs((replacements + 1)^0)\match line_text for fname in *args.files @@ -115,7 +115,7 @@ for fname in *args.files properties.tag fname -- note we don't use $ here - "/^#{escape_tagaddress line_text}$/;\"" + "/^#{escape_tagaddress line_text}/;\"" "f" table.concat fields, " " } @@ -128,7 +128,7 @@ for fname in *args.files properties.tag fname -- note we don't use $ here - "/^#{escape_tagaddress line_text}$/;\"" + "/^#{escape_tagaddress line_text}/;\"" "f" table.concat fields, " " } From fbd8ad48737651114a3d3a672b9f8f8b3a7022b7 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 23 Jun 2023 09:33:37 -0700 Subject: [PATCH 313/344] add support for detecting lapis routes --- bin/moon-tags | 61 +++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/bin/moon-tags b/bin/moon-tags index 2452a01e..0008a727 100755 --- a/bin/moon-tags +++ b/bin/moon-tags @@ -17,13 +17,14 @@ argparse = require "argparse" parser = argparse "moon-tags", "Generate ctags style tags file for MoonScript files" parser\argument("files", "MoonScript files to generate tags for")\args "+" parser\flag "--include-line", "Include line number field for each tag" +parser\flag "--lapis", "Support extracting lapis routes" args = parser\parse [v for _, v in ipairs _G.arg] TAGS = {} -- the final output of tags literals = require "moonscript.parse.literals" -import Indent from require "moonscript.parse.util" +import Indent, simple_string from require "moonscript.parse.util" import P, S, C, Cc, Cg, Cb, Ct, Cs, V from require "lpeg" @@ -32,18 +33,20 @@ until_end = (1 - literals.Stop)^0 whitespace = S"\t " -- not including newline ignore_line = Ct until_end -- tag it for empty line +-- NOTE: we disable interpolation parsing since we don't have full grammar +SingleString = simple_string "'", false +DoubleString = simple_string '"', false +String = SingleString + DoubleString + -- we have to do this double Ct to capture both the full line and the grouped captures Type = (name) -> Cg Cc(name), "type" Line = (type_name, p) -> Ct C Ct Cg(Indent, "indent") * p * Type type_name -class_line = Line "class", P"class" * whitespace^1 * Cg(literals.Name, "tag") * until_end - --- TODO: support lapis style routes --- class_property = Line P("@")^-1 * Cg(literals.Name, "tag") * P":" * until_end * Type "property" - method = P { P"=>" + P(1 - literals.Stop) * V(1) } func = P { P"->" + P"=>" + P(1 - literals.Stop) * V(1) } +self_prefix = Cg(P("@") * Cc(true), "self") + -- this matches end-of-file return table convention for module files to figure -- out what names are exported export_list = Ct P"{" * P { @@ -52,17 +55,37 @@ export_list = Ct P"{" * P { eof_exports = P { export_list * S(" \t\r\n")^0 * P(-1) + P(1) * V(1) } +class_line = Line "class", P"class" * whitespace^1 * Cg(literals.Name, "tag") * until_end +class_property = Line "property", self_prefix^-1 * Cg(literals.Name, "tag") * P":" * whitespace^0 * Cg(String, "value")^0 * until_end class_method = Line("method", P("@")^-1 * Cg(literals.Name, "tag") * P":" * method) * until_end function_def = Line("function", Cg(literals.Name, "tag") * whitespace^0 * P"=" * func) * until_end +lapis_route = Line "lapis-route", P"[" * Cg(literals.Name, "tag") * P":" * whitespace^0 * Cg(String, "route") * whitespace^0 * P("]:") * until_end + +line_types = class_line + class_method + class_property + function_def + +if args.lapis + line_types += lapis_route parse_lines = Ct P { - (class_line + class_method + function_def + ignore_line) * (P(-1) + literals.Break * V(1)) + (line_types + ignore_line) * (P(-1) + literals.Break * V(1)) } escape_tagaddress = (line_text) -> replacements = S([[\/.$^]]) / [[\%0]]+ P("\t") / [[\t]] + P("\r") / [[\r]] + P("\n") / [[\n]] Cs((replacements + 1)^0)\match line_text +import types from require "tableshape" + +class_field = types.partial { + "self": true + tag: types.string\tag "name" + value: types.partial { + "string" + types.string + types.string\tag "value" -- TODO: will need to un-escape this + } +} + for fname in *args.files file = assert io.open fname contents = assert file\read "*a" @@ -109,6 +132,30 @@ for fname in *args.files table.insert fields, 1, "line:#{line_no}" switch properties.type + when "lapis-route" + if cls = find_class properties + prefix = if cls.fields + cls.fields.name + + table.insert TAGS, { + "#{prefix or ""}#{properties.tag}" + fname + "/^#{escape_tagaddress line_text}/;\"" + "f" + table.concat fields, " " + } + + when "property" + -- this is necessary to register the correct indent level for the class + cls = find_class properties + + -- record the fields into the class object so they can be referenced by + -- other tags. Note this is code-order dependent + if cls and args.lapis + if field = class_field properties + cls.fields or= {} + cls.fields[field.name] = field.value + when "function" if exports[properties.tag] and properties.indent == 0 table.insert TAGS, { From 65c7b152f05efd79c8d65fd6a3be2fb28b50ce52 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 18 Oct 2023 15:27:23 -0700 Subject: [PATCH 314/344] add --no-header option for moon-tags --- bin/moon-tags | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/moon-tags b/bin/moon-tags index 0008a727..51b74fa4 100755 --- a/bin/moon-tags +++ b/bin/moon-tags @@ -18,6 +18,7 @@ parser = argparse "moon-tags", "Generate ctags style tags file for MoonScript fi parser\argument("files", "MoonScript files to generate tags for")\args "+" parser\flag "--include-line", "Include line number field for each tag" parser\flag "--lapis", "Support extracting lapis routes" +parser\flag "--no-header", "Don't print the header" args = parser\parse [v for _, v in ipairs _G.arg] @@ -190,7 +191,9 @@ for fname in *args.files table.concat fields, " " } -print HEADER +unless args.no_header + print HEADER + tag_lines = [table.concat(t, "\t") for t in *TAGS] table.sort tag_lines print table.concat tag_lines, "\n" From d5341c9093c49d3724072b209cde28b5cb0f47c9 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 6 Nov 2023 12:54:51 -0800 Subject: [PATCH 315/344] support "string" route names for lapis route detection in moon-tags --- bin/moon-tags | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/bin/moon-tags b/bin/moon-tags index 51b74fa4..5f657d55 100755 --- a/bin/moon-tags +++ b/bin/moon-tags @@ -56,11 +56,14 @@ export_list = Ct P"{" * P { eof_exports = P { export_list * S(" \t\r\n")^0 * P(-1) + P(1) * V(1) } +-- convert a parsed string to the value the string represents +StringVal = C(String) / (str) -> loadstring("return " .. str)() + class_line = Line "class", P"class" * whitespace^1 * Cg(literals.Name, "tag") * until_end class_property = Line "property", self_prefix^-1 * Cg(literals.Name, "tag") * P":" * whitespace^0 * Cg(String, "value")^0 * until_end class_method = Line("method", P("@")^-1 * Cg(literals.Name, "tag") * P":" * method) * until_end function_def = Line("function", Cg(literals.Name, "tag") * whitespace^0 * P"=" * func) * until_end -lapis_route = Line "lapis-route", P"[" * Cg(literals.Name, "tag") * P":" * whitespace^0 * Cg(String, "route") * whitespace^0 * P("]:") * until_end +lapis_route = Line "lapis-route", P"[" * Cg(literals.Name + StringVal, "tag") * P":" * whitespace^0 * Cg(String, "route") * whitespace^0 * P("]:") * until_end line_types = class_line + class_method + class_property + function_def From 60094ca0be870462678925413b6528ae5bf4690a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sat, 23 Nov 2024 12:13:21 -0800 Subject: [PATCH 316/344] remove alt_getopt from splat --- bin/splat.moon | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) diff --git a/bin/splat.moon b/bin/splat.moon index df523c14..15c1a5a8 100755 --- a/bin/splat.moon +++ b/bin/splat.moon @@ -1,26 +1,18 @@ #!/usr/bin/env moon +argparse = require "argparse" --- concatenate a collection of lua modules into one +parser = argparse "splat.moon", "Concatenate a collection of Lua modules into a single file" +parser\option("--load -l", "Module names that will be load on require")\count "*" -lfs = require "lfs" -alt_getopt = require "alt_getopt" - -import insert, concat from table -import dump, split from require "moonscript.util" - -opts, ind = alt_getopt.get_opts arg, "l:", { - load: "l" -} +parser\argument("directories", "Directories to scan for Lua modules")\args "+" -if not arg[ind] - print "usage: splat [-l module_names] directory [directories...]" - os.exit! - -dirs = [a for a in *arg[ind,]] +args = parser\parse [v for _, v in ipairs _G.arg] +dirs = args.directories normalize = (path) -> path\match("(.-)/*$").."/" +lfs = require "lfs" scan_directory = (root, patt, collected={}) -> root = normalize root for fname in lfs.dir root @@ -31,7 +23,7 @@ scan_directory = (root, patt, collected={}) -> scan_directory full_path, patt, collected else if full_path\match patt - insert collected, full_path + table.insert collected, full_path collected @@ -39,15 +31,14 @@ path_to_module_name = (path) -> (path\match("(.-)%.lua")\gsub("/", ".")) each_line = (text) -> - import yield from coroutine coroutine.wrap -> start = 1 while true pos, after = text\find "\n", start, true break if not pos - yield text\sub start, pos - 1 + coroutine.yield text\sub start, pos - 1 start = after + 1 - yield text\sub start, #text + coroutine.yield text\sub start, #text nil write_module = (name, text) -> @@ -73,8 +64,7 @@ for dir in *dirs name = base write_module name, content -if opts.l - for module_name in *split opts.l, "," - if modules[module_name] - print ([[package.preload["%s"]()]])\format module_name +for module_name in *args.load + if modules[module_name] + print ([[package.preload["%s"]()]])\format module_name From 3db3ec8234a1d77ad6f1fd773057efddfdc79a77 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Sun, 1 Dec 2024 09:27:02 -0800 Subject: [PATCH 317/344] don't fail if object doesn't match class types strictly --- moonscript/util.lua | 2 +- moonscript/util.moon | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/moonscript/util.lua b/moonscript/util.lua index 714767ed..83d7e37f 100644 --- a/moonscript/util.lua +++ b/moonscript/util.lua @@ -103,7 +103,7 @@ dump = function(what) end seen[what] = false local class_name - if what.__class then + if type(what.__class) == "table" and type(what.__class.__name) == "string" then class_name = "<" .. tostring(what.__class.__name) .. ">" end return tostring(class_name or "") .. "{\n" .. concat(lines) .. (" "):rep((depth - 1) * 4) .. "}\n" diff --git a/moonscript/util.moon b/moonscript/util.moon index 9516c8bc..9969ac4a 100644 --- a/moonscript/util.moon +++ b/moonscript/util.moon @@ -70,7 +70,7 @@ dump = (what) -> seen[what] = false - class_name = if what.__class + class_name = if type(what.__class) == "table" and type(what.__class.__name) == "string" "<#{what.__class.__name}>" "#{class_name or ""}{\n" .. concat(lines) .. (" ")\rep((depth - 1)*4) .. "}\n" From 98448bdcb219d6df081e7986200784b83ccd10f3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 14 Jan 2025 11:56:36 -0800 Subject: [PATCH 318/344] make moon.p print all arguments after dumping them --- moon/init.lua | 7 +++++-- moon/init.moon | 6 ++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/moon/init.lua b/moon/init.lua index d4c24903..52ead55d 100644 --- a/moon/init.lua +++ b/moon/init.lua @@ -8,8 +8,11 @@ do getfenv, setfenv, dump = _obj_0.getfenv, _obj_0.setfenv, _obj_0.dump end local p, is_object, type, debug, run_with_scope, bind_methods, defaultbl, extend, copy, mixin, mixin_object, mixin_table, fold -p = function(...) - return print(dump(...)) +p = function(o, ...) + print(dump(o)) + if select("#", ...) > 0 then + return p(...) + end end is_object = function(value) return lua.type(value) == "table" and value.__class diff --git a/moon/init.moon b/moon/init.moon index 658d8867..57325eea 100644 --- a/moon/init.moon +++ b/moon/init.moon @@ -4,8 +4,10 @@ import getfenv, setfenv, dump from require "moonscript.util" local * -p = (...) -> - print dump ... +p = (o, ...) -> + print dump o + if select("#", ...) > 0 + p ... is_object = (value) -> -- is a moonscript object lua.type(value) == "table" and value.__class From 3b134e01ebc5961ca132bff5ba2871c88d65347e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 27 Feb 2025 11:30:46 -0800 Subject: [PATCH 319/344] misc readme update --- README.md | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 967b84f6..b5bb5f92 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,22 @@ Online demo/compiler at . ## Join Our Community -We just created a Discord for those interested in MoonScript. You can join us here: +We have a Discord for those interested in MoonScript and related projects. You can join us here: + +## Contributing + +MoonScript is a self-hosted compiler, meaning it's written in MoonScript itself. When contributing, please follow the following guidelines: + +1. Edit `.moon` files, never modify the alongside `.lua` files directly +2. After making changes to `.moon` files, run the compiler to regenerate the corresponding `.lua` files +3. Both `.moon` and `.lua` files are included in the repository to ensure that: + - Users can install and use MoonScript without having to compile it themselves + - The compiler bootstrapping process works consistently + +It's helpful to have a separate installation of MoonScript should you break +something and you need to re-build the MoonScript with a working version of +MoonScript. You can check out the repo in another directory, or install it +using LuaRocks to have a separate working version. ## Running Tests @@ -34,7 +49,6 @@ busted Writing specs is a bit more complicated. Check out [the spec writing guide](spec/README.md). - ## Binaries Precompiled versions of MoonScript are provided for Windows. You can find them @@ -53,7 +67,7 @@ branch](https://github.com/leafo/moonscript/tree/binaries) ## License (MIT) -Copyright (C) 2020 by Leaf Corcoran +Copyright (C) 2025 by Leaf Corcoran Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal From b9768c893bd25c514c6404767fadec29dfd01060 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Wed, 17 Dec 2025 12:24:34 -0800 Subject: [PATCH 320/344] add support for `moon -e` to run code from command line --- bin/moon | 69 +++++++++++++++++++++++++++++++++++---------------- bin/moon.moon | 65 +++++++++++++++++++++++++++++++++--------------- 2 files changed, 93 insertions(+), 41 deletions(-) diff --git a/bin/moon b/bin/moon index 75f2156d..759cae8a 100755 --- a/bin/moon +++ b/bin/moon @@ -7,11 +7,12 @@ local unpack = util.unpack local argparser = argparse()({ name = "moon" }) -argparser:argument("script") +argparser:argument("script"):args("?") argparser:argument("args"):args("*") -argparser:option("-c --coverage", "Collect and print code coverage") -argparser:option("-d", "Disable stack trace rewriting") -argparser:option("-v --version", "Print version information") +argparser:flag("--coverage -c", "Collect and print code coverage") +argparser:flag("-d", "Disable stack trace rewriting") +argparser:option("--execute -e", "Execute MoonScript code string") +argparser:flag("--version -v", "Print version information") local base = 0 local _list_0 = arg for _index_0 = 1, #_list_0 do @@ -48,29 +49,55 @@ run = function() require("moonscript.version").print_version() os.exit() end - local script_fname = opts.script args = { unpack(arg, base + 1) } args[-1] = arg[0] - args[0] = opts.script local moonscript_chunk, lua_parse_error - local passed, err = pcall(function() - moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { - implicitly_return_root = false - }) - end) - if not (passed) then - print_err(err) - os.exit(1) - end - if not (moonscript_chunk) then - if lua_parse_error then - print_err(lua_parse_error) - else - print_err("Can't file file: " .. tostring(script_fname)) + if opts.execute then + args[0] = "-e" + local passed, err = pcall(function() + moonscript_chunk, lua_parse_error = moonscript.loadstring(opts.execute, "=(command line)", { + implicitly_return_root = false + }) + end) + if not (passed) then + print_err(err) + os.exit(1) + end + if not (moonscript_chunk) then + if lua_parse_error then + print_err(lua_parse_error) + else + print_err("Failed to compile: " .. tostring(opts.execute)) + end + os.exit(1) + end + else + local script_fname = opts.script + if not (script_fname) then + print_err("Usage: moon [options] script [args]") + print_err("Use 'moon --help' for more information.") + os.exit(1) + end + args[0] = script_fname + local passed, err = pcall(function() + moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { + implicitly_return_root = false + }) + end) + if not (passed) then + print_err(err) + os.exit(1) + end + if not (moonscript_chunk) then + if lua_parse_error then + print_err(lua_parse_error) + else + print_err("Can't file file: " .. tostring(script_fname)) + end + os.exit(1) end - os.exit(1) end util.getfenv(moonscript_chunk).arg = args local run_chunk diff --git a/bin/moon.moon b/bin/moon.moon index 94601d57..fcb8de89 100644 --- a/bin/moon.moon +++ b/bin/moon.moon @@ -8,11 +8,12 @@ unpack = util.unpack argparser = argparse! name: "moon" -argparser\argument "script" +argparser\argument("script")\args "?" argparser\argument("args")\args "*" -argparser\option "-c --coverage", "Collect and print code coverage" -argparser\option "-d", "Disable stack trace rewriting" -argparser\option "-v --version", "Print version information" +argparser\flag "--coverage -c", "Collect and print code coverage" +argparser\flag "-d", "Disable stack trace rewriting" +argparser\option "--execute -e", "Execute MoonScript code string" +argparser\flag "--version -v", "Print version information" base = 0 for flag in *arg @@ -31,30 +32,54 @@ run = -> require("moonscript.version").print_version! os.exit! - script_fname = opts.script - args = {unpack arg, base + 1} args[-1] = arg[0] - args[0] = opts.script local moonscript_chunk, lua_parse_error - passed, err = pcall -> - moonscript_chunk, lua_parse_error = moonscript.loadfile script_fname, { - implicitly_return_root: false - } + if opts.execute + args[0] = "-e" - unless passed - print_err err - os.exit 1 + passed, err = pcall -> + moonscript_chunk, lua_parse_error = moonscript.loadstring opts.execute, "=(command line)", { + implicitly_return_root: false + } - unless moonscript_chunk - if lua_parse_error - print_err lua_parse_error - else - print_err "Can't file file: #{script_fname}" + unless passed + print_err err + os.exit 1 - os.exit 1 + unless moonscript_chunk + if lua_parse_error + print_err lua_parse_error + else + print_err "Failed to compile: #{opts.execute}" + os.exit 1 + else + script_fname = opts.script + + unless script_fname + print_err "Usage: moon [options] script [args]" + print_err "Use 'moon --help' for more information." + os.exit 1 + + args[0] = script_fname + + passed, err = pcall -> + moonscript_chunk, lua_parse_error = moonscript.loadfile script_fname, { + implicitly_return_root: false + } + + unless passed + print_err err + os.exit 1 + + unless moonscript_chunk + if lua_parse_error + print_err lua_parse_error + else + print_err "Can't file file: #{script_fname}" + os.exit 1 util.getfenv(moonscript_chunk).arg = args From 6146357792ad823db40a84cc954423bcceef4554 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 12:47:34 -0800 Subject: [PATCH 321/344] does this fix anything --- .github/workflows/binaries.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index c4a70626..3fc235e0 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -15,18 +15,18 @@ jobs: - name: Setup Lua run: | curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz - tar -xZf lua-5.1.5.tar.gz + tar -xzf lua-5.1.5.tar.gz cd lua-5.1.5; make - name: Get LPeg run: | curl -o lpeg.tar.gz http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz - tar -xZf lpeg.tar.gz + tar -xzf lpeg.tar.gz - name: Get Luafilesystem run: | curl -L -o luafilesystem.tar.gz https://github.com/keplerproject/luafilesystem/archive/v1_8_0.tar.gz - tar -xZf luafilesystem.tar.gz + tar -xzf luafilesystem.tar.gz - name: Build run: gcc -o moon '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c @@ -48,18 +48,18 @@ jobs: - name: Setup Lua run: | curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz - tar -xZf lua-5.1.5.tar.gz + tar -xzf lua-5.1.5.tar.gz cd lua-5.1.5; make PLAT=mingw - name: Get LPeg run: | curl -o lpeg.tar.gz http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz - tar -xZf lpeg.tar.gz + tar -xzf lpeg.tar.gz - name: Get Luafilesystem run: | curl -L -o luafilesystem.tar.gz https://github.com/keplerproject/luafilesystem/archive/v1_8_0.tar.gz - tar -xZf luafilesystem.tar.gz + tar -xzf luafilesystem.tar.gz - name: List Files run: Get-ChildItem -Recurse From 2e2e478345b2d9c87f0afbe3367ff970a3de63c1 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 12:50:47 -0800 Subject: [PATCH 322/344] fix the curl commands --- .github/workflows/binaries.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 3fc235e0..d916fe34 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -14,13 +14,13 @@ jobs: - name: Setup Lua run: | - curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz + curl -L -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xzf lua-5.1.5.tar.gz cd lua-5.1.5; make - name: Get LPeg run: | - curl -o lpeg.tar.gz http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz + curl -L -o lpeg.tar.gz https://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz tar -xzf lpeg.tar.gz - name: Get Luafilesystem @@ -47,13 +47,13 @@ jobs: - name: Setup Lua run: | - curl -O https://www.lua.org/ftp/lua-5.1.5.tar.gz + curl -L -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xzf lua-5.1.5.tar.gz cd lua-5.1.5; make PLAT=mingw - name: Get LPeg run: | - curl -o lpeg.tar.gz http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz + curl -L -o lpeg.tar.gz https://www.inf.puc-rio.br/~roberto/lpeg/lpeg-1.0.2.tar.gz tar -xzf lpeg.tar.gz - name: Get Luafilesystem From 8b7b91ff984d49d9601f01201051b3960a9698d8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 12:53:06 -0800 Subject: [PATCH 323/344] more binary fixes --- .github/workflows/binaries.yml | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index d916fe34..1f2acb1e 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -29,7 +29,9 @@ jobs: tar -xzf luafilesystem.tar.gz - name: Build - run: gcc -o moon '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + run: | + mkdir -p dist + gcc -o dist/moon '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c windows: @@ -68,7 +70,9 @@ jobs: run: lua-5.1.5/src/lua.exe -v - name: Build - run: gcc -o moon.exe '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + run: | + mkdir -p dist + gcc -o dist/moon.exe '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c From 25555f86a53c2981d2603679efd888130f16a5ac Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 12:55:14 -0800 Subject: [PATCH 324/344] keep it coming --- .github/workflows/binaries.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 1f2acb1e..51bdfc74 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -31,7 +31,7 @@ jobs: - name: Build run: | mkdir -p dist - gcc -o dist/moon '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + gcc -o dist/moon -Ilua-5.1.5/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-5.1.5/src/liblua.a -lm -ldl windows: @@ -72,7 +72,7 @@ jobs: - name: Build run: | mkdir -p dist - gcc -o dist/moon.exe '-Ilua-5.1.5/src/' bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c + gcc -o dist/moon.exe -Ilua-5.1.5/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-5.1.5/src/liblua.a -lm From ad5b2eec9d21148754b1160d4948111211367c34 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 12:58:08 -0800 Subject: [PATCH 325/344] blip --- .github/workflows/binaries.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 51bdfc74..6a9d6c30 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -16,7 +16,7 @@ jobs: run: | curl -L -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xzf lua-5.1.5.tar.gz - cd lua-5.1.5; make + cd lua-5.1.5 && make linux - name: Get LPeg run: | @@ -51,7 +51,7 @@ jobs: run: | curl -L -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xzf lua-5.1.5.tar.gz - cd lua-5.1.5; make PLAT=mingw + cd lua-5.1.5 && make generic - name: Get LPeg run: | From 755bff1bd1ed088f2f310123304f1244d0e6cf99 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 12:59:46 -0800 Subject: [PATCH 326/344] what about this --- .github/workflows/binaries.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 6a9d6c30..a6ac69dc 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -16,7 +16,7 @@ jobs: run: | curl -L -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xzf lua-5.1.5.tar.gz - cd lua-5.1.5 && make linux + cd lua-5.1.5/src && make liblua.a MYCFLAGS=-DLUA_USE_POSIX - name: Get LPeg run: | @@ -51,7 +51,7 @@ jobs: run: | curl -L -O https://www.lua.org/ftp/lua-5.1.5.tar.gz tar -xzf lua-5.1.5.tar.gz - cd lua-5.1.5 && make generic + cd lua-5.1.5/src && make liblua.a - name: Get LPeg run: | From 2de9084a451e02c4f5a0d607ce40e1d6f4b83298 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 13:03:11 -0800 Subject: [PATCH 327/344] test run, and upload artifacts --- .github/workflows/binaries.yml | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index a6ac69dc..e473b449 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -31,8 +31,16 @@ jobs: - name: Build run: | mkdir -p dist - gcc -o dist/moon -Ilua-5.1.5/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-5.1.5/src/liblua.a -lm -ldl + gcc -static -o dist/moon -Ilua-5.1.5/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-5.1.5/src/liblua.a -lm -ldl + - name: Test run + run: dist/moon -e 'print "hello world"' + + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: moon-linux + path: dist/ windows: runs-on: windows-latest @@ -63,17 +71,16 @@ jobs: curl -L -o luafilesystem.tar.gz https://github.com/keplerproject/luafilesystem/archive/v1_8_0.tar.gz tar -xzf luafilesystem.tar.gz - - name: List Files - run: Get-ChildItem -Recurse - - - name: Run Lua - run: lua-5.1.5/src/lua.exe -v - - name: Build run: | mkdir -p dist - gcc -o dist/moon.exe -Ilua-5.1.5/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-5.1.5/src/liblua.a -lm - - + gcc -static -o dist/moon.exe -Ilua-5.1.5/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-5.1.5/src/liblua.a -lm + - name: Test run + run: dist/moon.exe -e 'print "hello world"' + - name: Upload artifact + uses: actions/upload-artifact@v4 + with: + name: moon-windows + path: dist/ From fb690d6d342ecb81926950e9acd86de60e5d03d0 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 13:04:54 -0800 Subject: [PATCH 328/344] make lua version part of matrix --- .github/workflows/binaries.yml | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index e473b449..68a45006 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -3,9 +3,13 @@ name: "binaries" on: [push] jobs: - linux: + linux: runs-on: ubuntu-latest + strategy: + matrix: + lua_version: ["5.1.5"] + steps: - uses: actions/checkout@master @@ -14,9 +18,9 @@ jobs: - name: Setup Lua run: | - curl -L -O https://www.lua.org/ftp/lua-5.1.5.tar.gz - tar -xzf lua-5.1.5.tar.gz - cd lua-5.1.5/src && make liblua.a MYCFLAGS=-DLUA_USE_POSIX + curl -L -O https://www.lua.org/ftp/lua-${{ matrix.lua_version }}.tar.gz + tar -xzf lua-${{ matrix.lua_version }}.tar.gz + cd lua-${{ matrix.lua_version }}/src && make liblua.a MYCFLAGS=-DLUA_USE_POSIX - name: Get LPeg run: | @@ -31,20 +35,24 @@ jobs: - name: Build run: | mkdir -p dist - gcc -static -o dist/moon -Ilua-5.1.5/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-5.1.5/src/liblua.a -lm -ldl + gcc -static -o dist/moon -Ilua-${{ matrix.lua_version }}/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-${{ matrix.lua_version }}/src/liblua.a -lm -ldl - name: Test run run: dist/moon -e 'print "hello world"' - name: Upload artifact - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v6 with: - name: moon-linux + name: moon-linux-lua${{ matrix.lua_version }} path: dist/ windows: runs-on: windows-latest + strategy: + matrix: + lua_version: ["5.1.5"] + steps: - uses: actions/checkout@master @@ -57,9 +65,9 @@ jobs: - name: Setup Lua run: | - curl -L -O https://www.lua.org/ftp/lua-5.1.5.tar.gz - tar -xzf lua-5.1.5.tar.gz - cd lua-5.1.5/src && make liblua.a + curl -L -O https://www.lua.org/ftp/lua-${{ matrix.lua_version }}.tar.gz + tar -xzf lua-${{ matrix.lua_version }}.tar.gz + cd lua-${{ matrix.lua_version }}/src && make liblua.a - name: Get LPeg run: | @@ -74,13 +82,13 @@ jobs: - name: Build run: | mkdir -p dist - gcc -static -o dist/moon.exe -Ilua-5.1.5/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-5.1.5/src/liblua.a -lm + gcc -static -o dist/moon.exe -Ilua-${{ matrix.lua_version }}/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-${{ matrix.lua_version }}/src/liblua.a -lm - name: Test run run: dist/moon.exe -e 'print "hello world"' - name: Upload artifact - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v6 with: - name: moon-windows + name: moon-windows-lua${{ matrix.lua_version }} path: dist/ From c8c945fa2cf821ff9c230c57304983f8dee6c3d3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 13:14:47 -0800 Subject: [PATCH 329/344] add makefile equivalent for making dist/moon --- .github/workflows/binaries.yml | 8 ++++++-- Makefile | 23 ++++++++++++++++++++++- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 68a45006..b04cc059 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -38,7 +38,9 @@ jobs: gcc -static -o dist/moon -Ilua-${{ matrix.lua_version }}/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-${{ matrix.lua_version }}/src/liblua.a -lm -ldl - name: Test run - run: dist/moon -e 'print "hello world"' + run: | + dist/moon -h + dist/moon -e 'print "hello world"' - name: Upload artifact uses: actions/upload-artifact@v6 @@ -85,7 +87,9 @@ jobs: gcc -static -o dist/moon.exe -Ilua-${{ matrix.lua_version }}/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-${{ matrix.lua_version }}/src/liblua.a -lm - name: Test run - run: dist/moon.exe -e 'print "hello world"' + run: | + dist/moon.exe -h + dist/moon.exe -e 'print "hello world"' - name: Upload artifact uses: actions/upload-artifact@v6 diff --git a/Makefile b/Makefile index ed924438..8c76c216 100644 --- a/Makefile +++ b/Makefile @@ -4,7 +4,10 @@ LUAROCKS = luarocks --lua-version=$(LUA_VERSION) LUA_PATH_MAKE = $(shell $(LUAROCKS) path --lr-path);./?.lua;./?/init.lua LUA_CPATH_MAKE = $(shell $(LUAROCKS) path --lr-cpath);./?.so -.PHONY: test local build watch lint count show +LUA_SRC_VERSION ?= 5.1.5 +LPEG_VERSION ?= 1.0.2 + +.PHONY: test local build watch lint count show test_binary build: LUA_PATH='$(LUA_PATH_MAKE)' LUA_CPATH='$(LUA_CPATH_MAKE)' $(LUA) bin/moonc moon/ moonscript/ @@ -44,3 +47,21 @@ lint: count: wc -l $$(git ls-files | grep 'moon$$') | sort -n | tail + +# Binary build targets for local verification (Linux only) +lua-$(LUA_SRC_VERSION)/src/liblua.a: + curl -L -O https://www.lua.org/ftp/lua-$(LUA_SRC_VERSION).tar.gz + tar -xzf lua-$(LUA_SRC_VERSION).tar.gz + cd lua-$(LUA_SRC_VERSION)/src && make liblua.a MYCFLAGS=-DLUA_USE_POSIX + +lpeg-$(LPEG_VERSION)/lptree.c: + curl -L -o lpeg.tar.gz https://www.inf.puc-rio.br/~roberto/lpeg/lpeg-$(LPEG_VERSION).tar.gz + tar -xzf lpeg.tar.gz + +dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c + mkdir -p dist + gcc -static -o dist/moon -Ilua-$(LUA_SRC_VERSION)/src/ bin/binaries/moon.c lpeg-$(LPEG_VERSION)/lpvm.c lpeg-$(LPEG_VERSION)/lpcap.c lpeg-$(LPEG_VERSION)/lptree.c lpeg-$(LPEG_VERSION)/lpcode.c lpeg-$(LPEG_VERSION)/lpprint.c lua-$(LUA_SRC_VERSION)/src/liblua.a -lm -ldl + +test_binary: dist/moon + dist/moon -h + dist/moon -e 'print "hello world"' From 905a24a44734718309aba323eb85f35778810c9e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 13:16:16 -0800 Subject: [PATCH 330/344] try to fix windows build --- .github/workflows/binaries.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index b04cc059..88ad11ba 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -55,6 +55,10 @@ jobs: matrix: lua_version: ["5.1.5"] + defaults: + run: + shell: msys2 {0} + steps: - uses: actions/checkout@master From 6284a0cf798a9b8a10a2e8de80c130289294147d Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 13:33:01 -0800 Subject: [PATCH 331/344] working through binaries some more --- Makefile | 24 ++++++++++++++++++++---- bin/binaries/moon.c | 29 ++++++++++++++++++++++++++++- bin/binaries/moonscript.c | 35 +++++++++++++++++++++++++++++++++++ moon.lua | 1 - moonscript.lua | 1 - 5 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 bin/binaries/moonscript.c delete mode 100644 moon.lua delete mode 100644 moonscript.lua diff --git a/Makefile b/Makefile index 8c76c216..66e2a752 100644 --- a/Makefile +++ b/Makefile @@ -58,10 +58,26 @@ lpeg-$(LPEG_VERSION)/lptree.c: curl -L -o lpeg.tar.gz https://www.inf.puc-rio.br/~roberto/lpeg/lpeg-$(LPEG_VERSION).tar.gz tar -xzf lpeg.tar.gz -dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c +bin/binaries/moonscript.h: moonscript/*.lua moon/*.lua + bin/splat.moon -l moonscript moonscript moon > moonscript.lua + xxd -i moonscript.lua > $@ + rm moonscript.lua + +dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h mkdir -p dist - gcc -static -o dist/moon -Ilua-$(LUA_SRC_VERSION)/src/ bin/binaries/moon.c lpeg-$(LPEG_VERSION)/lpvm.c lpeg-$(LPEG_VERSION)/lpcap.c lpeg-$(LPEG_VERSION)/lptree.c lpeg-$(LPEG_VERSION)/lpcode.c lpeg-$(LPEG_VERSION)/lpprint.c lua-$(LUA_SRC_VERSION)/src/liblua.a -lm -ldl + gcc -static -o dist/moon \ + -Ilua-$(LUA_SRC_VERSION)/src/ \ + -Ilpeg-$(LPEG_VERSION)/ \ + -Ibin/binaries/ \ + bin/binaries/moon.c \ + bin/binaries/moonscript.c \ + lpeg-$(LPEG_VERSION)/lpvm.c \ + lpeg-$(LPEG_VERSION)/lpcap.c \ + lpeg-$(LPEG_VERSION)/lptree.c \ + lpeg-$(LPEG_VERSION)/lpcode.c \ + lpeg-$(LPEG_VERSION)/lpprint.c \ + lua-$(LUA_SRC_VERSION)/src/liblua.a \ + -lm -ldl test_binary: dist/moon - dist/moon -h - dist/moon -e 'print "hello world"' + dist/moon diff --git a/bin/binaries/moon.c b/bin/binaries/moon.c index ea668ed9..344e3656 100644 --- a/bin/binaries/moon.c +++ b/bin/binaries/moon.c @@ -1,4 +1,31 @@ +#include +#include +#include +#include + +// from moonscript.c +extern int luaopen_moonscript(lua_State *l); -// this does nothing until we get a build working for all deps int main(int argc, char **argv) { + lua_State *l = luaL_newstate(); + luaL_openlibs(l); + + // Load moonscript (this also loads lpeg) + luaopen_moonscript(l); + lua_pop(l, 1); // pop the return value + + // Simple test: require moonscript and compile something + const char *test_code = + "local moonscript = require('moonscript')\n" + "local code = moonscript.to_lua('print \"hello from moonscript\"')\n" + "print(code)\n" + "loadstring(code)()\n"; + + if (luaL_dostring(l, test_code) != 0) { + fprintf(stderr, "Test failed: %s\n", lua_tostring(l, -1)); + return 1; + } + + lua_close(l); + return 0; } diff --git a/bin/binaries/moonscript.c b/bin/binaries/moonscript.c new file mode 100644 index 00000000..deed4f91 --- /dev/null +++ b/bin/binaries/moonscript.c @@ -0,0 +1,35 @@ +#include +#include +#include +#include + +#include "moonscript.h" + +// put whatever is on top of stack into package.loaded under name if something +// is not already there +void setloaded(lua_State* l, const char* name) { + int top = lua_gettop(l); + lua_getglobal(l, "package"); + lua_getfield(l, -1, "loaded"); + lua_getfield(l, -1, name); + if (lua_isnil(l, -1)) { + lua_pop(l, 1); + lua_pushvalue(l, top); + lua_setfield(l, -2, name); + } + + lua_settop(l, top); +} + +extern int luaopen_lpeg(lua_State *l); + +LUALIB_API int luaopen_moonscript(lua_State *l) { + luaopen_lpeg(l); + setloaded(l, "lpeg"); + + if (luaL_loadbuffer(l, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") == 0) { + lua_call(l, 0, 1); + return 1; + } + return 0; +} diff --git a/moon.lua b/moon.lua deleted file mode 100644 index 6b1536fd..00000000 --- a/moon.lua +++ /dev/null @@ -1 +0,0 @@ -return require "moon.init" diff --git a/moonscript.lua b/moonscript.lua deleted file mode 100644 index dd876be9..00000000 --- a/moonscript.lua +++ /dev/null @@ -1 +0,0 @@ -return require "moonscript.init" From 8fe8c2a41f7c515e478a72ae85c3c054957fe661 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 13:40:20 -0800 Subject: [PATCH 332/344] convert moon.moon -> moon.h, and run it through the moon.c entry point --- Makefile | 7 ++++++- bin/binaries/moon.c | 28 +++++++++++++++++++--------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index 66e2a752..d68b6812 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,12 @@ bin/binaries/moonscript.h: moonscript/*.lua moon/*.lua xxd -i moonscript.lua > $@ rm moonscript.lua -dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h +bin/binaries/moon.h: bin/moon + awk 'FNR>1' bin/moon > moon.lua + xxd -i moon.lua > $@ + rm moon.lua + +dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h mkdir -p dist gcc -static -o dist/moon \ -Ilua-$(LUA_SRC_VERSION)/src/ \ diff --git a/bin/binaries/moon.c b/bin/binaries/moon.c index 344e3656..f668d436 100644 --- a/bin/binaries/moon.c +++ b/bin/binaries/moon.c @@ -3,6 +3,8 @@ #include #include +#include "moon.h" // the CLI script + // from moonscript.c extern int luaopen_moonscript(lua_State *l); @@ -12,17 +14,25 @@ int main(int argc, char **argv) { // Load moonscript (this also loads lpeg) luaopen_moonscript(l); - lua_pop(l, 1); // pop the return value + lua_pop(l, 1); - // Simple test: require moonscript and compile something - const char *test_code = - "local moonscript = require('moonscript')\n" - "local code = moonscript.to_lua('print \"hello from moonscript\"')\n" - "print(code)\n" - "loadstring(code)()\n"; + // Set up arg table + lua_newtable(l); + lua_pushstring(l, "moon"); + lua_rawseti(l, -2, -1); + for (int i = 0; i < argc; i++) { + lua_pushstring(l, argv[i]); + lua_rawseti(l, -2, i); + } + lua_setglobal(l, "arg"); - if (luaL_dostring(l, test_code) != 0) { - fprintf(stderr, "Test failed: %s\n", lua_tostring(l, -1)); + // Load and execute the moon CLI script + if (luaL_loadbuffer(l, (const char *)moon_lua, moon_lua_len, "moon") != 0) { + fprintf(stderr, "Failed to load moon: %s\n", lua_tostring(l, -1)); + return 1; + } + if (lua_pcall(l, 0, 0, 0) != 0) { + fprintf(stderr, "Error: %s\n", lua_tostring(l, -1)); return 1; } From ae5d0c7af0efd1c20026c53eec255b804610f760 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 13:47:46 -0800 Subject: [PATCH 333/344] try a different stage to download headers --- .github/workflows/binaries.yml | 74 +++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 2 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 88ad11ba..8537acaf 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -3,8 +3,41 @@ name: "binaries" on: [push] jobs: + generate-headers: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@master + + - uses: leafo/gh-actions-lua@master + with: + luaVersion: "5.1" + + - uses: leafo/gh-actions-luarocks@master + + - name: Install dependencies + run: | + luarocks install argparse + luarocks make + + - name: Generate moonscript.h + run: | + bin/splat.moon -l moonscript moonscript moon > moonscript.lua + xxd -i moonscript.lua > bin/binaries/moonscript.h + + - name: Generate moon.h + run: | + awk 'FNR>1' bin/moon > moon.lua + xxd -i moon.lua > bin/binaries/moon.h + + - name: Upload headers + uses: actions/upload-artifact@v6 + with: + name: generated-headers + path: bin/binaries/*.h + linux: runs-on: ubuntu-latest + needs: generate-headers strategy: matrix: @@ -13,6 +46,12 @@ jobs: steps: - uses: actions/checkout@master + - name: Download headers + uses: actions/download-artifact@v4 + with: + name: generated-headers + path: bin/binaries/ + - name: Show GCC run: gcc -v @@ -35,7 +74,19 @@ jobs: - name: Build run: | mkdir -p dist - gcc -static -o dist/moon -Ilua-${{ matrix.lua_version }}/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-${{ matrix.lua_version }}/src/liblua.a -lm -ldl + gcc -static -o dist/moon \ + -Ilua-${{ matrix.lua_version }}/src/ \ + -Ilpeg-1.0.2/ \ + -Ibin/binaries/ \ + bin/binaries/moon.c \ + bin/binaries/moonscript.c \ + lpeg-1.0.2/lpvm.c \ + lpeg-1.0.2/lpcap.c \ + lpeg-1.0.2/lptree.c \ + lpeg-1.0.2/lpcode.c \ + lpeg-1.0.2/lpprint.c \ + lua-${{ matrix.lua_version }}/src/liblua.a \ + -lm -ldl - name: Test run run: | @@ -50,6 +101,7 @@ jobs: windows: runs-on: windows-latest + needs: generate-headers strategy: matrix: @@ -62,6 +114,12 @@ jobs: steps: - uses: actions/checkout@master + - name: Download headers + uses: actions/download-artifact@v4 + with: + name: generated-headers + path: bin/binaries/ + - uses: msys2/setup-msys2@v2 with: install: gcc make curl @@ -88,7 +146,19 @@ jobs: - name: Build run: | mkdir -p dist - gcc -static -o dist/moon.exe -Ilua-${{ matrix.lua_version }}/src/ bin/binaries/moon.c lpeg-1.0.2/lpvm.c lpeg-1.0.2/lpcap.c lpeg-1.0.2/lptree.c lpeg-1.0.2/lpcode.c lpeg-1.0.2/lpprint.c lua-${{ matrix.lua_version }}/src/liblua.a -lm + gcc -static -o dist/moon.exe \ + -Ilua-${{ matrix.lua_version }}/src/ \ + -Ilpeg-1.0.2/ \ + -Ibin/binaries/ \ + bin/binaries/moon.c \ + bin/binaries/moonscript.c \ + lpeg-1.0.2/lpvm.c \ + lpeg-1.0.2/lpcap.c \ + lpeg-1.0.2/lptree.c \ + lpeg-1.0.2/lpcode.c \ + lpeg-1.0.2/lpprint.c \ + lua-${{ matrix.lua_version }}/src/liblua.a \ + -lm - name: Test run run: | From 5c920e515115ed06409fd6691bda24026ea1c786 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 13:58:42 -0800 Subject: [PATCH 334/344] make specs look for the init files (shouldn't loadkit do this??) --- spec/helpers.moon | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/helpers.moon b/spec/helpers.moon index 45545b91..ddfcddb1 100644 --- a/spec/helpers.moon +++ b/spec/helpers.moon @@ -24,6 +24,12 @@ with_dev = (fn) -> setup -> _G.require = (mod) -> + switch mod + when "moonscript" + "moonscript.init" + when "moon" + "moon.init" + return dev_cache[mod] if dev_cache[mod] testable = mod\match("moonscript%.") or mod == "moonscript" or From ef9f892841bc83ce196a8998d2ce68a32582a74e Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 14:06:37 -0800 Subject: [PATCH 335/344] add support for embedding argparse --- Makefile | 10 +++++++++- bin/binaries/moonscript.c | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d68b6812..3c1aaab2 100644 --- a/Makefile +++ b/Makefile @@ -49,6 +49,9 @@ count: wc -l $$(git ls-files | grep 'moon$$') | sort -n | tail # Binary build targets for local verification (Linux only) +lua_modules: + luarocks install argparse --tree=lua_modules + lua-$(LUA_SRC_VERSION)/src/liblua.a: curl -L -O https://www.lua.org/ftp/lua-$(LUA_SRC_VERSION).tar.gz tar -xzf lua-$(LUA_SRC_VERSION).tar.gz @@ -68,7 +71,12 @@ bin/binaries/moon.h: bin/moon xxd -i moon.lua > $@ rm moon.lua -dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h +bin/binaries/argparse.h: lua_modules + bin/splat.moon -l argparse $$(find lua_modules/share/lua -name "*.lua" -exec dirname {} \; | head -1) > argparse.lua + xxd -i argparse.lua > $@ + rm argparse.lua + +dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h bin/binaries/argparse.h mkdir -p dist gcc -static -o dist/moon \ -Ilua-$(LUA_SRC_VERSION)/src/ \ diff --git a/bin/binaries/moonscript.c b/bin/binaries/moonscript.c index deed4f91..606242dd 100644 --- a/bin/binaries/moonscript.c +++ b/bin/binaries/moonscript.c @@ -4,6 +4,7 @@ #include #include "moonscript.h" +#include "argparse.h" // put whatever is on top of stack into package.loaded under name if something // is not already there @@ -27,6 +28,11 @@ LUALIB_API int luaopen_moonscript(lua_State *l) { luaopen_lpeg(l); setloaded(l, "lpeg"); + // Load argparse (splat output sets up package.preload) + if (luaL_loadbuffer(l, (const char *)argparse_lua, argparse_lua_len, "argparse.lua") == 0) { + lua_call(l, 0, 0); + } + if (luaL_loadbuffer(l, (const char *)moonscript_lua, moonscript_lua_len, "moonscript.lua") == 0) { lua_call(l, 0, 1); return 1; From 5bf04b131a35ce5a512baf5c0ea5294c44e400f1 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 14:10:40 -0800 Subject: [PATCH 336/344] generate argparse.h in the workflow --- .github/workflows/binaries.yml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 8537acaf..39c90209 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -29,6 +29,12 @@ jobs: awk 'FNR>1' bin/moon > moon.lua xxd -i moon.lua > bin/binaries/moon.h + - name: Generate argparse.h + run: | + luarocks install argparse --tree=lua_modules + bin/splat.moon -l argparse $(find lua_modules/share/lua -name "*.lua" -exec dirname {} \; | head -1) > argparse.lua + xxd -i argparse.lua > bin/binaries/argparse.h + - name: Upload headers uses: actions/upload-artifact@v6 with: From 158f7fccfe5c5ac1ba966bc5b01346fbd3f29ee7 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 14:11:58 -0800 Subject: [PATCH 337/344] woops, fix the module name rewrite --- spec/helpers.moon | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/helpers.moon b/spec/helpers.moon index ddfcddb1..caf885c2 100644 --- a/spec/helpers.moon +++ b/spec/helpers.moon @@ -24,7 +24,7 @@ with_dev = (fn) -> setup -> _G.require = (mod) -> - switch mod + mod = switch mod when "moonscript" "moonscript.init" when "moon" From bf965ef40397c883336ed0b888a0710f73df8f65 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Thu, 8 Jan 2026 14:24:55 -0800 Subject: [PATCH 338/344] fix splat for argparse dependency --- .github/workflows/binaries.yml | 4 ++-- Makefile | 5 ++--- bin/splat.moon | 15 ++++++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 39c90209..b8743605 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -32,8 +32,8 @@ jobs: - name: Generate argparse.h run: | luarocks install argparse --tree=lua_modules - bin/splat.moon -l argparse $(find lua_modules/share/lua -name "*.lua" -exec dirname {} \; | head -1) > argparse.lua - xxd -i argparse.lua > bin/binaries/argparse.h + bin/splat.moon --strip-prefix -l argparse $(find lua_modules/share/lua -name "argparse.lua" -exec dirname {} \; | head -1) > bin/binaries/argparse.lua + xxd -i -n argparse_lua bin/binaries/argparse.lua > bin/binaries/argparse.h - name: Upload headers uses: actions/upload-artifact@v6 diff --git a/Makefile b/Makefile index 3c1aaab2..84ebaf4c 100644 --- a/Makefile +++ b/Makefile @@ -72,9 +72,8 @@ bin/binaries/moon.h: bin/moon rm moon.lua bin/binaries/argparse.h: lua_modules - bin/splat.moon -l argparse $$(find lua_modules/share/lua -name "*.lua" -exec dirname {} \; | head -1) > argparse.lua - xxd -i argparse.lua > $@ - rm argparse.lua + bin/splat.moon --strip-prefix -l argparse $$(find lua_modules/share/lua -name "argparse.lua" -exec dirname {} \; | head -1) > bin/binaries/argparse.lua + xxd -i -n argparse_lua bin/binaries/argparse.lua > $@ dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h bin/binaries/argparse.h mkdir -p dist diff --git a/bin/splat.moon b/bin/splat.moon index 15c1a5a8..a7009948 100755 --- a/bin/splat.moon +++ b/bin/splat.moon @@ -1,16 +1,18 @@ #!/usr/bin/env moon argparse = require "argparse" +normalize = (path) -> + path\match("(.-)/*$").."/" + parser = argparse "splat.moon", "Concatenate a collection of Lua modules into a single file" parser\option("--load -l", "Module names that will be load on require")\count "*" +parser\flag("--strip-prefix -s", "Strip directory prefix from module names") parser\argument("directories", "Directories to scan for Lua modules")\args "+" args = parser\parse [v for _, v in ipairs _G.arg] dirs = args.directories - -normalize = (path) -> - path\match("(.-)/*$").."/" +strip_prefix = args.strip_prefix lfs = require "lfs" scan_directory = (root, patt, collected={}) -> @@ -27,7 +29,9 @@ scan_directory = (root, patt, collected={}) -> collected -path_to_module_name = (path) -> +path_to_module_name = (path, prefix) -> + if prefix and path\sub(1, #prefix) == prefix + path = path\sub(#prefix + 1) (path\match("(.-)%.lua")\gsub("/", ".")) each_line = (text) -> @@ -50,8 +54,9 @@ write_module = (name, text) -> modules = {} for dir in *dirs files = scan_directory dir, "%.lua$" + prefix = strip_prefix and normalize(dir) or nil chunks = for path in *files - module_name = path_to_module_name path + module_name = path_to_module_name path, prefix content = io.open(path)\read"*a" modules[module_name] = true {module_name, content} From 57009b142a11b5a63e2914a44660e6c3758ab6b3 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 9 Jan 2026 15:37:16 -0800 Subject: [PATCH 339/344] get moonc buildin --- Makefile | 29 ++++++++++++++++++++++++++++- bin/splat.moon | 3 +++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 84ebaf4c..c3c0868b 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,7 @@ LUA_CPATH_MAKE = $(shell $(LUAROCKS) path --lr-cpath);./?.so LUA_SRC_VERSION ?= 5.1.5 LPEG_VERSION ?= 1.0.2 +LFS_VERSION ?= 1_8_0 .PHONY: test local build watch lint count show test_binary @@ -61,6 +62,10 @@ lpeg-$(LPEG_VERSION)/lptree.c: curl -L -o lpeg.tar.gz https://www.inf.puc-rio.br/~roberto/lpeg/lpeg-$(LPEG_VERSION).tar.gz tar -xzf lpeg.tar.gz +luafilesystem-$(LFS_VERSION)/src/lfs.c: + curl -L -o luafilesystem.tar.gz https://github.com/keplerproject/luafilesystem/archive/v$(LFS_VERSION).tar.gz + tar -xzf luafilesystem.tar.gz + bin/binaries/moonscript.h: moonscript/*.lua moon/*.lua bin/splat.moon -l moonscript moonscript moon > moonscript.lua xxd -i moonscript.lua > $@ @@ -75,7 +80,12 @@ bin/binaries/argparse.h: lua_modules bin/splat.moon --strip-prefix -l argparse $$(find lua_modules/share/lua -name "argparse.lua" -exec dirname {} \; | head -1) > bin/binaries/argparse.lua xxd -i -n argparse_lua bin/binaries/argparse.lua > $@ -dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h bin/binaries/argparse.h +bin/binaries/moonc.h: bin/moonc + awk 'FNR>1' bin/moonc > moonc.lua + xxd -i moonc.lua > $@ + rm moonc.lua + +dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin/binaries/moonscript.h bin/binaries/moon.h bin/binaries/argparse.h bin/binaries/moon.c bin/binaries/moonscript.c mkdir -p dist gcc -static -o dist/moon \ -Ilua-$(LUA_SRC_VERSION)/src/ \ @@ -91,5 +101,22 @@ dist/moon: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c bin lua-$(LUA_SRC_VERSION)/src/liblua.a \ -lm -ldl +dist/moonc: lua-$(LUA_SRC_VERSION)/src/liblua.a lpeg-$(LPEG_VERSION)/lptree.c luafilesystem-$(LFS_VERSION)/src/lfs.c bin/binaries/moonscript.h bin/binaries/moonc.h bin/binaries/argparse.h bin/binaries/moonc.c bin/binaries/moonscript.c + mkdir -p dist + gcc -static -o dist/moonc \ + -Ilua-$(LUA_SRC_VERSION)/src/ \ + -Ilpeg-$(LPEG_VERSION)/ \ + -Ibin/binaries/ \ + bin/binaries/moonc.c \ + bin/binaries/moonscript.c \ + lpeg-$(LPEG_VERSION)/lpvm.c \ + lpeg-$(LPEG_VERSION)/lpcap.c \ + lpeg-$(LPEG_VERSION)/lptree.c \ + lpeg-$(LPEG_VERSION)/lpcode.c \ + lpeg-$(LPEG_VERSION)/lpprint.c \ + luafilesystem-$(LFS_VERSION)/src/lfs.c \ + lua-$(LUA_SRC_VERSION)/src/liblua.a \ + -lm -ldl + test_binary: dist/moon dist/moon diff --git a/bin/splat.moon b/bin/splat.moon index a7009948..d6127042 100755 --- a/bin/splat.moon +++ b/bin/splat.moon @@ -1,6 +1,9 @@ #!/usr/bin/env moon argparse = require "argparse" +-- TODO: it would be cool if you could just point this at a luarocks tree, pass a list of top level module names, and it figures it out for you. +-- Perhaps even merge the header generation into here as well to avoid using xxd + normalize = (path) -> path\match("(.-)/*$").."/" From dc1edf1d5f82d4d8b76be7a728a319b03f70c64a Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 9 Jan 2026 15:55:53 -0800 Subject: [PATCH 340/344] add moonc compile to binaries workflow --- .github/workflows/binaries.yml | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index b8743605..0fd0053c 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -35,6 +35,11 @@ jobs: bin/splat.moon --strip-prefix -l argparse $(find lua_modules/share/lua -name "argparse.lua" -exec dirname {} \; | head -1) > bin/binaries/argparse.lua xxd -i -n argparse_lua bin/binaries/argparse.lua > bin/binaries/argparse.h + - name: Generate moonc.h + run: | + awk 'FNR>1' bin/moonc > moonc.lua + xxd -i moonc.lua > bin/binaries/moonc.h + - name: Upload headers uses: actions/upload-artifact@v6 with: @@ -93,11 +98,26 @@ jobs: lpeg-1.0.2/lpprint.c \ lua-${{ matrix.lua_version }}/src/liblua.a \ -lm -ldl + gcc -static -o dist/moonc \ + -Ilua-${{ matrix.lua_version }}/src/ \ + -Ilpeg-1.0.2/ \ + -Ibin/binaries/ \ + bin/binaries/moonc.c \ + bin/binaries/moonscript.c \ + lpeg-1.0.2/lpvm.c \ + lpeg-1.0.2/lpcap.c \ + lpeg-1.0.2/lptree.c \ + lpeg-1.0.2/lpcode.c \ + lpeg-1.0.2/lpprint.c \ + luafilesystem-1_8_0/src/lfs.c \ + lua-${{ matrix.lua_version }}/src/liblua.a \ + -lm -ldl - name: Test run run: | dist/moon -h dist/moon -e 'print "hello world"' + dist/moonc -h - name: Upload artifact uses: actions/upload-artifact@v6 @@ -165,11 +185,26 @@ jobs: lpeg-1.0.2/lpprint.c \ lua-${{ matrix.lua_version }}/src/liblua.a \ -lm + gcc -static -o dist/moonc.exe \ + -Ilua-${{ matrix.lua_version }}/src/ \ + -Ilpeg-1.0.2/ \ + -Ibin/binaries/ \ + bin/binaries/moonc.c \ + bin/binaries/moonscript.c \ + lpeg-1.0.2/lpvm.c \ + lpeg-1.0.2/lpcap.c \ + lpeg-1.0.2/lptree.c \ + lpeg-1.0.2/lpcode.c \ + lpeg-1.0.2/lpprint.c \ + luafilesystem-1_8_0/src/lfs.c \ + lua-${{ matrix.lua_version }}/src/liblua.a \ + -lm - name: Test run run: | dist/moon.exe -h dist/moon.exe -e 'print "hello world"' + dist/moonc.exe -h - name: Upload artifact uses: actions/upload-artifact@v6 From bbd101517bd9d5b541901a4ce7c32070f2371fba Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 9 Jan 2026 16:05:05 -0800 Subject: [PATCH 341/344] forgot a file --- bin/binaries/moonc.c | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 bin/binaries/moonc.c diff --git a/bin/binaries/moonc.c b/bin/binaries/moonc.c new file mode 100644 index 00000000..9cd9a67b --- /dev/null +++ b/bin/binaries/moonc.c @@ -0,0 +1,55 @@ +#include +#include +#include +#include + +#include "moonc.h" // the CLI script + +// from moonscript.c +extern int luaopen_moonscript(lua_State *l); + +// from lfs.c +extern int luaopen_lfs(lua_State *l); + +int main(int argc, char **argv) { + lua_State *l = luaL_newstate(); + luaL_openlibs(l); + + // Load moonscript (this also loads lpeg and argparse) + luaopen_moonscript(l); + lua_pop(l, 1); + + // Load luafilesystem and register it in package.loaded + int nresults = luaopen_lfs(l); + if (nresults > 0) { + lua_getglobal(l, "package"); + lua_getfield(l, -1, "loaded"); + lua_pushvalue(l, -3); // push lfs table + lua_setfield(l, -2, "lfs"); + lua_pop(l, 2); // pop loaded, package + } + lua_pop(l, nresults); + + // Set up arg table + lua_newtable(l); + lua_pushstring(l, "moonc"); + lua_rawseti(l, -2, -1); + for (int i = 0; i < argc; i++) { + lua_pushstring(l, argv[i]); + lua_rawseti(l, -2, i); + } + lua_setglobal(l, "arg"); + + // Load and execute the moonc CLI script + if (luaL_loadbuffer(l, (const char *)moonc_lua, moonc_lua_len, "moonc") != 0) { + fprintf(stderr, "Failed to load moonc: %s\n", lua_tostring(l, -1)); + return 1; + } + if (lua_pcall(l, 0, 0, 0) != 0) { + fprintf(stderr, "Error: %s\n", lua_tostring(l, -1)); + return 1; + } + + lua_close(l); + return 0; +} From b00d4a6400803fe738ff5941aac820adf91029b1 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 9 Jan 2026 16:42:09 -0800 Subject: [PATCH 342/344] better artifact naming, auto publishing to release --- .github/workflows/binaries.yml | 46 +++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 0fd0053c..7f7ec1da 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -57,6 +57,14 @@ jobs: steps: - uses: actions/checkout@master + - name: Set version suffix + run: | + if [[ "${{ github.ref_type }}" == "tag" ]]; then + echo "VERSION_SUFFIX=${{ github.ref_name }}" >> $GITHUB_ENV + else + echo "VERSION_SUFFIX=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + fi + - name: Download headers uses: actions/download-artifact@v4 with: @@ -122,9 +130,21 @@ jobs: - name: Upload artifact uses: actions/upload-artifact@v6 with: - name: moon-linux-lua${{ matrix.lua_version }} + name: moonscript-${{ env.VERSION_SUFFIX }}-linux-lua${{ matrix.lua_version }} path: dist/ + - name: Package for release + if: github.ref_type == 'tag' + run: | + cd dist + tar -czvf ../moonscript-${{ env.VERSION_SUFFIX }}-linux-x86_64.tar.gz * + + - name: Upload to release + if: github.ref_type == 'tag' + uses: softprops/action-gh-release@v2 + with: + files: moonscript-${{ env.VERSION_SUFFIX }}-linux-x86_64.tar.gz + windows: runs-on: windows-latest needs: generate-headers @@ -140,6 +160,14 @@ jobs: steps: - uses: actions/checkout@master + - name: Set version suffix + run: | + if [[ "${{ github.ref_type }}" == "tag" ]]; then + echo "VERSION_SUFFIX=${{ github.ref_name }}" >> $GITHUB_ENV + else + echo "VERSION_SUFFIX=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + fi + - name: Download headers uses: actions/download-artifact@v4 with: @@ -148,7 +176,7 @@ jobs: - uses: msys2/setup-msys2@v2 with: - install: gcc make curl + install: gcc make curl zip - name: Show GCC run: gcc -v @@ -209,5 +237,17 @@ jobs: - name: Upload artifact uses: actions/upload-artifact@v6 with: - name: moon-windows-lua${{ matrix.lua_version }} + name: moonscript-${{ env.VERSION_SUFFIX }}-windows-lua${{ matrix.lua_version }} path: dist/ + + - name: Package for release + if: github.ref_type == 'tag' + run: | + cd dist + zip ../moonscript-${{ env.VERSION_SUFFIX }}-windows-x86_64.zip * + + - name: Upload to release + if: github.ref_type == 'tag' + uses: softprops/action-gh-release@v2 + with: + files: moonscript-${{ env.VERSION_SUFFIX }}-windows-x86_64.zip From 2429bb8097f8d2457458c1d21a961df19c52fb69 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 9 Jan 2026 16:49:37 -0800 Subject: [PATCH 343/344] fix order of operations --- .github/workflows/binaries.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index 7f7ec1da..e49e9f26 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -160,14 +160,6 @@ jobs: steps: - uses: actions/checkout@master - - name: Set version suffix - run: | - if [[ "${{ github.ref_type }}" == "tag" ]]; then - echo "VERSION_SUFFIX=${{ github.ref_name }}" >> $GITHUB_ENV - else - echo "VERSION_SUFFIX=$(git rev-parse --short HEAD)" >> $GITHUB_ENV - fi - - name: Download headers uses: actions/download-artifact@v4 with: @@ -178,6 +170,14 @@ jobs: with: install: gcc make curl zip + - name: Set version suffix + run: | + if [[ "${{ github.ref_type }}" == "tag" ]]; then + echo "VERSION_SUFFIX=${{ github.ref_name }}" >> $GITHUB_ENV + else + echo "VERSION_SUFFIX=$(git rev-parse --short HEAD)" >> $GITHUB_ENV + fi + - name: Show GCC run: gcc -v From 74e1e55cef028f7a1ef5dc80c116cea4cec45545 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Fri, 9 Jan 2026 17:13:26 -0800 Subject: [PATCH 344/344] see if this fixes windows archive name --- .github/workflows/binaries.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/binaries.yml b/.github/workflows/binaries.yml index e49e9f26..b0324230 100644 --- a/.github/workflows/binaries.yml +++ b/.github/workflows/binaries.yml @@ -171,6 +171,7 @@ jobs: install: gcc make curl zip - name: Set version suffix + shell: bash run: | if [[ "${{ github.ref_type }}" == "tag" ]]; then echo "VERSION_SUFFIX=${{ github.ref_name }}" >> $GITHUB_ENV