function p(first, ...)
  local dump = require('utils').dump
  local l = dump(first)
  for i, v in ipairs{...} do
    l = l .. "\t" .. dump(v)
  end
  print(l)
end

if not process.argv[1] then
  print("usage:\n\t" .. process.argv[0] .. " progname.lua\n")
  return;
end

dofile(process.argv[1])

require('uv').run()


-- Dump args to the screen for debugging
function p(...)
  local n = select('#', ...)
  local arguments = { n = n, ... }
  local dump = require('utils').dump
  local parts = {}

  for i = 1, arguments.n do
    parts[i] = dump(arguments[i])
  end

  print(table.concat(parts, "\t"))
end


if process.argv[1] then
  dofile(process.argv[1])
else
  -- TODO: make repl non-blocking

  local dump = require('utils').dump

  local function gather_results(success, ...)
    local n = select('#', ...)
    return success, { n = n, ... }
  end

  local function print_results(results)
    local parts = {}

    for i = 1, results.n do
      parts[#parts + 1] = dump(results[i])
    end

    print(table.concat(parts, '\t'))
  end


  do
    local buffer = ''

    function evaluate_line(line)
      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 = gather_results(xpcall(f, debug.traceback))

        if success then
          -- successful call
          if results.n > 0 then
            print_results(results)
          end
        else
          -- error
          print(results[1])
        end
      else

        if err:match "'<eof>'$" then
          -- Lua expects some more input; stow it away for next time
          buffer = chunk .. '\n'
          return '>>'
        else
          print(err)
        end
      end

      return '>'
    end
  end

  function display_prompt(prompt)
    io.stdout:write(prompt .. ' ')
  end

  display_prompt '>'
  for line in io.stdin:lines() do
    local prompt = evaluate_line(line)
    display_prompt(prompt)
  end
end

require('uv').run()


Add a blocking repl to the executable. TODO: make non-blocking
Add a blocking repl to the executable.  TODO: make non-blocking

Change-Id: I36dee78fb8a73b9256706817c0b31fbeabaefa21
