semi_primitives.alc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. create_edge(create_edge(a, c), b)
  18. return a!
  19. Boolean function element_neq(a : Element, b : Element):
  20. return bool_not(element_eq(a, b))!
  21. Integer function dict_len(a : Element):
  22. return read_nr_out(a)!
  23. Integer function list_len(a : Element):
  24. return read_nr_out(a)!
  25. Integer function set_len(a : Element):
  26. return read_nr_out(a)!
  27. Float function float_neg(a : Float):
  28. return float_subtraction(0, a)!
  29. Integer function integer_neg(a : Integer):
  30. return integer_subtraction(0, a)!
  31. Element function list_read(a : Element, b : Integer):
  32. return dict_read(a, b)!
  33. Element function list_append(a : Element, b : Element):
  34. dict_add(a, integer_addition(list_len(a), 1), b)
  35. return a!
  36. Element function set_add(a : Element, b : Element):
  37. if (bool_not(set_in(a, b))):
  38. create_edge(a, b)
  39. return a!
  40. Element function set_pop(a : Element):
  41. if (integer_gt(set_len(a), 0)):
  42. Element edge
  43. edge = read_out(a, 0)
  44. edge = read_edge_dst(edge)
  45. delete_element(edge)
  46. return edge!
  47. else:
  48. return read_root()!
  49. Element function set_read(a : Element):
  50. if (integer_gt(set_len(a), 0)):
  51. return read_edge_dst(read_out(a, 0))!
  52. else:
  53. return read_root()!
  54. Void function sleep(a : Float):
  55. __sleep(a, False)
  56. return!
  57. Void function interruptable_sleep(a : Float):
  58. __sleep(a, True)
  59. return!
  60. Element function exec(first_instr : Element):
  61. // This does very ugly things, so beware!
  62. // Basically, we dynamically construct an if True condition with as body the provided instructions
  63. // after the if conditional, we append a return of an empty element, as we need a return at the end
  64. // returns in the code are therefore allowed (and will be the return value), but not necessarily
  65. Element n
  66. Element exec_if
  67. Element exec_const_true
  68. Element exec_return
  69. n = create_node()
  70. exec_if = create_value(!if)
  71. exec_const_true = create_value(!constant)
  72. exec_return = create_value(!return)
  73. dict_add(n, "params", create_node())
  74. dict_add(exec_const_true, "node", create_value(True))
  75. dict_add(exec_if, "cond", exec_const_true)
  76. dict_add(exec_if, "then", first_instr)
  77. dict_add(n, "body", exec_if)
  78. dict_add(exec_if, "next", exec_return)
  79. return n()!
  80. Boolean function string_startswith(a: String, b: String):
  81. Integer i
  82. i = 0
  83. if (string_len(b) > string_len(a)):
  84. return False!
  85. while (i < string_len(b)):
  86. if (string_get(a, i) != string_get(b, i)):
  87. return False!
  88. i = i + 1
  89. return True!
  90. Boolean function has_value(a: Element):
  91. 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)))!
  92. Boolean function float_gte(a: Float, b: Float):
  93. return bool_or(float_gt(a, b), value_eq(a, b))!
  94. Boolean function float_lte(a: Float, b: Float):
  95. return bool_or(float_lt(a, b), value_eq(a, b))!
  96. Boolean function integer_lte(a: Integer, b: Integer):
  97. return bool_or(integer_lt(a, b), value_eq(a, b))!
  98. Boolean function integer_gte(a: Integer, b: Integer):
  99. return bool_or(integer_gt(a, b), value_eq(a, b))!
  100. String function string_substr(a: String, b: Integer, c: Integer):
  101. String result
  102. Integer i
  103. // First handle corner cases!
  104. // If the string is too short for b to even start, return empty
  105. if (b > string_len(a)):
  106. return ""!
  107. // If the part we want to snip is negative, we return empty
  108. if (b > c):
  109. return ""!
  110. i = 0
  111. result = ""
  112. while (i < string_len(a)):
  113. if (bool_and(i >= b, i <= c)):
  114. result = result + string_get(a, i)
  115. if (i > c):
  116. return result!
  117. i = i + 1
  118. return result!
  119. Element function resolve(name : String):
  120. // Could directly access it through introspection
  121. // But seems safer to create some code and execute it...
  122. Element task_root
  123. task_root = read_taskroot()
  124. task_root = task_root["globals"][name]["value"]
  125. return task_root!
  126. Integer function integer_modulo(a : Integer, b : Integer):
  127. return a - b * (a / b)!
  128. Boolean function has_input():
  129. return dict_in(dict_read(read_taskroot(), "input"), "value")!
  130. Element function list_pop(lst : Element, index : Integer):
  131. Element v
  132. v = list_read(lst, index)
  133. list_delete(lst, index)
  134. return v!
  135. Element function set_copy(a : Element):
  136. Element b
  137. Integer i
  138. Integer count
  139. b = set_create()
  140. i = 0
  141. count = read_nr_out(a)
  142. while (i < count):
  143. set_add(b, read_edge_dst(read_out(a, i)))
  144. i = i + 1
  145. return b!
  146. String function set_to_string(s : Element):
  147. String result
  148. Integer i
  149. result = "{"
  150. i = 0
  151. while (i < read_nr_out(s)):
  152. result = result + cast_v2s(read_edge_dst(read_out(s, i)))
  153. result = result + ", "
  154. i = i + 1
  155. result = result + "}"
  156. return result!
  157. String function list_to_string(s : Element):
  158. String result
  159. Integer i
  160. result = "["
  161. i = 0
  162. while (i < read_nr_out(s)):
  163. result = result + cast_v2s(dict_read(s, i))
  164. result = result + ", "
  165. i = i + 1
  166. result = result + "]"
  167. return result!
  168. Element function create_tuple(a : Element, b : Element):
  169. Element tuple
  170. tuple = list_create()
  171. list_append(tuple, a)
  172. list_append(tuple, b)
  173. return tuple!
  174. String function dict_to_string(d : Element):
  175. String result
  176. Element keys
  177. Element key
  178. result = "{"
  179. keys = dict_keys(d)
  180. while (read_nr_out(keys) > 0):
  181. key = set_pop(keys)
  182. result = result + cast_v2s(key)
  183. result = result + ": "
  184. result = result + cast_v2s(dict_read_node(d, key))
  185. result = result + ", "
  186. result = result + "}"
  187. return result!
  188. Element function set_overlap(sa : Element, sb : Element):
  189. Element result
  190. Integer i
  191. if (read_nr_out(sa) > read_nr_out(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. i = 0
  198. // Iterate over each element of sa and only add it to the result if it is also in sb
  199. while (i < read_nr_out(sa)):
  200. if (set_in(sb, read_edge_dst(read_out(sa, i)))):
  201. // Shared between both
  202. set_add(result, read_edge_dst(read_out(sa, i)))
  203. i = i + 1
  204. return result!
  205. Boolean function set_equality(sa : Element, sb : Element):
  206. if (read_nr_out(sa) != read_nr_out(sb)):
  207. return False!
  208. Integer i
  209. i = 0
  210. while (i < read_nr_out(sa)):
  211. if (set_in(sb, read_edge_dst(read_out(sa, i)))):
  212. // Shared between both, so all is fine
  213. i = i + 1
  214. else:
  215. // Not shared
  216. return False!
  217. return True!
  218. Boolean function dict_eq(da : Element, db : Element):
  219. if (bool_not(set_equality(dict_keys(da), dict_keys(db)))):
  220. // They don't even share the same keys
  221. return False!
  222. Element keys
  223. Element key
  224. keys = dict_keys(da)
  225. while (read_nr_out(keys) > 0):
  226. key = set_pop(keys)
  227. if (value_neq(da[key], db[key])):
  228. // Value that is not equal
  229. return False!
  230. return True!
  231. Element function dict_copy(d : Element):
  232. String result
  233. Element keys
  234. Element key
  235. result = dict_create()
  236. keys = dict_keys(d)
  237. while (read_nr_out(keys) > 0):
  238. key = set_pop(keys)
  239. dict_add_fast(result, key, dict_read_node(d, key))
  240. return result!
  241. Element function set_to_list(s : Element):
  242. Element result
  243. Element tmp
  244. result = list_create()
  245. tmp = set_copy(s)
  246. while (read_nr_out(tmp) > 0):
  247. list_append(result, set_pop(tmp))
  248. return result!
  249. Void function dict_overwrite(d : Element, key : Element, value : Element):
  250. if (dict_in(d, key)):
  251. dict_delete(d, key)
  252. if (dict_in_node(d, key)):
  253. dict_delete_node(d, key)
  254. dict_add(d, key, value)
  255. return !
  256. Void function set_merge(a : Element, b : Element):
  257. b = set_copy(b)
  258. while (read_nr_out(b) > 0):
  259. set_add(a, set_pop(b))
  260. return!
  261. Element function make_reverse_dictionary(dict : Element):
  262. Element keys
  263. Element reverse
  264. String key
  265. String value
  266. reverse = dict_create()
  267. keys = dict_keys(dict)
  268. while (read_nr_out(keys) > 0):
  269. key = set_pop(keys)
  270. value = cast_id2s(dict[key])
  271. if (dict_in(reverse, value)):
  272. dict_delete(reverse, value)
  273. dict_add(reverse, value, key)
  274. return reverse!