[Mumps2Py:] [57] Tweaked tuplify, added support for the Merge command (and zwrite).

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


Revision: 57
Author:   pgallot
Date:     2008-01-26 20:00:24 +0000 (Sat, 26 Jan 2008)

Log Message:
-----------
Tweaked tuplify, added support for the Merge command (and zwrite).

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


Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py	2008-01-26 19:56:30 UTC (rev 56)
+++ trunk/mumps2py/tok2python.py	2008-01-26 20:00:24 UTC (rev 57)
@@ -99,12 +99,15 @@
 def tuplify(translation, listitems):
     """take a list of items, turn them in to a tuple for Python."""
     if listitems == None:
-        return "(None)"
+        return "(None, )"
     else:
         tuple_str = "("
         for item in listitems:
             tuple_str = tuple_str + translate_expr(translation, item) + ", "
-        return tuple_str[:-2] + ")"
+        if len(listitems) > 1:
+            tuple_str = tuple_str[:-2]
+        
+        return tuple_str + ")"
 
 def post_condition(translation, token, indentlevel):
     """post-conditions a command."""
@@ -334,6 +337,14 @@
     write_str = "%s%sprint %s\n" % (write_str, tab(indentlevel), write_args)
     return write_str
 
+
+def translate_zwrite(translation, token):
+    """translate the (non-standard) Mumps ZWrite Command to Python"""
+    for item in token.write_list:
+        if item.is_var() and item.has_index() == (None):
+            item.unindex()
+    return translate_write(translation, token)
+
 def translate_for(translation, token):
     """translate the Mumps For Command to Python"""
     if len(token.for_loops) == 0: # then we've got an infinite-loop.
@@ -502,9 +513,34 @@
 
 def translate_merge(translation, token):
     """translate the Mumps Merge command to Python"""
-    raise TranslationError(translation, token,
-                           "token type translation not implemented")
+    if not token.items[0].has_key("r_item"):
+        raise TranslationError(translation, token, "idiom not implemented")
 
+    indentlevel, merge_str = post_condition(translation, token, \
+                                            token.indentlevel)
+    for item in token.items:
+        recipient, copy_item = item['l_item'], item['r_item']
+        if  recipient.is_var() and copy_item.is_var():
+            if not translation.initialized(recipient.varname):
+                translation.add_initialization(recipient.varname)
+
+            if not recipient.has_index() and not copy_item.has_index():
+                merge_str = "%s%s%s.update(%s)\n" % \
+                            (merge_str, tab(indentlevel),
+                             recipient.varname, copy_item.varname)
+            else:
+                translation.add_cl_import()
+                merge_str = "%s%sMc_merge(%s, %s, %s, %s)\n" % \
+                            (merge_str, tab(indentlevel),
+                             recipient.varname,
+                             tuplify(translation, recipient.indices),
+                             copy_item.varname,
+                             tuplify(translation, copy_item.indices))
+        else:
+            raise TranslationError(translation, token, "idiom not implemented")
+
+    return merge_str    
+
 def translate_new(translation, token):
     """ translate the MUmps New command to Python"""
     raise TranslationError(translation, token,
@@ -573,6 +609,7 @@
     VIEWCMD: translate_view,
     WRITECMD: translate_write,
     XECUTECMD: translate_xecute,
+    ZWRITECMD: translate_zwrite,
     }
 
 
@@ -592,8 +629,8 @@
                 raise TranslationError(translation, token, 
                                        "token type translation not implemented")
         translation.add_frontmatter()
-    except TranslationError, e:
-        print e.error_msg()
+    except TranslationError, err:
+        print err.error_msg()
 #    finally:
 #        translation.code.append("incomplete!!!\n")
 #        return translation.code


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