move comprehension to new module

This commit is contained in:
leaf corcoran 2015-12-06 00:02:55 -08:00
parent 292337ee2b
commit 5a0c5d1e55
11 changed files with 109 additions and 91 deletions

View File

@ -43,6 +43,7 @@ build = {
["moonscript.parse.util"] = "moonscript/parse/util.lua",
["moonscript.transform"] = "moonscript/transform.lua",
["moonscript.transform.accumulator"] = "moonscript/transform/accumulator.lua",
["moonscript.transform.comprehension"] = "moonscript/transform/comprehension.lua",
["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua",
["moonscript.transform.names"] = "moonscript/transform/names.lua",
["moonscript.transform.statement"] = "moonscript/transform/statement.lua",

View File

@ -0,0 +1,55 @@
local is_value
is_value = require("moonscript.types").is_value
local reversed
reversed = require("moonscript.util").reversed
local construct_comprehension
construct_comprehension = function(inner, clauses)
local current_stms = inner
for _, clause in reversed(clauses) do
local t = clause[1]
local _exp_0 = t
if "for" == _exp_0 then
local name, bounds
_, name, bounds = clause[1], clause[2], clause[3]
current_stms = {
"for",
name,
bounds,
current_stms
}
elseif "foreach" == _exp_0 then
local names, iter
_, names, iter = clause[1], clause[2], clause[3]
current_stms = {
"foreach",
names,
{
iter
},
current_stms
}
elseif "when" == _exp_0 then
local cond
_, cond = clause[1], clause[2]
current_stms = {
"if",
cond,
current_stms
}
else
current_stms = error("Unknown comprehension clause: " .. t)
end
current_stms = {
current_stms
}
end
return current_stms[1]
end
local comprehension_has_value
comprehension_has_value = function(comp)
return is_value(comp[2])
end
return {
construct_comprehension = construct_comprehension,
comprehension_has_value = comprehension_has_value
}

View File

@ -0,0 +1,30 @@
import is_value from require "moonscript.types"
-- TODO: reversed unecessary
import reversed from require "moonscript.util"
construct_comprehension = (inner, clauses) ->
current_stms = inner
for _, clause in reversed clauses
t = clause[1]
current_stms = switch t
when "for"
{_, name, bounds} = clause
{"for", name, bounds, current_stms}
when "foreach"
{_, names, iter} = clause
{"foreach", names, {iter}, current_stms}
when "when"
{_, cond} = clause
{"if", cond, current_stms}
else
error "Unknown comprehension clause: "..t
current_stms = {current_stms}
current_stms[1]
comprehension_has_value = (comp) ->
is_value comp[2]
{:construct_comprehension, :comprehension_has_value}

View File

@ -5,10 +5,10 @@ do
local _obj_0 = require("moonscript.transform.names")
NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName
end
local Run, transform_last_stm, implicitly_return, construct_comprehension, last_stm
local Run, transform_last_stm, implicitly_return, last_stm
do
local _obj_0 = require("moonscript.transform.statements")
Run, transform_last_stm, implicitly_return, construct_comprehension, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.construct_comprehension, _obj_0.last_stm
Run, transform_last_stm, implicitly_return, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.last_stm
end
local types = require("moonscript.types")
local build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP
@ -16,6 +16,8 @@ build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP = types.bu
local insert
insert = table.insert
local destructure = require("moonscript.transform.destructure")
local construct_comprehension
construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
local CONSTRUCTOR_NAME = "new"
local with_continue_listener
with_continue_listener = function(body)

View File

@ -2,8 +2,8 @@ import Transformer from require "moonscript.transform.transformer"
import NameProxy, LocalName from require "moonscript.transform.names"
import Run, transform_last_stm, implicitly_return, construct_comprehension,
last_stm from require "moonscript.transform.statements"
import Run, transform_last_stm, implicitly_return, last_stm
from require "moonscript.transform.statements"
types = require "moonscript.types"
@ -13,6 +13,7 @@ import build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP
import insert from table
destructure = require "moonscript.transform.destructure"
import construct_comprehension from require "moonscript.transform.comprehension"
CONSTRUCTOR_NAME = "new"

View File

@ -1,6 +1,8 @@
local types = require("moonscript.types")
local ntype, mtype, is_value, NOOP
ntype, mtype, is_value, NOOP = types.ntype, types.mtype, types.is_value, types.NOOP
local comprehension_has_value
comprehension_has_value = require("moonscript.transform.comprehension").comprehension_has_value
local Run
do
local _class_0
@ -92,7 +94,7 @@ implicitly_return = function(scope)
return stm
end
else
if t == "comprehension" and not types.comprehension_has_value(stm) then
if t == "comprehension" and not comprehension_has_value(stm) then
return stm
else
return {
@ -104,56 +106,10 @@ implicitly_return = function(scope)
end
return fn
end
local reversed
reversed = require("moonscript.util").reversed
local construct_comprehension
construct_comprehension = function(inner, clauses)
local current_stms = inner
for _, clause in reversed(clauses) do
local t = clause[1]
local _exp_0 = t
if "for" == _exp_0 then
local name, bounds
_, name, bounds = clause[1], clause[2], clause[3]
current_stms = {
"for",
name,
bounds,
current_stms
}
elseif "foreach" == _exp_0 then
local names, iter
_, names, iter = clause[1], clause[2], clause[3]
current_stms = {
"foreach",
names,
{
iter
},
current_stms
}
elseif "when" == _exp_0 then
local cond
_, cond = clause[1], clause[2]
current_stms = {
"if",
cond,
current_stms
}
else
current_stms = error("Unknown comprehension clause: " .. t)
end
current_stms = {
current_stms
}
end
return current_stms[1]
end
return {
Run = Run,
last_stm = last_stm,
transform_last_stm = transform_last_stm,
chain_is_stub = chain_is_stub,
implicitly_return = implicitly_return,
construct_comprehension = construct_comprehension
implicitly_return = implicitly_return
}

View File

@ -2,6 +2,8 @@
types = require "moonscript.types"
import ntype, mtype, is_value, NOOP from types
import comprehension_has_value from require "moonscript.transform.comprehension"
-- A Run is a special statement node that lets a function run and mutate the
-- state of the compiler
class Run
@ -65,36 +67,12 @@ implicitly_return = (scope) ->
else
stm
else
if t == "comprehension" and not types.comprehension_has_value stm
if t == "comprehension" and not comprehension_has_value stm
stm
else
{"return", stm}
fn
-- TODO: reversed unecessary
import reversed from require "moonscript.util"
construct_comprehension = (inner, clauses) ->
current_stms = inner
for _, clause in reversed clauses
t = clause[1]
current_stms = switch t
when "for"
{_, name, bounds} = clause
{"for", name, bounds, current_stms}
when "foreach"
{_, names, iter} = clause
{"foreach", names, {iter}, current_stms}
when "when"
{_, cond} = clause
{"if", cond, current_stms}
else
error "Unknown comprehension clause: "..t
current_stms = {current_stms}
current_stms[1]
{:Run, :last_stm, :transform_last_stm, :chain_is_stub, :implicitly_return,
:construct_comprehension}
{:Run, :last_stm, :transform_last_stm, :chain_is_stub, :implicitly_return }

View File

@ -14,11 +14,13 @@ do
end
local lua_keywords
lua_keywords = require("moonscript.data").lua_keywords
local Run, transform_last_stm, implicitly_return, chain_is_stub, construct_comprehension
local Run, transform_last_stm, implicitly_return, chain_is_stub
do
local _obj_0 = require("moonscript.transform.statements")
Run, transform_last_stm, implicitly_return, chain_is_stub, construct_comprehension = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.chain_is_stub, _obj_0.construct_comprehension
Run, transform_last_stm, implicitly_return, chain_is_stub = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.chain_is_stub
end
local construct_comprehension
construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
local insert
insert = table.insert
return Transformer({

View File

@ -5,8 +5,9 @@ import NameProxy from require "moonscript.transform.names"
import Accumulator, default_accumulator from require "moonscript.transform.accumulator"
import lua_keywords from require "moonscript.data"
import Run, transform_last_stm, implicitly_return, chain_is_stub,
construct_comprehension from require "moonscript.transform.statements"
import Run, transform_last_stm, implicitly_return, chain_is_stub from require "moonscript.transform.statements"
import construct_comprehension from require "moonscript.transform.comprehension"
import insert from table

View File

@ -58,10 +58,6 @@ is_value = function(stm)
local transform = require("moonscript.transform")
return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
end
local comprehension_has_value
comprehension_has_value = function(comp)
return is_value(comp[2])
end
local value_is_singular
value_is_singular = function(node)
return type(node) ~= "table" or node[1] ~= "exp" or #node == 2
@ -320,7 +316,6 @@ return {
manual_return = manual_return,
cascading = cascading,
value_is_singular = value_is_singular,
comprehension_has_value = comprehension_has_value,
value_can_be_statement = value_can_be_statement,
mtype = mtype,
terminating = terminating,

View File

@ -52,9 +52,6 @@ is_value = (stm) ->
compile.Block\is_value(stm) or transform.Value\can_transform stm
comprehension_has_value = (comp) ->
is_value comp[2]
value_is_singular = (node) ->
type(node) != "table" or node[1] != "exp" or #node == 2
@ -193,7 +190,7 @@ NOOP = {"noop"}
{
:ntype, :smart_node, :build, :is_value, :is_slice, :manual_return,
:cascading, :value_is_singular, :comprehension_has_value,
:cascading, :value_is_singular,
:value_can_be_statement, :mtype, :terminating
:NOOP
}