Created AnimViewMap
This commit is contained in:
172
luprex/syscpp/textgame.cpp
Normal file
172
luprex/syscpp/textgame.cpp
Normal file
@@ -0,0 +1,172 @@
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "luastack.hpp"
|
||||
#include "util.hpp"
|
||||
#include "viewer.hpp"
|
||||
#include "traceback.hpp"
|
||||
#include "textgame.hpp"
|
||||
|
||||
// Add another error status.
|
||||
|
||||
static lua_State *globalL = NULL;
|
||||
|
||||
|
||||
static void lstop(lua_State *L, lua_Debug *ar)
|
||||
{
|
||||
(void)ar; /* unused arg. */
|
||||
lua_sethook(L, NULL, 0, 0);
|
||||
/* Avoid luaL_error -- a C hook doesn't add an extra frame. */
|
||||
luaL_where(L, 0);
|
||||
lua_pushfstring(L, "%sinterrupted!", lua_tostring(L, -1));
|
||||
lua_error(L);
|
||||
}
|
||||
|
||||
static void laction(int i)
|
||||
{
|
||||
signal(i, SIG_DFL); /* if another SIGINT happens before lstop,
|
||||
terminate process (default action) */
|
||||
lua_sethook(globalL, lstop, LUA_MASKCALL | LUA_MASKRET | LUA_MASKCOUNT, 1);
|
||||
}
|
||||
|
||||
static void l_message(const char *msg)
|
||||
{
|
||||
fputs(msg, stderr);
|
||||
fputc('\n', stderr);
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
static int report(lua_State *L, int status)
|
||||
{
|
||||
if (status && !lua_isnil(L, -1))
|
||||
{
|
||||
const char *msg = lua_tostring(L, -1);
|
||||
if (msg == NULL)
|
||||
msg = "(error object is not a string)";
|
||||
l_message(msg);
|
||||
lua_pop(L, 1);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
static int docall(lua_State *L, int narg, int nret)
|
||||
{
|
||||
signal(SIGINT, laction);
|
||||
int status = traceback_pcall(L, narg, nret);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
/* force a complete garbage collection in case of errors */
|
||||
if (status != LUA_OK)
|
||||
lua_gc(L, LUA_GCCOLLECT, 0);
|
||||
return status;
|
||||
}
|
||||
|
||||
// Print a prompt, read a line of text, and push it on the stack.
|
||||
// If it's the first line, translate "=" to "return"
|
||||
//
|
||||
static int pushline(lua_State *L, int firstline)
|
||||
{
|
||||
const int MAXINPUT = 1000;
|
||||
char buf[MAXINPUT];
|
||||
fputs(firstline ? "> " : ">> ", stdout);
|
||||
fflush(stdout);
|
||||
if (fgets(buf, MAXINPUT, stdin))
|
||||
{
|
||||
size_t len = strlen(buf);
|
||||
if (len > 0 && buf[len - 1] == '\n')
|
||||
buf[len - 1] = '\0';
|
||||
if (firstline && buf[0] == '=')
|
||||
lua_pushfstring(L, "return %s", buf + 1);
|
||||
else
|
||||
lua_pushstring(L, buf);
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Same as lua_loadbuffer, except: if the form passed in
|
||||
// is a partial form, returns LUA_ERRPARTIAL.
|
||||
//
|
||||
static int loadbuffer_partial(lua_State *L, const char *s, int len, const char *fn)
|
||||
{
|
||||
const char *eof = "'<eof>'";
|
||||
int status = luaL_loadbuffer(L, s, len, fn);
|
||||
if (status != LUA_ERRSYNTAX)
|
||||
{
|
||||
return status;
|
||||
}
|
||||
size_t lmsg;
|
||||
const char *msg = lua_tolstring(L, -1, &lmsg);
|
||||
const char *tp = msg + lmsg - (sizeof(eof) - 1);
|
||||
if (strstr(msg, eof) == tp)
|
||||
{
|
||||
return LUA_ERRPARTIAL;
|
||||
}
|
||||
else
|
||||
{
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
// Read an entire form. On success, returns a closure. On EOF,
|
||||
// returns just the LUA_ERREOF status code. On failure, returns an
|
||||
// error message and a status code.
|
||||
static int read_and_load(lua_State *L)
|
||||
{
|
||||
int status;
|
||||
lua_settop(L, 0);
|
||||
if (!pushline(L, 1))
|
||||
return LUA_ERREOF; /* no input */
|
||||
for (;;)
|
||||
{ /* repeat until gets a complete line */
|
||||
status = loadbuffer_partial(L, lua_tostring(L, 1), lua_strlen(L, 1), "=stdin");
|
||||
if (status != LUA_ERRPARTIAL)
|
||||
break;
|
||||
lua_pop(L, 1); /* pop the error message */
|
||||
if (!pushline(L, 0)) /* no more input? */
|
||||
return LUA_ERREOF;
|
||||
lua_pushliteral(L, "\n"); /* add a new line... */
|
||||
lua_insert(L, -2); /* ...between the two lines */
|
||||
lua_concat(L, 3); /* join them */
|
||||
}
|
||||
lua_remove(L, 1); /* remove line */
|
||||
return status;
|
||||
}
|
||||
|
||||
static void dotty(lua_State *L)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
int status = read_and_load(L);
|
||||
if (status == LUA_ERREOF) break;
|
||||
if (status == LUA_OK) status = docall(L, 0, LUA_MULTRET);
|
||||
report(L, status);
|
||||
if (status == LUA_OK && lua_gettop(L) > 0)
|
||||
{ /* any result to print? */
|
||||
lua_getglobal(L, "pprint");
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, 1);
|
||||
lua_getglobal(L, "print");
|
||||
}
|
||||
lua_insert(L, 1);
|
||||
if (lua_pcall(L, lua_gettop(L) - 1, 0, 0) != 0)
|
||||
{
|
||||
l_message(
|
||||
lua_pushfstring(L, "error calling 'print' (%s)",
|
||||
lua_tostring(L, -1)));
|
||||
}
|
||||
}
|
||||
}
|
||||
lua_settop(L, 0); /* clear stack */
|
||||
fputs("\n", stdout);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
void TextGame::run()
|
||||
{
|
||||
dotty(viewer_.get_lua_state());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user