[Mumps2Py:] [133] more argument indirection work.

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


Revision: 133
Author:   pgallot
Date:     2008-02-26 04:51:56 +0000 (Tue, 26 Feb 2008)

Log Message:
-----------
more argument indirection work.

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


Modified: trunk/mumps2py/mumpsCL.py
===================================================================
--- trunk/mumps2py/mumpsCL.py	2008-02-26 04:50:46 UTC (rev 132)
+++ trunk/mumps2py/mumpsCL.py	2008-02-26 04:51:56 UTC (rev 133)
@@ -24,15 +24,26 @@
 # value. F i:1:1:10 iterates from 1 to 10, inclusive. the equivalent would be
 # range(1, 11)
 
-def M_eval(line, loc_adr_space):
+def M_eval(line):
     """parses, translates, and evaluates a string as a Mumps expression"""
     from mumps2py import parse_expr, Translation, translate_expr
     
     token = parse_expr(line, 0)
     translation = Translation(None)
     expression = translate_expr(translation, token)
-    return eval(expression, loc_adr_space)
+    return expression
 
+def M_cmd_eval(cmd, arg_str):
+    """ evaluates a Mumps command using argument indirection"""
+    from mumps2py import parse_command, Translation, translate
+
+    cmd_str = " %s %s" % (cmd, arg_str)
+    token = parse_command(cmd_str)
+    translation = Translation(None)
+    expression = translate(translation, token)
+    return expression
+    
+
 def Mf_horolog(timestamp = None):
     """function that emulates Mumps $Horolog variable."""
     if timestamp == None:

Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py	2008-02-26 04:50:46 UTC (rev 132)
+++ trunk/mumps2py/tok2python.py	2008-02-26 04:51:56 UTC (rev 133)
@@ -114,14 +114,20 @@
     """post-conditions a command."""
     if token.post_condition_expr:
         condition_str = "%sif %s:\n" % \
-                             (tab(indentlevel),
-                              translate_expr(translation,
-                                             token.post_condition_expr))
+                        (tab(indentlevel),
+                         translate_expr(translation, token.post_condition_expr))
         new_indent = indentlevel + 1
     else:
         condition_str = ""
         new_indent = indentlevel
     return (new_indent, condition_str)
+
+def command_indirection(translation, token):
+    """handle command-argument indirection"""
+    translation.add_cl_import()
+    cmd_str = "eval(M_cmd_eval(%s, eval(M_eval(%s))))" % \
+              (token.name, translate_expr(translation, token.indirect))
+    return cmd_str
     
 def translate_emptyline(translation, token):
     """translate an empty line (a base-case, if you will)."""
@@ -164,7 +170,7 @@
     def translate_indirection(translation, token):
         """translates an atomic indirection expression to Python"""
         translation.add_cl_import()
-        indr_str = "M_eval(%s, locals())" % \
+        indr_str = "eval(M_eval(%s))" % \
                    translate_expr(translation, token.expr)
         if token.is_indexed():
             indr_str = "%s[%s]" % \
@@ -556,6 +562,11 @@
     """translate the Mumps Write Command to Python"""
     indentlevel, write_str = post_condition(translation, token,
                                             token.indentlevel)
+    if token.indirect:
+        write_str = "%s%s%s" % (write_str, tab(indentlevel), \
+                                command_indirection(translation, token))
+        return write_str 
+        
     write_args = ""
     for item in token.write_list:
         write_args = write_args + translate_expr(translation, item)+", "
@@ -634,6 +645,11 @@
         return set_str
 
     indentlevel, set_str = post_condition(translation, token, token.indentlevel)
+
+    if token.indirect:
+        set_str = "%s%s%s" % (set_str, tab(indentlevel), \
+                              command_indirection(translation, token))
+        return set_str 
     
     for item in token.var_set:
         if item.has_key("indirection"):
@@ -686,6 +702,11 @@
 
 def translate_if(translation, token):
     """translate the Mumps If commands to Python"""
+    if token.indirect:
+        if_str = "%s%s" % (tab(token.indentlevel),
+                           command_indirection(translation, token))
+        return if_str 
+
     condition_list = token.condition_list[:]
     condition_str = ""
     while len(condition_list) > 1:
@@ -729,11 +750,17 @@
 
 def translate_kill(translation, token):
     """translate the Mumps Kill command to Python"""
-    if len(token.excl_list) or not len(token.kill_list):
+    indentlevel, kill_str = post_condition(translation, token,
+                                            token.indentlevel)
+
+    if token.indirect:
+        kill_str = "%s%s%s" % (kill_str, tab(indentlevel), \
+                               command_indirection(translation, token))
+        return kill_str
+    elif len(token.excl_list) or not len(token.kill_list):
         raise TranslationError(translation, token,
                            "idiom translation not implemented")
-    indentlevel, kill_str = post_condition(translation, token,
-                                            token.indentlevel)
+
     for item in token.kill_list:
         kill_str = "%s%sdel %s\n" % \
                    (kill_str, tab(indentlevel),
@@ -747,11 +774,16 @@
 
 def translate_merge(translation, token):
     """translate the Mumps Merge command to Python"""
-    if not token.items[0].has_key("r_item"):
+                               
+    indentlevel, merge_str = post_condition(translation, token, \
+                                            token.indentlevel)
+    if token.indirect:
+        merge_str = "%s%s%s" % (merge_str, tab(indentlevel), \
+                                command_indirection(translation, token))
+        return merge_str
+    elif 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():
@@ -776,7 +808,7 @@
     return merge_str    
 
 def translate_new(translation, token):
-    """ translate the MUmps New command to Python"""
+    """ translate the Mumps New command to Python"""
     raise TranslationError(translation, token,
                            "token type translation not implemented")
 


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