View difference between Paste ID: kXJ002ya and Dh7mfnJS
SHOW: | | - or go back to the newest paste.
1
local function shell()
2
  dofile("rom/programs/shell")
3
end
4
5
local function pop(tab,index)
6
  local size = #tab
7
  tab[index] = nil
8
  for i=index+1,size do
9
    tab[i-1] = tab[i]
10
    tab[i] = nil
11
  end
12
end
13
14
15
threads = {}
16
threads.main = {}
17
18
threads.add = function(func,name)
19
  name = name or "[Untitled]"
20
  local threadid = coroutine.create(func)
21
  local threadTable = {}
22
  threadTable[1] = name
23
  threadTable[2] = threadid
24
  threads.main[#threads.main+1] = threadTable
25
end
26
27
threads.pullEvent = function(event)
28
  local ret = {coroutine.yield(event)}
29
  return table.unpack(ret)
30
end
31
32
threads.stop = function()
33
  coroutine.yield("end")
34
end
35
36
threads.run = function()
37
  os.queueEvent("start")
38
  while true do
39
    local event = {os.pullEventRaw())
40
	if threads[event[1]] then
41-
      local newEvent = coroutine.resume(v[2],table.unpack(event))
41+
42-
      if not (newEvent == nil) then
42+
43-
        if not threads[newEvent] then
43+
        if not newEvent then newEvent =  "main" end
44-
          threads[newEvent] = {}
44+
		if not (event[1] == newEvent) then
45-
        end
45+
			if not threads[newEvent] then
46-
        threads[newEvent][#threads[newEvent]+1] = v
46+
				threads[newEvent] = {}
47-
        pop(threads.main,i)
47+
			end
48
			threads[newEvent][#threads[newEvent]+1] = v
49
			if newEvent == "main" then v[3] = false end
50-
    if threads[event[1]] then
50+
			pop(threads[event[1]],i)
51
		end
52
	  end
53-
        if not newEvent then newEvent      
53+
	end
54
    for i,v in pairs(threads.main) do
55
	  if v[3] == nil then v[3] = true end
56
	  if v[3] then
57-
threads.add(function() dofile("rom/programs/shell") end)
57+
		local newEvent = coroutine.resume(v[2],table.unpack(event))
58
		if not (newEvent == nil) then
59
			if not threads[newEvent] then
60
				threads[newEvent] = {}
61
			end
62
			threads[newEvent][#threads[newEvent]+1] = v
63
			pop(threads.main,i)
64
		end
65
      end
66
	  v[3] = true
67
    end
68
  end
69
end
70
        
71
72
73
os.pullEvent = threads.pullEvent
74
threads.add(function() dofile("rom/programs/shell") end)
75
threads.run()