semi_primitives.alc 13 KB

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