semi_primitives.alc 8.3 KB

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