compile_types.py (1523B)
1 #!/usr/bin/env python 2 import os 3 import sys 4 5 from compile_arc import Obj 6 7 8 def toCamelCase(t): 9 return ''.join([w.capitalize() for w in t.split('_')]) 10 11 12 def main(): 13 print """class Archetype(object): 14 def __init__(self, **kw): 15 self.__dict__.update(kw) 16 17 """ 18 19 in_comment = False 20 types = {0: "Archetype"} 21 for line in sys.stdin: 22 if in_comment: 23 i = line.find('*/') 24 if i == -1: 25 continue 26 else: 27 line = line[i+2:] 28 in_comment = False 29 else: 30 i = line.find('/*') 31 if i != -1: 32 j = line.find('*/', i+2) 33 if j != -1: 34 line = line[:i] + line[j+2:] 35 else: 36 line = line[:i] 37 in_comment = True 38 39 i = line.find('//') 40 if i != -1: 41 line = line[:i] 42 43 line = line.strip() 44 if not line: 45 continue 46 47 l = line.split() 48 if len(l) != 3: 49 raise ValueError, "Wrong number of tokens: {0!r}".format(l) 50 51 if l[0] != '#define': 52 raise ValueError, "Got line that doesn't start with #define: {0!r}".format(l) 53 54 t = toCamelCase(l[1]) 55 n = int(l[2]) 56 print """class {cls}(Archetype): 57 pass 58 59 """.format(cls=t) 60 61 types[n] = t 62 63 print "types = {" 64 for n in sorted(types): 65 print " {0}: {1},".format(n, types[n]) 66 67 print " }" 68 69 70 if __name__ == '__main__': 71 main()