Add scope field to {} nodes

Since we need to add variables, we need to know which scope to add them to. Add this information to the {} node, which is what creates a new scope.

An alternative would be to scan for any variable or label declaration within the braces and use that or create a new one if none, which is more expensive and may waste symbol tables.
This commit is contained in:
Sei Lisa 2018-12-26 19:47:22 +01:00
parent 3542824d51
commit 76f483fc11
3 changed files with 32 additions and 14 deletions

View file

@ -199,7 +199,9 @@ class foldconst(object):
if (node.nt == 'FNCALL' and 'Loc' in self.symtab[0][node.name]
and self.FnSEF(node)
):
parent[index] = nr(nt='{}', t=None, ch=[
scope = len(self.symtab)
self.symtab.append({})
parent[index] = nr(nt='{}', t=None, scope=scope, ch=[
nr(nt='EXPR', t=x.t, ch=[x]) for x in node.ch])
self.FoldTree(parent, index)
return
@ -1662,9 +1664,11 @@ class foldconst(object):
self.FoldTree(child, idx)
if not child:
# All events removed - add a dummy timer()
scope = len(self.symtab)
self.symtab.append({})
child.append(nr(nt='FNDEF', t=None, name='timer',
pscope=0, ptypes=[], pnames=[],
ch=[nr(nt='{}', t=None, ch=[])]
pscope=scope, ptypes=[], pnames=[],
ch=[nr(nt='{}', t=None, scope=scope, ch=[])]
))
return
@ -1867,7 +1871,10 @@ class foldconst(object):
# We're in the case where there are expressions. If any
# remain, they are not SEF (or they would have been
# removed earlier) so don't mark this node as SEF.
parent[index] = nr(nt='{}', t=None, ch=exprlist)
scope = len(self.symtab)
self.symtab.append({})
parent[index] = nr(nt='{}', t=None, scope=scope,
ch=exprlist)
else:
parent[index] = nr(nt=';', t=None, SEF=True)
return