replace has_value with functional value_can_be_statement

This commit is contained in:
leaf corcoran 2015-10-05 20:19:36 -07:00
parent e4c4e8e0ee
commit c4ad47c112
4 changed files with 23 additions and 26 deletions

View File

@ -8,10 +8,10 @@ do
end
local Set
Set = require("moonscript.data").Set
local ntype, has_value
local ntype, value_can_be_statement
do
local _obj_0 = require("moonscript.types")
ntype, has_value = _obj_0.ntype, _obj_0.has_value
ntype, value_can_be_statement = _obj_0.ntype, _obj_0.value_can_be_statement
end
local statement_compilers = require("moonscript.compile.statement")
local value_compilers = require("moonscript.compile.value")
@ -525,7 +525,9 @@ do
if fn then
result = fn(self, node, ...)
else
if has_value(node) then
if value_can_be_statement(node) then
result = self:value(node)
else
result = self:stm({
"assign",
{
@ -535,8 +537,6 @@ do
node
}
})
else
result = self:value(node)
end
end
end

View File

@ -5,7 +5,7 @@ transform = require "moonscript.transform"
import NameProxy, LocalName from require "moonscript.transform.names"
import Set from require "moonscript.data"
import ntype, has_value from require "moonscript.types"
import ntype, value_can_be_statement from require "moonscript.types"
statement_compilers = require "moonscript.compile.statement"
value_compilers = require "moonscript.compile.value"
@ -379,11 +379,11 @@ class Block
result = if fn = @statement_compilers[ntype(node)]
fn @, node, ...
else
-- coerce value into statement
if has_value node
@stm {"assign", {"_"}, {node}}
else
if value_can_be_statement node
@value node
else
-- coerce value into statement
@stm {"assign", {"_"}, {node}}
if result
if type(node) == "table" and type(result) == "table" and node[-1]

View File

@ -45,14 +45,12 @@ do
return moon_type(val)
end
end
local has_value
has_value = function(node)
if ntype(node) == "chain" then
local ctype = ntype(node[#node])
return ctype ~= "call" and ctype ~= "colon"
else
return true
local value_can_be_statement
value_can_be_statement = function(node)
if not (ntype(node) == "chain") then
return false
end
return ntype(node[#node]) == "call"
end
local is_value
is_value = function(stm)
@ -320,7 +318,7 @@ return {
cascading = cascading,
value_is_singular = value_is_singular,
comprehension_has_value = comprehension_has_value,
has_value = has_value,
value_can_be_statement = value_can_be_statement,
mtype = mtype,
terminating = terminating
}

View File

@ -40,13 +40,11 @@ mtype = do
return "table" if mt and mt.smart_node
moon_type val
-- does this always return a value
has_value = (node) ->
if ntype(node) == "chain"
ctype = ntype(node[#node])
ctype != "call" and ctype != "colon"
else
true
-- can this value be compiled in a line by itself
value_can_be_statement = (node) ->
return false unless ntype(node) == "chain"
-- it's a function call
ntype(node[#node]) == "call"
is_value = (stm) ->
compile = require "moonscript.compile"
@ -193,6 +191,7 @@ smart_node = (node) ->
{
:ntype, :smart_node, :build, :is_value, :is_slice, :manual_return,
:cascading, :value_is_singular, :comprehension_has_value, :has_value, :mtype, :terminating
:cascading, :value_is_singular, :comprehension_has_value,
:value_can_be_statement, :mtype, :terminating
}