forked from ceu-lang/ceu
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathast.lua
More file actions
275 lines (249 loc) · 6.55 KB
/
Copy pathast.lua
File metadata and controls
275 lines (249 loc) · 6.55 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
_AST = {
root = nil,
}
local MT = {
__index = function (t,k)
if t.tag == 'Ref' then
return t[1][k]
end
end,
}
local STACK = {}
function _AST.isNode (node)
return (getmetatable(node) == MT) and node.tag
end
function _AST.isChild (n1, n2)
return n1 == n2
or n2.__par and _AST.isChild(n1, n2.__par)
end
local _N = 0
function _AST.node (tag, ln, ...)
local me
if tag == '_Stmts' then
-- "Ct" as a special case to avoid "too many captures" (HACK_1)
tag = 'Stmts'
me = setmetatable((...), MT)
else
me = setmetatable({ ... }, MT)
end
me.n = _N
_N = _N + 1
me.ln = ln
me.tag = tag
return me
end
function _AST.copy (node, ln)
local ret = setmetatable({}, MT)
for k, v in pairs(node) do
if k == '__par' then
ret[k] = v
elseif _AST.isNode(v) then
if v.tag == 'Ref' then
ret[k] = v
else
ret[k] = _AST.copy(v, ln)
ret[k].ln = ln or ret[k].ln
end
else
ret[k] = v
end
end
return ret
end
function _AST.pred_async (me)
local tag = me.tag
return tag=='Async' or tag=='Thread'
end
function _AST.pred_par (me)
local tag = me.tag
return tag=='ParOr' or tag=='ParAnd' or tag=='ParEver'
end
function _AST.pred_true (me) return true end
function _AST.par (me, pred)
if type(pred) == 'string' then
local tag = pred
pred = function(me) return me.tag==tag end
end
if not me.__par then
return nil
elseif pred(me.__par) then
return me.__par
else
return _AST.par(me.__par, pred)
end
end
function _AST.iter (pred, inc)
if pred == nil then
pred = _AST.pred_true
elseif type(pred) == 'string' then
local tag = pred
pred = function(me) return me.tag==tag end
end
local from = (inc and 1) or #STACK
local to = (inc and #STACK) or 1
local step = (inc and 1) or -1
local i = from
return function ()
for j=i, to, step do
local stmt = STACK[j]
if pred(stmt) then
i = j+step
return stmt
end
end
end
end
function _AST.dump (me, spc)
spc = spc or 0
local ks = ''
--[[
for k, v in pairs(me) do
if type(k)~='number' then
v = string.gsub(string.sub(tostring(v),1,8),'\n','\\n')
ks = ks.. k..'='..v..','
end
end
]]
--local t=0; for _ in pairs(me.aw.t) do t=t+1 end
--ks = 'n='..(me.aw.n or '?')..',t='..t..',ever='..(me.aw.forever_ and 1 or 0)
--ks = table.concat(me.trails,'-')
--
if me.ana then
local f = function(v)
return type(v)=='table'
and (type(v[1])=='table' and v[1].id or v[1])
or tostring(v)
end
local t = {}
for k in pairs(me.ana.pre) do t[#t+1]=f(k) end
ks = '['..table.concat(t,',')..']'
local t = {}
for k in pairs(me.ana.pos) do t[#t+1]=f(k) end
ks = ks..'['..table.concat(t,',')..']'
end
--[[
]]
--
--ks = me.ns.trails..' / '..tostring(me.needs_clr)
DBG(string.rep(' ',spc)..me.tag..
' (ln='..me.ln[2]..' n='..me.n..
' d='..(me.__depth or 0)..
' p='..(me.__par and me.__par.n or '')..
') '..ks)
for i, sub in ipairs(me) do
if _AST.isNode(sub) then
_AST.dump(sub, spc+2)
else
DBG(string.rep(' ',spc+2) .. '['..tostring(sub)..']')
end
end
end
local function FF (F, str)
local f = F[str]
if type(f) == 'string' then
return FF(F, f)
end
assert(f==nil or type(f)=='function')
return f
end
local function visit_aux (me, F)
local _me = me
me.__par = STACK[#STACK]
me.__depth = (me.__par and me.__par.__depth+1) or 0
ASR(me.__depth < 0xFF, me, 'max depth of 0xFF')
local pre, mid, pos = FF(F,me.tag..'_pre'), FF(F,me.tag), FF(F,me.tag..'_pos')
local bef, aft = FF(F,me.tag..'_bef'), FF(F,me.tag..'_aft')
if F.Node_pre then
me = F.Node_pre(me) or me
if me ~= _me then
--DBG('Node_pre', me.tag, me)
return visit_aux(me, F)
end
end
if pre then
me = pre(me) or me
if me ~= _me then
--DBG('XXXX_pre', me.tag, me, _me.tag, _me)
return visit_aux(me, F)
end
end
STACK[#STACK+1] = me
for i, sub in ipairs(me) do
if _AST.isNode(sub) and sub.tag~='Ref' then
if bef then assert(bef(me, sub, i)==nil) end
me[i] = visit_aux(sub, F)
if aft then assert(aft(me, sub, i)==nil) end
end
end
if mid then
assert(mid(me) == nil, me.tag)
end
if F.Node then
assert(F.Node(me) == nil)
end
STACK[#STACK] = nil
if pos then
me = pos(me) or me
if _AST.isNode(me) then
me.__par = STACK[#STACK]
me.__depth = (me.__par and me.__par.__depth+1) or 0
end
end
if F.Node_pos then
me = F.Node_pos(me) or me
if _AST.isNode(me) then
me.__par = STACK[#STACK]
me.__depth = (me.__par and me.__par.__depth+1) or 0
end
end
return me
end
_AST.visit_aux = visit_aux
function _AST.visit (F, node)
assert(_AST)
--STACK = {}
return visit_aux(node or _AST.root, F)
end
local function i2l (p)
return _LINES.i2l[p]
end
for tag, patt in pairs(_GG) do
if string.sub(tag,1,2) ~= '__' then
_GG[tag] = m.Cc(tag) * (m.Cp()/i2l) * patt / _AST.node
end
end
local function f (ln, v1, op, v2, v3, ...)
--DBG('2',ln[2],v1,op,v2,v3,...)
local ret
if not op then
ret = v1
elseif v1=='call' or v1=='call/delay' then
-- Prim call
ASR(op.tag=='Op2_call', ln, 'invalid call')
op[1] = v1 -- change modifier
ret = op
elseif v1 then
-- Op2_*
if op == 'call' then
ret = f(ln, _AST.node('Op2_'..op,ln,op,v1,v2,v3), ...)
else
ret = f(ln, _AST.node('Op2_'..op,ln,op,v1,v2), v3, ...)
end
else
-- Op1_*
if op == 'cast' then
-- consume the type
ret = _AST.node('Op1_'..op, ln, v2, f(ln,v3,...))
else
ret = _AST.node('Op1_'..op, ln, op, f(ln,v2,v3,...))
end
end
ret.__ast_isexp = true
return ret
end
for i=1, 12 do
local tag = '__'..i
_GG[tag] = (m.Cp()/i2l) * _GG[tag] / f
end
_AST.root = m.P(_GG):match(_OPTS.source)
--DBG('oi',_AST.root)