Freack100

Debugger

Aug 29th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.25 KB | None | 0 0
  1. local args = {...}
  2. local fileName = args[1]
  3. local debugKey = keys.f2
  4. local running = true
  5. local programIsRunning = true
  6. local env = {}
  7. local fileContent = {}
  8. local handle = fs.open(fileName,"r")
  9. local sLine = handle.readLine()
  10. while sLine do
  11.     fileContent[#fileContent+1] = sLine:gsub("local","") --make all variables global
  12.     sLine = handle.readLine()
  13. end
  14. handle.close()
  15. local handle = fs.open(".debug_file","w")
  16. for k,v in pairs(fileContent) do
  17.     handle.writeLine(v.."\n")
  18. end
  19. handle.close()
  20.  
  21. local function run()
  22.     dofile(".debug_file")
  23. end
  24. setfenv(run, env)
  25.  
  26. local function debug()
  27.     while true do
  28.         sleep(0)
  29.         term.setBackgroundColor(colors.lightGray)
  30.         term.clear()
  31.         term.setCursorPos(1,1)
  32.         for key,var in pairs(env) do
  33.             print(key, " = ", var)
  34.         end
  35.     end
  36. end
  37.  
  38. local runCo = coroutine.create(run)
  39. local debugCo = coroutine.create(debug)
  40. coroutine.resume(runCo)
  41. while running do
  42.     local evt = {os.pullEvent()}
  43.     if evt[1] == "key"and evt[2] == debugKey then
  44.         programIsRunning = not programIsRunning
  45.     end
  46.     if programIsRunning then
  47.         if coroutine.status(runCo) ~= "dead" then
  48.             coroutine.resume(runCo, unpack(evt))
  49.         end
  50.     else
  51.         if coroutine.status(debugCo) ~= "dead" then
  52.             coroutine.resume(debugCo, unpack(evt))
  53.         end
  54.     end
  55. end
Advertisement
Add Comment
Please, Sign In to add comment