Skip to content

Commit e1f31d9

Browse files
committed
"declare" no longer shadows, introduce "declare_with_shadows" for local keyword
1 parent 99e1c9d commit e1f31d9

File tree

6 files changed

+45
-7
lines changed

6 files changed

+45
-7
lines changed

moonscript/compile/statement.lua

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ line_compile = {
1515
return self:add(text)
1616
end,
1717
declare = function(self, node)
18-
local _, names = unpack(node)
18+
local names = node[2]
1919
local undeclared = self:declare(names)
2020
if #undeclared > 0 then
2121
do
2222
local _with_0 = self:line("local ")
2323
_with_0:append_list((function()
2424
local _accum_0 = { }
2525
local _len_0 = 0
26-
local _list_0 = names
26+
local _list_0 = undeclared
2727
for _index_0 = 1, #_list_0 do
2828
local name = _list_0[_index_0]
2929
_len_0 = _len_0 + 1
@@ -35,6 +35,25 @@ line_compile = {
3535
end
3636
end
3737
end,
38+
declare_with_shadows = function(self, node)
39+
local names = node[2]
40+
self:declare(names)
41+
do
42+
local _with_0 = self:line("local ")
43+
_with_0:append_list((function()
44+
local _accum_0 = { }
45+
local _len_0 = 0
46+
local _list_0 = names
47+
for _index_0 = 1, #_list_0 do
48+
local name = _list_0[_index_0]
49+
_len_0 = _len_0 + 1
50+
_accum_0[_len_0] = self:name(name)
51+
end
52+
return _accum_0
53+
end)(), ", ")
54+
return _with_0
55+
end
56+
end,
3857
assign = function(self, node)
3958
local _, names, values = unpack(node)
4059
local undeclared = self:declare(names)

moonscript/compile/statement.moon

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,18 @@ line_compile =
1717
@add text
1818

1919
declare: (node) =>
20-
_, names = unpack node
20+
names = node[2]
2121
undeclared = @declare names
2222
if #undeclared > 0
2323
with @line "local "
24-
\append_list [@name name for name in *names], ", "
24+
\append_list [@name name for name in *undeclared], ", "
25+
26+
-- this overrides the existing names with new locals, used for local keyword
27+
declare_with_shadows: (node) =>
28+
names = node[2]
29+
@declare names
30+
with @line "local "
31+
\append_list [@name name for name in *names], ", "
2532

2633
assign: (node) =>
2734
_, names, values = unpack node

moonscript/parse.lua

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

363-
Local = key"local" * Ct(NameList) / mark"declare",
363+
Local = key"local" * Ct(NameList) / mark"declare_with_shadows",
364364

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

tests/inputs/local.moon

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ local a
33
local a,b,c
44

55

6+
x = 1212
7+
something = ->
8+
local x
9+
x = 1212
10+
11+

tests/outputs/import.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local hello = yeah.hello
2-
local hello, world
2+
local world
33
do
44
local _table_0 = table["cool"]
55
hello, world = _table_0.hello, _table_0.world

tests/outputs/local.lua

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
local a
2-
local a, b, c
2+
local a, b, c
3+
local x = 1212
4+
local something
5+
something = function()
6+
local x
7+
x = 1212
8+
end

0 commit comments

Comments
 (0)