bootstrap.py 15 KB

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