[Mumps2Py:] [25] made a change to expression translation to reduce extraneous parentheses that I 'm not completely sure about. |
[ Thread Index |
Date Index
| More lists.mumps2py.org/discuss Archives
]
Revision: 25
Author: pgallot
Date: 2008-01-18 19:36:07 +0000 (Fri, 18 Jan 2008)
Log Message:
-----------
made a change to expression translation to reduce extraneous parentheses that I'm not completely sure about. But the main change was adding support for the $CHAR intrinsic.
Modified Paths:
--------------
trunk/mumps2py/tok2python.py
Modified: trunk/mumps2py/tok2python.py
===================================================================
--- trunk/mumps2py/tok2python.py 2008-01-18 19:31:40 UTC (rev 24)
+++ trunk/mumps2py/tok2python.py 2008-01-18 19:36:07 UTC (rev 25)
@@ -117,7 +117,19 @@
raise TranslationError(translation, token,
"incorrect number of parameters")
return ascii_str
-
+
+ def translate_intr_char(translation, token):
+ """ translates the Mumps Intrinsic function CHAR to Python"""
+ if len(token.params) == 1:
+ char_str = "chr(%s)" % \
+ translate_expr(translation, token.params[0])
+ else:
+ char_str = r"''.join(map(chr, "
+ for param in token.params:
+ char_str = char_str + translate_expr(translation, param) + ", "
+ char_str = char_str[:-2] + ')'
+ return char_str
+
def translate_intr_extract(translation, token):
""" translates the Mumps Intrinsic function EXTRACT to Python"""
inputstr_str = translate_expr(translation, token.params[0])
@@ -169,7 +181,9 @@
expr_list = token.expr_list[:]
first_token = expr_list.pop(0)
if first_token.is_unaryop():
- left_str = "(%s)" % (trans_unary_expr(first_token, expr_list.pop(0)))
+ left_str = "%s" % (trans_unary_expr(first_token, expr_list.pop(0)))
+ if len(expr_list):
+ left_str = '(' + left_str + ')'
else:
left_str = translate_expr(translation, first_token)
while expr_list:
@@ -184,9 +198,14 @@
right_str = trans_unary_expr(right_token, expr_list.pop(0))
else:
right_str = translate_expr(translation, right_token)
- left_str = "%s(%s %s %s)" % (not_str, left_str,
- trans_binary_op(binop_token),
- right_str)
+
+ if len(expr_list) or len(not_str):
+ left_str = "%s(%s %s %s)" % (not_str, left_str,
+ trans_binary_op(binop_token),
+ right_str)
+ else:
+ left_str = "%s %s %s" % \
+ (left_str, trans_binary_op(binop_token), right_str )
return left_str
expr_transl_dict = {
@@ -194,8 +213,9 @@
STRINGLITERAL:translate_str,
LOCALVAR:translate_local_var,
FCC_NEWLINE:translate_fcc_newline,
+ F_ASCII: translate_intr_ascii,
+ F_CHAR: translate_intr_char,
F_EXTRACT: translate_intr_extract,
- F_ASCII: translate_intr_ascii,
EXPR: translate_expr_list}
if expr_transl_dict.has_key(token.toktype):