semi_primitives.alc 7.6 KB

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