-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathtree.lua
More file actions
105 lines (105 loc) · 2.08 KB
/
Copy pathtree.lua
File metadata and controls
105 lines (105 loc) · 2.08 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
local unpack = unpack or table.unpack
local ntype
ntype = function(node)
if type(node) == "table" then
return node[1]
end
return "value"
end
local chain_assignable = {
index = true,
dot = true,
slice = true
}
local is_assignable
is_assignable = function(node)
if node == "..." then
return false
end
local _exp_0 = ntype(node)
if "ref" == _exp_0 or "self" == _exp_0 or "value" == _exp_0 or "self_class" == _exp_0 or "table" == _exp_0 then
return true
elseif "chain" == _exp_0 then
return chain_assignable[ntype(node[#node])]
else
return false
end
end
local flatten_or_mark
flatten_or_mark = function(name)
return function(tbl)
if #tbl == 1 then
return tbl[1]
end
table.insert(tbl, 1, name)
return tbl
end
end
local flatten_explist = flatten_or_mark("explist")
local format_assign
format_assign = function(lhs_exps, assign)
if not (assign) then
return flatten_explist(lhs_exps)
end
for _index_0 = 1, #lhs_exps do
local assign_exp = lhs_exps[_index_0]
if not (is_assignable(assign_exp)) then
error({
assign_exp,
"left hand expression is not assignable"
})
end
end
local t = ntype(assign)
local _exp_0 = t
if "assign" == _exp_0 then
return {
"assign",
lhs_exps,
unpack(assign, 2)
}
elseif "update" == _exp_0 then
return {
"update",
lhs_exps[1],
unpack(assign, 2)
}
else
return error("unknown assign expression: " .. tostring(t))
end
end
local format_single_assign
format_single_assign = function(lhs, assign)
if assign then
return format_assign({
lhs
}, assign)
end
return lhs
end
local join_chain
join_chain = function(callee, args)
if #args == 0 then
return callee
end
args = {
"call",
args
}
if ntype(callee) == "chain" then
table.insert(callee, args)
return callee
end
return {
"chain",
callee,
args
}
end
return {
ntype = ntype,
is_assignable = is_assignable,
format_assign = format_assign,
format_single_assign = format_single_assign,
join_chain = join_chain
}