Modify luaconsole to use slash-command syntax

This commit is contained in:
2021-11-04 14:58:38 -04:00
parent 7966707450
commit bc01bd8513
2 changed files with 17 additions and 24 deletions

View File

@@ -4,10 +4,6 @@
#include "util.hpp" #include "util.hpp"
#include <iostream> #include <iostream>
static bool is_single_letter(const std::string &s) {
return ((s.size() == 1) && (s[0] >= 'a') && (s[0] <= 'z'));
}
LuaConsole::LuaConsole() { LuaConsole::LuaConsole() {
lua_state_ = luaL_newstate(); lua_state_ = luaL_newstate();
clear_raw_input(); clear_raw_input();
@@ -57,16 +53,11 @@ void LuaConsole::add(std::string line) {
lines_ += 1; lines_ += 1;
prompt_ = ">> "; prompt_ = ">> ";
// Try to interpret it as a special command. // Try to interpret it as a slash-command.
if (lines_ == 1) { if ((lines_ == 1)&&(raw_input_[0] == '/')) {
StringVec words = split_words(raw_input_); words_ = split_words(raw_input_.substr(1));
if (words.size() >= 1) { clear_raw_input();
if (is_single_letter(words[0]) || (util::validinteger(words[0]))) { return;
words_ = words;
clear_raw_input();
return;
}
}
} }
// Translate lua expression with leading '=' to 'return' // Translate lua expression with leading '=' to 'return'

View File

@@ -10,19 +10,21 @@
// 3. Add the line to the LuaConsole using 'add'. // 3. Add the line to the LuaConsole using 'add'.
// 4. Get the command words using 'get_command'. // 4. Get the command words using 'get_command'.
// 5. If the command is empty, do nothing. // 5. If the command is empty, do nothing.
// 6. If the command is nonempty, execute it and call 'clear'. // 6. If the command is nonempty, execute it.
// //
// The 'words' returned by the luaconsole can be empty, meaning // The LuaConsole expects you to type one of three things:
// that there's no command to execute. If the words are nonempty,
// then the first word is the command. There are two hardwired
// commands:
// //
// lua <expr> - execute a lua expression // * Valid Lua Code.
// syntax <message> - print a syntax error message // * Valid Lua Code preceded by '=' as shorthand for 'return'
// * A slash-command.
// //
// If the first word is anything else, it means the user typed // The 'get_command' returned by the luaconsole can be one of
// that word on the command line. It is up to you to interpret // the following:
// such commands. //
// empty vector - still collecting input: do nothing.
// lua <expr> - found a lua command: execute it as lua.
// syntax <words> - detected a syntax error: print the error.
// word word ... - read a slash-command: execute the command.
// //
////////////////////////////////////////////////////// //////////////////////////////////////////////////////