semi_primitives.alc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. include "primitives.alh"
  2. include "utils.alh"
  3. include "random.alh"
  4. // This function must be kept internally, only called through the "sleep" and "interruptable_sleep" functions
  5. Float function __sleep(a : Float, b : Boolean) = ?primitives/__sleep
  6. Element function list_create():
  7. return create_node()!
  8. Element function dict_create():
  9. return create_node()!
  10. Element function set_create():
  11. return create_node()!
  12. Boolean function value_neq(a : Element, b : Element):
  13. return bool_not(value_eq(a, b))!
  14. Boolean function integer_gt(a : Element, b : Element):
  15. return bool_not(bool_or(integer_lt(a, b), value_eq(a, b)))!
  16. Boolean function float_gt(a : Element, b : Element):
  17. return bool_not(bool_or(float_lt(a, b), value_eq(a, b)))!
  18. Element function dict_add(a : Element, b : Element, c : Element):
  19. create_edge(create_edge(a, c), b)
  20. return a!
  21. Boolean function element_neq(a : Element, b : Element):
  22. return bool_not(element_eq(a, b))!
  23. Integer function dict_len(a : Element):
  24. return read_nr_out(a)!
  25. Integer function list_len(a : Element):
  26. return read_nr_out(a)!
  27. Integer function set_len(a : Element):
  28. return read_nr_out(a)!
  29. Float function float_neg(a : Float):
  30. return float_subtraction(0, a)!
  31. Integer function integer_neg(a : Integer):
  32. return integer_subtraction(0, a)!
  33. Element function list_read(a : Element, b : Integer):
  34. return dict_read(a, b)!
  35. Element function list_append(a : Element, b : Element):
  36. dict_add(a, list_len(a), b)
  37. return a!
  38. Element function set_add(a : Element, b : Element):
  39. if (bool_not(set_in(a, b))):
  40. // Treat set as a dictionary with a mock-value
  41. dict_add(a, b, a)
  42. return a!
  43. Element function set_add_node(a : Element, b : Element):
  44. if (bool_not(set_in_node(a, b))):
  45. // Treat set as a dictionary with a mock-value
  46. dict_add(a, b, a)
  47. return a!
  48. Element function set_pop(a : Element):
  49. if (integer_gt(set_len(a), 0)):
  50. Element edge
  51. Element result
  52. edge = read_out(a, 0)
  53. result = read_edge_dst(read_out(edge, 0))
  54. delete_element(edge)
  55. return result!
  56. else:
  57. return read_root()!
  58. Element function set_read(a : Element):
  59. if (integer_gt(set_len(a), 0)):
  60. return read_edge_dst(read_out(read_out(a, 0), 0))!
  61. else:
  62. return read_root()!
  63. Void function sleep(a : Float):
  64. __sleep(a, False)
  65. return!
  66. Void function interruptable_sleep(a : Float):
  67. __sleep(a, True)
  68. return!
  69. Element function exec(first_instr : Element):
  70. // This does very ugly things, so beware!
  71. // Basically, we dynamically construct an if True condition with as body the provided instructions
  72. // after the if conditional, we append a return of an empty element, as we need a return at the end
  73. // returns in the code are therefore allowed (and will be the return value), but not necessarily
  74. Element n
  75. Element exec_if
  76. Element exec_const_true
  77. Element exec_return
  78. n = create_node()
  79. exec_if = create_value(!if)
  80. exec_const_true = create_value(!constant)
  81. exec_return = create_value(!return)
  82. dict_add(n, "params", create_node())
  83. dict_add(exec_const_true, "node", create_value(True))
  84. dict_add(exec_if, "cond", exec_const_true)
  85. dict_add(exec_if, "then", first_instr)
  86. dict_add(n, "body", exec_if)
  87. dict_add(exec_if, "next", exec_return)
  88. return n()!
  89. Boolean function string_startswith(a: String, b: String):
  90. Integer i
  91. i = 0
  92. if (string_len(b) > string_len(a)):
  93. return False!
  94. while (i < string_len(b)):
  95. if (string_get(a, i) != string_get(b, i)):
  96. return False!
  97. i = i + 1
  98. return True!
  99. Boolean function has_value(a: Element):
  100. return bool_or(bool_or(bool_or(is_physical_action, is_physical_int(a)), bool_or(is_physical_none(a), is_physical_float(a))), bool_or(is_physical_string(a), is_physical_boolean(a)))!
  101. Boolean function float_gte(a: Float, b: Float):
  102. return bool_or(float_gt(a, b), value_eq(a, b))!
  103. Boolean function float_lte(a: Float, b: Float):
  104. return bool_or(float_lt(a, b), value_eq(a, b))!
  105. Boolean function integer_lte(a: Integer, b: Integer):
  106. return bool_or(integer_lt(a, b), value_eq(a, b))!
  107. Boolean function integer_gte(a: Integer, b: Integer):
  108. return bool_or(integer_gt(a, b), value_eq(a, b))!
  109. String function string_substr(a: String, b: Integer, c: Integer):
  110. String result
  111. Integer i
  112. // First handle corner cases!
  113. // If the string is too short for b to even start, return empty
  114. if (b > string_len(a)):
  115. return ""!
  116. // If the part we want to snip is negative, we return empty
  117. if (b > c):
  118. return ""!
  119. i = 0
  120. result = ""
  121. while (i < string_len(a)):
  122. if (bool_and(i >= b, i <= c)):
  123. result = result + string_get(a, i)
  124. if (i > c):
  125. return result!
  126. i = i + 1
  127. return result!
  128. String function string_replace(a : String, b : String, c : String):
  129. Element lst
  130. String result
  131. lst = string_split(a, b)
  132. result = cast_string(list_pop_final(lst))
  133. while (set_len(lst) > 0):
  134. result = cast_string(list_pop_final(lst)) + c + result
  135. return result!
  136. Element function resolve(name : String):
  137. // Could directly access it through introspection
  138. // But seems safer to create some code and execute it...
  139. Element task_root
  140. task_root = read_taskroot()
  141. task_root = task_root["globals"][name]["value"]
  142. return task_root!
  143. Integer function integer_modulo(a : Integer, b : Integer):
  144. return a - b * (a / b)!
  145. Boolean function has_input():
  146. return dict_in(dict_read(read_taskroot(), "input"), "value")!
  147. Element function list_pop(lst : Element, index : Integer):
  148. Element v
  149. v = list_read(lst, index)
  150. list_delete(lst, index)
  151. return v!
  152. Element function list_pop_final(lst : Element):
  153. return list_pop(lst, list_len(lst) - 1)!
  154. Element function set_copy(a : Element):
  155. return dict_keys(a)!
  156. String function set_to_string(s : Element):
  157. String result
  158. result = "{"
  159. s = set_copy(s)
  160. while (set_len(s) > 0):
  161. result = (result + cast_value(set_pop(s))) + ", "
  162. result = result + "}"
  163. return result!
  164. String function list_to_string(s : Element):
  165. String result
  166. Integer i
  167. result = "["
  168. i = 0
  169. while (i < list_len(s)):
  170. result = result + cast_value(list_read(s, i))
  171. result = result + ", "
  172. i = i + 1
  173. result = result + "]"
  174. return result!
  175. Element function create_tuple(a : Element, b : Element):
  176. Element tuple
  177. tuple = list_create()
  178. list_append(tuple, a)
  179. list_append(tuple, b)
  180. return tuple!
  181. String function dict_to_string(d : Element):
  182. String result
  183. Element keys
  184. Element key
  185. result = "{"
  186. keys = dict_keys(d)
  187. while (set_len(keys) > 0):
  188. key = set_pop(keys)
  189. result = result + cast_value(key)
  190. result = result + ": "
  191. result = result + cast_value(dict_read_node(d, key))
  192. if (set_len(keys) > 0):
  193. result = result + ", "
  194. result = result + "}"
  195. return result!
  196. Element function set_overlap(sa : Element, sb : Element):
  197. Element result
  198. Element elem
  199. if (set_len(sa) > set_len(sb)):
  200. // Pick the smallest set to iterate over, so switch if sa is not the smallest
  201. result = sa
  202. sa = sb
  203. sb = result
  204. result = set_create()
  205. sa = set_copy(sa)
  206. // Iterate over each element of sa and only add it to the result if it is also in sb
  207. while (set_len(sa) > 0):
  208. elem = set_pop(sa)
  209. if (set_in(sb, elem)):
  210. // Shared between both
  211. set_add(result, elem)
  212. return result!
  213. Boolean function set_equality(sa : Element, sb : Element):
  214. if (set_len(sa) != set_len(sb)):
  215. return False!
  216. sa = set_copy(sa)
  217. while (0 < set_len(sa)):
  218. if (bool_not(set_in(sb, set_pop(sa)))):
  219. return False!
  220. return True!
  221. Boolean function dict_eq(da : Element, db : Element):
  222. if (bool_not(set_equality(dict_keys(da), dict_keys(db)))):
  223. // They don't even share the same keys
  224. return False!
  225. Element keys
  226. Element key
  227. keys = dict_keys(da)
  228. while (set_len(keys) > 0):
  229. key = set_pop(keys)
  230. if (value_neq(da[key], db[key])):
  231. // Value that is not equal
  232. return False!
  233. return True!
  234. Element function dict_copy(d : Element):
  235. String result
  236. Element keys
  237. Element key
  238. result = dict_create()
  239. keys = dict_keys(d)
  240. while (set_len(keys) > 0):
  241. key = set_pop(keys)
  242. dict_add_fast(result, key, dict_read_node(d, key))
  243. return result!
  244. Element function list_copy(lst : Element):
  245. Integer counter
  246. Element result
  247. result = list_create()
  248. counter = 0
  249. while (counter < list_len(lst)):
  250. list_append(result, lst[counter])
  251. counter = counter + 1
  252. return result!
  253. Element function set_to_list(s : Element):
  254. Element result
  255. Element tmp
  256. result = list_create()
  257. tmp = set_copy(s)
  258. while (set_len(tmp) > 0):
  259. list_append(result, set_pop(tmp))
  260. return result!
  261. Element function list_to_set(s : Element):
  262. Element result
  263. Integer i
  264. i = 0
  265. result = set_create()
  266. while (i < list_len(s)):
  267. set_add(result, s[i])
  268. i = i + 1
  269. return result!
  270. Void function dict_overwrite(d : Element, key : Element, value : Element):
  271. if (dict_in(d, key)):
  272. dict_delete(d, key)
  273. if (dict_in_node(d, key)):
  274. dict_delete_node(d, key)
  275. dict_add(d, key, value)
  276. return !
  277. Void function set_merge(a : Element, b : Element):
  278. b = set_copy(b)
  279. while (set_len(b) > 0):
  280. set_add(a, set_pop(b))
  281. return!
  282. Element function make_reverse_dictionary(dict : Element):
  283. Element keys
  284. Element reverse
  285. String key
  286. String value
  287. reverse = dict_create()
  288. keys = dict_keys(dict)
  289. while (set_len(keys) > 0):
  290. key = set_pop(keys)
  291. value = cast_id(dict[key])
  292. if (dict_in(reverse, value)):
  293. dict_delete(reverse, value)
  294. dict_add(reverse, value, key)
  295. return reverse!
  296. Element function set_remove(a: Element, b: Element):
  297. return dict_delete(a, b)!
  298. Element function set_remove_node(a: Element, b: Element):
  299. return dict_delete_node(a, b)!
  300. Boolean function set_in(a: Element, b: Element):
  301. return dict_in(a, b)!
  302. Element function set_in_node(a: Element, b: Element):
  303. return dict_in_node(a, b)!
  304. String function reverseKeyLookup(dict : Element, element : Element):
  305. // TODO don't know if this AL will actually work...
  306. Integer nr_in
  307. Integer nr_out
  308. Integer counter
  309. Element link
  310. nr_in = read_nr_in(element)
  311. counter = 0
  312. while (counter < nr_in):
  313. if (element_eq(read_edge_src(read_in(element, counter)), dict)):
  314. // Got a match
  315. return (read_edge_dst(read_out(read_in(element, counter), 0)))!
  316. counter = counter + 1
  317. return ""!
  318. Element function reverseKeyLookupMulti(dict : Element, element : Element):
  319. // TODO don't know if this AL will actually work...
  320. Integer nr_in
  321. Integer nr_out
  322. Integer counter
  323. Element link
  324. Element result
  325. result = set_create()
  326. nr_in = read_nr_in(element)
  327. counter = 0
  328. while (counter < nr_in):
  329. if (element_eq(read_edge_src(read_in(element, counter)), dict)):
  330. // Got a match
  331. set_add(result, read_edge_dst(read_out(read_in(element, counter), 0)))
  332. counter = counter + 1
  333. return result!
  334. Boolean function list_in(list : Element, elem : Element):
  335. Integer i
  336. i = 0
  337. while (i < list_len(list)):
  338. if (value_eq(list_read(list, i), elem)):
  339. return True!
  340. i = i + 1
  341. return False!
  342. Element function dict_values(dict : Element):
  343. Element result
  344. result = set_create()
  345. Element keys
  346. keys = dict_keys(dict)
  347. while (set_len(keys) > 0):
  348. set_add(result, dict[set_pop(keys)])
  349. return result!
  350. Element function set_difference(sa : Element, sb : Element):
  351. Element result
  352. Element elem
  353. result = set_copy(sa)
  354. sb = set_copy(sb)
  355. while (set_len(sb) > 0):
  356. elem = set_pop(sb)
  357. if (set_in(sa, elem)):
  358. // Shared between both
  359. set_remove(result, elem)
  360. return result!
  361. Void function set_subtract(set1 : Element, set2 : Element):
  362. Element elem
  363. set2 = set_copy(set2)
  364. while (set_len(set2) > 0):
  365. elem = set_pop(set2)
  366. if (set_in(set1, elem)):
  367. set_remove(set1, elem)
  368. return!
  369. Element function range(max : Integer):
  370. Element result
  371. Integer counter
  372. result = list_create()
  373. counter = 0
  374. while (counter < max):
  375. list_append(result, counter)
  376. counter = counter + 1
  377. return result!
  378. String function spawn(function : Element, arguments : Element):
  379. // Define a new task, with all the required stack information
  380. // This is taken from the "task_management" code
  381. Element task_root
  382. Element task_frame
  383. Element output_value
  384. Element input_value
  385. Element root
  386. root = read_root()
  387. task_root = create_node()
  388. task_frame = create_node()
  389. output_value = create_node()
  390. input_value = create_node()
  391. dict_add_fast(task_root, "frame", task_frame)
  392. dict_add_fast(task_root, "output", output_value)
  393. dict_add_fast(task_root, "last_output", output_value)
  394. dict_add_fast(task_root, "input", input_value)
  395. dict_add_fast(task_root, "last_input", input_value)
  396. dict_add_fast(task_frame, "evalstack", create_node())
  397. dict_add_fast(task_frame, "returnvalue", create_node())
  398. dict_add_fast(task_frame, "phase", "init")
  399. dict_add_fast(task_frame, "symbols", create_node())
  400. // Instead of just spawning, we set a different IP
  401. dict_add_fast(task_frame, "IP", function["body"])
  402. // Additionally, we copy over all globals that we previously had
  403. dict_add_fast(task_root, "globals", dict_copy(root[get_taskname()]["globals"]))
  404. if (dict_in(function, "params")):
  405. // And add the arguments to the symbol table
  406. Element symbols
  407. String arg_names_call
  408. Element param_dict
  409. Integer arg_i
  410. symbols = task_frame["symbols"]
  411. arg_names_call = "abcdefghijklmnopqrstuvwxyz"
  412. arg_i = 0
  413. param_dict = function["params"]
  414. arguments = list_copy(arguments)
  415. Element t
  416. Element entry
  417. while (list_len(arguments) > 0):
  418. entry = create_node()
  419. dict_add(entry, "value", list_pop(arguments, 0))
  420. t = create_edge(symbols, entry)
  421. create_edge(t, param_dict[string_get(arg_names_call, arg_i)])
  422. arg_i = arg_i + 1
  423. // Add this only at the end, as otherwise the task will already be detected and executed
  424. String taskname
  425. taskname = random_string(30)
  426. while (dict_in(read_root(), taskname)):
  427. taskname = random_string(30)
  428. dict_add_fast(read_root(), taskname, task_root)
  429. return taskname!