constructors.alc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. include "primitives.alh"
  2. include "conformance_scd.alh"
  3. include "library.alh"
  4. include "io.alh"
  5. include "modelling.alh"
  6. include "utils.alh"
  7. include "object_operations.alh"
  8. Element while_stack = ?
  9. Element variable_map = ?
  10. Element function construct_function_list(list : Element):
  11. String command
  12. Element result
  13. String main_function
  14. Boolean continue
  15. String prev_element
  16. String first_element
  17. Element model
  18. model = instantiate_model(import_node("models/ActionLanguage"))
  19. list = list_reverse(list)
  20. // Initialize variables
  21. prev_element = read_root()
  22. main_function = read_root()
  23. // Clear global variables
  24. while_stack = list_create()
  25. variable_map = dict_create()
  26. continue = True
  27. while (continue):
  28. command = list_pop_final(list)
  29. if (command == "global"):
  30. result = construct_global(model, list)
  31. elif (command == "funcdef"):
  32. result = construct_funcdef(model, list, False)
  33. elif (command == "mutable_funcdef"):
  34. result = construct_funcdef(model, list, True)
  35. else:
  36. log("ERROR (1): did not understand command " + cast_value(command))
  37. output("ERROR: compiled code not understood: " + cast_value(command))
  38. return read_root()!
  39. continue = list_pop_final(list)
  40. if (element_neq(prev_element, read_root())):
  41. create_al_link(model, "Statement_next", prev_element, result["start"], "next")
  42. else:
  43. first_element = result["start"]
  44. if (bool_and(element_eq(main_function, read_root()), command != "global")):
  45. // In case no main function is defined, it is the first defined function
  46. // This is mostly there to ensure backwards compatibility
  47. main_function = result["instruction"]
  48. prev_element = result["end"]
  49. if (value_eq(result["name"], "main")):
  50. // It was the function that we want to call
  51. main_function = result["instruction"]
  52. if (element_eq(main_function, read_root())):
  53. log("ERROR (2): no main function found")
  54. output("ERROR: no main function found")
  55. return read_root()!
  56. // Overwrite the main function with our declaration function
  57. prev_element = set_pop(allAssociationDestinations(model, main_function, "funcdef_body"))
  58. model_delete_element(model, set_pop(allOutgoingAssociationInstances(model, main_function, "funcdef_body")))
  59. create_al_link(model, "funcdef_body", main_function, first_element, "body")
  60. create_al_link(model, "Statement_next", result["end"], prev_element, "next")
  61. instantiate_link(model, "initial_funcdef", "", instantiate_node(model, "Initial", ""), main_function)
  62. return model!
  63. Void function create_al_link(model : Element, linktype : String, source : String, target : String, dictname : String):
  64. instantiate_attribute(model, instantiate_link(model, linktype, "", source, target), "name", dictname)
  65. return!
  66. String function construct_global(model : Element, list : Element):
  67. String this_element
  68. String declared_element
  69. String op
  70. this_element = instantiate_value(model, "global", "", create_value(!global))
  71. declared_element = instantiate_value(model, "String", "", create_value(list_pop_final(list)))
  72. create_al_link(model, "global_var", this_element, declared_element, "var")
  73. op = list_pop_final(list)
  74. if (op != "none"):
  75. // Defines
  76. String assign
  77. String resolve
  78. String value
  79. assign = instantiate_value(model, "assign", "", create_value(!assign))
  80. create_al_link(model, "Statement_next", this_element, assign, "next")
  81. resolve = instantiate_value(model, "resolve", "", create_value(!resolve))
  82. create_al_link(model, "assign_var", assign, resolve, "var")
  83. create_al_link(model, "resolve_var", resolve, declared_element, "var")
  84. if (op == "deref"):
  85. value = construct_deref(model, list)
  86. elif (op == "empty"):
  87. value = instantiate_value(model, "call", "", create_value(!call))
  88. String res
  89. String acc
  90. res = instantiate_value(model, "resolve", "", create_value(!resolve))
  91. acc = instantiate_value(model, "access", "", create_value(!access))
  92. create_al_link(model, "call_func", value, acc, "func")
  93. create_al_link(model, "access_var", acc, res, "var")
  94. create_al_link(model, "resolve_var", res, instantiate_value(model, "String", "", "create_node"), "var")
  95. elif (op == "const"):
  96. value = instantiate_value(model, "constant", "", create_value(!constant))
  97. create_al_link(model, "constant_node", value, instantiate_value(model, "Element", "", list_pop_final(list)), "node")
  98. create_al_link(model, "assign_value", assign, value, "value")
  99. Element result
  100. result = dict_create()
  101. dict_add_fast(result, "name", "")
  102. dict_add_fast(result, "instruction", this_element)
  103. dict_add_fast(result, "start", this_element)
  104. dict_add_fast(result, "end", assign)
  105. return result!
  106. else:
  107. Element result
  108. result = dict_create()
  109. dict_add_fast(result, "name", "")
  110. dict_add_fast(result, "instruction", this_element)
  111. dict_add_fast(result, "start", this_element)
  112. dict_add_fast(result, "end", this_element)
  113. return result!
  114. String function construct_funcdef(model : Element, list : Element, mutable : Boolean):
  115. String assign
  116. String resolve
  117. String constant
  118. String formal
  119. String func
  120. String params
  121. String declare
  122. String name
  123. declare = instantiate_value(model, "global", "", create_value(!global))
  124. assign = instantiate_value(model, "assign", "", create_value(!assign))
  125. resolve = instantiate_value(model, "resolve", "", create_value(!resolve))
  126. constant = instantiate_value(model, "constant", "", create_value(!constant))
  127. name = list_pop_final(list)
  128. if (dict_in(variable_map, name)):
  129. formal = dict_read(variable_map, name)
  130. else:
  131. formal = instantiate_value(model, "String", "", name)
  132. func = instantiate_node(model, "funcdef", "")
  133. params = instantiate_node(model, "param_dict", "")
  134. create_al_link(model, "global_var", declare, formal, "var")
  135. create_al_link(model, "Statement_next", declare, assign, "next")
  136. create_al_link(model, "assign_var", assign, resolve, "var")
  137. create_al_link(model, "assign_value", assign, constant, "value")
  138. create_al_link(model, "resolve_var", resolve, formal, "var")
  139. create_al_link(model, "constant_node", constant, func, "node")
  140. create_al_link(model, "funcdef_params", func, params, "params")
  141. if (mutable):
  142. create_al_link(model, "funcdef_mutable", func, instantiate_node(model, "Element", ""), "mutable")
  143. Integer nrParams
  144. nrParams = list_pop_final(list)
  145. Integer counter
  146. counter = 0
  147. Element param
  148. String arg_names_decl
  149. arg_names_decl = "abcdefghijklmnopqrstuvwxyz"
  150. while (counter < nrParams):
  151. param = instantiate_node(model, "Element", "")
  152. create_al_link(model, "param_dict_link", params, param, string_get(arg_names_decl, counter))
  153. dict_add_fast(variable_map, list_pop_final(list), param)
  154. // Output each parameter in turn
  155. counter = counter + 1
  156. // Now add the body
  157. create_al_link(model, "funcdef_body", func, construct_unknown(model, list), "body")
  158. Element result
  159. result = dict_create()
  160. dict_add_fast(result, "name", name)
  161. dict_add_fast(result, "instruction", func)
  162. dict_add_fast(result, "start", declare)
  163. dict_add_fast(result, "end", assign)
  164. return result!
  165. Element function construct_unknown(model : Element, list : Element):
  166. String elem
  167. Element new_model
  168. Element new_model_model
  169. elem = list_pop_final(list)
  170. if (elem == "if"):
  171. return construct_if(model, list)!
  172. elif (elem == "while"):
  173. return construct_while(model, list)!
  174. elif (elem == "access"):
  175. return construct_access(model, list)!
  176. elif (elem == "resolve"):
  177. return construct_resolve(model, list)!
  178. elif (elem == "assign"):
  179. return construct_assign(model, list)!
  180. elif (elem == "call"):
  181. return construct_call(model, list)!
  182. elif (elem == "return"):
  183. return construct_return(model, list)!
  184. elif (elem == "const"):
  185. return construct_const(model, list)!
  186. elif (elem == "declare"):
  187. return construct_declare(model, list)!
  188. elif (elem == "output"):
  189. return construct_output(model, list)!
  190. elif (elem == "input"):
  191. return construct_input(model, list)!
  192. elif (elem == "deref"):
  193. return construct_deref(model, list)!
  194. elif (elem == "break"):
  195. return construct_break(model, list)!
  196. elif (elem == "continue"):
  197. return construct_continue(model, list)!
  198. else:
  199. log("ERROR (2): did not understand command " + cast_value(elem))
  200. output("ERROR: compiled code not understood: " + cast_value(elem))
  201. return read_root()!
  202. String function construct_if(model : Element, list : Element):
  203. String this_element
  204. this_element = instantiate_value(model, "if", "", create_value(!if))
  205. create_al_link(model, "if_cond", this_element, construct_unknown(model, list), "cond")
  206. create_al_link(model, "if_then", this_element, construct_unknown(model, list), "then")
  207. if (list_pop_final(list)):
  208. create_al_link(model, "if_else", this_element, construct_unknown(model, list), "else")
  209. if (list_pop_final(list)):
  210. create_al_link(model, "Statement_next", this_element, construct_unknown(model, list), "next")
  211. return this_element!
  212. String function construct_while(model : Element, list : Element):
  213. String this_element
  214. this_element = instantiate_value(model, "while", "", create_value(!while))
  215. create_al_link(model, "while_cond", this_element, construct_unknown(model, list), "cond")
  216. list_append(while_stack, this_element)
  217. create_al_link(model, "while_body", this_element, construct_unknown(model, list), "body")
  218. list_delete(while_stack, list_len(while_stack) - 1)
  219. if (list_pop_final(list)):
  220. create_al_link(model, "Statement_next", this_element, construct_unknown(model, list), "next")
  221. return this_element!
  222. String function construct_access(model : Element, list : Element):
  223. String this_element
  224. this_element = instantiate_value(model, "access", "", create_value(!access))
  225. create_al_link(model, "access_var", this_element, construct_unknown(model, list), "var")
  226. return this_element!
  227. String function construct_resolve(model : Element, list : Element):
  228. String this_element
  229. Element linked_element
  230. String name
  231. this_element = instantiate_value(model, "resolve", "", create_value(!resolve))
  232. name = list_pop_final(list)
  233. // TODO might have to check if this is to be added
  234. if dict_in(variable_map, name):
  235. linked_element = variable_map[name]
  236. else:
  237. linked_element = instantiate_value(model, "String", "", name)
  238. create_al_link(model, "resolve_var", this_element, linked_element, "var")
  239. return this_element!
  240. String function construct_assign(model : Element, list : Element):
  241. String this_element
  242. this_element = instantiate_value(model, "assign", "", create_value(!assign))
  243. create_al_link(model, "assign_var", this_element, construct_unknown(model, list), "var")
  244. create_al_link(model, "assign_value", this_element, construct_unknown(model, list), "value")
  245. if (list_pop_final(list)):
  246. create_al_link(model, "Statement_next", this_element, construct_unknown(model, list), "next")
  247. return this_element!
  248. String function construct_call(model : Element, list : Element):
  249. String this_element
  250. this_element = instantiate_value(model, "call", "", create_value(!call))
  251. create_al_link(model, "call_func", this_element, construct_unknown(model, list), "func")
  252. Integer nrParams
  253. nrParams = list_pop_final(list)
  254. Integer counter
  255. counter = 0
  256. Element param
  257. Element prev_param
  258. String arg_names_call
  259. arg_names_call = "abcdefghijklmnopqrstuvwxyz"
  260. while (counter < nrParams):
  261. param = instantiate_node(model, "param", "")
  262. create_al_link(model, "param_name", param, instantiate_value(model, "String", "", string_get(arg_names_call, counter)), "name")
  263. create_al_link(model, "param_value", param, construct_unknown(model, list), "value")
  264. if (counter == 0):
  265. create_al_link(model, "call_params", this_element, param, "params")
  266. else:
  267. create_al_link(model, "param_next_param", prev_param, param, "next_param")
  268. prev_param = param
  269. counter = counter + 1
  270. if (nrParams > 0):
  271. create_al_link(model, "call_last_param", this_element, prev_param, "last_param")
  272. if (list_pop_final(list)):
  273. create_al_link(model, "Statement_next", this_element, construct_unknown(model, list), "next")
  274. return this_element!
  275. String function construct_return(model : Element, list : Element):
  276. if (list_pop_final(list)):
  277. String this_element
  278. this_element = instantiate_value(model, "return", "", create_value(!return))
  279. create_al_link(model, "return_value", this_element, construct_unknown(model, list), "value")
  280. return this_element!
  281. else:
  282. return instantiate_value(model, "return", "", create_value(!return))!
  283. String function construct_const(model : Element, list : Element):
  284. String this_element
  285. this_element = instantiate_value(model, "constant", "", create_value(!constant))
  286. create_al_link(model, "constant_node", this_element, instantiate_value(model, "Element", "", list_pop_final(list)), "node")
  287. return this_element!
  288. String function construct_declare(model : Element, list : Element):
  289. String this_element
  290. Element declared_element
  291. String name
  292. this_element = instantiate_value(model, "declare", "", create_value(!declare))
  293. declared_element = instantiate_node(model, "Element", "")
  294. create_al_link(model, "declare_var", this_element, declared_element, "var")
  295. name = list_pop_final(list)
  296. dict_add_fast(variable_map, name, declared_element)
  297. // Assign value
  298. String op
  299. op = list_pop_final(list)
  300. if (op != "none"):
  301. String assign
  302. String resolve
  303. String value
  304. assign = instantiate_value(model, "assign", "", create_value(!assign))
  305. create_al_link(model, "Statement_next", this_element, assign, "next")
  306. resolve = instantiate_value(model, "resolve", "", create_value(!resolve))
  307. create_al_link(model, "assign_var", assign, resolve, "var")
  308. create_al_link(model, "resolve_var", resolve, declared_element, "var")
  309. if (op == "deref"):
  310. value = construct_deref(model, list)
  311. elif (op == "empty"):
  312. value = instantiate_value(model, "call", "", create_value(!call))
  313. Element res
  314. Element acc
  315. res = instantiate_value(model, "resolve", "", create_value(!resolve))
  316. acc = instantiate_value(model, "access", "", create_value(!access))
  317. create_al_link(model, "call_func", value, acc, "func")
  318. create_al_link(model, "access_var", acc, res, "var")
  319. create_al_link(model, "resolve_var", res, instantiate_value(model, "String", "", "create_node"), "var")
  320. elif (op == "const"):
  321. value = instantiate_value(model, "constant", "", create_value(!constant))
  322. create_al_link(model, "constant_node", value, instantiate_value(model, "Element", "", list_pop_final(list)), "node")
  323. create_al_link(model, "assign_value", assign, value, "value")
  324. if (list_pop_final(list)):
  325. create_al_link(model, "Statement_next", assign, construct_unknown(model, list), "next")
  326. else:
  327. if (list_pop_final(list)):
  328. create_al_link(model, "Statement_next", this_element, construct_unknown(model, list), "next")
  329. return this_element!
  330. String function construct_input(model : Element, list : Element):
  331. return instantiate_value(model, "input", "", create_value(!input))!
  332. String function construct_output(model : Element, list : Element):
  333. String this_element
  334. this_element = instantiate_value(model, "output", "", create_value(!output))
  335. create_al_link(model, "output_value", this_element, construct_unknown(model, list), "value")
  336. if (list_pop_final(list)):
  337. create_al_link(model, "Statement_next", this_element, construct_unknown(model, list), "next")
  338. return this_element!
  339. String function construct_deref(model : Element, list : Element):
  340. String this_element
  341. this_element = instantiate_value(model, "constant", "", create_value(!constant))
  342. String val
  343. val = list_pop_final(list)
  344. create_al_link(model, "constant_node", this_element, reuse_element(model, "Element", "", import_node(val)), "node")
  345. return this_element!
  346. String function construct_break(model : Element, list : Element):
  347. String this_element
  348. this_element = instantiate_value(model, "break", "", create_value(!break))
  349. create_al_link(model, "break_while", this_element, while_stack[list_len(while_stack) - 1], "while")
  350. return this_element!
  351. String function construct_continue(model : Element, list : Element):
  352. String this_element
  353. this_element = instantiate_value(model, "continue", "", create_value(!continue))
  354. create_al_link(model, "continue_while", this_element, while_stack[list_len(while_stack) - 1], "while")
  355. return this_element!