semi_primitives.alc 7.9 KB

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