Ejemplo n.º 1
0
/*
** Prompt the user, read a line, and push it into the Lua stack.
*/
static int pushline (lua_State *L, int firstline) {
  char buffer[LUA_MAXINPUT];
  char *b = buffer;
  size_t l;
  const char *prmt = get_prompt(L, firstline);
  int readstatus = lua_readline(L, b, prmt);
  if (readstatus == 0)
    return 0;  /* no input (prompt will be popped by caller) */
  lua_pop(L, 1);  /* remove prompt */
  l = strlen(b);
  if (l > 0 && b[l-1] == '\n')  /* line ends with newline? */
    b[--l] = '\0';  /* remove it */
  if (firstline && b[0] == '=')  /* for compatibility with 5.2, ... */
    lua_pushfstring(L, "return %s", b + 1);  /* change '=' to 'return' */
  else
    lua_pushlstring(L, b, l);
  lua_freeline(L, b);
  return 1;
}
Ejemplo n.º 2
0
static int pushline (lua_State *L, int firstline) {
  char buffer[LUA_MAXINPUT];
  char *b = buffer;
  size_t l;
  const char *prmt = get_prompt(L, firstline);
  int readstatus = lua_readline(L, b, prmt);
  lua_pop(L, 1);  /* remove result from 'get_prompt' */
  if (readstatus == 0)
    return 0;  /* no input */
  l = strlen(b);
  if (l > 0 && b[l-1] == '\n')  /* line ends with newline? */
    b[l-1] = '\0';  /* remove it */
  if (firstline && b[0] == '=')  /* first line starts with `=' ? */
    lua_pushfstring(L, "return %s", b+1);  /* change it to `return' */
  else
    lua_pushstring(L, b);
  lua_freeline(L, b);
  return 1;
}
Ejemplo n.º 3
0
static int pushline(lua_State *L, int firstline)
{
  char buffer[LUA_MAXINPUT];
  char *buf = buffer;
  size_t len;
  const char *prmt = get_prompt(L, firstline);
  if (lua_readline(L, buf, prmt)) {
    lua_pop(L, 1); /* prompt */
    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;
}
Ejemplo n.º 4
0
void interpreter_exec_interactively (const std::string &prompt)
{
    // STACK: []

    while (true) {

        // STACK: []

        std::string input;

        {
            char buffer[LUA_MAXINPUT];
            char *b = buffer;

            if (lua_readline(L, b, prompt.c_str()) == 0) {
                // end of stdin, exit cleanly
                break;
            }
            input = b;
        }

        if (input == "") continue;

        // trim newline if it has one
        if (input[input.length()-1] == '\n') input.erase(input.length()-1);

        if (input == "") continue;

        lua_pushstring(L, input.c_str());
        lua_saveline(L, -1);
        lua_pop(L, 1);

        ///////////
        // PARSE //
        ///////////

        // first try with 'return'

        int status = parse_with_return(input, "[interactive]");

        if (status == 0) {

            /////////////
            // EXECUTE //
            /////////////

            // STACK: [func]
            status = execute_code();
            if (status == 0) {

                // STACK: [retvals...]

                print_stack();

                // STACK: []

            } else {

                // STACK: [err]
                lua_pop(L, 1);

                // STACK: []
            }
        } else if (status == LUA_ERRSYNTAX) {
            // STACK: [err]
            const char *msg = lua_tostring(L, -1);
            std::cerr << BOLD YELLOW << "Syntax error: " << msg << RESET << std::endl;
            lua_pop(L, 1);
            // STACK: []
        } else if (status == LUA_ERRMEM) {
            std::cerr << "ERROR: Out of memory while parsing interactive input." << std::endl;
            exit(EXIT_FAILURE);
        }

        // STACK: []
    }

    // end the 'prompt' line
    std::cout << std::endl;
}