-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathtree.moon
More file actions
71 lines (54 loc) · 1.68 KB
/
Copy pathtree.moon
File metadata and controls
71 lines (54 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
-- Transform helpers for the grammar's Cfn callbacks. The callbacks
-- themselves live as code strings in grammar.moon and require this module
-- at parser load.
--
-- format_assign raises error({node, msg}) for invalid assignment targets;
-- it propagates out of parse() and is formatted by moonscript/parse.moon.
unpack = unpack or table.unpack
-- never called with nil: that would report "value"
ntype = (node) ->
if type(node) == "table"
return node[1]
"value"
chain_assignable = {index: true, dot: true, slice: true}
is_assignable = (node) ->
return false if node == "..."
switch ntype node
when "ref", "self", "value", "self_class", "table"
true
when "chain"
chain_assignable[ntype node[#node]]
else
false
flatten_or_mark = (name) ->
(tbl) ->
return tbl[1] if #tbl == 1
table.insert tbl, 1, name
tbl
flatten_explist = flatten_or_mark "explist"
format_assign = (lhs_exps, assign) ->
unless assign
return flatten_explist lhs_exps
for assign_exp in *lhs_exps
unless is_assignable assign_exp
error {assign_exp, "left hand expression is not assignable"}
t = ntype assign
switch t
when "assign"
{"assign", lhs_exps, unpack assign, 2}
when "update"
{"update", lhs_exps[1], unpack assign, 2}
else
error "unknown assign expression: #{t}"
format_single_assign = (lhs, assign) ->
if assign
return format_assign {lhs}, assign
lhs
join_chain = (callee, args) ->
return callee if #args == 0
args = {"call", args}
if ntype(callee) == "chain"
table.insert callee, args
return callee
{"chain", callee, args}
{ :ntype, :is_assignable, :format_assign, :format_single_assign, :join_chain }