2021-01-23 16:10:29 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
//
|
|
|
|
|
// LuaConsole:
|
|
|
|
|
//
|
2021-02-02 16:29:07 -05:00
|
|
|
// Used to parse commands that are being interactively typed
|
2021-11-04 11:20:29 -04:00
|
|
|
// in by a user. The common usage pattern is:
|
2021-01-23 20:12:21 -05:00
|
|
|
//
|
2021-11-04 11:20:29 -04:00
|
|
|
// 1. Print the prompt suggested by 'get_prompt'.
|
|
|
|
|
// 2. Read a line of text from stdin.
|
|
|
|
|
// 3. Add the line to the LuaConsole using 'add'.
|
2021-11-04 14:49:25 -04:00
|
|
|
// 4. Get the command words using 'get_command'.
|
|
|
|
|
// 5. If the command is empty, do nothing.
|
2021-11-04 14:58:38 -04:00
|
|
|
// 6. If the command is nonempty, execute it.
|
2021-01-23 20:12:21 -05:00
|
|
|
//
|
2021-11-04 14:58:38 -04:00
|
|
|
// The LuaConsole expects you to type one of three things:
|
2021-01-23 20:12:21 -05:00
|
|
|
//
|
2021-11-04 14:58:38 -04:00
|
|
|
// * Valid Lua Code.
|
|
|
|
|
// * Valid Lua Code preceded by '=' as shorthand for 'return'
|
|
|
|
|
// * A slash-command.
|
2021-02-02 16:29:07 -05:00
|
|
|
//
|
2021-11-04 14:58:38 -04:00
|
|
|
// The 'get_command' returned by the luaconsole can be one of
|
|
|
|
|
// the following:
|
|
|
|
|
//
|
|
|
|
|
// 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.
|
2021-01-23 20:12:21 -05:00
|
|
|
//
|
2021-01-23 16:10:29 -05:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
#ifndef LUACONSOLE_HPP
|
|
|
|
|
#define LUACONSOLE_HPP
|
|
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
#include "luastack.hpp"
|
|
|
|
|
|
|
|
|
|
class LuaConsole {
|
|
|
|
|
public:
|
2021-02-02 16:29:07 -05:00
|
|
|
using StringVec = std::vector<std::string>;
|
|
|
|
|
|
2021-01-23 16:10:29 -05:00
|
|
|
private:
|
|
|
|
|
lua_State *lua_state_;
|
2021-02-02 16:29:07 -05:00
|
|
|
std::string raw_input_;
|
2021-01-23 16:10:29 -05:00
|
|
|
int lines_;
|
2021-02-02 16:29:07 -05:00
|
|
|
StringVec words_;
|
2021-10-07 14:58:20 -04:00
|
|
|
std::string prompt_;
|
2021-01-23 16:10:29 -05:00
|
|
|
|
2021-11-04 14:49:25 -04:00
|
|
|
void clear_raw_input();
|
2021-02-02 16:29:07 -05:00
|
|
|
|
2021-01-23 16:10:29 -05:00
|
|
|
public:
|
|
|
|
|
LuaConsole();
|
|
|
|
|
~LuaConsole();
|
|
|
|
|
|
2021-11-04 14:49:25 -04:00
|
|
|
// Fetch the recommended prompt.
|
|
|
|
|
//
|
|
|
|
|
// You should update the prompt immediately after 'add'.
|
|
|
|
|
//
|
|
|
|
|
const std::string &get_prompt() { return prompt_; }
|
2021-10-07 14:58:20 -04:00
|
|
|
|
2021-11-04 14:49:25 -04:00
|
|
|
// Fetch the command to execute.
|
2021-11-04 11:20:29 -04:00
|
|
|
//
|
2021-11-04 14:49:25 -04:00
|
|
|
// You should fetch the command after calling 'add'.
|
2021-11-04 11:20:29 -04:00
|
|
|
// Returns the empty vector if there is no command.
|
|
|
|
|
// If there is a command, the first word is the command word.
|
|
|
|
|
// See the file comment for certain built-in command words.
|
|
|
|
|
//
|
2021-11-04 14:49:25 -04:00
|
|
|
StringVec get_command();
|
2021-02-02 16:29:07 -05:00
|
|
|
|
|
|
|
|
// Add a line of text that was just read from the console.
|
2021-11-04 14:49:25 -04:00
|
|
|
//
|
2021-01-23 16:10:29 -05:00
|
|
|
void add(std::string line);
|
2021-02-02 16:29:07 -05:00
|
|
|
|
2021-01-23 16:10:29 -05:00
|
|
|
};
|
|
|
|
|
|
2021-01-23 20:12:21 -05:00
|
|
|
#endif // LUACONSOLE_HPP
|