Skip to content

Commit 5a0c5d1

Browse files
committed
move comprehension to new module
1 parent 292337e commit 5a0c5d1

File tree

11 files changed

+109
-91
lines changed

11 files changed

+109
-91
lines changed

moonscript-dev-1.rockspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ build = {
4343
["moonscript.parse.util"] = "moonscript/parse/util.lua",
4444
["moonscript.transform"] = "moonscript/transform.lua",
4545
["moonscript.transform.accumulator"] = "moonscript/transform/accumulator.lua",
46+
["moonscript.transform.comprehension"] = "moonscript/transform/comprehension.lua",
4647
["moonscript.transform.destructure"] = "moonscript/transform/destructure.lua",
4748
["moonscript.transform.names"] = "moonscript/transform/names.lua",
4849
["moonscript.transform.statement"] = "moonscript/transform/statement.lua",
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
local is_value
2+
is_value = require("moonscript.types").is_value
3+
local reversed
4+
reversed = require("moonscript.util").reversed
5+
local construct_comprehension
6+
construct_comprehension = function(inner, clauses)
7+
local current_stms = inner
8+
for _, clause in reversed(clauses) do
9+
local t = clause[1]
10+
local _exp_0 = t
11+
if "for" == _exp_0 then
12+
local name, bounds
13+
_, name, bounds = clause[1], clause[2], clause[3]
14+
current_stms = {
15+
"for",
16+
name,
17+
bounds,
18+
current_stms
19+
}
20+
elseif "foreach" == _exp_0 then
21+
local names, iter
22+
_, names, iter = clause[1], clause[2], clause[3]
23+
current_stms = {
24+
"foreach",
25+
names,
26+
{
27+
iter
28+
},
29+
current_stms
30+
}
31+
elseif "when" == _exp_0 then
32+
local cond
33+
_, cond = clause[1], clause[2]
34+
current_stms = {
35+
"if",
36+
cond,
37+
current_stms
38+
}
39+
else
40+
current_stms = error("Unknown comprehension clause: " .. t)
41+
end
42+
current_stms = {
43+
current_stms
44+
}
45+
end
46+
return current_stms[1]
47+
end
48+
local comprehension_has_value
49+
comprehension_has_value = function(comp)
50+
return is_value(comp[2])
51+
end
52+
return {
53+
construct_comprehension = construct_comprehension,
54+
comprehension_has_value = comprehension_has_value
55+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
import is_value from require "moonscript.types"
3+
4+
-- TODO: reversed unecessary
5+
import reversed from require "moonscript.util"
6+
construct_comprehension = (inner, clauses) ->
7+
current_stms = inner
8+
for _, clause in reversed clauses
9+
t = clause[1]
10+
current_stms = switch t
11+
when "for"
12+
{_, name, bounds} = clause
13+
{"for", name, bounds, current_stms}
14+
when "foreach"
15+
{_, names, iter} = clause
16+
{"foreach", names, {iter}, current_stms}
17+
when "when"
18+
{_, cond} = clause
19+
{"if", cond, current_stms}
20+
else
21+
error "Unknown comprehension clause: "..t
22+
23+
current_stms = {current_stms}
24+
25+
current_stms[1]
26+
27+
comprehension_has_value = (comp) ->
28+
is_value comp[2]
29+
30+
{:construct_comprehension, :comprehension_has_value}

moonscript/transform/statement.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,19 @@ do
55
local _obj_0 = require("moonscript.transform.names")
66
NameProxy, LocalName = _obj_0.NameProxy, _obj_0.LocalName
77
end
8-
local Run, transform_last_stm, implicitly_return, construct_comprehension, last_stm
8+
local Run, transform_last_stm, implicitly_return, last_stm
99
do
1010
local _obj_0 = require("moonscript.transform.statements")
11-
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
11+
Run, transform_last_stm, implicitly_return, last_stm = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.implicitly_return, _obj_0.last_stm
1212
end
1313
local types = require("moonscript.types")
1414
local build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP
1515
build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP = types.build, types.ntype, types.is_value, types.smart_node, types.value_is_singular, types.is_slice, types.NOOP
1616
local insert
1717
insert = table.insert
1818
local destructure = require("moonscript.transform.destructure")
19+
local construct_comprehension
20+
construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
1921
local CONSTRUCTOR_NAME = "new"
2022
local with_continue_listener
2123
with_continue_listener = function(body)

moonscript/transform/statement.moon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import Transformer from require "moonscript.transform.transformer"
22

33
import NameProxy, LocalName from require "moonscript.transform.names"
44

5-
import Run, transform_last_stm, implicitly_return, construct_comprehension,
6-
last_stm from require "moonscript.transform.statements"
5+
import Run, transform_last_stm, implicitly_return, last_stm
6+
from require "moonscript.transform.statements"
77

88
types = require "moonscript.types"
99

@@ -13,6 +13,7 @@ import build, ntype, is_value, smart_node, value_is_singular, is_slice, NOOP
1313
import insert from table
1414

1515
destructure = require "moonscript.transform.destructure"
16+
import construct_comprehension from require "moonscript.transform.comprehension"
1617

1718
CONSTRUCTOR_NAME = "new"
1819

moonscript/transform/statements.lua

Lines changed: 4 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
local types = require("moonscript.types")
22
local ntype, mtype, is_value, NOOP
33
ntype, mtype, is_value, NOOP = types.ntype, types.mtype, types.is_value, types.NOOP
4+
local comprehension_has_value
5+
comprehension_has_value = require("moonscript.transform.comprehension").comprehension_has_value
46
local Run
57
do
68
local _class_0
@@ -92,7 +94,7 @@ implicitly_return = function(scope)
9294
return stm
9395
end
9496
else
95-
if t == "comprehension" and not types.comprehension_has_value(stm) then
97+
if t == "comprehension" and not comprehension_has_value(stm) then
9698
return stm
9799
else
98100
return {
@@ -104,56 +106,10 @@ implicitly_return = function(scope)
104106
end
105107
return fn
106108
end
107-
local reversed
108-
reversed = require("moonscript.util").reversed
109-
local construct_comprehension
110-
construct_comprehension = function(inner, clauses)
111-
local current_stms = inner
112-
for _, clause in reversed(clauses) do
113-
local t = clause[1]
114-
local _exp_0 = t
115-
if "for" == _exp_0 then
116-
local name, bounds
117-
_, name, bounds = clause[1], clause[2], clause[3]
118-
current_stms = {
119-
"for",
120-
name,
121-
bounds,
122-
current_stms
123-
}
124-
elseif "foreach" == _exp_0 then
125-
local names, iter
126-
_, names, iter = clause[1], clause[2], clause[3]
127-
current_stms = {
128-
"foreach",
129-
names,
130-
{
131-
iter
132-
},
133-
current_stms
134-
}
135-
elseif "when" == _exp_0 then
136-
local cond
137-
_, cond = clause[1], clause[2]
138-
current_stms = {
139-
"if",
140-
cond,
141-
current_stms
142-
}
143-
else
144-
current_stms = error("Unknown comprehension clause: " .. t)
145-
end
146-
current_stms = {
147-
current_stms
148-
}
149-
end
150-
return current_stms[1]
151-
end
152109
return {
153110
Run = Run,
154111
last_stm = last_stm,
155112
transform_last_stm = transform_last_stm,
156113
chain_is_stub = chain_is_stub,
157-
implicitly_return = implicitly_return,
158-
construct_comprehension = construct_comprehension
114+
implicitly_return = implicitly_return
159115
}

moonscript/transform/statements.moon

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
types = require "moonscript.types"
33
import ntype, mtype, is_value, NOOP from types
44

5+
import comprehension_has_value from require "moonscript.transform.comprehension"
6+
57
-- A Run is a special statement node that lets a function run and mutate the
68
-- state of the compiler
79
class Run
@@ -65,36 +67,12 @@ implicitly_return = (scope) ->
6567
else
6668
stm
6769
else
68-
if t == "comprehension" and not types.comprehension_has_value stm
70+
if t == "comprehension" and not comprehension_has_value stm
6971
stm
7072
else
7173
{"return", stm}
7274

7375
fn
7476

75-
-- TODO: reversed unecessary
76-
import reversed from require "moonscript.util"
77-
construct_comprehension = (inner, clauses) ->
78-
current_stms = inner
79-
for _, clause in reversed clauses
80-
t = clause[1]
81-
current_stms = switch t
82-
when "for"
83-
{_, name, bounds} = clause
84-
{"for", name, bounds, current_stms}
85-
when "foreach"
86-
{_, names, iter} = clause
87-
{"foreach", names, {iter}, current_stms}
88-
when "when"
89-
{_, cond} = clause
90-
{"if", cond, current_stms}
91-
else
92-
error "Unknown comprehension clause: "..t
93-
94-
current_stms = {current_stms}
95-
96-
current_stms[1]
97-
98-
{:Run, :last_stm, :transform_last_stm, :chain_is_stub, :implicitly_return,
99-
:construct_comprehension}
77+
{:Run, :last_stm, :transform_last_stm, :chain_is_stub, :implicitly_return }
10078

moonscript/transform/value.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ do
1414
end
1515
local lua_keywords
1616
lua_keywords = require("moonscript.data").lua_keywords
17-
local Run, transform_last_stm, implicitly_return, chain_is_stub, construct_comprehension
17+
local Run, transform_last_stm, implicitly_return, chain_is_stub
1818
do
1919
local _obj_0 = require("moonscript.transform.statements")
20-
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
20+
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
2121
end
22+
local construct_comprehension
23+
construct_comprehension = require("moonscript.transform.comprehension").construct_comprehension
2224
local insert
2325
insert = table.insert
2426
return Transformer({

moonscript/transform/value.moon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import NameProxy from require "moonscript.transform.names"
55
import Accumulator, default_accumulator from require "moonscript.transform.accumulator"
66
import lua_keywords from require "moonscript.data"
77

8-
import Run, transform_last_stm, implicitly_return, chain_is_stub,
9-
construct_comprehension from require "moonscript.transform.statements"
8+
import Run, transform_last_stm, implicitly_return, chain_is_stub from require "moonscript.transform.statements"
9+
10+
import construct_comprehension from require "moonscript.transform.comprehension"
1011

1112
import insert from table
1213

moonscript/types.lua

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@ is_value = function(stm)
5858
local transform = require("moonscript.transform")
5959
return compile.Block:is_value(stm) or transform.Value:can_transform(stm)
6060
end
61-
local comprehension_has_value
62-
comprehension_has_value = function(comp)
63-
return is_value(comp[2])
64-
end
6561
local value_is_singular
6662
value_is_singular = function(node)
6763
return type(node) ~= "table" or node[1] ~= "exp" or #node == 2
@@ -320,7 +316,6 @@ return {
320316
manual_return = manual_return,
321317
cascading = cascading,
322318
value_is_singular = value_is_singular,
323-
comprehension_has_value = comprehension_has_value,
324319
value_can_be_statement = value_can_be_statement,
325320
mtype = mtype,
326321
terminating = terminating,

0 commit comments

Comments
 (0)