mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
destructure works with if assignment cond
This commit is contained in:
parent
5162082085
commit
181e06d9ae
@ -602,6 +602,22 @@ local Statement = Transformer({
|
|||||||
["if"] = function(self, node, ret)
|
["if"] = function(self, node, ret)
|
||||||
if ntype(node[2]) == "assign" then
|
if ntype(node[2]) == "assign" then
|
||||||
local _, assign, body = unpack(node)
|
local _, assign, body = unpack(node)
|
||||||
|
if destructure.has_destructure(assign[2]) then
|
||||||
|
local name = NameProxy("des")
|
||||||
|
body = {
|
||||||
|
destructure.build_assign(assign[2][1], name),
|
||||||
|
build.group(node[3])
|
||||||
|
}
|
||||||
|
return build["do"]({
|
||||||
|
build.assign_one(name, assign[3][1]),
|
||||||
|
{
|
||||||
|
"if",
|
||||||
|
name,
|
||||||
|
body,
|
||||||
|
unpack(node, 4)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
else
|
||||||
local name = assign[2][1]
|
local name = assign[2][1]
|
||||||
return build["do"]({
|
return build["do"]({
|
||||||
assign,
|
assign,
|
||||||
@ -612,6 +628,7 @@ local Statement = Transformer({
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
|
end
|
||||||
node = expand_elseif_assign(node)
|
node = expand_elseif_assign(node)
|
||||||
if ret then
|
if ret then
|
||||||
smart_node(node)
|
smart_node(node)
|
||||||
@ -668,8 +685,7 @@ local Statement = Transformer({
|
|||||||
do
|
do
|
||||||
local _with_0 = NameProxy("des")
|
local _with_0 = NameProxy("des")
|
||||||
local proxy = _with_0
|
local proxy = _with_0
|
||||||
local extracted = destructure.extract_assign_names(name)
|
insert(destructures, destructure.build_assign(name, proxy))
|
||||||
insert(destructures, destructure.build_assign(extracted, proxy))
|
|
||||||
_value_0 = _with_0
|
_value_0 = _with_0
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -291,6 +291,19 @@ Statement = Transformer {
|
|||||||
-- expand assign in cond
|
-- expand assign in cond
|
||||||
if ntype(node[2]) == "assign"
|
if ntype(node[2]) == "assign"
|
||||||
_, assign, body = unpack node
|
_, assign, body = unpack node
|
||||||
|
if destructure.has_destructure assign[2]
|
||||||
|
name = NameProxy "des"
|
||||||
|
|
||||||
|
body = {
|
||||||
|
destructure.build_assign assign[2][1], name
|
||||||
|
build.group node[3]
|
||||||
|
}
|
||||||
|
|
||||||
|
return build.do {
|
||||||
|
build.assign_one name, assign[3][1]
|
||||||
|
{"if", name, body, unpack node, 4}
|
||||||
|
}
|
||||||
|
else
|
||||||
name = assign[2][1]
|
name = assign[2][1]
|
||||||
return build["do"] {
|
return build["do"] {
|
||||||
assign
|
assign
|
||||||
@ -341,8 +354,7 @@ Statement = Transformer {
|
|||||||
node.names = for i, name in ipairs node.names
|
node.names = for i, name in ipairs node.names
|
||||||
if ntype(name) == "table"
|
if ntype(name) == "table"
|
||||||
with proxy = NameProxy "des"
|
with proxy = NameProxy "des"
|
||||||
extracted = destructure.extract_assign_names name
|
insert destructures, destructure.build_assign name, proxy
|
||||||
insert destructures, destructure.build_assign extracted, proxy
|
|
||||||
else
|
else
|
||||||
name
|
name
|
||||||
|
|
||||||
|
@ -46,47 +46,6 @@ has_destructure = function(names)
|
|||||||
end
|
end
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
local build_assign
|
|
||||||
build_assign = function(extracted_names, receiver)
|
|
||||||
local names = { }
|
|
||||||
local values = { }
|
|
||||||
local inner = {
|
|
||||||
"assign",
|
|
||||||
names,
|
|
||||||
values
|
|
||||||
}
|
|
||||||
local obj
|
|
||||||
if mtype(receiver) == NameProxy then
|
|
||||||
obj = receiver
|
|
||||||
else
|
|
||||||
do
|
|
||||||
local _with_0 = NameProxy("obj")
|
|
||||||
obj = _with_0
|
|
||||||
inner = build["do"]({
|
|
||||||
build.assign_one(obj, receiver),
|
|
||||||
{
|
|
||||||
"assign",
|
|
||||||
names,
|
|
||||||
values
|
|
||||||
}
|
|
||||||
})
|
|
||||||
obj = _with_0
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local _list_0 = extracted_names
|
|
||||||
for _index_0 = 1, #_list_0 do
|
|
||||||
local tuple = _list_0[_index_0]
|
|
||||||
insert(names, tuple[1])
|
|
||||||
insert(values, obj:chain(unpack(tuple[2])))
|
|
||||||
end
|
|
||||||
return build.group({
|
|
||||||
{
|
|
||||||
"declare",
|
|
||||||
names
|
|
||||||
},
|
|
||||||
inner
|
|
||||||
})
|
|
||||||
end
|
|
||||||
local extract_assign_names
|
local extract_assign_names
|
||||||
extract_assign_names = function(name, accum, prefix)
|
extract_assign_names = function(name, accum, prefix)
|
||||||
if accum == nil then
|
if accum == nil then
|
||||||
@ -143,6 +102,48 @@ extract_assign_names = function(name, accum, prefix)
|
|||||||
end
|
end
|
||||||
return accum
|
return accum
|
||||||
end
|
end
|
||||||
|
local build_assign
|
||||||
|
build_assign = function(destruct_literal, receiver)
|
||||||
|
local extracted_names = extract_assign_names(destruct_literal)
|
||||||
|
local names = { }
|
||||||
|
local values = { }
|
||||||
|
local inner = {
|
||||||
|
"assign",
|
||||||
|
names,
|
||||||
|
values
|
||||||
|
}
|
||||||
|
local obj
|
||||||
|
if mtype(receiver) == NameProxy then
|
||||||
|
obj = receiver
|
||||||
|
else
|
||||||
|
do
|
||||||
|
local _with_0 = NameProxy("obj")
|
||||||
|
obj = _with_0
|
||||||
|
inner = build["do"]({
|
||||||
|
build.assign_one(obj, receiver),
|
||||||
|
{
|
||||||
|
"assign",
|
||||||
|
names,
|
||||||
|
values
|
||||||
|
}
|
||||||
|
})
|
||||||
|
obj = _with_0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local _list_0 = extracted_names
|
||||||
|
for _index_0 = 1, #_list_0 do
|
||||||
|
local tuple = _list_0[_index_0]
|
||||||
|
insert(names, tuple[1])
|
||||||
|
insert(values, obj:chain(unpack(tuple[2])))
|
||||||
|
end
|
||||||
|
return build.group({
|
||||||
|
{
|
||||||
|
"declare",
|
||||||
|
names
|
||||||
|
},
|
||||||
|
inner
|
||||||
|
})
|
||||||
|
end
|
||||||
local split_assign
|
local split_assign
|
||||||
split_assign = function(assign)
|
split_assign = function(assign)
|
||||||
local names, values = unpack(assign, 2)
|
local names, values = unpack(assign, 2)
|
||||||
@ -182,8 +183,7 @@ split_assign = function(assign)
|
|||||||
end)()
|
end)()
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
local extracted = extract_assign_names(n)
|
insert(g, build_assign(n, values[i]))
|
||||||
insert(g, build_assign(extracted, values[i]))
|
|
||||||
start = i + 1
|
start = i + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -237,6 +237,5 @@ end
|
|||||||
return {
|
return {
|
||||||
has_destructure = has_destructure,
|
has_destructure = has_destructure,
|
||||||
split_assign = split_assign,
|
split_assign = split_assign,
|
||||||
extract_assign_names = extract_assign_names,
|
|
||||||
build_assign = build_assign
|
build_assign = build_assign
|
||||||
}
|
}
|
||||||
|
@ -18,30 +18,6 @@ has_destructure = (names) ->
|
|||||||
return true if ntype(n) == "table"
|
return true if ntype(n) == "table"
|
||||||
false
|
false
|
||||||
|
|
||||||
build_assign = (extracted_names, receiver) ->
|
|
||||||
names = {}
|
|
||||||
values = {}
|
|
||||||
|
|
||||||
inner = {"assign", names, values}
|
|
||||||
|
|
||||||
obj = if mtype(receiver) == NameProxy
|
|
||||||
receiver
|
|
||||||
else
|
|
||||||
with obj = NameProxy "obj"
|
|
||||||
inner = build.do {
|
|
||||||
build.assign_one obj, receiver
|
|
||||||
{"assign", names, values}
|
|
||||||
}
|
|
||||||
|
|
||||||
for tuple in *extracted_names
|
|
||||||
insert names, tuple[1]
|
|
||||||
insert values, obj\chain unpack tuple[2]
|
|
||||||
|
|
||||||
build.group {
|
|
||||||
{"declare", names}
|
|
||||||
inner
|
|
||||||
}
|
|
||||||
|
|
||||||
extract_assign_names = (name, accum={}, prefix={}) ->
|
extract_assign_names = (name, accum={}, prefix={}) ->
|
||||||
i = 1
|
i = 1
|
||||||
for tuple in *name[2]
|
for tuple in *name[2]
|
||||||
@ -70,6 +46,32 @@ extract_assign_names = (name, accum={}, prefix={}) ->
|
|||||||
|
|
||||||
accum
|
accum
|
||||||
|
|
||||||
|
build_assign = (destruct_literal, receiver) ->
|
||||||
|
extracted_names = extract_assign_names destruct_literal
|
||||||
|
|
||||||
|
names = {}
|
||||||
|
values = {}
|
||||||
|
|
||||||
|
inner = {"assign", names, values}
|
||||||
|
|
||||||
|
obj = if mtype(receiver) == NameProxy
|
||||||
|
receiver
|
||||||
|
else
|
||||||
|
with obj = NameProxy "obj"
|
||||||
|
inner = build.do {
|
||||||
|
build.assign_one obj, receiver
|
||||||
|
{"assign", names, values}
|
||||||
|
}
|
||||||
|
|
||||||
|
for tuple in *extracted_names
|
||||||
|
insert names, tuple[1]
|
||||||
|
insert values, obj\chain unpack tuple[2]
|
||||||
|
|
||||||
|
build.group {
|
||||||
|
{"declare", names}
|
||||||
|
inner
|
||||||
|
}
|
||||||
|
|
||||||
-- applies to destructuring to a assign node
|
-- applies to destructuring to a assign node
|
||||||
split_assign = (assign) ->
|
split_assign = (assign) ->
|
||||||
names, values = unpack assign, 2
|
names, values = unpack assign, 2
|
||||||
@ -93,8 +95,7 @@ split_assign = (assign) ->
|
|||||||
values[i]
|
values[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
extracted = extract_assign_names n
|
insert g, build_assign n, values[i]
|
||||||
insert g, build_assign extracted, values[i]
|
|
||||||
|
|
||||||
start = i + 1
|
start = i + 1
|
||||||
|
|
||||||
@ -113,4 +114,4 @@ split_assign = (assign) ->
|
|||||||
|
|
||||||
build.group g
|
build.group g
|
||||||
|
|
||||||
{ :has_destructure, :split_assign, :extract_assign_names, :build_assign }
|
{ :has_destructure, :split_assign, :build_assign }
|
||||||
|
@ -51,3 +51,28 @@ for {x,y} in *thing
|
|||||||
print x,y
|
print x,y
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
with {a,b} = thing
|
||||||
|
print a, b
|
||||||
|
|
||||||
|
|
||||||
|
--
|
||||||
|
|
||||||
|
thing = nil
|
||||||
|
if {a} = thing
|
||||||
|
print a
|
||||||
|
else
|
||||||
|
print "nothing"
|
||||||
|
|
||||||
|
thang = {1,2}
|
||||||
|
if {a,b} = thang
|
||||||
|
print a,b
|
||||||
|
|
||||||
|
if {a,b} = thing
|
||||||
|
print a,b
|
||||||
|
elseif {c,d} = thang
|
||||||
|
print c,d
|
||||||
|
else
|
||||||
|
print "NO"
|
||||||
|
|
||||||
|
@ -102,3 +102,46 @@ for _index_0 = 1, #_list_0 do
|
|||||||
x, y = _des_0[1], _des_0[2]
|
x, y = _des_0[1], _des_0[2]
|
||||||
print(x, y)
|
print(x, y)
|
||||||
end
|
end
|
||||||
|
do
|
||||||
|
local _with_0 = thing
|
||||||
|
a, b = _with_0[1], _with_0[2]
|
||||||
|
print(a, b)
|
||||||
|
end
|
||||||
|
thing = nil
|
||||||
|
do
|
||||||
|
local _des_0 = thing
|
||||||
|
if _des_0 then
|
||||||
|
a = _des_0[1]
|
||||||
|
print(a)
|
||||||
|
else
|
||||||
|
print("nothing")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
local thang = {
|
||||||
|
1,
|
||||||
|
2
|
||||||
|
}
|
||||||
|
do
|
||||||
|
local _des_0 = thang
|
||||||
|
if _des_0 then
|
||||||
|
a, b = _des_0[1], _des_0[2]
|
||||||
|
print(a, b)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
do
|
||||||
|
local _des_0 = thing
|
||||||
|
if _des_0 then
|
||||||
|
a, b = _des_0[1], _des_0[2]
|
||||||
|
print(a, b)
|
||||||
|
else
|
||||||
|
do
|
||||||
|
local _des_1 = thang
|
||||||
|
if _des_1 then
|
||||||
|
c, d = _des_1[1], _des_1[2]
|
||||||
|
print(c, d)
|
||||||
|
else
|
||||||
|
return print("NO")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
12
todo
12
todo
@ -1,4 +1,9 @@
|
|||||||
# TODO
|
# TODO
|
||||||
|
#
|
||||||
|
#
|
||||||
|
#
|
||||||
|
|
||||||
|
-- local * and local ^
|
||||||
|
|
||||||
-- seems like running in moon messes up require order
|
-- seems like running in moon messes up require order
|
||||||
|
|
||||||
@ -8,19 +13,14 @@
|
|||||||
|
|
||||||
- or= and=
|
- or= and=
|
||||||
|
|
||||||
- a do block for making a quick anon func and calling it
|
|
||||||
|
|
||||||
- error with stray comma at end of line
|
- error with stray comma at end of line
|
||||||
- ugly error if attempting to assign to rvalue like #hello
|
|
||||||
|
|
||||||
- class expressions, x = class extends Hello do new: => print "hello"
|
|
||||||
|
|
||||||
* multiline comments
|
* multiline comments
|
||||||
* table slices (almost)
|
* table slices (almost)
|
||||||
|
|
||||||
* combine for and if line decorators
|
* combine for and if line decorators
|
||||||
|
|
||||||
* export later?
|
* export later? nah
|
||||||
|
|
||||||
x = 232
|
x = 232
|
||||||
export x
|
export x
|
||||||
|
Loading…
Reference in New Issue
Block a user