Changes in sasmodels/py2c.py [ddfdb16:7b1dcf9] in sasmodels
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sasmodels/py2c.py
rddfdb16 r7b1dcf9 81 81 BOOLOP_SYMBOLS = {} 82 82 BOOLOP_SYMBOLS[ast.And] = '&&' 83 BOOLOP_SYMBOLS[ast.Or] 83 BOOLOP_SYMBOLS[ast.Or] = '||' 84 84 85 85 CMPOP_SYMBOLS = {} 86 CMPOP_SYMBOLS[ast.Eq] 86 CMPOP_SYMBOLS[ast.Eq] = '==' 87 87 CMPOP_SYMBOLS[ast.NotEq] = '!=' 88 88 CMPOP_SYMBOLS[ast.Lt] = '<' … … 145 145 self.new_lines = 0 146 146 self.c_proc = [] 147 # for C147 # for C 148 148 self.signature_line = 0 149 149 self.arguments = [] … … 181 181 def add_c_line(self, x): 182 182 string = '' 183 for iin range(self.indentation):183 for _ in range(self.indentation): 184 184 string += (" ") 185 185 string += str(x) … … 188 188 189 189 def add_current_line(self): 190 if (len(self.current_statement) > 0):190 if self.current_statement: 191 191 self.add_c_line(self.current_statement) 192 192 self.current_statement = '' 193 193 194 194 def AddUniqueVar(self, new_var): 195 if ((new_var not in self.C_Vars)):195 if new_var not in self.C_Vars: 196 196 self.C_Vars.append(str(new_var)) 197 197 … … 210 210 self.write_c('# line: %s' % node.lineno) 211 211 self.new_lines = 1 212 if (len(self.current_statement)):212 if self.current_statement: 213 213 self.Statements.append(self.current_statement) 214 214 self.current_statement = '' 215 215 216 216 def body(self, statements): 217 if (len(self.current_statement)):217 if self.current_statement: 218 218 self.add_current_line() 219 219 self.new_line = True 220 220 self.indentation += 1 221 221 for stmt in statements: 222 target_name = '' 223 if(hasattr(stmt, 'targets')): 224 if(hasattr(stmt.targets[0], 'id')): 225 target_name = stmt.targets[0].id # target name needed for debug only 222 #if hasattr(stmt, 'targets') and hasattr(stmt.targets[0], 'id'): 223 # target_name = stmt.targets[0].id # target name needed for debug only 226 224 self.visit(stmt) 227 225 self.add_current_line() # just for breaking point. to be deleted. … … 242 240 else: 243 241 want_comma.append(True) 244 # for C 242 243 # for C 245 244 for arg in node.args: 246 self.arguments.append(arg.arg) 245 # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 246 try: 247 arg_name = arg.arg 248 except AttributeError: 249 arg_name = arg.id 250 self.arguments.append(arg_name) 247 251 248 252 padding = [None] *(len(node.args) - len(node.defaults)) 249 253 for arg, default in zip(node.args, padding + node.defaults): 250 254 if default is not None: 251 self.warnings.append("Default Parameter unknown to C") 252 w_str = "Default Parameters are unknown to C: '" + arg.arg + \ 253 " = " + str(default.n) + "'" 255 # CRUFT: 2.7 uses arg.id, 3.x uses arg.arg 256 try: 257 arg_name = arg.arg 258 except AttributeError: 259 arg_name = arg.id 260 w_str = ("Default Parameters are unknown to C: '%s = %s" 261 % arg_name, str(default.n)) 254 262 self.warnings.append(w_str) 255 263 … … 271 279 272 280 def define_C_Vars(self, target): 273 if (hasattr(target, 'id')):274 # a variable is considered an array if it apears in the agrument list275 # and being assigned to. For example, the variable p in the following276 # sniplet is a pointer, while q is not277 # def somefunc(p, q):278 # p = q + 1279 # return280 #281 if (target.id not in self.C_Vars):282 if (target.id in self.arguments):281 if hasattr(target, 'id'): 282 # a variable is considered an array if it apears in the agrument list 283 # and being assigned to. For example, the variable p in the following 284 # sniplet is a pointer, while q is not 285 # def somefunc(p, q): 286 # p = q + 1 287 # return 288 # 289 if target.id not in self.C_Vars: 290 if target.id in self.arguments: 283 291 idx = self.arguments.index(target.id) 284 292 new_target = self.arguments[idx] + "[0]" 285 if (new_target not in self.C_Pointers):293 if new_target not in self.C_Pointers: 286 294 target.id = new_target 287 295 self.C_Pointers.append(self.arguments[idx]) … … 291 299 def add_semi_colon(self): 292 300 semi_pos = self.current_statement.find(';') 293 if (semi_pos > 0.0):294 self.current_statement = self.current_statement.replace(';', '')301 if semi_pos > 0.0: 302 self.current_statement = self.current_statement.replace(';', '') 295 303 self.write_c(';') 296 304 … … 302 310 self.define_C_Vars(target) 303 311 self.visit(target) 304 if (len(self.Tuples) > 0):312 if self.Tuples: 305 313 tplTargets = list(self.Tuples) 306 self.Tuples.clear()314 del self.Tuples[:] 307 315 self.write_c(' = ') 308 316 self.is_sequence = False … … 317 325 self.add_semi_colon() 318 326 self.add_current_line() 319 if ((self.is_sequence) and (not self.visited_args)):327 if self.is_sequence and not self.visited_args: 320 328 for target in node.targets: 321 if (hasattr(target, 'id')):322 if ((target.id in self.C_Vars) and(target.id not in self.C_DclPointers)):323 if (target.id not in self.C_DclPointers):329 if hasattr(target, 'id'): 330 if target.id in self.C_Vars and target.id not in self.C_DclPointers: 331 if target.id not in self.C_DclPointers: 324 332 self.C_DclPointers.append(target.id) 325 if (target.id in self.C_Vars):333 if target.id in self.C_Vars: 326 334 self.C_Vars.remove(target.id) 327 335 self.current_statement = '' 328 336 329 337 def visit_AugAssign(self, node): 330 if (node.target.id not in self.C_Vars):331 if (node.target.id not in self.arguments):338 if node.target.id not in self.C_Vars: 339 if node.target.id not in self.arguments: 332 340 self.C_Vars.append(node.target.id) 333 341 self.visit(node.target) … … 355 363 self.generic_visit(node) 356 364 357 def listToDeclare(self, Vars): 358 s = '' 359 if(len(Vars) > 0): 360 s = ",".join(Vars) 361 return(s) 365 def listToDeclare(self, vars): 366 return ", ".join(vars) 362 367 363 368 def write_C_Pointers(self, start_var): 364 if (len(self.C_DclPointers) > 0):365 var s = ""369 if self.C_DclPointers: 370 var_list = [] 366 371 for c_ptr in self.C_DclPointers: 367 372 if(len(vars) > 0): 368 373 vars += ", " 369 if(c_ptr not in self.arguments): 370 vars += "*" + c_ptr 371 if(c_ptr in self.C_Vars): 372 if(c_ptr in self.C_Vars): 373 self.C_Vars.remove(c_ptr) 374 if(len(vars) > 0): 375 c_dcl = " double " + vars + ";" 376 self.c_proc.insert(start_var, c_dcl + "\n") 374 if c_ptr not in self.arguments: 375 var_list.append("*" + c_ptr) 376 if c_ptr in self.C_Vars: 377 self.C_Vars.remove(c_ptr) 378 if var_list: 379 c_dcl = " double " + ", ".join(var_list) + ";\n" 380 self.c_proc.insert(start_var, c_dcl) 377 381 start_var += 1 378 382 return start_var … … 381 385 fLine = False 382 386 start_var = self.write_C_Pointers(start_var) 383 if (len(self.C_IntVars) > 0):387 if self.C_IntVars: 384 388 for var in self.C_IntVars: 385 if (var in self.C_Vars):389 if var in self.C_Vars: 386 390 self.C_Vars.remove(var) 387 391 s = self.listToDeclare(self.C_IntVars) … … 390 394 start_var += 1 391 395 392 if (len(self.C_Vars) > 0):396 if self.C_Vars: 393 397 s = self.listToDeclare(self.C_Vars) 394 398 self.c_proc.insert(start_var, " double " + s + ";\n") 395 399 fLine = True 396 400 start_var += 1 397 if(len(self.C_Vectors) > 0): 401 402 if self.C_Vectors: 398 403 s = self.listToDeclare(self.C_Vectors) 399 404 for n in range(len(self.C_Vectors)): … … 402 407 self.c_proc.insert(start_var, c_dcl + "\n") 403 408 start_var += 1 404 self.C_Vars.clear() 405 self.C_IntVars.clear() 406 self.C_Vectors.clear() 407 self.C_Pointers.clear() 409 410 del self.C_Vars[:] 411 del self.C_IntVars[:] 412 del self.C_Vectors[:] 413 del self.C_Pointers[:] 408 414 self.C_DclPointers 409 if (fLine == True):415 if fLine: 410 416 self.c_proc.insert(start_var, "\n") 411 return412 s = ''413 for n in range(len(self.C_Vars)):414 s += str(self.C_Vars[n])415 if n < len(self.C_Vars) - 1:416 s += ", "417 if(len(s) > 0):418 self.c_proc.insert(start_var, " double " + s + ";\n")419 self.c_proc.insert(start_var + 1, "\n")420 421 def writeInclude(self):422 if(self.MathIncludeed == False):423 self.add_c_line("#include <math.h>\n")424 self.add_c_line("static double pi = 3.14159265359;\n")425 self.MathIncludeed = True426 427 def ListToString(self, strings):428 s = ''429 for n in range(len(strings)):430 s += strings[n]431 if(n < (len(strings) - 1)):432 s += ", "433 return(s)434 435 def getMethodSignature(self):436 args_str = ''437 for n in range(len(self.arguments)):438 args_str += "double " + self.arguments[n]439 if(n < (len(self.arguments) - 1)):440 args_str += ", "441 return(args_str)442 417 443 418 def InsertSignature(self): 444 arg s_str = ''445 for n in range(len(self.arguments)):446 args_str += "double " + self.arguments[n]447 if (self.arguments[n] in self.C_Pointers):448 args_str+= "[]"449 if(n < (len(self.arguments) - 1)):450 args_str += ", "419 arg_decls = [] 420 for arg in self.arguments: 421 decl = "double " + arg 422 if arg in self.C_Pointers: 423 decl += "[]" 424 arg_decls.append(decl) 425 args_str = ", ".join(arg_decls) 451 426 self.strMethodSignature = 'double ' + self.name + '(' + args_str + ")" 452 if (self.signature_line >= 0):427 if self.signature_line >= 0: 453 428 self.c_proc.insert(self.signature_line, self.strMethodSignature) 454 429 … … 459 434 self.arguments = [] 460 435 self.name = node.name 461 print("Parsing '" + self.name + "'") 462 args_str = "" 436 #if self.name not in self.required_functions[0]: 437 # return 438 #print("Parsing '" + self.name + "'") 463 439 464 440 self.visit(node.args) 465 self.getMethodSignature()441 # for C 466 442 self.signature_line = len(self.c_proc) 443 #self.add_c_line(self.strMethodSignature) 467 444 self.add_c_line("\n{") 468 445 start_vars = len(self.c_proc) + 1 … … 489 466 paren_or_comma() 490 467 self.visit(base) 491 # XXX: the if here is used to keep this module compatible 492 # with python 2.6. 468 # CRUFT: python 2.6 does not have "keywords" attribute 493 469 if hasattr(node, 'keywords'): 494 470 for keyword in node.keywords: … … 517 493 if len(else_) == 0: 518 494 break 519 #elif hasattr(else_, 'orelse'):495 #elif hasattr(else_, 'orelse'): 520 496 elif len(else_) == 1 and isinstance(else_[0], ast.If): 521 497 node = else_[0] 522 #self.newline()498 #self.newline() 523 499 self.write_c('else if ') 524 500 self.visit(node.test) … … 527 503 self.add_current_line() 528 504 self.add_c_line('}') 529 #break505 #break 530 506 else: 531 507 self.newline() … … 537 513 def getNodeLineNo(self, node): 538 514 line_number = -1 539 if (hasattr(node,'value')):515 if hasattr(node, 'value'): 540 516 line_number = node.value.lineno 541 517 elif hasattr(node, 'iter'): 542 518 if hasattr(node.iter, 'lineno'): 543 519 line_number = node.iter.lineno 544 return (line_number)520 return line_number 545 521 546 522 def GetNodeAsString(self, node): 547 523 res = '' 548 if (hasattr(node, 'n')):524 if hasattr(node, 'n'): 549 525 res = str(node.n) 550 elif (hasattr(node, 'id')):526 elif hasattr(node, 'id'): 551 527 res = node.id 552 return (res)528 return res 553 529 554 530 def GetForRange(self, node): … … 564 540 self.current_statement = '' 565 541 self.current_statement = temp_statement 566 if (len(for_args) == 1):542 if len(for_args) == 1: 567 543 stop = for_args[0] 568 elif (len(for_args) == 2):544 elif len(for_args) == 2: 569 545 start = for_args[0] 570 546 stop = for_args[1] 571 elif (len(for_args) == 3):547 elif len(for_args) == 3: 572 548 start = for_args[0] 573 549 stop = for_args[1] … … 575 551 else: 576 552 raise("Ilegal for loop parameters") 577 return (start, stop, step)553 return start, stop, step 578 554 579 555 def visit_For(self, node): 580 # node: for iterator is stored in node.target.581 # Iterator name is in node.target.id.556 # node: for iterator is stored in node.target. 557 # Iterator name is in node.target.id. 582 558 self.add_current_line() 583 559 fForDone = False 584 560 self.current_statement = '' 585 if (hasattr(node.iter, 'func')):586 if (hasattr(node.iter.func, 'id')):587 if (node.iter.func.id == 'range'):561 if hasattr(node.iter, 'func'): 562 if hasattr(node.iter.func, 'id'): 563 if node.iter.func.id == 'range': 588 564 self.visit(node.target) 589 565 iterator = self.current_statement 590 566 self.current_statement = '' 591 if (iterator not in self.C_IntVars):567 if iterator not in self.C_IntVars: 592 568 self.C_IntVars.append(iterator) 593 569 start, stop, step = self.GetForRange(node) 594 self.write_c("for(" + iterator + "=" + str(start) + \595 " ; " + iterator + " < " + str(stop) + \596 570 self.write_c("for(" + iterator + "=" + str(start) + 571 " ; " + iterator + " < " + str(stop) + 572 " ; " + iterator + " += " + str(step) + ") {") 597 573 self.body_or_else(node) 598 574 self.write_c("}") 599 575 fForDone = True 600 if (fForDone == False):576 if not fForDone: 601 577 line_number = self.getNodeLineNo(node) 602 578 self.current_statement = '' … … 632 608 633 609 def visit_Print(self, node): 634 # XXX: python 2.6 only610 # CRUFT: python 2.6 only 635 611 self.newline(node) 636 612 self.write_c('print ') … … 700 676 701 677 def visit_Raise(self, node): 702 # XXX: Python 2.6 / 3.0 compatibility678 # CRUFT: Python 2.6 / 3.0 compatibility 703 679 self.newline(node) 704 680 self.write_python('raise') … … 734 710 else: 735 711 want_comma.append(True) 736 if (hasattr(node.func, 'id')):737 if (node.func.id not in self.C_Functions):712 if hasattr(node.func, 'id'): 713 if node.func.id not in self.C_Functions: 738 714 self.C_Functions.append(node.func.id) 739 if (node.func.id == 'abs'):715 if node.func.id == 'abs': 740 716 self.write_c("fabs ") 741 elif (node.func.id == 'int'):717 elif node.func.id == 'int': 742 718 self.write_c('(int) ') 743 elif (node.func.id == "SINCOS"):719 elif node.func.id == "SINCOS": 744 720 self.WriteSincos(node) 745 721 return … … 772 748 def visit_Name(self, node): 773 749 self.write_c(node.id) 774 if ((node.id in self.C_Pointers) and(not self.SubRef)):750 if node.id in self.C_Pointers and not self.SubRef: 775 751 self.write_c("[0]") 776 752 name = "" 777 753 sub = node.id.find("[") 778 if (sub > 0):754 if sub > 0: 779 755 name = node.id[0:sub].strip() 780 756 else: 781 757 name = node.id 782 #add variable to C_Vars if it ins't there yet, not an argument and not a number783 if ( (name not in self.C_Functions) and (name not in self.C_Vars) and \784 (name not in self.C_IntVars) and (name not in self.arguments) and \785 (name not in self.C_Constants) and (name.isnumeric() == False)):786 if (self.InSubscript):758 # add variable to C_Vars if it ins't there yet, not an argument and not a number 759 if (name not in self.C_Functions and name not in self.C_Vars and 760 name not in self.C_IntVars and name not in self.arguments and 761 name not in self.C_Constants and not name.isdigit()): 762 if self.InSubscript: 787 763 self.C_IntVars.append(node.id) 788 764 else: … … 810 786 s = "" 811 787 for idx, item in enumerate(node.elts): 812 if ((idx > 0) and(len(s) > 0)):788 if idx > 0 and s: 813 789 s += ', ' 814 if (hasattr(item, 'id')):790 if hasattr(item, 'id'): 815 791 s += item.id 816 elif (hasattr(item, 'n')):792 elif hasattr(item, 'n'): 817 793 s += str(item.n) 818 if (len(s) > 0):794 if s: 819 795 self.C_Vectors.append(s) 820 796 vec_name = "vec" + str(len(self.C_Vectors)) 821 797 self.write_c(vec_name) 822 vec_name += "#"823 798 return visit 824 799 … … 840 815 function_name = '' 841 816 is_negative_exp = False 842 if (isevaluable(str(self.current_statement))):817 if isevaluable(str(self.current_statement)): 843 818 exponent = eval(string) 844 819 is_negative_exp = exponent < 0 845 820 abs_exponent = abs(exponent) 846 if (abs_exponent == 2):821 if abs_exponent == 2: 847 822 function_name = "square" 848 elif (abs_exponent == 3):823 elif abs_exponent == 3: 849 824 function_name = "cube" 850 elif (abs_exponent == 0.5):825 elif abs_exponent == 0.5: 851 826 function_name = "sqrt" 852 elif (abs_exponent == 1.0/3.0):827 elif abs_exponent == 1.0/3.0: 853 828 function_name = "cbrt" 854 if (function_name == ''):829 if function_name == '': 855 830 function_name = "pow" 856 831 return function_name, is_negative_exp 857 832 858 833 def translate_power(self, node): 859 # get exponent by visiting the right hand argument.834 # get exponent by visiting the right hand argument. 860 835 function_name = "pow" 861 836 temp_statement = self.current_statement 862 # 'visit' functions write the results to the 'current_statement' class memnber863 # Here, a temporary variable, 'temp_statement', is used, that enables the864 # use of the 'visit' function837 # 'visit' functions write the results to the 'current_statement' class memnber 838 # Here, a temporary variable, 'temp_statement', is used, that enables the 839 # use of the 'visit' function 865 840 self.current_statement = '' 866 841 self.visit(node.right) … … 868 843 function_name, is_negative_exp = self.get_special_power(self.current_statement) 869 844 self.current_statement = temp_statement 870 if (is_negative_exp):845 if is_negative_exp: 871 846 self.write_c("1.0 /(") 872 847 self.write_c(function_name + "(") 873 848 self.visit(node.left) 874 if (function_name == "pow"):849 if function_name == "pow": 875 850 self.write_c(", ") 876 851 self.visit(node.right) 877 852 self.write_c(")") 878 if (is_negative_exp):853 if is_negative_exp: 879 854 self.write_c(")") 880 855 self.write_c(" ") … … 889 864 def visit_BinOp(self, node): 890 865 self.write_c("(") 891 if ('%s' % BINOP_SYMBOLS[type(node.op)] == BINOP_SYMBOLS[ast.Pow]):866 if '%s' % BINOP_SYMBOLS[type(node.op)] == BINOP_SYMBOLS[ast.Pow]: 892 867 self.translate_power(node) 893 elif ('%s' % BINOP_SYMBOLS[type(node.op)] == BINOP_SYMBOLS[ast.FloorDiv]):868 elif '%s' % BINOP_SYMBOLS[type(node.op)] == BINOP_SYMBOLS[ast.FloorDiv]: 894 869 self.translate_integer_divide(node) 895 870 else: … … 899 874 self.write_c(")") 900 875 901 #for C876 # for C 902 877 def visit_BoolOp(self, node): 903 878 self.write_c('(') … … 926 901 927 902 def visit_Subscript(self, node): 928 if (node.value.id not in self.C_Constants):929 if (node.value.id not in self.C_Pointers):903 if node.value.id not in self.C_Constants: 904 if node.value.id not in self.C_Pointers: 930 905 self.C_Pointers.append(node.value.id) 931 906 self.SubRef = True … … 976 951 self.visit(comprehension) 977 952 self.write_c(right) 978 #self.write_python(right)953 #self.write_python(right) 979 954 return visit 980 955 … … 1005 980 1006 981 def visit_Repr(self, node): 1007 # XXX: python 2.6 only982 # CRUFT: python 2.6 only 1008 983 self.write_c('`') 1009 984 self.visit(node.value) … … 1021 996 self.visit(node.target) 1022 997 self.write_C(' in ') 1023 #self.write_python(' in ')998 #self.write_python(' in ') 1024 999 self.visit(node.iter) 1025 1000 if node.ifs: … … 1043 1018 print(tree_source) 1044 1019 1045 def add_constants(sniplets, c_constants):1046 sniplets.append("#include <math.h>")1047 sniplets.append("")1048 vars = c_constants.keys()1049 for c_var in vars:1050 c_values = c_constants[c_var]1051 if isinstance(c_values, (int, float)):1052 parts = ["double ", c_var, " = ", "%.15g"%c_values, ";"]1053 else:1054 elements = ["%.15g"%v for v in c_values]1055 parts = ["double ", c_var, "[]", " = ", "{\n ", ", ".join(elements), "\n};"]1056 sniplets.append("".join(parts))1057 1058 1020 def translate(functions, constants=0): 1059 snip lets = []1060 add_constants (sniplets, constants)1061 for source,fname,line_no in functions:1062 line_directive = '#line %d "%s"' %(line_no,fname)1063 line_directive = line_directive.replace('\\','\\\\')1064 # sniplets.append(line_directive)1021 snippets = [] 1022 #snippets.append("#include <math.h>") 1023 #snippets.append("") 1024 for source, fname, line_no in functions: 1025 line_directive = '#line %d "%s"'%(line_no, fname.replace('\\', '\\\\')) 1026 snippets.append(line_directive) 1065 1027 tree = ast.parse(source) 1066 sniplet = to_source(tree, functions, constants) # in the future add filename, offset, constants 1067 sniplets.append(sniplet) 1068 c_code = "\n".join(sniplets) 1069 f_out = open ("xlate.c", "w+") 1070 f_out.write (c_code) 1071 f_out.close() 1072 return("\n".join(sniplets)) 1073 1074 def get_file_names(): 1075 fname_in = "" 1076 fname_out = "" 1077 if(len(sys.argv) > 1): 1078 fname_in = sys.argv[1] 1079 fname_base = os.path.splitext(fname_in) 1080 if(len(sys.argv) == 2): 1081 fname_out = str(fname_base[0]) + '.c' 1082 else: 1083 fname_out = sys.argv[2] 1084 if(len(fname_in) > 0): 1085 python_file = open(sys.argv[1], "r") 1086 if(len(fname_out) > 0): 1087 file_out = open(fname_out, "w+") 1088 return len(sys.argv), fname_in, fname_out 1089 1090 if __name__ == "__main__": 1028 # in the future add filename, offset, constants 1029 c_code = to_source(tree, functions, constants) 1030 snippets.append(c_code) 1031 return snippets 1032 1033 def main(): 1091 1034 import os 1092 1035 print("Parsing...using Python" + sys.version) 1093 try: 1094 fname_in = "" 1095 fname_out = "" 1096 if(len(sys.argv) == 1): 1097 print("Usage:\npython parse01.py <infile> [<outfile>](if omitted, output file is '<infile>.c'") 1098 else: 1099 fname_in = sys.argv[1] 1100 fname_base = os.path.splitext(fname_in) 1101 if(len(sys.argv) == 2): 1102 fname_out = str(fname_base[0]) + '.c' 1103 else: 1104 fname_out = sys.argv[2] 1105 if(len(fname_in) > 0): 1106 python_file = open(sys.argv[1], "r") 1107 if(len(fname_out) > 0): 1108 file_out = open(fname_out, "w+") 1109 functions = ["MultAsgn", "Iq41", "Iq2"] 1110 tpls = [functions, fname_in, 0] 1111 c_txt = translate(tpls) 1112 file_out.write(c_txt) 1113 file_out.close() 1114 except Exception as excp: 1115 print("Error:\n" + str(excp.args)) 1036 if len(sys.argv) == 1: 1037 print("""\ 1038 Usage: python py2c.py <infile> [<outfile>] 1039 1040 if outfile is omitted, output file is '<infile>.c' 1041 """) 1042 return 1043 1044 fname_in = sys.argv[1] 1045 if len(sys.argv) == 2: 1046 fname_base = os.path.splitext(fname_in)[0] 1047 fname_out = str(fname_base) + '.c' 1048 else: 1049 fname_out = sys.argv[2] 1050 1051 with open(fname_in, "r") as python_file: 1052 code = python_file.read() 1053 1054 translation = translate([code, fname_in, 1])[0] 1055 1056 with open(fname_out, "w") as file_out: 1057 file_out.write(translation) 1116 1058 print("...Done") 1059 1060 if __name__ == "__main__": 1061 main()
Note: See TracChangeset
for help on using the changeset viewer.