chains can implicitly return

This commit is contained in:
leaf corcoran 2011-06-01 21:11:03 -07:00
parent 9c0fced067
commit 72eca8071c
4 changed files with 20 additions and 19 deletions

View File

@ -485,18 +485,19 @@ local compiler_index = {
stm = function(self, node, return_value) stm = function(self, node, return_value)
local value = self:value(node, return_value) local value = self:value(node, return_value)
if must_return[ntype(node)] then local is_value = must_return[ntype(node)]
if return_value then local ret_chain = ntype(node) == "chain" and node[2] ~= "return"
if return_value and (is_value or ret_chain) then
local return_to = "return" local return_to = "return"
if type(return_value) == "string" then if type(return_value) == "string" then
return_to = return_value.." =" return_to = return_value.." ="
end end
return return_to.." "..value return return_to.." "..value
else elseif is_value then
return self:value({"assign", {"_"}, {value}}, false) return self:value({"assign", {"_"}, {value}}, false)
end end
end
return value return value
end, end,

View File

@ -54,7 +54,7 @@ for z in items do
end end
end end
require("util") require("util")
local dump = function(x) print(util.dump(x)) end local dump = function(x) return print(util.dump(x)) end
local range = function(count) local range = function(count)
local i = 0 local i = 0
return(coroutine.wrap(function() return(coroutine.wrap(function()

View File

@ -1,13 +1,13 @@
local x = function() print(what) end local x = function() return print(what) end
local _ = function() end local _ = function() end
_ = function() _ = function()
return function() return function() end end return function() return function() end end
end end
go(to(the(barn))) go(to(the(barn)))
open(function() the(function() return door end) end) open(function() return the(function() return door end) end)
open(function() open(function()
the(door) the(door)
local hello = function() my(func) end local hello = function() return my(func) end
end) end)
local h = function() return hi end local h = function() return hi end
eat(function() end, world) eat(function() end, world)
@ -40,21 +40,21 @@ return(5 + function() return 4 + 2 end)
return(5 + (function() return 4 end) + 2) return(5 + (function() return 4 end) + 2)
print(5 + function() print(5 + function()
_ = 34 _ = 34
good(nads) return good(nads)
end) end)
something('else', "ya") something('else', "ya")
something('else') something('else')
something("else") something("else")
here(we)("go")[12123] here(we)("go")[12123]
local something = { test = 12323, what = function() print("hello world") end } local something = { test = 12323, what = function() return print("hello world") end }
print(something.test) print(something.test)
local frick = { hello = "world" } local frick = { hello = "world" }
local argon = { num = 100, world = function(self) local argon = { num = 100, world = function(self)
print(self.num) print(self.num)
return({ something = function() print("hi from something") end }) return({ something = function() return print("hi from something") end })
end, somethin = function(self,str) end, somethin = function(self,str)
print("string is", str) print("string is", str)
return({ world = function(a,b) print("sum", a + b) end }) return({ world = function(a,b) return print("sum", a + b) end })
end } end }
something.what() something.what()
argon:world().something() argon:world().something()
@ -82,7 +82,7 @@ else
_ = whack _ = whack
end end
local arg = { ... } local arg = { ... }
x = function(...) dump({ ... }) end x = function(...) return dump({ ... }) end
x = not true x = not true
local y = not (5 + 5) local y = not (5 + 5)
y = #"hello" y = #"hello"

View File

@ -3,7 +3,7 @@ local backpack = {
yeah = 200, yeah = 200,
they = function() they = function()
print("hello") print("hello")
yor_feet("small") return yor_feet("small")
end, end,
pretty = hair, pretty = hair,
gold = hmm gold = hmm