[Mumps2Py:] [34] refactored post-condition handling, added handling for the Quit command, and a bit of lint removal.

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


Revision: 34
Author:   pgallot
Date:     2008-01-20 14:58:38 +0000 (Sun, 20 Jan 2008)

Log Message:
-----------
refactored post-condition handling, added handling for the Quit command, and a bit of lint removal.

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


Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py	2008-01-20 14:55:44 UTC (rev 33)
+++ trunk/mumps2py/tok2python.py	2008-01-20 14:58:38 UTC (rev 34)
@@ -68,6 +68,19 @@
 def tab(indentlevel):
     tab_str = "%*c" % (indentlevel*4, '^') 
     return tab_str[:-1]
+
+def post_condition(translation, token, indentlevel):
+    """post-conditions a command."""
+    if token.post_condition_expr:
+        condition_str = "%sif %s:\n" % \
+                             (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 translate_emptyline(translation, token):
     """translate an empty line (a base-case, if you will)."""
@@ -183,7 +196,7 @@
             op_trans_dict = {OPADD:'+', OPSUB:'-', OPMULT:'*', OPEXP:'**',
                              OPMODULO:'%', OPGT:'>', OPLT:'<', OPNGT:'<=',
                              OPNLT:'>=', OPEQ:'==', OPNEQ:'!=', OPAND:'and',
-                             OPOR:'or'}
+                             OPOR:'or', OPFRACDIV:'/', OPINTDIV:'/'}
 
             if op_trans_dict.has_key(token.toktype):
                 return op_trans_dict[token.toktype]
@@ -256,20 +269,13 @@
 
 def translate_write(translation, token):
     """translate the Mumps Write Command to Python"""
-    write_str = ""
-    indentlevel = token.indentlevel
-    if token.post_condition_expr:
-        write_str = "%sif %s:\n" % (tab(indentlevel),
-                                  translate_expr(translation,
-                                                 token.post_condition_expr))
-        indentlevel = indentlevel + 1
-
+    indentlevel, write_str = post_condition(translation, token,
+                                            token.indentlevel)
     write_args = ""
     for item in token.write_list:
         write_args = write_args + translate_expr(translation, item)+", "
 
-    write_str = "%s%sprint %s\n" % (write_str, tab(token.indentlevel),
-                                    write_args)
+    write_str = "%s%sprint %s\n" % (write_str, tab(indentlevel), write_args)
     return write_str
 
 def translate_for(translation, token):
@@ -326,13 +332,8 @@
 
 def translate_set(translation, token):
     """ translate the Mumps Set command to Python"""
-    set_str = ""
-    indentlevel = token.indentlevel
-    if token.post_condition_expr:
-        set_str = "%sif %s:\n" % (tab(indentlevel),
-                                  translate_expr(translation,
-                                                 token.post_condition_expr))
-        indentlevel = indentlevel + 1
+    indentlevel, set_str = post_condition(translation, token,
+                                            token.indentlevel)
     for item in token.var_set:
         if item.has_key("indirection"):
             raise TranslationError(translation, token, "idiom not implemented")
@@ -427,9 +428,16 @@
 
 def translate_quit(translation, token):
     """ translate the Mumps Quit command to Python"""
-    raise TranslationError(translation, token,
-                           "token type translation not implemented")
-
+    indentlevel, quit_str = post_condition(translation, token,
+                                            token.indentlevel)
+    if token.expr:
+        quit_str = "%s%sreturn %s\n" % \
+                   (quit_str, tab(indentlevel),
+                    translate_expr(translation, token.expr))
+    else:
+        quit_str = "%s%sbreak\n" % (quit_str, tab(indentlevel))
+    return quit_str
+                                      
 def translate_read(translation, token):
     """ translate the Mumps Read command to Python"""
     raise TranslationError(translation, token,
@@ -496,6 +504,7 @@
                                        "token type translation not implemented")
         translation.add_frontmatter()
     except TranslationError, e:
+        print e.error_msg()
         translation.code.append("incomplete!!!\n")
         return translation.code
     


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