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,11 +1,13 @@
a = ->
do
a = ->
with something
print .hello
print hi
print "world"
with leaf
do
with leaf
.world!
.world 1,2,3
@ -18,20 +20,23 @@ with leaf
\hi "yeah", "man"
.world = 200
zyzyzy = with something
do
zyzyzy = with something
.set_state "hello world"
x = 5 + with Something!
do
x = 5 + with Something!
\write "hello world"
x = {
do
x = {
hello: with yeah
\okay!
}
}
with foo
do
with foo
\prop"something".hello
.prop\send(one)
.prop\send one
@ -39,26 +44,55 @@ with foo
--
with a, b -- b is lost
do
with a, b -- b is lost
print .world
mod = with _M = {}
mod = with _M = {}
.Thing = "hi"
-- operate on a only
with a, b = something, pooh
-- operate on a only
with a, b = something, pooh
print .world
x = with a, b = 1, 2
x = with a, b = 1, 2
print a + b
print with a, b = 1, 2
print with a, b = 1, 2
print a + b
-- assignment lhs must be evaluated in the order they appear
p = with hello!.x, world!.y = 1, 2
-- assignment lhs must be evaluated in the order they appear
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,5 +1,6 @@
local a
a = function()
do
local a
a = function()
do
local _with_0 = something
print(_with_0.hello)
@ -7,8 +8,10 @@ a = function()
print("world")
return _with_0
end
end
end
do
do
local _with_0 = leaf
_with_0.world()
_with_0.world(1, 2, 3)
@ -17,21 +20,27 @@ do
_with_0:hi(1, 2).world(2323)
_with_0:hi("yeah", "man")
_with_0.world = 200
end
end
local zyzyzy
do
local zyzyzy
do
local _with_0 = something
_with_0.set_state("hello world")
zyzyzy = _with_0
end
end
local x = 5 + (function()
do
local x = 5 + (function()
do
local _with_0 = Something()
_with_0:write("hello world")
return _with_0
end
end)()
x = {
end)()
end
do
local x = {
hello = (function()
do
local _with_0 = yeah
@ -39,50 +48,89 @@ x = {
return _with_0
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
end
do
local _with_0 = something
local b
a, b = _with_0, pooh
print(_with_0.world)
end
do
local _with_0 = 1
local b
a, b = _with_0, 2
print(a + b)
x = _with_0
end
print((function()
do
local _with_0 = 1
local b
a, b = _with_0, 2
print(a + b)
return _with_0
end
end)())
local p
do
local mod
do
local _M = { }
_M.Thing = "hi"
mod = _M
end
do
local a, b = something, pooh
print(a.world)
end
local x
do
local a, b = 1, 2
print(a + b)
x = a
end
print((function()
do
local a, b = 1, 2
print(a + b)
return a
end
end)())
local p
do
local _with_0 = 1
hello().x, world().y = _with_0, 2
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