/deprecated.py

https://github.com/shoaibkamil/kdt-specializer · Python · 144 lines · 126 code · 3 blank · 15 comment · 5 complexity · af6ac75e35e0bb15943a06a4ab6e6608 MD5 · raw file

  1. ####
  2. ####
  3. # This file is a temporary placeholder for (potentially) deprecated code taken from the specializer,
  4. # mostly for when it couldn't support using the Asp infrastructure.
  5. ####
  6. ####
  7. class IfNotDefined(cpp_ast.Generable):
  8. """
  9. A generable AST node for the 'if not defined' (#ifndef) directive.
  10. Accepts argument 'symbol', the token to check for defined status.
  11. """
  12. def __init__(self, symbol):
  13. self.symbol = symbol
  14. def generate(self):
  15. yield "#ifndef %s" % self.symbol
  16. class EndIf(cpp_ast.Generable):
  17. """
  18. A generable AST node for the 'end if' (#endif) directive.
  19. """
  20. def generate(self):
  21. yield "#endif"
  22. class CMakeModule(object):
  23. """
  24. This module is a (still somewhat hacky) mimic of the style of CodePy's Boost
  25. Python module in order to add support for including ASP-generated code in
  26. projects which use GNU make for a build system.
  27. Note that the compile() member method is specific to the pyCombBLAS project
  28. makefile that accepts a DYNFILE= command line argument, the filename of a
  29. dynamically generated file.
  30. Arguments:
  31. temp_dir - Directory to store dynamically generated cpp, header, and SWIG interface files.
  32. makefile_dir - Directory of the makefile.
  33. name - A name given to the generated files.
  34. namespace - A namespace to include all code generated in.
  35. include_files - A list of files to #include at the top of the header and cpp files.
  36. """
  37. def __init__(self, temp_dir, makefile_dir, name="module", namespace=None, include_files=[]):
  38. self.name = name
  39. self.preamble = []
  40. self.mod_body = []
  41. self.header_body = []
  42. self.namespace = namespace
  43. self.temp_dir = temp_dir
  44. self.makefile_dir = makefile_dir
  45. self.include_files = include_files
  46. def include_file(self, filepath):
  47. self.include_files.append(filepath)
  48. def add_to_preamble(self, pa):
  49. self.preamble.extend(pa)
  50. def add_to_module(self, body):
  51. self.mod_body.extend(body)
  52. def add_function(self, func):
  53. """*func* is a :class:`cgen.FunctionBody`."""
  54. self.mod_body.append(func)
  55. # Want the prototype for the function added to the header.
  56. self.header_body.append(func.fdecl)
  57. def add_struct(self, struct):
  58. self.mod_body.append(struct)
  59. def generate(self):
  60. source = []
  61. if self.namespace is not None:
  62. self.mod_body = [Namespace(self.namespace, cpp_ast.Block(self.mod_body))]
  63. print "Got to 1"
  64. self.preamble += [cpp_ast.Include(self.temp_dir+self.name+".h", system=False)]
  65. for include in self.include_files:
  66. self.preamble += [cpp_ast.Include(include, system=False)]
  67. print "Got to 2"
  68. source += self.preamble + [codepy.cgen.Line()] + self.mod_body
  69. print "Got to 3"
  70. return codepy.cgen.Module(source)
  71. def generate_header(self):
  72. header = []
  73. if self.namespace is not None:
  74. self.header_body = [Namespace(self.namespace, cpp_ast.Block(self.header_body))]
  75. header_top = [IfNotDefined(self.name+"_H"), cpp_ast.Define(self.name+"_H", "")]
  76. for include in self.include_files:
  77. header_top += [cpp_ast.Include(include, system=False)]
  78. header += header_top + self.header_body + [EndIf()]
  79. return codepy.cgen.Module(header)
  80. def generate_swig_interface(self):
  81. interface_string = "%module " + self.name + "\n"
  82. interface_string += "%{\n"
  83. interface_string += str(cpp_ast.Include(self.temp_dir+self.name+".h", system=False))
  84. interface_string += "\n"
  85. interface_string += "%}\n"
  86. interface_string += "".join([str(line) for line in self.header_body])
  87. return interface_string
  88. def compile(self):
  89. from os import getcwd, chdir
  90. from subprocess import call
  91. original_dir = getcwd()
  92. chdir(self.temp_dir)
  93. header_file = open(self.name + ".h", 'w')
  94. print >>header_file, self.generate_header()
  95. header_file.close()
  96. cpp_file = open(self.name + ".cpp", 'w')
  97. print >>cpp_file, self.generate()
  98. cpp_file.close()
  99. i_file = open(self.name + ".i", 'w')
  100. print >>i_file, self.generate_swig_interface()
  101. i_file.close()
  102. chdir(self.makefile_dir)
  103. args = ["make", "DYNFILE="+self.temp_dir+self.name]
  104. call(args)
  105. chdir(original_dir)
  106. class Operator(object):
  107. """
  108. Class to represent the data associated with an operator.
  109. Used a class because empty fields are nicer than with NamedTuple.
  110. """
  111. def __init__(self, name, assoc=None, comm=None, src=None, ast=None):
  112. self.name = name
  113. self.src = src
  114. self.ast = ast
  115. self.assoc = assoc
  116. self.comm = comm