Lots of work on the lua read-eval-print loop

This commit is contained in:
2026-05-21 18:41:09 -04:00
parent f7983b1f02
commit 2bfa3024f1
15 changed files with 70 additions and 120 deletions

View File

@@ -336,7 +336,7 @@ eng::string LuaCoreStack::load(LuaSlot result, std::string_view code, std::strin
const char *str = lua_tolstring(L_, -1, &len);
eng::string message(str, len);
lua_pop(L_, 1);
if (sv::has_suffix(message, "near <eof>"))
if (sv::has_suffix(message, "near <eof>") && sv::is_possible_long_lua_expression(code))
{
message = "truncated lua";
}

View File

@@ -192,7 +192,6 @@ bool PrintChanneler::channel(const PrintBuffer *printbuffer, StreamBuffer *sb) {
line_ = printbuffer->first_line();
}
while (line_ < printbuffer->first_unchecked()) {
sb->write_bytes("|");
sb->write_bytes(printbuffer->nth(line_));
sb->write_bytes("\n");
line_ += 1;

View File

@@ -200,6 +200,24 @@ bool is_lua_comment(string_view s) {
return s.substr(start, 2) == "--";
}
bool is_possible_long_lua_expression(string_view s) {
read_space(s);
string_view id = read_lua_identifier(s);
if (id.empty()) return false;
if ((id == "function") || (id == "if") || (id == "while") || (id == "for") || (id == "repeat") || (id == "do")) return true;
if (id == "local")
{
read_space(s);
id = read_lua_identifier(s);
}
read_space(s);
read_prefix(s, "="); // If not present, returns false but we continue anyway.
read_space(s);
if (has_prefix(s, "[")) return true;
if (has_prefix(s, "(")) return true;
return false;
}
bool is_whitespace(string_view s) {
for (int i = 0; i < int(s.size()); i++) {
if (!ascii_isspace(s[i])) {

View File

@@ -106,6 +106,14 @@ bool is_lua_classname(string_view s);
// Return true if the line of code is a lua comment.
bool is_lua_comment(string_view s);
// Return true if the line of code could be the beginning of a long expression.
// In a read-eval-print loop, if the user types something like "function foo",
// that's not a complete lua expression. But we don't want to just print an error,
// we want to give the user a chance to continue typing so that he can turn it
// into a complete lua expression. This function returns true if the string looks
// like the beginning of a long lua expression. This is only a heuristic.
bool is_possible_long_lua_expression(string_view s);
// Return true if the line is entirely whitespace.
bool is_whitespace(string_view s);

View File

@@ -1071,6 +1071,10 @@ void World::run_scheduled_threads() {
PrettyPrint::Indented().print(LSCO, LuaSpecial(i), &lthread_prints_);
lthread_prints_ << std::endl;
}
if (lthread_prints_.view().empty())
{
lthread_prints_ << "ok\n";
}
}
} else if (status == LUA_YIELD) {
if (is_authoritative()) {

View File

@@ -40,6 +40,7 @@ function cube.lookmenu(add)
add("Cube Hi", function () dprint("Doing Cube Hi") end)
add("Cube Bye", function () dprint("Doing Cube Bye") end)
add("Cube Yo", function () dprint("Doing Cube Yo") end)
add("Cube Z", function () dprint("Doing Cube Z") end)
end
function sphere.lookhotkeys(add)