1 local ipairs
,pairs
=ipairs
,pairs
2 module("yu",package
.seeall
)
5 if type(m
)=='string' then
9 for i
,s
in ipairs(m
) do
10 if i
>1 then src
=src
..'/' end
18 s
=string.gsub(s
,'\\(%d%d?%d?)',string.char
)
19 s
=string.gsub(s
,'\\.',{
39 function stackmt
:push(d
)
45 function stackmt
:pop()
53 function stackmt
:peek(k
)
65 function findLine(lineInfo
,pos
)
69 for l
,off
in ipairs(lineInfo
) do
70 if pos
>=off0
and pos
<off
then
79 function getTokenPosString(token
,currentModule
)
80 local pos
=token
.p0
or 0
81 local m
=token
.module
or currentModule
82 local line
,lpos
=findLine(m
.lineInfo
,pos
)
83 return "@"..m
.file
.."<"..line
..":"..lpos
..">"
86 function compileErr(msg
,token
,currentModule
)
87 local errmsg
="error"..(token
and getTokenPosString(token
,currentModule
) or "")..":"..msg
88 print('----------COMPILE ERROR:')
90 return error('compile error')
95 function is(i
,a
,b
,...)
96 if i
==a
then return true end
97 if b
then return is(i
,b
,...) end
101 ---------------------Token types
103 return is(c
.tag,'number','boolean','string','nil')
108 return is(tag,"vardecl" ,"classdecl" ,"funcdecl" ,"enumdecl" ,"methoddecl"
109 ,"var","externfunc","externclass")
112 function isGlobalDecl(b
)
114 if tag=="vardecl" then
116 return vtype
=="global" or vtype
=="const"
118 return is(tag, "classdecl" ,"funcdecl" ,"enumdecl" ,"methoddecl")
122 function isMemberDecl(d
)
124 return ((tag=='vardecl' or tag=='var') and d
.vtype
=='field') or tag=='methoddecl'
127 function isTypeDecl(t
)
128 return is(t
.tag,'classdecl','enumdecl','functype')
131 local function findExternModule(src
,dst
,checked
)
132 if src
==dst
then return true end
134 for path
,m
in pairs(src
.externModules
) do
135 if not checked
[m
] and findExternModule(m
,dst
,checked
) then
142 local _visibleCache
={}
143 function isModuleVisible(src
,dst
)
144 local vis
=src
.visibleMods
145 if not vis
then vis
={} src
.visibleMods
=vis
end
147 if v
~=nil then return v
end
148 v
=findExternModule(src
,dst
,{})
153 local function getConstNode(n
)
155 if is(tag,'string','nil','number') then return n
end
156 if tag=='var' and n
.vtype
=='const' then return getConstNode(n
.value
) end
157 if is(tag,'varacc','member') then
159 if decl
then return getConstNode(decl
) end
161 if tag=='enumitem' then
162 return getConstNode(n
.value
)
168 local function getConst(n
)
170 if tag=='string' then return string.format('%q',yu
.unescape(n
.v
)) end
171 if tag=='nil' then return 'nil' end
172 if tag=='number' or tag=='boolean' then return n
.v
end
173 error('non const:'..tag)
176 local function getDeclName(d
)
180 if vtype
=='local' then return d
.name
end
181 if vtype
=='global' then return d
.fullname
end
182 if vtype
=='const' then return getConst(d
.value
) end
183 if vtype
=='field' then
187 elseif tag=='funcdecl' then
189 elseif tag=='arg' then
198 {'>','<','>=','<=','==','~='},
227 function getOpPrecedence(op
)
228 for i
,s
in ipairs(opPrecedence
) do
229 for j
,o
in ipairs(s
) do
230 if op
==o
then return i
end
233 return error("unknown op:"..op
)
236 function getOpClass(op
)
240 _M
.getConstNode
=getConstNode
242 _M
.getDeclName
=getDeclName