Skip to content

Commit f04af88

Browse files
committed
slices will reuse local vars for list
1 parent 0848c1e commit f04af88

File tree

5 files changed

+28
-21
lines changed

5 files changed

+28
-21
lines changed

moonscript/compile.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,9 @@ do
389389
if t == NameProxy or t == LocalName then
390390
return true
391391
end
392+
if t == "table" and node[1] == "chain" and #node == 2 then
393+
return self:is_local(node[2])
394+
end
392395
return false
393396
end,
394397
free_name = function(self, prefix, dont_put)

moonscript/compile.moon

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ class Block
257257
t = mtype node
258258
return @has_name(node, false) if t == "string"
259259
return true if t == NameProxy or t == LocalName
260+
261+
if t == "table" and node[1] == "chain" and #node == 2
262+
return @is_local node[2]
263+
260264
false
261265

262266
free_name: (prefix, dont_put) =>

moonscript/transform.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,9 @@ Statement = Transformer({
716716
local slice = list[#list]
717717
table.remove(list)
718718
table.remove(slice, 1)
719+
if self:is_local(list) then
720+
list_name = list
721+
end
719722
if slice[2] and slice[2] ~= "" then
720723
local max_tmp_name = NameProxy("max")
721724
slice_var = build.assign_one(max_tmp_name, slice[2])

moonscript/transform.moon

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,7 @@ Statement = Transformer {
400400
list = source[2]
401401

402402
index_name = NameProxy "index"
403+
403404
list_name = @is_local(list) and list or NameProxy "list"
404405

405406
slice_var = nil
@@ -408,6 +409,8 @@ Statement = Transformer {
408409
table.remove list
409410
table.remove slice, 1
410411

412+
list_name = list if @is_local list
413+
411414
slice[2] = if slice[2] and slice[2] != ""
412415
max_tmp_name = NameProxy "max"
413416
slice_var = build.assign_one max_tmp_name, slice[2]

spec/outputs/lists.lua

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -221,38 +221,32 @@ x = {
221221
6,
222222
7
223223
}
224-
local _list_0 = x
225224
local _max_0 = -5
226-
for _index_0 = 2, _max_0 < 0 and #_list_0 + _max_0 or _max_0, 2 do
227-
local y = _list_0[_index_0]
225+
for _index_0 = 2, _max_0 < 0 and #x + _max_0 or _max_0, 2 do
226+
local y = x[_index_0]
228227
print(y)
229228
end
230-
local _list_1 = x
231229
local _max_1 = 3
232-
for _index_0 = 1, _max_1 < 0 and #_list_1 + _max_1 or _max_1 do
233-
local y = _list_1[_index_0]
230+
for _index_0 = 1, _max_1 < 0 and #x + _max_1 or _max_1 do
231+
local y = x[_index_0]
234232
print(y)
235233
end
236-
local _list_2 = x
237-
for _index_0 = 2, #_list_2 do
238-
local y = _list_2[_index_0]
234+
for _index_0 = 2, #x do
235+
local y = x[_index_0]
239236
print(y)
240237
end
241-
local _list_3 = x
242-
for _index_0 = 1, #_list_3, 2 do
243-
local y = _list_3[_index_0]
238+
for _index_0 = 1, #x, 2 do
239+
local y = x[_index_0]
244240
print(y)
245241
end
246-
local _list_4 = x
247-
for _index_0 = 2, #_list_4, 2 do
248-
local y = _list_4[_index_0]
242+
for _index_0 = 2, #x, 2 do
243+
local y = x[_index_0]
249244
print(y)
250245
end
251246
local a, b, c = 1, 5, 2
252-
local _list_5 = x
253247
local _max_2 = b
254-
for _index_0 = a, _max_2 < 0 and #_list_5 + _max_2 or _max_2, c do
255-
local y = _list_5[_index_0]
248+
for _index_0 = a, _max_2 < 0 and #x + _max_2 or _max_2, c do
249+
local y = x[_index_0]
256250
print(y)
257251
end
258252
local normal
@@ -274,9 +268,9 @@ for _index_0 = 1, #test do
274268
end
275269
local _
276270
_ = function()
277-
local _list_6 = rows
278-
for _index_0 = 1, #_list_6 do
279-
local row = _list_6[_index_0]
271+
local _list_0 = rows
272+
for _index_0 = 1, #_list_0 do
273+
local row = _list_0[_index_0]
280274
a = b
281275
end
282276
end

0 commit comments

Comments
 (0)