semi_primitives.alc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. Element result
  189. Element elem
  190. if (set_len(sa) > set_len(sb)):
  191. // Pick the smallest set to iterate over, so switch if sa is not the smallest
  192. result = sa
  193. sa = sb
  194. sb = result
  195. result = set_create()
  196. sa = set_copy(sa)
  197. //log("SA: " + set_to_string(sa))
  198. //log("SB: " + set_to_string(sb))
  199. // Iterate over each element of sa and only add it to the result if it is also in sb
  200. while (set_len(sa) > 0):
  201. elem = set_pop(sa)
  202. if (set_in(sb, elem)):
  203. // Shared between both
  204. set_add(result, elem)
  205. return result!
  206. Boolean function set_equality(sa : Element, sb : Element):
  207. if (set_len(sa) != set_len(sb)):
  208. return False!
  209. sa = set_copy(sa)
  210. while (0 < set_len(sa)):
  211. if (bool_not(set_in(sb, set_pop(sa)))):
  212. return False!
  213. return True!
  214. Boolean function dict_eq(da : Element, db : Element):
  215. if (bool_not(set_equality(dict_keys(da), dict_keys(db)))):
  216. // They don't even share the same keys
  217. return False!
  218. Element keys
  219. Element key
  220. keys = dict_keys(da)
  221. while (set_len(keys) > 0):
  222. key = set_pop(keys)
  223. if (value_neq(da[key], db[key])):
  224. // Value that is not equal
  225. return False!
  226. return True!
  227. Element function dict_copy(d : Element):
  228. String result
  229. Element keys
  230. Element key
  231. result = dict_create()
  232. keys = dict_keys(d)
  233. while (set_len(keys) > 0):
  234. key = set_pop(keys)
  235. dict_add_fast(result, key, dict_read_node(d, key))
  236. return result!
  237. Element function set_to_list(s : Element):
  238. Element result
  239. Element tmp
  240. result = list_create()
  241. tmp = set_copy(s)
  242. while (set_len(tmp) > 0):
  243. list_append(result, set_pop(tmp))
  244. return result!
  245. Void function dict_overwrite(d : Element, key : Element, value : Element):
  246. if (dict_in(d, key)):
  247. dict_delete(d, key)
  248. if (dict_in_node(d, key)):
  249. dict_delete_node(d, key)
  250. dict_add(d, key, value)
  251. return !
  252. Void function set_merge(a : Element, b : Element):
  253. b = set_copy(b)
  254. while (set_len(b) > 0):
  255. set_add(a, set_pop(b))
  256. return!
  257. Element function make_reverse_dictionary(dict : Element):
  258. Element keys
  259. Element reverse
  260. String key
  261. String value
  262. reverse = dict_create()
  263. keys = dict_keys(dict)
  264. while (set_len(keys) > 0):
  265. key = set_pop(keys)
  266. value = cast_id2s(dict[key])
  267. if (dict_in(reverse, value)):
  268. dict_delete(reverse, value)
  269. dict_add(reverse, value, key)
  270. return reverse!
  271. Element function set_remove(a: Element, b: Element):
  272. return dict_delete(a, b)!
  273. Element function set_remove_node(a: Element, b: Element):
  274. return dict_delete_node(a, b)!
  275. Boolean function set_in(a: Element, b: Element):
  276. return dict_in(a, b)!
  277. Element function set_in_node(a: Element, b: Element):
  278. return dict_in_node(a, b)!
  279. String function reverseKeyLookup(dict : Element, element : Element):
  280. // TODO don't know if this AL will actually work...
  281. Integer nr_in
  282. Integer nr_out
  283. Integer counter
  284. Element link
  285. nr_in = read_nr_in(element)
  286. counter = 0
  287. while (counter < nr_in):
  288. if (element_eq(read_edge_src(read_in(element, counter)), dict)):
  289. // Got a match
  290. return (read_edge_dst(read_out(read_in(element, counter), 0)))!
  291. counter = counter + 1
  292. return string_join(string_join("(unknown: ", cast_e2s(element)), " )")!
  293. Element function reverseKeyLookupMulti(dict : Element, element : Element):
  294. // TODO don't know if this AL will actually work...
  295. Integer nr_in
  296. Integer nr_out
  297. Integer counter
  298. Element link
  299. Element result
  300. result = set_create()
  301. nr_in = read_nr_in(element)
  302. counter = 0
  303. while (counter < nr_in):
  304. if (element_eq(read_edge_src(read_in(element, counter)), dict)):
  305. // Got a match
  306. set_add(result, read_edge_dst(read_out(read_in(element, counter), 0)))
  307. counter = counter + 1
  308. return result!
  309. Boolean function list_in(list : Element, elem : Element):
  310. Integer i
  311. i = 0
  312. while (i < list_len(list)):
  313. if (value_eq(list_read(list, i), elem)):
  314. return True!
  315. i = i + 1
  316. return False!
  317. Element function dict_values(dict : Element):
  318. Element result
  319. result = set_create()
  320. Element keys
  321. keys = dict_keys(dict)
  322. while (set_len(keys) > 0):
  323. set_add(result, dict[set_pop(keys)])
  324. return result!