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")
NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName
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 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
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
local is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value
is_singular = function(body)
if #body ~= 1 then
return false
@ -323,7 +276,7 @@ Statement = Transformer({
return fn(node)
end,
root_stms = function(self, body)
return apply_to_last(body, implicitly_return(self))
return transform_last_stm(body, implicitly_return(self))
end,
["return"] = function(self, node)
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
ret_val = Value:transform_once(self, ret_val)
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",
stm
@ -565,7 +518,7 @@ Statement = Transformer({
end,
["do"] = function(self, node, ret)
if ret then
node[2] = apply_to_last(node[2], ret)
node[2] = transform_last_stm(node[2], ret)
end
return node
end,
@ -676,11 +629,11 @@ Statement = Transformer({
node = expand_elseif_assign(node)
if ret then
smart_node(node)
node['then'] = apply_to_last(node['then'], ret)
node['then'] = transform_last_stm(node['then'], ret)
for i = 4, #node do
local case = 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
return node
@ -861,7 +814,7 @@ Statement = Transformer({
body = case_exps
end
if ret then
body = apply_to_last(body, ret)
body = transform_last_stm(body, ret)
end
insert(out, body)
return out
@ -1305,7 +1258,7 @@ do
body = { }
val = single_stm
else
body = apply_to_last(body, function(n)
body = transform_last_stm(body, function(n)
if types.is_value(n) then
return build.assign_one(self.value_name, n)
else
@ -1498,7 +1451,7 @@ Value = Transformer({
end,
fndef = function(self, 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 = {
Run(function(self)
return self:listen("varargs", function() end)

View File

@ -6,37 +6,15 @@ data = require "moonscript.data"
import reversed, unpack from util
import ntype, mtype, build, smart_node, is_slice, value_is_singular from types
import insert from table
import NameProxy, LocalName from require "moonscript.transform.names"
import Run, transform_last_stm from require "moonscript.transform.statements"
destructure = require "moonscript.transform.destructure"
NOOP = {"noop"}
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_singular = (body) ->
return false if #body != 1
@ -82,6 +60,7 @@ constructor_name = "new"
with_continue_listener = (body) ->
continue_name = nil
{
Run =>
@listen "continue", ->
@ -95,7 +74,8 @@ with_continue_listener = (body) ->
Run =>
return unless continue_name
@put_name continue_name, nil
@splice (lines) -> {
@splice (lines) ->
{
{"assign", {continue_name}, {"false"}}
{"repeat", "true", {
lines
@ -173,7 +153,7 @@ Statement = Transformer {
fn node
root_stms: (body) =>
apply_to_last body, implicitly_return @
transform_last_stm body, implicitly_return @
return: (node) =>
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"
ret_val = Value\transform_once @, ret_val
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}
node[2] = ret_val
@ -323,7 +303,7 @@ Statement = Transformer {
construct_comprehension action(exp), clauses
do: (node, ret) =>
node[2] = apply_to_last node[2], ret if ret
node[2] = transform_last_stm node[2], ret if ret
node
decorated: (node) =>
@ -381,11 +361,11 @@ Statement = Transformer {
if ret
smart_node node
-- mutate all the bodies
node['then'] = apply_to_last node['then'], ret
node['then'] = transform_last_stm node['then'], ret
for i = 4, #node
case = 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
@ -519,7 +499,7 @@ Statement = Transformer {
body = case_exps
if ret
body = apply_to_last body, ret
body = transform_last_stm body, ret
insert out, body
@ -786,7 +766,7 @@ class Accumulator
body = {}
single_stm
else
body = apply_to_last body, (n) ->
body = transform_last_stm body, (n) ->
if types.is_value n
build.assign_one @value_name, n
else
@ -905,7 +885,7 @@ Value = Transformer {
fndef: (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 = {
Run => @listen "varargs", -> -- capture event
unpack node.body