semi_primitives.alc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  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. Element function resolve(name : String):
  133. // Could directly access it through introspection
  134. // But seems safer to create some code and execute it...
  135. Element task_root
  136. task_root = read_taskroot()
  137. task_root = task_root["globals"][name]["value"]
  138. return task_root!
  139. Integer function integer_modulo(a : Integer, b : Integer):
  140. return a - b * (a / b)!
  141. Boolean function has_input():
  142. return dict_in(dict_read(read_taskroot(), "input"), "value")!
  143. Element function list_pop(lst : Element, index : Integer):
  144. Element v
  145. v = list_read(lst, index)
  146. list_delete(lst, index)
  147. return v!
  148. Element function list_pop_final(lst : Element):
  149. return list_pop(lst, list_len(lst) - 1)!
  150. Element function set_copy(a : Element):
  151. return dict_keys(a)!
  152. String function set_to_string(s : Element):
  153. String result
  154. result = "{"
  155. s = set_copy(s)
  156. while (set_len(s) > 0):
  157. result = (result + cast_value(set_pop(s))) + ", "
  158. result = result + "}"
  159. return result!
  160. String function list_to_string(s : Element):
  161. String result
  162. Integer i
  163. result = "["
  164. i = 0
  165. while (i < list_len(s)):
  166. result = result + cast_value(list_read(s, i))
  167. result = result + ", "
  168. i = i + 1
  169. result = result + "]"
  170. return result!
  171. Element function create_tuple(a : Element, b : Element):
  172. Element tuple
  173. tuple = list_create()
  174. list_append(tuple, a)
  175. list_append(tuple, b)
  176. return tuple!
  177. String function dict_to_string(d : Element):
  178. String result
  179. Element keys
  180. Element key
  181. result = "{"
  182. keys = dict_keys(d)
  183. while (set_len(keys) > 0):
  184. key = set_pop(keys)
  185. result = result + cast_value(key)
  186. result = result + ": "
  187. result = result + cast_value(dict_read_node(d, key))
  188. if (set_len(keys) > 0):
  189. result = result + ", "
  190. result = result + "}"
  191. return result!
  192. Element function set_overlap(sa : Element, sb : Element):
  193. Element result
  194. Element elem
  195. if (set_len(sa) > set_len(sb)):
  196. // Pick the smallest set to iterate over, so switch if sa is not the smallest
  197. result = sa
  198. sa = sb
  199. sb = result
  200. result = set_create()
  201. sa = set_copy(sa)
  202. // Iterate over each element of sa and only add it to the result if it is also in sb
  203. while (set_len(sa) > 0):
  204. elem = set_pop(sa)
  205. if (set_in(sb, elem)):
  206. // Shared between both
  207. set_add(result, elem)
  208. return result!
  209. Boolean function set_equality(sa : Element, sb : Element):
  210. if (set_len(sa) != set_len(sb)):
  211. return False!
  212. sa = set_copy(sa)
  213. while (0 < set_len(sa)):
  214. if (bool_not(set_in(sb, set_pop(sa)))):
  215. return False!
  216. return True!
  217. Boolean function dict_eq(da : Element, db : Element):
  218. if (bool_not(set_equality(dict_keys(da), dict_keys(db)))):
  219. // They don't even share the same keys
  220. return False!
  221. Element keys
  222. Element key
  223. keys = dict_keys(da)
  224. while (set_len(keys) > 0):
  225. key = set_pop(keys)
  226. if (value_neq(da[key], db[key])):
  227. // Value that is not equal
  228. return False!
  229. return True!
  230. Element function dict_copy(d : Element):
  231. String result
  232. Element keys
  233. Element key
  234. result = dict_create()
  235. keys = dict_keys(d)
  236. while (set_len(keys) > 0):
  237. key = set_pop(keys)
  238. dict_add_fast(result, key, dict_read_node(d, key))
  239. return result!
  240. Element function list_copy(lst : Element):
  241. Integer counter
  242. Element result
  243. result = list_create()
  244. counter = 0
  245. while (counter < list_len(lst)):
  246. list_append(result, lst[counter])
  247. counter = counter + 1
  248. return result!
  249. Element function set_to_list(s : Element):
  250. Element result
  251. Element tmp
  252. result = list_create()
  253. tmp = set_copy(s)
  254. while (set_len(tmp) > 0):
  255. list_append(result, set_pop(tmp))
  256. return result!
  257. Element function list_to_set(s : Element):
  258. Element result
  259. Integer i
  260. i = 0
  261. result = set_create()
  262. while (i < list_len(s)):
  263. set_add(result, s[i])
  264. i = i + 1
  265. return result!
  266. Void function dict_overwrite(d : Element, key : Element, value : Element):
  267. if (dict_in(d, key)):
  268. dict_delete(d, key)
  269. if (dict_in_node(d, key)):
  270. dict_delete_node(d, key)
  271. dict_add(d, key, value)
  272. return !
  273. Void function set_merge(a : Element, b : Element):
  274. b = set_copy(b)
  275. while (set_len(b) > 0):
  276. set_add(a, set_pop(b))
  277. return!
  278. Element function make_reverse_dictionary(dict : Element):
  279. Element keys
  280. Element reverse
  281. String key
  282. String value
  283. reverse = dict_create()
  284. keys = dict_keys(dict)
  285. while (set_len(keys) > 0):
  286. key = set_pop(keys)
  287. value = cast_id(dict[key])
  288. if (dict_in(reverse, value)):
  289. dict_delete(reverse, value)
  290. dict_add(reverse, value, key)
  291. return reverse!
  292. Element function set_remove(a: Element, b: Element):
  293. return dict_delete(a, b)!
  294. Element function set_remove_node(a: Element, b: Element):
  295. return dict_delete_node(a, b)!
  296. Boolean function set_in(a: Element, b: Element):
  297. return dict_in(a, b)!
  298. Element function set_in_node(a: Element, b: Element):
  299. return dict_in_node(a, b)!
  300. String function reverseKeyLookup(dict : Element, element : Element):
  301. // TODO don't know if this AL will actually work...
  302. Integer nr_in
  303. Integer nr_out
  304. Integer counter
  305. Element link
  306. nr_in = read_nr_in(element)
  307. counter = 0
  308. while (counter < nr_in):
  309. if (element_eq(read_edge_src(read_in(element, counter)), dict)):
  310. // Got a match
  311. return (read_edge_dst(read_out(read_in(element, counter), 0)))!
  312. counter = counter + 1
  313. return ""!
  314. Element function reverseKeyLookupMulti(dict : Element, element : Element):
  315. // TODO don't know if this AL will actually work...
  316. Integer nr_in
  317. Integer nr_out
  318. Integer counter
  319. Element link
  320. Element result
  321. result = set_create()
  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. set_add(result, read_edge_dst(read_out(read_in(element, counter), 0)))
  328. counter = counter + 1
  329. return result!
  330. Boolean function list_in(list : Element, elem : Element):
  331. Integer i
  332. i = 0
  333. while (i < list_len(list)):
  334. if (value_eq(list_read(list, i), elem)):
  335. return True!
  336. i = i + 1
  337. return False!
  338. Element function dict_values(dict : Element):
  339. Element result
  340. result = set_create()
  341. Element keys
  342. keys = dict_keys(dict)
  343. while (set_len(keys) > 0):
  344. set_add(result, dict[set_pop(keys)])
  345. return result!
  346. Element function set_difference(sa : Element, sb : Element):
  347. Element result
  348. Element elem
  349. result = set_copy(sa)
  350. sb = set_copy(sb)
  351. while (set_len(sb) > 0):
  352. elem = set_pop(sb)
  353. if (set_in(sa, elem)):
  354. // Shared between both
  355. set_remove(result, elem)
  356. return result!
  357. Void function set_subtract(set1 : Element, set2 : Element):
  358. Element elem
  359. set2 = set_copy(set2)
  360. while (set_len(set2) > 0):
  361. elem = set_pop(set2)
  362. if (set_in(set1, elem)):
  363. set_remove(set1, elem)
  364. return!
  365. Element function range(max : Integer):
  366. Element result
  367. Integer counter
  368. result = list_create()
  369. counter = 0
  370. while (counter < max):
  371. list_append(result, counter)
  372. counter = counter + 1
  373. return result!
  374. String function spawn(function : Element, arguments : Element):
  375. // Define a new task, with all the required stack information
  376. // This is taken from the "task_management" code
  377. Element task_root
  378. Element task_frame
  379. Element output_value
  380. Element input_value
  381. Element root
  382. root = read_root()
  383. task_root = create_node()
  384. task_frame = create_node()
  385. output_value = create_node()
  386. input_value = create_node()
  387. dict_add_fast(task_root, "frame", task_frame)
  388. dict_add_fast(task_root, "output", output_value)
  389. dict_add_fast(task_root, "last_output", output_value)
  390. dict_add_fast(task_root, "input", input_value)
  391. dict_add_fast(task_root, "last_input", input_value)
  392. dict_add_fast(task_frame, "evalstack", create_node())
  393. dict_add_fast(task_frame, "returnvalue", create_node())
  394. dict_add_fast(task_frame, "phase", "init")
  395. dict_add_fast(task_frame, "symbols", create_node())
  396. // Instead of just spawning, we set a different IP
  397. dict_add_fast(task_frame, "IP", function["body"])
  398. // Additionally, we copy over all globals that we previously had
  399. dict_add_fast(task_root, "globals", dict_copy(root[get_taskname()]["globals"]))
  400. log("Setting IP to " + cast_value(function["body"]))
  401. log("Has outputs: " + set_to_string(dict_keys(function)))
  402. if (dict_in(function, "params")):
  403. // And add the arguments to the symbol table
  404. Element symbols
  405. String arg_names_call
  406. Element param_dict
  407. Integer arg_i
  408. symbols = task_frame["symbols"]
  409. arg_names_call = "abcdefghijklmnopqrstuvwxyz"
  410. arg_i = 0
  411. param_dict = function["params"]
  412. arguments = list_copy(arguments)
  413. Element t
  414. Element entry
  415. while (list_len(arguments) > 0):
  416. entry = create_node()
  417. dict_add(entry, "value", list_pop(arguments, 0))
  418. t = create_edge(symbols, entry)
  419. create_edge(t, param_dict[string_get(arg_names_call, arg_i)])
  420. log("Adding to symbols: " + cast_value(string_get(arg_names_call, arg_i)))
  421. arg_i = arg_i + 1
  422. // Add this only at the end, as otherwise the task will already be detected and executed
  423. String taskname
  424. taskname = random_string(30)
  425. while (dict_in(read_root(), taskname)):
  426. taskname = random_string(30)
  427. dict_add_fast(read_root(), taskname, task_root)
  428. return taskname!