semi_primitives.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. include "primitives.alh"
  2. // This function must be kept internally, only called through the "sleep" and "interruptable_sleep" functions
  3. Float function __sleep(a : Float, b : Boolean) = ?primitives/__sleep
  4. Element function list_create():
  5. return create_node()!
  6. Element function dict_create():
  7. return create_node()!
  8. Element function set_create():
  9. return create_node()!
  10. Boolean function value_neq(a : Element, b : Element):
  11. return bool_not(value_eq(a, b))!
  12. Boolean function integer_gt(a : Element, b : Element):
  13. return bool_or(integer_lt(a, b), value_eq(a, b))!
  14. Boolean function float_gt(a : Element, b : Element):
  15. return bool_or(float_lt(a, b), value_eq(a, b))!
  16. Element function dict_add(a : Element, b : Element, c : Element):
  17. log("Doing dict_add for " + cast_v2s(b))
  18. create_edge(create_edge(a, c), b)
  19. return a!
  20. Boolean function element_neq(a : Element, b : Element):
  21. return bool_not(element_eq(a, b))!
  22. Integer function dict_len(a : Element):
  23. return read_nr_out(a)!
  24. Integer function list_len(a : Element):
  25. return read_nr_out(a)!
  26. Integer function set_len(a : Element):
  27. return read_nr_out(a)!
  28. Float function float_neg(a : Float):
  29. return float_subtraction(0, a)!
  30. Integer function integer_neg(a : Integer):
  31. return integer_subtraction(0, a)!
  32. Element function list_read(a : Element, b : Integer):
  33. return dict_read(a, b)!
  34. Element function list_append(a : Element, b : Element):
  35. dict_add(a, integer_addition(list_len(a), 1), b)
  36. return a!
  37. Element function set_add(a : Element, b : Element):
  38. if (bool_not(set_in(a, b))):
  39. // Treat set as a dictionary with a mock-value
  40. dict_add(a, b, a)
  41. return a!
  42. Element function set_add_node(a : Element, b : Element):
  43. if (bool_not(set_in_node(a, b))):
  44. // Treat set as a dictionary with a mock-value
  45. dict_add(a, b, a)
  46. return a!
  47. Element function set_pop(a : Element):
  48. if (integer_gt(set_len(a), 0)):
  49. Element edge
  50. Element result
  51. edge = read_out(a, 0)
  52. result = read_edge_dst(read_out(edge, 0))
  53. delete_element(edge)
  54. log("Value: " + cast_v2s(result))
  55. return result!
  56. else:
  57. return read_root()!
  58. Element function set_read(a : Element):
  59. if (integer_gt(set_len(a), 0)):
  60. return read_edge_dst(read_out(read_out(a, 0), 0))!
  61. else:
  62. return read_root()!
  63. Void function sleep(a : Float):
  64. __sleep(a, False)
  65. return!
  66. Void function interruptable_sleep(a : Float):
  67. __sleep(a, True)
  68. return!
  69. Element function exec(first_instr : Element):
  70. // This does very ugly things, so beware!
  71. // Basically, we dynamically construct an if True condition with as body the provided instructions
  72. // after the if conditional, we append a return of an empty element, as we need a return at the end
  73. // returns in the code are therefore allowed (and will be the return value), but not necessarily
  74. Element n
  75. Element exec_if
  76. Element exec_const_true
  77. Element exec_return
  78. n = create_node()
  79. exec_if = create_value(!if)
  80. exec_const_true = create_value(!constant)
  81. exec_return = create_value(!return)
  82. dict_add(n, "params", create_node())
  83. dict_add(exec_const_true, "node", create_value(True))
  84. dict_add(exec_if, "cond", exec_const_true)
  85. dict_add(exec_if, "then", first_instr)
  86. dict_add(n, "body", exec_if)
  87. dict_add(exec_if, "next", exec_return)
  88. return n()!
  89. Boolean function string_startswith(a: String, b: String):
  90. Integer i
  91. i = 0
  92. if (string_len(b) > string_len(a)):
  93. return False!
  94. while (i < string_len(b)):
  95. if (string_get(a, i) != string_get(b, i)):
  96. return False!
  97. i = i + 1
  98. return True!
  99. Boolean function has_value(a: Element):
  100. return bool_or(bool_or(bool_or(is_physical_action, is_physical_int(a)), is_physical_float(a)), bool_or(is_physical_string(a), is_physical_boolean(a)))!
  101. Boolean function float_gte(a: Float, b: Float):
  102. return bool_or(float_gt(a, b), value_eq(a, b))!
  103. Boolean function float_lte(a: Float, b: Float):
  104. return bool_or(float_lt(a, b), value_eq(a, b))!
  105. Boolean function integer_lte(a: Integer, b: Integer):
  106. return bool_or(integer_lt(a, b), value_eq(a, b))!
  107. Boolean function integer_gte(a: Integer, b: Integer):
  108. return bool_or(integer_gt(a, b), value_eq(a, b))!
  109. String function string_substr(a: String, b: Integer, c: Integer):
  110. String result
  111. Integer i
  112. // First handle corner cases!
  113. // If the string is too short for b to even start, return empty
  114. if (b > string_len(a)):
  115. return ""!
  116. // If the part we want to snip is negative, we return empty
  117. if (b > c):
  118. return ""!
  119. i = 0
  120. result = ""
  121. while (i < string_len(a)):
  122. if (bool_and(i >= b, i <= c)):
  123. result = result + string_get(a, i)
  124. if (i > c):
  125. return result!
  126. i = i + 1
  127. return result!
  128. Element function resolve(name : String):
  129. // Could directly access it through introspection
  130. // But seems safer to create some code and execute it...
  131. Element task_root
  132. task_root = read_taskroot()
  133. task_root = task_root["globals"][name]["value"]
  134. return task_root!
  135. Integer function integer_modulo(a : Integer, b : Integer):
  136. return a - b * (a / b)!
  137. Boolean function has_input():
  138. return dict_in(dict_read(read_taskroot(), "input"), "value")!
  139. Element function list_pop(lst : Element, index : Integer):
  140. Element v
  141. v = list_read(lst, index)
  142. list_delete(lst, index)
  143. return v!
  144. Element function list_pop_final(lst : Element):
  145. return list_pop(lst, list_len(lst) - 1)!
  146. Element function set_copy(a : Element):
  147. return dict_keys(a)!
  148. String function set_to_string(s : Element):
  149. String result
  150. result = "{"
  151. s = set_copy(s)
  152. while (set_len(s) > 0):
  153. result = (result + cast_v2s(set_pop(s))) + ", "
  154. result = result + "}"
  155. return result!
  156. String function list_to_string(s : Element):
  157. String result
  158. Integer i
  159. result = "["
  160. i = 0
  161. while (i < list_len(s)):
  162. result = result + cast_v2s(list_read(s, i))
  163. result = result + ", "
  164. i = i + 1
  165. result = result + "]"
  166. return result!
  167. Element function create_tuple(a : Element, b : Element):
  168. Element tuple
  169. tuple = list_create()
  170. list_append(tuple, a)
  171. list_append(tuple, b)
  172. return tuple!
  173. String function dict_to_string(d : Element):
  174. String result
  175. Element keys
  176. Element key
  177. result = "{"
  178. keys = dict_keys(d)
  179. while (set_len(keys) > 0):
  180. key = set_pop(keys)
  181. result = result + cast_v2s(key)
  182. result = result + ": "
  183. result = result + cast_v2s(dict_read_node(d, key))
  184. result = result + ", "
  185. result = result + "}"
  186. return result!
  187. Element function set_overlap(sa : Element, sb : Element):
  188. // This has a compiled version as there seems to be a non-deterministic bug in this code, or at least in the executed code
  189. Element result
  190. Element elem
  191. if (set_len(sa) > set_len(sb)):
  192. // Pick the smallest set to iterate over, so switch if sa is not the smallest
  193. result = sa
  194. sa = sb
  195. sb = result
  196. result = set_create()
  197. sa = set_copy(sa)
  198. //log("SA: " + set_to_string(sa))
  199. //log("SB: " + set_to_string(sb))
  200. // Iterate over each element of sa and only add it to the result if it is also in sb
  201. while (set_len(sa) > 0):
  202. elem = set_pop(sa)
  203. if (set_in(sb, elem)):
  204. // Shared between both
  205. set_add(result, elem)
  206. return result!
  207. Boolean function set_equality(sa : Element, sb : Element):
  208. if (set_len(sa) != set_len(sb)):
  209. return False!
  210. sa = set_copy(sa)
  211. while (0 < set_len(sa)):
  212. if (bool_not(set_in(sb, set_pop(sa)))):
  213. return False!
  214. return True!
  215. Boolean function dict_eq(da : Element, db : Element):
  216. if (bool_not(set_equality(dict_keys(da), dict_keys(db)))):
  217. // They don't even share the same keys
  218. return False!
  219. Element keys
  220. Element key
  221. keys = dict_keys(da)
  222. while (set_len(keys) > 0):
  223. key = set_pop(keys)
  224. if (value_neq(da[key], db[key])):
  225. // Value that is not equal
  226. return False!
  227. return True!
  228. Element function dict_copy(d : Element):
  229. String result
  230. Element keys
  231. Element key
  232. result = dict_create()
  233. keys = dict_keys(d)
  234. while (set_len(keys) > 0):
  235. key = set_pop(keys)
  236. dict_add_fast(result, key, dict_read_node(d, key))
  237. return result!
  238. Element function set_to_list(s : Element):
  239. Element result
  240. Element tmp
  241. result = list_create()
  242. tmp = set_copy(s)
  243. while (set_len(tmp) > 0):
  244. list_append(result, set_pop(tmp))
  245. return result!
  246. Void function dict_overwrite(d : Element, key : Element, value : Element):
  247. if (dict_in(d, key)):
  248. dict_delete(d, key)
  249. if (dict_in_node(d, key)):
  250. dict_delete_node(d, key)
  251. dict_add(d, key, value)
  252. return !
  253. Void function set_merge(a : Element, b : Element):
  254. b = set_copy(b)
  255. while (set_len(b) > 0):
  256. set_add(a, set_pop(b))
  257. return!
  258. Element function make_reverse_dictionary(dict : Element):
  259. Element keys
  260. Element reverse
  261. String key
  262. String value
  263. reverse = dict_create()
  264. keys = dict_keys(dict)
  265. while (set_len(keys) > 0):
  266. key = set_pop(keys)
  267. value = cast_id2s(dict[key])
  268. if (dict_in(reverse, value)):
  269. dict_delete(reverse, value)
  270. dict_add(reverse, value, key)
  271. return reverse!
  272. Element function set_remove(a: Element, b: Element):
  273. return dict_delete(a, b)!
  274. Element function set_remove_node(a: Element, b: Element):
  275. return dict_delete_node(a, b)!
  276. Boolean function set_in(a: Element, b: Element):
  277. return dict_in(a, b)!
  278. Element function set_in_node(a: Element, b: Element):
  279. return dict_in_node(a, b)!
  280. String function reverseKeyLookup(dict : Element, element : Element):
  281. // TODO don't know if this AL will actually work...
  282. Integer nr_in
  283. Integer nr_out
  284. Integer counter
  285. Element link
  286. nr_in = read_nr_in(element)
  287. counter = 0
  288. while (counter < nr_in):
  289. if (element_eq(read_edge_src(read_in(element, counter)), dict)):
  290. // Got a match
  291. return (read_edge_dst(read_out(read_in(element, counter), 0)))!
  292. counter = counter + 1
  293. return string_join(string_join("(unknown: ", cast_e2s(element)), " )")!
  294. Element function reverseKeyLookupMulti(dict : Element, element : Element):
  295. // TODO don't know if this AL will actually work...
  296. Integer nr_in
  297. Integer nr_out
  298. Integer counter
  299. Element link
  300. Element result
  301. result = set_create()
  302. nr_in = read_nr_in(element)
  303. counter = 0
  304. while (counter < nr_in):
  305. if (element_eq(read_edge_src(read_in(element, counter)), dict)):
  306. // Got a match
  307. set_add(result, read_edge_dst(read_out(read_in(element, counter), 0)))
  308. counter = counter + 1
  309. return result!
  310. Boolean function list_in(list : Element, elem : Element):
  311. Integer i
  312. i = 0
  313. while (i < list_len(list)):
  314. if (value_eq(list_read(list, i), elem)):
  315. return True!
  316. i = i + 1
  317. return False!
  318. Element function dict_values(dict : Element):
  319. Element result
  320. result = set_create()
  321. Element keys
  322. keys = dict_keys(dict)
  323. while (set_len(keys) > 0):
  324. set_add(result, dict[set_pop(keys)])
  325. return result!