move some stuff around

This commit is contained in:
leaf corcoran 2015-09-26 20:06:41 -07:00
parent 9dbc1d5062
commit ad7be54ff9
2 changed files with 36 additions and 103 deletions

View File

@ -12,63 +12,16 @@ do
local _obj_0 = require("moonscript.transform.names") local _obj_0 = require("moonscript.transform.names")
NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName
end end
local Run, transform_last_stm
do
local _obj_0 = require("moonscript.transform.statements")
Run, transform_last_stm = _obj_0.Run, _obj_0.transform_last_stm
end
local destructure = require("moonscript.transform.destructure") local destructure = require("moonscript.transform.destructure")
local NOOP = { local NOOP = {
"noop" "noop"
} }
local Run, apply_to_last, is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value local is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value
do
local _base_0 = {
call = function(self, state)
return self.fn(state)
end
}
_base_0.__index = _base_0
local _class_0 = setmetatable({
__init = function(self, fn)
self.fn = fn
self[1] = "run"
end,
__base = _base_0,
__name = "Run"
}, {
__index = _base_0,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
Run = _class_0
end
apply_to_last = function(stms, fn)
local last_exp_id = 0
for i = #stms, 1, -1 do
local stm = stms[i]
if stm and mtype(stm) ~= Run then
last_exp_id = i
break
end
end
return (function()
local _accum_0 = { }
local _len_0 = 1
for i, stm in ipairs(stms) do
if i == last_exp_id then
_accum_0[_len_0] = {
"transform",
stm,
fn
}
else
_accum_0[_len_0] = stm
end
_len_0 = _len_0 + 1
end
return _accum_0
end)()
end
is_singular = function(body) is_singular = function(body)
if #body ~= 1 then if #body ~= 1 then
return false return false
@ -323,7 +276,7 @@ Statement = Transformer({
return fn(node) return fn(node)
end, end,
root_stms = function(self, body) root_stms = function(self, body)
return apply_to_last(body, implicitly_return(self)) return transform_last_stm(body, implicitly_return(self))
end, end,
["return"] = function(self, node) ["return"] = function(self, node)
local ret_val = node[2] local ret_val = node[2]
@ -338,7 +291,7 @@ Statement = Transformer({
if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" then if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" then
ret_val = Value:transform_once(self, ret_val) ret_val = Value:transform_once(self, ret_val)
if ntype(ret_val) == "block_exp" then if ntype(ret_val) == "block_exp" then
return build.group(apply_to_last(ret_val[2], function(stm) return build.group(transform_last_stm(ret_val[2], function(stm)
return { return {
"return", "return",
stm stm
@ -565,7 +518,7 @@ Statement = Transformer({
end, end,
["do"] = function(self, node, ret) ["do"] = function(self, node, ret)
if ret then if ret then
node[2] = apply_to_last(node[2], ret) node[2] = transform_last_stm(node[2], ret)
end end
return node return node
end, end,
@ -676,11 +629,11 @@ Statement = Transformer({
node = expand_elseif_assign(node) node = expand_elseif_assign(node)
if ret then if ret then
smart_node(node) smart_node(node)
node['then'] = apply_to_last(node['then'], ret) node['then'] = transform_last_stm(node['then'], ret)
for i = 4, #node do for i = 4, #node do
local case = node[i] local case = node[i]
local body_idx = #node[i] local body_idx = #node[i]
case[body_idx] = apply_to_last(case[body_idx], ret) case[body_idx] = transform_last_stm(case[body_idx], ret)
end end
end end
return node return node
@ -861,7 +814,7 @@ Statement = Transformer({
body = case_exps body = case_exps
end end
if ret then if ret then
body = apply_to_last(body, ret) body = transform_last_stm(body, ret)
end end
insert(out, body) insert(out, body)
return out return out
@ -1305,7 +1258,7 @@ do
body = { } body = { }
val = single_stm val = single_stm
else else
body = apply_to_last(body, function(n) body = transform_last_stm(body, function(n)
if types.is_value(n) then if types.is_value(n) then
return build.assign_one(self.value_name, n) return build.assign_one(self.value_name, n)
else else
@ -1498,7 +1451,7 @@ Value = Transformer({
end, end,
fndef = function(self, node) fndef = function(self, node)
smart_node(node) smart_node(node)
node.body = apply_to_last(node.body, implicitly_return(self)) node.body = transform_last_stm(node.body, implicitly_return(self))
node.body = { node.body = {
Run(function(self) Run(function(self)
return self:listen("varargs", function() end) return self:listen("varargs", function() end)

View File

@ -6,37 +6,15 @@ data = require "moonscript.data"
import reversed, unpack from util import reversed, unpack from util
import ntype, mtype, build, smart_node, is_slice, value_is_singular from types import ntype, mtype, build, smart_node, is_slice, value_is_singular from types
import insert from table import insert from table
import NameProxy, LocalName from require "moonscript.transform.names" import NameProxy, LocalName from require "moonscript.transform.names"
import Run, transform_last_stm from require "moonscript.transform.statements"
destructure = require "moonscript.transform.destructure" destructure = require "moonscript.transform.destructure"
NOOP = {"noop"} NOOP = {"noop"}
local * local *
class Run
new: (@fn) =>
self[1] = "run"
call: (state) =>
self.fn state
-- transform the last stm is a list of stms
-- will puke on group
apply_to_last = (stms, fn) ->
-- find last (real) exp
last_exp_id = 0
for i = #stms, 1, -1
stm = stms[i]
if stm and mtype(stm) != Run
last_exp_id = i
break
return for i, stm in ipairs stms
if i == last_exp_id
{"transform", stm, fn}
else
stm
-- is a body a sindle expression/statement -- is a body a sindle expression/statement
is_singular = (body) -> is_singular = (body) ->
return false if #body != 1 return false if #body != 1
@ -82,6 +60,7 @@ constructor_name = "new"
with_continue_listener = (body) -> with_continue_listener = (body) ->
continue_name = nil continue_name = nil
{ {
Run => Run =>
@listen "continue", -> @listen "continue", ->
@ -95,7 +74,8 @@ with_continue_listener = (body) ->
Run => Run =>
return unless continue_name return unless continue_name
@put_name continue_name, nil @put_name continue_name, nil
@splice (lines) -> { @splice (lines) ->
{
{"assign", {continue_name}, {"false"}} {"assign", {continue_name}, {"false"}}
{"repeat", "true", { {"repeat", "true", {
lines lines
@ -173,7 +153,7 @@ Statement = Transformer {
fn node fn node
root_stms: (body) => root_stms: (body) =>
apply_to_last body, implicitly_return @ transform_last_stm body, implicitly_return @
return: (node) => return: (node) =>
ret_val = node[2] ret_val = node[2]
@ -190,7 +170,7 @@ Statement = Transformer {
if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension" if ret_val_type == "chain" or ret_val_type == "comprehension" or ret_val_type == "tblcomprehension"
ret_val = Value\transform_once @, ret_val ret_val = Value\transform_once @, ret_val
if ntype(ret_val) == "block_exp" if ntype(ret_val) == "block_exp"
return build.group apply_to_last ret_val[2], (stm)-> return build.group transform_last_stm ret_val[2], (stm)->
{"return", stm} {"return", stm}
node[2] = ret_val node[2] = ret_val
@ -323,7 +303,7 @@ Statement = Transformer {
construct_comprehension action(exp), clauses construct_comprehension action(exp), clauses
do: (node, ret) => do: (node, ret) =>
node[2] = apply_to_last node[2], ret if ret node[2] = transform_last_stm node[2], ret if ret
node node
decorated: (node) => decorated: (node) =>
@ -381,11 +361,11 @@ Statement = Transformer {
if ret if ret
smart_node node smart_node node
-- mutate all the bodies -- mutate all the bodies
node['then'] = apply_to_last node['then'], ret node['then'] = transform_last_stm node['then'], ret
for i = 4, #node for i = 4, #node
case = node[i] case = node[i]
body_idx = #node[i] body_idx = #node[i]
case[body_idx] = apply_to_last case[body_idx], ret case[body_idx] = transform_last_stm case[body_idx], ret
node node
@ -519,7 +499,7 @@ Statement = Transformer {
body = case_exps body = case_exps
if ret if ret
body = apply_to_last body, ret body = transform_last_stm body, ret
insert out, body insert out, body
@ -786,7 +766,7 @@ class Accumulator
body = {} body = {}
single_stm single_stm
else else
body = apply_to_last body, (n) -> body = transform_last_stm body, (n) ->
if types.is_value n if types.is_value n
build.assign_one @value_name, n build.assign_one @value_name, n
else else
@ -905,7 +885,7 @@ Value = Transformer {
fndef: (node) => fndef: (node) =>
smart_node node smart_node node
node.body = apply_to_last node.body, implicitly_return self node.body = transform_last_stm node.body, implicitly_return self
node.body = { node.body = {
Run => @listen "varargs", -> -- capture event Run => @listen "varargs", -> -- capture event
unpack node.body unpack node.body