primitives.alc 11 KB

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