From 01f2bba2f4a0125d066e50e802c474a40bd37a32 Mon Sep 17 00:00:00 2001 From: Sei Lisa Date: Tue, 3 Mar 2015 00:49:14 +0100 Subject: [PATCH] Make *-2 and *2 only work for local variables. Needed an adition to the parser. --- lslopt/lslfoldconst.py | 22 +++++++++------------- lslopt/lslparse.py | 2 +- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lslopt/lslfoldconst.py b/lslopt/lslfoldconst.py index 8b6a719..ad0f8a6 100644 --- a/lslopt/lslfoldconst.py +++ b/lslopt/lslfoldconst.py @@ -540,8 +540,8 @@ class foldconst(object): # expr * 1 -> expr # expr * 0 -> 0 if side-effect free # expr * -1 -> -expr - # ident * 2 -> ident+ident - # ident * -2 -> -(ident + ident) (this can turn out to be counter-productive if ident is not a local) + # ident * 2 -> ident + ident (no gain except if ident is local) + # ident * -2 -> -(ident + ident) (this is counter-productive if ident is not local) # expr/1 -> expr # expr/-1 -> -expr if nt == '*' and child[b]['t'] in ('float', 'integer') \ @@ -561,17 +561,13 @@ class foldconst(object): node['SEF'] = True return # only -2, 2 remain - if child[a]['nt'] == 'IDENT': - # FIXME: The -2 case is counter-productive if the var is not local. - # We don't have info on whether a variable is local yet. - # Or maybe we do; got to check. Disabled for now. - if val != -2: - child[b] = child[a].copy() - node['nt'] = '+' - if val == -2: - parent[index] = {'nt':'NEG', 't':node['t'], 'ch':[node]} - if 'SEF' in node: - parent[index]['SEF'] = True + if child[a]['nt'] == 'IDENT' and 'Local' in self.symtab[child[a]['scope']][child[a]['name']]: + child[b] = child[a].copy() + node['nt'] = '+' + if val == -2: + parent[index] = {'nt':'NEG', 't':node['t'], 'ch':[node]} + if 'SEF' in node: + parent[index]['SEF'] = True return return diff --git a/lslopt/lslparse.py b/lslopt/lslparse.py index e989fca..b0535c5 100644 --- a/lslopt/lslparse.py +++ b/lslopt/lslparse.py @@ -1335,7 +1335,7 @@ class parser(object): decl['ch'] = [self.autocastcheck(self.Parse_expression(), typ)] self.expect(';') self.NextToken() - self.AddSymbol('v', self.scopeindex, name, Type=typ) + self.AddSymbol('v', self.scopeindex, name, Type=typ, Local=True) return decl # If none of the above, it must be an expression.