Advertisement
Guest User

example.c

a guest
Dec 8th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.31 KB | None | 0 0
  1. // Copyright https://github.com/tilkinsc/ MIT
  2.  
  3. // handles line by line interpretation (REPL)
  4. static int lua_main_postexist(lua_State* L) {
  5.     input = malloc(PRIMARY_REPL_BUFFER_SIZE);
  6.     retfmt = malloc(SECONDARY_REPL_BUFFER_SIZE);
  7.     check_error_OOM(input == NULL || retfmt == NULL, __LINE__);
  8.    
  9.     int base = 0;
  10.     int status = 0;
  11.     unsigned int ch = 0;
  12.     size_t i = 0;
  13.     while(should_close != 1) {
  14. retry:
  15.         // 1. reset
  16.         status = 0;
  17.         i = 0;
  18.        
  19.         // 2. read
  20.         lfprint("%c", '>');
  21.         while((ch = fgetc(stdin)) != '\n') {
  22.             if(ch == -1) // sigint
  23.                 // TODO: might crash utf16 multichar string
  24.                 return luaL_error(L, "SIGINT");
  25.             if(i == PRIMARY_REPL_BUFFER_SIZE - 1) { // if max input reached
  26.                 //lferror(_(REPL_LINE_ERROR));
  27.                 lferror("Repl line error");
  28.                 fflush(stdin);
  29.                 goto retry;
  30.             }
  31.             input[i++] = ch;
  32.         }
  33.         input[i++] = '\0'; // ensure c-string format
  34.        
  35.         // 2.1 exit if requested
  36.         if(memcmp(input, "return", 6 + 1) == 0) { // return\0 returns if without arguments
  37.             should_close = 1;
  38.             return 0;
  39.         }
  40.        
  41.         // 3. return %s; test
  42.         snprintf(retfmt, SECONDARY_REPL_BUFFER_SIZE, "return %s;", input);
  43.         status = luaL_loadbuffer(L, retfmt, strlen(retfmt), "REPL return");
  44.         if(status != 0) {
  45.             lua_pop(L, 1); // err msg
  46.         } else {
  47.             // attempt first test, return seems to work
  48.             int top = lua_gettop(L);
  49.             status = lua_pcall(L, 0, LUA_MULTRET, 0);
  50.             if(status == 0) { // on success
  51.                 top = lua_gettop(L) - top + 1; // + 1 = ignore pushed function
  52.                 if(top > 0) { // more than 0 arguments returned
  53.                     lua_getglobal(L, "print");
  54.                     if(lua_isnil(L, -1) != 1) { // check even if no_libraries == 1
  55.                         lua_insert(L, lua_gettop(L)-top);
  56.                         lua_call(L, top, 0);
  57.                     } else
  58.                         lua_pop(L, 1 + top); // nil, anything returned
  59.                 }
  60.                 continue;
  61.             } else lua_pop(L, 1); // err msg, also ignore it - no relevance
  62.         }
  63.        
  64.         // 4. load and execute originally inserted code with error handler
  65.         lua_pushcclosure(L, lua_print_error, 0);
  66.         base = lua_gettop(L);
  67.         if((status = luaL_loadbuffer(L, input, strlen(input), "REPL")) != 0) {
  68.             print_error(SYNTAX_ERROR, 1);
  69.             lua_pop(L, 2); // err msg, err handler
  70.         } else {
  71.             if((status = lua_pcall(L, 0, LUA_MULTRET, base)) != 0)
  72.                 lua_pop(L, 2); // err msg, err handler, also ignore it - no relevance
  73.         }
  74.     }
  75.    
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement