Tidying up error handling and REPL.

This commit is contained in:
Bruce Hill 2018-06-19 15:24:24 -07:00
parent 7deed5af41
commit c1cec2ac84
4 changed files with 33 additions and 27 deletions

View File

@ -49,8 +49,8 @@ debug.getinfo = function(thread, f, what)
end
return info
end
local print_err_msg
print_err_msg = function(error_message, stack_offset)
local print_error
print_error = function(error_message, stack_offset)
if stack_offset == nil then
stack_offset = 3
end
@ -194,13 +194,17 @@ print_err_msg = function(error_message, stack_offset)
end
return io.stderr:flush()
end
local err_hand
err_hand = function(error_message)
print_err_msg(error_message)
local error_handler
error_handler = function(error_message)
print_error(error_message)
return os.exit(false, true)
end
local safe_run
safe_run = function(fn)
return xpcall(fn, err_hand)
local run_safely
run_safely = function(fn)
return xpcall(fn, error_handler)
end
return safe_run
return {
run_safely = run_safely,
print_error = print_error,
error_handler = error_handler
}

View File

@ -32,7 +32,7 @@ debug.getinfo = (thread,f,what)->
info.short_src = info.source\match('@([^[]*)') or info.short_src
return info
print_err_msg = (error_message, stack_offset=3)->
print_error = (error_message, stack_offset=3)->
io.stderr\write("#{colored.red "ERROR:"} #{colored.bright colored.red (error_message or "")}\n")
io.stderr\write("stack traceback:\n")
@ -120,11 +120,11 @@ print_err_msg = (error_message, stack_offset=3)->
io.stderr\flush!
err_hand = (error_message)->
print_err_msg error_message
error_handler = (error_message)->
print_error error_message
os.exit(false, true)
safe_run = (fn)->
xpcall(fn, err_hand)
run_safely = (fn)->
xpcall(fn, error_handler)
return safe_run
return {:run_safely, :print_error, :error_handler}

View File

@ -15,7 +15,7 @@ OPTIONS
]=]
local lpeg = require('lpeg')
local re = require('re')
local run_safely = require("error_handling")
local Errhand = require("error_handling")
local NomsuCompiler = require("nomsu_compiler")
local NomsuCode, LuaCode, Source
do
@ -173,16 +173,17 @@ run = function()
break
end
buff = table.concat(buff)
FILE_CACHE["REPL#" .. repl_line] = buff
local pseudo_filename = "user input #" .. repl_line
FILE_CACHE[pseudo_filename] = buff
local err_hand
err_hand = function(error_message)
return print_err_msg(error_message)
return Errhand.print_error(error_message)
end
local ok, ret = xpcall(nomsu.run, err_hand, nomsu, buff, Source("REPL#" .. repl_line, 1, #buff))
local ok, ret = xpcall(nomsu.run, err_hand, nomsu, buff, Source(pseudo_filename, 1, #buff))
if ok and ret ~= nil then
print("= " .. repr(ret))
elseif not ok then
print_err_msg(ret)
Errhand.print_error(ret)
end
end
end
@ -191,5 +192,5 @@ local has_ldt, ldt = pcall(require, 'ldt')
if has_ldt then
return ldt.guard(run)
else
return run_safely(run)
return Errhand.run_safely(run)
end

View File

@ -19,7 +19,7 @@ OPTIONS
lpeg = require 'lpeg'
re = require 're'
run_safely = require "error_handling"
Errhand = require "error_handling"
NomsuCompiler = require "nomsu_compiler"
{:NomsuCode, :LuaCode, :Source} = require "code_obj"
STDIN, STDOUT, STDERR = "/dev/fd/0", "/dev/fd/1", "/dev/fd/2"
@ -148,17 +148,18 @@ run = ->
break -- Exit
buff = table.concat(buff)
FILE_CACHE["REPL#"..repl_line] = buff
pseudo_filename = "user input #"..repl_line
FILE_CACHE[pseudo_filename] = buff
err_hand = (error_message)->
print_err_msg error_message
ok, ret = xpcall(nomsu.run, err_hand, nomsu, buff, Source("REPL#"..repl_line, 1, #buff))
Errhand.print_error error_message
ok, ret = xpcall(nomsu.run, err_hand, nomsu, buff, Source(pseudo_filename, 1, #buff))
if ok and ret != nil
print "= "..repr(ret)
elseif not ok
print_err_msg ret
Errhand.print_error ret
has_ldt, ldt = pcall(require,'ldt')
if has_ldt
ldt.guard(run)
else
run_safely(run)
Errhand.run_safely(run)