home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 15 / AACD15.ISO / AACD / Programming / Python2 / Python20_source / Amiga_Misc / unused / ConvertFD.py < prev    next >
Encoding:
Python Source  |  1996-02-21  |  3.8 KB  |  157 lines

  1. #######
  2. #
  3. #    ConvertFD.py    -    ©1996 Irmen de Jong
  4. #
  5. #######
  6. #
  7. #    Usage:    python ConvertFD.py libbasename ...
  8. #
  9. #    for example: python ConvertFD.py gadtools icon utility
  10. #    will generate library module files in RAM: for each of the 3 libraries.
  11. #
  12. #    This program converts Amiga FD files (containing library function
  13. #    descriptions) to Python modules. It parses the FD's and builds
  14. #    correct LVO tables, including the "magic number" describing the
  15. #    function parameters (which register must take a value).
  16. #    The following is generated:
  17. #    - LVO table
  18. #    - Declaration of exception for the module
  19. #    - General 'call' function
  20. #
  21. #    This program is slightly similar to `fd2pragma'.
  22. #
  23. #    You must have FD: assigned to where you keep your FD files.
  24. #    Also, check the version number of the library. Default is 37,
  25. #    which might raise problems with 3rd party libraries.
  26. #
  27. #    $VER: ConvertFD 1.0 (16.2.96)
  28. #
  29. ######
  30.  
  31. default_libver = 37
  32.  
  33. import regex
  34.  
  35. # regular expression matching FD:
  36. lvoregx=regex.compile("\([a-zA-Z0-9_]+\)(\([a-zA-Z0-9_,]*\))(\([aAdD0-9,/]*\))")
  37.  
  38.  
  39. def fdfile(lib):     return "FD:"+lib+"_lib.fd"
  40. def libname(lib):    return lib+".library"
  41. def capitalize(str): return chr(ord(str[0])&~32)+str[1:]
  42.  
  43. # convert register string to register number:
  44. def convreg(str):
  45.     if str[0]=='d' or str[0]=='D':
  46.         return 1<<(ord(str[1])-48)
  47.     elif str[0]=='a' or str[0]=='A':
  48.         return 1<<(ord(str[1])-40)
  49.     else:
  50.         raise ValueError,"Illegal register spec"
  51.  
  52. # convert FD register list to "magic number":
  53. def str2reg(regspec):
  54.     if len(regspec)>1:
  55.         if regspec[0]==',' or regspec[0]=='/':
  56.             return convreg(regspec[1:]) | str2reg(regspec[3:])
  57.         else:
  58.             return convreg(regspec) | str2reg(regspec[2:])
  59.     else:
  60.         return 0
  61.  
  62. # convert FD file to Python "pragma" file (library module):
  63. def fd2pragma(fdfile):
  64.  
  65.     offs=0;    cont=1; priv=0;    dict={}
  66.  
  67.     fdfh=open(fdfile,"r")
  68.  
  69.     while cont:
  70.         l=fdfh.readline()
  71.         if l: 
  72.             if l[0]=='*':
  73.                 continue
  74.             elif l[0]=='#':
  75.                 # control func, like ##bias
  76.                 if l[2:5]=='end':
  77.                     cont=0
  78.                 elif l[2:6]=='bias':
  79.                     offs=-eval(l[7:])
  80.                 elif l[2:8]=='public' :
  81.                     priv=0
  82.                 elif l[2:9]=='private' :
  83.                     priv=1
  84.  
  85.             else:
  86.                 if not priv:
  87.                     # public library func
  88.                     if lvoregx.match(l) < 0:
  89.                         raise ValueError, "wrong FD syntax"
  90.  
  91.                     r=lvoregx.regs
  92.                     a,b=r[1]
  93.                     c,d=r[3]
  94.                     dict[l[a:b]]=(offs,str2reg(l[c:d]))
  95.  
  96.                 offs=offs-6
  97.         else:
  98.             cont=0
  99.     
  100.  
  101.     fdfh.close()
  102.     return dict
  103.  
  104. # output the generated LVO dictionary:
  105. def printdict(dict,output):
  106.     output.write("LVO = {\n")
  107.  
  108.     k=dict.keys()
  109.     k.sort()
  110.     for i in k:
  111.         output.write(repr(i)+":"+repr(dict[i])+",\n")
  112.  
  113.     output.write("}\n")
  114.  
  115. # convert. The main function.
  116. def cvt(lib,outfile):
  117.  
  118.     try:
  119.         dic=fd2pragma(fdfile(lib))
  120.     except IOError:
  121.         print "Can't open FD file",fdfile(lib)
  122.         return
  123.  
  124.     output=open(outfile,"w")
  125.  
  126.     output.write("#\n# AMIGA LIBRARY INTERFACE FOR "+libname(lib)+"\n#\n")
  127.     output.write("# Generated from FD file "+fdfile(lib)+" by ConvertFD.py\n#\n\n")
  128.     output.write("# LVO definitions:\n\n")
  129.  
  130.     printdict(dic,output)
  131.  
  132.     output.write("\n\nimport amigalibs\nlibname='"+libname(lib)+"'\n\n")
  133.     output.write("lib = amigalibs.openlib(libname,"+`default_libver`+")\n\n")
  134.  
  135.     output.write("# Exception for this library:\n"+capitalize(lib)+
  136.                  "libError='"+capitalize(lib)+"libError'\n")
  137.     output.write("\n# General libcall interface:\n")
  138.     output.write("def call(func,args):\n")
  139.     output.write("\ttry:\n\t\treturn lib.call(LVO[func],args)\n")
  140.     output.write("\texcept KeyError:\n\t\traise NameError,func+' not found in '+libname\n")
  141.  
  142.     output.write("\n################ USER CODE FOLLOWS\n\n")
  143.     output.close()
  144.  
  145.  
  146. # start if executed as script:
  147.  
  148. import sys
  149.  
  150. if __name__ == '__main__':
  151.     if not sys.argv[1:]: # No arguments
  152.         sys.stderr.write('usage: ' + sys.argv[0] + ' libbasename ...\n')
  153.         sys.exit(2)
  154.     for arg in sys.argv[1:]:
  155.         print "Converting",arg,"..."
  156.         cvt(arg,"RAM:"+arg+"lib.py")
  157.