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.parse.util"] = "moonscript/parse/util.lua",
["moonscript.transform"] = "moonscript/transform.lua", ["moonscript.transform"] = "moonscript/transform.lua",
["moonscript.transform.accumulator"] = "moonscript/transform/accumulator.lua", ["moonscript.transform.accumulator"] = "moonscript/transform/accumulator.lua",
["moonscript.transform.comprehension"] = "moonscript/transform/comprehension.lua",
["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua", ["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua",
["moonscript.transform.names"] = "moonscript/transform/names.lua", ["moonscript.transform.names"] = "moonscript/transform/names.lua",
["moonscript.transform.statement"] = "moonscript/transform/statement.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") 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, implicitly_return, construct_comprehension, last_stm local Run, transform_last_stm, implicitly_return, last_stm
do do
local _obj_0 = require("moonscript.transform.statements") 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 end
local types = require("moonscript.types") local types = require("moonscript.types")
local build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP 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 local insert
insert = table.insert insert = table.insert
local destructure = require("moonscript.transform.destructure") local destructure = require("moonscript.transform.destructure")
local construct_comprehension
construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
local CONSTRUCTOR_NAME = "new" local CONSTRUCTOR_NAME = "new"
local with_continue_listener local with_continue_listener
with_continue_listener = function(body) 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 NameProxy, LocalName from require "moonscript.transform.names"
import Run, transform_last_stm, implicitly_return, construct_comprehension, import Run, transform_last_stm, implicitly_return, last_stm
last_stm from require "moonscript.transform.statements" from require "moonscript.transform.statements"
types = require "moonscript.types" 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 import insert from table
destructure = require "moonscript.transform.destructure" destructure = require "moonscript.transform.destructure"
import construct_comprehension from require "moonscript.transform.comprehension"
CONSTRUCTOR_NAME = "new" CONSTRUCTOR_NAME = "new"

View File

@ -1,6 +1,8 @@
local types = require("moonscript.types") local types = require("moonscript.types")
local ntype, mtype, is_value, NOOP local ntype, mtype, is_value, NOOP
ntype, mtype, is_value, NOOP = types.ntype, types.mtype, types.is_value, types.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 local Run
do do
local _class_0 local _class_0
@ -92,7 +94,7 @@ implicitly_return = function(scope)
return stm return stm
end end
else else
if t == "comprehension" and not types.comprehension_has_value(stm) then if t == "comprehension" and not comprehension_has_value(stm) then
return stm return stm
else else
return { return {
@ -104,56 +106,10 @@ implicitly_return = function(scope)
end end
return fn return fn
end 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 { return {
Run = Run, Run = Run,
last_stm = last_stm, last_stm = last_stm,
transform_last_stm = transform_last_stm, transform_last_stm = transform_last_stm,
chain_is_stub = chain_is_stub, chain_is_stub = chain_is_stub,
implicitly_return = implicitly_return, implicitly_return = implicitly_return
construct_comprehension = construct_comprehension
} }

View File

@ -2,6 +2,8 @@
types = require "moonscript.types" types = require "moonscript.types"
import ntype, mtype, is_value, NOOP from 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 -- A Run is a special statement node that lets a function run and mutate the
-- state of the compiler -- state of the compiler
class Run class Run
@ -65,36 +67,12 @@ implicitly_return = (scope) ->
else else
stm stm
else else
if t == "comprehension" and not types.comprehension_has_value stm if t == "comprehension" and not comprehension_has_value stm
stm stm
else else
{"return", stm} {"return", stm}
fn fn
-- TODO: reversed unecessary {:Run, :last_stm, :transform_last_stm, :chain_is_stub, :implicitly_return }
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}

View File

@ -14,11 +14,13 @@ do
end end
local lua_keywords local lua_keywords
lua_keywords = require("moonscript.data").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 do
local _obj_0 = require("moonscript.transform.statements") 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 end
local construct_comprehension
construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
local insert local insert
insert = table.insert insert = table.insert
return Transformer({ 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 Accumulator, default_accumulator from require "moonscript.transform.accumulator"
import lua_keywords from require "moonscript.data" import lua_keywords from require "moonscript.data"
import Run, transform_last_stm, implicitly_return, chain_is_stub, import Run, transform_last_stm, implicitly_return, chain_is_stub from require "moonscript.transform.statements"
construct_comprehension from require "moonscript.transform.statements"
import construct_comprehension from require "moonscript.transform.comprehension"
import insert from table import insert from table

View File

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

View File

@ -52,9 +52,6 @@ is_value = (stm) ->
compile.Block\is_value(stm) or transform.Value\can_transform stm compile.Block\is_value(stm) or transform.Value\can_transform stm
comprehension_has_value = (comp) ->
is_value comp[2]
value_is_singular = (node) -> value_is_singular = (node) ->
type(node) != "table" or node[1] != "exp" or #node == 2 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, :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 :value_can_be_statement, :mtype, :terminating
:NOOP :NOOP
} }