Skip to content

Commit 53ef9fe

Browse files
committed
move transformer to separate class
1 parent d73c0a1 commit 53ef9fe

File tree

2 files changed

+49
-151
lines changed

2 files changed

+49
-151
lines changed

moonscript/transform.lua

Lines changed: 47 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ do
1717
local _obj_0 = require("moonscript.transform.statements")
1818
Run, transform_last_stm, last_stm, chain_is_stub = _obj_0.Run, _obj_0.transform_last_stm, _obj_0.last_stm, _obj_0.chain_is_stub
1919
end
20+
local Transformer
21+
Transformer = require("moonscript.transform.transformer").Transformer
2022
local destructure = require("moonscript.transform.destructure")
2123
local NOOP = {
2224
"noop"
2325
}
24-
local is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, Transformer, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value
26+
local is_singular, extract_declarations, expand_elseif_assign, constructor_name, with_continue_listener, construct_comprehension, Statement, Accumulator, default_accumulator, implicitly_return, Value
2527
is_singular = function(body)
2628
if #body ~= 1 then
2729
return false
@@ -170,74 +172,6 @@ with_continue_listener = function(body)
170172
end)
171173
}
172174
end
173-
do
174-
local _class_0
175-
local _base_0 = {
176-
transform_once = function(self, scope, node, ...)
177-
if self.seen_nodes[node] then
178-
return node
179-
end
180-
self.seen_nodes[node] = true
181-
local transformer = self.transformers[ntype(node)]
182-
if transformer then
183-
return transformer(scope, node, ...) or node
184-
else
185-
return node
186-
end
187-
end,
188-
transform = function(self, scope, node, ...)
189-
if self.seen_nodes[node] then
190-
return node
191-
end
192-
self.seen_nodes[node] = true
193-
while true do
194-
local transformer = self.transformers[ntype(node)]
195-
local res
196-
if transformer then
197-
res = transformer(scope, node, ...) or node
198-
else
199-
res = node
200-
end
201-
if res == node then
202-
return node
203-
end
204-
node = res
205-
end
206-
return node
207-
end,
208-
bind = function(self, scope)
209-
return function(...)
210-
return self:transform(scope, ...)
211-
end
212-
end,
213-
__call = function(self, ...)
214-
return self:transform(...)
215-
end,
216-
can_transform = function(self, node)
217-
return self.transformers[ntype(node)] ~= nil
218-
end
219-
}
220-
_base_0.__index = _base_0
221-
_class_0 = setmetatable({
222-
__init = function(self, transformers)
223-
self.transformers = transformers
224-
self.seen_nodes = setmetatable({ }, {
225-
__mode = "k"
226-
})
227-
end,
228-
__base = _base_0,
229-
__name = "Transformer"
230-
}, {
231-
__index = _base_0,
232-
__call = function(cls, ...)
233-
local _self_0 = setmetatable({}, _base_0)
234-
cls.__init(_self_0, ...)
235-
return _self_0
236-
end
237-
})
238-
_base_0.__class = _class_0
239-
Transformer = _class_0
240-
end
241175
construct_comprehension = function(inner, clauses)
242176
local current_stms = inner
243177
for _, clause in reversed(clauses) do
@@ -1135,60 +1069,60 @@ Statement = Transformer({
11351069
"__parent"
11361070
}
11371071
}
1138-
if chain then
1139-
local chain_tail = {
1140-
unpack(chain, 3)
1072+
if not (chain) then
1073+
return relative_parent
1074+
end
1075+
local chain_tail = {
1076+
unpack(chain, 3)
1077+
}
1078+
local head = chain_tail[1]
1079+
if head == nil then
1080+
return relative_parent
1081+
end
1082+
local new_chain = relative_parent
1083+
local _exp_1 = head[1]
1084+
if "call" == _exp_1 then
1085+
local calling_name = block:get("current_block")
1086+
assert(calling_name, "missing calling name")
1087+
chain_tail[1] = {
1088+
"call",
1089+
{
1090+
"self",
1091+
unpack(head[2])
1092+
}
11411093
}
1142-
local head = chain_tail[1]
1143-
if head == nil then
1144-
return relative_parent
1094+
if ntype(calling_name) == "key_literal" then
1095+
insert(new_chain, {
1096+
"dot",
1097+
calling_name[2]
1098+
})
1099+
else
1100+
insert(new_chain, {
1101+
"index",
1102+
calling_name
1103+
})
11451104
end
1146-
local new_chain = relative_parent
1147-
local _exp_1 = head[1]
1148-
if "call" == _exp_1 then
1149-
local calling_name = block:get("current_block")
1105+
elseif "colon" == _exp_1 then
1106+
local call = chain_tail[2]
1107+
if call and call[1] == "call" then
11501108
chain_tail[1] = {
1109+
"dot",
1110+
head[2]
1111+
}
1112+
chain_tail[2] = {
11511113
"call",
11521114
{
11531115
"self",
1154-
unpack(head[2])
1116+
unpack(call[2])
11551117
}
11561118
}
1157-
if ntype(calling_name) == "key_literal" then
1158-
insert(new_chain, {
1159-
"dot",
1160-
calling_name[2]
1161-
})
1162-
else
1163-
insert(new_chain, {
1164-
"index",
1165-
calling_name
1166-
})
1167-
end
1168-
elseif "colon" == _exp_1 then
1169-
local call = chain_tail[2]
1170-
if call and call[1] == "call" then
1171-
chain_tail[1] = {
1172-
"dot",
1173-
head[2]
1174-
}
1175-
chain_tail[2] = {
1176-
"call",
1177-
{
1178-
"self",
1179-
unpack(call[2])
1180-
}
1181-
}
1182-
end
11831119
end
1184-
for _index_0 = 1, #chain_tail do
1185-
local item = chain_tail[_index_0]
1186-
insert(new_chain, item)
1187-
end
1188-
return new_chain
1189-
else
1190-
return relative_parent
11911120
end
1121+
for _index_0 = 1, #chain_tail do
1122+
local item = chain_tail[_index_0]
1123+
insert(new_chain, item)
1124+
end
1125+
return new_chain
11921126
end)
11931127
end),
11941128
{

moonscript/transform.moon

Lines changed: 2 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import insert from table
1010
import NameProxy, LocalName from require "moonscript.transform.names"
1111
import Run, transform_last_stm, last_stm, chain_is_stub from require "moonscript.transform.statements"
1212

13+
import Transformer from require "moonscript.transform.transformer"
14+
1315
destructure = require "moonscript.transform.destructure"
1416
NOOP = {"noop"}
1517

@@ -93,44 +95,6 @@ with_continue_listener = (body) ->
9395
}
9496

9597

96-
class Transformer
97-
new: (@transformers) =>
98-
@seen_nodes = setmetatable {}, __mode: "k"
99-
100-
transform_once: (scope, node, ...) =>
101-
return node if @seen_nodes[node]
102-
@seen_nodes[node] = true
103-
104-
transformer = @transformers[ntype node]
105-
if transformer
106-
transformer(scope, node, ...) or node
107-
else
108-
node
109-
110-
transform: (scope, node, ...) =>
111-
return node if @seen_nodes[node]
112-
113-
@seen_nodes[node] = true
114-
while true
115-
transformer = @transformers[ntype node]
116-
res = if transformer
117-
transformer(scope, node, ...) or node
118-
else
119-
node
120-
121-
return node if res == node
122-
node = res
123-
124-
node
125-
126-
bind: (scope) =>
127-
(...) -> @transform scope, ...
128-
129-
__call: (...) => @transform ...
130-
131-
can_transform: (node) =>
132-
@transformers[ntype node] != nil
133-
13498
construct_comprehension = (inner, clauses) ->
13599
current_stms = inner
136100
for _, clause in reversed clauses

0 commit comments

Comments
 (0)