1#!/usr/bin/python 2 3import sys 4 5def read_levels(): 6 f = open('../../../libs/toollog/include/xentoollog.h', 'r') 7 8 levels = [] 9 record = False 10 for l in f.readlines(): 11 if 'XTL_NUM_LEVELS' in l: 12 break 13 if record == True: 14 levels.append(l.split(',')[0].strip()) 15 if 'XTL_NONE' in l: 16 record = True 17 18 f.close() 19 20 olevels = [level[4:].capitalize() for level in levels] 21 22 return levels, olevels 23 24# .ml 25 26def gen_ml(olevels): 27 s = "" 28 29 s += "type level = \n" 30 for level in olevels: 31 s += '\t| %s\n' % level 32 33 s += "\nlet level_to_string level =\n" 34 s += "\tmatch level with\n" 35 for level in olevels: 36 s += '\t| %s -> "%s"\n' % (level, level) 37 38 s += "\nlet level_to_prio level =\n" 39 s += "\tmatch level with\n" 40 for index,level in enumerate(olevels): 41 s += '\t| %s -> %d\n' % (level, index) 42 43 return s 44 45# .mli 46 47def gen_mli(olevels): 48 s = "" 49 50 s += "type level = \n" 51 for level in olevels: 52 s += '\t| %s\n' % level 53 54 return s 55 56# .c 57 58def gen_c(level): 59 s = "" 60 61 s += "static value Val_level(xentoollog_level c_level)\n" 62 s += "{\n" 63 s += "\tswitch (c_level) {\n" 64 s += "\tcase XTL_NONE: /* Not a real value */\n" 65 s += '\t\tcaml_raise_sys_error(caml_copy_string("Val_level XTL_NONE"));\n' 66 s += "\t\tbreak;\n" 67 68 for index,level in enumerate(levels): 69 s += "\tcase %s:\n\t\treturn Val_int(%d);\n" % (level, index) 70 71 s += """\tcase XTL_NUM_LEVELS: /* Not a real value! */ 72 \t\tcaml_raise_sys_error( 73 \t\t\tcaml_copy_string("Val_level XTL_NUM_LEVELS")); 74 #if 0 /* Let the compiler catch this */ 75 \tdefault: 76 \t\tcaml_raise_sys_error(caml_copy_string("Val_level Unknown")); 77 \t\tbreak; 78 #endif 79 \t} 80 \tabort(); 81 } 82 """ 83 84 return s 85 86def autogen_header(open_comment, close_comment): 87 s = open_comment + " AUTO-GENERATED FILE DO NOT EDIT " + close_comment + "\n" 88 s += open_comment + " autogenerated by \n" 89 s += reduce(lambda x,y: x + " ", range(len(open_comment + " ")), "") 90 s += "%s" % " ".join(sys.argv) 91 s += "\n " + close_comment + "\n\n" 92 return s 93 94if __name__ == '__main__': 95 if len(sys.argv) < 3: 96 print >>sys.stderr, "Usage: genlevels.py <mli> <ml> <c-inc>" 97 sys.exit(1) 98 99 levels, olevels = read_levels() 100 101 _mli = sys.argv[1] 102 mli = open(_mli, 'w') 103 mli.write(autogen_header("(*", "*)")) 104 105 _ml = sys.argv[2] 106 ml = open(_ml, 'w') 107 ml.write(autogen_header("(*", "*)")) 108 109 _cinc = sys.argv[3] 110 cinc = open(_cinc, 'w') 111 cinc.write(autogen_header("/*", "*/")) 112 113 mli.write(gen_mli(olevels)) 114 mli.write("\n") 115 116 ml.write(gen_ml(olevels)) 117 ml.write("\n") 118 119 cinc.write(gen_c(levels)) 120 cinc.write("\n") 121 122 ml.write("(* END OF AUTO-GENERATED CODE *)\n") 123 ml.close() 124 mli.write("(* END OF AUTO-GENERATED CODE *)\n") 125 mli.close() 126 cinc.close() 127 128