-
Notifications
You must be signed in to change notification settings - Fork 197
Expand file tree
/
Copy pathtypes.lua
More file actions
226 lines (226 loc) · 3.5 KB
/
Copy pathtypes.lua
File metadata and controls
226 lines (226 loc) · 3.5 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
module("moonscript.types", package.seeall)
local util = require("moonscript.util")
local data = require("moonscript.data")
local insert = table.insert
manual_return = data.Set({
"foreach",
"for",
"while",
"return"
})
cascading = data.Set({
"if",
"with"
})
ntype = function(node)
if type(node) ~= "table" then
return "value"
else
return node[1]
end
end
is_slice = function(node)
return ntype(node) == "chain" and ntype(node[#node]) == "slice"
end
local t = { }
local node_types = {
class = {
{
"name",
"Tmp"
},
{
"body",
t
}
},
fndef = {
{
"args",
t
},
{
"whitelist",
t
},
{
"arrow",
"slim"
},
{
"body",
t
}
},
foreach = {
{
"names",
t
},
{
"iter"
},
{
"body",
{ }
}
},
["for"] = {
{
"name"
},
{
"bounds",
t
},
{
"body",
t
}
},
assign = {
{
"names",
t
},
{
"values",
t
}
},
declare = {
{
"names",
t
}
},
["if"] = {
{
"cond",
t
},
{
"then",
t
}
}
}
local build_table
build_table = function()
local key_table = { }
for name, args in pairs(node_types) do
local index = { }
for i, tuple in ipairs(args) do
local name = tuple[1]
index[name] = i + 1
end
key_table[name] = index
end
return key_table
end
local key_table = build_table()
local make_builder
make_builder = function(name)
local spec = node_types[name]
if not spec then
error("don't know how to build node: " .. name)
end
return function(props)
if props == nil then
props = { }
end
local node = {
name
}
for i, arg in ipairs(spec) do
local key, default_value = unpack(arg)
local val
if props[key] then
val = props[key]
else
val = default_value
end
if val == t then
val = { }
end
node[i + 1] = val
end
return node
end
end
build = nil
build = setmetatable({
group = function(body)
return {
"group",
body
}
end,
["do"] = function(body)
return {
"do",
body
}
end,
assign_one = function(name, value)
return build.assign({
names = {
name
},
values = {
value
}
})
end,
table = function(tbl)
return {
"table",
tbl
}
end,
block_exp = function(body)
return {
"block_exp",
body
}
end,
chain = function(parts)
local base = parts.base or error("expecting base property for chain")
local node = {
"chain",
base
}
local _list_0 = parts
for _index_0 = 1, #_list_0 do
local part = _list_0[_index_0]
insert(node, part)
end
return node
end
}, {
__index = function(self, name)
self[name] = make_builder(name)
return rawget(self, name)
end
})
smart_node = function(node)
local index = key_table[ntype(node)]
if not index then
return node
end
return setmetatable(node, {
__index = function(node, key)
if index[key] then
return rawget(node, index[key])
elseif type(key) == "string" then
return error("unknown key: `" .. key .. "` on node type: `" .. ntype(node) .. "`")
end
end,
__newindex = function(node, key, value)
if index[key] then
key = index[key]
end
return rawset(node, key, value)
end
})
end