semi_primitives.alc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 mutable function execute(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. log("Copy " + cast_value(set_len(keys)))
  241. while (set_len(keys) > 0):
  242. key = set_pop(keys)
  243. dict_add_fast(result, key, dict_read_node(d, key))
  244. return result!
  245. Element function list_copy(lst : Element):
  246. Integer counter
  247. Element result
  248. result = list_create()
  249. counter = 0
  250. while (counter < list_len(lst)):
  251. list_append(result, lst[counter])
  252. counter = counter + 1
  253. return result!
  254. Element function set_to_list(s : Element):
  255. Element result
  256. Element tmp
  257. result = list_create()
  258. tmp = set_copy(s)
  259. while (set_len(tmp) > 0):
  260. list_append(result, set_pop(tmp))
  261. return result!
  262. Element function list_to_set(s : Element):
  263. Element result
  264. Integer i
  265. i = 0
  266. result = set_create()
  267. while (i < list_len(s)):
  268. set_add(result, s[i])
  269. i = i + 1
  270. return result!
  271. Void function dict_overwrite(d : Element, key : Element, value : Element):
  272. if (dict_in(d, key)):
  273. dict_delete(d, key)
  274. if (dict_in_node(d, key)):
  275. dict_delete_node(d, key)
  276. dict_add(d, key, value)
  277. return !
  278. Void function set_merge(a : Element, b : Element):
  279. b = set_copy(b)
  280. while (set_len(b) > 0):
  281. set_add(a, set_pop(b))
  282. return!
  283. Element function make_reverse_dictionary(dict : Element):
  284. Element keys
  285. Element reverse
  286. String key
  287. String value
  288. reverse = dict_create()
  289. keys = dict_keys(dict)
  290. while (set_len(keys) > 0):
  291. key = set_pop(keys)
  292. value = cast_id(dict[key])
  293. dict_overwrite(reverse, value, key)
  294. return reverse!
  295. Element function make_reverse_dictionary_multi(dict : Element):
  296. Element keys
  297. Element reverse
  298. String key
  299. String value
  300. reverse = dict_create()
  301. keys = dict_keys(dict)
  302. while (set_len(keys) > 0):
  303. key = set_pop(keys)
  304. value = cast_string(dict[key])
  305. if (bool_not(dict_in(reverse, value))):
  306. dict_add(reverse, value, set_create())
  307. set_add(reverse[value], key)
  308. return reverse!
  309. Element function set_remove(a: Element, b: Element):
  310. return dict_delete(a, b)!
  311. Element function set_remove_node(a: Element, b: Element):
  312. return dict_delete_node(a, b)!
  313. Boolean function set_in(a: Element, b: Element):
  314. return dict_in(a, b)!
  315. Element function set_in_node(a: Element, b: Element):
  316. return dict_in_node(a, b)!
  317. String function reverseKeyLookup(dict : Element, element : Element):
  318. Integer nr_in
  319. Integer nr_out
  320. Integer counter
  321. Element link
  322. nr_in = read_nr_in(element)
  323. counter = 0
  324. while (counter < nr_in):
  325. if (element_eq(read_edge_src(read_in(element, counter)), dict)):
  326. // Got a match
  327. return (read_edge_dst(read_out(read_in(element, counter), 0)))!
  328. counter = counter + 1
  329. return ""!
  330. Element function reverseKeyLookupMultiValue(dict : Element, element : Element):
  331. Integer nr_out
  332. Integer counter
  333. Element link
  334. Element result
  335. result = set_create()
  336. nr_out = read_nr_out(dict)
  337. counter = 0
  338. while (counter < nr_out):
  339. if (value_eq(read_edge_dst(read_out(dict, counter)), element)):
  340. // Got a match
  341. set_add(result, read_edge_dst(read_out(read_out(dict, counter), 0)))
  342. counter = counter + 1
  343. return result!
  344. Element function reverseKeyLookupMultiID(dict : Element, element : Element):
  345. Integer nr_out
  346. Integer counter
  347. Element link
  348. Element result
  349. result = set_create()
  350. nr_out = read_nr_out(dict)
  351. counter = 0
  352. while (counter < nr_out):
  353. if (element_eq(read_edge_dst(read_out(dict, counter)), element)):
  354. // Got a match
  355. set_add(result, read_edge_dst(read_out(read_out(dict, counter), 0)))
  356. counter = counter + 1
  357. return result!
  358. Boolean function list_in(list : Element, elem : Element):
  359. Integer i
  360. i = 0
  361. while (i < list_len(list)):
  362. if (value_eq(list_read(list, i), elem)):
  363. return True!
  364. i = i + 1
  365. return False!
  366. Element function dict_values(dict : Element):
  367. Element result
  368. result = set_create()
  369. Element keys
  370. keys = dict_keys(dict)
  371. while (set_len(keys) > 0):
  372. set_add(result, dict[set_pop(keys)])
  373. return result!
  374. Element function set_difference(sa : Element, sb : Element):
  375. Element result
  376. Element elem
  377. result = set_copy(sa)
  378. sb = set_copy(sb)
  379. while (set_len(sb) > 0):
  380. elem = set_pop(sb)
  381. if (set_in(sa, elem)):
  382. // Shared between both
  383. set_remove(result, elem)
  384. return result!
  385. Void function set_subtract(set1 : Element, set2 : Element):
  386. Element elem
  387. set2 = set_copy(set2)
  388. while (set_len(set2) > 0):
  389. elem = set_pop(set2)
  390. if (set_in(set1, elem)):
  391. set_remove(set1, elem)
  392. return!
  393. Element function range(max : Integer):
  394. Element result
  395. Integer counter
  396. result = list_create()
  397. counter = 0
  398. while (counter < max):
  399. list_append(result, counter)
  400. counter = counter + 1
  401. return result!
  402. String function spawn(function : Element, arguments : Element):
  403. // Define a new task, with all the required stack information
  404. // This is taken from the "task_management" code
  405. Element task_root
  406. Element task_frame
  407. Element output_value
  408. Element input_value
  409. Element root
  410. root = read_root()
  411. task_root = create_node()
  412. task_frame = create_node()
  413. output_value = create_node()
  414. input_value = create_node()
  415. dict_add_fast(task_root, "frame", task_frame)
  416. dict_add_fast(task_root, "output", output_value)
  417. dict_add_fast(task_root, "last_output", output_value)
  418. dict_add_fast(task_root, "input", input_value)
  419. dict_add_fast(task_root, "last_input", input_value)
  420. dict_add_fast(task_frame, "evalstack", create_node())
  421. dict_add_fast(task_frame, "returnvalue", create_node())
  422. dict_add_fast(task_frame, "phase", "init")
  423. dict_add_fast(task_frame, "symbols", create_node())
  424. // Instead of just spawning, we set a different IP
  425. dict_add_fast(task_frame, "IP", function["body"])
  426. // Additionally, we copy over all globals that we previously had
  427. dict_add_fast(task_root, "globals", dict_copy(root[get_taskname()]["globals"]))
  428. if (dict_in(function, "params")):
  429. // And add the arguments to the symbol table
  430. Element symbols
  431. String arg_names_call
  432. Element param_dict
  433. Integer arg_i
  434. symbols = task_frame["symbols"]
  435. arg_names_call = "abcdefghijklmnopqrstuvwxyz"
  436. arg_i = 0
  437. param_dict = function["params"]
  438. arguments = list_copy(arguments)
  439. Element t
  440. Element entry
  441. while (list_len(arguments) > 0):
  442. entry = create_node()
  443. dict_add(entry, "value", list_pop(arguments, 0))
  444. t = create_edge(symbols, entry)
  445. create_edge(t, param_dict[string_get(arg_names_call, arg_i)])
  446. arg_i = arg_i + 1
  447. // Add this only at the end, as otherwise the task will already be detected and executed
  448. String taskname
  449. taskname = random_string(30)
  450. while (dict_in(read_root(), taskname)):
  451. taskname = random_string(30)
  452. dict_add_fast(read_root(), taskname, task_root)
  453. return taskname!
  454. Element function list_insert(a : Element, b : Element, c : Integer):
  455. Integer current
  456. Element prev
  457. Element new_prev
  458. current = c
  459. prev = b
  460. while (current < list_len(a)):
  461. new_prev = list_read(a, current)
  462. dict_delete(a, current)
  463. dict_add_fast(a, current, prev)
  464. prev = new_prev
  465. current = current + 1
  466. dict_add_fast(a, current, prev)
  467. return a!
  468. Element function list_delete(a : Element, b : Integer):
  469. Integer current
  470. current = b
  471. while (current < (list_len(a) - 1)):
  472. dict_delete(a, current)
  473. dict_add_fast(a, current, a[current + 1])
  474. current = current + 1
  475. dict_delete(a, current)
  476. return a!
  477. Void function dict_update(a : Element, b : Element):
  478. Element keys
  479. String key
  480. keys = dict_keys(b)
  481. while (set_len(keys) > 0):
  482. key = set_pop(keys)
  483. dict_overwrite(a, key, b[key])
  484. return!