semi_primitives.alc 8.1 KB

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