<commit_before>#include <stdio.h>
#include <stdlib.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include <readline/readline.h>
#include <readline/history.h>

lua_State *g_state;
char **g_words;
int g_quit;

void err(lua_State *L);

int cf_quit(lua_State *L);
int cf_ls(lua_State *L);

char** lisc_completion(const char* text, int start, int end);
char* var_generator(const char* text, int state);

char** make_words(lua_State *L);
char** make_words_table(lua_State *L, const char *text, int start, int end);

void xfree(void *obj);

int main(int argc, char **argv) {
  char *line;
  lua_State *L;
  int error_load;

  // initialize lua
  L = luaL_newstate();
  luaL_openlibs(L);
  lua_register(L, "quit", cf_quit);
  lua_register(L, "ls", cf_ls);

  // initial setting for gnu readline
  rl_readline_name = "lisc";
  rl_basic_word_break_characters = " \t\n\"+-*/^%><=|;,{(#";
  rl_attempted_completion_function = lisc_completion;

  g_state = L;
  g_words = NULL;
  g_quit = 0;

  while(g_quit == 0) {
    // read
    line = readline(">");

    // eval
    error_load = 0;
    if(luaL_loadstring(L, line)) {
      char *p;
      p = malloc(strlen(line)*sizeof(char)+sizeof("return "));
      sprintf(p, "return %s", line);
      if(luaL_loadstring(L, p)) {
        lua_pop(L, 1);
        error_load = 1;
      } else {
        lua_remove(L, lua_gettop(L)-1);
      }
      free(p);
    }

    if(error_load) {
      err(L);
    } else {
      if(lua_pcall(L, 0, LUA_MULTRET, 0)) {
        err(L);
      }
    }

    // print
    if(0 < lua_gettop(L)) {
      lua_getglobal(L, "print");
      lua_insert(L, 1);
      if(lua_pcall(L, lua_gettop(L)-1, 0, 0)) {
        err(L);
      }
    }

    add_history(line);

    xfree(line);
  }

  xfree(g_words);

  return 0;
}

void err(lua_State *L) {
  const char *p = lua_tostring(L, -1);
  printf("[pcall-err] %s\n", p);
  lua_pop(L, 1);
}

int cf_quit(lua_State *L) {
  int n = lua_gettop(L);

  if(n != 0) {
    lua_pushstring(L, "invalid arguments");
    return lua_error(L);
  }

  g_quit = 1;
<commit_msg>Execute a given file first<commit_after>#include <stdio.h>
#include <stdlib.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include <readline/readline.h>
#include <readline/history.h>

lua_State *g_state;
char **g_words;
int g_quit;

void err(lua_State *L);

int cf_quit(lua_State *L);
int cf_ls(lua_State *L);

char** lisc_completion(const char* text, int start, int end);
char* var_generator(const char* text, int state);

char** make_words(lua_State *L);
char** make_words_table(lua_State *L, const char *text, int start, int end);

void xfree(void *obj);

int main(int argc, char **argv) {
  char *line;
  lua_State *L;
  int error_load;

  // initialize lua
  L = luaL_newstate();
  luaL_openlibs(L);
  lua_register(L, "quit", cf_quit);
  lua_register(L, "ls", cf_ls);

  // initial setting for gnu readline
  rl_readline_name = "lisc";
  rl_basic_word_break_characters = " \t\n\"+-*/^%><=|;,{(#";
  rl_attempted_completion_function = lisc_completion;

  g_state = L;
  g_words = NULL;
  g_quit = 0;

  if(2 <= argc) {
    // exec argv[1]
    lua_getglobal(L, "dofile");
    lua_pushstring(L, argv[1]);

    if(lua_pcall(L, 1, 0, 0)) {
      err(L);
    }
  }

  while(g_quit == 0) {
    // read
    line = readline("lua>");

    // eval
    error_load = 0;
    if(luaL_loadstring(L, line)) {
      char *p;
      p = malloc(strlen(line)*sizeof(char)+sizeof("return "));
      sprintf(p, "return %s", line);
      if(luaL_loadstring(L, p)) {
        lua_pop(L, 1);
        error_load = 1;
      } else {
        lua_remove(L, lua_gettop(L)-1);
      }
      free(p);
    }

    if(error_load || lua_pcall(L, 0, LUA_MULTRET, 0)) {
      err(L);
    }

    // print
    if(0 < lua_gettop(L)) {
      lua_getglobal(L, "print");
      lua_insert(L, 1);
      if(lua_pcall(L, lua_gettop(L)-1, 0, 0)) {
        err(L);
      }
    }

    add_history(line);

    xfree(line);
  }

  xfree(g_words);

  lua_close(L);

  return 0;
}

void err(lua_State *L) {
  const char *p = lua_tostring(L, -1);
  printf("[pcall-err] %s\n", p);
  lua_pop(L, 1);
}

int cf_quit(lua_State *L) {
  int n = lua_gettop(L);

  if(n != 0) {
    lua_pushstring(L, "invalid arguments");
    return lua_error(L);
  }

  g_quit = 1;

{'fraction_non_alphanumeric': 0.1695783834148614, 'fraction_numerical': 0.009783368273934312, 'mean_word_length': 2.994418604651163, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1380923', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1774, 'n_words': 374}