Skip to content

Commit f560952

Browse files
committed
extract declarations in transform instead of delayed line
1 parent e075c2f commit f560952

File tree

8 files changed

+177
-108
lines changed

8 files changed

+177
-108
lines changed

moonscript/compile.lua

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,18 @@ do
582582
if ret then
583583
error("deprecated stms call, use transformer")
584584
end
585-
local _list_0 = stms
586-
for _index_0 = 1, #_list_0 do
587-
local stm = _list_0[_index_0]
588-
self:stm(stm)
585+
local current_stms, current_stm_i
586+
do
587+
local _obj_0 = self
588+
current_stms, current_stm_i = _obj_0.current_stms, _obj_0.current_stm_i
589+
end
590+
self.current_stms = stms
591+
for i = 1, #stms do
592+
self.current_stm_i = i
593+
self:stm(stms[i])
589594
end
595+
self.current_stms = current_stms
596+
self.current_stm_i = current_stm_i
590597
return nil
591598
end,
592599
splice = function(self, fn)
@@ -663,11 +670,7 @@ do
663670
if not (self.options.implicitly_return_root == false) then
664671
stms = transform.Statement.transformers.root_stms(self, stms)
665672
end
666-
local _list_0 = stms
667-
for _index_0 = 1, #_list_0 do
668-
local s = _list_0[_index_0]
669-
self:stm(s)
670-
end
673+
return self:stms(stms)
671674
end,
672675
render = function(self)
673676
local buffer = self._lines:flatten()

moonscript/compile.moon

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,16 @@ class Block
356356

357357
stms: (stms, ret) =>
358358
error "deprecated stms call, use transformer" if ret
359-
@stm stm for stm in *stms
359+
{:current_stms, :current_stm_i} = @
360+
361+
@current_stms = stms
362+
for i=1,#stms
363+
@current_stm_i = i
364+
@stm stms[i]
365+
366+
@current_stms = current_stms
367+
@current_stm_i = current_stm_i
368+
360369
nil
361370

362371
splice: (fn) =>
@@ -374,7 +383,7 @@ class RootBlock extends Block
374383
root_stms: (stms) =>
375384
unless @options.implicitly_return_root == false
376385
stms = transform.Statement.transformers.root_stms self, stms
377-
@stm s for s in *stms
386+
@stms stms
378387

379388
render: =>
380389
-- print @_lines

moonscript/compile/statement.lua

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,6 @@ local statement_compilers = {
3939
end
4040
end
4141
end,
42-
declare_glob = function(self, node)
43-
local kind = node[2]
44-
local names = { }
45-
local fn
46-
local _exp_0 = node[2]
47-
if "*" == _exp_0 then
48-
fn = function(name)
49-
if type(name) == "string" then
50-
insert(names, name)
51-
return true
52-
end
53-
end
54-
elseif "^" == _exp_0 then
55-
fn = function(name)
56-
if type(name) == "string" and name:match("^%u") then
57-
insert(names, name)
58-
return true
59-
end
60-
end
61-
else
62-
fn = error("unknown glob")
63-
end
64-
self:set("name_glob", fn)
65-
return data.DelayedLine(function(buff)
66-
insert(buff, "local ")
67-
local _list_0 = names
68-
for _index_0 = 1, #_list_0 do
69-
local name = _list_0[_index_0]
70-
insert(buff, self:name(name))
71-
insert(buff, ", ")
72-
end
73-
buff[#buff] = nil
74-
end)
75-
end,
7642
declare_with_shadows = function(self, node)
7743
local names = node[2]
7844
self:declare(names)

moonscript/compile/statement.moon

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,6 @@ statement_compilers =
2020
with @line "local "
2121
\append_list [@name name for name in *undeclared], ", "
2222

23-
declare_glob: (node) =>
24-
kind = node[2]
25-
26-
names = {}
27-
fn = switch node[2]
28-
when "*"
29-
(name) ->
30-
if type(name) == "string"
31-
insert names, name
32-
true
33-
when "^"
34-
(name) ->
35-
if type(name) == "string" and name\match "^%u"
36-
insert names, name
37-
true
38-
else
39-
error "unknown glob"
40-
41-
@set "name_glob", fn
42-
43-
data.DelayedLine (buff) ->
44-
insert buff, "local "
45-
for name in *names
46-
insert buff, @name name
47-
insert buff, ", "
48-
buff[#buff] = nil -- strips local if no names
49-
5023
-- this overrides the existing names with new locals, used for local keyword
5124
declare_with_shadows: (node) =>
5225
names = node[2]

moonscript/transform.lua

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,35 @@ hoist_declarations = function(body)
127127
assigns
128128
})
129129
end
130+
local extract_declarations
131+
extract_declarations = function(self, body, start, out)
132+
if body == nil then
133+
body = self.current_stms
134+
end
135+
if start == nil then
136+
start = self.current_stm_i + 1
137+
end
138+
if out == nil then
139+
out = { }
140+
end
141+
for i = start, #body do
142+
local stm = self.transform.statement(body[i])
143+
body[i] = stm
144+
local _exp_0 = stm[1]
145+
if "assign" == _exp_0 or "declare" == _exp_0 then
146+
local _list_0 = stm[2]
147+
for _index_0 = 1, #_list_0 do
148+
local name = _list_0[_index_0]
149+
if type(name) == "string" then
150+
insert(out, name)
151+
end
152+
end
153+
elseif "group" == _exp_0 then
154+
extract_declarations(self, stm[2], 1, out)
155+
end
156+
end
157+
return out
158+
end
130159
local expand_elseif_assign
131160
expand_elseif_assign = function(ifstm)
132161
for i = 4, #ifstm do
@@ -342,20 +371,40 @@ local Statement = Transformer({
342371
root_stms = function(self, body)
343372
return apply_to_last(body, implicitly_return(self))
344373
end,
345-
assign = function(self, node)
346-
local names, values = unpack(node, 2)
347-
do
348-
local globber = self:get_current("name_glob")
349-
if globber then
374+
declare_glob = function(self, node)
375+
local names = extract_declarations(self)
376+
if node[2] == "^" then
377+
names = (function()
378+
local _accum_0 = { }
379+
local _len_0 = 1
350380
local _list_0 = names
351381
for _index_0 = 1, #_list_0 do
352-
local name = _list_0[_index_0]
353-
if globber(name) then
354-
self:put_name(name)
382+
local _continue_0 = false
383+
repeat
384+
local name = _list_0[_index_0]
385+
if not (name:match("^%u")) then
386+
_continue_0 = true
387+
break
388+
end
389+
local _value_0 = name
390+
_accum_0[_len_0] = _value_0
391+
_len_0 = _len_0 + 1
392+
_continue_0 = true
393+
until true
394+
if not _continue_0 then
395+
break
355396
end
356397
end
357-
end
398+
return _accum_0
399+
end)()
358400
end
401+
return {
402+
"declare",
403+
names
404+
}
405+
end,
406+
assign = function(self, node)
407+
local names, values = unpack(node, 2)
359408
local transformed
360409
if #values == 1 then
361410
local value = values[1]

moonscript/transform.moon

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,20 @@ hoist_declarations = (body) ->
6767

6868
table.insert body, idx, {"declare", assigns}
6969

70+
71+
-- this mutates body searching for assigns
72+
extract_declarations = (body=@current_stms, start=@current_stm_i + 1, out={}) =>
73+
for i=start,#body
74+
stm = @transform.statement body[i]
75+
body[i] = stm
76+
switch stm[1]
77+
when "assign", "declare"
78+
for name in *stm[2]
79+
insert out, name if type(name) == "string"
80+
when "group"
81+
extract_declarations @, stm[2], 1, out
82+
out
83+
7084
expand_elseif_assign = (ifstm) ->
7185
for i = 4, #ifstm
7286
case = ifstm[i]
@@ -161,14 +175,19 @@ Statement = Transformer {
161175
root_stms: (body) =>
162176
apply_to_last body, implicitly_return @
163177

178+
declare_glob: (node) =>
179+
names = extract_declarations @
180+
181+
if node[2] == "^"
182+
names = for name in *names
183+
continue unless name\match "^%u"
184+
name
185+
186+
{"declare", names}
187+
164188
assign: (node) =>
165189
names, values = unpack node, 2
166190

167-
if globber = @get_current "name_glob"
168-
for name in *names
169-
if globber name
170-
@put_name name
171-
172191
-- bubble cascading assigns
173192
transformed = if #values == 1
174193
value = values[1]

tests/inputs/local.moon

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11

2-
local a
3-
local a,b,c
2+
do
3+
local a
4+
local a,b,c
45

6+
b,g = 23232
57

6-
x = 1212
7-
something = ->
8-
local x
9-
x = 1212
108

9+
do
10+
x = 1212
11+
something = ->
12+
local x
13+
x = 1212
1114

1215
do
1316
local *
@@ -39,7 +42,6 @@ do
3942
if something
4043
x = 2323
4144

42-
-- this is broken
4345
do
4446
local *
4547
do
@@ -50,3 +52,27 @@ do
5052
do
5153
x = "two"
5254

55+
do
56+
local *
57+
k = if what
58+
10
59+
x = 100
60+
61+
{:a,:b, :c} = y
62+
63+
64+
do
65+
local *
66+
67+
a = 100
68+
print "hi"
69+
b = 200
70+
71+
local *
72+
c = 100
73+
print "hi"
74+
d = 200
75+
d = 2323
76+
77+
78+
g = 2323 -- test if anything leaked

0 commit comments

Comments
 (0)