mirror of
https://github.com/curioustorvald/Terrarum.git
synced 2026-06-12 11:34:05 +09:00
proper error handling in ROMBASIC
Former-commit-id: b5bd084e6807c765cdd6d3ffff1b1628321b9c6a Former-commit-id: 55c3bb3cd56c7867809c0819f178aeebf1e46676
This commit is contained in:
@@ -19,8 +19,8 @@ _G.DLE = string.char(16) -- default error colour
|
||||
_G.getMem = function() collectgarbage() return collectgarbage("count") * 1024 end
|
||||
-- getTotalMem: implemented in Kotlin class
|
||||
_G.getFreeMem = function() return getTotalMem() - getMem() end
|
||||
_G.runscript = function(s, env)
|
||||
local code, reason = load(s, env)
|
||||
_G.runscript = function(s, source, ...)
|
||||
local code, reason = load(s, source)
|
||||
|
||||
if _G.getFreeMem() <= 0 then
|
||||
print("out of memory")
|
||||
@@ -29,9 +29,9 @@ _G.runscript = function(s, env)
|
||||
end
|
||||
|
||||
if code then
|
||||
xpcall(code, eprint)
|
||||
xpcall(code(...), eprint)
|
||||
else
|
||||
print(DLE..tostring(reason))
|
||||
print(DLE..tostring(reason)) -- it catches syntax errors
|
||||
end
|
||||
end
|
||||
_G.__scanMode__ = "UNINIT" -- part of inputstream implementation
|
||||
@@ -59,16 +59,22 @@ if shell.status == shell.halt then
|
||||
end
|
||||
|
||||
-- load Lua prompt, if bios is not found
|
||||
if (#_COMPUTER.OEM > 0) then print(_COMPUTER.OEM) end
|
||||
print("Rom basic "..DC2.._VERSION..DC4)
|
||||
print("Copyright (C) 1994-2015 Lua.org, PUC-Rio")
|
||||
print(DC2..tostring(math.floor(getFreeMem()/1024+0.5))..DC4.." Kbytes free")
|
||||
print("To boot your system, run 'boot()'")
|
||||
print("Ok")
|
||||
|
||||
while not native.isHalted() do
|
||||
io.write(_COMPUTER.prompt)
|
||||
local s = io.read()
|
||||
runscript(s, "=stdin")
|
||||
while not native.isHalted() do
|
||||
io.write(_COMPUTER.prompt)
|
||||
local s = io.read()
|
||||
xpcall(
|
||||
function() _G.runscript(s, "=stdin") end,
|
||||
function(s) print(DLE..s) end -- it catches logical errors
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
native.closeInputString()
|
||||
__haltsystemexplicit__()
|
||||
return
|
||||
|
||||
@@ -6,10 +6,10 @@
|
||||
-- ALIASES --
|
||||
-------------
|
||||
|
||||
fs.dofile = function(p)
|
||||
fs.dofile = function(p, ...)
|
||||
local f = fs.open(p, "r")
|
||||
local s = f.readAll()
|
||||
_G.runscript(s, "="..p)
|
||||
_G.runscript(s, "="..p, ...)
|
||||
end
|
||||
|
||||
_G.loadstring = _G.load
|
||||
@@ -18,6 +18,15 @@ _G.print = term.print
|
||||
|
||||
--_G.dofile = function(f) fs.dofile(f) end
|
||||
|
||||
_G.boot = function() fs.dofile("/boot/efi") end
|
||||
|
||||
|
||||
fs.fetchText = function(p)
|
||||
local file = fs.open(p, "r")
|
||||
local text = file.readAll()
|
||||
file.close()
|
||||
return text
|
||||
end
|
||||
|
||||
-----------------------------------------
|
||||
-- INPUTSTREAM AND SCANNER (java-like) --
|
||||
@@ -38,9 +47,9 @@ override fun keyPressed(key: Int, c: Char) {
|
||||
...it basically says to close the input if RETURN is hit,
|
||||
and THIS exact part will close the input for this function.
|
||||
]]
|
||||
_G.__scanForLine__ = function()
|
||||
_G.__scanforline__ = function(echo) -- pass '1' to not echo; pass nothing to echo
|
||||
native.closeInputString()
|
||||
native.openInput()
|
||||
native.openInput(echo or 0)
|
||||
_G.__scanMode__ = "line"
|
||||
local s
|
||||
repeat -- we can do this ONLY IF lua execution process is SEPARATE THREAD
|
||||
@@ -51,9 +60,9 @@ _G.__scanForLine__ = function()
|
||||
end
|
||||
|
||||
-- use Keys API to identify the keycode
|
||||
_G.__scanForChar__ = function()
|
||||
_G.__scanforchar__ = function(echo) -- pass '1' to not echo; pass nothing to echo
|
||||
native.closeInputString()
|
||||
native.openInput()
|
||||
native.openInput(echo or 0)
|
||||
_G.__scanMode__ = "a_key"
|
||||
local key
|
||||
repeat -- we can do this ONLY IF lua execution process is SEPARATE THREAD
|
||||
@@ -63,7 +72,7 @@ _G.__scanForChar__ = function()
|
||||
return key
|
||||
end
|
||||
|
||||
io.read = _G.__scanForLine__
|
||||
io.read = _G.__scanforline__
|
||||
|
||||
|
||||
-----------------
|
||||
|
||||
Reference in New Issue
Block a user