CC-Tweaked/src/main/resources/data/computercraft/lua/rom/programs/lua.lua

111 lines
3.5 KiB
Lua
Raw Normal View History

local tArgs = { ... }
if #tArgs > 0 then
print("This is an interactive Lua prompt.")
print("To run a lua program, just type its name.")
return
end
local pretty = require "cc.pretty"
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
local exception = require "cc.internal.exception"
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
local running = true
local tCommandHistory = {}
local tEnv = {
["exit"] = setmetatable({}, {
2020-01-15 09:29:11 +00:00
__tostring = function() return "Call exit() to exit." end,
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
__call = function() running = false end,
}),
["_echo"] = function(...)
return ...
end,
}
setmetatable(tEnv, { __index = _ENV })
-- Replace our require with new instance that loads from the current directory
-- rather than from /rom/programs. This makes it more friendly to use and closer
-- to what you'd expect.
do
local make_package = require "cc.require".make
local dir = shell.dir()
_ENV.require, _ENV.package = make_package(_ENV, dir)
end
if term.isColour() then
term.setTextColour(colours.yellow)
end
print("Interactive Lua prompt.")
print("Call exit() to exit.")
term.setTextColour(colours.white)
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
local chunk_idx, chunk_map = 1, {}
while running do
--if term.isColour() then
-- term.setTextColour( colours.yellow )
--end
write("lua> ")
--term.setTextColour( colours.white )
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
local input = read(nil, tCommandHistory, function(sLine)
if settings.get("lua.autocomplete") then
local nStartPos = string.find(sLine, "[a-zA-Z0-9_%.:]+$")
if nStartPos then
sLine = string.sub(sLine, nStartPos)
end
if #sLine > 0 then
return textutils.complete(sLine, tEnv)
end
end
return nil
end)
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
if input:match("%S") and tCommandHistory[#tCommandHistory] ~= input then
table.insert(tCommandHistory, input)
end
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
if settings.get("lua.warn_against_use_of_local") and input:match("^%s*local%s+") then
if term.isColour() then
term.setTextColour(colours.yellow)
end
print("To access local variables in later inputs, remove the local keyword.")
term.setTextColour(colours.white)
end
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
local name, offset = "=lua[" .. chunk_idx .. "]", 0
local func, err = load(input, name, "t", tEnv)
if load("return " .. input) then
-- We wrap the expression with a call to _echo(...), which prevents tail
-- calls (and thus confusing errors). Note we check this is a valid
-- expression separately, to avoid accepting inputs like `)--` (which are
-- parsed as `_echo()--)`.
func = load("return _echo(" .. input .. "\n)", name, "t", tEnv)
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
offset = 13
end
if func then
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
chunk_map[name] = { contents = input, offset = offset }
chunk_idx = chunk_idx + 1
local results = table.pack(exception.try(func))
if results[1] then
for i = 2, results.n do
local value = results[i]
local ok, serialised = pcall(pretty.pretty, value, {
function_args = settings.get("lua.function_args"),
function_source = settings.get("lua.function_source"),
})
if ok then
pretty.print(serialised)
else
print(tostring(value))
end
end
else
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
printError(results[2])
require "cc.internal.exception".report(results[2], results[3], chunk_map)
end
else
Cherry pick several changes back from 1.19.3 The main purpose of this is to backport the improved parse/runtime errors to older versions. I think they're sufficiently useful that we should try to make it as widely available as possible. We've been running them for a week now on SC3 and the released version and not seen any issues, so I think it's probably stable enough. This is a pretty lazy commit: I ended up copying the whole ROM over and then picking up a few other related changes along the way. - Trim spaces from file paths (b8fce1eecccae652d1128fcf50b57a09eda69dca) - Correctly format 12AM/PM with %I (9f48395596131a932fbc37644fe1e4b15ffb6a61) - Fix http.request and htpt.websocketAsync not handling a few failure edge-cases correctly (3b42f22a4f36dad0c53bb238e64aada352a063cf). - Move the internal modules into the main package path, hidden under cc.internal (34a31abd9ce9106b84549ade2cc30524016107c9). - Gather code coverage in Java instead of Lua (28a55349a961c0739adc9d52fc3761c463678be9). - Make error messages in edit more obvious (8cfbfe7ceb35e87579b4f6fe8c892e6bce9ed0eb). - Make mcfly's test methods global. This means we don't need to pass stub everywhere (7335a892b5742f7879a4ca07f059cd7b8136aa3a). - Improve runtime and parse errors. This comes from numerous commits, but chiefly a12b405acfb63d58d6a895e8a8a139ef5c42fbfc, and 55024121817bb112ea68d30e7cb5511a16ccfc94. - Hide the internal redirect methods in multishell (33b6f383397d51074bd504a4067253ae65f5b77c). Note this does /not/ include the shebang changes (sorry Emma!). I've tried to avoid adding any user-controllable features, mostly because I don't know how to handle the versioning otherwise :).
2023-02-14 09:45:02 +00:00
local parser = require "cc.internal.syntax"
if parser.parse_repl(input) then printError(err) end
end
end