Skip to content

Commit 625a240

Browse files
committed
redo varargs bubbling with send, fix some varargs issues
1 parent ec6b222 commit 625a240

File tree

8 files changed

+44
-37
lines changed

8 files changed

+44
-37
lines changed

moonscript/compile.lua

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,6 @@ Block = (function()
154154
end
155155
return "Block<" .. tostring(h) .. "> <- " .. tostring(self.parent)
156156
end,
157-
bubble = function(self, other)
158-
if other == nil then
159-
other = self.parent
160-
end
161-
local has_varargs = self.has_varargs and not self:has_name("...")
162-
other.has_varargs = other.has_varargs or has_varargs
163-
end,
164157
line_table = function(self)
165158
return self._posmap
166159
end,
@@ -173,6 +166,9 @@ Block = (function()
173166
listen = function(self, name, fn)
174167
self._listeners[name] = fn
175168
end,
169+
unlisten = function(self, name)
170+
self._listeners[name] = nil
171+
end,
176172
send = function(self, name, ...)
177173
do
178174
local fn = self._listeners[name]

moonscript/compile.moon

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,6 @@ class Block
112112
else
113113
@indent = 0
114114

115-
-- bubble properties into parent
116-
bubble: (other=@parent) =>
117-
has_varargs = @has_varargs and not @has_name "..."
118-
other.has_varargs = other.has_varargs or has_varargs
119-
120115
line_table: =>
121116
@_posmap
122117

@@ -129,6 +124,9 @@ class Block
129124
listen: (name, fn) =>
130125
@_listeners[name] = fn
131126

127+
unlisten: (name) =>
128+
@_listeners[name] = nil
129+
132130
send: (name, ...) =>
133131
if fn = @_listeners[name]
134132
fn self, ...

moonscript/compile/value.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ value_compile = {
298298
return self:value(sup(self))
299299
end
300300
if value == "..." then
301-
self.has_varargs = true
301+
self:send("varargs")
302302
end
303303
return tostring(value)
304304
end

moonscript/compile/value.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,6 @@ value_compile =
192192
return @value sup self
193193

194194
if value == "..."
195-
@has_varargs = true
195+
@send "varargs"
196196

197197
tostring value

moonscript/transform.lua

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1453,6 +1453,12 @@ Value = Transformer({
14531453
fndef = function(self, node)
14541454
smart_node(node)
14551455
node.body = apply_to_last(node.body, implicitly_return(self))
1456+
node.body = {
1457+
Run(function(self)
1458+
return self:listen("varargs", function() end)
1459+
end),
1460+
unpack(node.body)
1461+
}
14561462
return node
14571463
end,
14581464
["if"] = function(self, node)
@@ -1549,16 +1555,19 @@ Value = Transformer({
15491555
local _, body = unpack(node)
15501556
local fn = nil
15511557
local arg_list = { }
1552-
insert(body, Run(function(self)
1553-
if self.has_varargs then
1554-
insert(arg_list, "...")
1555-
return insert(fn.args, {
1556-
"..."
1557-
})
1558-
end
1559-
end))
15601558
fn = smart_node(build.fndef({
1561-
body = body
1559+
body = {
1560+
Run(function(self)
1561+
return self:listen("varargs", function()
1562+
insert(arg_list, "...")
1563+
insert(fn.args, {
1564+
"..."
1565+
})
1566+
return self:unlisten("varargs")
1567+
end)
1568+
end),
1569+
unpack(body)
1570+
}
15621571
}))
15631572
return build.chain({
15641573
base = {

moonscript/transform.moon

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,11 @@ Value = Transformer {
765765
fndef: (node) =>
766766
smart_node node
767767
node.body = apply_to_last node.body, implicitly_return self
768+
node.body = {
769+
Run => @listen "varargs", -> -- capture event
770+
unpack node.body
771+
}
772+
768773
node
769774

770775
if: (node) => build.block_exp { node }
@@ -823,12 +828,16 @@ Value = Transformer {
823828
fn = nil
824829
arg_list = {}
825830

826-
insert body, Run =>
827-
if @has_varargs
828-
insert arg_list, "..."
829-
insert fn.args, {"..."}
831+
fn = smart_node build.fndef body: {
832+
Run =>
833+
@listen "varargs", ->
834+
insert arg_list, "..."
835+
insert fn.args, {"..."}
836+
@unlisten "varargs"
837+
838+
unpack body
839+
}
830840

831-
fn = smart_node build.fndef body: body
832841
build.chain { base: {"parens", fn}, {"call", arg_list} }
833842
}
834843

tests/outputs/bubbling.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ local j = (function()
4646
end)()
4747
local m
4848
m = function(...)
49-
return (function()
49+
return (function(...)
5050
local _accum_0 = { }
5151
local _len_0 = 0
5252
local _list_0 = {
@@ -60,7 +60,7 @@ m = function(...)
6060
end
6161
end
6262
return _accum_0
63-
end)()
63+
end)(...)
6464
end
6565
local x = (function(...)
6666
local _accum_0 = { }
@@ -114,7 +114,7 @@ local a = (function(...)
114114
end
115115
return _accum_0
116116
end)(...)
117-
local b = (function(...)
117+
local b = (function()
118118
local _accum_0 = { }
119119
local _len_0 = 0
120120
for i = 1, 10 do
@@ -128,4 +128,4 @@ local b = (function(...)
128128
end
129129
end
130130
return _accum_0
131-
end)(...)
131+
end)()

todo

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ x = for x in y
4343

4444
* any/every keywords for comprehensions? (what about iterators)
4545

46-
not working right:
47-
48-
double_args = (...) ->
49-
[x * 2 for x in *{...}]
50-
5146
* let array items in table be defined without {} when indented
5247

5348

0 commit comments

Comments
 (0)