OpenComputers Von Neumann Machine Programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.5 KiB

2 years ago
  1. local coroutine = require("coroutine")
  2. local net = require("net")
  3. local table = require("table")
  4. function eventLoop(...)
  5. -- Put in here in for later usage
  6. local done = {}
  7. -- Get running coroutines
  8. local running = {...}
  9. local canRun = true
  10. local i = 1
  11. while canRun do
  12. while coroutine.status(running[i]) == "dead" do
  13. table.insert(done, #done+1, table.remove(running, i))
  14. if #running == 0 then
  15. goto done
  16. end
  17. end
  18. if #running == 0 then
  19. canRun = false
  20. else
  21. _, pkt = coroutine.resume(running[i])
  22. if pkt ~= nil then print("Received: "..pkt.msg) end
  23. i = (i % #running) + 1
  24. end
  25. end
  26. ::done::
  27. end
  28. -- TODO finish this
  29. function betterLoop(...)
  30. -- Get running coroutines
  31. local done = {}
  32. local results = {}
  33. local running = {...}
  34. local canRun = true
  35. local i = 1
  36. while true do
  37. local coro = running[i][1]
  38. status, t = coroutine.resume(coro, slice(running[i], 2, #(running[i])))
  39. if t ~= nil then
  40. print(#t)
  41. end
  42. i = (i % #running) + 1
  43. while coroutine.status(coro) == "dead" do
  44. table.insert(results, #results+1, table.remove(running, i)) -- can do this better
  45. if #running == 0 then
  46. goto done
  47. end
  48. end
  49. end
  50. ::done::
  51. return results
  52. end
  53. function simple()
  54. print("Foo")
  55. coroutine.yield()
  56. print("Done")
  57. return "a"
  58. end
  59. local coro = coroutine.create(simple)
  60. betterLoop({coro})