semi_primitives.alc 6.5 KB

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