bootstrap.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. ### Configuration for creating the bootstrap model using conformance_bottom.
  2. import glob
  3. import hashlib
  4. def bootstrap():
  5. root = ["__hierarchy"]
  6. bootstrap_files = glob.glob("bootstrap/*.alc")
  7. user_data = [ "input",
  8. "output",
  9. "globals",
  10. "frame",
  11. ]
  12. user_frame = [ "evalstack",
  13. "symbols",
  14. "returnvalue",
  15. ]
  16. primitives = { "integer_addition": ["Integer", "Integer", "Integer"],
  17. "integer_subtraction": ["Integer", "Integer", "Integer"],
  18. "integer_multiplication": ["Integer", "Integer", "Integer"],
  19. "integer_division": ["Integer", "Integer", "Integer"],
  20. "integer_gt": ["Boolean", "Integer", "Integer"],
  21. "integer_lt": ["Boolean", "Integer", "Integer"],
  22. "integer_neg": ["Integer", "Integer"],
  23. "float_addition": ["Float", "Float", "Float"],
  24. "float_subtraction": ["Float", "Float", "Float"],
  25. "float_multiplication": ["Float", "Float", "Float"],
  26. "float_division": ["Float", "Float", "Float"],
  27. "float_gt": ["Boolean", "Float", "Float"],
  28. "float_lt": ["Boolean", "Float", "Float"],
  29. "float_neg": ["Float", "Float"],
  30. "bool_and": ["Boolean", "Boolean", "Boolean"],
  31. "bool_or": ["Boolean", "Boolean", "Boolean"],
  32. "bool_not": ["Boolean", "Boolean"],
  33. "string_join": ["String", "String", "String"],
  34. "string_get": ["String", "String", "Integer"],
  35. "string_len": ["Integer", "String"],
  36. "string_split": ["Element", "String", "String"],
  37. "value_eq": ["Boolean", "Element", "Element"],
  38. "value_neq": ["Boolean", "Element", "Element"],
  39. "cast_i2f": ["Float", "Integer"],
  40. "cast_i2s": ["String", "Integer"],
  41. "cast_i2b": ["Boolean", "Integer"],
  42. "cast_f2i": ["Integer", "Float"],
  43. "cast_f2b": ["Boolean", "Float"],
  44. "cast_f2s": ["String", "Float"],
  45. "cast_s2i": ["Integer", "String"],
  46. "cast_s2f": ["Float", "String"],
  47. "cast_s2b": ["Boolean", "String"],
  48. "cast_b2i": ["Integer", "Boolean"],
  49. "cast_b2f": ["Float", "Boolean"],
  50. "cast_b2s": ["String", "Boolean"],
  51. "cast_e2s": ["String", "Element"],
  52. "cast_a2s": ["String", "Action"],
  53. "cast_v2s": ["String", "Element"],
  54. "cast_id2s": ["String", "Element"],
  55. "list_read": ["Element", "Element", "Integer"],
  56. "list_append": ["Element", "Element", "Element"],
  57. "list_insert": ["Element", "Element", "Integer", "Element"],
  58. "list_delete": ["Element", "Element", "Integer"],
  59. "list_len": ["Integer", "Element"],
  60. "dict_add": ["Element", "Element", "Element", "Element"],
  61. "dict_delete": ["Element", "Element", "Element"],
  62. "dict_read": ["Element", "Element", "Element"],
  63. "dict_read_edge": ["Element", "Element", "Element"],
  64. "dict_read_node": ["Element", "Element", "Element"],
  65. "dict_len": ["Integer", "Element"],
  66. "dict_in": ["Boolean", "Element", "Element"],
  67. "dict_in_node": ["Boolean", "Element", "Element"],
  68. "dict_keys": ["Element", "Element"],
  69. "set_add": ["Element", "Element", "Element"],
  70. "set_pop": ["Element", "Element"],
  71. "set_remove": ["Element", "Element", "Element"],
  72. "set_remove_node": ["Element", "Element", "Element"],
  73. "set_in": ["Boolean", "Element", "Element"],
  74. "set_in_node": ["Boolean", "Element", "Element"],
  75. "is_physical_int": ["Boolean", "Element"],
  76. "is_physical_boolean": ["Boolean", "Element"],
  77. "is_physical_string": ["Boolean", "Element"],
  78. "is_physical_action": ["Boolean", "Element"],
  79. "is_physical_float": ["Boolean", "Element"],
  80. "create_node": ["Element"],
  81. "create_edge": ["Element", "Element", "Element"],
  82. "create_value": ["Element", "Element"],
  83. "is_edge": ["Boolean", "Element"],
  84. "read_nr_out": ["Integer", "Element"],
  85. "read_out": ["Element", "Element", "Integer"],
  86. "read_nr_in": ["Integer", "Element"],
  87. "read_in": ["Element", "Element", "Integer"],
  88. "read_edge_src": ["Element", "Element"],
  89. "read_edge_dst": ["Element", "Element"],
  90. "delete_element": ["Element", "Element"],
  91. "element_eq": ["Boolean", "Element", "Element"],
  92. "element_neq": ["Boolean", "Element", "Element"],
  93. "read_root": ["Element"],
  94. "read_userroot": ["Element"],
  95. "deserialize": ["Element", "String"],
  96. "log": ["String", "String"],
  97. }
  98. initial_user = "user_manager"
  99. initial_user_code = \
  100. '''
  101. include "primitives.alc"
  102. include "user_manager.alh"
  103. Void function __main():
  104. \tElement root
  105. \troot = read_root()
  106. \troot = root["__hierarchy"]["objects"]
  107. \texec(root["bootstrap/user_manager.alc"]["initializers"])
  108. \tuser_management()
  109. '''
  110. code_new_users = \
  111. '''
  112. include "primitives.alc"
  113. include "user_interface.alh"
  114. Void function __main():
  115. \tElement root
  116. \troot = read_root()
  117. \troot = root["__hierarchy"]["objects"]
  118. \texec(root["bootstrap/primitives.alc"]["initializers"])
  119. \texec(root["bootstrap/compilation_manager.alc"]["initializers"])
  120. \texec(root["bootstrap/constructors.alc"]["initializers"])
  121. \texec(root["bootstrap/library.alc"]["initializers"])
  122. \texec(root["bootstrap/object_operations.alc"]["initializers"])
  123. \texec(root["bootstrap/conformance_scd.alc"]["initializers"])
  124. \texec(root["bootstrap/metamodels.alc"]["initializers"])
  125. \texec(root["bootstrap/modelling.alc"]["initializers"])
  126. \texec(root["bootstrap/user_interface.alc"]["initializers"])
  127. \tnew_user()
  128. '''
  129. ### Actual script to generate the file
  130. import os
  131. import sys
  132. class Writer(object):
  133. def __init__(self, file_a, file_b):
  134. self.file_a = file_a
  135. self.file_b = file_b
  136. def write(self, text, both=True):
  137. self.file_a.write(text)
  138. if both:
  139. self.file_b.write(text)
  140. try:
  141. with open("bootstrap/bootstrap.m", "w") as fa:
  142. with open("bootstrap/minimal.m", "w") as fb:
  143. f = Writer(fa, fb)
  144. # Create the root first
  145. f.write("Node root()\n")
  146. # Create all children of the root
  147. for node in root:
  148. f.write("Node %s()\n" % node)
  149. f.write("Edge _%s(root, %s)\n" % (node, node))
  150. f.write('Node __%s("%s")\n' % (node, node))
  151. f.write("Edge ___%s(_%s, __%s)\n" % (node, node, node))
  152. f.write("Node primitives()\n")
  153. f.write("Edge _primitives(__hierarchy, primitives)\n")
  154. f.write('Node __primitives("primitives")\n')
  155. f.write("Edge ___primitives(_primitives, __primitives)\n")
  156. # Define all primitive functions
  157. for function, parameters in primitives.iteritems():
  158. f.write("Node _func_signature_%s()\n" % function)
  159. f.write("Node _func_params_%s()\n" % function)
  160. f.write("Node _func_body_%s()\n" % function)
  161. f.write("Edge _primitives_%s(primitives, _func_signature_%s)\n" % (function, function))
  162. f.write('Node _name_%s("%s")\n' % (function, function))
  163. f.write("Edge _primitives_name_%s(_primitives_%s, _name_%s)\n" % (function, function, function))
  164. f.write('Node _body_%s("body")\n' % function)
  165. f.write("Edge _signature_body_%s(_func_signature_%s, _func_body_%s)\n" % (function, function, function))
  166. f.write("Edge _signature_body_str_%s(_signature_body_%s, _body_%s)\n" % (function, function, function))
  167. f.write('Node _params_%s("params")\n' % function)
  168. f.write("Edge _signature_params_%s(_func_signature_%s, _func_params_%s)\n" % (function, function, function))
  169. f.write("Edge _signature_params_str_%s(_signature_params_%s, _params_%s)\n" % (function, function, function))
  170. parameter_names = "abcdefghijklmnopqrstuvwxyz"
  171. for number, param in enumerate(parameters[1:]):
  172. param_encoding = "%s_%s" % (function, parameter_names[number])
  173. f.write("Node _func_params_%s()\n" % (param_encoding))
  174. f.write('Node _name_%s("%s")\n' % (param_encoding, parameter_names[number]))
  175. f.write("Edge _param_link_%s(_func_params_%s, _func_params_%s)\n" % (param_encoding, function, param_encoding))
  176. f.write("Edge _param_link_str_%s(_param_link_%s, _name_%s)\n" % (param_encoding, param_encoding, param_encoding))
  177. f.write('Node _name_str_%s("name")\n' % param_encoding)
  178. f.write("Edge _param_name_%s(_func_params_%s, _name_%s)\n" % (param_encoding, param_encoding, param_encoding))
  179. f.write("Edge _param_name_str_%s(_param_name_%s, _name_str_%s)\n" % (param_encoding, param_encoding, param_encoding))
  180. # Create the initial user
  181. f.write("Node user_root()\n")
  182. for data in user_data:
  183. f.write("Node user_%s()\n" % data)
  184. f.write('Node ___user_%s("%s")\n' % (data, data))
  185. f.write("Edge _user_%s(user_root, user_%s)\n" % (data, data))
  186. f.write("Edge __user_%s(_user_%s, ___user_%s)\n" % (data, data, data))
  187. for data in user_frame:
  188. f.write("Node user_%s()\n" % data)
  189. f.write('Node ___user_%s("%s")\n' % (data, data))
  190. f.write("Edge _user_%s(user_frame, user_%s)\n" % (data, data))
  191. f.write("Edge __user_%s(_user_%s, ___user_%s)\n" % (data, data, data))
  192. # Add last_input and last_output links
  193. for data in ["input", "output"]:
  194. f.write('Node ___user_last_%s("last_%s")\n' % (data, data))
  195. f.write("Edge _user_last_%s(user_root, user_%s)\n" % (data, data))
  196. f.write("Edge __user_last_%s(_user_last_%s, ___user_last_%s)\n" % (data, data, data))
  197. # Bind user to the root
  198. f.write('Node ___new_user("%s")\n' % initial_user)
  199. f.write("Edge _new_user(root, user_root)\n")
  200. f.write("Edge __new_user(_new_user, ___new_user)\n")
  201. def compile_code_AL(code, target, prepend="", is_file=False, main=False):
  202. import sys
  203. sys.path.append("interface/HUTN/")
  204. from hutn_compiler.compiler import main as compile_code
  205. if not is_file:
  206. with open("bootstrap/bootstrap.al", "w") as f:
  207. f.write(code)
  208. filename = "bootstrap/bootstrap.al"
  209. else:
  210. filename = code
  211. code = compile_code(filename, "interface/HUTN/grammars/actionlanguage.g", "BS", ["--debug", "--prepend:%s" % prepend, "--main" if main else "--not-main"])
  212. if not is_file:
  213. os.remove("bootstrap/bootstrap.al")
  214. return code.replace("auto_initial_IP", target)
  215. # Create all library code
  216. # But first create the structure to hold compiled data
  217. f.write("Node __objects()\n", both=False)
  218. f.write('Node __objects_name("objects")\n', both=False)
  219. f.write("Edge __obj_link(__hierarchy, __objects)\n", both=False)
  220. f.write("Edge _name_obj_link(__obj_link, __objects_name)\n", both=False)
  221. # Compile all files and add to structure manually
  222. for bootstrap_file in bootstrap_files:
  223. # Compile the subfile
  224. print("[COMP] %s" % bootstrap_file)
  225. result = compile_code_AL(bootstrap_file, "initial_IP", prepend=bootstrap_file, is_file=True)
  226. f.write(result, both=False)
  227. # Now link the code with the compilation manager structure
  228. f.write("Node elem()\n", both=False)
  229. f.write('Node initializers("initializers")\n', both=False)
  230. f.write('Node hash("hash_md5")\n', both=False)
  231. f.write("Edge _(__objects, elem)\n", both=False)
  232. f.write('Node filename("%s")\n' % bootstrap_file, both=False)
  233. f.write("Edge _(_, filename)\n", both=False)
  234. f.write("Edge _(elem, %s_initial_IP)\n" % bootstrap_file, both=False)
  235. f.write("Edge _(_, initializers)\n", both=False)
  236. md5 = hashlib.md5()
  237. md5.update(open(bootstrap_file, 'r').read())
  238. f.write('Node hash_value("%s")\n' % md5.hexdigest(), both=False)
  239. f.write("Edge _(elem, hash_value)\n", both=False)
  240. f.write("Edge _(_, hash)\n", both=False)
  241. #TODO add the symbols as well!
  242. # Create code for initial user
  243. print("[BOOT] initial_user")
  244. f.write(compile_code_AL(initial_user_code, "initial_IP", prepend="user_manager", main=True), both=False)
  245. f.write('Node _IP_str("IP")\n', both=False)
  246. f.write("Edge _user_frame(user_frame, user_manager_initial_IP)\n", both=False)
  247. f.write("Edge __user_frame(_user_frame, _IP_str)\n", both=False)
  248. f.write('Node __phase("init")\n', both=False)
  249. f.write('Node __phase_str("phase")\n', both=False)
  250. f.write("Edge _user_phase(user_frame, __phase)\n", both=False)
  251. f.write("Edge __user_phase(_user_phase, __phase_str)\n", both=False)
  252. # Create code for new users to start at
  253. print("[BOOT] new_user")
  254. f.write(compile_code_AL(code_new_users, "initial_IP", prepend="new_user", main=True), both=False)
  255. f.write('Node __IP_str("__IP")\n', both=False)
  256. f.write("Edge _user_IP(__hierarchy, new_user_initial_IP)\n", both=False)
  257. f.write("Edge __user_IP(_user_IP, __IP_str)\n", both=False)
  258. except:
  259. os.remove("bootstrap/bootstrap.m")
  260. os.remove("bootstrap/minimal.m")
  261. raise