Changeset 5c2a0f2 in sasmodels
- Timestamp:
- Dec 18, 2017 11:59:35 AM (7 years ago)
- Children:
- 4c72117
- Parents:
- d9341fa
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/py2c.py
rd9341fa r5c2a0f2 100 100 101 101 #def to_source(node, indent_with=' ' * 4, add_line_information=False): 102 def to_source(node, func_name ):102 def to_source(node, func_name, constants=None): 103 103 """This function can convert a node tree back into python sourcecode. 104 104 This is useful for debugging purposes, especially if you're dealing with … … 118 118 number information of statement nodes. 119 119 """ 120 generator = SourceGenerator(' ' * 4, False )120 generator = SourceGenerator(' ' * 4, False, constants) 121 121 # generator.required_functions = func_name 122 122 generator.visit(node) … … 138 138 """ 139 139 140 def __init__(self, indent_with, add_line_information=False ):140 def __init__(self, indent_with, add_line_information=False, constants=None): 141 141 self.result = [] 142 142 self.indent_with = indent_with … … 160 160 self.C_Functions = [] 161 161 self.C_Vectors = [] 162 self.C_Constants = constants 162 163 self.SubRef = False 163 164 self.InSubscript = False … … 800 801 name = node.id 801 802 # add variable to C_Vars if it ins't there yet, not an argument and not a number 802 if ((name not in self.C_Functions) and (name not in self.C_Vars) and \803 if ((name not in self.C_Functions) and (name not in self.C_Vars) and \ 803 804 (name not in self.C_IntVars) and (name not in self.arguments) and \ 804 (name .isnumeric() == False)):805 (name not in self.C_Constants) and (name.isnumeric() == False)): 805 806 if(self.InSubscript): 806 807 self.C_IntVars.append(node.id) … … 945 946 946 947 def visit_Subscript(self, node): 947 if(node.value.id not in self.C_Pointers): 948 self.C_Pointers.append(node.value.id) 948 if (node.value.id not in self.C_Constants): 949 if(node.value.id not in self.C_Pointers): 950 self.C_Pointers.append(node.value.id) 949 951 self.SubRef = True 950 952 self.visit(node.value) … … 1105 1107 vars = c_constants.keys() 1106 1108 for c_var in vars: 1107 declare_values = str(c_constants[c_var]) 1108 if (hasattr(declare_values,'__len__')): 1109 declare_values = declare_values.replace (' ',' ') 1110 declare_values = declare_values.replace ('[ ','[') 1111 declare_values = declare_values.replace (' ]',']') 1112 declare_values = declare_values.replace (' ',',') 1109 c_values = c_constants[c_var] 1110 declare_values = str(c_values) 1113 1111 str_dcl = "double " + c_var 1112 if (hasattr(c_values,'__len__')): 1113 str_dcl += "[]" 1114 len_prev = len(declare_values) 1115 len_after = len_prev - 1 1116 declare_values = declare_values.replace ('[','') 1117 declare_values = declare_values.replace (']','').strip() 1118 while (len_after < len_prev): 1119 len_prev = len_after 1120 declare_values = declare_values.replace (' ',' ') 1121 len_after = len(declare_values) 1122 declare_values = "{" + declare_values.replace (' ',',') + "}" 1114 1123 str_dcl += " = " + declare_values + ";" 1115 1124 sniplets.append (str_dcl) … … 1126 1135 # sniplets.append(line_directive) 1127 1136 tree = ast.parse(source) 1128 sniplet = to_source(tree, functions ) # in the future add filename, offset, constants1137 sniplet = to_source(tree, functions, constants) # in the future add filename, offset, constants 1129 1138 sniplets.append(sniplet) 1130 1139 c_code = "\n".join(sniplets)
Note: See TracChangeset
for help on using the changeset viewer.