local pprint=require"pprint" pprint.setup{show_all=true,use_tostring=true} local repl_env = {} local function reload(modname) local oldmod = require(modname) assert(type(oldmod) == "table","reloading only works on modules that return tables") package.loaded[modname] = nil local newmod = require(modname) package.loaded[modname]=oldmod assert(type(newmod) == "table","new module doesn't seem to be a table, cancelling") for k in pairs(oldmod) do oldmod[k] = nil end for k,v in pairs(newmod) do oldmod[k] = v end end -- should use loadstring on 5.1 -- works on luajit though local function load_chunk(s) return load(s,"repl","t") end local function prompt(p) io.write(p) return io.read() end local function read_chunk() -- try with return -- if that doesn't work try without return -- if error ends in try multiline local function try(text) if text:sub(1,3) == ",r " then return function() reload(text:sub(4)) print("reloaded "..text:sub(4)) end end local f, err = load_chunk("return "..text..";") if f then return f end local f, err = load_chunk(text) if f then return f end if err:sub(-5) == "" or err:sub(-7) == "''" then local nt = text.."\n"..prompt("} ") return try(nt) end return f, err end return try(prompt("] ")) end local function result(ok,...) if ok then pprint(...) else print("error: "..(...)) end end while true do local f,err = read_chunk() if not f then print("load error: "..err) else result(xpcall(f,function(e) return debug.traceback(e,1) end)) end end