[Mumps2Py:] [7] added handling of the $EXTRACT intrinsic function,

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


Revision: 7
Author:   pgallot
Date:     2008-01-17 17:55:53 +0000 (Thu, 17 Jan 2008)

Log Message:
-----------
added handling of the $EXTRACT intrinsic function,
improvements to the base-case FOR command translating,
and improved the WRITE command translation.

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


Modified: trunk/tok2python.py
===================================================================
--- trunk/tok2python.py	2008-01-17 17:53:51 UTC (rev 6)
+++ trunk/tok2python.py	2008-01-17 17:55:53 UTC (rev 7)
@@ -86,6 +86,22 @@
         """ translates a Mumps-style newline to Python"""
         return r'"\n"'
 
+    def translate_intr_extract(translation, token):
+        """ translates the Mumps Intrinsic function EXTRACT to python"""
+        if len(token.params) == 3:
+            extract_str = "%s[%s:%s]" % (
+                translate_expr(translation, token.params[0]),
+                translate_expr_sub_one(translation, token.params[1]),
+                translate_expr(translation, token.params[2]))
+        elif len(token.params) == 2:
+            extract_str = "%s[%s]" % (
+                translate_expr(translation, token.params[0]),
+                translate_expr_sub_one(translation, token.params[1]))
+        else:
+            raise TranslationError(translation,token,
+                                   "incorrect number of parameters")
+        return extract_str
+                 
     def translate_local_var(translation, token):
         """ translates a local variable into a Python variable"""
         if not token.__dict__.has_key('indices'):
@@ -143,6 +159,7 @@
         STRINGLITERAL:translate_str,
         LOCALVAR:translate_local_var,
         FCC_NEWLINE:translate_fcc_newline,
+        F_EXTRACT: translate_intr_extract,
         EXPR: translate_expr_list}
 
     if expr_transl_dict.has_key(token.toktype):
@@ -153,6 +170,17 @@
         raise TranslationError(translation, token,
                                "token type translation not implemented")
 
+def translate_expr_plus_one(translation, token):
+    if token.is_int():
+        return str(int(token.val,10) + 1)
+    else:
+        return translate_expr(translation,token) + "+1"
+    
+def translate_expr_sub_one(translation, token):
+    if token.is_int():
+        return str(int(token.val,10) - 1)
+    else:
+        return translate_expr(translation,token) + "-1"
 
 def translate_write(translation, token):
     """translate the Mumps Write Command to Python"""
@@ -165,8 +193,11 @@
         indentlevel = indentlevel + 1
 
     write_args = ""
-    for item in token.write_list:
-        write_args = write_args + translate_expr(translation, item)+", "
+    for item in token.write_list[:-1]:
+        write_args = write_args + translate_expr(translation, item) + " + "
+
+    write_args = write_args + translate_expr(translation,
+                                             token.write_list[-1]) + ", "
     write_str = "%s%sprint %s\n" % (write_str, tab(token.indentlevel),
                                     write_args)
     return write_str
@@ -175,9 +206,20 @@
     """translate the Mumps For Command to Python"""
     #TODO: completely redo to use a MumpsCL iterator...
     if len(token.for_loops) == 1 and token.for_loops[0].has_key('TermVal'):
-        init_val = translate_expr(translation, token.for_loops[0]['initVal'])
-        incr_val = translate_expr(translation, token.for_loops[0]['IncrVal'])
-        trm_val = translate_expr(translation, token.for_loops[0]['TermVal'])
+        init_tok = token.for_loops[0]['initVal']
+        incr_tok = token.for_loops[0]['IncrVal']
+        trm_tok = token.for_loops[0]['TermVal']
+        init_val = translate_expr(translation, init_tok)
+        incr_val = translate_expr(translation, incr_tok)
+        
+        if incr_tok.is_int():
+            if int(incr_tok.val) > 0:
+                trm_val = translate_expr_plus_one(translation, trm_tok)
+            else:
+                trm_val = translate_expr_sub_one(translation, trm_tok)
+        else:
+            trm_val = translate_expr(translation, trm_tok)
+
         if init_val == '0': # the default starting value in a range, omit
             init_val = ""
         else:


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