io.alc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. include "io.alh"
  2. include "primitives.alh"
  3. // Placeholder for internal functions
  4. Element function __input()
  5. Void function __output(value : Element)
  6. Element function input():
  7. while (bool_not(has_input())):
  8. interruptable_sleep(0.5)
  9. Element inp
  10. inp = __input()
  11. return inp!
  12. Void function output(value : Element):
  13. __output(value)
  14. return!
  15. Element function input_timeout(timeout : Float):
  16. Float start
  17. start = time()
  18. // Do this first, such that we will always get at least the chance to push input in
  19. if (has_input()):
  20. return input()!
  21. // Now just try this again from time to time
  22. while (time() - start < timeout):
  23. if (has_input()):
  24. return input()!
  25. else:
  26. // Nothing to do, we should yield...
  27. interruptable_sleep(0.1)
  28. return read_root()!
  29. Boolean function other_has_output(comm : String):
  30. // Check if there is any input on the specified pseudo-username
  31. Element root
  32. root = read_root()
  33. return dict_in(root[comm]["output"], "value")!
  34. Void function give_input_to_other(comm : String, value : Element):
  35. Element root
  36. root = read_root()
  37. Element new
  38. new = create_node()
  39. dict_add(root[comm]["last_input"], "value", value)
  40. dict_add(root[comm]["last_input"], "next", new)
  41. dict_overwrite(root[comm], "last_input", new)
  42. return!
  43. Element function get_output_from_other(comm : String):
  44. // Fetch input from the service
  45. while (bool_not(other_has_output(comm))):
  46. sleep(0.1)
  47. // Return the value that we got
  48. Element root
  49. Element value
  50. root = read_root()
  51. value = root[comm]["output"]["value"]
  52. dict_overwrite(root[comm], "output", root[comm]["output"]["next"])
  53. return value!