Skip to content

Commit c4ad47c

Browse files
committed
replace has_value with functional value_can_be_statement
1 parent e4c4e8e commit c4ad47c

File tree

4 files changed

+23
-26
lines changed

4 files changed

+23
-26
lines changed

moonscript/compile.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ do
88
end
99
local Set
1010
Set = require("moonscript.data").Set
11-
local ntype, has_value
11+
local ntype, value_can_be_statement
1212
do
1313
local _obj_0 = require("moonscript.types")
14-
ntype, has_value = _obj_0.ntype, _obj_0.has_value
14+
ntype, value_can_be_statement = _obj_0.ntype, _obj_0.value_can_be_statement
1515
end
1616
local statement_compilers = require("moonscript.compile.statement")
1717
local value_compilers = require("moonscript.compile.value")
@@ -525,7 +525,9 @@ do
525525
if fn then
526526
result = fn(self, node, ...)
527527
else
528-
if has_value(node) then
528+
if value_can_be_statement(node) then
529+
result = self:value(node)
530+
else
529531
result = self:stm({
530532
"assign",
531533
{
@@ -535,8 +537,6 @@ do
535537
node
536538
}
537539
})
538-
else
539-
result = self:value(node)
540540
end
541541
end
542542
end

moonscript/compile.moon

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ transform = require "moonscript.transform"
55

66
import NameProxy, LocalName from require "moonscript.transform.names"
77
import Set from require "moonscript.data"
8-
import ntype, has_value from require "moonscript.types"
8+
import ntype, value_can_be_statement from require "moonscript.types"
99

1010
statement_compilers = require "moonscript.compile.statement"
1111
value_compilers = require "moonscript.compile.value"
@@ -379,11 +379,11 @@ class Block
379379
result = if fn = @statement_compilers[ntype(node)]
380380
fn @, node, ...
381381
else
382-
-- coerce value into statement
383-
if has_value node
384-
@stm {"assign", {"_"}, {node}}
385-
else
382+
if value_can_be_statement node
386383
@value node
384+
else
385+
-- coerce value into statement
386+
@stm {"assign", {"_"}, {node}}
387387

388388
if result
389389
if type(node) == "table" and type(result) == "table" and node[-1]

moonscript/types.lua

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@ do
4545
return moon_type(val)
4646
end
4747
end
48-
local has_value
49-
has_value = function(node)
50-
if ntype(node) == "chain" then
51-
local ctype = ntype(node[#node])
52-
return ctype ~= "call" and ctype ~= "colon"
53-
else
54-
return true
48+
local value_can_be_statement
49+
value_can_be_statement = function(node)
50+
if not (ntype(node) == "chain") then
51+
return false
5552
end
53+
return ntype(node[#node]) == "call"
5654
end
5755
local is_value
5856
is_value = function(stm)
@@ -320,7 +318,7 @@ return {
320318
cascading = cascading,
321319
value_is_singular = value_is_singular,
322320
comprehension_has_value = comprehension_has_value,
323-
has_value = has_value,
321+
value_can_be_statement = value_can_be_statement,
324322
mtype = mtype,
325323
terminating = terminating
326324
}

moonscript/types.moon

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,11 @@ mtype = do
4040
return "table" if mt and mt.smart_node
4141
moon_type val
4242

43-
-- does this always return a value
44-
has_value = (node) ->
45-
if ntype(node) == "chain"
46-
ctype = ntype(node[#node])
47-
ctype != "call" and ctype != "colon"
48-
else
49-
true
43+
-- can this value be compiled in a line by itself
44+
value_can_be_statement = (node) ->
45+
return false unless ntype(node) == "chain"
46+
-- it's a function call
47+
ntype(node[#node]) == "call"
5048

5149
is_value = (stm) ->
5250
compile = require "moonscript.compile"
@@ -193,6 +191,7 @@ smart_node = (node) ->
193191

194192
{
195193
:ntype, :smart_node, :build, :is_value, :is_slice, :manual_return,
196-
:cascading, :value_is_singular, :comprehension_has_value, :has_value, :mtype, :terminating
194+
:cascading, :value_is_singular, :comprehension_has_value,
195+
:value_can_be_statement, :mtype, :terminating
197196
}
198197

0 commit comments

Comments
 (0)