75 lines
2.1 KiB
C++
75 lines
2.1 KiB
C++
//////////////////////////////////////////////////////
|
|
//
|
|
// LuaConsole:
|
|
//
|
|
// Used to parse commands that are being interactively typed
|
|
// in by a user. The common usage pattern is:
|
|
//
|
|
// 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'.
|
|
// 4. Get the command words using 'words'.
|
|
// 5. If the words are empty, do nothing.
|
|
// 6. If the words contain something, execute and call 'clear'.
|
|
//
|
|
// The 'words' returned by the luaconsole can be empty, meaning
|
|
// 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
|
|
// syntax <message> - print a syntax error message
|
|
//
|
|
// If the first word is anything else, it means the user typed
|
|
// that word on the command line. It is up to you to interpret
|
|
// such commands.
|
|
//
|
|
//////////////////////////////////////////////////////
|
|
|
|
#ifndef LUACONSOLE_HPP
|
|
#define LUACONSOLE_HPP
|
|
|
|
#include <string>
|
|
#include "luastack.hpp"
|
|
|
|
class LuaConsole {
|
|
public:
|
|
using StringVec = std::vector<std::string>;
|
|
|
|
private:
|
|
lua_State *lua_state_;
|
|
std::string raw_input_;
|
|
int lines_;
|
|
StringVec words_;
|
|
std::string prompt_;
|
|
|
|
void split_words();
|
|
void clear_command();
|
|
|
|
public:
|
|
LuaConsole();
|
|
~LuaConsole();
|
|
|
|
// Fetch the stored prompt. Also clears the stored prompt. You should fetch
|
|
// and print the prompt after 'add' or 'clear'.
|
|
std::string get_prompt();
|
|
|
|
// Get the command words.
|
|
//
|
|
// 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.
|
|
//
|
|
const StringVec &words() const { return words_; }
|
|
|
|
// Add a line of text that was just read from the console.
|
|
// If more input is needed, stores the ">> " prompt.
|
|
void add(std::string line);
|
|
|
|
// Call 'clear' after executing an action.
|
|
// Stores the "> " prompt.
|
|
void clear();
|
|
};
|
|
|
|
#endif // LUACONSOLE_HPP
|