5 local getTypeDecl
,newTypeRef
=yu
.getTypeDecl
,yu
.newTypeRef
6 local ipairs
,print=ipairs
,print
8 module("yu",package
.seeall
)
13 local isConst
,isDecl
,isGlobalDecl
=isConst
,isDecl
,isGlobalDecl
15 local function makeNamePrefix(stack
)
17 for i
,n
in ipairs(stack
) do
27 function newDeclCollector()
29 return yu
.newVisitor({
33 nameStack
=yu
.newStack(),
34 scopeStack
=yu
.newStack(),
39 pushName
=function(self
,name
)
40 self
.nameStack
:push(name
)
41 self
.currentNamePrefix
=makeNamePrefix(self
.nameStack
.stack
)
44 popName
=function(self
)
46 self
.currentNamePrefix
=makeNamePrefix(self
.nameStack
.stack
)
49 pushScope
=function(self
)
51 self
.scopeStack
:push(scope
)
52 self
.currentScope
=scope
53 self
.currentNode
.scope
=scope
57 popScope
=function(self
)
58 self
.currentScope
=self
.scopeStack
:pop()
59 return self
.currentScope
62 addEachDecl
=function(self
,list
)
63 for i
,n
in ipairs(list
) do
64 if isDecl(n
) or is(n
.tag,"public","private") then
70 addDecl
=function(self
,decl
)
71 local currentScope
=self
.currentScope
73 if isBuiltinType(decl
.name
) then
74 self
:err(decl
.name
..' is builtin type',decl
,self
.currentModule
)
79 if tag=="private" then
80 currentScope
.private
=true
82 elseif tag=="public" then
83 currentScope
.private
=false
87 if tag=="vardecl" then
88 for i
,var
in ipairs(decl
.vars
) do
99 local scope
=currentScope
100 decl
.private
=scope
.private
103 local decl0
=scope
[name
]
105 if decl0
then --duplicated?
107 if tag==tag0
and is(tag,"funcdecl","methoddecl") then
108 --TODO:function overload
109 if decl0
.private
~= decl
.private
then
110 self
:err('function overload must be both private or public',decl
)
116 local d1
=dd
.nextProto
127 "duplicated declaration:'"..decl
.name
128 .."',first defined at"
129 ..getTokenPosString(decl0
,self
.currentModule
)
130 ,decl
,self
.currentModule
)
133 scope
[decl
.name
]=decl
137 decl
.fullname
=decl
.alias
140 decl
.fullname
=decl
.name
142 local currentNamePrefix
=self
.currentNamePrefix
144 if currentNamePrefix
~='' then
145 decl
.fullname
=currentNamePrefix
..'_'..name
151 decl
.fullname
='_'..decl
.protoId
..decl
.fullname
159 -- print("..added decl:",decl.tag,decl.name,decl.fullname,decl.vtype)
168 function pre
.any(vi
,n
)
169 if vi
.currentModule
then
170 n
.module
=vi
.currentModule
174 function pre
.module(vi
,m
)
181 function post
.module(vi
,m
)
189 function pre
.block(vi
,b
,parent
)
190 -- if not is(parent.tag,'funcdecl','methoddecl','forstmt','catch','foreachstmt')
191 -- b.scope=vi:pushScope()
195 local pn
=vi
.nodeStack
:peek(1)
196 if pn
.tag=='module' then -- top block? add named imported module into scope
197 for i
,h
in ipairs(pn
.heads
) do
198 if h
.tag=='import' and h
.alias
then
199 h
.type=moduleMetaType
207 function post
.block(vi
,b
)
211 function pre
.dostmt(vi
,d
)
215 function post
.dostmt(vi
,d
)
221 function pre
.extern(vi
,e
)
222 for i
,s
in ipairs(e
.decls
) do
223 if isDecl(s
) or is(s
.tag,"public","private") then
232 local function spreadVarTypes(vars
,vardecl
)
241 -- table.foreach(var,print)
242 return yu
.compileErr("type identifier expected",var
,vardecl
.module
)
248 function pre
:vardecl(vd
)
250 if vd
.def
then --should no type id
251 for i
,var
in ipairs(vars
) do
252 if var
.type then return self
:err("unnecessary type identifier",var
.type) end
254 --todo:mulitple return
255 for i
,var
in ipairs(vars
) do
258 var
.type=newTypeRef(v
)
262 spreadVarTypes(vars
,vd
)
263 --todo:mulitple return
265 for i
,var
in ipairs(vars
) do
266 var
.value
=vd
.values
[i
]
276 function pre
.funcdecl(vi
,f
)
281 function post
.funcdecl(vi
,f
)
286 function pre
.closure(vi
,f
)
292 function post
.closure(vi
,f
)
299 function pre
.methoddecl(vi
,f
,parent
)
307 function post
.methoddecl(vi
,f
)
314 function pre
.externfunc(vi
,f
)
319 function post
.externfunc(vi
,f
)
325 function pre
.externmethod(vi
,f
)
330 function post
.externmethod(vi
,f
)
335 function pre
.functype(vi
,ft
)
338 spreadVarTypes(args
,ft
)
339 for i
,arg
in ipairs(args
) do
346 function post
.functype(vi
,ft
)
353 function pre
.catch(vi
,c
)
355 for i
,v
in ipairs(c
.vars
) do
360 function post
.catch(vi
,c
)
365 function pre
.foreachstmt(vi
,f
)
367 for i
,v
in ipairs(f
.vars
) do
372 function post
.foreachstmt(vi
,f
)
377 function pre
.forstmt(vi
,f
)
379 f
.var
.type=numberType
384 function post
.forstmt(vi
,f
)
390 function pre
.classdecl(vi
,c
)
396 function post
.classdecl(vi
,c
)
398 for i
,v
in ipairs(c
.tvars
) do
404 for i
,d
in ipairs(c
.decls
) do
413 function pre
.externclass(vi
,c
)
417 for i
,d
in ipairs(c
.decls
) do
423 function post
.externclass(vi
,c
)
430 function pre
.enumdecl(vi
,e
)
434 for i
,ei
in ipairs(e
.items
) do
440 function post
.enumdecl(vi
,c
)