Annotation of python/pyGiNaC/tools/wrap_typedefs.py, revision 1.1

1.1     ! pearu       1: #!/usr/bin/env python
        !             2: 
        !             3: import re
        !             4: 
        !             5: ok_types = [
        !             6:     'int','unsigned int','long','unsigned long','double const','const std::string &',
        !             7:     'bool','unsigned','double','const char *','const cln::cl_N &','void',
        !             8:     'std::ostream &','std::string const &'
        !             9:     ]
        !            10: 
        !            11: def fix_type(a):
        !            12:     if a in ok_types:
        !            13:         return a
        !            14:     m = re.match(r'(?P<before>.*?)\b(?P<type>symbol|ex|numeric|expairseq|exprseq|basic|exvector|epvector|exlist|evalffunctype|matrix|lst|scalar_products|pseries|relational|expair|epplist)\b(?P<rest>.*)',a)
        !            15:     if m:
        !            16:         return m.group('before')+'GiNaC::'+m.group('type')+m.group('rest')
        !            17:     print 'Not fixed:',a
        !            18:     return a
        !            19: 
        !            20: def construct_vector_wrapper(name,ft,t):
        !            21:     return '''\
        !            22: // typedef #name# wrapper
        !            23: struct #name#_wrapper: #ftype# {
        !            24: \t#name#_wrapper(PyObject*, const #ftype#& v) : #ftype#(v) {}
        !            25: \t#name#_wrapper(PyObject* self) : #ftype#() {}
        !            26: \t#name#_wrapper(PyObject* self, const int n) : #ftype#(n) {}
        !            27: \t#name#_wrapper(PyObject* self, const python::tuple seq) : #ftype#()
        !            28: \t{
        !            29: \t\tfor (unsigned int i = 0; i < seq.size(); i++)
        !            30: \t\t\tthis->push_back(BOOST_PYTHON_CONVERSION::from_python(seq[i].get(), python::type< #type# >()));
        !            31: \t}
        !            32: };
        !            33: void #name#_throw_index_error_if(const #ftype#& v, const std::size_t key)
        !            34: {
        !            35: \tif ((key >= v.size()) || (key < 0))
        !            36: \t{
        !            37: \t\tPyErr_SetObject(PyExc_IndexError, BOOST_PYTHON_CONVERSION::to_python(key));
        !            38: \t\tthrow python::error_already_set();
        !            39: \t}
        !            40: }
        !            41: #type# #name#_getitem (const #ftype#& v, const std::size_t key)
        !            42: {
        !            43: \t#name#_throw_index_error_if(v, key);
        !            44: \treturn v[key];
        !            45: }
        !            46: void #name#_setitem(#ftype#& v, const std::size_t key, const #type# &d)
        !            47: {
        !            48: \t#ftype#::iterator viter = v.begin();
        !            49: \t#name#_throw_index_error_if(v, key);
        !            50: \tviter[key] = d;
        !            51: }
        !            52: void #name#_delitem(#ftype# & v, const std::size_t key)
        !            53: {
        !            54: \t#ftype#::iterator viter = v.begin();
        !            55: \t#name#_throw_index_error_if(v, key);
        !            56: \tv.erase(&viter[key]);
        !            57: }
        !            58: '''.replace('#name#',name).replace('#ftype#',ft).replace('#type#',t)
        !            59: def construct_list_wrapper(name,ft,t):
        !            60:     return '''\
        !            61: // typedef #name# wrapper
        !            62: struct #name#_wrapper: #ftype# {
        !            63: \t#name#_wrapper(PyObject*, const #ftype#& v) : #ftype#(v) {}
        !            64: \t#name#_wrapper(PyObject* self) : #ftype#() {}
        !            65: \t#name#_wrapper(PyObject* self, const int n) : #ftype#(n) {}
        !            66: \t#name#_wrapper(PyObject* self, const python::tuple seq) : #ftype#()
        !            67: \t{
        !            68: \t\tfor (unsigned int i = 0; i < seq.size(); i++)
        !            69: \t\t\tthis->push_back(BOOST_PYTHON_CONVERSION::from_python(seq[i].get(),python::type< #type# >()));
        !            70: \t}
        !            71: };
        !            72: void #name#_throw_index_error_if(const #ftype#& v, const std::size_t key)
        !            73: {
        !            74: \tif ((key >= v.size()) || (key < 0))
        !            75: \t{
        !            76: \t\tPyErr_SetObject(PyExc_IndexError, BOOST_PYTHON_CONVERSION::to_python(key));
        !            77: \t\tthrow python::error_already_set();
        !            78: \t}
        !            79: }
        !            80: #type# #name#_getitem (const #ftype#& v, const std::size_t key)
        !            81: {
        !            82: \t#ftype#::const_iterator viter = v.begin();
        !            83: \t#name#_throw_index_error_if(v, key);
        !            84: \tfor(unsigned int i = 0; i < key; i++,viter++);
        !            85: \treturn *viter;
        !            86: }
        !            87: void #name#_setitem(#ftype#& v, const std::size_t key, const #type# &d)
        !            88: {
        !            89: \t#ftype#::iterator viter = v.begin();
        !            90: \t#name#_throw_index_error_if(v, key);
        !            91: \tfor(unsigned int i = 0; i < key; i++,viter++);
        !            92: \tv.insert(viter,d);
        !            93: \tviter = v.begin();
        !            94: \tfor(unsigned int i = 0; i < key; i++,viter++);
        !            95: \tviter++;
        !            96: \tv.erase(viter);
        !            97: }
        !            98: void #name#_delitem(#ftype# & v, const std::size_t key)
        !            99: {
        !           100: \t#ftype#::iterator viter = v.begin();
        !           101: \t#name#_throw_index_error_if(v, key);
        !           102: \tfor(unsigned int i = 0; i < key; i++,viter++);
        !           103: \tv.erase(viter);
        !           104: }
        !           105: '''.replace('#name#',name).replace('#ftype#',ft).replace('#type#',t)
        !           106: 
        !           107: fin = open('strip_ginac_rest.txt','r')
        !           108: ftodo = open('todo_wtf.txt','w')
        !           109: def todo(l):
        !           110:     ftodo.write(l)
        !           111: 
        !           112: td_m = re.compile(r'typedef\s+(?P<type>.*?)\s+\b(?P<name>\w+)\b\s*;')
        !           113: 
        !           114: defs = {'classes':[],'defs':[],'wrappers':[],'proto':['\t// prototypes of typedef ... wrappers']}
        !           115: ignore = []
        !           116: for l in fin.xreadlines():
        !           117:     m = td_m.match(l.strip())
        !           118:     if m:
        !           119:         n,t = m.group('name'),m.group('type').strip()
        !           120:         if (n,t) in ignore: continue
        !           121:         ignore.append((n,t))
        !           122: 
        !           123:         if (t[:11]=='std::vector' or t[:9]=='std::list') and n in ['exvector','epvector','exlist']:
        !           124:             if t[:11]=='std::vector':
        !           125:                 args = [fix_type(a.strip()) for a in t[12:-1].split(',')]
        !           126:                 ft = 'std::vector<'+', '.join(args)+'>'
        !           127:                 if n=='exvector' and len(args)==2:
        !           128:                     continue
        !           129:             elif t[:9]=='std::list':
        !           130:                 args = [fix_type(a.strip()) for a in t[10:-1].split(',')]
        !           131:                 ft = 'std::list<'+', '.join(args)+'>'
        !           132:                 if n=='exlist' and len(args)==2:
        !           133:                     continue
        !           134:             else:
        !           135:                 assert 0,'unimpl.'
        !           136:             r = '\tpython::class_builder<%s, %s_wrapper> %s_class(this_module, "%s");'%(ft,n,n,n)
        !           137:             defs['classes'].append('\t// typedef %s'%(n))
        !           138:             defs['classes'].append(r)
        !           139:             defs['defs'].append('\t// typedef %s constructors'%(n))
        !           140:             defs['defs'].append('\t%s_class.def(python::constructor<>());'%(n))
        !           141:             defs['defs'].append('\t%s_class.def(python::constructor<const int>());'%(n))
        !           142:             defs['defs'].append('\t%s_class.def(python::constructor<python::tuple>());'%(n))
        !           143:             defs['defs'].append('\t%s_class.def(&%s::size, "__len__");'%(n,ft))
        !           144:             assert len(args)==1
        !           145:             if t[:11]=='std::vector':
        !           146:                 defs['wrappers'].append(construct_vector_wrapper(n,ft,args[0]))
        !           147:             elif t[:9]=='std::list':
        !           148:                 defs['wrappers'].append(construct_list_wrapper(n,ft,args[0]))
        !           149:             else:
        !           150:                 assert 0,'unimpl.'
        !           151:             defs['proto'].append('\tstruct %s_wrapper;'%n)
        !           152:             defs['proto'].append('\t%s %s_getitem(const %s &, const std::size_t);'%(args[0],n,ft))
        !           153:             defs['proto'].append('\tvoid %s_setitem(%s &, const std::size_t, const %s &);'%(n,ft,args[0]))
        !           154:             defs['proto'].append('\tvoid %s_delitem(%s &, const std::size_t);'%(n,ft))
        !           155:             defs['defs'].append('\t%s_class.def(%s_getitem, "__getitem__");'%(n,n))
        !           156:             defs['defs'].append('\t%s_class.def(%s_setitem, "__setitem__");'%(n,n))
        !           157:             defs['defs'].append('\t%s_class.def(%s_delitem, "__delitem__");'%(n,n))
        !           158:             continue
        !           159:         print 'Skipped typedef:',t,n
        !           160:     todo(l)
        !           161: fin.close()
        !           162: ftodo.close()
        !           163: 
        !           164: open('../src/typedef_class._cpp','w').write('\n'.join(defs['classes'])+'\n')
        !           165: open('../src/typedef_method._cpp','w').write('\n'.join(defs['defs'])+'\n')
        !           166: open('../src/typedef_wrapper._cpp','w').write('\n'.join(defs['wrappers'])+'\n')
        !           167: open('../src/typedef_proto._cpp','w').write('\n'.join(defs['proto'])+'\n')

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>