From cc24a339d8c0517259084b5c178d784626ba965c Mon Sep 17 00:00:00 2001 From: ImJezze Date: Sun, 21 Feb 2016 11:48:45 +0100 Subject: Merge remote-tracking branch 'refs/remotes/mamedev/master' Second attempt --- 3rdparty/luv/examples/repl.lua | 89 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 3rdparty/luv/examples/repl.lua (limited to '3rdparty/luv/examples/repl.lua') diff --git a/3rdparty/luv/examples/repl.lua b/3rdparty/luv/examples/repl.lua new file mode 100644 index 00000000000..92be0f17d87 --- /dev/null +++ b/3rdparty/luv/examples/repl.lua @@ -0,0 +1,89 @@ +local uv = require('luv') +local utils = require('lib/utils') + +if uv.guess_handle(0) ~= "tty" or + uv.guess_handle(1) ~= "tty" then + error "stdio must be a tty" +end +local stdin = uv.new_tty(0, true) +local stdout = require('lib/utils').stdout + +local debug = require('debug') +local c = utils.color + +local function gatherResults(success, ...) + local n = select('#', ...) + return success, { n = n, ... } +end + +local function printResults(results) + for i = 1, results.n do + results[i] = utils.dump(results[i]) + end + print(table.concat(results, '\t')) +end + +local buffer = '' + +local function evaluateLine(line) + if line == "<3\n" then + print("I " .. c("Bred") .. "♥" .. c() .. " you too!") + return '>' + end + local chunk = buffer .. line + local f, err = loadstring('return ' .. chunk, 'REPL') -- first we prefix return + + if not f then + f, err = loadstring(chunk, 'REPL') -- try again without return + end + + if f then + buffer = '' + local success, results = gatherResults(xpcall(f, debug.traceback)) + + if success then + -- successful call + if results.n > 0 then + printResults(results) + end + else + -- error + print(results[1]) + end + else + + if err:match "''$" then + -- Lua expects some more input; stow it away for next time + buffer = chunk .. '\n' + return '>>' + else + print(err) + buffer = '' + end + end + + return '>' +end + +local function displayPrompt(prompt) + uv.write(stdout, prompt .. ' ') +end + +local function onread(err, line) + if err then error(err) end + if line then + local prompt = evaluateLine(line) + displayPrompt(prompt) + else + uv.close(stdin) + end +end + +coroutine.wrap(function() + displayPrompt '>' + uv.read_start(stdin, onread) +end)() + +uv.run() + +print("") -- cgit v1.2.3-70-g09d2