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

View File

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

View File

@ -1,63 +1,97 @@
a = -> do
with something a = ->
print .hello with something
print hi print .hello
print "world" print hi
print "world"
with leaf do
.world! with leaf
.world 1,2,3 .world!
.world 1,2,3
g = .what.is.this g = .what.is.this
.hi 1,2,3 .hi 1,2,3
\hi(1,2).world 2323 \hi(1,2).world 2323
\hi "yeah", "man" \hi "yeah", "man"
.world = 200 .world = 200
zyzyzy = with something do
.set_state "hello world" zyzyzy = with something
.set_state "hello world"
do
x = 5 + with Something!
\write "hello world"
x = 5 + with Something! do
\write "hello world" x = {
hello: with yeah
\okay!
}
do
x = { with foo
hello: with yeah \prop"something".hello
\okay! .prop\send(one)
} .prop\send one
with foo
\prop"something".hello
.prop\send(one)
.prop\send one
-- --
with a, b -- b is lost do
print .world with a, b -- b is lost
print .world
mod = with _M = {} mod = with _M = {}
.Thing = "hi" .Thing = "hi"
-- operate on a only -- operate on a only
with a, b = something, pooh with a, b = something, pooh
print .world print .world
x = with a, b = 1, 2 x = with a, b = 1, 2
print a + b print a + b
print with a, b = 1, 2 print with a, b = 1, 2
print a + b print a + b
-- assignment lhs must be evaluated in the order they appear -- assignment lhs must be evaluated in the order they appear
p = with hello!.x, world!.y = 1, 2 p = with hello!.x, world!.y = 1, 2
print a + b 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 local hallo = 3434
end end
do do
local _with_0 = tmp
local j = 2000 local j = 2000
end end
end end

View File

@ -1,88 +1,136 @@
local a do
a = function() local a
a = function()
do
local _with_0 = something
print(_with_0.hello)
print(hi)
print("world")
return _with_0
end
end
end
do
do
local _with_0 = leaf
_with_0.world()
_with_0.world(1, 2, 3)
local g = _with_0.what.is.this
_with_0.hi(1, 2, 3)
_with_0:hi(1, 2).world(2323)
_with_0:hi("yeah", "man")
_with_0.world = 200
end
end
do
local zyzyzy
do do
local _with_0 = something local _with_0 = something
print(_with_0.hello) _with_0.set_state("hello world")
print(hi) zyzyzy = _with_0
print("world")
return _with_0
end end
end end
do do
local _with_0 = leaf local x = 5 + (function()
_with_0.world()
_with_0.world(1, 2, 3)
local g = _with_0.what.is.this
_with_0.hi(1, 2, 3)
_with_0:hi(1, 2).world(2323)
_with_0:hi("yeah", "man")
_with_0.world = 200
end
local zyzyzy
do
local _with_0 = something
_with_0.set_state("hello world")
zyzyzy = _with_0
end
local x = 5 + (function()
do
local _with_0 = Something()
_with_0:write("hello world")
return _with_0
end
end)()
x = {
hello = (function()
do do
local _with_0 = yeah local _with_0 = Something()
_with_0:okay() _with_0:write("hello world")
return _with_0 return _with_0
end end
end)() end)()
}
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 local x = {
print(_with_0.world) hello = (function()
end do
local mod local _with_0 = yeah
do _with_0:okay()
local _with_0 = { } return _with_0
local _M = _with_0 end
_with_0.Thing = "hi" end)()
mod = _with_0 }
end end
do do
local _with_0 = something do
local b local _with_0 = foo
a, b = _with_0, pooh local _ = _with_0:prop("something").hello
print(_with_0.world) _with_0.prop:send(one)
_with_0.prop:send(one)
end
end end
do do
local _with_0 = 1 do
local b local _with_0 = a, b
a, b = _with_0, 2 print(_with_0.world)
print(a + b) end
x = _with_0 local mod
end do
print((function() 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 do
local _with_0 = 1 local _with_0 = 1
local b hello().x, world().y = _with_0, 2
a, b = _with_0, 2
print(a + b) 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 return _with_0
end end
end)())
local p
do
local _with_0 = 1
hello().x, world().y = _with_0, 2
print(a + b)
p = _with_0
end end