fix compile failure when comprehension exp has no value

This commit is contained in:
leaf corcoran 2011-11-19 13:03:51 -08:00
parent f7a93241d9
commit 02a5dbcdd1
6 changed files with 42 additions and 18 deletions

View File

@ -5,10 +5,6 @@ local data = require("moonscript.data")
local reversed = util.reversed
local ntype, build, smart_node, is_slice = types.ntype, types.build, types.smart_node, types.is_slice
local insert = table.insert
local is_value
is_value = function(stm)
return moonscript.compile.Block:is_value(stm) or Value:can_transform(stm)
end
NameProxy = (function()
local _parent_0 = nil
local _base_0 = {
@ -241,7 +237,7 @@ Statement = Transformer({
if #values == 1 and types.cascading[ntype(values[1])] then
values[1] = self.transform.statement(values[1], function(stm)
local t = ntype(stm)
if is_value(stm) then
if types.is_value(stm) then
return {
"assign",
names,
@ -843,15 +839,19 @@ implicitly_return = function(scope)
local fn
fn = function(stm)
local t = ntype(stm)
if types.manual_return[t] or not is_value(stm) then
if types.manual_return[t] or not types.is_value(stm) then
return stm
elseif types.cascading[t] then
return scope.transform.statement(stm, fn)
else
return {
"return",
stm
}
if t == "comprehension" and not types.comprehension_has_value(stm) then
return stm
else
return {
"return",
stm
}
end
end
end
return fn

View File

@ -11,10 +11,6 @@ import insert from table
export Statement, Value, NameProxy, Run
-- TODO refactor
is_value = (stm) ->
moonscript.compile.Block\is_value(stm) or Value\can_transform stm
class NameProxy
new: (@prefix) =>
self[1] = "temp_name"
@ -131,7 +127,7 @@ Statement = Transformer {
if #values == 1 and types.cascading[ntype values[1]]
values[1] = @transform.statement values[1], (stm) ->
t = ntype stm
if is_value stm
if types.is_value stm
{"assign", names, {stm}}
else
stm
@ -201,6 +197,7 @@ Statement = Transformer {
comprehension: (node, action) =>
_, exp, clauses = unpack node
action = action or (exp) -> {exp}
construct_comprehension action(exp), clauses
@ -470,12 +467,15 @@ default_accumulator = (node) =>
implicitly_return = (scope) ->
fn = (stm) ->
t = ntype stm
if types.manual_return[t] or not is_value stm
if types.manual_return[t] or not types.is_value stm
stm
elseif types.cascading[t]
scope.transform.statement stm, fn
else
{"return", stm}
if t == "comprehension" and not types.comprehension_has_value stm
stm
else
{"return", stm}
fn

View File

@ -13,6 +13,13 @@ cascading = data.Set({
"with",
"switch"
})
is_value = function(stm)
local compile, transform = moonscript.compile, moonscript.transform
return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
end
comprehension_has_value = function(comp)
return is_value(comp[2])
end
ntype = function(node)
if type(node) ~= "table" then
return "value"

View File

@ -2,8 +2,9 @@ module "moonscript.types", package.seeall
util = require "moonscript.util"
data = require "moonscript.data"
export ntype, smart_node, build
export ntype, smart_node, build, is_value
export is_slice, manual_return, cascading
export comprehension_has_value
import insert from table
@ -13,6 +14,13 @@ manual_return = data.Set{"foreach", "for", "while", "return"}
-- assigns and returns are bubbled into their bodies
cascading = data.Set{ "if", "with", "switch" }
is_value = (stm) ->
import compile, transform from moonscript
compile.Block\is_value(stm) or transform.Value\can_transform stm
comprehension_has_value = (comp) ->
is_value comp[2]
-- type of node as string
ntype = (node) ->
if type(node) != "table"

View File

@ -69,4 +69,5 @@ normal = (hello) ->
test = x 1,2,3,4,5
print thing for thing in *test
-> a = b for row in *rows

View File

@ -270,4 +270,12 @@ local _list_8 = test
for _index_0 = 1, #_list_8 do
local thing = _list_8[_index_0]
print(thing)
end
local _
_ = function()
local _list_9 = rows
for _index_0 = 1, #_list_9 do
local row = _list_9[_index_0]
a = b
end
end