[Mumps2Py:] [60] figured out how to implement $LENGTH(str, sep), and added support for $PIECE().

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


Revision: 60
Author:   pgallot
Date:     2008-01-28 17:09:18 +0000 (Mon, 28 Jan 2008)

Log Message:
-----------
figured out how to implement $LENGTH(str, sep), and added support for $PIECE().

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


Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py	2008-01-28 17:03:05 UTC (rev 59)
+++ trunk/mumps2py/tok2python.py	2008-01-28 17:09:18 UTC (rev 60)
@@ -206,7 +206,7 @@
         """ translates the Mumps Intrinsic function EXTRACT to Python"""
         inputstr_str = translate_expr(translation, token.params[0])
         start_str = translate_expr_sub_one(translation, token.params[1])
-
+        
         if len(token.params) == 2:
             extract_str = "%s[%s]" % (inputstr_str, start_str)
         elif len(token.params) == 3:
@@ -225,10 +225,39 @@
         if len(token.params) == 1:
             length_str = "len(%s)" % \
                        translate_expr(translation, token.params[0])
-            return length_str
+        elif len(token.params) == 2:
+            length_str = "%s.count(%s) + 1" % \
+                         (translate_expr(translation, token.params[0]),
+                          translate_expr(translation, token.params[1]))
         else:
-            raise TranslationError(translation, token, "idiom not implemented")
+            raise TranslationError(translation, token,
+                                   "incorrect number of parameters")
+        return length_str
 
+    def translate_intr_piece(translation, token):
+        """ translates the Mumps Intrinsic function EXTRACT to Python"""
+        translation.add_import("re")
+        inputstr_str = translate_expr(translation, token.params[0])
+        sep_str = translate_expr(translation, token.params[1])
+
+        if len(token.params) == 2:
+            piece_str = "re.split(%s, %s)[0]" % (sep_str, inputstr_str)
+        else:
+            start0_str = translate_expr_sub_one(translation, token.params[2])
+            if len(token.params) == 3:
+                start1_str = translate_expr(translation, token.params[2])
+                piece_str = "re.split(%s, %s, %s)[%s]" % \
+                            (sep_str, inputstr_str, start1_str, start0_str)
+            elif len(token.params) == 4:
+                end_str = translate_expr(translation, token.params[3])
+                piece_str = "%s.join(re.split(%s, %s, %s)[%s:%s])" % \
+                            (sep_str, sep_str, inputstr_str, end_str, \
+                             start0_str, end_str)
+            else:
+                raise TranslationError(translation, token, \
+                                       "incorrect number of parameters")
+        return piece_str
+    
     def translate_local_var(translation, token):
         """ translates a local variable into a Python variable"""
         if not token.is_indexed() or \
@@ -302,6 +331,7 @@
         F_DATA: translate_intr_data,
         F_EXTRACT: translate_intr_extract,
         F_LENGTH: translate_intr_length,
+        F_PIECE: translate_intr_piece,
         EXPR: translate_expr_list}
 
     if expr_transl_dict.has_key(token.toktype):


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