primitives.alc 11 KB

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