bootstrap.py 15 KB

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