Changeset 3f9db6e in sasmodels
 Timestamp:
 Dec 8, 2017 9:06:05 AM (6 years ago)
 Children:
 7f79cba
 Parents:
 71779b2
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

sasmodels/py2c.py
r71779b2 r3f9db6e 113 113 return ''.join(generator.c_proc) 114 114 115 def isevaluable(s): 116 try: 117 eval(s) 118 return True 119 except: 120 return False 121 115 122 class SourceGenerator(NodeVisitor): 116 123 """This visitor is able to transform a well formed syntax tree into python … … 778 785 self.write_python('}') 779 786 787 def get_special_power (self, string): 788 function_name = '' 789 is_negative_exp = False 790 if (isevaluable(str(self.current_statement))): 791 exponent = eval(string) 792 is_negative_exp = exponent < 0 793 abs_exponent = abs(exponent) 794 if (abs_exponent == 2): 795 function_name = "square" 796 elif (abs_exponent == 3): 797 function_name = "cube" 798 elif (abs_exponent == 0.5): 799 function_name = "sqrt" 800 elif (abs_exponent == 1.0/3.0): 801 function_name = "cbrt" 802 if (function_name == ''): 803 function_name = "pow" 804 return function_name, is_negative_exp 805 780 806 def translate_power (self, node): 781 807 # get exponent by visiting the right hand argument. 782 808 function_name = "pow" 783 809 temp_statement = self.current_statement 810 # 'visit' functions write the results to the 'current_statement' class memnber 811 # Here, a temporary variable, 'temp_statement', is used, that enables the 812 # use of the 'visit' function 784 813 self.current_statement = '' 785 814 self.visit(node.right) 786 815 exponent = self.current_statement.replace(' ','') 787 exponent = exponent.replace('(','') 788 exponent = exponent.replace(')','') 816 function_name, is_negative_exp = self.get_special_power (self.current_statement) 789 817 self.current_statement = temp_statement 790 # is the right hand argument, the exponent, a number? 791 if (hasattr(node.right, 'n')): # power of constand 792 exponent = node.right.n 793 if (exponent == 2): 794 function_name = "square" 795 elif (exponent == 3): 796 function_name = "cube" 797 elif (exponent == 0.5): 798 function_name = "sqrt" 799 elif (exponent == "1/2"): 800 function_name = "sqrt" 818 if (is_negative_exp): 819 self.write_c ("1.0 / (") 801 820 self.write_c (function_name + " (") 802 821 self.visit(node.left) … … 805 824 self.visit(node.right) 806 825 self.write_c(")") 826 if (is_negative_exp): 827 self.write_c(")") 828 self.write_c(" ") 807 829 808 830 def translate_integer_divide (self, node):
Note: See TracChangeset
for help on using the changeset viewer.