Skip to content

Commit 952987f

Browse files
committed
refactor transformer names
1 parent 9813071 commit 952987f

File tree

6 files changed

+34
-18
lines changed

6 files changed

+34
-18
lines changed

moonscript/compile.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ Block_ = (function()
318318
return self:value(node)
319319
end,
320320
value = function(self, node, ...)
321-
node = transform.value(node)
321+
node = transform.Value(node)
322322
local action
323323
if type(node) ~= "table" then
324324
action = "raw_value"
@@ -354,7 +354,7 @@ Block_ = (function()
354354
if not node then
355355
return
356356
end
357-
node = transform.stm(node)
357+
node = transform.Statement(node)
358358
local fn = line_compile[ntype(node)]
359359
if not fn then
360360
if has_value(node) then

moonscript/compile.moon

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ class Block_
226226
-- line wise compile functions
227227
name: (node) => @value node
228228
value: (node, ...) =>
229-
node = transform.value node
229+
node = transform.Value node
230230
action = if type(node) != "table"
231231
"raw_value"
232232
else
@@ -244,7 +244,7 @@ class Block_
244244

245245
stm: (node, ...) =>
246246
return if not node -- slip blank statements
247-
node = transform.stm node
247+
node = transform.Statement node
248248
fn = line_compile[ntype(node)]
249249
if not fn
250250
-- coerce value into statement

moonscript/transform.lua

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ local ntype, build, smart_node, is_slice = types.ntype, types.build, types.smart
77
local insert = table.insert
88
local is_value
99
is_value = function(stm)
10-
return moonscript.compile.Block:is_value(stm) or value.can_transform(stm)
10+
return moonscript.compile.Block:is_value(stm) or Value.can_transform(stm)
1111
end
1212
NameProxy = (function()
1313
local _parent_0 = nil
@@ -186,7 +186,7 @@ Transformer = function(transformers)
186186
end
187187
})
188188
end
189-
stm = Transformer({
189+
Statement = Transformer({
190190
comprehension = function(node, action)
191191
local _, exp, clauses = unpack(node)
192192
action = action or function(exp)
@@ -223,6 +223,10 @@ stm = Transformer({
223223
end
224224
return current_stms[1]
225225
end,
226+
["if"] = function(node, ret)
227+
print("node:", node, "ret:", ret)
228+
return node
229+
end,
226230
foreach = function(node)
227231
smart_node(node)
228232
if ntype(node.iter) == "unpack" then
@@ -588,13 +592,13 @@ local default_accumulator
588592
default_accumulator = function(node)
589593
return Accumulator():convert(node)
590594
end
591-
value = Transformer({
595+
Value = Transformer({
592596
["for"] = default_accumulator,
593597
["while"] = default_accumulator,
594598
foreach = default_accumulator,
595599
comprehension = function(node)
596600
local a = Accumulator()
597-
node = stm(node, function(exp)
601+
node = Statement(node, function(exp)
598602
return a:mutate_body({
599603
exp
600604
}, false)

moonscript/transform.moon

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import reversed from util
99
import ntype, build, smart_node, is_slice from types
1010
import insert from table
1111

12-
export stm, value, NameProxy, Run
12+
export Statement, Value, NameProxy, Run
1313

1414
-- TODO refactor
1515
is_value = (stm) ->
16-
moonscript.compile.Block\is_value(stm) or value.can_transform stm
16+
moonscript.compile.Block\is_value(stm) or Value.can_transform stm
1717

1818
class NameProxy
1919
new: (@prefix) =>
@@ -57,7 +57,7 @@ class Run
5757

5858
-- transform the last stm is a list of stms
5959
-- will puke on group
60-
apply_to_last = (stms, fn using nil) ->
60+
apply_to_last = (stms, fn) ->
6161
-- find last (real) exp
6262
last_exp_id = 0
6363
for i = #stms, 1, -1
@@ -105,7 +105,7 @@ Transformer = (transformers) ->
105105
__call: (...) => self.transform ...
106106
}
107107

108-
stm = Transformer {
108+
Statement = Transformer {
109109
comprehension: (node, action) ->
110110
_, exp, clauses = unpack node
111111

@@ -126,6 +126,11 @@ stm = Transformer {
126126

127127
current_stms[1]
128128

129+
-- handle cascading return decorator
130+
if: (node, ret) ->
131+
print "node:", node, "ret:", ret
132+
node
133+
129134
foreach: (node) ->
130135
smart_node node
131136
if ntype(node.iter) == "unpack"
@@ -166,7 +171,7 @@ stm = Transformer {
166171
}
167172
}
168173

169-
class: (node using nil) ->
174+
class: (node) ->
170175
_, name, parent_val, tbl = unpack node
171176

172177
constructor = nil
@@ -328,14 +333,14 @@ class Accumulator
328333
default_accumulator = (node) ->
329334
Accumulator!\convert node
330335

331-
value = Transformer {
336+
Value = Transformer {
332337
for: default_accumulator
333338
while: default_accumulator
334339
foreach: default_accumulator
335340

336341
comprehension: (node) ->
337342
a = Accumulator!
338-
node = stm node, (exp) ->
343+
node = Statement node, (exp) ->
339344
a\mutate_body {exp}, false
340345
a\wrap node
341346

@@ -344,9 +349,9 @@ value = Transformer {
344349

345350
node.body = apply_to_last node.body, (stm) ->
346351
t = ntype stm
347-
-- TODO okay this needs a refactor
348352
if types.manual_return[t] or not is_value stm
349353
stm
354+
-- elseif types.cascading[t]
350355
else
351356
{"return", stm}
352357

moonscript/types.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ manual_return = data.Set({
88
"while",
99
"return"
1010
})
11+
cascading = data.Set({
12+
"if",
13+
"with"
14+
})
1115
ntype = function(node)
1216
if type(node) ~= "table" then
1317
return "value"

moonscript/types.moon

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ util = require "moonscript.util"
33
data = require "moonscript.data"
44

55
export ntype, smart_node, build
6-
export is_slice, manual_return
6+
export is_slice, manual_return, cascading
77

88
import insert from table
99

10+
-- implicit return does not work on these statements
1011
manual_return = data.Set{"foreach", "for", "while", "return"}
1112

13+
-- assigns and returns are bubbled into their bodies
14+
cascading = data.Set{ "if", "with" }
15+
1216
-- type of node as string
1317
ntype = (node) ->
1418
if type(node) != "table"
1519
"value"
1620
else
1721
node[1]
1822

19-
2023
is_slice = (node) ->
2124
ntype(node) == "chain" and ntype(node[#node]) == "slice"
2225

0 commit comments

Comments
 (0)