primitives.alc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  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 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. Float function __sleep(a : Float) = ?primitives/__sleep
  86. Void function sleep(a : Float):
  87. __sleep(a)
  88. return!
  89. Element function exec(first_instr : Element):
  90. // This does very ugly things, so beware!
  91. // Basically, we dynamically construct an if True condition with as body the provided instructions
  92. // after the if conditional, we append a return of an empty element, as we need a return at the end
  93. // returns in the code are therefore allowed (and will be the return value), but not necessarily
  94. Element n
  95. Element exec_if
  96. Element exec_const_true
  97. Element exec_return
  98. n = create_node()
  99. exec_if = create_value(!if)
  100. exec_const_true = create_value(!constant)
  101. exec_return = create_value(!return)
  102. dict_add(n, "params", create_node())
  103. dict_add(exec_const_true, "node", create_value(True))
  104. dict_add(exec_if, "cond", exec_const_true)
  105. dict_add(exec_if, "then", first_instr)
  106. dict_add(n, "body", exec_if)
  107. dict_add(exec_if, "next", exec_return)
  108. return n()!
  109. Boolean function string_startswith(a: String, b: String):
  110. Integer i
  111. i = 0
  112. if (string_len(b) > string_len(a)):
  113. return False!
  114. while (i < string_len(b)):
  115. if (string_get(a, i) != string_get(b, i)):
  116. return False!
  117. i = i + 1
  118. return True!
  119. Boolean function has_value(a: Element):
  120. 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)))!
  121. Boolean function float_gte(a: Float, b: Float):
  122. return bool_or(float_gt(a, b), value_eq(a, b))!
  123. Boolean function float_lte(a: Float, b: Float):
  124. return bool_or(float_lt(a, b), value_eq(a, b))!
  125. Boolean function integer_lte(a: Integer, b: Integer):
  126. return bool_or(integer_lt(a, b), value_eq(a, b))!
  127. Boolean function integer_gte(a: Integer, b: Integer):
  128. return bool_or(integer_gt(a, b), value_eq(a, b))!
  129. String function string_substr(a: String, b: Integer, c: Integer):
  130. String result
  131. Integer i
  132. // First handle corner cases!
  133. // If the string is too short for b to even start, return empty
  134. if (b > string_len(a)):
  135. return ""!
  136. // If the part we want to snip is negative, we return empty
  137. if (b > c):
  138. return ""!
  139. i = 0
  140. result = ""
  141. while (i < string_len(a)):
  142. if (bool_and(i >= b, i <= c)):
  143. result = result + string_get(a, i)
  144. if (i > c):
  145. return result!
  146. i = i + 1
  147. return result!
  148. Element function resolve(name : String):
  149. // Could directly access it through introspection
  150. // But seems safer to create some code and execute it...
  151. Element task_root
  152. task_root = read_taskroot()
  153. task_root = task_root["globals"][name]["value"]
  154. return task_root!
  155. Integer function integer_modulo(a : Integer, b : Integer):
  156. return a - b * (a / b)!
  157. Boolean function has_input():
  158. return dict_in(dict_read(read_taskroot(), "input"), "value")!
  159. Element function list_pop(lst : Element, index : Integer):
  160. Element v
  161. v = list_read(lst, index)
  162. list_delete(lst, index)
  163. return v!
  164. Element function set_copy(a : Element):
  165. Element b
  166. Integer i
  167. Integer count
  168. b = create_node()
  169. i = 0
  170. count = read_nr_out(a)
  171. while (i < count):
  172. set_add(b, read_edge_dst(read_out(a, i)))
  173. i = i + 1
  174. return b!
  175. String function set_to_string(s : Element):
  176. String result
  177. Integer i
  178. result = "{"
  179. i = 0
  180. while (i < read_nr_out(s)):
  181. result = result + cast_v2s(read_edge_dst(read_out(s, i)))
  182. result = result + ", "
  183. i = i + 1
  184. result = result + "}"
  185. return result!
  186. String function list_to_string(s : Element):
  187. String result
  188. Integer i
  189. result = "["
  190. i = 0
  191. while (i < read_nr_out(s)):
  192. result = result + cast_v2s(dict_read(s, i))
  193. result = result + ", "
  194. i = i + 1
  195. result = result + "]"
  196. return result!
  197. Element function create_tuple(a : Element, b : Element):
  198. Element tuple
  199. tuple = create_node()
  200. list_append(tuple, a)
  201. list_append(tuple, b)
  202. return tuple!
  203. String function dict_to_string(d : Element):
  204. String result
  205. Element keys
  206. Element key
  207. result = "{"
  208. keys = dict_keys(d)
  209. while (read_nr_out(keys) > 0):
  210. key = set_pop(keys)
  211. result = result + cast_v2s(key)
  212. result = result + ": "
  213. result = result + cast_v2s(dict_read_node(d, key))
  214. result = result + ", "
  215. result = result + "}"
  216. return result!
  217. Element function set_overlap(sa : Element, sb : Element):
  218. Element result
  219. Integer i
  220. if (read_nr_out(sa) > read_nr_out(sb)):
  221. // Pick the smallest set to iterate over, so switch if sa is not the smallest
  222. result = sa
  223. sa = sb
  224. sb = result
  225. result = create_node()
  226. i = 0
  227. // Iterate over each element of sa and only add it to the result if it is also in sb
  228. while (i < read_nr_out(sa)):
  229. if (set_in(sb, read_edge_dst(read_out(sa, i)))):
  230. // Shared between both
  231. set_add(result, read_edge_dst(read_out(sa, i)))
  232. i = i + 1
  233. return result!
  234. Boolean function set_equality(sa : Element, sb : Element):
  235. if (read_nr_out(sa) != read_nr_out(sb)):
  236. return False!
  237. Integer i
  238. i = 0
  239. while (i < read_nr_out(sa)):
  240. if (set_in(sb, read_edge_dst(read_out(sa, i)))):
  241. // Shared between both, so all is fine
  242. i = i + 1
  243. else:
  244. // Not shared
  245. return False!
  246. return True!
  247. Boolean function dict_eq(da : Element, db : Element):
  248. if (bool_not(set_equality(dict_keys(da), dict_keys(db)))):
  249. // They don't even share the same keys
  250. return False!
  251. Element keys
  252. Element key
  253. keys = dict_keys(da)
  254. while (read_nr_out(keys) > 0):
  255. key = set_pop(keys)
  256. if (value_neq(da[key], db[key])):
  257. // Value that is not equal
  258. return False!
  259. return True!
  260. Element function dict_copy(d : Element):
  261. String result
  262. Element keys
  263. Element key
  264. result = create_node()
  265. keys = dict_keys(d)
  266. while (read_nr_out(keys) > 0):
  267. key = set_pop(keys)
  268. dict_add_fast(result, key, dict_read_node(d, key))
  269. return result!
  270. Element function set_to_list(s : Element):
  271. Element result
  272. Element tmp
  273. result = create_node()
  274. tmp = set_copy(s)
  275. while (read_nr_out(tmp) > 0):
  276. list_append(result, set_pop(tmp))
  277. return result!
  278. Void function dict_overwrite(d : Element, key : Element, value : Element):
  279. if (dict_in(d, key)):
  280. dict_delete(d, key)
  281. if (dict_in_node(d, key)):
  282. dict_delete_node(d, key)
  283. dict_add(d, key, value)
  284. return !
  285. Void function set_merge(a : Element, b : Element):
  286. b = set_copy(b)
  287. while (read_nr_out(b) > 0):
  288. set_add(a, set_pop(b))
  289. return!
  290. Element function make_reverse_dictionary(dict : Element):
  291. Element keys
  292. Element reverse
  293. String key
  294. String value
  295. reverse = create_node()
  296. keys = dict_keys(dict)
  297. while (read_nr_out(keys) > 0):
  298. key = set_pop(keys)
  299. value = cast_id2s(dict[key])
  300. if (dict_in(reverse, value)):
  301. dict_delete(reverse, value)
  302. dict_add(reverse, value, key)
  303. return reverse!