with doesn't create its own reference unless necessary

This commit is contained in:
leaf corcoran 2013-06-10 16:12:26 -07:00
parent aa2d411693
commit 56112e074c
5 changed files with 245 additions and 137 deletions

View File

@ -10,6 +10,9 @@ do
NameProxy, LocalName = _table_0.NameProxy, _table_0.LocalName
end
local destructure = require("moonscript.transform.destructure")
local NOOP = {
"noop"
}
local implicitly_return
local Run
do
@ -683,12 +686,19 @@ local Statement = Transformer({
return node
end,
with = function(self, node, ret)
local _, exp, block = unpack(node)
local scope_name = NameProxy("with")
local named_assign
local exp, block = unpack(node, 2)
local copy_scope = true
local scope_name, named_assign
if ntype(exp) == "assign" then
local names, values = unpack(exp, 2)
local assign_name = names[1]
local first_name = names[1]
if ntype(first_name) == "value" then
scope_name = first_name
named_assign = exp
exp = values[1]
copy_scope = false
else
scope_name = NameProxy("with")
exp = values[1]
values[1] = scope_name
named_assign = {
@ -697,14 +707,17 @@ local Statement = Transformer({
values
}
end
elseif self:is_local(exp) then
scope_name = exp
copy_scope = false
end
scope_name = scope_name or NameProxy("with")
return build["do"]({
Run(function(self)
return self:set("scope_var", scope_name)
end),
build.assign_one(scope_name, exp),
build.group({
named_assign
}),
copy_scope and build.assign_one(scope_name, exp) or NOOP,
named_assign or NOOP,
build.group(block),
(function()
if ret then
@ -1363,9 +1376,7 @@ implicitly_return = function(scope)
return scope.transform.statement(stm, fn)
elseif types.manual_return[t] or not types.is_value(stm) then
if is_top and t == "return" and stm[2] == "" then
return {
"noop"
}
return NOOP
else
return stm
end

View File

@ -9,6 +9,7 @@ import insert from table
import NameProxy, LocalName from require "moonscript.transform.names"
destructure = require "moonscript.transform.destructure"
NOOP = {"noop"}
local implicitly_return
@ -339,21 +340,36 @@ Statement = Transformer {
node
with: (node, ret) =>
_, exp, block = unpack node
exp, block = unpack node, 2
scope_name = NameProxy "with"
copy_scope = true
local scope_name, named_assign
named_assign = if ntype(exp) == "assign"
if ntype(exp) == "assign"
names, values = unpack exp, 2
assign_name = names[1]
first_name = names[1]
if ntype(first_name) == "value"
scope_name = first_name
named_assign = exp
exp = values[1]
copy_scope = false
else
scope_name = NameProxy "with"
exp = values[1]
values[1] = scope_name
{"assign", names, values}
named_assign = {"assign", names, values}
elseif @is_local exp
scope_name = exp
copy_scope = false
scope_name or= NameProxy "with"
build.do {
Run => @set "scope_var", scope_name
build.assign_one scope_name, exp
build.group { named_assign }
copy_scope and build.assign_one(scope_name, exp) or NOOP
named_assign or NOOP
build.group block
if ret
@ -747,7 +763,7 @@ implicitly_return = (scope) ->
elseif types.manual_return[t] or not types.is_value stm
-- remove blank return statement
if is_top and t == "return" and stm[2] == ""
{"noop"}
NOOP
else
stm
else

View File

@ -1,10 +1,12 @@
do
a = ->
with something
print .hello
print hi
print "world"
do
with leaf
.world!
.world 1,2,3
@ -18,19 +20,22 @@ with leaf
\hi "yeah", "man"
.world = 200
do
zyzyzy = with something
.set_state "hello world"
do
x = 5 + with Something!
\write "hello world"
do
x = {
hello: with yeah
\okay!
}
do
with foo
\prop"something".hello
.prop\send(one)
@ -39,6 +44,7 @@ with foo
--
do
with a, b -- b is lost
print .world
@ -59,6 +65,34 @@ print with a, b = 1, 2
p = with hello!.x, world!.y = 1, 2
print a + b
--
do
x = "hello"
with x
x\upper!
do
with k = "jo"
print \upper!
do
with a,b,c = "", "", ""
print \upper!
do
a = "bunk"
with a,b,c = "", "", ""
print \upper!
do
with j
print \upper!
do
with k.j = "jo"
print \upper!

View File

@ -76,7 +76,6 @@ do
local hallo = 3434
end
do
local _with_0 = tmp
local j = 2000
end
end

View File

@ -1,3 +1,4 @@
do
local a
a = function()
do
@ -8,6 +9,8 @@ a = function()
return _with_0
end
end
end
do
do
local _with_0 = leaf
_with_0.world()
@ -18,12 +21,16 @@ do
_with_0:hi("yeah", "man")
_with_0.world = 200
end
end
do
local zyzyzy
do
local _with_0 = something
_with_0.set_state("hello world")
zyzyzy = _with_0
end
end
do
local x = 5 + (function()
do
local _with_0 = Something()
@ -31,7 +38,9 @@ local x = 5 + (function()
return _with_0
end
end)()
x = {
end
do
local x = {
hello = (function()
do
local _with_0 = yeah
@ -40,43 +49,41 @@ x = {
end
end)()
}
end
do
do
local _with_0 = foo
local _ = _with_0:prop("something").hello
_with_0.prop:send(one)
_with_0.prop:send(one)
end
end
do
do
local _with_0 = a, b
print(_with_0.world)
end
local mod
do
local _with_0 = { }
local _M = _with_0
_with_0.Thing = "hi"
mod = _with_0
local _M = { }
_M.Thing = "hi"
mod = _M
end
do
local _with_0 = something
local b
a, b = _with_0, pooh
print(_with_0.world)
local a, b = something, pooh
print(a.world)
end
local x
do
local _with_0 = 1
local b
a, b = _with_0, 2
local a, b = 1, 2
print(a + b)
x = _with_0
x = a
end
print((function()
do
local _with_0 = 1
local b
a, b = _with_0, 2
local a, b = 1, 2
print(a + b)
return _with_0
return a
end
end)())
local p
@ -86,3 +93,44 @@ do
print(a + b)
p = _with_0
end
end
do
local x = "hello"
do
x:upper()
end
end
do
do
local k = "jo"
print(k:upper())
end
end
do
do
local a, b, c = "", "", ""
print(a:upper())
end
end
do
local a = "bunk"
do
local b, c
a, b, c = "", "", ""
print(a:upper())
end
end
do
do
local _with_0 = j
print(_with_0:upper())
end
end
do
do
local _with_0 = "jo"
k.j = _with_0
print(_with_0:upper())
return _with_0
end
end