From c4ad47c1123c303eeec1ebd3e00973f2297b6da8 Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Mon, 5 Oct 2015 20:19:36 -0700 Subject: [PATCH] replace has_value with functional value_can_be_statement --- moonscript/compile.lua | 10 +++++----- moonscript/compile.moon | 10 +++++----- moonscript/types.lua | 14 ++++++-------- moonscript/types.moon | 15 +++++++-------- 4 files changed, 23 insertions(+), 26 deletions(-) diff --git a/moonscript/compile.lua b/moonscript/compile.lua index 5eab8e6..98045ce 100644 --- a/moonscript/compile.lua +++ b/moonscript/compile.lua @@ -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 diff --git a/moonscript/compile.moon b/moonscript/compile.moon index 6206b26..a2b17fe 100644 --- a/moonscript/compile.moon +++ b/moonscript/compile.moon @@ -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] diff --git a/moonscript/types.lua b/moonscript/types.lua index 29c2452..a8bd6c7 100644 --- a/moonscript/types.lua +++ b/moonscript/types.lua @@ -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 } diff --git a/moonscript/types.moon b/moonscript/types.moon index ad00a1d..abac311 100644 --- a/moonscript/types.moon +++ b/moonscript/types.moon @@ -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 }