local pprint=require"pprint" pprint.setup{show_all=true,use_tostring=true} local repl_env = {} -- 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) 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 local function msgh(e) return debug.traceback(e,1) end while true do local f,err = read_chunk() if not f then print("load error: "..err) else result(xpcall(f,msgh)) end end