[Mumps2Py:] [39] added support for indexed local variables. |
[ Thread Index |
Date Index
| More lists.mumps2py.org/discuss Archives
]
Revision: 39
Author: pgallot
Date: 2008-01-21 19:54:17 +0000 (Mon, 21 Jan 2008)
Log Message:
-----------
added support for indexed local variables.
Modified Paths:
--------------
trunk/mumps2py/tok2python.py
Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py 2008-01-21 19:51:52 UTC (rev 38)
+++ trunk/mumps2py/tok2python.py 2008-01-21 19:54:17 UTC (rev 39)
@@ -42,6 +42,8 @@
self.mumps_module = a_mumps_module
self.code = []
self.imports = {}
+ self.modvars = {}
+ self.dict_inits = []
def add_code(self, block):
"""adds another block of code to the translation"""
@@ -60,12 +62,30 @@
import_args = ""
for pymod in self.imports.keys():
import_args = import_args + pymod + ", "
- block_str.append("import %s\n" % import_args[:-2])
+ block_str = "%simport %s\n\n" % (block_str, import_args[:-2])
+
+
+ for init in self.dict_inits:
+ block_str = "%s%s = {}\n" % (block_str, init)
- self.code.insert(0, block_str)
+ self.code.insert(0, block_str + "\n")
+
+ def initialized(self, varname):
+ """ tells if a variable in the module has been initialized"""
+ return self.modvars.has_key(varname)
+
+ def initialize(self, varname):
+ """ called to indicate a variable has been initialized"""
+ self.modvars[varname] = True
+
+ def add_initialization(self, varname):
+ """ called when a variable must be initialized at the top"""
+ self.dict_inits.append(varname)
+ self.modvars[varname] = True
def tab(indentlevel):
+ """puts out 4 spaces for every indentlevel."""
tab_str = "%*c" % (indentlevel*4, '^')
return tab_str[:-1]
@@ -176,10 +196,17 @@
def translate_local_var(translation, token):
""" translates a local variable into a Python variable"""
- if not token.__dict__.has_key('indices'):
+ if not token.__dict__.has_key('indices') or \
+ not translation.initialized(token.varname):
return token.varname
else:
- raise TranslationError(translation, token, "idiom not implemented")
+ var_str = "%s[(" % token.varname
+ for indexval in token.indices[:-1]:
+ var_str = "%s%s, " % \
+ (var_str, translate_expr(translation, indexval))
+ var_str = "%s%s)]" % (var_str, translate_expr(translation,
+ token.indices[-1]))
+ return var_str
def translate_expr_list(translation, token):
""" translates a sequence of literals, variables, operators, etc."""
@@ -280,7 +307,6 @@
def translate_for(translation, token):
"""translate the Mumps For Command to Python"""
- #TODO: completely redo to use a MumpsCL iterator...
if len(token.for_loops) == 0: # then we've got an infinite-loop.
return "%swhile 1:\n" % tab(token.indentlevel)
elif len(token.for_loops) == 1 and token.for_loops[0].has_key('TermVal'):
@@ -332,22 +358,62 @@
def translate_set(translation, token):
""" translate the Mumps Set command to Python"""
- indentlevel, set_str = post_condition(translation, token,
- token.indentlevel)
+
+ def initialize_indexedvar( translation, vartoken, valtoken):
+ """ initializes an indexed variable """
+ set_args = "("
+ for indexval in vartoken.indices[:-1]:
+ set_args = "%s%s, " % (set_args, translate_expr(translation,
+ indexval))
+ set_args = "%s%s)" % (set_args, translate_expr(translation,
+ vartoken.indices[-1]))
+
+ set_str = "%s = {%s:%s}" % (translate_expr(translation, vartoken),
+ set_args,
+ translate_expr(translation, valtoken))
+ translation.initialize(vartoken.varname)
+ return set_str
+
+ indentlevel, set_str = post_condition(translation, token, token.indentlevel)
+
for item in token.var_set:
if item.has_key("indirection"):
raise TranslationError(translation, token, "idiom not implemented")
elif item.has_key("multiset"):
for varname in item["var_names"]:
+ if token.indentlevel == 0 and \
+ varname.__dict__.has_key('indices') and \
+ not translation.initialized(varname.varname):
+ set_str = "%s%s%s\n" % \
+ (set_str, tab(indentlevel),
+ initialize_indexedvar(translation, varname, \
+ item["val"]))
+ else:
+ if varname.__dict__.has_key('indices') and \
+ not translation.initialized(varname.varname):
+ translation.add_initialization(varname.varname)
+
+ set_str = "%s%s%s = %s\n" % \
+ (set_str, tab(indentlevel),
+ translate_expr(translation, varname),
+ translate_expr(translation, item["val"]))
+ else:
+ if token.indentlevel == 0 and \
+ item['varname'].__dict__.has_key('indices') and \
+ not translation.initialized(item['varname'].varname):
+ set_str = "%s%s%s\n" % \
+ (set_str, tab(indentlevel),
+ initialize_indexedvar(translation,
+ item['varname'],
+ item["val"]))
+ else:
+ if item['varname'].__dict__.has_key('indices') and \
+ not translation.initialized(item['varname'].varname):
+ translation.add_initialization(item['varname'].varname)
set_str = "%s%s%s = %s\n" % \
( set_str, tab(indentlevel),
- translate_expr(translation, varname),
+ translate_expr(translation, item["varname"]),
translate_expr(translation, item["val"]))
- else:
- set_str = "%s%s%s = %s\n" % \
- ( set_str, tab(indentlevel),
- translate_expr(translation, item["varname"]),
- translate_expr(translation, item["val"]))
return set_str
def translate_do(translation, token):