[Mumps2Py:] [47] added support for calling the Mumps Compatibility library so that I could implement translation of $DATA .

[ Thread Index | Date Index | More lists.mumps2py.org/discuss Archives ]


Revision: 47
Author:   pgallot
Date:     2008-01-23 21:24:55 +0000 (Wed, 23 Jan 2008)

Log Message:
-----------
added support for calling the Mumps Compatibility library so that I could implement translation of $DATA.
Note that this does not yet support Global variables.

Modified Paths:
--------------
    trunk/mumps2py/tok2python.py


Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py	2008-01-23 21:17:54 UTC (rev 46)
+++ trunk/mumps2py/tok2python.py	2008-01-23 21:24:55 UTC (rev 47)
@@ -42,6 +42,7 @@
         self.mumps_module = a_mumps_module
         self.code = []
         self.imports = {}
+        self.cl_import = False
         self.modvars = {}
         self.dict_inits = []
 
@@ -53,6 +54,10 @@
         """adds a module to the list to be imported in the translation"""
         self.imports[a_module] = True
 
+    def add_cl_import(self):
+        """the Compatibility Library needs to be imported."""
+        self.cl_import = True
+
     def add_frontmatter(self):
         """add in what needs to go at the top of the translated code"""
         block_str = "# %s \n# translated from %s\n# by Mumps2Py\n\n" % \
@@ -62,8 +67,10 @@
             import_args = ""
             for pymod in self.imports.keys():
                 import_args = import_args + pymod + ", "
-            block_str = "%simport %s\n\n" % (block_str, import_args[:-2])
-            
+            block_str = "%simport %s\n" % (block_str, import_args[:-2])
+        if self.cl_import:
+            block_str = block_str + "from mumps2py.mumpsCL import *\n"
+        block_str = block_str + "\n"
         
         for init in self.dict_inits:
             block_str = "%s%s = {}\n" % (block_str, init)
@@ -86,9 +93,19 @@
 
 def tab(indentlevel):
     """puts out 4 spaces for every indentlevel."""
-    tab_str = "%*c" % (indentlevel*4, '^') 
-    return tab_str[:-1]
+    return ' ' * (indentlevel * 4)
 
+
+def tuplify(translation, listitems):
+    """take a list of items, turn them in to a tuple for Python."""
+    if listitems == None:
+        return "(None)"
+    else:
+        tuple_str = "("
+        for item in listitems:
+            tuple_str = tuple_str + translate_expr(translation, item) + ", "
+        return tuple_str[:-2] + ")"
+
 def post_condition(translation, token, indentlevel):
     """post-conditions a command."""
     if token.post_condition_expr:
@@ -118,9 +135,9 @@
             return "def %s(%s):\n" % (token.val, token.parameters[0])
         else:
             label_str = "def %s(" % (token.val)
-            for param in token.parameters[:-1]:
+            for param in token.parameters:
                 label_str = "%s%s ," % (label_str, param)
-            label_str = label_str + " ):\n"
+            label_str = label_str[:-2] + " ):\n"
             return label_str
     else:
         return "def %s():\n" % (token.Name)
@@ -161,12 +178,27 @@
             char_str = "chr(%s)" % \
                        translate_expr(translation, token.params[0])
         else:
-            char_str = r"''.join(map(chr, ("
-            for param in token.params:
-                char_str = char_str + translate_expr(translation, param) + ", "
-            char_str = char_str[:-2] + ")))"
+            char_str = r"''.join(map(chr, %s))" % \
+                       tuplify(translation, token.params)
         return char_str
 
+    def translate_intr_data(translation, token):
+        """ translates the Mumps Intrinsic function DATA to Python"""
+        if len(token.params) != 1:
+            raise TranslationError(translation, token,
+                                   "incorrect number of parameters")
+        param = token.params[0]
+        if not param.is_var():
+            raise TranslationError(translation, token, "idiom not implemented")
+
+        translation.add_cl_import()
+        if param.has_index():
+            data_str = "Mf_data(globals(), '%s', %s)" % \
+                       (param.varname, tuplify(translation, param.indices))
+        else:
+            data_str = "Mf_data(globals(), '%s')" % param.varname
+        return data_str                
+
     def translate_intr_extract(translation, token):
         """ translates the Mumps Intrinsic function EXTRACT to Python"""
         inputstr_str = translate_expr(translation, token.params[0])
@@ -196,16 +228,12 @@
 
     def translate_local_var(translation, token):
         """ translates a local variable into a Python variable"""
-        if not token.__dict__.has_key('indices') or \
+        if not token.is_indexed() or \
            not translation.initialized(token.varname):
             return token.varname
         else:
-            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]))
+            var_str = "%s[%s]" % \
+                      (token.varname, tuplify(translation, token.indices))
             return var_str
 
     def translate_expr_list(translation, token):
@@ -268,6 +296,7 @@
         FCC_NEWLINE:translate_fcc_newline,
         F_ASCII: translate_intr_ascii,
         F_CHAR: translate_intr_char,
+        F_DATA: translate_intr_data,
         F_EXTRACT: translate_intr_extract,
         F_LENGTH: translate_intr_length,
         EXPR: translate_expr_list}
@@ -361,15 +390,8 @@
 
     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,
+                                    tuplify(translation, vartoken.indices),
                                     translate_expr(translation, valtoken))
         translation.initialize(vartoken.varname)
         return set_str
@@ -382,14 +404,14 @@
         elif item.has_key("multiset"):
             for varname in item["var_names"]:
                 if token.indentlevel == 0 and \
-                   varname.__dict__.has_key('indices') and \
+                   varname.is_var() and varname.is_indexed() 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 \
+                    if varname.is_var() and varname.is_indexed() and \
                        not translation.initialized(varname.varname):
                         translation.add_initialization(varname.varname)
                         
@@ -398,21 +420,22 @@
                                translate_expr(translation, varname),
                                translate_expr(translation, item["val"]))
         else:
+            varname = item['varname']
             if token.indentlevel == 0 and \
-               item['varname'].__dict__.has_key('indices') and \
-               not translation.initialized(item['varname'].varname):
+               varname.is_var() and varname.is_indexed() and \
+               not translation.initialized(varname.varname):
                 set_str = "%s%s%s\n" % \
                           (set_str, tab(indentlevel),
                            initialize_indexedvar(translation,
-                                                 item['varname'],
+                                                 varname,
                                                  item["val"]))
             else:
-                if item['varname'].__dict__.has_key('indices') and \
-                  not translation.initialized(item['varname'].varname):
-                    translation.add_initialization(item['varname'].varname)
+                if varname.is_var() and varname.is_indexed() 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, item["varname"]),
+                            translate_expr(translation, varname),
                             translate_expr(translation, item["val"]))
     return set_str
 
@@ -571,7 +594,8 @@
         translation.add_frontmatter()
     except TranslationError, e:
         print e.error_msg()
-        translation.code.append("incomplete!!!\n")
-        return translation.code
+#    finally:
+#        translation.code.append("incomplete!!!\n")
+#        return translation.code
     
     return translation.code


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/