[Mumps2Py:] [23] added handling for the $ASCII intrinsic function,

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


Revision: 23
Author:   pgallot
Date:     2008-01-18 16:40:27 +0000 (Fri, 18 Jan 2008)

Log Message:
-----------
added handling for the $ASCII intrinsic function,
added a way to add front_matter,
reverted the change to the Write command translation (we can live with the extra spaces for the naive translation).

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


Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py	2008-01-18 16:34:53 UTC (rev 22)
+++ trunk/mumps2py/tok2python.py	2008-01-18 16:40:27 UTC (rev 23)
@@ -37,14 +37,34 @@
 
 
 class Translation:
+    """ Class that receives the translation."""
     def __init__(self, a_mumps_module):
         self.mumps_module = a_mumps_module
         self.code = []
+        self.imports = {}
 
     def add_code(self, block):
         """adds another block of code to the translation"""
         self.code.append(block)
 
+    def add_import(self, a_module):
+        """adds a module to the list to be imported in the translation"""
+        self.imports[a_module] = True
+
+    def add_frontmatter(self):
+        """add in what needs to go at the top of the translated code"""
+        block_str = "# %s \n# translated from %s\n# by Mumps2Py\n\n" % \
+                    (self.mumps_module.output_file,
+                     self.mumps_module.input_file)
+        if len(self.imports):
+            import_args = ""
+            for pymod in self.imports.keys():
+                import_args = import_args + pymod + ", "
+            block_str.append("import %s\n" % import_args[:-2])
+
+        self.code.insert(0, block_str)
+    
+
 def tab(indentlevel):
     tab_str = "%*c" % (indentlevel*4, '^') 
     return tab_str[:-1]
@@ -84,8 +104,22 @@
         """ translates a Mumps-style newline to Python"""
         return r'"\n"'
 
+    def translate_intr_ascii(translation, token):
+        """ translates the Mumps Intrinsic function ASCII to Python"""
+        if len(token.params) == 1:
+            ascii_str = "ord(%s[0])" % \
+                        translate_expr(translation, token.params[0])
+        elif len(token.params) == 2:
+            ascii_str = "ord(%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 ascii_str
+            
     def translate_intr_extract(translation, token):
-        """ translates the Mumps Intrinsic function EXTRACT to python"""
+        """ 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])
 
@@ -161,6 +195,7 @@
         LOCALVAR:translate_local_var,
         FCC_NEWLINE:translate_fcc_newline,
         F_EXTRACT: translate_intr_extract,
+        F_ASCII: translate_intr_ascii,
         EXPR: translate_expr_list}
 
     if expr_transl_dict.has_key(token.toktype):
@@ -196,11 +231,9 @@
         indentlevel = indentlevel + 1
 
     write_args = ""
-    for item in token.write_list[:-1]:
-        write_args = write_args + translate_expr(translation, item) + " + "
+    for item in token.write_list:
+        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
@@ -407,11 +440,11 @@
                 trans_func = TRANS_FUNC_DICT[token.toktype]
                 
                 code_block = trans_func(translation, token)
-                print code_block
                 translation.add_code(code_block)
             else:
                 raise TranslationError(translation, token, 
                                        "token type translation not implemented")
+        translation.add_frontmatter()
     except TranslationError, e:
         translation.code.append("incomplete!!!\n")
         return translation.code


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