Skip to content

Commit 54dc8ef

Browse files
committed
local *
1 parent fa06e61 commit 54dc8ef

File tree

7 files changed

+72
-7
lines changed

7 files changed

+72
-7
lines changed

moonscript/compile.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,17 @@ tree = function(tree, options)
749749
return lua_code, posmap
750750
end
751751
end
752+
do
753+
local _with_0 = require("moonscript.data")
754+
local data = _with_0
755+
for name, cls in pairs({
756+
Line = Line,
757+
Lines = Lines,
758+
DelayedLine = DelayedLine
759+
}) do
760+
data[name] = cls
761+
end
762+
end
752763
return {
753764
tree = tree,
754765
value = value,

moonscript/compile.moon

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,4 +411,9 @@ tree = (tree, options={}) ->
411411
posmap = scope._lines\flatten_posmap!
412412
lua_code, posmap
413413

414+
-- mmmm
415+
with data = require "moonscript.data"
416+
for name, cls in pairs {:Line, :Lines, :DelayedLine}
417+
data[name] = cls
418+
414419
{ :tree, :value, :format_error, :Block, :RootBlock }

moonscript/compile/statement.lua

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
local reversed, unpack
2-
do
3-
local _table_0 = require("moonscript.util")
4-
reversed, unpack = _table_0.reversed, _table_0.unpack
5-
end
1+
local util = require("moonscript.util")
2+
local data = require("moonscript.data")
3+
local reversed, unpack = util.reversed, util.unpack
64
local ntype
75
do
86
local _table_0 = require("moonscript.types")
@@ -41,6 +39,23 @@ local statement_compilers = {
4139
end
4240
end
4341
end,
42+
declare_glob = function(self, node)
43+
local names = { }
44+
self:set("name_glob", function(name)
45+
insert(names, name)
46+
return true
47+
end)
48+
return data.DelayedLine(function(buff)
49+
insert(buff, "local ")
50+
local _list_0 = names
51+
for _index_0 = 1, #_list_0 do
52+
local name = _list_0[_index_0]
53+
insert(buff, self:name(name))
54+
insert(buff, ", ")
55+
end
56+
buff[#buff] = nil
57+
end)
58+
end,
4459
declare_with_shadows = function(self, node)
4560
local names = node[2]
4661
self:declare(names)

moonscript/compile/statement.moon

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11

2-
import reversed, unpack from require "moonscript.util"
2+
util = require "moonscript.util"
3+
data = require "moonscript.data"
4+
5+
import reversed, unpack from util
36
import ntype from require "moonscript.types"
47
import concat, insert from table
58

@@ -17,6 +20,19 @@ statement_compilers =
1720
with @line "local "
1821
\append_list [@name name for name in *undeclared], ", "
1922

23+
declare_glob: (node) =>
24+
names = {}
25+
@set "name_glob", (name) ->
26+
insert names, name
27+
true
28+
29+
data.DelayedLine (buff) ->
30+
insert buff, "local "
31+
for name in *names
32+
insert buff, @name name
33+
insert buff, ", "
34+
buff[#buff] = nil -- strips local if no names
35+
2036
-- this overrides the existing names with new locals, used for local keyword
2137
declare_with_shadows: (node) =>
2238
names = node[2]

moonscript/parse.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ local build_grammar = wrap_env(function()
404404
PopIndent = Cmt("", pop_indent),
405405
InBlock = Advance * Block * PopIndent,
406406

407-
Local = key"local" * Ct(NameList) / mark"declare_with_shadows",
407+
Local = key"local" * ((op"*" + op"^") / mark"declare_glob" + Ct(NameList) / mark"declare_with_shadows"),
408408

409409
Import = key"import" * Ct(ImportNameList) * key"from" * Exp / mark"import",
410410
ImportName = (sym"\\" * Ct(Cc"colon_stub" * Name) + Name),

moonscript/transform.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,18 @@ local Statement = Transformer({
329329
end,
330330
assign = function(self, node)
331331
local names, values = unpack(node, 2)
332+
do
333+
local globber = self:get("name_glob")
334+
if globber then
335+
local _list_0 = names
336+
for _index_0 = 1, #_list_0 do
337+
local name = _list_0[_index_0]
338+
if globber(name) then
339+
self:put_name(name)
340+
end
341+
end
342+
end
343+
end
332344
local transformed
333345
if #values == 1 then
334346
local value = values[1]

moonscript/transform.moon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,12 @@ Statement = Transformer {
158158

159159
assign: (node) =>
160160
names, values = unpack node, 2
161+
162+
if globber = @get "name_glob"
163+
for name in *names
164+
if globber name
165+
@put_name name
166+
161167
-- bubble cascading assigns
162168
transformed = if #values == 1
163169
value = values[1]

0 commit comments

Comments
 (0)