Make *-2 and *2 only work for local variables. Needed an adition to the parser.

This commit is contained in:
Sei Lisa 2015-03-03 00:49:14 +01:00
parent d8f42a5071
commit 01f2bba2f4
2 changed files with 10 additions and 14 deletions

View file

@ -540,8 +540,8 @@ class foldconst(object):
# expr * 1 -> expr # expr * 1 -> expr
# expr * 0 -> 0 if side-effect free # expr * 0 -> 0 if side-effect free
# expr * -1 -> -expr # expr * -1 -> -expr
# ident * 2 -> ident+ident # ident * 2 -> ident + ident (no gain except if ident is local)
# ident * -2 -> -(ident + ident) (this can turn out to be counter-productive if ident is not a local) # ident * -2 -> -(ident + ident) (this is counter-productive if ident is not local)
# expr/1 -> expr # expr/1 -> expr
# expr/-1 -> -expr # expr/-1 -> -expr
if nt == '*' and child[b]['t'] in ('float', 'integer') \ if nt == '*' and child[b]['t'] in ('float', 'integer') \
@ -561,17 +561,13 @@ class foldconst(object):
node['SEF'] = True node['SEF'] = True
return return
# only -2, 2 remain # only -2, 2 remain
if child[a]['nt'] == 'IDENT': if child[a]['nt'] == 'IDENT' and 'Local' in self.symtab[child[a]['scope']][child[a]['name']]:
# FIXME: The -2 case is counter-productive if the var is not local. child[b] = child[a].copy()
# We don't have info on whether a variable is local yet. node['nt'] = '+'
# Or maybe we do; got to check. Disabled for now. if val == -2:
if val != -2: parent[index] = {'nt':'NEG', 't':node['t'], 'ch':[node]}
child[b] = child[a].copy() if 'SEF' in node:
node['nt'] = '+' parent[index]['SEF'] = True
if val == -2:
parent[index] = {'nt':'NEG', 't':node['t'], 'ch':[node]}
if 'SEF' in node:
parent[index]['SEF'] = True
return return
return return

View file

@ -1335,7 +1335,7 @@ class parser(object):
decl['ch'] = [self.autocastcheck(self.Parse_expression(), typ)] decl['ch'] = [self.autocastcheck(self.Parse_expression(), typ)]
self.expect(';') self.expect(';')
self.NextToken() self.NextToken()
self.AddSymbol('v', self.scopeindex, name, Type=typ) self.AddSymbol('v', self.scopeindex, name, Type=typ, Local=True)
return decl return decl
# If none of the above, it must be an expression. # If none of the above, it must be an expression.