mirror of
https://github.com/leafo/moonscript.git
synced 2025-01-09 00:04:22 +00:00
moved value loops to transformer
This commit is contained in:
parent
0131786390
commit
dd0511b7bf
@ -35,45 +35,6 @@ table_append = function(name, len, value)
|
||||
}
|
||||
}
|
||||
end
|
||||
local create_accumulate_wrapper
|
||||
create_accumulate_wrapper = function(block_pos)
|
||||
return function(self, node)
|
||||
do
|
||||
local _with_0 = self:block("(function()", "end)()")
|
||||
local accum_name = _with_0:init_free_var("accum", {
|
||||
"table"
|
||||
})
|
||||
local count_name = _with_0:init_free_var("len", 0)
|
||||
local value_name = _with_0:free_name("value", true)
|
||||
local inner = node[block_pos]
|
||||
inner[#inner] = {
|
||||
"assign",
|
||||
{
|
||||
value_name
|
||||
},
|
||||
{
|
||||
inner[#inner]
|
||||
}
|
||||
}
|
||||
insert(inner, {
|
||||
"if",
|
||||
{
|
||||
"exp",
|
||||
value_name,
|
||||
"~=",
|
||||
"nil"
|
||||
},
|
||||
table_append(accum_name, count_name, value_name)
|
||||
})
|
||||
_with_0:stm(node)
|
||||
_with_0:stm({
|
||||
"return",
|
||||
accum_name
|
||||
})
|
||||
return _with_0
|
||||
end
|
||||
end
|
||||
end
|
||||
value_compile = {
|
||||
exp = function(self, node)
|
||||
local _comp
|
||||
@ -169,9 +130,6 @@ value_compile = {
|
||||
return _with_0
|
||||
end
|
||||
end,
|
||||
["for"] = create_accumulate_wrapper(4),
|
||||
foreach = create_accumulate_wrapper(4),
|
||||
["while"] = create_accumulate_wrapper(3),
|
||||
chain = function(self, node)
|
||||
local callee = node[2]
|
||||
if callee == -1 then
|
||||
|
@ -18,23 +18,6 @@ table_append = (name, len, value) ->
|
||||
{"chain", name, {"index", len}} }, { value }}
|
||||
}
|
||||
|
||||
create_accumulate_wrapper = (block_pos) ->
|
||||
(node) =>
|
||||
with @block "(function()", "end)()"
|
||||
accum_name = \init_free_var "accum", {"table"}
|
||||
count_name = \init_free_var "len", 0
|
||||
value_name = \free_name "value", true
|
||||
|
||||
inner = node[block_pos]
|
||||
inner[#inner] = {"assign", {value_name}, {inner[#inner]}}
|
||||
insert inner, {
|
||||
"if", {"exp", value_name, "~=", "nil"},
|
||||
table_append accum_name, count_name, value_name
|
||||
}
|
||||
|
||||
\stm node
|
||||
\stm {"return", accum_name}
|
||||
|
||||
value_compile =
|
||||
exp: (node) =>
|
||||
_comp = (i, value) ->
|
||||
@ -87,10 +70,6 @@ value_compile =
|
||||
else
|
||||
"(function()", "end)()"
|
||||
|
||||
for: create_accumulate_wrapper 4
|
||||
foreach: create_accumulate_wrapper 4
|
||||
while: create_accumulate_wrapper 3
|
||||
|
||||
chain: (node) =>
|
||||
callee = node[2]
|
||||
|
||||
|
@ -45,6 +45,15 @@ NameProxy = (function(_parent_0)
|
||||
unpack(items)
|
||||
})
|
||||
end,
|
||||
index = function(self, key)
|
||||
return build.chain({
|
||||
base = self,
|
||||
{
|
||||
"index",
|
||||
key
|
||||
}
|
||||
})
|
||||
end,
|
||||
__tostring = function(self)
|
||||
if self.name then
|
||||
return ("name<%s>"):format(self.name)
|
||||
@ -99,12 +108,40 @@ Run = (function(_parent_0)
|
||||
_base_0.__class = _class_0
|
||||
return _class_0
|
||||
end)()
|
||||
local apply_to_last
|
||||
apply_to_last = function(stms, fn)
|
||||
local last_exp_id = 0
|
||||
for i = #stms, 1, -1 do
|
||||
local stm = stms[i]
|
||||
if stm and util.moon.type(stm) ~= Run then
|
||||
last_exp_id = i
|
||||
break
|
||||
end
|
||||
end
|
||||
return (function()
|
||||
local _accum_0 = { }
|
||||
local _len_0 = 0
|
||||
for i, stm in ipairs(stms) do
|
||||
local _value_0
|
||||
if i == last_exp_id then
|
||||
_value_0 = fn(stm)
|
||||
else
|
||||
_value_0 = stm
|
||||
end
|
||||
if _value_0 ~= nil then
|
||||
_len_0 = _len_0 + 1
|
||||
_accum_0[_len_0] = _value_0
|
||||
end
|
||||
end
|
||||
return _accum_0
|
||||
end)()
|
||||
end
|
||||
local constructor_name = "new"
|
||||
local transformer
|
||||
transformer = function(transformers)
|
||||
local Transformer
|
||||
Transformer = function(transformers)
|
||||
return function(n)
|
||||
while true do
|
||||
transformer = transformers[ntype(n)]
|
||||
local transformer = transformers[ntype(n)]
|
||||
local res
|
||||
if transformer then
|
||||
res = transformer(n) or n
|
||||
@ -118,7 +155,7 @@ transformer = function(transformers)
|
||||
end
|
||||
end
|
||||
end
|
||||
stm = transformer({
|
||||
stm = Transformer({
|
||||
class = function(node)
|
||||
local _, name, parent_val, tbl = unpack(node)
|
||||
local constructor = nil
|
||||
@ -333,7 +370,45 @@ stm = transformer({
|
||||
return value
|
||||
end
|
||||
})
|
||||
value = transformer({
|
||||
local create_accumulator
|
||||
create_accumulator = function(body_index)
|
||||
return function(node)
|
||||
local accum_name = NameProxy("accum")
|
||||
local value_name = NameProxy("value")
|
||||
local len_name = NameProxy("len")
|
||||
local body = apply_to_last(node[body_index], function(n)
|
||||
return build.assign_one(value_name, n)
|
||||
end)
|
||||
table.insert(body, build["if"]({
|
||||
cond = {
|
||||
"exp",
|
||||
value_name,
|
||||
"!=",
|
||||
"nil"
|
||||
},
|
||||
["then"] = {
|
||||
{
|
||||
"update",
|
||||
len_name,
|
||||
"+=",
|
||||
1
|
||||
},
|
||||
build.assign_one(accum_name:index(len_name), value_name)
|
||||
}
|
||||
}))
|
||||
node[body_index] = body
|
||||
return build.block_exp({
|
||||
build.assign_one(accum_name, build.table()),
|
||||
build.assign_one(len_name, 0),
|
||||
node,
|
||||
accum_name
|
||||
})
|
||||
end
|
||||
end
|
||||
value = Transformer({
|
||||
["for"] = create_accumulator(4),
|
||||
foreach = create_accumulator(4),
|
||||
["while"] = create_accumulator(3),
|
||||
chain = function(node)
|
||||
local stub = node[#node]
|
||||
if type(stub) == "table" and stub[1] == "colon_stub" then
|
||||
|
@ -32,6 +32,11 @@ class NameProxy
|
||||
unpack items
|
||||
}
|
||||
|
||||
index: (key) =>
|
||||
build.chain {
|
||||
base: self, {"index", key}
|
||||
}
|
||||
|
||||
__tostring: =>
|
||||
if @name
|
||||
("name<%s>")\format @name
|
||||
@ -45,9 +50,25 @@ class Run
|
||||
call: (state) =>
|
||||
self.fn state
|
||||
|
||||
-- transform the last stm is a list of stms
|
||||
apply_to_last = (stms, fn using nil) ->
|
||||
-- find last (real) exp
|
||||
last_exp_id = 0
|
||||
for i = #stms, 1, -1
|
||||
stm = stms[i]
|
||||
if stm and util.moon.type(stm) != Run
|
||||
last_exp_id = i
|
||||
break
|
||||
|
||||
return for i, stm in ipairs stms
|
||||
if i == last_exp_id
|
||||
fn stm
|
||||
else
|
||||
stm
|
||||
|
||||
constructor_name = "new"
|
||||
|
||||
transformer = (transformers) ->
|
||||
Transformer = (transformers) ->
|
||||
(n) ->
|
||||
while true
|
||||
transformer = transformers[ntype n]
|
||||
@ -58,7 +79,7 @@ transformer = (transformers) ->
|
||||
return n if res == n
|
||||
n = res
|
||||
|
||||
stm = transformer {
|
||||
stm = Transformer {
|
||||
class: (node using nil) ->
|
||||
_, name, parent_val, tbl = unpack node
|
||||
|
||||
@ -169,7 +190,37 @@ stm = transformer {
|
||||
value
|
||||
}
|
||||
|
||||
value = transformer {
|
||||
create_accumulator = (body_index) ->
|
||||
(node) ->
|
||||
accum_name = NameProxy "accum"
|
||||
value_name = NameProxy "value"
|
||||
len_name = NameProxy "len"
|
||||
|
||||
body = apply_to_last node[body_index], (n) ->
|
||||
build.assign_one value_name, n
|
||||
|
||||
table.insert body, build["if"] {
|
||||
cond: {"exp", value_name, "!=", "nil"}
|
||||
then: {
|
||||
{"update", len_name, "+=", 1}
|
||||
build.assign_one accum_name\index(len_name), value_name
|
||||
}
|
||||
}
|
||||
|
||||
node[body_index] = body
|
||||
|
||||
build.block_exp {
|
||||
build.assign_one accum_name, build.table!
|
||||
build.assign_one len_name, 0
|
||||
node
|
||||
accum_name
|
||||
}
|
||||
|
||||
value = Transformer {
|
||||
for: create_accumulator 4
|
||||
foreach: create_accumulator 4
|
||||
while: create_accumulator 3
|
||||
|
||||
-- pull out colon chain
|
||||
chain: (node) ->
|
||||
stub = node[#node]
|
||||
|
Loading…
Reference in New Issue
Block a user