Advertisement
aiden50_70

Untitled

May 14th, 2019
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 22.35 KB | None | 0 0
  1. // this file was made by tollepuxis
  2. #ifdef __cplusplus
  3. extern "C" {
  4. #endif
  5. #include <lua.h>
  6. #include <lauxlib.h>
  7. #include <lualib.h>
  8. #ifdef __cplusplus
  9. }
  10. #endif
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13.  
  14.  
  15. #include <string.h>
  16.  
  17.  
  18. #include <assert.h>
  19.  
  20. /* pushes new closure table onto the stack, using closure table at
  21.  * given index as its parent */
  22. static void lc_newclosuretable(lua_State * L, int idx) {
  23.  
  24.   lua_newtable(L);
  25.   lua_pushvalue(L,idx);
  26.   lua_rawseti(L,-2,0);
  27.  
  28.  
  29. }
  30.  
  31. /* __add metamethod handler.
  32.  * warning: assumes indices in range LUA_REGISTRYINDEX < x < 0 are relative. */
  33. static void lc_add(lua_State * L, int idxa, int idxb) {
  34.   if (lua_isnumber(L,idxa) && lua_isnumber(L,idxb)) {
  35.     lua_pushnumber(L,lua_tonumber(L,idxa) + lua_tonumber(L,idxb));
  36.   }
  37.   else {
  38.     if (luaL_getmetafield(L,idxa,"__add")||luaL_getmetafield(L,idxb,"__add")) {
  39.       lua_pushvalue(L,idxa < 0 && idxa > LUA_REGISTRYINDEX ? idxa-1 : idxa);
  40.       lua_pushvalue(L,idxb < 0 && idxb > LUA_REGISTRYINDEX ? idxb-2 : idxb);
  41.       lua_call(L,2,1);
  42.     }
  43.     else {
  44.       luaL_error(L, "attempt to perform arithmetic");
  45.     }
  46.   }
  47. }
  48.  
  49.  
  50. /* gets upvalue with ID varid by consulting upvalue table at index
  51.  * tidx for the upvalue table at given nesting level. */
  52. static void lc_getupvalue(lua_State * L, int tidx, int level, int varid) {
  53.   if (level == 0) {
  54.     lua_rawgeti(L,tidx,varid);
  55.   }
  56.   else {
  57.     lua_pushvalue(L,tidx);
  58.     while (--level >= 0) {
  59.       lua_rawgeti(L,tidx,0); /* 0 links to parent table */
  60.       lua_remove(L,-2);
  61.       tidx = -1;
  62.     }
  63.     lua_rawgeti(L,-1,varid);
  64.     lua_remove(L,-2);
  65.   }
  66. }
  67.  
  68.  
  69. static void lc_setupvalue(lua_State * L, int tidx, int level, int varid) {
  70.   if (level == 0) {
  71.     lua_rawseti(L,tidx,varid);
  72.   }
  73.   else {
  74.     lua_pushvalue(L,tidx);
  75.     while(--level >= 0) {
  76.       lua_rawgeti(L,tidx,0); /* 0 links to parent table */
  77.       lua_remove(L,-2);
  78.       tidx = -1;
  79.     }
  80.     lua_insert(L,-2);
  81.     lua_rawseti(L,-2,varid);
  82.     lua_pop(L,1);
  83.   }
  84. }
  85.  
  86.  
  87. /* function(c) */
  88. static int lcf5 (lua_State * L) {
  89.   enum { lc_nformalargs = 1 };
  90.   lua_settop(L,1);
  91.  
  92.   /* if c == "\n" then */
  93.   enum { lc2 = 1 };
  94.   lua_pushliteral(L,"\n");
  95.   const int lc3 = lua_equal(L,1,-1);
  96.   lua_pop(L,1);
  97.   lua_pushboolean(L,lc3);
  98.   const int lc4 = lua_toboolean(L,-1);
  99.   lua_pop(L,1);
  100.   if (lc4) {
  101.    
  102.     /* line = line + 1 */
  103.     lc_getupvalue(L,lua_upvalueindex(1),0,1);
  104.     lua_pushnumber(L,1);
  105.     lc_add(L,-2,-1);
  106.     lua_remove(L,-2);
  107.     lua_remove(L,-2);
  108.     lc_setupvalue(L,lua_upvalueindex(1),0,1);
  109.     assert(lua_gettop(L) == 1);
  110.   }
  111.   lua_settop(L,lc2);
  112.   assert(lua_gettop(L) == 1);
  113.   return 0;
  114. }
  115.  
  116.  
  117. /* name: GetLines
  118.  * function(s) */
  119. static int lcf1_GetLines (lua_State * L) {
  120.   enum { lc_nformalargs = 1 };
  121.   lua_settop(L,1);
  122.  
  123.   /* local line = 1 */
  124.   lc_newclosuretable(L,lua_upvalueindex(1));
  125.   enum { lc1 = 2 };
  126.   assert((lua_gettop(L) == lc1));
  127.   lua_pushnumber(L,1);
  128.   lua_rawseti(L,lc1,1);
  129.   assert(lua_gettop(L) == 2);
  130.  
  131.   /* s:gsub(".",function(c)
  132.    *        if c == "\n" then line = line + 1 end
  133.    *    end) */
  134.   lua_pushvalue(L,1);
  135.   lua_pushliteral(L,"gsub");
  136.   lua_gettable(L,-2);
  137.   lua_insert(L,-2);
  138.   lua_pushliteral(L,".");
  139.   lua_pushvalue(L,lc1);
  140.   lua_pushcclosure(L,lcf5,1);
  141.   lua_call(L,3,0);
  142.   assert(lua_gettop(L) == 2);
  143.  
  144.   /* return line */
  145.   lc_getupvalue(L,lc1,0,1);
  146.   return 1;
  147.   assert(lua_gettop(L) == 2);
  148. }
  149.  
  150.  
  151. /* function(n) */
  152. static int lcf10 (lua_State * L) {
  153.   enum { lc_nformalargs = 1 };
  154.   lua_settop(L,1);
  155.  
  156.   /* if n ~= "\n" then */
  157.   enum { lc7 = 1 };
  158.   lua_pushliteral(L,"\n");
  159.   const int lc8 = lua_equal(L,1,-1);
  160.   lua_pop(L,1);
  161.   lua_pushboolean(L,lc8);
  162.   lua_pushboolean(L,!(lua_toboolean(L,-1)));
  163.   lua_remove(L,-2);
  164.   const int lc9 = lua_toboolean(L,-1);
  165.   lua_pop(L,1);
  166.   if (lc9) {
  167.    
  168.     /* k = k + 1 */
  169.     lc_getupvalue(L,lua_upvalueindex(1),0,2);
  170.     lua_pushnumber(L,1);
  171.     lc_add(L,-2,-1);
  172.     lua_remove(L,-2);
  173.     lua_remove(L,-2);
  174.     lc_setupvalue(L,lua_upvalueindex(1),0,2);
  175.     assert(lua_gettop(L) == 1);
  176.   }
  177.   lua_settop(L,lc7);
  178.   assert(lua_gettop(L) == 1);
  179.   return 0;
  180. }
  181.  
  182.  
  183. /* name: len
  184.  * function(c) */
  185. static int lcf1_len (lua_State * L) {
  186.   enum { lc_nformalargs = 1 };
  187.   lua_settop(L,1);
  188.  
  189.   /* local k = 1 */
  190.   lc_newclosuretable(L,lua_upvalueindex(1));
  191.   enum { lc6 = 2 };
  192.   assert((lua_gettop(L) == lc6));
  193.   lua_pushnumber(L,1);
  194.   lua_rawseti(L,lc6,2);
  195.   assert(lua_gettop(L) == 2);
  196.  
  197.   /* c:gsub('.',function(n)
  198.    *            if n ~= "\n" then
  199.    *                k = k + 1
  200.    *            end
  201.    *        end) */
  202.   lua_pushvalue(L,1);
  203.   lua_pushliteral(L,"gsub");
  204.   lua_gettable(L,-2);
  205.   lua_insert(L,-2);
  206.   lua_pushliteral(L,".");
  207.   lua_pushvalue(L,lc6);
  208.   lua_pushcclosure(L,lcf10,1);
  209.   lua_call(L,3,0);
  210.   assert(lua_gettop(L) == 2);
  211.  
  212.   /* return k */
  213.   lc_getupvalue(L,lc6,0,2);
  214.   return 1;
  215.   assert(lua_gettop(L) == 2);
  216. }
  217.  
  218.  
  219. /* name: GetChars
  220.  * function(s) */
  221. static int lcf1_GetChars (lua_State * L) {
  222.   enum { lc_nformalargs = 1 };
  223.   lua_settop(L,1);
  224.  
  225.   /* local len = function(c)
  226.    *        local k = 1
  227.    *        c:gsub('.',function(n)
  228.    *            if n ~= "\n" then
  229.    *                k = k + 1
  230.    *            end
  231.    *        end)
  232.    *        return k
  233.    *    end */
  234.   lua_pushcfunction(L,lcf1_len);
  235.   assert(lua_gettop(L) == 2);
  236.  
  237.   /* return len(s) */
  238.   const int lc11 = lua_gettop(L);
  239.   lua_pushvalue(L,2);
  240.   lua_pushvalue(L,1);
  241.   lua_call(L,1,LUA_MULTRET);
  242.   return (lua_gettop(L) - lc11);
  243.   assert(lua_gettop(L) == 2);
  244. }
  245.  
  246.  
  247. /* function(n) */
  248. static int lcf20 (lua_State * L) {
  249.   enum { lc_nformalargs = 1 };
  250.   lua_settop(L,1);
  251.  
  252.   /* if n=="warn" then */
  253.   enum { lc17 = 1 };
  254.   lua_pushliteral(L,"warn");
  255.   const int lc18 = lua_equal(L,1,-1);
  256.   lua_pop(L,1);
  257.   lua_pushboolean(L,lc18);
  258.   const int lc19 = lua_toboolean(L,-1);
  259.   lua_pop(L,1);
  260.   if (lc19) {
  261.    
  262.     /* warns=warns  +1 */
  263.     lc_getupvalue(L,lua_upvalueindex(1),0,4);
  264.     lua_pushnumber(L,1);
  265.     lc_add(L,-2,-1);
  266.     lua_remove(L,-2);
  267.     lua_remove(L,-2);
  268.     lc_setupvalue(L,lua_upvalueindex(1),0,4);
  269.     assert(lua_gettop(L) == 1);
  270.   }
  271.   lua_settop(L,lc17);
  272.   assert(lua_gettop(L) == 1);
  273.   return 0;
  274. }
  275.  
  276.  
  277. /* name: callend
  278.  * function() */
  279. static int lcf1_callend (lua_State * L) {
  280.   enum { lc_nformalargs = 0 };
  281.   lua_settop(L,0);
  282.  
  283.   /* -- find all 'Warn'  statements
  284.    * -- find all 'Error' statements
  285.    * src:gsub("%w+",function(n)
  286.    *            if n=="warn" then
  287.    *                warns=warns  +1
  288.    *            end
  289.    *        end) */
  290.   lc_getupvalue(L,lua_upvalueindex(1),1,3);
  291.   lua_pushliteral(L,"gsub");
  292.   lua_gettable(L,-2);
  293.   lua_insert(L,-2);
  294.   lua_pushliteral(L,"%w+");
  295.   lua_pushvalue(L,lua_upvalueindex(1));
  296.   lua_pushcclosure(L,lcf20,1);
  297.   lua_call(L,3,0);
  298.   assert(lua_gettop(L) == 0);
  299.  
  300.   /* print("Executed with") */
  301.   lua_getfield(L,LUA_ENVIRONINDEX,"print");
  302.   lua_pushliteral(L,"Executed with");
  303.   lua_call(L,1,0);
  304.   assert(lua_gettop(L) == 0);
  305.  
  306.   /* print(errors.." error(s)") */
  307.   lua_getfield(L,LUA_ENVIRONINDEX,"print");
  308.   lc_getupvalue(L,lua_upvalueindex(1),0,5);
  309.   lua_pushliteral(L," error(s)");
  310.   lua_concat(L,2);
  311.   lua_call(L,1,0);
  312.   assert(lua_gettop(L) == 0);
  313.  
  314.   /* print(warns.." warn(s)") */
  315.   lua_getfield(L,LUA_ENVIRONINDEX,"print");
  316.   lc_getupvalue(L,lua_upvalueindex(1),0,4);
  317.   lua_pushliteral(L," warn(s)");
  318.   lua_concat(L,2);
  319.   lua_call(L,1,0);
  320.   assert(lua_gettop(L) == 0);
  321.  
  322.   /* print("(This is only speculation)") */
  323.   lua_getfield(L,LUA_ENVIRONINDEX,"print");
  324.   lua_pushliteral(L,"(This is only speculation)");
  325.   lua_call(L,1,0);
  326.   assert(lua_gettop(L) == 0);
  327.   return 0;
  328. }
  329.  
  330.  
  331. /* function(...) */
  332. static int lcf23 (lua_State * L) {
  333.   enum { lc_nformalargs = 0 };
  334.   const int lc_nactualargs = lua_gettop(L);
  335.   #ifndef NDEBUG
  336.   const int lc_nextra = (lc_nactualargs - lc_nformalargs);
  337.   #endif
  338.  
  339.   /* return ... */
  340.   const int lc22 = lua_gettop(L);
  341.   {int i; for (i=lc_nformalargs+1; i<=lc_nactualargs; i++) { lua_pushvalue(L, i); }}
  342.   return (lua_gettop(L) - lc22);
  343.   assert(lua_gettop(L) - lc_nextra == 0);
  344. }
  345.  
  346.  
  347. /* function(...) */
  348. static int lcf25 (lua_State * L) {
  349.   enum { lc_nformalargs = 0 };
  350.   const int lc_nactualargs = lua_gettop(L);
  351.   #ifndef NDEBUG
  352.   const int lc_nextra = (lc_nactualargs - lc_nformalargs);
  353.   #endif
  354.  
  355.   /* return ... */
  356.   const int lc24 = lua_gettop(L);
  357.   {int i; for (i=lc_nformalargs+1; i<=lc_nactualargs; i++) { lua_pushvalue(L, i); }}
  358.   return (lua_gettop(L) - lc24);
  359.   assert(lua_gettop(L) - lc_nextra == 0);
  360. }
  361.  
  362.  
  363. /* function() */
  364. static int lcf31 (lua_State * L) {
  365.   enum { lc_nformalargs = 0 };
  366.   lua_settop(L,0);
  367.  
  368.   /* local func = loadstring(src) */
  369.   lua_getfield(L,LUA_ENVIRONINDEX,"loadstring");
  370.   lc_getupvalue(L,lua_upvalueindex(1),2,3);
  371.   lua_call(L,1,1);
  372.   assert(lua_gettop(L) == 1);
  373.  
  374.   /* if func == nil then */
  375.   enum { lc26 = 1 };
  376.   lua_pushnil(L);
  377.   const int lc27 = lua_equal(L,1,-1);
  378.   lua_pop(L,1);
  379.   lua_pushboolean(L,lc27);
  380.   const int lc28 = lua_toboolean(L,-1);
  381.   lua_pop(L,1);
  382.   if (lc28) {
  383.    
  384.     /* errors = errors + 1 */
  385.     lc_getupvalue(L,lua_upvalueindex(1),1,5);
  386.     lua_pushnumber(L,1);
  387.     lc_add(L,-2,-1);
  388.     lua_remove(L,-2);
  389.     lua_remove(L,-2);
  390.     lc_setupvalue(L,lua_upvalueindex(1),1,5);
  391.     assert(lua_gettop(L) == 1);
  392.   }
  393.   lua_settop(L,lc26);
  394.   assert(lua_gettop(L) == 1);
  395.  
  396.   /* assert(not (type(func) == nil), "Syntax Parse Failure") */
  397.   lua_getfield(L,LUA_ENVIRONINDEX,"assert");
  398.   lua_getfield(L,LUA_ENVIRONINDEX,"type");
  399.   lua_pushvalue(L,1);
  400.   lua_call(L,1,1);
  401.   lua_pushnil(L);
  402.   const int lc29 = lua_equal(L,-2,-1);
  403.   lua_pop(L,2);
  404.   lua_pushboolean(L,lc29);
  405.   lua_pushboolean(L,!(lua_toboolean(L,-1)));
  406.   lua_remove(L,-2);
  407.   lua_pushliteral(L,"Syntax Parse Failure");
  408.   lua_call(L,2,0);
  409.   assert(lua_gettop(L) == 1);
  410.  
  411.   /* setfenv(func, setmetatable(env, {__index = getfenv()})) */
  412.   lua_getfield(L,LUA_ENVIRONINDEX,"setfenv");
  413.   const int lc30 = lua_gettop(L);
  414.   lua_pushvalue(L,1);
  415.   lua_getfield(L,LUA_ENVIRONINDEX,"setmetatable");
  416.   lc_getupvalue(L,lua_upvalueindex(1),0,6);
  417.   lua_createtable(L,0,1);
  418.   lua_pushliteral(L,"__index");
  419.   lua_getfield(L,LUA_ENVIRONINDEX,"getfenv");
  420.   lua_call(L,0,1);
  421.   lua_rawset(L,-3);
  422.   lua_call(L,2,LUA_MULTRET);
  423.   lua_call(L,(lua_gettop(L) - lc30),0);
  424.   assert(lua_gettop(L) == 1);
  425.   return 0;
  426. }
  427.  
  428.  
  429. /* name: build.BuildLua
  430.  * function(src) */
  431. static int lcf1_build_BuildLua (lua_State * L) {
  432.   enum { lc_nformalargs = 1 };
  433.   lua_settop(L,1);
  434.   lc_newclosuretable(L,lua_upvalueindex(1));
  435.   enum { lc12 = 2 };
  436.   assert((lua_gettop(L) == lc12));
  437.   lua_pushvalue(L,1);
  438.   lua_rawseti(L,-2,3);
  439.  
  440.   /* if GetChars(src) < 2 then */
  441.   enum { lc13 = 2 };
  442.   lua_getfield(L,LUA_ENVIRONINDEX,"GetChars");
  443.   lc_getupvalue(L,lc12,0,3);
  444.   lua_call(L,1,1);
  445.   lua_pushnumber(L,2);
  446.   const int lc14 = lua_lessthan(L,-2,-1);
  447.   lua_pop(L,2);
  448.   lua_pushboolean(L,lc14);
  449.   const int lc15 = lua_toboolean(L,-1);
  450.   lua_pop(L,1);
  451.   if (lc15) {
  452.    
  453.     /* print("=== CALL ERROR ===") */
  454.     lua_getfield(L,LUA_ENVIRONINDEX,"print");
  455.     lua_pushliteral(L,"=== CALL ERROR ===");
  456.     lua_call(L,1,0);
  457.     assert(lua_gettop(L) == 2);
  458.    
  459.     /* print("Script Source must be above 2 characters") */
  460.     lua_getfield(L,LUA_ENVIRONINDEX,"print");
  461.     lua_pushliteral(L,"Script Source must be above 2 characters");
  462.     lua_call(L,1,0);
  463.     assert(lua_gettop(L) == 2);
  464.    
  465.     /* print("=== CALL ERROR ===") */
  466.     lua_getfield(L,LUA_ENVIRONINDEX,"print");
  467.     lua_pushliteral(L,"=== CALL ERROR ===");
  468.     lua_call(L,1,0);
  469.     assert(lua_gettop(L) == 2);
  470.    
  471.     /* return false */
  472.     lua_pushboolean(L,0);
  473.     return 1;
  474.     assert(lua_gettop(L) == 2);
  475.   }
  476.   lua_settop(L,lc13);
  477.   assert(lua_gettop(L) == 2);
  478.  
  479.   /* print("Building Lua...") */
  480.   lua_getfield(L,LUA_ENVIRONINDEX,"print");
  481.   lua_pushliteral(L,"Building Lua...");
  482.   lua_call(L,1,0);
  483.   assert(lua_gettop(L) == 2);
  484.  
  485.   /* local lines = GetLines(src) */
  486.   lua_getfield(L,LUA_ENVIRONINDEX,"GetLines");
  487.   lc_getupvalue(L,lc12,0,3);
  488.   lua_call(L,1,1);
  489.   assert(lua_gettop(L) == 3);
  490.  
  491.   /* local chars = GetChars(src) */
  492.   lua_getfield(L,LUA_ENVIRONINDEX,"GetChars");
  493.   lc_getupvalue(L,lc12,0,3);
  494.   lua_call(L,1,1);
  495.   assert(lua_gettop(L) == 4);
  496.  
  497.   /* print("==========") */
  498.   lua_getfield(L,LUA_ENVIRONINDEX,"print");
  499.   lua_pushliteral(L,"==========");
  500.   lua_call(L,1,0);
  501.   assert(lua_gettop(L) == 4);
  502.  
  503.   /* print("Lines          >> ".. lines) */
  504.   lua_getfield(L,LUA_ENVIRONINDEX,"print");
  505.   lua_pushliteral(L,"Lines          >> ");
  506.   lua_pushvalue(L,3);
  507.   lua_concat(L,2);
  508.   lua_call(L,1,0);
  509.   assert(lua_gettop(L) == 4);
  510.  
  511.   /* print("Characters     >> ".. chars) */
  512.   lua_getfield(L,LUA_ENVIRONINDEX,"print");
  513.   lua_pushliteral(L,"Characters     >> ");
  514.   lua_pushvalue(L,4);
  515.   lua_concat(L,2);
  516.   lua_call(L,1,0);
  517.   assert(lua_gettop(L) == 4);
  518.  
  519.   /* local errors,warns = 0,0 */
  520.   lc_newclosuretable(L,lc12);
  521.   enum { lc16 = 5 };
  522.   assert((lua_gettop(L) == lc16));
  523.   lua_pushnumber(L,0);
  524.   lua_pushnumber(L,0);
  525.   lua_rawseti(L,lc16,4);
  526.   lua_rawseti(L,lc16,5);
  527.   assert(lua_gettop(L) == 5);
  528.  
  529.   /* local function callend()
  530.    *        -- find all 'Warn'  statements
  531.    *        -- find all 'Error' statements
  532.    *        src:gsub("%w+",function(n)
  533.    *            if n=="warn" then
  534.    *                warns=warns  +1
  535.    *            end
  536.    *        end)
  537.    *        print("Executed with")
  538.    *        print(errors.." error(s)")
  539.    *        print(warns.." warn(s)")
  540.    *        print("(This is only speculation)")
  541.    *    end */
  542.   lua_pushvalue(L,lc16);
  543.   lua_pushcclosure(L,lcf1_callend,1);
  544.   assert(lua_gettop(L) == 6);
  545.  
  546.   /* --local result,err = pcall(function()
  547.    * local env = {
  548.    *            print = function(...)
  549.    *                return ...
  550.    *            end,
  551.    *            warn = function(...)
  552.    *                return ...
  553.    *            end
  554.    *        } */
  555.   lc_newclosuretable(L,lc16);
  556.   enum { lc21 = 7 };
  557.   assert((lua_gettop(L) == lc21));
  558.   lua_createtable(L,0,2);
  559.   lua_pushliteral(L,"print");
  560.   lua_pushcfunction(L,lcf23);
  561.   lua_rawset(L,-3);
  562.   lua_pushliteral(L,"warn");
  563.   lua_pushcfunction(L,lcf25);
  564.   lua_rawset(L,-3);
  565.   lua_rawseti(L,lc21,6);
  566.   assert(lua_gettop(L) == 7);
  567.  
  568.   /* local res,err = pcall(function()
  569.    *            local func = loadstring(src)
  570.    *            if func == nil then errors = errors + 1 end
  571.    *            assert(not (type(func) == nil), "Syntax Parse Failure")
  572.    *            setfenv(func, setmetatable(env, {__index = getfenv()}))
  573.    *        end) */
  574.   lua_getfield(L,LUA_ENVIRONINDEX,"pcall");
  575.   lua_pushvalue(L,lc21);
  576.   lua_pushcclosure(L,lcf31,1);
  577.   lua_call(L,1,2);
  578.   assert(lua_gettop(L) == 9);
  579.  
  580.   /* --end)
  581.    * if result == false then */
  582.   enum { lc32 = 9 };
  583.   lua_getfield(L,LUA_ENVIRONINDEX,"result");
  584.   lua_pushboolean(L,0);
  585.   const int lc33 = lua_equal(L,-2,-1);
  586.   lua_pop(L,2);
  587.   lua_pushboolean(L,lc33);
  588.   const int lc34 = lua_toboolean(L,-1);
  589.   lua_pop(L,1);
  590.   if (lc34) {
  591.    
  592.     /* callend() */
  593.     lua_pushvalue(L,6);
  594.     lua_call(L,0,0);
  595.     assert(lua_gettop(L) == 9);
  596.    
  597.     /* print(err) */
  598.     lua_getfield(L,LUA_ENVIRONINDEX,"print");
  599.     lua_pushvalue(L,9);
  600.     lua_call(L,1,0);
  601.     assert(lua_gettop(L) == 9);
  602.    
  603.     /* print("==========") */
  604.     lua_getfield(L,LUA_ENVIRONINDEX,"print");
  605.     lua_pushliteral(L,"==========");
  606.     lua_call(L,1,0);
  607.     assert(lua_gettop(L) == 9);
  608.    
  609.     /* return false,err */
  610.     lua_pushboolean(L,0);
  611.     lua_pushvalue(L,9);
  612.     return 2;
  613.     assert(lua_gettop(L) == 9);
  614.   }
  615.   else {
  616.    
  617.     /* else
  618.      * callend() */
  619.     lua_pushvalue(L,6);
  620.     lua_call(L,0,0);
  621.     assert(lua_gettop(L) == 9);
  622.    
  623.     /* print("==========") */
  624.     lua_getfield(L,LUA_ENVIRONINDEX,"print");
  625.     lua_pushliteral(L,"==========");
  626.     lua_call(L,1,0);
  627.     assert(lua_gettop(L) == 9);
  628.    
  629.     /* return true,src */
  630.     lua_pushboolean(L,1);
  631.     lc_getupvalue(L,lc21,2,3);
  632.     return 2;
  633.     assert(lua_gettop(L) == 9);
  634.   }
  635.   lua_settop(L,lc32);
  636.   assert(lua_gettop(L) == 9);
  637.   return 0;
  638. }
  639.  
  640.  
  641. /* name: (main)
  642.  * function(...) */
  643. static int lcf_main (lua_State * L) {
  644.   enum { lc_nformalargs = 0 };
  645.   const int lc_nactualargs = lua_gettop(L);
  646.   const int lc_nextra = (lc_nactualargs - lc_nformalargs);
  647.  
  648.   /* local build = {} */
  649.   lua_newtable(L);
  650.   assert(lua_gettop(L) - lc_nextra == 1);
  651.  
  652.   /* local binary = require("binary") */
  653.   lua_getfield(L,LUA_ENVIRONINDEX,"require");
  654.   lua_pushliteral(L,"binary");
  655.   lua_call(L,1,1);
  656.   assert(lua_gettop(L) - lc_nextra == 2);
  657.  
  658.   /* function GetLines(s)
  659.    *    local line = 1
  660.    *    s:gsub(".",function(c)
  661.    *        if c == "\n" then line = line + 1 end
  662.    *    end)
  663.    *    return line
  664.    * end */
  665.   lua_pushcfunction(L,lcf1_GetLines);
  666.   lua_setfield(L,LUA_ENVIRONINDEX,"GetLines");
  667.   assert(lua_gettop(L) - lc_nextra == 2);
  668.  
  669.   /* function GetChars(s)
  670.    *    local len = function(c)
  671.    *        local k = 1
  672.    *        c:gsub('.',function(n)
  673.    *            if n ~= "\n" then
  674.    *                k = k + 1
  675.    *            end
  676.    *        end)
  677.    *        return k
  678.    *    end
  679.    *    return len(s)
  680.    * end */
  681.   lua_pushcfunction(L,lcf1_GetChars);
  682.   lua_setfield(L,LUA_ENVIRONINDEX,"GetChars");
  683.   assert(lua_gettop(L) - lc_nextra == 2);
  684.  
  685.   /* function build.BuildLua(src)
  686.    *    if GetChars(src) < 2 then
  687.    *        print("=== CALL ERROR ===")
  688.    *        print("Script Source must be above 2 characters")
  689.    *        print("=== CALL ERROR ===")
  690.    *        return false
  691.    *    end
  692.    *    print("Building Lua...")
  693.    *    local lines = GetLines(src)
  694.    *    local chars = GetChars(src)
  695.    *    print("==========")
  696.    *    print("Lines          >> ".. lines)
  697.    *    print("Characters     >> ".. chars)
  698.    *    local errors,warns = 0,0
  699.    *    local function callend()
  700.    *        -- find all 'Warn'  statements
  701.    *        -- find all 'Error' statements
  702.    *        src:gsub("%w+",function(n)
  703.    *            if n=="warn" then
  704.    *                warns=warns  +1
  705.    *            end
  706.    *        end)
  707.    *        print("Executed with")
  708.    *        print(errors.." error(s)")
  709.    *        print(warns.." warn(s)")
  710.    *        print("(This is only speculation)")
  711.    *    end
  712.    *    --local result,err = pcall(function()
  713.    *        local env = {
  714.    *            print = function(...)
  715.    *                return ...
  716.    *            end,
  717.    *            warn = function(...)
  718.    *                return ...
  719.    *            end
  720.    *        }
  721.    *        local res,err = pcall(function()
  722.    *            local func = loadstring(src)
  723.    *            if func == nil then errors = errors + 1 end
  724.    *            assert(not (type(func) == nil), "Syntax Parse Failure")
  725.    *            setfenv(func, setmetatable(env, {__index = getfenv()}))
  726.    *        end)
  727.    *    --end)
  728.    *    if result == false then
  729.    *        callend()
  730.    *        print(err)
  731.    *        print("==========")
  732.    *        return false,err
  733.    *    else
  734.    *        callend()
  735.    *        print("==========")
  736.    *        return true,src
  737.    *    end
  738.    * end */
  739.   lua_pushcfunction(L,lcf1_build_BuildLua);
  740.   lua_pushliteral(L,"BuildLua");
  741.   lua_insert(L,-2);
  742.   lua_settable(L,(1 + lc_nextra));
  743.   assert(lua_gettop(L) - lc_nextra == 2);
  744.  
  745.   /* return build */
  746.   lua_pushvalue(L,(1 + lc_nextra));
  747.   return 1;
  748.   assert(lua_gettop(L) - lc_nextra == 2);
  749. }
  750.  
  751.  
  752. /* from lua.c */
  753. static int traceback (lua_State *L) {
  754.   if (!lua_isstring(L, 1))  /* 'message' not a string? */
  755.     return 1;  /* keep it intact */
  756.   lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  757.   if (!lua_istable(L, -1)) {
  758.     lua_pop(L, 1);
  759.     return 1;
  760.   }
  761.   lua_getfield(L, -1, "traceback");
  762.   if (!lua_isfunction(L, -1)) {
  763.     lua_pop(L, 2);
  764.     return 1;
  765.   }
  766.   lua_pushvalue(L, 1);  /* pass error message */
  767.   lua_pushinteger(L, 2);  /* skip this function and traceback */
  768.   lua_call(L, 2, 1);  /* call debug.traceback */
  769.   return 1;
  770. }
  771.  
  772.  
  773. static void lc_l_message (const char *pname, const char *msg) {
  774.   if (pname) fprintf(stderr, "%s: ", pname);
  775.   fprintf(stderr, "%s\n", msg);
  776.   fflush(stderr);
  777. }
  778.  
  779. static int lc_report (lua_State *L, int status) {
  780.   if (status && !lua_isnil(L, -1)) {
  781.     const char *msg = lua_tostring(L, -1);
  782.     if (msg == NULL) msg = "(error object is not a string)";
  783.     /*FIX-IMROVE:progname*/
  784.     lc_l_message("lua", msg);
  785.     lua_pop(L, 1);
  786.   }
  787.   return status;
  788. }
  789.  
  790. static int lc_docall (lua_State *L, int narg, int clear) {
  791.   int status;
  792.   int base = lua_gettop(L) - narg;  /* function index */
  793.   lua_pushcfunction(L, traceback);  /* push traceback function */
  794.   lua_insert(L, base);  /* put it under chunk and args */
  795.   /*FIX? signal(SIGINT, laction); */
  796.   status = lua_pcall(L, narg, (clear ? 0 : LUA_MULTRET), base);
  797.   /*FIX? signal(SIGINT, SIG_DFL); */
  798.   lua_remove(L, base);  /* remove traceback function */
  799.   /* force a complete garbage collection in case of errors */
  800.   if (status != 0) lua_gc(L, LUA_GCCOLLECT, 0);
  801.   return status;
  802. }
  803.  
  804. static int lc_dofile (lua_State *L, const char *name) {
  805.   int status = luaL_loadfile(L, name) || lc_docall(L, 0, 1);
  806.   return lc_report(L, status);
  807. }
  808.  
  809. static int lc_dostring (lua_State *L, const char *s, const char *name) {
  810.   int status = luaL_loadbuffer(L, s, strlen(s), name) || lc_docall(L, 0, 1);
  811.   return lc_report(L, status);
  812. }
  813.  
  814. static int lc_handle_luainit (lua_State *L) {
  815.   const char *init = getenv(LUA_INIT);
  816.   if (init == NULL) return 0;  /* status OK */
  817.   else if (init[0] == '@')
  818.     return lc_dofile(L, init+1);
  819.   else
  820.     return lc_dostring(L, init, "=" LUA_INIT);
  821. }
  822.  
  823.  
  824. typedef struct {
  825.   int c;
  826.   const char ** v;
  827. } lc_args_t;
  828.  
  829.  
  830. /* create global arg table */
  831. static void lc_createarg(lua_State * L, const lc_args_t * const args) {
  832.   int i;
  833.   lua_newtable(L);
  834.   for (i=0; i < args->c; i++) {
  835.     lua_pushstring(L, args->v[i]);
  836.     lua_rawseti(L, -2, i);
  837.   }
  838.   lua_setglobal(L, "arg");
  839. }
  840.  
  841.  
  842. static int lc_pmain(lua_State * L) {
  843.   luaL_openlibs(L);
  844.  
  845.   const lc_args_t * const args = (lc_args_t*)lua_touserdata(L, 1);
  846.   lc_createarg(L, args);
  847.  
  848.   lua_pushcfunction(L, traceback);
  849.  
  850.   const int status1 = lc_handle_luainit(L);
  851.   if (status1 != 0) return 0;
  852.  
  853.   /* note: IMPROVE: closure not always needed here */
  854.   lua_newtable(L); /* closure table */
  855.   lua_pushcclosure(L, lcf_main, 1);
  856.   int i;
  857.   for (i=1; i < args->c; i++) {
  858.     lua_pushstring(L, args->v[i]);
  859.   }
  860.   int status2 = lua_pcall(L, args->c-1, 0, -2);
  861.   if (status2 != 0) {
  862.     const char * msg = lua_tostring(L,-1);
  863.     if (msg == NULL) msg = "(error object is not a string)";
  864.     fputs(msg, stderr);
  865.   }
  866.   return 0;
  867. }
  868.  
  869.  
  870. int main(int argc, const char ** argv) {
  871.   lc_args_t args = {argc, argv};
  872.   lua_State * L = luaL_newstate();
  873.   if (! L) { fputs("Failed creating Lua state.", stderr); exit(1); }
  874.  
  875.   int status = lua_cpcall(L, lc_pmain, &args);
  876.   if (status != 0) {
  877.     fputs(lua_tostring(L,-1), stderr);
  878.   }
  879.  
  880.   lua_close(L);
  881.   return 0;
  882. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement