bootstrap.py 16 KB

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