primitives.alc 12 KB

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