[Mumps2Py:] [38] reworked tokprepass. py so that I could fix variable names and initialization in addition to indentlevels . |
[ Thread Index |
Date Index
| More lists.mumps2py.org/discuss Archives
]
Revision: 38
Author: pgallot
Date: 2008-01-21 19:51:52 +0000 (Mon, 21 Jan 2008)
Log Message:
-----------
reworked tokprepass.py so that I could fix variable names and initialization in addition to indentlevels.
Modified Paths:
--------------
trunk/mumps2py/tokprepass.py
Modified: trunk/mumps2py/tokprepass.py
===================================================================
--- trunk/mumps2py/tokprepass.py 2008-01-21 19:48:09 UTC (rev 37)
+++ trunk/mumps2py/tokprepass.py 2008-01-21 19:51:52 UTC (rev 38)
@@ -19,31 +19,68 @@
tokprepass contains code for massaging the intermediate tokens before
translating a module into Python.
"""
+import re
from mumps_module import ModuleInfo
from tokens import *
-def fix_indent_levels(a_mumps_module):
+def fix_indent_levels(token, state):
"""fixing the indent levels for FOR,IF, ELSE commands"""
- new_indentlevel = 0
- next_indentlevel = 0
- for token in a_mumps_module.TokenList:
- if token.lineno: # starting a new line...
- new_indentlevel = token.indentlevel
- next_indentlevel = token.indentlevel
- if token.toktype in (FORCMD, IFCMD, ELSECMD):
- # the scope of these commands is the remainder of the line
- # so the remainder of the line is a nested block of code.
- # that's the theory anyway.
- next_indentlevel = new_indentlevel + 1
+ if token.lineno: # starting a new line...
+ state['new'] = token.indentlevel
+ state['next'] = token.indentlevel
+ if token.toktype in (FORCMD, IFCMD, ELSECMD):
+ # the scope of these commands is the remainder of the line
+ # so the remainder of the line is a nested block of code.
+ # that's the theory anyway.
+ state['next'] = state['new'] + 1
- token.indentlevel = new_indentlevel
- new_indentlevel = next_indentlevel
-## print "%s: current indent level: %d, next command indent level: %d" % \
-## (token.dscr(),token.indentlevel, new_indentlevel)
+ token.indentlevel = state['new']
+ state['new'] = state['next']
+## print "%s: current indent level: %d, next command indent level: %d" % \
+## (token.dscr(),token.indentlevel, new_indentlevel)
+def findall_localvars(token, state):
+ """ find all subtoken LOCALVAR instances """
+ token.extract_subtokens(state['collection'], (LOCALVAR, ))
+
def prepass(a_mumps_module):
""" Calls various functions to massage the tokens within a module."""
+ indentlevel_state = {'new':0, 'next':0}
+ localvars_state = {'collection':{} }
+ for token in a_mumps_module.TokenList:
+ fix_indent_levels(token, indentlevel_state)
+ findall_localvars(token, localvars_state)
- fix_indent_levels(a_mumps_module)
+ # there are two things to be done with variables:
+ # (1)The variable names need to have any '%' characters replaced with
+ # something suitable, wherever that variable is used.
+ # (2) If any use of a variable is with indices, then all uses of that
+ # variable need to use indices.
+
+ vars_dict = {}
+ for token in localvars_state['collection'][LOCALVAR]:
+ if vars_dict.has_key(token.varname):
+ vars_dict[token.varname].append(token)
+ else:
+ vars_dict[token.varname] = [ token, ]
+ for var in vars_dict.keys():
+ is_indexed = False
+ for token in vars_dict[var]:
+ is_indexed = is_indexed or token.__dict__.has_key('indices')
+ if is_indexed:
+ for token in vars_dict[var]:
+ if not token.__dict__.has_key('indices'):
+ token.indices = None
+
+ if re.search(r'[%]', var):
+ if len(var) == 1:
+ new_varname = "var"
+ else:
+ new_varname = var[:]
+ re.sub(r'[%]', '_', new_varname)
+ if new_varname[0] == '_':
+ new_varname = 'var' + new_varname
+ for token in vars_dict[var]:
+ token.varname = new_varname