Advertisement
bootylicker

Untitled

Jul 7th, 2022
846
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
RBScript 199.94 KB | None | 0 0
  1. -- Gui to Lua
  2. -- Version: 3.2
  3.  
  4. -- Instances:
  5.  
  6. local SS = Instance.new("ScreenGui")
  7. local Frame = Instance.new("Frame")
  8. local Frame_2 = Instance.new("Frame")
  9. local TextLabel = Instance.new("TextLabel")
  10. local source = Instance.new("TextBox")
  11. local execute = Instance.new("TextButton")
  12. local clear = Instance.new("TextButton")
  13.  
  14. --Properties:
  15.  
  16. SS.Name = "SS"
  17. SS.Parent = game.Workspace
  18. SS.ZIndexBehavior = Enum.ZIndexBehavior.Sibling
  19.  
  20. Frame.Parent = SS
  21. Frame.BackgroundColor3 = Color3.fromRGB(0, 170, 0)
  22. Frame.BorderSizePixel = 0
  23. Frame.Position = UDim2.new(0.608663201, 0, 0.493857503, 0)
  24. Frame.Size = UDim2.new(0, 419, 0, 282)
  25.  
  26. Frame_2.Parent = Frame
  27. Frame_2.BackgroundColor3 = Color3.fromRGB(170, 255, 127)
  28. Frame_2.BorderSizePixel = 0
  29. Frame_2.Position = UDim2.new(-0.00231534243, 0, -0.00259640813, 0)
  30. Frame_2.Size = UDim2.new(0, 419, 0, 41)
  31.  
  32. TextLabel.Parent = Frame_2
  33. TextLabel.BackgroundColor3 = Color3.fromRGB(255, 255, 255)
  34. TextLabel.BackgroundTransparency = 1.000
  35. TextLabel.BorderSizePixel = 0
  36. TextLabel.Size = UDim2.new(0, 418, 0, 41)
  37. TextLabel.Font = Enum.Font.Gotham
  38. TextLabel.Text = "Tutorial SS"
  39. TextLabel.TextColor3 = Color3.fromRGB(0, 0, 0)
  40. TextLabel.TextScaled = true
  41. TextLabel.TextSize = 14.000
  42. TextLabel.TextWrapped = true
  43.  
  44. source.Name = "source"
  45. source.Parent = Frame
  46. source.BackgroundColor3 = Color3.fromRGB(170, 255, 127)
  47. source.BorderSizePixel = 0
  48. source.Position = UDim2.new(0.0214797128, 0, 0.184397161, 0)
  49. source.Size = UDim2.new(0, 398, 0, 185)
  50. source.Font = Enum.Font.SourceSans
  51. source.PlaceholderText = "Put script here!"
  52. source.Text = ""
  53. source.TextColor3 = Color3.fromRGB(0, 0, 0)
  54. source.TextSize = 14.000
  55. source.TextXAlignment = Enum.TextXAlignment.Left
  56. source.TextYAlignment = Enum.TextYAlignment.Top
  57.  
  58. execute.Name = "execute"
  59. execute.Parent = Frame
  60. execute.BackgroundColor3 = Color3.fromRGB(170, 255, 127)
  61. execute.BorderSizePixel = 0
  62. execute.Position = UDim2.new(0.0214797128, 0, 0.872340441, 0)
  63. execute.Size = UDim2.new(0, 185, 0, 28)
  64. execute.Font = Enum.Font.Gotham
  65. execute.Text = "Execute"
  66. execute.TextColor3 = Color3.fromRGB(0, 0, 0)
  67. execute.TextScaled = true
  68. execute.TextSize = 14.000
  69. execute.TextWrapped = true
  70.  
  71. clear.Name = "clear"
  72. clear.Parent = Frame
  73. clear.BackgroundColor3 = Color3.fromRGB(170, 255, 127)
  74. clear.BorderSizePixel = 0
  75. clear.Position = UDim2.new(0.529832959, 0, 0.872340441, 0)
  76. clear.Size = UDim2.new(0, 185, 0, 28)
  77. clear.Font = Enum.Font.Gotham
  78. clear.Text = "Clear"
  79. clear.TextColor3 = Color3.fromRGB(0, 0, 0)
  80. clear.TextScaled = true
  81. clear.TextSize = 14.000
  82. clear.TextWrapped = true
  83.  
  84. -- Module Scripts:
  85.  
  86. local fake_module_scripts = {}
  87.  
  88. do -- nil.Loadstring
  89.     local script = Instance.new('ModuleScript', nil)
  90.     script.Name = "Loadstring"
  91.     local function module_script()
  92.         --[[
  93.             Credit to einsteinK.
  94.             Credit to Stravant for LBI.
  95.            
  96.             Credit to the creators of all the other modules used in this.
  97.            
  98.             Sceleratis was here and decided modify some things.
  99.            
  100.             einsteinK was here again to fix a bug in LBI for if-statements
  101.         --]]
  102.        
  103.         local waitDeps = {
  104.             'Rerubi';
  105.             'LuaK';
  106.             'LuaP';
  107.             'LuaU';
  108.             'LuaX';
  109.             'LuaY';
  110.             'LuaZ';
  111.         }
  112.        
  113.         for i,v in pairs(waitDeps) do script:WaitForChild(v) end
  114.        
  115.         local luaX = require(script.LuaX)
  116.         local luaY = require(script.LuaY)
  117.         local luaZ = require(script.LuaZ)
  118.         local luaU = require(script.LuaU)
  119.         local rerubi = require(script.Rerubi)
  120.        
  121.         luaX:init()
  122.         local LuaState = {}
  123.        
  124.         getfenv().script = nil
  125.        
  126.         return function(str,env)
  127.             local f,writer,buff,name
  128.             local env = env or getfenv(2)
  129.             local name = (env.script and env.script:GetFullName())
  130.             local ran,error = pcall(function()
  131.                 local zio = luaZ:init(luaZ:make_getS(str), nil)
  132.                 if not zio then return error() end
  133.                 local func = luaY:parser(LuaState, zio, nil, name or "nil")
  134.                 writer, buff = luaU:make_setS()
  135.                 luaU:dump(LuaState, func, writer, buff)
  136.                 f = rerubi(buff.data, env)
  137.             end)
  138.            
  139.             if ran then
  140.                 return f,buff.data
  141.             else
  142.                 return nil,error
  143.             end
  144.         end
  145.     end
  146.     fake_module_scripts[script] = module_script
  147. end
  148. do -- nil.LuaZ
  149.     local script = Instance.new('ModuleScript', nil)
  150.     script.Name = "LuaZ"
  151.     local function module_script()
  152.         --[[--------------------------------------------------------------------
  153.        
  154.           lzio.lua
  155.           Lua buffered streams in Lua
  156.           This file is part of Yueliang.
  157.        
  158.           Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net>
  159.           The COPYRIGHT file describes the conditions
  160.           under which this software may be distributed.
  161.        
  162.           See the ChangeLog for more information.
  163.        
  164.         ----------------------------------------------------------------------]]
  165.        
  166.         --[[--------------------------------------------------------------------
  167.         -- Notes:
  168.         -- * EOZ is implemented as a string, "EOZ"
  169.         -- * Format of z structure (ZIO)
  170.         --     z.n       -- bytes still unread
  171.         --     z.p       -- last read position position in buffer
  172.         --     z.reader  -- chunk reader function
  173.         --     z.data    -- additional data
  174.         -- * Current position, p, is now last read index instead of a pointer
  175.         --
  176.         -- Not implemented:
  177.         -- * luaZ_lookahead: used only in lapi.c:lua_load to detect binary chunk
  178.         -- * luaZ_read: used only in lundump.c:ezread to read +1 bytes
  179.         -- * luaZ_openspace: dropped; let Lua handle buffers as strings (used in
  180.         --   lundump.c:LoadString & lvm.c:luaV_concat)
  181.         -- * luaZ buffer macros: dropped; buffers are handled as strings
  182.         -- * lauxlib.c:getF reader implementation has an extraline flag to
  183.         --   skip over a shbang (#!) line, this is not implemented here
  184.         --
  185.         -- Added:
  186.         -- (both of the following are vaguely adapted from lauxlib.c)
  187.         -- * luaZ:make_getS: create Reader from a string
  188.         -- * luaZ:make_getF: create Reader that reads from a file
  189.         --
  190.         -- Changed in 5.1.x:
  191.         -- * Chunkreader renamed to Reader (ditto with Chunkwriter)
  192.         -- * Zio struct: no more name string, added Lua state for reader
  193.         --   (however, Yueliang readers do not require a Lua state)
  194.         ----------------------------------------------------------------------]]
  195.        
  196.         local luaZ = {}
  197.        
  198.         ------------------------------------------------------------------------
  199.         -- * reader() should return a string, or nil if nothing else to parse.
  200.         --   Additional data can be set only during stream initialization
  201.         -- * Readers are handled in lauxlib.c, see luaL_load(file|buffer|string)
  202.         -- * LUAL_BUFFERSIZE=BUFSIZ=512 in make_getF() (located in luaconf.h)
  203.         -- * Original Reader typedef:
  204.         --   const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
  205.         -- * This Lua chunk reader implementation:
  206.         --   returns string or nil, no arguments to function
  207.         ------------------------------------------------------------------------
  208.        
  209.         ------------------------------------------------------------------------
  210.         -- create a chunk reader from a source string
  211.         ------------------------------------------------------------------------
  212.         function luaZ:make_getS(buff)
  213.           local b = buff
  214.           return function() -- chunk reader anonymous function here
  215.             if not b then return nil end
  216.             local data = b
  217.             b = nil
  218.             return data
  219.           end
  220.         end
  221.        
  222.         ------------------------------------------------------------------------
  223.         -- create a chunk reader from a source file
  224.         ------------------------------------------------------------------------
  225.         --[[
  226.         function luaZ:make_getF(filename)
  227.           local LUAL_BUFFERSIZE = 512
  228.           local h = io.open(filename, "r")
  229.           if not h then return nil end
  230.           return function() -- chunk reader anonymous function here
  231.             if not h or io.type(h) == "closed file" then return nil end
  232.             local buff = h:read(LUAL_BUFFERSIZE)
  233.             if not buff then h:close(); h = nil end
  234.             return buff
  235.           end
  236.         end
  237.         --]]
  238.         ------------------------------------------------------------------------
  239.         -- creates a zio input stream
  240.         -- returns the ZIO structure, z
  241.         ------------------------------------------------------------------------
  242.         function luaZ:init(reader, data, name)
  243.           if not reader then return end
  244.           local z = {}
  245.           z.reader = reader
  246.           z.data = data or ""
  247.           z.name = name
  248.           -- set up additional data for reading
  249.           if not data or data == "" then z.n = 0 else z.n = #data end
  250.           z.p = 0
  251.           return z
  252.         end
  253.        
  254.         ------------------------------------------------------------------------
  255.         -- fill up input buffer
  256.         ------------------------------------------------------------------------
  257.         function luaZ:fill(z)
  258.           local buff = z.reader()
  259.           z.data = buff
  260.           if not buff or buff == "" then return "EOZ" end
  261.           z.n, z.p = #buff - 1, 1
  262.           return string.sub(buff, 1, 1)
  263.         end
  264.        
  265.         ------------------------------------------------------------------------
  266.         -- get next character from the input stream
  267.         -- * local n, p are used to optimize code generation
  268.         ------------------------------------------------------------------------
  269.         function luaZ:zgetc(z)
  270.           local n, p = z.n, z.p + 1
  271.           if n > 0 then
  272.             z.n, z.p = n - 1, p
  273.             return string.sub(z.data, p, p)
  274.           else
  275.             return self:fill(z)
  276.           end
  277.         end
  278.        
  279.         return luaZ
  280.     end
  281.     fake_module_scripts[script] = module_script
  282. end
  283. do -- nil.LuaX
  284.     local script = Instance.new('ModuleScript', nil)
  285.     script.Name = "LuaX"
  286.     local function module_script()
  287.         --[[--------------------------------------------------------------------
  288.        
  289.           llex.lua
  290.           Lua lexical analyzer in Lua
  291.           This file is part of Yueliang.
  292.        
  293.           Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net>
  294.           The COPYRIGHT file describes the conditions
  295.           under which this software may be distributed.
  296.        
  297.           See the ChangeLog for more information.
  298.        
  299.         ----------------------------------------------------------------------]]
  300.        
  301.         --[[--------------------------------------------------------------------
  302.         -- Notes:
  303.         -- * intended to 'imitate' llex.c code; performance is not a concern
  304.         -- * tokens are strings; code structure largely retained
  305.         -- * deleted stuff (compared to llex.c) are noted, comments retained
  306.         -- * nextc() returns the currently read character to simplify coding
  307.         --   here; next() in llex.c does not return anything
  308.         -- * compatibility code is marked with "--#" comments
  309.         --
  310.         -- Added:
  311.         -- * luaX:chunkid (function luaO_chunkid from lobject.c)
  312.         -- * luaX:str2d (function luaO_str2d from lobject.c)
  313.         -- * luaX.LUA_QS used in luaX:lexerror (from luaconf.h)
  314.         -- * luaX.LUA_COMPAT_LSTR in luaX:read_long_string (from luaconf.h)
  315.         -- * luaX.MAX_INT used in luaX:inclinenumber (from llimits.h)
  316.         --
  317.         -- To use the lexer:
  318.         -- (1) luaX:init() to initialize the lexer
  319.         -- (2) luaX:setinput() to set the input stream to lex
  320.         -- (3) call luaX:next() or luaX:luaX:lookahead() to get tokens,
  321.         --     until "TK_EOS": luaX:next()
  322.         -- * since EOZ is returned as a string, be careful when regexp testing
  323.         --
  324.         -- Not implemented:
  325.         -- * luaX_newstring: not required by this Lua implementation
  326.         -- * buffer MAX_SIZET size limit (from llimits.h) test not implemented
  327.         --   in the interest of performance
  328.         -- * locale-aware number handling is largely redundant as Lua's
  329.         --   tonumber() function is already capable of this
  330.         --
  331.         -- Changed in 5.1.x:
  332.         -- * TK_NAME token order moved down
  333.         -- * string representation for TK_NAME, TK_NUMBER, TK_STRING changed
  334.         -- * token struct renamed to lower case (LS -> ls)
  335.         -- * LexState struct: removed nestlevel, added decpoint
  336.         -- * error message functions have been greatly simplified
  337.         -- * token2string renamed to luaX_tokens, exposed in llex.h
  338.         -- * lexer now handles all kinds of newlines, including CRLF
  339.         -- * shbang first line handling removed from luaX:setinput;
  340.         --   it is now done in lauxlib.c (luaL_loadfile)
  341.         -- * next(ls) macro renamed to nextc(ls) due to new luaX_next function
  342.         -- * EXTRABUFF and MAXNOCHECK removed due to lexer changes
  343.         -- * checkbuffer(ls, len) macro deleted
  344.         -- * luaX:read_numeral now has 3 support functions: luaX:trydecpoint,
  345.         --   luaX:buffreplace and (luaO_str2d from lobject.c) luaX:str2d
  346.         -- * luaX:read_numeral is now more promiscuous in slurping characters;
  347.         --   hexadecimal numbers was added, locale-aware decimal points too
  348.         -- * luaX:skip_sep is new; used by luaX:read_long_string
  349.         -- * luaX:read_long_string handles new-style long blocks, with some
  350.         --   optional compatibility code
  351.         -- * luaX:llex: parts changed to support new-style long blocks
  352.         -- * luaX:llex: readname functionality has been folded in
  353.         -- * luaX:llex: removed test for control characters
  354.         --
  355.         --------------------------------------------------------------------]]
  356.        
  357.         local luaZ = require(script.Parent.LuaZ)
  358.        
  359.         local luaX = {}
  360.        
  361.         -- FIRST_RESERVED is not required as tokens are manipulated as strings
  362.         -- TOKEN_LEN deleted; maximum length of a reserved word not needed
  363.        
  364.         ------------------------------------------------------------------------
  365.         -- "ORDER RESERVED" deleted; enumeration in one place: luaX.RESERVED
  366.         ------------------------------------------------------------------------
  367.        
  368.         -- terminal symbols denoted by reserved words: TK_AND to TK_WHILE
  369.         -- other terminal symbols: TK_NAME to TK_EOS
  370.         luaX.RESERVED = [[
  371.         TK_AND and
  372.         TK_BREAK break
  373.         TK_DO do
  374.         TK_ELSE else
  375.         TK_ELSEIF elseif
  376.         TK_END end
  377.         TK_FALSE false
  378.         TK_FOR for
  379.         TK_FUNCTION function
  380.         TK_IF if
  381.         TK_IN in
  382.         TK_LOCAL local
  383.         TK_NIL nil
  384.         TK_NOT not
  385.         TK_OR or
  386.         TK_REPEAT repeat
  387.         TK_RETURN return
  388.         TK_THEN then
  389.         TK_TRUE true
  390.         TK_UNTIL until
  391.         TK_WHILE while
  392.         TK_CONCAT ..
  393.         TK_DOTS ...
  394.         TK_EQ ==
  395.         TK_GE >=
  396.         TK_LE <=
  397.         TK_NE ~=
  398.         TK_NAME <name>
  399.         TK_NUMBER <number>
  400.         TK_STRING <string>
  401.         TK_EOS <eof>]]
  402.        
  403.         -- NUM_RESERVED is not required; number of reserved words
  404.        
  405.         --[[--------------------------------------------------------------------
  406.         -- Instead of passing seminfo, the Token struct (e.g. ls.t) is passed
  407.         -- so that lexer functions can use its table element, ls.t.seminfo
  408.         --
  409.         -- SemInfo (struct no longer needed, a mixed-type value is used)
  410.         --
  411.         -- Token (struct of ls.t and ls.lookahead):
  412.         --   token  -- token symbol
  413.         --   seminfo  -- semantics information
  414.         --
  415.         -- LexState (struct of ls; ls is initialized by luaX:setinput):
  416.         --   current  -- current character (charint)
  417.         --   linenumber  -- input line counter
  418.         --   lastline  -- line of last token 'consumed'
  419.         --   t  -- current token (table: struct Token)
  420.         --   lookahead  -- look ahead token (table: struct Token)
  421.         --   fs  -- 'FuncState' is private to the parser
  422.         --   L -- LuaState
  423.         --   z  -- input stream
  424.         --   buff  -- buffer for tokens
  425.         --   source  -- current source name
  426.         --   decpoint -- locale decimal point
  427.         --   nestlevel  -- level of nested non-terminals
  428.         ----------------------------------------------------------------------]]
  429.        
  430.         -- luaX.tokens (was luaX_tokens) is now a hash; see luaX:init
  431.        
  432.         luaX.MAXSRC = 80
  433.         luaX.MAX_INT = 2147483645       -- constants from elsewhere (see above)
  434.         luaX.LUA_QS = "'%s'"
  435.         luaX.LUA_COMPAT_LSTR = 1
  436.         --luaX.MAX_SIZET = 4294967293
  437.        
  438.         ------------------------------------------------------------------------
  439.         -- initialize lexer
  440.         -- * original luaX_init has code to create and register token strings
  441.         -- * luaX.tokens: TK_* -> token
  442.         -- * luaX.enums:  token -> TK_* (used in luaX:llex)
  443.         ------------------------------------------------------------------------
  444.         function luaX:init()
  445.           local tokens, enums = {}, {}
  446.           for v in string.gmatch(self.RESERVED, "[^\n]+") do
  447.             local _, _, tok, str = string.find(v, "(%S+)%s+(%S+)")
  448.             tokens[tok] = str
  449.             enums[str] = tok
  450.           end
  451.           self.tokens = tokens
  452.           self.enums = enums
  453.         end
  454.        
  455.         ------------------------------------------------------------------------
  456.         -- returns a suitably-formatted chunk name or id
  457.         -- * from lobject.c, used in llex.c and ldebug.c
  458.         -- * the result, out, is returned (was first argument)
  459.         ------------------------------------------------------------------------
  460.         function luaX:chunkid(source, bufflen)
  461.           local out
  462.           local first = string.sub(source, 1, 1)
  463.           if first == "=" then
  464.             out = string.sub(source, 2, bufflen)  -- remove first char
  465.           else  -- out = "source", or "...source"
  466.             if first == "@" then
  467.               source = string.sub(source, 2)  -- skip the '@'
  468.               bufflen = bufflen - #" '...' "
  469.               local l = #source
  470.               out = ""
  471.               if l > bufflen then
  472.                 source = string.sub(source, 1 + l - bufflen)  -- get last part of file name
  473.                 out = out.."..."
  474.               end
  475.               out = out..source
  476.             else  -- out = [string "string"]
  477.               local len = string.find(source, "[\n\r]")  -- stop at first newline
  478.               len = len and (len - 1) or #source
  479.               bufflen = bufflen - #(" [string \"...\"] ")
  480.               if len > bufflen then len = bufflen end
  481.               out = "[string \""
  482.               if len < #source then  -- must truncate?
  483.                 out = out..string.sub(source, 1, len).."..."
  484.               else
  485.                 out = out..source
  486.               end
  487.               out = out.."\"]"
  488.             end
  489.           end
  490.           return out
  491.         end
  492.        
  493.         --[[--------------------------------------------------------------------
  494.         -- Support functions for lexer
  495.         -- * all lexer errors eventually reaches lexerror:
  496.              syntaxerror -> lexerror
  497.         ----------------------------------------------------------------------]]
  498.        
  499.         ------------------------------------------------------------------------
  500.         -- look up token and return keyword if found (also called by parser)
  501.         ------------------------------------------------------------------------
  502.         function luaX:token2str(ls, token)
  503.           if string.sub(token, 1, 3) ~= "TK_" then
  504.             if string.find(token, "%c") then
  505.               return string.format("char(%d)", string.byte(token))
  506.             end
  507.             return token
  508.           else
  509.           end
  510.             return self.tokens[token]
  511.         end
  512.        
  513.         ------------------------------------------------------------------------
  514.         -- throws a lexer error
  515.         -- * txtToken has been made local to luaX:lexerror
  516.         -- * can't communicate LUA_ERRSYNTAX, so it is unimplemented
  517.         ------------------------------------------------------------------------
  518.         function luaX:lexerror(ls, msg, token)
  519.           local function txtToken(ls, token)
  520.             if token == "TK_NAME" or
  521.                token == "TK_STRING" or
  522.                token == "TK_NUMBER" then
  523.               return ls.buff
  524.             else
  525.               return self:token2str(ls, token)
  526.             end
  527.           end
  528.           local buff = self:chunkid(ls.source, self.MAXSRC)
  529.           local msg = string.format("%s:%d: %s", buff, ls.linenumber, msg)
  530.           if token then
  531.             msg = string.format("%s near "..self.LUA_QS, msg, txtToken(ls, token))
  532.           end
  533.           -- luaD_throw(ls->L, LUA_ERRSYNTAX)
  534.           error(msg)
  535.         end
  536.        
  537.         ------------------------------------------------------------------------
  538.         -- throws a syntax error (mainly called by parser)
  539.         -- * ls.t.token has to be set by the function calling luaX:llex
  540.         --   (see luaX:next and luaX:lookahead elsewhere in this file)
  541.         ------------------------------------------------------------------------
  542.         function luaX:syntaxerror(ls, msg)
  543.           self:lexerror(ls, msg, ls.t.token)
  544.         end
  545.        
  546.         ------------------------------------------------------------------------
  547.         -- move on to next line
  548.         ------------------------------------------------------------------------
  549.         function luaX:currIsNewline(ls)
  550.           return ls.current == "\n" or ls.current == "\r"
  551.         end
  552.        
  553.         function luaX:inclinenumber(ls)
  554.           local old = ls.current
  555.           -- lua_assert(currIsNewline(ls))
  556.           self:nextc(ls)  -- skip '\n' or '\r'
  557.           if self:currIsNewline(ls) and ls.current ~= old then
  558.             self:nextc(ls)  -- skip '\n\r' or '\r\n'
  559.           end
  560.           ls.linenumber = ls.linenumber + 1
  561.           if ls.linenumber >= self.MAX_INT then
  562.             self:syntaxerror(ls, "chunk has too many lines")
  563.           end
  564.         end
  565.        
  566.         ------------------------------------------------------------------------
  567.         -- initializes an input stream for lexing
  568.         -- * if ls (the lexer state) is passed as a table, then it is filled in,
  569.         --   otherwise it has to be retrieved as a return value
  570.         -- * LUA_MINBUFFER not used; buffer handling not required any more
  571.         ------------------------------------------------------------------------
  572.         function luaX:setinput(L, ls, z, source)
  573.           if not ls then ls = {} end  -- create struct
  574.           if not ls.lookahead then ls.lookahead = {} end
  575.           if not ls.t then ls.t = {} end
  576.           ls.decpoint = "."
  577.           ls.L = L
  578.           ls.lookahead.token = "TK_EOS"  -- no look-ahead token
  579.           ls.z = z
  580.           ls.fs = nil
  581.           ls.linenumber = 1
  582.           ls.lastline = 1
  583.           ls.source = source
  584.           self:nextc(ls)  -- read first char
  585.         end
  586.        
  587.         --[[--------------------------------------------------------------------
  588.         -- LEXICAL ANALYZER
  589.         ----------------------------------------------------------------------]]
  590.        
  591.         ------------------------------------------------------------------------
  592.         -- checks if current character read is found in the set 'set'
  593.         ------------------------------------------------------------------------
  594.         function luaX:check_next(ls, set)
  595.           if not string.find(set, ls.current, 1, 1) then
  596.             return false
  597.           end
  598.           self:save_and_next(ls)
  599.           return true
  600.         end
  601.        
  602.         ------------------------------------------------------------------------
  603.         -- retrieve next token, checking the lookahead buffer if necessary
  604.         -- * note that the macro next(ls) in llex.c is now luaX:nextc
  605.         -- * utilized used in lparser.c (various places)
  606.         ------------------------------------------------------------------------
  607.         function luaX:next(ls)
  608.           ls.lastline = ls.linenumber
  609.           if ls.lookahead.token ~= "TK_EOS" then  -- is there a look-ahead token?
  610.             -- this must be copy-by-value
  611.             ls.t.seminfo = ls.lookahead.seminfo  -- use this one
  612.             ls.t.token = ls.lookahead.token
  613.             ls.lookahead.token = "TK_EOS"  -- and discharge it
  614.           else
  615.             ls.t.token = self:llex(ls, ls.t)  -- read next token
  616.           end
  617.         end
  618.        
  619.         ------------------------------------------------------------------------
  620.         -- fill in the lookahead buffer
  621.         -- * utilized used in lparser.c:constructor
  622.         ------------------------------------------------------------------------
  623.         function luaX:lookahead(ls)
  624.           -- lua_assert(ls.lookahead.token == "TK_EOS")
  625.           ls.lookahead.token = self:llex(ls, ls.lookahead)
  626.         end
  627.        
  628.         ------------------------------------------------------------------------
  629.         -- gets the next character and returns it
  630.         -- * this is the next() macro in llex.c; see notes at the beginning
  631.         ------------------------------------------------------------------------
  632.         function luaX:nextc(ls)
  633.           local c = luaZ:zgetc(ls.z)
  634.           ls.current = c
  635.           return c
  636.         end
  637.        
  638.         ------------------------------------------------------------------------
  639.         -- saves the given character into the token buffer
  640.         -- * buffer handling code removed, not used in this implementation
  641.         -- * test for maximum token buffer length not used, makes things faster
  642.         ------------------------------------------------------------------------
  643.        
  644.         function luaX:save(ls, c)
  645.           local buff = ls.buff
  646.           -- if you want to use this, please uncomment luaX.MAX_SIZET further up
  647.           --if #buff > self.MAX_SIZET then
  648.           --  self:lexerror(ls, "lexical element too long")
  649.           --end
  650.           ls.buff = buff..c
  651.         end
  652.        
  653.         ------------------------------------------------------------------------
  654.         -- save current character into token buffer, grabs next character
  655.         -- * like luaX:nextc, returns the character read for convenience
  656.         ------------------------------------------------------------------------
  657.         function luaX:save_and_next(ls)
  658.           self:save(ls, ls.current)
  659.           return self:nextc(ls)
  660.         end
  661.        
  662.         ------------------------------------------------------------------------
  663.         -- LUA_NUMBER
  664.         -- * luaX:read_numeral is the main lexer function to read a number
  665.         -- * luaX:str2d, luaX:buffreplace, luaX:trydecpoint are support functions
  666.         ------------------------------------------------------------------------
  667.        
  668.         ------------------------------------------------------------------------
  669.         -- string to number converter (was luaO_str2d from lobject.c)
  670.         -- * returns the number, nil if fails (originally returns a boolean)
  671.         -- * conversion function originally lua_str2number(s,p), a macro which
  672.         --   maps to the strtod() function by default (from luaconf.h)
  673.         ------------------------------------------------------------------------
  674.         function luaX:str2d(s)
  675.           local result = tonumber(s)
  676.           if result then return result end
  677.           -- conversion failed
  678.           if string.lower(string.sub(s, 1, 2)) == "0x" then  -- maybe an hexadecimal constant?
  679.             result = tonumber(s, 16)
  680.             if result then return result end  -- most common case
  681.             -- Was: invalid trailing characters?
  682.             -- In C, this function then skips over trailing spaces.
  683.             -- true is returned if nothing else is found except for spaces.
  684.             -- If there is still something else, then it returns a false.
  685.             -- All this is not necessary using Lua's tonumber.
  686.           end
  687.           return nil
  688.         end
  689.        
  690.         ------------------------------------------------------------------------
  691.         -- single-character replacement, for locale-aware decimal points
  692.         ------------------------------------------------------------------------
  693.         function luaX:buffreplace(ls, from, to)
  694.           local result, buff = "", ls.buff
  695.           for p = 1, #buff do
  696.             local c = string.sub(buff, p, p)
  697.             if c == from then c = to end
  698.             result = result..c
  699.           end
  700.           ls.buff = result
  701.         end
  702.        
  703.         ------------------------------------------------------------------------
  704.         -- Attempt to convert a number by translating '.' decimal points to
  705.         -- the decimal point character used by the current locale. This is not
  706.         -- needed in Yueliang as Lua's tonumber() is already locale-aware.
  707.         -- Instead, the code is here in case the user implements localeconv().
  708.         ------------------------------------------------------------------------
  709.         function luaX:trydecpoint(ls, Token)
  710.           -- format error: try to update decimal point separator
  711.           local old = ls.decpoint
  712.           -- translate the following to Lua if you implement localeconv():
  713.           -- struct lconv *cv = localeconv();
  714.           -- ls->decpoint = (cv ? cv->decimal_point[0] : '.');
  715.           self:buffreplace(ls, old, ls.decpoint)  -- try updated decimal separator
  716.           local seminfo = self:str2d(ls.buff)
  717.           Token.seminfo = seminfo
  718.           if not seminfo then
  719.             -- format error with correct decimal point: no more options
  720.             self:buffreplace(ls, ls.decpoint, ".")  -- undo change (for error message)
  721.             self:lexerror(ls, "malformed number", "TK_NUMBER")
  722.           end
  723.         end
  724.        
  725.         ------------------------------------------------------------------------
  726.         -- main number conversion function
  727.         -- * "^%w$" needed in the scan in order to detect "EOZ"
  728.         ------------------------------------------------------------------------
  729.         function luaX:read_numeral(ls, Token)
  730.           -- lua_assert(string.find(ls.current, "%d"))
  731.           repeat
  732.             self:save_and_next(ls)
  733.           until string.find(ls.current, "%D") and ls.current ~= "."
  734.           if self:check_next(ls, "Ee") then  -- 'E'?
  735.             self:check_next(ls, "+-")  -- optional exponent sign
  736.           end
  737.           while string.find(ls.current, "^%w$") or ls.current == "_" do
  738.             self:save_and_next(ls)
  739.           end
  740.           self:buffreplace(ls, ".", ls.decpoint)  -- follow locale for decimal point
  741.           local seminfo = self:str2d(ls.buff)
  742.           Token.seminfo = seminfo
  743.           if not seminfo then  -- format error?
  744.             self:trydecpoint(ls, Token) -- try to update decimal point separator
  745.           end
  746.         end
  747.        
  748.         ------------------------------------------------------------------------
  749.         -- count separators ("=") in a long string delimiter
  750.         -- * used by luaX:read_long_string
  751.         ------------------------------------------------------------------------
  752.         function luaX:skip_sep(ls)
  753.           local count = 0
  754.           local s = ls.current
  755.           -- lua_assert(s == "[" or s == "]")
  756.           self:save_and_next(ls)
  757.           while ls.current == "=" do
  758.             self:save_and_next(ls)
  759.             count = count + 1
  760.           end
  761.           return (ls.current == s) and count or (-count) - 1
  762.         end
  763.        
  764.         ------------------------------------------------------------------------
  765.         -- reads a long string or long comment
  766.         ------------------------------------------------------------------------
  767.         function luaX:read_long_string(ls, Token, sep)
  768.           local cont = 0
  769.           self:save_and_next(ls)  -- skip 2nd '['
  770.           if self:currIsNewline(ls) then  -- string starts with a newline?
  771.             self:inclinenumber(ls)  -- skip it
  772.           end
  773.           while true do
  774.             local c = ls.current
  775.             if c == "EOZ" then
  776.               self:lexerror(ls, Token and "unfinished long string" or
  777.                             "unfinished long comment", "TK_EOS")
  778.             elseif c == "[" then
  779.               --# compatibility code start
  780.               if self.LUA_COMPAT_LSTR then
  781.                 if self:skip_sep(ls) == sep then
  782.                   self:save_and_next(ls)  -- skip 2nd '['
  783.                   cont = cont + 1
  784.                   --# compatibility code start
  785.                   if self.LUA_COMPAT_LSTR == 1 then
  786.                     if sep == 0 then
  787.                       self:lexerror(ls, "nesting of [[...]] is deprecated", "[")
  788.                     end
  789.                   end
  790.                   --# compatibility code end
  791.                 end
  792.               end
  793.               --# compatibility code end
  794.             elseif c == "]" then
  795.               if self:skip_sep(ls) == sep then
  796.                 self:save_and_next(ls)  -- skip 2nd ']'
  797.                 --# compatibility code start
  798.                 if self.LUA_COMPAT_LSTR and self.LUA_COMPAT_LSTR == 2 then
  799.                   cont = cont - 1
  800.                   if sep == 0 and cont >= 0 then break end
  801.                 end
  802.                 --# compatibility code end
  803.                 break
  804.               end
  805.             elseif self:currIsNewline(ls) then
  806.               self:save(ls, "\n")
  807.               self:inclinenumber(ls)
  808.               if not Token then ls.buff = "" end -- avoid wasting space
  809.             else  -- default
  810.               if Token then
  811.                 self:save_and_next(ls)
  812.               else
  813.                 self:nextc(ls)
  814.               end
  815.             end--if c
  816.           end--while
  817.           if Token then
  818.             local p = 3 + sep
  819.             Token.seminfo = string.sub(ls.buff, p, -p)
  820.           end
  821.         end
  822.        
  823.         ------------------------------------------------------------------------
  824.         -- reads a string
  825.         -- * has been restructured significantly compared to the original C code
  826.         ------------------------------------------------------------------------
  827.        
  828.         function luaX:read_string(ls, del, Token)
  829.           self:save_and_next(ls)
  830.           while ls.current ~= del do
  831.             local c = ls.current
  832.             if c == "EOZ" then
  833.               self:lexerror(ls, "unfinished string", "TK_EOS")
  834.             elseif self:currIsNewline(ls) then
  835.               self:lexerror(ls, "unfinished string", "TK_STRING")
  836.             elseif c == "\\" then
  837.               c = self:nextc(ls)  -- do not save the '\'
  838.               if self:currIsNewline(ls) then  -- go through
  839.                 self:save(ls, "\n")
  840.                 self:inclinenumber(ls)
  841.               elseif c ~= "EOZ" then -- will raise an error next loop
  842.                 -- escapes handling greatly simplified here:
  843.                 local i = string.find("abfnrtv", c, 1, 1)
  844.                 if i then
  845.                   self:save(ls, string.sub("\a\b\f\n\r\t\v", i, i))
  846.                   self:nextc(ls)
  847.                 elseif not string.find(c, "%d") then
  848.                   self:save_and_next(ls)  -- handles \\, \", \', and \?
  849.                 else  -- \xxx
  850.                   c, i = 0, 0
  851.                   repeat
  852.                     c = 10 * c + ls.current
  853.                     self:nextc(ls)
  854.                     i = i + 1
  855.                   until i >= 3 or not string.find(ls.current, "%d")
  856.                   if c > 255 then  -- UCHAR_MAX
  857.                     self:lexerror(ls, "escape sequence too large", "TK_STRING")
  858.                   end
  859.                   self:save(ls, string.char(c))
  860.                 end
  861.               end
  862.             else
  863.               self:save_and_next(ls)
  864.             end--if c
  865.           end--while
  866.           self:save_and_next(ls)  -- skip delimiter
  867.           Token.seminfo = string.sub(ls.buff, 2, -2)
  868.         end
  869.        
  870.         ------------------------------------------------------------------------
  871.         -- main lexer function
  872.         ------------------------------------------------------------------------
  873.         function luaX:llex(ls, Token)
  874.           ls.buff = ""
  875.           while true do
  876.             local c = ls.current
  877.             ----------------------------------------------------------------
  878.             if self:currIsNewline(ls) then
  879.               self:inclinenumber(ls)
  880.             ----------------------------------------------------------------
  881.             elseif c == "-" then
  882.               c = self:nextc(ls)
  883.               if c ~= "-" then return "-" end
  884.               -- else is a comment
  885.               local sep = -1
  886.               if self:nextc(ls) == '[' then
  887.                 sep = self:skip_sep(ls)
  888.                 ls.buff = ""  -- 'skip_sep' may dirty the buffer
  889.               end
  890.               if sep >= 0 then
  891.                 self:read_long_string(ls, nil, sep)  -- long comment
  892.                 ls.buff = ""
  893.               else  -- else short comment
  894.                 while not self:currIsNewline(ls) and ls.current ~= "EOZ" do
  895.                   self:nextc(ls)
  896.                 end
  897.               end
  898.             ----------------------------------------------------------------
  899.             elseif c == "[" then
  900.               local sep = self:skip_sep(ls)
  901.               if sep >= 0 then
  902.                 self:read_long_string(ls, Token, sep)
  903.                 return "TK_STRING"
  904.               elseif sep == -1 then
  905.                 return "["
  906.               else
  907.                 self:lexerror(ls, "invalid long string delimiter", "TK_STRING")
  908.               end
  909.             ----------------------------------------------------------------
  910.             elseif c == "=" then
  911.               c = self:nextc(ls)
  912.               if c ~= "=" then return "="
  913.               else self:nextc(ls); return "TK_EQ" end
  914.             ----------------------------------------------------------------
  915.             elseif c == "<" then
  916.               c = self:nextc(ls)
  917.               if c ~= "=" then return "<"
  918.               else self:nextc(ls); return "TK_LE" end
  919.             ----------------------------------------------------------------
  920.             elseif c == ">" then
  921.               c = self:nextc(ls)
  922.               if c ~= "=" then return ">"
  923.               else self:nextc(ls); return "TK_GE" end
  924.             ----------------------------------------------------------------
  925.             elseif c == "~" then
  926.               c = self:nextc(ls)
  927.               if c ~= "=" then return "~"
  928.               else self:nextc(ls); return "TK_NE" end
  929.             ----------------------------------------------------------------
  930.             elseif c == "\"" or c == "'" then
  931.               self:read_string(ls, c, Token)
  932.               return "TK_STRING"
  933.             ----------------------------------------------------------------
  934.             elseif c == "." then
  935.               c = self:save_and_next(ls)
  936.               if self:check_next(ls, ".") then
  937.                 if self:check_next(ls, ".") then
  938.                   return "TK_DOTS"   -- ...
  939.                 else return "TK_CONCAT"   -- ..
  940.                 end
  941.               elseif not string.find(c, "%d") then
  942.                 return "."
  943.               else
  944.                 self:read_numeral(ls, Token)
  945.                 return "TK_NUMBER"
  946.               end
  947.             ----------------------------------------------------------------
  948.             elseif c == "EOZ" then
  949.               return "TK_EOS"
  950.             ----------------------------------------------------------------
  951.             else  -- default
  952.               if string.find(c, "%s") then
  953.                 -- lua_assert(self:currIsNewline(ls))
  954.                 self:nextc(ls)
  955.               elseif string.find(c, "%d") then
  956.                 self:read_numeral(ls, Token)
  957.                 return "TK_NUMBER"
  958.               elseif string.find(c, "[_%a]") then
  959.                 -- identifier or reserved word
  960.                 repeat
  961.                   c = self:save_and_next(ls)
  962.                 until c == "EOZ" or not string.find(c, "[_%w]")
  963.                 local ts = ls.buff
  964.                 local tok = self.enums[ts]
  965.                 if tok then return tok end  -- reserved word?
  966.                 Token.seminfo = ts
  967.                 return "TK_NAME"
  968.               else
  969.                 self:nextc(ls)
  970.                 return c  -- single-char tokens (+ - / ...)
  971.               end
  972.             ----------------------------------------------------------------
  973.             end--if c
  974.           end--while
  975.         end
  976.        
  977.         return luaX
  978.     end
  979.     fake_module_scripts[script] = module_script
  980. end
  981. do -- nil.LuaY
  982.     local script = Instance.new('ModuleScript', nil)
  983.     script.Name = "LuaY"
  984.     local function module_script()
  985.         --[[--------------------------------------------------------------------
  986.        
  987.           lparser.lua
  988.           Lua 5 parser in Lua
  989.           This file is part of Yueliang.
  990.        
  991.           Copyright (c) 2005-2007 Kein-Hong Man <khman@users.sf.net>
  992.           The COPYRIGHT file describes the conditions
  993.           under which this software may be distributed.
  994.        
  995.           See the ChangeLog for more information.
  996.        
  997.         ----------------------------------------------------------------------]]
  998.        
  999.         --[[--------------------------------------------------------------------
  1000.         -- Notes:
  1001.         -- * some unused C code that were not converted are kept as comments
  1002.         -- * LUA_COMPAT_VARARG option changed into a comment block
  1003.         -- * for value/size specific code added, look for 'NOTE: '
  1004.         --
  1005.         -- Not implemented:
  1006.         -- * luaX_newstring not needed by this Lua implementation
  1007.         -- * luaG_checkcode() in assert is not currently implemented
  1008.         --
  1009.         -- Added:
  1010.         -- * some constants added from various header files
  1011.         -- * luaY.LUA_QS used in error_expected, check_match (from luaconf.h)
  1012.         -- * luaY:LUA_QL needed for error messages (from luaconf.h)
  1013.         -- * luaY:growvector (from lmem.h) -- skeleton only, limit checking
  1014.         -- * luaY.SHRT_MAX (from <limits.h>) for registerlocalvar
  1015.         -- * luaY:newproto (from lfunc.c)
  1016.         -- * luaY:int2fb (from lobject.c)
  1017.         -- * NOTE: HASARG_MASK, for implementing a VARARG_HASARG bit operation
  1018.         -- * NOTE: value-specific code for VARARG_NEEDSARG to replace a bitop
  1019.         --
  1020.         -- Changed in 5.1.x:
  1021.         -- * various code changes are not detailed...
  1022.         -- * names of constants may have changed, e.g. added a LUAI_ prefix
  1023.         -- * struct expkind: added VKNUM, VVARARG; VCALL's info changed?
  1024.         -- * struct expdesc: added nval
  1025.         -- * struct FuncState: upvalues data type changed to upvaldesc
  1026.         -- * macro hasmultret is new
  1027.         -- * function checklimit moved to parser from lexer
  1028.         -- * functions anchor_token, errorlimit, checknext are new
  1029.         -- * checknext is new, equivalent to 5.0.x's check, see check too
  1030.         -- * luaY:next and luaY:lookahead moved to lexer
  1031.         -- * break keyword no longer skipped in luaY:breakstat
  1032.         -- * function new_localvarstr replaced by new_localvarliteral
  1033.         -- * registerlocalvar limits local variables to SHRT_MAX
  1034.         -- * create_local deleted, new_localvarliteral used instead
  1035.         -- * constant LUAI_MAXUPVALUES increased to 60
  1036.         -- * constants MAXPARAMS, LUA_MAXPARSERLEVEL, MAXSTACK removed
  1037.         -- * function interface changed: singlevaraux, singlevar
  1038.         -- * enterlevel and leavelevel uses nCcalls to track call depth
  1039.         -- * added a name argument to main entry function, luaY:parser
  1040.         -- * function luaY_index changed to yindex
  1041.         -- * luaY:int2fb()'s table size encoding format has been changed
  1042.         -- * luaY:log2() no longer needed for table constructors
  1043.         -- * function code_params deleted, functionality folded in parlist
  1044.         -- * vararg flags handling (is_vararg) changes; also see VARARG_*
  1045.         -- * LUA_COMPATUPSYNTAX section for old-style upvalues removed
  1046.         -- * repeatstat() calls chunk() instead of block()
  1047.         -- * function interface changed: cond, test_then_block
  1048.         -- * while statement implementation considerably simplified; MAXEXPWHILE
  1049.         --   and EXTRAEXP no longer required, no limits to the complexity of a
  1050.         --   while condition
  1051.         -- * repeat, forbody statement implementation has major changes,
  1052.         --   mostly due to new scoping behaviour of local variables
  1053.         -- * OPR_MULT renamed to OPR_MUL
  1054.         ----------------------------------------------------------------------]]
  1055.        
  1056.         --requires luaP, luaX, luaK
  1057.         local luaY = {}
  1058.         local luaX = require(script.Parent.LuaX)
  1059.         local luaK = require(script.Parent.LuaK)(luaY)
  1060.         local luaP = require(script.Parent.LuaP)
  1061.        
  1062.         --[[--------------------------------------------------------------------
  1063.         -- Expression descriptor
  1064.         -- * expkind changed to string constants; luaY:assignment was the only
  1065.         --   function to use a relational operator with this enumeration
  1066.         -- VVOID       -- no value
  1067.         -- VNIL        -- no value
  1068.         -- VTRUE       -- no value
  1069.         -- VFALSE      -- no value
  1070.         -- VK          -- info = index of constant in 'k'
  1071.         -- VKNUM       -- nval = numerical value
  1072.         -- VLOCAL      -- info = local register
  1073.         -- VUPVAL,     -- info = index of upvalue in 'upvalues'
  1074.         -- VGLOBAL     -- info = index of table; aux = index of global name in 'k'
  1075.         -- VINDEXED    -- info = table register; aux = index register (or 'k')
  1076.         -- VJMP        -- info = instruction pc
  1077.         -- VRELOCABLE  -- info = instruction pc
  1078.         -- VNONRELOC   -- info = result register
  1079.         -- VCALL       -- info = instruction pc
  1080.         -- VVARARG     -- info = instruction pc
  1081.         } ----------------------------------------------------------------------]]
  1082.        
  1083.         --[[--------------------------------------------------------------------
  1084.         -- * expdesc in Lua 5.1.x has a union u and another struct s; this Lua
  1085.         --   implementation ignores all instances of u and s usage
  1086.         -- struct expdesc:
  1087.         --   k  -- (enum: expkind)
  1088.         --   info, aux -- (int, int)
  1089.         --   nval -- (lua_Number)
  1090.         --   t  -- patch list of 'exit when true'
  1091.         --   f  -- patch list of 'exit when false'
  1092.         ----------------------------------------------------------------------]]
  1093.        
  1094.         --[[--------------------------------------------------------------------
  1095.         -- struct upvaldesc:
  1096.         --   k  -- (lu_byte)
  1097.         --   info -- (lu_byte)
  1098.         ----------------------------------------------------------------------]]
  1099.        
  1100.         --[[--------------------------------------------------------------------
  1101.         -- state needed to generate code for a given function
  1102.         -- struct FuncState:
  1103.         --   f  -- current function header (table: Proto)
  1104.         --   h  -- table to find (and reuse) elements in 'k' (table: Table)
  1105.         --   prev  -- enclosing function (table: FuncState)
  1106.         --   ls  -- lexical state (table: LexState)
  1107.         --   L  -- copy of the Lua state (table: lua_State)
  1108.         --   bl  -- chain of current blocks (table: BlockCnt)
  1109.         --   pc  -- next position to code (equivalent to 'ncode')
  1110.         --   lasttarget   -- 'pc' of last 'jump target'
  1111.         --   jpc  -- list of pending jumps to 'pc'
  1112.         --   freereg  -- first free register
  1113.         --   nk  -- number of elements in 'k'
  1114.         --   np  -- number of elements in 'p'
  1115.         --   nlocvars  -- number of elements in 'locvars'
  1116.         --   nactvar  -- number of active local variables
  1117.         --   upvalues[LUAI_MAXUPVALUES]  -- upvalues (table: upvaldesc)
  1118.         --   actvar[LUAI_MAXVARS]  -- declared-variable stack
  1119.         ----------------------------------------------------------------------]]
  1120.        
  1121.         ------------------------------------------------------------------------
  1122.         -- constants used by parser
  1123.         -- * picks up duplicate values from luaX if required
  1124.         ------------------------------------------------------------------------
  1125.        
  1126.         luaY.LUA_QS = luaX.LUA_QS or "'%s'"  -- (from luaconf.h)
  1127.        
  1128.         luaY.SHRT_MAX = 32767 -- (from <limits.h>)
  1129.         luaY.LUAI_MAXVARS = 200  -- (luaconf.h)
  1130.         luaY.LUAI_MAXUPVALUES = 60  -- (luaconf.h)
  1131.         luaY.MAX_INT = luaX.MAX_INT or 2147483645  -- (from llimits.h)
  1132.           -- * INT_MAX-2 for 32-bit systems
  1133.         luaY.LUAI_MAXCCALLS = 200  -- (from luaconf.h)
  1134.        
  1135.         luaY.VARARG_HASARG = 1  -- (from lobject.h)
  1136.         -- NOTE: HASARG_MASK is value-specific
  1137.         luaY.HASARG_MASK = 2 -- this was added for a bitop in parlist()
  1138.         luaY.VARARG_ISVARARG = 2
  1139.         -- NOTE: there is some value-specific code that involves VARARG_NEEDSARG
  1140.         luaY.VARARG_NEEDSARG = 4
  1141.        
  1142.         luaY.LUA_MULTRET = -1  -- (lua.h)
  1143.        
  1144.         --[[--------------------------------------------------------------------
  1145.         -- other functions
  1146.         ----------------------------------------------------------------------]]
  1147.        
  1148.         ------------------------------------------------------------------------
  1149.         -- LUA_QL describes how error messages quote program elements.
  1150.         -- CHANGE it if you want a different appearance. (from luaconf.h)
  1151.         ------------------------------------------------------------------------
  1152.         function luaY:LUA_QL(x)
  1153.           return "'"..x.."'"
  1154.         end
  1155.        
  1156.         ------------------------------------------------------------------------
  1157.         -- this is a stripped-down luaM_growvector (from lmem.h) which is a
  1158.         -- macro based on luaM_growaux (in lmem.c); all the following does is
  1159.         -- reproduce the size limit checking logic of the original function
  1160.         -- so that error behaviour is identical; all arguments preserved for
  1161.         -- convenience, even those which are unused
  1162.         -- * set the t field to nil, since this originally does a sizeof(t)
  1163.         -- * size (originally a pointer) is never updated, their final values
  1164.         --   are set by luaY:close_func(), so overall things should still work
  1165.         ------------------------------------------------------------------------
  1166.         function luaY:growvector(L, v, nelems, size, t, limit, e)
  1167.           if nelems >= limit then
  1168.             error(e)  -- was luaG_runerror
  1169.           end
  1170.         end
  1171.        
  1172.         ------------------------------------------------------------------------
  1173.         -- initialize a new function prototype structure (from lfunc.c)
  1174.         -- * used only in open_func()
  1175.         ------------------------------------------------------------------------
  1176.         function luaY:newproto(L)
  1177.           local f = {} -- Proto
  1178.           -- luaC_link(L, obj2gco(f), LUA_TPROTO); /* GC */
  1179.           f.k = {}
  1180.           f.sizek = 0
  1181.           f.p = {}
  1182.           f.sizep = 0
  1183.           f.code = {}
  1184.           f.sizecode = 0
  1185.           f.sizelineinfo = 0
  1186.           f.sizeupvalues = 0
  1187.           f.nups = 0
  1188.           f.upvalues = {}
  1189.           f.numparams = 0
  1190.           f.is_vararg = 0
  1191.           f.maxstacksize = 0
  1192.           f.lineinfo = {}
  1193.           f.sizelocvars = 0
  1194.           f.locvars = {}
  1195.           f.lineDefined = 0
  1196.           f.lastlinedefined = 0
  1197.           f.source = nil
  1198.           return f
  1199.         end
  1200.        
  1201.         ------------------------------------------------------------------------
  1202.         -- converts an integer to a "floating point byte", represented as
  1203.         -- (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  1204.         -- eeeee != 0 and (xxx) otherwise.
  1205.         ------------------------------------------------------------------------
  1206.         function luaY:int2fb(x)
  1207.           local e = 0  -- exponent
  1208.           while x >= 16 do
  1209.             x = math.floor((x + 1) / 2)
  1210.             e = e + 1
  1211.           end
  1212.           if x < 8 then
  1213.             return x
  1214.           else
  1215.             return ((e + 1) * 8) + (x - 8)
  1216.           end
  1217.         end
  1218.        
  1219.         --[[--------------------------------------------------------------------
  1220.         -- parser functions
  1221.         ----------------------------------------------------------------------]]
  1222.        
  1223.         ------------------------------------------------------------------------
  1224.         -- true of the kind of expression produces multiple return values
  1225.         ------------------------------------------------------------------------
  1226.         function luaY:hasmultret(k)
  1227.           return k == "VCALL" or k == "VVARARG"
  1228.         end
  1229.        
  1230.         ------------------------------------------------------------------------
  1231.         -- convenience function to access active local i, returns entry
  1232.         ------------------------------------------------------------------------
  1233.         function luaY:getlocvar(fs, i)
  1234.           return fs.f.locvars[ fs.actvar[i] ]
  1235.         end
  1236.        
  1237.         ------------------------------------------------------------------------
  1238.         -- check a limit, string m provided as an error message
  1239.         ------------------------------------------------------------------------
  1240.         function luaY:checklimit(fs, v, l, m)
  1241.           if v > l then self:errorlimit(fs, l, m) end
  1242.         end
  1243.        
  1244.         --[[--------------------------------------------------------------------
  1245.         -- nodes for block list (list of active blocks)
  1246.         -- struct BlockCnt:
  1247.         --   previous  -- chain (table: BlockCnt)
  1248.         --   breaklist  -- list of jumps out of this loop
  1249.         --   nactvar  -- # active local variables outside the breakable structure
  1250.         --   upval  -- true if some variable in the block is an upvalue (boolean)
  1251.         --   isbreakable  -- true if 'block' is a loop (boolean)
  1252.         ----------------------------------------------------------------------]]
  1253.        
  1254.         ------------------------------------------------------------------------
  1255.         -- prototypes for recursive non-terminal functions
  1256.         ------------------------------------------------------------------------
  1257.         -- prototypes deleted; not required in Lua
  1258.        
  1259.         ------------------------------------------------------------------------
  1260.         -- reanchor if last token is has a constant string, see close_func()
  1261.         -- * used only in close_func()
  1262.         ------------------------------------------------------------------------
  1263.         function luaY:anchor_token(ls)
  1264.           if ls.t.token == "TK_NAME" or ls.t.token == "TK_STRING" then
  1265.             -- not relevant to Lua implementation of parser
  1266.             -- local ts = ls.t.seminfo
  1267.             -- luaX_newstring(ls, getstr(ts), ts->tsv.len); /* C */
  1268.           end
  1269.         end
  1270.        
  1271.         ------------------------------------------------------------------------
  1272.         -- throws a syntax error if token expected is not there
  1273.         ------------------------------------------------------------------------
  1274.         function luaY:error_expected(ls, token)
  1275.           luaX:syntaxerror(ls,
  1276.             string.format(self.LUA_QS.." expected", luaX:token2str(ls, token)))
  1277.         end
  1278.        
  1279.         ------------------------------------------------------------------------
  1280.         -- prepares error message for display, for limits exceeded
  1281.         -- * used only in checklimit()
  1282.         ------------------------------------------------------------------------
  1283.         function luaY:errorlimit(fs, limit, what)
  1284.           local msg = (fs.f.linedefined == 0) and
  1285.             string.format("main function has more than %d %s", limit, what) or
  1286.             string.format("function at line %d has more than %d %s",
  1287.                           fs.f.linedefined, limit, what)
  1288.           luaX:lexerror(fs.ls, msg, 0)
  1289.         end
  1290.        
  1291.         ------------------------------------------------------------------------
  1292.         -- tests for a token, returns outcome
  1293.         -- * return value changed to boolean
  1294.         ------------------------------------------------------------------------
  1295.         function luaY:testnext(ls, c)
  1296.           if ls.t.token == c then
  1297.             luaX:next(ls)
  1298.             return true
  1299.           else
  1300.             return false
  1301.           end
  1302.         end
  1303.        
  1304.         ------------------------------------------------------------------------
  1305.         -- check for existence of a token, throws error if not found
  1306.         ------------------------------------------------------------------------
  1307.         function luaY:check(ls, c)
  1308.           if ls.t.token ~= c then
  1309.             self:error_expected(ls, c)
  1310.           end
  1311.         end
  1312.        
  1313.         ------------------------------------------------------------------------
  1314.         -- verify existence of a token, then skip it
  1315.         ------------------------------------------------------------------------
  1316.         function luaY:checknext(ls, c)
  1317.           self:check(ls, c)
  1318.           luaX:next(ls)
  1319.         end
  1320.        
  1321.         ------------------------------------------------------------------------
  1322.         -- throws error if condition not matched
  1323.         ------------------------------------------------------------------------
  1324.         function luaY:check_condition(ls, c, msg)
  1325.           if not c then luaX:syntaxerror(ls, msg) end
  1326.         end
  1327.        
  1328.         ------------------------------------------------------------------------
  1329.         -- verifies token conditions are met or else throw error
  1330.         ------------------------------------------------------------------------
  1331.         function luaY:check_match(ls, what, who, where)
  1332.           if not self:testnext(ls, what) then
  1333.             if where == ls.linenumber then
  1334.               self:error_expected(ls, what)
  1335.             else
  1336.               luaX:syntaxerror(ls, string.format(
  1337.                 self.LUA_QS.." expected (to close "..self.LUA_QS.." at line %d)",
  1338.                 luaX:token2str(ls, what), luaX:token2str(ls, who), where))
  1339.             end
  1340.           end
  1341.         end
  1342.        
  1343.         ------------------------------------------------------------------------
  1344.         -- expect that token is a name, return the name
  1345.         ------------------------------------------------------------------------
  1346.         function luaY:str_checkname(ls)
  1347.           self:check(ls, "TK_NAME")
  1348.           local ts = ls.t.seminfo
  1349.           luaX:next(ls)
  1350.           return ts
  1351.         end
  1352.        
  1353.         ------------------------------------------------------------------------
  1354.         -- initialize a struct expdesc, expression description data structure
  1355.         ------------------------------------------------------------------------
  1356.         function luaY:init_exp(e, k, i)
  1357.           e.f, e.t = luaK.NO_JUMP, luaK.NO_JUMP
  1358.           e.k = k
  1359.           e.info = i
  1360.         end
  1361.        
  1362.         ------------------------------------------------------------------------
  1363.         -- adds given string s in string pool, sets e as VK
  1364.         ------------------------------------------------------------------------
  1365.         function luaY:codestring(ls, e, s)
  1366.           self:init_exp(e, "VK", luaK:stringK(ls.fs, s))
  1367.         end
  1368.        
  1369.         ------------------------------------------------------------------------
  1370.         -- consume a name token, adds it to string pool, sets e as VK
  1371.         ------------------------------------------------------------------------
  1372.         function luaY:checkname(ls, e)
  1373.           self:codestring(ls, e, self:str_checkname(ls))
  1374.         end
  1375.        
  1376.         ------------------------------------------------------------------------
  1377.         -- creates struct entry for a local variable
  1378.         -- * used only in new_localvar()
  1379.         ------------------------------------------------------------------------
  1380.         function luaY:registerlocalvar(ls, varname)
  1381.           local fs = ls.fs
  1382.           local f = fs.f
  1383.           self:growvector(ls.L, f.locvars, fs.nlocvars, f.sizelocvars,
  1384.                           nil, self.SHRT_MAX, "too many local variables")
  1385.           -- loop to initialize empty f.locvar positions not required
  1386.           f.locvars[fs.nlocvars] = {} -- LocVar
  1387.           f.locvars[fs.nlocvars].varname = varname
  1388.           -- luaC_objbarrier(ls.L, f, varname) /* GC */
  1389.           local nlocvars = fs.nlocvars
  1390.           fs.nlocvars = fs.nlocvars + 1
  1391.           return nlocvars
  1392.         end
  1393.        
  1394.         ------------------------------------------------------------------------
  1395.         -- creates a new local variable given a name and an offset from nactvar
  1396.         -- * used in fornum(), forlist(), parlist(), body()
  1397.         ------------------------------------------------------------------------
  1398.         function luaY:new_localvarliteral(ls, v, n)
  1399.           self:new_localvar(ls, v, n)
  1400.         end
  1401.        
  1402.         ------------------------------------------------------------------------
  1403.         -- register a local variable, set in active variable list
  1404.         ------------------------------------------------------------------------
  1405.         function luaY:new_localvar(ls, name, n)
  1406.           local fs = ls.fs
  1407.           self:checklimit(fs, fs.nactvar + n + 1, self.LUAI_MAXVARS, "local variables")
  1408.           fs.actvar[fs.nactvar + n] = self:registerlocalvar(ls, name)
  1409.         end
  1410.        
  1411.         ------------------------------------------------------------------------
  1412.         -- adds nvars number of new local variables, set debug information
  1413.         ------------------------------------------------------------------------
  1414.         function luaY:adjustlocalvars(ls, nvars)
  1415.           local fs = ls.fs
  1416.           fs.nactvar = fs.nactvar + nvars
  1417.           for i = nvars, 1, -1 do
  1418.             self:getlocvar(fs, fs.nactvar - i).startpc = fs.pc
  1419.           end
  1420.         end
  1421.        
  1422.         ------------------------------------------------------------------------
  1423.         -- removes a number of locals, set debug information
  1424.         ------------------------------------------------------------------------
  1425.         function luaY:removevars(ls, tolevel)
  1426.           local fs = ls.fs
  1427.           while fs.nactvar > tolevel do
  1428.             fs.nactvar = fs.nactvar - 1
  1429.             self:getlocvar(fs, fs.nactvar).endpc = fs.pc
  1430.           end
  1431.         end
  1432.        
  1433.         ------------------------------------------------------------------------
  1434.         -- returns an existing upvalue index based on the given name, or
  1435.         -- creates a new upvalue struct entry and returns the new index
  1436.         -- * used only in singlevaraux()
  1437.         ------------------------------------------------------------------------
  1438.         function luaY:indexupvalue(fs, name, v)
  1439.           local f = fs.f
  1440.           for i = 0, f.nups - 1 do
  1441.             if fs.upvalues[i].k == v.k and fs.upvalues[i].info == v.info then
  1442.               assert(f.upvalues[i] == name)
  1443.               return i
  1444.             end
  1445.           end
  1446.           -- new one
  1447.           self:checklimit(fs, f.nups + 1, self.LUAI_MAXUPVALUES, "upvalues")
  1448.           self:growvector(fs.L, f.upvalues, f.nups, f.sizeupvalues,
  1449.                           nil, self.MAX_INT, "")
  1450.           -- loop to initialize empty f.upvalues positions not required
  1451.           f.upvalues[f.nups] = name
  1452.           -- luaC_objbarrier(fs->L, f, name); /* GC */
  1453.           assert(v.k == "VLOCAL" or v.k == "VUPVAL")
  1454.           -- this is a partial copy; only k & info fields used
  1455.           fs.upvalues[f.nups] = { k = v.k, info = v.info }
  1456.           local nups = f.nups
  1457.           f.nups = f.nups + 1
  1458.           return nups
  1459.         end
  1460.        
  1461.         ------------------------------------------------------------------------
  1462.         -- search the local variable namespace of the given fs for a match
  1463.         -- * used only in singlevaraux()
  1464.         ------------------------------------------------------------------------
  1465.         function luaY:searchvar(fs, n)
  1466.           for i = fs.nactvar - 1, 0, -1 do
  1467.             if n == self:getlocvar(fs, i).varname then
  1468.               return i
  1469.             end
  1470.           end
  1471.           return -1  -- not found
  1472.         end
  1473.        
  1474.         ------------------------------------------------------------------------
  1475.         -- * mark upvalue flags in function states up to a given level
  1476.         -- * used only in singlevaraux()
  1477.         ------------------------------------------------------------------------
  1478.         function luaY:markupval(fs, level)
  1479.           local bl = fs.bl
  1480.           while bl and bl.nactvar > level do bl = bl.previous end
  1481.           if bl then bl.upval = true end
  1482.         end
  1483.        
  1484.         ------------------------------------------------------------------------
  1485.         -- handle locals, globals and upvalues and related processing
  1486.         -- * search mechanism is recursive, calls itself to search parents
  1487.         -- * used only in singlevar()
  1488.         ------------------------------------------------------------------------
  1489.         function luaY:singlevaraux(fs, n, var, base)
  1490.           if fs == nil then  -- no more levels?
  1491.             self:init_exp(var, "VGLOBAL", luaP.NO_REG)  -- default is global variable
  1492.             return "VGLOBAL"
  1493.           else
  1494.             local v = self:searchvar(fs, n)  -- look up at current level
  1495.             if v >= 0 then
  1496.               self:init_exp(var, "VLOCAL", v)
  1497.               if base == 0 then
  1498.                 self:markupval(fs, v)  -- local will be used as an upval
  1499.               end
  1500.               return "VLOCAL"
  1501.             else  -- not found at current level; try upper one
  1502.               if self:singlevaraux(fs.prev, n, var, 0) == "VGLOBAL" then
  1503.                 return "VGLOBAL"
  1504.               end
  1505.               var.info = self:indexupvalue(fs, n, var)  -- else was LOCAL or UPVAL
  1506.               var.k = "VUPVAL"  -- upvalue in this level
  1507.               return "VUPVAL"
  1508.             end--if v
  1509.           end--if fs
  1510.         end
  1511.        
  1512.         ------------------------------------------------------------------------
  1513.         -- consume a name token, creates a variable (global|local|upvalue)
  1514.         -- * used in prefixexp(), funcname()
  1515.         ------------------------------------------------------------------------
  1516.         function luaY:singlevar(ls, var)
  1517.           local varname = self:str_checkname(ls)
  1518.           local fs = ls.fs
  1519.           if self:singlevaraux(fs, varname, var, 1) == "VGLOBAL" then
  1520.             var.info = luaK:stringK(fs, varname)  -- info points to global name
  1521.           end
  1522.         end
  1523.        
  1524.         ------------------------------------------------------------------------
  1525.         -- adjust RHS to match LHS in an assignment
  1526.         -- * used in assignment(), forlist(), localstat()
  1527.         ------------------------------------------------------------------------
  1528.         function luaY:adjust_assign(ls, nvars, nexps, e)
  1529.           local fs = ls.fs
  1530.           local extra = nvars - nexps
  1531.           if self:hasmultret(e.k) then
  1532.             extra = extra + 1  -- includes call itself
  1533.             if extra <= 0 then extra = 0 end
  1534.             luaK:setreturns(fs, e, extra)  -- last exp. provides the difference
  1535.             if extra > 1 then luaK:reserveregs(fs, extra - 1) end
  1536.           else
  1537.             if e.k ~= "VVOID" then luaK:exp2nextreg(fs, e) end  -- close last expression
  1538.             if extra > 0 then
  1539.               local reg = fs.freereg
  1540.               luaK:reserveregs(fs, extra)
  1541.               luaK:_nil(fs, reg, extra)
  1542.             end
  1543.           end
  1544.         end
  1545.        
  1546.         ------------------------------------------------------------------------
  1547.         -- tracks and limits parsing depth, assert check at end of parsing
  1548.         ------------------------------------------------------------------------
  1549.         function luaY:enterlevel(ls)
  1550.           ls.L.nCcalls = ls.L.nCcalls + 1
  1551.           if ls.L.nCcalls > self.LUAI_MAXCCALLS then
  1552.             luaX:lexerror(ls, "chunk has too many syntax levels", 0)
  1553.           end
  1554.         end
  1555.        
  1556.         ------------------------------------------------------------------------
  1557.         -- tracks parsing depth, a pair with luaY:enterlevel()
  1558.         ------------------------------------------------------------------------
  1559.         function luaY:leavelevel(ls)
  1560.           ls.L.nCcalls = ls.L.nCcalls - 1
  1561.         end
  1562.        
  1563.         ------------------------------------------------------------------------
  1564.         -- enters a code unit, initializes elements
  1565.         ------------------------------------------------------------------------
  1566.         function luaY:enterblock(fs, bl, isbreakable)
  1567.           bl.breaklist = luaK.NO_JUMP
  1568.           bl.isbreakable = isbreakable
  1569.           bl.nactvar = fs.nactvar
  1570.           bl.upval = false
  1571.           bl.previous = fs.bl
  1572.           fs.bl = bl
  1573.           assert(fs.freereg == fs.nactvar)
  1574.         end
  1575.        
  1576.         ------------------------------------------------------------------------
  1577.         -- leaves a code unit, close any upvalues
  1578.         ------------------------------------------------------------------------
  1579.         function luaY:leaveblock(fs)
  1580.           local bl = fs.bl
  1581.           fs.bl = bl.previous
  1582.           self:removevars(fs.ls, bl.nactvar)
  1583.           if bl.upval then
  1584.             luaK:codeABC(fs, "OP_CLOSE", bl.nactvar, 0, 0)
  1585.           end
  1586.           -- a block either controls scope or breaks (never both)
  1587.           assert(not bl.isbreakable or not bl.upval)
  1588.           assert(bl.nactvar == fs.nactvar)
  1589.           fs.freereg = fs.nactvar  -- free registers
  1590.           luaK:patchtohere(fs, bl.breaklist)
  1591.         end
  1592.        
  1593.         ------------------------------------------------------------------------
  1594.         -- implement the instantiation of a function prototype, append list of
  1595.         -- upvalues after the instantiation instruction
  1596.         -- * used only in body()
  1597.         ------------------------------------------------------------------------
  1598.         function luaY:pushclosure(ls, func, v)
  1599.           local fs = ls.fs
  1600.           local f = fs.f
  1601.           self:growvector(ls.L, f.p, fs.np, f.sizep, nil,
  1602.                           luaP.MAXARG_Bx, "constant table overflow")
  1603.           -- loop to initialize empty f.p positions not required
  1604.           f.p[fs.np] = func.f
  1605.           fs.np = fs.np + 1
  1606.           -- luaC_objbarrier(ls->L, f, func->f); /* C */
  1607.           self:init_exp(v, "VRELOCABLE", luaK:codeABx(fs, "OP_CLOSURE", 0, fs.np - 1))
  1608.           for i = 0, func.f.nups - 1 do
  1609.             local o = (func.upvalues[i].k == "VLOCAL") and "OP_MOVE" or "OP_GETUPVAL"
  1610.             luaK:codeABC(fs, o, 0, func.upvalues[i].info, 0)
  1611.           end
  1612.         end
  1613.        
  1614.         ------------------------------------------------------------------------
  1615.         -- opening of a function
  1616.         ------------------------------------------------------------------------
  1617.         function luaY:open_func(ls, fs)
  1618.           local L = ls.L
  1619.           local f = self:newproto(ls.L)
  1620.           fs.f = f
  1621.           fs.prev = ls.fs  -- linked list of funcstates
  1622.           fs.ls = ls
  1623.           fs.L = L
  1624.           ls.fs = fs
  1625.           fs.pc = 0
  1626.           fs.lasttarget = -1
  1627.           fs.jpc = luaK.NO_JUMP
  1628.           fs.freereg = 0
  1629.           fs.nk = 0
  1630.           fs.np = 0
  1631.           fs.nlocvars = 0
  1632.           fs.nactvar = 0
  1633.           fs.bl = nil
  1634.           f.source = ls.source
  1635.           f.maxstacksize = 2  -- registers 0/1 are always valid
  1636.           fs.h = {}  -- constant table; was luaH_new call
  1637.           -- anchor table of constants and prototype (to avoid being collected)
  1638.           -- sethvalue2s(L, L->top, fs->h); incr_top(L); /* C */
  1639.           -- setptvalue2s(L, L->top, f); incr_top(L);
  1640.         end
  1641.        
  1642.         ------------------------------------------------------------------------
  1643.         -- closing of a function
  1644.         ------------------------------------------------------------------------
  1645.         function luaY:close_func(ls)
  1646.           local L = ls.L
  1647.           local fs = ls.fs
  1648.           local f = fs.f
  1649.           self:removevars(ls, 0)
  1650.           luaK:ret(fs, 0, 0)  -- final return
  1651.           -- luaM_reallocvector deleted for f->code, f->lineinfo, f->k, f->p,
  1652.           -- f->locvars, f->upvalues; not required for Lua table arrays
  1653.           f.sizecode = fs.pc
  1654.           f.sizelineinfo = fs.pc
  1655.           f.sizek = fs.nk
  1656.           f.sizep = fs.np
  1657.           f.sizelocvars = fs.nlocvars
  1658.           f.sizeupvalues = f.nups
  1659.           --assert(luaG_checkcode(f))  -- currently not implemented
  1660.           assert(fs.bl == nil)
  1661.           ls.fs = fs.prev
  1662.           -- the following is not required for this implementation; kept here
  1663.           -- for completeness
  1664.           -- L->top -= 2;  /* remove table and prototype from the stack */
  1665.           -- last token read was anchored in defunct function; must reanchor it
  1666.           if fs then self:anchor_token(ls) end
  1667.         end
  1668.        
  1669.         ------------------------------------------------------------------------
  1670.         -- parser initialization function
  1671.         -- * note additional sub-tables needed for LexState, FuncState
  1672.         ------------------------------------------------------------------------
  1673.         function luaY:parser(L, z, buff, name)
  1674.           local lexstate = {}  -- LexState
  1675.                 lexstate.t = {}
  1676.                 lexstate.lookahead = {}
  1677.           local funcstate = {}  -- FuncState
  1678.                 funcstate.upvalues = {}
  1679.                 funcstate.actvar = {}
  1680.           -- the following nCcalls initialization added for convenience
  1681.           L.nCcalls = 0
  1682.           lexstate.buff = buff
  1683.           luaX:setinput(L, lexstate, z, name)
  1684.           self:open_func(lexstate, funcstate)
  1685.           funcstate.f.is_vararg = self.VARARG_ISVARARG  -- main func. is always vararg
  1686.           luaX:next(lexstate)  -- read first token
  1687.           self:chunk(lexstate)
  1688.           self:check(lexstate, "TK_EOS")
  1689.           self:close_func(lexstate)
  1690.           assert(funcstate.prev == nil)
  1691.           assert(funcstate.f.nups == 0)
  1692.           assert(lexstate.fs == nil)
  1693.           return funcstate.f
  1694.         end
  1695.        
  1696.         --[[--------------------------------------------------------------------
  1697.         -- GRAMMAR RULES
  1698.         ----------------------------------------------------------------------]]
  1699.        
  1700.         ------------------------------------------------------------------------
  1701.         -- parse a function name suffix, for function call specifications
  1702.         -- * used in primaryexp(), funcname()
  1703.         ------------------------------------------------------------------------
  1704.         function luaY:field(ls, v)
  1705.           -- field -> ['.' | ':'] NAME
  1706.           local fs = ls.fs
  1707.           local key = {}  -- expdesc
  1708.           luaK:exp2anyreg(fs, v)
  1709.           luaX:next(ls)  -- skip the dot or colon
  1710.           self:checkname(ls, key)
  1711.           luaK:indexed(fs, v, key)
  1712.         end
  1713.        
  1714.         ------------------------------------------------------------------------
  1715.         -- parse a table indexing suffix, for constructors, expressions
  1716.         -- * used in recfield(), primaryexp()
  1717.         ------------------------------------------------------------------------
  1718.         function luaY:yindex(ls, v)
  1719.           -- index -> '[' expr ']'
  1720.           luaX:next(ls)  -- skip the '['
  1721.           self:expr(ls, v)
  1722.           luaK:exp2val(ls.fs, v)
  1723.           self:checknext(ls, "]")
  1724.         end
  1725.        
  1726.         --[[--------------------------------------------------------------------
  1727.         -- Rules for Constructors
  1728.         ----------------------------------------------------------------------]]
  1729.        
  1730.         --[[--------------------------------------------------------------------
  1731.         -- struct ConsControl:
  1732.         --   v  -- last list item read (table: struct expdesc)
  1733.         --   t  -- table descriptor (table: struct expdesc)
  1734.         --   nh  -- total number of 'record' elements
  1735.         --   na  -- total number of array elements
  1736.         --   tostore  -- number of array elements pending to be stored
  1737.         ----------------------------------------------------------------------]]
  1738.        
  1739.         ------------------------------------------------------------------------
  1740.         -- parse a table record (hash) field
  1741.         -- * used in constructor()
  1742.         ------------------------------------------------------------------------
  1743.         function luaY:recfield(ls, cc)
  1744.           -- recfield -> (NAME | '['exp1']') = exp1
  1745.           local fs = ls.fs
  1746.           local reg = ls.fs.freereg
  1747.           local key, val = {}, {}  -- expdesc
  1748.           if ls.t.token == "TK_NAME" then
  1749.             self:checklimit(fs, cc.nh, self.MAX_INT, "items in a constructor")
  1750.             self:checkname(ls, key)
  1751.           else  -- ls->t.token == '['
  1752.             self:yindex(ls, key)
  1753.           end
  1754.           cc.nh = cc.nh + 1
  1755.           self:checknext(ls, "=")
  1756.           local rkkey = luaK:exp2RK(fs, key)
  1757.           self:expr(ls, val)
  1758.           luaK:codeABC(fs, "OP_SETTABLE", cc.t.info, rkkey, luaK:exp2RK(fs, val))
  1759.           fs.freereg = reg  -- free registers
  1760.         end
  1761.        
  1762.         ------------------------------------------------------------------------
  1763.         -- emit a set list instruction if enough elements (LFIELDS_PER_FLUSH)
  1764.         -- * used in constructor()
  1765.         ------------------------------------------------------------------------
  1766.         function luaY:closelistfield(fs, cc)
  1767.           if cc.v.k == "VVOID" then return end  -- there is no list item
  1768.           luaK:exp2nextreg(fs, cc.v)
  1769.           cc.v.k = "VVOID"
  1770.           if cc.tostore == luaP.LFIELDS_PER_FLUSH then
  1771.             luaK:setlist(fs, cc.t.info, cc.na, cc.tostore)  -- flush
  1772.             cc.tostore = 0  -- no more items pending
  1773.           end
  1774.         end
  1775.        
  1776.         ------------------------------------------------------------------------
  1777.         -- emit a set list instruction at the end of parsing list constructor
  1778.         -- * used in constructor()
  1779.         ------------------------------------------------------------------------
  1780.         function luaY:lastlistfield(fs, cc)
  1781.           if cc.tostore == 0 then return end
  1782.           if self:hasmultret(cc.v.k) then
  1783.             luaK:setmultret(fs, cc.v)
  1784.             luaK:setlist(fs, cc.t.info, cc.na, self.LUA_MULTRET)
  1785.             cc.na = cc.na - 1  -- do not count last expression (unknown number of elements)
  1786.           else
  1787.             if cc.v.k ~= "VVOID" then
  1788.               luaK:exp2nextreg(fs, cc.v)
  1789.             end
  1790.             luaK:setlist(fs, cc.t.info, cc.na, cc.tostore)
  1791.           end
  1792.         end
  1793.        
  1794.         ------------------------------------------------------------------------
  1795.         -- parse a table list (array) field
  1796.         -- * used in constructor()
  1797.         ------------------------------------------------------------------------
  1798.         function luaY:listfield(ls, cc)
  1799.           self:expr(ls, cc.v)
  1800.           self:checklimit(ls.fs, cc.na, self.MAX_INT, "items in a constructor")
  1801.           cc.na = cc.na + 1
  1802.           cc.tostore = cc.tostore + 1
  1803.         end
  1804.        
  1805.         ------------------------------------------------------------------------
  1806.         -- parse a table constructor
  1807.         -- * used in funcargs(), simpleexp()
  1808.         ------------------------------------------------------------------------
  1809.         function luaY:constructor(ls, t)
  1810.           -- constructor -> '{' [ field { fieldsep field } [ fieldsep ] ] '}'
  1811.           -- field -> recfield | listfield
  1812.           -- fieldsep -> ',' | ';'
  1813.           local fs = ls.fs
  1814.           local line = ls.linenumber
  1815.           local pc = luaK:codeABC(fs, "OP_NEWTABLE", 0, 0, 0)
  1816.           local cc = {}  -- ConsControl
  1817.                 cc.v = {}
  1818.           cc.na, cc.nh, cc.tostore = 0, 0, 0
  1819.           cc.t = t
  1820.           self:init_exp(t, "VRELOCABLE", pc)
  1821.           self:init_exp(cc.v, "VVOID", 0)  -- no value (yet)
  1822.           luaK:exp2nextreg(ls.fs, t)  -- fix it at stack top (for gc)
  1823.           self:checknext(ls, "{")
  1824.           repeat
  1825.             assert(cc.v.k == "VVOID" or cc.tostore > 0)
  1826.             if ls.t.token == "}" then break end
  1827.             self:closelistfield(fs, cc)
  1828.             local c = ls.t.token
  1829.        
  1830.             if c == "TK_NAME" then  -- may be listfields or recfields
  1831.               luaX:lookahead(ls)
  1832.               if ls.lookahead.token ~= "=" then  -- expression?
  1833.                 self:listfield(ls, cc)
  1834.               else
  1835.                 self:recfield(ls, cc)
  1836.               end
  1837.             elseif c == "[" then  -- constructor_item -> recfield
  1838.               self:recfield(ls, cc)
  1839.             else  -- constructor_part -> listfield
  1840.               self:listfield(ls, cc)
  1841.             end
  1842.           until not self:testnext(ls, ",") and not self:testnext(ls, ";")
  1843.           self:check_match(ls, "}", "{", line)
  1844.           self:lastlistfield(fs, cc)
  1845.           luaP:SETARG_B(fs.f.code[pc], self:int2fb(cc.na)) -- set initial array size
  1846.           luaP:SETARG_C(fs.f.code[pc], self:int2fb(cc.nh)) -- set initial table size
  1847.         end
  1848.        
  1849.         -- }======================================================================
  1850.        
  1851.         ------------------------------------------------------------------------
  1852.         -- parse the arguments (parameters) of a function declaration
  1853.         -- * used in body()
  1854.         ------------------------------------------------------------------------
  1855.         function luaY:parlist(ls)
  1856.           -- parlist -> [ param { ',' param } ]
  1857.           local fs = ls.fs
  1858.           local f = fs.f
  1859.           local nparams = 0
  1860.           f.is_vararg = 0
  1861.           if ls.t.token ~= ")" then  -- is 'parlist' not empty?
  1862.             repeat
  1863.               local c = ls.t.token
  1864.               if c == "TK_NAME" then  -- param -> NAME
  1865.                 self:new_localvar(ls, self:str_checkname(ls), nparams)
  1866.                 nparams = nparams + 1
  1867.               elseif c == "TK_DOTS" then  -- param -> `...'
  1868.                 luaX:next(ls)
  1869.         -- [[
  1870.         -- #if defined(LUA_COMPAT_VARARG)
  1871.                 -- use `arg' as default name
  1872.                 self:new_localvarliteral(ls, "arg", nparams)
  1873.                 nparams = nparams + 1
  1874.                 f.is_vararg = self.VARARG_HASARG + self.VARARG_NEEDSARG
  1875.         -- #endif
  1876.         --]]
  1877.                 f.is_vararg = f.is_vararg + self.VARARG_ISVARARG
  1878.               else
  1879.                 luaX:syntaxerror(ls, "<name> or "..self:LUA_QL("...").." expected")
  1880.               end
  1881.             until f.is_vararg ~= 0 or not self:testnext(ls, ",")
  1882.           end--if
  1883.           self:adjustlocalvars(ls, nparams)
  1884.           -- NOTE: the following works only when HASARG_MASK is 2!
  1885.           f.numparams = fs.nactvar - (f.is_vararg % self.HASARG_MASK)
  1886.           luaK:reserveregs(fs, fs.nactvar)  -- reserve register for parameters
  1887.         end
  1888.        
  1889.         ------------------------------------------------------------------------
  1890.         -- parse function declaration body
  1891.         -- * used in simpleexp(), localfunc(), funcstat()
  1892.         ------------------------------------------------------------------------
  1893.         function luaY:body(ls, e, needself, line)
  1894.           -- body ->  '(' parlist ')' chunk END
  1895.           local new_fs = {}  -- FuncState
  1896.                 new_fs.upvalues = {}
  1897.                 new_fs.actvar = {}
  1898.           self:open_func(ls, new_fs)
  1899.           new_fs.f.lineDefined = line
  1900.           self:checknext(ls, "(")
  1901.           if needself then
  1902.             self:new_localvarliteral(ls, "self", 0)
  1903.             self:adjustlocalvars(ls, 1)
  1904.           end
  1905.           self:parlist(ls)
  1906.           self:checknext(ls, ")")
  1907.           self:chunk(ls)
  1908.           new_fs.f.lastlinedefined = ls.linenumber
  1909.           self:check_match(ls, "TK_END", "TK_FUNCTION", line)
  1910.           self:close_func(ls)
  1911.           self:pushclosure(ls, new_fs, e)
  1912.         end
  1913.        
  1914.         ------------------------------------------------------------------------
  1915.         -- parse a list of comma-separated expressions
  1916.         -- * used is multiple locations
  1917.         ------------------------------------------------------------------------
  1918.         function luaY:explist1(ls, v)
  1919.           -- explist1 -> expr { ',' expr }
  1920.           local n = 1  -- at least one expression
  1921.           self:expr(ls, v)
  1922.           while self:testnext(ls, ",") do
  1923.             luaK:exp2nextreg(ls.fs, v)
  1924.             self:expr(ls, v)
  1925.             n = n + 1
  1926.           end
  1927.           return n
  1928.         end
  1929.        
  1930.         ------------------------------------------------------------------------
  1931.         -- parse the parameters of a function call
  1932.         -- * contrast with parlist(), used in function declarations
  1933.         -- * used in primaryexp()
  1934.         ------------------------------------------------------------------------
  1935.         function luaY:funcargs(ls, f)
  1936.           local fs = ls.fs
  1937.           local args = {}  -- expdesc
  1938.           local nparams
  1939.           local line = ls.linenumber
  1940.           local c = ls.t.token
  1941.           if c == "(" then  -- funcargs -> '(' [ explist1 ] ')'
  1942.             if line ~= ls.lastline then
  1943.               luaX:syntaxerror(ls, "ambiguous syntax (function call x new statement)")
  1944.             end
  1945.             luaX:next(ls)
  1946.             if ls.t.token == ")" then  -- arg list is empty?
  1947.               args.k = "VVOID"
  1948.             else
  1949.               self:explist1(ls, args)
  1950.               luaK:setmultret(fs, args)
  1951.             end
  1952.             self:check_match(ls, ")", "(", line)
  1953.           elseif c == "{" then  -- funcargs -> constructor
  1954.             self:constructor(ls, args)
  1955.           elseif c == "TK_STRING" then  -- funcargs -> STRING
  1956.             self:codestring(ls, args, ls.t.seminfo)
  1957.             luaX:next(ls)  -- must use 'seminfo' before 'next'
  1958.           else
  1959.             luaX:syntaxerror(ls, "function arguments expected")
  1960.             return
  1961.           end
  1962.           assert(f.k == "VNONRELOC")
  1963.           local base = f.info  -- base register for call
  1964.           if self:hasmultret(args.k) then
  1965.             nparams = self.LUA_MULTRET  -- open call
  1966.           else
  1967.             if args.k ~= "VVOID" then
  1968.               luaK:exp2nextreg(fs, args)  -- close last argument
  1969.             end
  1970.             nparams = fs.freereg - (base + 1)
  1971.           end
  1972.           self:init_exp(f, "VCALL", luaK:codeABC(fs, "OP_CALL", base, nparams + 1, 2))
  1973.           luaK:fixline(fs, line)
  1974.           fs.freereg = base + 1  -- call remove function and arguments and leaves
  1975.                                  -- (unless changed) one result
  1976.         end
  1977.        
  1978.         --[[--------------------------------------------------------------------
  1979.         -- Expression parsing
  1980.         ----------------------------------------------------------------------]]
  1981.        
  1982.         ------------------------------------------------------------------------
  1983.         -- parses an expression in parentheses or a single variable
  1984.         -- * used in primaryexp()
  1985.         ------------------------------------------------------------------------
  1986.         function luaY:prefixexp(ls, v)
  1987.           -- prefixexp -> NAME | '(' expr ')'
  1988.           local c = ls.t.token
  1989.           if c == "(" then
  1990.             local line = ls.linenumber
  1991.             luaX:next(ls)
  1992.             self:expr(ls, v)
  1993.             self:check_match(ls, ")", "(", line)
  1994.             luaK:dischargevars(ls.fs, v)
  1995.           elseif c == "TK_NAME" then
  1996.             self:singlevar(ls, v)
  1997.           else
  1998.             luaX:syntaxerror(ls, "unexpected symbol")
  1999.           end--if c
  2000.           return
  2001.         end
  2002.        
  2003.         ------------------------------------------------------------------------
  2004.         -- parses a prefixexp (an expression in parentheses or a single variable)
  2005.         -- or a function call specification
  2006.         -- * used in simpleexp(), assignment(), exprstat()
  2007.         ------------------------------------------------------------------------
  2008.         function luaY:primaryexp(ls, v)
  2009.           -- primaryexp ->
  2010.           --    prefixexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs }
  2011.           local fs = ls.fs
  2012.           self:prefixexp(ls, v)
  2013.           while true do
  2014.             local c = ls.t.token
  2015.             if c == "." then  -- field
  2016.               self:field(ls, v)
  2017.             elseif c == "[" then  -- '[' exp1 ']'
  2018.               local key = {}  -- expdesc
  2019.               luaK:exp2anyreg(fs, v)
  2020.               self:yindex(ls, key)
  2021.               luaK:indexed(fs, v, key)
  2022.             elseif c == ":" then  -- ':' NAME funcargs
  2023.               local key = {}  -- expdesc
  2024.               luaX:next(ls)
  2025.               self:checkname(ls, key)
  2026.               luaK:_self(fs, v, key)
  2027.               self:funcargs(ls, v)
  2028.             elseif c == "(" or c == "TK_STRING" or c == "{" then  -- funcargs
  2029.               luaK:exp2nextreg(fs, v)
  2030.               self:funcargs(ls, v)
  2031.             else
  2032.               return
  2033.             end--if c
  2034.           end--while
  2035.         end
  2036.        
  2037.         ------------------------------------------------------------------------
  2038.         -- parses general expression types, constants handled here
  2039.         -- * used in subexpr()
  2040.         ------------------------------------------------------------------------
  2041.         function luaY:simpleexp(ls, v)
  2042.           -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... |
  2043.           --              constructor | FUNCTION body | primaryexp
  2044.           local c = ls.t.token
  2045.           if c == "TK_NUMBER" then
  2046.             self:init_exp(v, "VKNUM", 0)
  2047.             v.nval = ls.t.seminfo
  2048.           elseif c == "TK_STRING" then
  2049.             self:codestring(ls, v, ls.t.seminfo)
  2050.           elseif c == "TK_NIL" then
  2051.             self:init_exp(v, "VNIL", 0)
  2052.           elseif c == "TK_TRUE" then
  2053.             self:init_exp(v, "VTRUE", 0)
  2054.           elseif c == "TK_FALSE" then
  2055.             self:init_exp(v, "VFALSE", 0)
  2056.           elseif c == "TK_DOTS" then  -- vararg
  2057.             local fs = ls.fs
  2058.             self:check_condition(ls, fs.f.is_vararg ~= 0,
  2059.                             "cannot use "..self:LUA_QL("...").." outside a vararg function");
  2060.             -- NOTE: the following substitutes for a bitop, but is value-specific
  2061.             local is_vararg = fs.f.is_vararg
  2062.             if is_vararg >= self.VARARG_NEEDSARG then
  2063.               fs.f.is_vararg = is_vararg - self.VARARG_NEEDSARG  -- don't need 'arg'
  2064.             end
  2065.             self:init_exp(v, "VVARARG", luaK:codeABC(fs, "OP_VARARG", 0, 1, 0))
  2066.           elseif c == "{" then  -- constructor
  2067.             self:constructor(ls, v)
  2068.             return
  2069.           elseif c == "TK_FUNCTION" then
  2070.             luaX:next(ls)
  2071.             self:body(ls, v, false, ls.linenumber)
  2072.             return
  2073.           else
  2074.             self:primaryexp(ls, v)
  2075.             return
  2076.           end--if c
  2077.           luaX:next(ls)
  2078.         end
  2079.        
  2080.         ------------------------------------------------------------------------
  2081.         -- Translates unary operators tokens if found, otherwise returns
  2082.         -- OPR_NOUNOPR. getunopr() and getbinopr() are used in subexpr().
  2083.         -- * used in subexpr()
  2084.         ------------------------------------------------------------------------
  2085.         function luaY:getunopr(op)
  2086.           if op == "TK_NOT" then
  2087.             return "OPR_NOT"
  2088.           elseif op == "-" then
  2089.             return "OPR_MINUS"
  2090.           elseif op == "#" then
  2091.             return "OPR_LEN"
  2092.           else
  2093.             return "OPR_NOUNOPR"
  2094.           end
  2095.         end
  2096.        
  2097.         ------------------------------------------------------------------------
  2098.         -- Translates binary operator tokens if found, otherwise returns
  2099.         -- OPR_NOBINOPR. Code generation uses OPR_* style tokens.
  2100.         -- * used in subexpr()
  2101.         ------------------------------------------------------------------------
  2102.         luaY.getbinopr_table = {
  2103.           ["+"] = "OPR_ADD",
  2104.           ["-"] = "OPR_SUB",
  2105.           ["*"] = "OPR_MUL",
  2106.           ["/"] = "OPR_DIV",
  2107.           ["%"] = "OPR_MOD",
  2108.           ["^"] = "OPR_POW",
  2109.           ["TK_CONCAT"] = "OPR_CONCAT",
  2110.           ["TK_NE"] = "OPR_NE",
  2111.           ["TK_EQ"] = "OPR_EQ",
  2112.           ["<"] = "OPR_LT",
  2113.           ["TK_LE"] = "OPR_LE",
  2114.           [">"] = "OPR_GT",
  2115.           ["TK_GE"] = "OPR_GE",
  2116.           ["TK_AND"] = "OPR_AND",
  2117.           ["TK_OR"] = "OPR_OR",
  2118.         }
  2119.         function luaY:getbinopr(op)
  2120.           local opr = self.getbinopr_table[op]
  2121.           if opr then return opr else return "OPR_NOBINOPR" end
  2122.         end
  2123.        
  2124.         ------------------------------------------------------------------------
  2125.         -- the following priority table consists of pairs of left/right values
  2126.         -- for binary operators (was a static const struct); grep for ORDER OPR
  2127.         -- * the following struct is replaced:
  2128.         --   static const struct {
  2129.         --     lu_byte left;  /* left priority for each binary operator */
  2130.         --     lu_byte right; /* right priority */
  2131.         --   } priority[] = {  /* ORDER OPR */
  2132.         ------------------------------------------------------------------------
  2133.         luaY.priority = {
  2134.           {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, -- `+' `-' `/' `%'
  2135.           {10, 9}, {5, 4},                 -- power and concat (right associative)
  2136.           {3, 3}, {3, 3},                  -- equality
  2137.           {3, 3}, {3, 3}, {3, 3}, {3, 3},  -- order
  2138.           {2, 2}, {1, 1}                   -- logical (and/or)
  2139.         }
  2140.        
  2141.         luaY.UNARY_PRIORITY = 8  -- priority for unary operators
  2142.        
  2143.         ------------------------------------------------------------------------
  2144.         -- Parse subexpressions. Includes handling of unary operators and binary
  2145.         -- operators. A subexpr is given the rhs priority level of the operator
  2146.         -- immediately left of it, if any (limit is -1 if none,) and if a binop
  2147.         -- is found, limit is compared with the lhs priority level of the binop
  2148.         -- in order to determine which executes first.
  2149.         ------------------------------------------------------------------------
  2150.        
  2151.         ------------------------------------------------------------------------
  2152.         -- subexpr -> (simpleexp | unop subexpr) { binop subexpr }
  2153.         -- where 'binop' is any binary operator with a priority higher than 'limit'
  2154.         -- * for priority lookups with self.priority[], 1=left and 2=right
  2155.         -- * recursively called
  2156.         -- * used in expr()
  2157.         ------------------------------------------------------------------------
  2158.         function luaY:subexpr(ls, v, limit)
  2159.           self:enterlevel(ls)
  2160.           local uop = self:getunopr(ls.t.token)
  2161.           if uop ~= "OPR_NOUNOPR" then
  2162.             luaX:next(ls)
  2163.             self:subexpr(ls, v, self.UNARY_PRIORITY)
  2164.             luaK:prefix(ls.fs, uop, v)
  2165.           else
  2166.             self:simpleexp(ls, v)
  2167.           end
  2168.           -- expand while operators have priorities higher than 'limit'
  2169.           local op = self:getbinopr(ls.t.token)
  2170.           while op ~= "OPR_NOBINOPR" and self.priority[luaK.BinOpr[op] + 1][1] > limit do
  2171.             local v2 = {}  -- expdesc
  2172.             luaX:next(ls)
  2173.             luaK:infix(ls.fs, op, v)
  2174.             -- read sub-expression with higher priority
  2175.             local nextop = self:subexpr(ls, v2, self.priority[luaK.BinOpr[op] + 1][2])
  2176.             luaK:posfix(ls.fs, op, v, v2)
  2177.             op = nextop
  2178.           end
  2179.           self:leavelevel(ls)
  2180.           return op  -- return first untreated operator
  2181.         end
  2182.        
  2183.         ------------------------------------------------------------------------
  2184.         -- Expression parsing starts here. Function subexpr is entered with the
  2185.         -- left operator (which is non-existent) priority of -1, which is lower
  2186.         -- than all actual operators. Expr information is returned in parm v.
  2187.         -- * used in multiple locations
  2188.         ------------------------------------------------------------------------
  2189.         function luaY:expr(ls, v)
  2190.           self:subexpr(ls, v, 0)
  2191.         end
  2192.        
  2193.         -- }====================================================================
  2194.        
  2195.         --[[--------------------------------------------------------------------
  2196.         -- Rules for Statements
  2197.         ----------------------------------------------------------------------]]
  2198.        
  2199.         ------------------------------------------------------------------------
  2200.         -- checks next token, used as a look-ahead
  2201.         -- * returns boolean instead of 0|1
  2202.         -- * used in retstat(), chunk()
  2203.         ------------------------------------------------------------------------
  2204.         function luaY:block_follow(token)
  2205.           if token == "TK_ELSE" or token == "TK_ELSEIF" or token == "TK_END"
  2206.              or token == "TK_UNTIL" or token == "TK_EOS" then
  2207.             return true
  2208.           else
  2209.             return false
  2210.           end
  2211.         end
  2212.        
  2213.         ------------------------------------------------------------------------
  2214.         -- parse a code block or unit
  2215.         -- * used in multiple functions
  2216.         ------------------------------------------------------------------------
  2217.         function luaY:block(ls)
  2218.           -- block -> chunk
  2219.           local fs = ls.fs
  2220.           local bl = {}  -- BlockCnt
  2221.           self:enterblock(fs, bl, false)
  2222.           self:chunk(ls)
  2223.           assert(bl.breaklist == luaK.NO_JUMP)
  2224.           self:leaveblock(fs)
  2225.         end
  2226.        
  2227.         ------------------------------------------------------------------------
  2228.         -- structure to chain all variables in the left-hand side of an
  2229.         -- assignment
  2230.         -- struct LHS_assign:
  2231.         --   prev  -- (table: struct LHS_assign)
  2232.         --   v  -- variable (global, local, upvalue, or indexed) (table: expdesc)
  2233.         ------------------------------------------------------------------------
  2234.        
  2235.         ------------------------------------------------------------------------
  2236.         -- check whether, in an assignment to a local variable, the local variable
  2237.         -- is needed in a previous assignment (to a table). If so, save original
  2238.         -- local value in a safe place and use this safe copy in the previous
  2239.         -- assignment.
  2240.         -- * used in assignment()
  2241.         ------------------------------------------------------------------------
  2242.         function luaY:check_conflict(ls, lh, v)
  2243.           local fs = ls.fs
  2244.           local extra = fs.freereg  -- eventual position to save local variable
  2245.           local conflict = false
  2246.           while lh do
  2247.             if lh.v.k == "VINDEXED" then
  2248.               if lh.v.info == v.info then  -- conflict?
  2249.                 conflict = true
  2250.                 lh.v.info = extra  -- previous assignment will use safe copy
  2251.               end
  2252.               if lh.v.aux == v.info then  -- conflict?
  2253.                 conflict = true
  2254.                 lh.v.aux = extra  -- previous assignment will use safe copy
  2255.               end
  2256.             end
  2257.             lh = lh.prev
  2258.           end
  2259.           if conflict then
  2260.             luaK:codeABC(fs, "OP_MOVE", fs.freereg, v.info, 0)  -- make copy
  2261.             luaK:reserveregs(fs, 1)
  2262.           end
  2263.         end
  2264.        
  2265.         ------------------------------------------------------------------------
  2266.         -- parse a variable assignment sequence
  2267.         -- * recursively called
  2268.         -- * used in exprstat()
  2269.         ------------------------------------------------------------------------
  2270.         function luaY:assignment(ls, lh, nvars)
  2271.           local e = {}  -- expdesc
  2272.           -- test was: VLOCAL <= lh->v.k && lh->v.k <= VINDEXED
  2273.           local c = lh.v.k
  2274.           self:check_condition(ls, c == "VLOCAL" or c == "VUPVAL" or c == "VGLOBAL"
  2275.                                or c == "VINDEXED", "syntax error")
  2276.           if self:testnext(ls, ",") then  -- assignment -> ',' primaryexp assignment
  2277.             local nv = {}  -- LHS_assign
  2278.                   nv.v = {}
  2279.             nv.prev = lh
  2280.             self:primaryexp(ls, nv.v)
  2281.             if nv.v.k == "VLOCAL" then
  2282.               self:check_conflict(ls, lh, nv.v)
  2283.             end
  2284.             self:checklimit(ls.fs, nvars, self.LUAI_MAXCCALLS - ls.L.nCcalls,
  2285.                             "variables in assignment")
  2286.             self:assignment(ls, nv, nvars + 1)
  2287.           else  -- assignment -> '=' explist1
  2288.             self:checknext(ls, "=")
  2289.             local nexps = self:explist1(ls, e)
  2290.             if nexps ~= nvars then
  2291.               self:adjust_assign(ls, nvars, nexps, e)
  2292.               if nexps > nvars then
  2293.                 ls.fs.freereg = ls.fs.freereg - (nexps - nvars)  -- remove extra values
  2294.               end
  2295.             else
  2296.               luaK:setoneret(ls.fs, e)  -- close last expression
  2297.               luaK:storevar(ls.fs, lh.v, e)
  2298.               return  -- avoid default
  2299.             end
  2300.           end
  2301.           self:init_exp(e, "VNONRELOC", ls.fs.freereg - 1)  -- default assignment
  2302.           luaK:storevar(ls.fs, lh.v, e)
  2303.         end
  2304.        
  2305.         ------------------------------------------------------------------------
  2306.         -- parse condition in a repeat statement or an if control structure
  2307.         -- * used in repeatstat(), test_then_block()
  2308.         ------------------------------------------------------------------------
  2309.         function luaY:cond(ls)
  2310.           -- cond -> exp
  2311.           local v = {}  -- expdesc
  2312.           self:expr(ls, v)  -- read condition
  2313.           if v.k == "VNIL" then v.k = "VFALSE" end  -- 'falses' are all equal here
  2314.           luaK:goiftrue(ls.fs, v)
  2315.           return v.f
  2316.         end
  2317.        
  2318.         ------------------------------------------------------------------------
  2319.         -- parse a break statement
  2320.         -- * used in statements()
  2321.         ------------------------------------------------------------------------
  2322.         function luaY:breakstat(ls)
  2323.           -- stat -> BREAK
  2324.           local fs = ls.fs
  2325.           local bl = fs.bl
  2326.           local upval = false
  2327.           while bl and not bl.isbreakable do
  2328.             if bl.upval then upval = true end
  2329.             bl = bl.previous
  2330.           end
  2331.           if not bl then
  2332.             luaX:syntaxerror(ls, "no loop to break")
  2333.           end
  2334.           if upval then
  2335.             luaK:codeABC(fs, "OP_CLOSE", bl.nactvar, 0, 0)
  2336.           end
  2337.           bl.breaklist = luaK:concat(fs, bl.breaklist, luaK:jump(fs))
  2338.         end
  2339.        
  2340.         ------------------------------------------------------------------------
  2341.         -- parse a while-do control structure, body processed by block()
  2342.         -- * with dynamic array sizes, MAXEXPWHILE + EXTRAEXP limits imposed by
  2343.         --   the function's implementation can be removed
  2344.         -- * used in statements()
  2345.         ------------------------------------------------------------------------
  2346.         function luaY:whilestat(ls, line)
  2347.           -- whilestat -> WHILE cond DO block END
  2348.           local fs = ls.fs
  2349.           local bl = {}  -- BlockCnt
  2350.           luaX:next(ls)  -- skip WHILE
  2351.           local whileinit = luaK:getlabel(fs)
  2352.           local condexit = self:cond(ls)
  2353.           self:enterblock(fs, bl, true)
  2354.           self:checknext(ls, "TK_DO")
  2355.           self:block(ls)
  2356.           luaK:patchlist(fs, luaK:jump(fs), whileinit)
  2357.           self:check_match(ls, "TK_END", "TK_WHILE", line)
  2358.           self:leaveblock(fs)
  2359.           luaK:patchtohere(fs, condexit)  -- false conditions finish the loop
  2360.         end
  2361.        
  2362.         ------------------------------------------------------------------------
  2363.         -- parse a repeat-until control structure, body parsed by chunk()
  2364.         -- * used in statements()
  2365.         ------------------------------------------------------------------------
  2366.         function luaY:repeatstat(ls, line)
  2367.           -- repeatstat -> REPEAT block UNTIL cond
  2368.           local fs = ls.fs
  2369.           local repeat_init = luaK:getlabel(fs)
  2370.           local bl1, bl2 = {}, {}  -- BlockCnt
  2371.           self:enterblock(fs, bl1, true)  -- loop block
  2372.           self:enterblock(fs, bl2, false)  -- scope block
  2373.           luaX:next(ls)  -- skip REPEAT
  2374.           self:chunk(ls)
  2375.           self:check_match(ls, "TK_UNTIL", "TK_REPEAT", line)
  2376.           local condexit = self:cond(ls)  -- read condition (inside scope block)
  2377.           if not bl2.upval then  -- no upvalues?
  2378.             self:leaveblock(fs)  -- finish scope
  2379.             luaK:patchlist(ls.fs, condexit, repeat_init)  -- close the loop
  2380.           else  -- complete semantics when there are upvalues
  2381.             self:breakstat(ls)  -- if condition then break
  2382.             luaK:patchtohere(ls.fs, condexit)  -- else...
  2383.             self:leaveblock(fs)  -- finish scope...
  2384.             luaK:patchlist(ls.fs, luaK:jump(fs), repeat_init)  -- and repeat
  2385.           end
  2386.           self:leaveblock(fs)  -- finish loop
  2387.         end
  2388.        
  2389.         ------------------------------------------------------------------------
  2390.         -- parse the single expressions needed in numerical for loops
  2391.         -- * used in fornum()
  2392.         ------------------------------------------------------------------------
  2393.         function luaY:exp1(ls)
  2394.           local e = {}  -- expdesc
  2395.           self:expr(ls, e)
  2396.           local k = e.k
  2397.           luaK:exp2nextreg(ls.fs, e)
  2398.           return k
  2399.         end
  2400.        
  2401.         ------------------------------------------------------------------------
  2402.         -- parse a for loop body for both versions of the for loop
  2403.         -- * used in fornum(), forlist()
  2404.         ------------------------------------------------------------------------
  2405.         function luaY:forbody(ls, base, line, nvars, isnum)
  2406.           -- forbody -> DO block
  2407.           local bl = {}  -- BlockCnt
  2408.           local fs = ls.fs
  2409.           self:adjustlocalvars(ls, 3)  -- control variables
  2410.           self:checknext(ls, "TK_DO")
  2411.           local prep = isnum and luaK:codeAsBx(fs, "OP_FORPREP", base, luaK.NO_JUMP)
  2412.                              or luaK:jump(fs)
  2413.           self:enterblock(fs, bl, false)  -- scope for declared variables
  2414.           self:adjustlocalvars(ls, nvars)
  2415.           luaK:reserveregs(fs, nvars)
  2416.           self:block(ls)
  2417.           self:leaveblock(fs)  -- end of scope for declared variables
  2418.           luaK:patchtohere(fs, prep)
  2419.           local endfor = isnum and luaK:codeAsBx(fs, "OP_FORLOOP", base, luaK.NO_JUMP)
  2420.                                or luaK:codeABC(fs, "OP_TFORLOOP", base, 0, nvars)
  2421.           luaK:fixline(fs, line)  -- pretend that `OP_FOR' starts the loop
  2422.           luaK:patchlist(fs, isnum and endfor or luaK:jump(fs), prep + 1)
  2423.         end
  2424.        
  2425.         ------------------------------------------------------------------------
  2426.         -- parse a numerical for loop, calls forbody()
  2427.         -- * used in forstat()
  2428.         ------------------------------------------------------------------------
  2429.         function luaY:fornum(ls, varname, line)
  2430.           -- fornum -> NAME = exp1,exp1[,exp1] forbody
  2431.           local fs = ls.fs
  2432.           local base = fs.freereg
  2433.           self:new_localvarliteral(ls, "(for index)", 0)
  2434.           self:new_localvarliteral(ls, "(for limit)", 1)
  2435.           self:new_localvarliteral(ls, "(for step)", 2)
  2436.           self:new_localvar(ls, varname, 3)
  2437.           self:checknext(ls, '=')
  2438.           self:exp1(ls)  -- initial value
  2439.           self:checknext(ls, ",")
  2440.           self:exp1(ls)  -- limit
  2441.           if self:testnext(ls, ",") then
  2442.             self:exp1(ls)  -- optional step
  2443.           else  -- default step = 1
  2444.             luaK:codeABx(fs, "OP_LOADK", fs.freereg, luaK:numberK(fs, 1))
  2445.             luaK:reserveregs(fs, 1)
  2446.           end
  2447.           self:forbody(ls, base, line, 1, true)
  2448.         end
  2449.        
  2450.         ------------------------------------------------------------------------
  2451.         -- parse a generic for loop, calls forbody()
  2452.         -- * used in forstat()
  2453.         ------------------------------------------------------------------------
  2454.         function luaY:forlist(ls, indexname)
  2455.           -- forlist -> NAME {,NAME} IN explist1 forbody
  2456.           local fs = ls.fs
  2457.           local e = {}  -- expdesc
  2458.           local nvars = 0
  2459.           local base = fs.freereg
  2460.           -- create control variables
  2461.           self:new_localvarliteral(ls, "(for generator)", nvars)
  2462.           nvars = nvars + 1
  2463.           self:new_localvarliteral(ls, "(for state)", nvars)
  2464.           nvars = nvars + 1
  2465.           self:new_localvarliteral(ls, "(for control)", nvars)
  2466.           nvars = nvars + 1
  2467.           -- create declared variables
  2468.           self:new_localvar(ls, indexname, nvars)
  2469.           nvars = nvars + 1
  2470.           while self:testnext(ls, ",") do
  2471.             self:new_localvar(ls, self:str_checkname(ls), nvars)
  2472.             nvars = nvars + 1
  2473.           end
  2474.           self:checknext(ls, "TK_IN")
  2475.           local line = ls.linenumber
  2476.           self:adjust_assign(ls, 3, self:explist1(ls, e), e)
  2477.           luaK:checkstack(fs, 3)  -- extra space to call generator
  2478.           self:forbody(ls, base, line, nvars - 3, false)
  2479.         end
  2480.        
  2481.         ------------------------------------------------------------------------
  2482.         -- initial parsing for a for loop, calls fornum() or forlist()
  2483.         -- * used in statements()
  2484.         ------------------------------------------------------------------------
  2485.         function luaY:forstat(ls, line)
  2486.           -- forstat -> FOR (fornum | forlist) END
  2487.           local fs = ls.fs
  2488.           local bl = {}  -- BlockCnt
  2489.           self:enterblock(fs, bl, true)  -- scope for loop and control variables
  2490.           luaX:next(ls)  -- skip `for'
  2491.           local varname = self:str_checkname(ls)  -- first variable name
  2492.           local c = ls.t.token
  2493.           if c == "=" then
  2494.             self:fornum(ls, varname, line)
  2495.           elseif c == "," or c == "TK_IN" then
  2496.             self:forlist(ls, varname)
  2497.           else
  2498.             luaX:syntaxerror(ls, self:LUA_QL("=").." or "..self:LUA_QL("in").." expected")
  2499.           end
  2500.           self:check_match(ls, "TK_END", "TK_FOR", line)
  2501.           self:leaveblock(fs)  -- loop scope (`break' jumps to this point)
  2502.         end
  2503.        
  2504.         ------------------------------------------------------------------------
  2505.         -- parse part of an if control structure, including the condition
  2506.         -- * used in ifstat()
  2507.         ------------------------------------------------------------------------
  2508.         function luaY:test_then_block(ls)
  2509.           -- test_then_block -> [IF | ELSEIF] cond THEN block
  2510.           luaX:next(ls)  -- skip IF or ELSEIF
  2511.           local condexit = self:cond(ls)
  2512.           self:checknext(ls, "TK_THEN")
  2513.           self:block(ls)  -- `then' part
  2514.           return condexit
  2515.         end
  2516.        
  2517.         ------------------------------------------------------------------------
  2518.         -- parse an if control structure
  2519.         -- * used in statements()
  2520.         ------------------------------------------------------------------------
  2521.         function luaY:ifstat(ls, line)
  2522.           -- ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END
  2523.           local fs = ls.fs
  2524.           local escapelist = luaK.NO_JUMP
  2525.           local flist = self:test_then_block(ls)  -- IF cond THEN block
  2526.           while ls.t.token == "TK_ELSEIF" do
  2527.             escapelist = luaK:concat(fs, escapelist, luaK:jump(fs))
  2528.             luaK:patchtohere(fs, flist)
  2529.             flist = self:test_then_block(ls)  -- ELSEIF cond THEN block
  2530.           end
  2531.           if ls.t.token == "TK_ELSE" then
  2532.             escapelist = luaK:concat(fs, escapelist, luaK:jump(fs))
  2533.             luaK:patchtohere(fs, flist)
  2534.             luaX:next(ls)  -- skip ELSE (after patch, for correct line info)
  2535.             self:block(ls)  -- 'else' part
  2536.           else
  2537.             escapelist = luaK:concat(fs, escapelist, flist)
  2538.           end
  2539.           luaK:patchtohere(fs, escapelist)
  2540.           self:check_match(ls, "TK_END", "TK_IF", line)
  2541.         end
  2542.        
  2543.         ------------------------------------------------------------------------
  2544.         -- parse a local function statement
  2545.         -- * used in statements()
  2546.         ------------------------------------------------------------------------
  2547.         function luaY:localfunc(ls)
  2548.           local v, b = {}, {}  -- expdesc
  2549.           local fs = ls.fs
  2550.           self:new_localvar(ls, self:str_checkname(ls), 0)
  2551.           self:init_exp(v, "VLOCAL", fs.freereg)
  2552.           luaK:reserveregs(fs, 1)
  2553.           self:adjustlocalvars(ls, 1)
  2554.           self:body(ls, b, false, ls.linenumber)
  2555.           luaK:storevar(fs, v, b)
  2556.           -- debug information will only see the variable after this point!
  2557.           self:getlocvar(fs, fs.nactvar - 1).startpc = fs.pc
  2558.         end
  2559.        
  2560.         ------------------------------------------------------------------------
  2561.         -- parse a local variable declaration statement
  2562.         -- * used in statements()
  2563.         ------------------------------------------------------------------------
  2564.         function luaY:localstat(ls)
  2565.           -- stat -> LOCAL NAME {',' NAME} ['=' explist1]
  2566.           local nvars = 0
  2567.           local nexps
  2568.           local e = {}  -- expdesc
  2569.           repeat
  2570.             self:new_localvar(ls, self:str_checkname(ls), nvars)
  2571.             nvars = nvars + 1
  2572.           until not self:testnext(ls, ",")
  2573.           if self:testnext(ls, "=") then
  2574.             nexps = self:explist1(ls, e)
  2575.           else
  2576.             e.k = "VVOID"
  2577.             nexps = 0
  2578.           end
  2579.           self:adjust_assign(ls, nvars, nexps, e)
  2580.           self:adjustlocalvars(ls, nvars)
  2581.         end
  2582.        
  2583.         ------------------------------------------------------------------------
  2584.         -- parse a function name specification
  2585.         -- * used in funcstat()
  2586.         ------------------------------------------------------------------------
  2587.         function luaY:funcname(ls, v)
  2588.           -- funcname -> NAME {field} [':' NAME]
  2589.           local needself = false
  2590.           self:singlevar(ls, v)
  2591.           while ls.t.token == "." do
  2592.             self:field(ls, v)
  2593.           end
  2594.           if ls.t.token == ":" then
  2595.             needself = true
  2596.             self:field(ls, v)
  2597.           end
  2598.           return needself
  2599.         end
  2600.        
  2601.         ------------------------------------------------------------------------
  2602.         -- parse a function statement
  2603.         -- * used in statements()
  2604.         ------------------------------------------------------------------------
  2605.         function luaY:funcstat(ls, line)
  2606.           -- funcstat -> FUNCTION funcname body
  2607.           local v, b = {}, {}  -- expdesc
  2608.           luaX:next(ls)  -- skip FUNCTION
  2609.           local needself = self:funcname(ls, v)
  2610.           self:body(ls, b, needself, line)
  2611.           luaK:storevar(ls.fs, v, b)
  2612.           luaK:fixline(ls.fs, line)  -- definition 'happens' in the first line
  2613.         end
  2614.        
  2615.         ------------------------------------------------------------------------
  2616.         -- parse a function call with no returns or an assignment statement
  2617.         -- * used in statements()
  2618.         ------------------------------------------------------------------------
  2619.         function luaY:exprstat(ls)
  2620.           -- stat -> func | assignment
  2621.           local fs = ls.fs
  2622.           local v = {}  -- LHS_assign
  2623.                 v.v = {}
  2624.           self:primaryexp(ls, v.v)
  2625.           if v.v.k == "VCALL" then  -- stat -> func
  2626.             luaP:SETARG_C(luaK:getcode(fs, v.v), 1)  -- call statement uses no results
  2627.           else  -- stat -> assignment
  2628.             v.prev = nil
  2629.             self:assignment(ls, v, 1)
  2630.           end
  2631.         end
  2632.        
  2633.         ------------------------------------------------------------------------
  2634.         -- parse a return statement
  2635.         -- * used in statements()
  2636.         ------------------------------------------------------------------------
  2637.         function luaY:retstat(ls)
  2638.           -- stat -> RETURN explist
  2639.           local fs = ls.fs
  2640.           local e = {}  -- expdesc
  2641.           local first, nret  -- registers with returned values
  2642.           luaX:next(ls)  -- skip RETURN
  2643.           if self:block_follow(ls.t.token) or ls.t.token == ";" then
  2644.             first, nret = 0, 0  -- return no values
  2645.           else
  2646.             nret = self:explist1(ls, e)  -- optional return values
  2647.             if self:hasmultret(e.k) then
  2648.               luaK:setmultret(fs, e)
  2649.               if e.k == "VCALL" and nret == 1 then  -- tail call?
  2650.                 luaP:SET_OPCODE(luaK:getcode(fs, e), "OP_TAILCALL")
  2651.                 assert(luaP:GETARG_A(luaK:getcode(fs, e)) == fs.nactvar)
  2652.               end
  2653.               first = fs.nactvar
  2654.               nret = self.LUA_MULTRET  -- return all values
  2655.             else
  2656.               if nret == 1 then  -- only one single value?
  2657.                 first = luaK:exp2anyreg(fs, e)
  2658.               else
  2659.                 luaK:exp2nextreg(fs, e)  -- values must go to the 'stack'
  2660.                 first = fs.nactvar  -- return all 'active' values
  2661.                 assert(nret == fs.freereg - first)
  2662.               end
  2663.             end--if
  2664.           end--if
  2665.           luaK:ret(fs, first, nret)
  2666.         end
  2667.        
  2668.         ------------------------------------------------------------------------
  2669.         -- initial parsing for statements, calls a lot of functions
  2670.         -- * returns boolean instead of 0|1
  2671.         -- * used in chunk()
  2672.         ------------------------------------------------------------------------
  2673.         function luaY:statement(ls)
  2674.           local line = ls.linenumber  -- may be needed for error messages
  2675.           local c = ls.t.token
  2676.           if c == "TK_IF" then  -- stat -> ifstat
  2677.             self:ifstat(ls, line)
  2678.             return false
  2679.           elseif c == "TK_WHILE" then  -- stat -> whilestat
  2680.             self:whilestat(ls, line)
  2681.             return false
  2682.           elseif c == "TK_DO" then  -- stat -> DO block END
  2683.             luaX:next(ls)  -- skip DO
  2684.             self:block(ls)
  2685.             self:check_match(ls, "TK_END", "TK_DO", line)
  2686.             return false
  2687.           elseif c == "TK_FOR" then  -- stat -> forstat
  2688.             self:forstat(ls, line)
  2689.             return false
  2690.           elseif c == "TK_REPEAT" then  -- stat -> repeatstat
  2691.             self:repeatstat(ls, line)
  2692.             return false
  2693.           elseif c == "TK_FUNCTION" then  -- stat -> funcstat
  2694.             self:funcstat(ls, line)
  2695.             return false
  2696.           elseif c == "TK_LOCAL" then  -- stat -> localstat
  2697.             luaX:next(ls)  -- skip LOCAL
  2698.             if self:testnext(ls, "TK_FUNCTION") then  -- local function?
  2699.               self:localfunc(ls)
  2700.             else
  2701.               self:localstat(ls)
  2702.             end
  2703.             return false
  2704.           elseif c == "TK_RETURN" then  -- stat -> retstat
  2705.             self:retstat(ls)
  2706.             return true  -- must be last statement
  2707.           elseif c == "TK_BREAK" then  -- stat -> breakstat
  2708.             luaX:next(ls)  -- skip BREAK
  2709.             self:breakstat(ls)
  2710.             return true  -- must be last statement
  2711.           else
  2712.             self:exprstat(ls)
  2713.             return false  -- to avoid warnings
  2714.           end--if c
  2715.         end
  2716.        
  2717.         ------------------------------------------------------------------------
  2718.         -- parse a chunk, which consists of a bunch of statements
  2719.         -- * used in parser(), body(), block(), repeatstat()
  2720.         ------------------------------------------------------------------------
  2721.         function luaY:chunk(ls)
  2722.           -- chunk -> { stat [';'] }
  2723.           local islast = false
  2724.           self:enterlevel(ls)
  2725.           while not islast and not self:block_follow(ls.t.token) do
  2726.             islast = self:statement(ls)
  2727.             self:testnext(ls, ";")
  2728.             assert(ls.fs.f.maxstacksize >= ls.fs.freereg and
  2729.                        ls.fs.freereg >= ls.fs.nactvar)
  2730.             ls.fs.freereg = ls.fs.nactvar  -- free registers
  2731.           end
  2732.           self:leavelevel(ls)
  2733.         end
  2734.        
  2735.         -- }======================================================================
  2736.         return luaY
  2737.     end
  2738.     fake_module_scripts[script] = module_script
  2739. end
  2740. do -- nil.LuaK
  2741.     local script = Instance.new('ModuleScript', nil)
  2742.     script.Name = "LuaK"
  2743.     local function module_script()
  2744.         --[[--------------------------------------------------------------------
  2745.        
  2746.           lcode.lua
  2747.           Lua 5 code generator in Lua
  2748.           This file is part of Yueliang.
  2749.        
  2750.           Copyright (c) 2005-2007 Kein-Hong Man <khman@users.sf.net>
  2751.           The COPYRIGHT file describes the conditions
  2752.           under which this software may be distributed.
  2753.        
  2754.           See the ChangeLog for more information.
  2755.        
  2756.         ----------------------------------------------------------------------]]
  2757.        
  2758.         --[[--------------------------------------------------------------------
  2759.         -- Notes:
  2760.         -- * one function manipulate a pointer argument with a simple data type
  2761.         --   (can't be emulated by a table, ambiguous), now returns that value:
  2762.         --   luaK:concat(fs, l1, l2)
  2763.         -- * luaM_growvector uses the faux luaY:growvector, for limit checking
  2764.         -- * some function parameters changed to boolean, additional code
  2765.         --   translates boolean back to 1/0 for instruction fields
  2766.         --
  2767.         -- Not implemented:
  2768.         -- * NOTE there is a failed assert in luaK:addk, a porting problem
  2769.         --
  2770.         -- Added:
  2771.         -- * constant MAXSTACK from llimits.h
  2772.         -- * luaK:ttisnumber(o) (from lobject.h)
  2773.         -- * luaK:nvalue(o) (from lobject.h)
  2774.         -- * luaK:setnilvalue(o) (from lobject.h)
  2775.         -- * luaK:setnvalue(o, x) (from lobject.h)
  2776.         -- * luaK:setbvalue(o, x) (from lobject.h)
  2777.         -- * luaK:sethvalue(o, x) (from lobject.h), parameter L deleted
  2778.         -- * luaK:setsvalue(o, x) (from lobject.h), parameter L deleted
  2779.         -- * luaK:numadd, luaK:numsub, luaK:nummul, luaK:numdiv, luaK:nummod,
  2780.         --   luaK:numpow, luaK:numunm, luaK:numisnan (from luaconf.h)
  2781.         -- * copyexp(e1, e2) added in luaK:posfix to copy expdesc struct
  2782.         --
  2783.         -- Changed in 5.1.x:
  2784.         -- * enum BinOpr has a new entry, OPR_MOD
  2785.         -- * enum UnOpr has a new entry, OPR_LEN
  2786.         -- * binopistest, unused in 5.0.x, has been deleted
  2787.         -- * macro setmultret is new
  2788.         -- * functions isnumeral, luaK_ret, boolK are new
  2789.         -- * funcion nilK was named nil_constant in 5.0.x
  2790.         -- * function interface changed: need_value, patchtestreg, concat
  2791.         -- * TObject now a TValue
  2792.         -- * functions luaK_setreturns, luaK_setoneret are new
  2793.         -- * function luaK:setcallreturns deleted, to be replaced by:
  2794.         --   luaK:setmultret, luaK:ret, luaK:setreturns, luaK:setoneret
  2795.         -- * functions constfolding, codearith, codecomp are new
  2796.         -- * luaK:codebinop has been deleted
  2797.         -- * function luaK_setlist is new
  2798.         -- * OPR_MULT renamed to OPR_MUL
  2799.         ----------------------------------------------------------------------]]
  2800.        
  2801.         -- requires luaP, luaX, luaY
  2802.         local luaK = {}
  2803.         local luaP = require(script.Parent.LuaP)
  2804.         local luaX = require(script.Parent.LuaX)
  2805.        
  2806.         ------------------------------------------------------------------------
  2807.         -- constants used by code generator
  2808.         ------------------------------------------------------------------------
  2809.         -- maximum stack for a Lua function
  2810.         luaK.MAXSTACK = 250  -- (from llimits.h)
  2811.        
  2812.         --[[--------------------------------------------------------------------
  2813.         -- other functions
  2814.         ----------------------------------------------------------------------]]
  2815.        
  2816.         ------------------------------------------------------------------------
  2817.         -- emulation of TValue macros (these are from lobject.h)
  2818.         -- * TValue is a table since lcode passes references around
  2819.         -- * tt member field removed, using Lua's type() instead
  2820.         -- * for setsvalue, sethvalue, parameter L (deleted here) in lobject.h
  2821.         --   is used in an assert for testing, see checkliveness(g,obj)
  2822.         ------------------------------------------------------------------------
  2823.         function luaK:ttisnumber(o)
  2824.           if o then return type(o.value) == "number" else return false end
  2825.         end
  2826.         function luaK:nvalue(o) return o.value end
  2827.         function luaK:setnilvalue(o) o.value = nil end
  2828.         function luaK:setsvalue(o, x) o.value = x end
  2829.         luaK.setnvalue = luaK.setsvalue
  2830.         luaK.sethvalue = luaK.setsvalue
  2831.         luaK.setbvalue = luaK.setsvalue
  2832.        
  2833.         ------------------------------------------------------------------------
  2834.         -- The luai_num* macros define the primitive operations over numbers.
  2835.         -- * this is not the entire set of primitive operations from luaconf.h
  2836.         -- * used in luaK:constfolding()
  2837.         ------------------------------------------------------------------------
  2838.         function luaK:numadd(a, b) return a + b end
  2839.         function luaK:numsub(a, b) return a - b end
  2840.         function luaK:nummul(a, b) return a * b end
  2841.         function luaK:numdiv(a, b) return a / b end
  2842.         function luaK:nummod(a, b) return a % b end
  2843.           -- ((a) - floor((a)/(b))*(b)) /* actual, for reference */
  2844.         function luaK:numpow(a, b) return a ^ b end
  2845.         function luaK:numunm(a) return -a end
  2846.         function luaK:numisnan(a) return not a == a end
  2847.           -- a NaN cannot equal another NaN
  2848.        
  2849.         --[[--------------------------------------------------------------------
  2850.         -- code generator functions
  2851.         ----------------------------------------------------------------------]]
  2852.        
  2853.         ------------------------------------------------------------------------
  2854.         -- Marks the end of a patch list. It is an invalid value both as an absolute
  2855.         -- address, and as a list link (would link an element to itself).
  2856.         ------------------------------------------------------------------------
  2857.         luaK.NO_JUMP = -1
  2858.        
  2859.         ------------------------------------------------------------------------
  2860.         -- grep "ORDER OPR" if you change these enums
  2861.         ------------------------------------------------------------------------
  2862.         luaK.BinOpr = {
  2863.           OPR_ADD = 0, OPR_SUB = 1, OPR_MUL = 2, OPR_DIV = 3, OPR_MOD = 4, OPR_POW = 5,
  2864.           OPR_CONCAT = 6,
  2865.           OPR_NE = 7, OPR_EQ = 8,
  2866.           OPR_LT = 9, OPR_LE = 10, OPR_GT = 11, OPR_GE = 12,
  2867.           OPR_AND = 13, OPR_OR = 14,
  2868.           OPR_NOBINOPR = 15,
  2869.         }
  2870.        
  2871.         -- * UnOpr is used by luaK:prefix's op argument, but not directly used
  2872.         --   because the function receives the symbols as strings, e.g. "OPR_NOT"
  2873.         luaK.UnOpr = {
  2874.           OPR_MINUS = 0, OPR_NOT = 1, OPR_LEN = 2, OPR_NOUNOPR = 3
  2875.         }
  2876.        
  2877.         ------------------------------------------------------------------------
  2878.         -- returns the instruction object for given e (expdesc), was a macro
  2879.         ------------------------------------------------------------------------
  2880.         function luaK:getcode(fs, e)
  2881.           return fs.f.code[e.info]
  2882.         end
  2883.        
  2884.         ------------------------------------------------------------------------
  2885.         -- codes an instruction with a signed Bx (sBx) field, was a macro
  2886.         -- * used in luaK:jump(), (lparser) luaY:forbody()
  2887.         ------------------------------------------------------------------------
  2888.         function luaK:codeAsBx(fs, o, A, sBx)
  2889.           return self:codeABx(fs, o, A, sBx + luaP.MAXARG_sBx)
  2890.         end
  2891.        
  2892.         ------------------------------------------------------------------------
  2893.         -- set the expdesc e instruction for multiple returns, was a macro
  2894.         ------------------------------------------------------------------------
  2895.         function luaK:setmultret(fs, e)
  2896.           self:setreturns(fs, e, luaY.LUA_MULTRET)
  2897.         end
  2898.        
  2899.         ------------------------------------------------------------------------
  2900.         -- there is a jump if patch lists are not identical, was a macro
  2901.         -- * used in luaK:exp2reg(), luaK:exp2anyreg(), luaK:exp2val()
  2902.         ------------------------------------------------------------------------
  2903.         function luaK:hasjumps(e)
  2904.           return e.t ~= e.f
  2905.         end
  2906.        
  2907.         ------------------------------------------------------------------------
  2908.         -- true if the expression is a constant number (for constant folding)
  2909.         -- * used in constfolding(), infix()
  2910.         ------------------------------------------------------------------------
  2911.         function luaK:isnumeral(e)
  2912.           return e.k == "VKNUM" and e.t == self.NO_JUMP and e.f == self.NO_JUMP
  2913.         end
  2914.        
  2915.         ------------------------------------------------------------------------
  2916.         -- codes loading of nil, optimization done if consecutive locations
  2917.         -- * used in luaK:discharge2reg(), (lparser) luaY:adjust_assign()
  2918.         ------------------------------------------------------------------------
  2919.         function luaK:_nil(fs, from, n)
  2920.           if fs.pc > fs.lasttarget then  -- no jumps to current position?
  2921.             if fs.pc == 0 then  -- function start?
  2922.               if from >= fs.nactvar then
  2923.                 return  -- positions are already clean
  2924.               end
  2925.             else
  2926.               local previous = fs.f.code[fs.pc - 1]
  2927.               if luaP:GET_OPCODE(previous) == "OP_LOADNIL" then
  2928.                 local pfrom = luaP:GETARG_A(previous)
  2929.                 local pto = luaP:GETARG_B(previous)
  2930.                 if pfrom <= from and from <= pto + 1 then  -- can connect both?
  2931.                   if from + n - 1 > pto then
  2932.                     luaP:SETARG_B(previous, from + n - 1)
  2933.                   end
  2934.                   return
  2935.                 end
  2936.               end
  2937.             end
  2938.           end
  2939.           self:codeABC(fs, "OP_LOADNIL", from, from + n - 1, 0)  -- else no optimization
  2940.         end
  2941.        
  2942.         ------------------------------------------------------------------------
  2943.         --
  2944.         -- * used in multiple locations
  2945.         ------------------------------------------------------------------------
  2946.         function luaK:jump(fs)
  2947.           local jpc = fs.jpc  -- save list of jumps to here
  2948.           fs.jpc = self.NO_JUMP
  2949.           local j = self:codeAsBx(fs, "OP_JMP", 0, self.NO_JUMP)
  2950.           j = self:concat(fs, j, jpc)  -- keep them on hold
  2951.           return j
  2952.         end
  2953.        
  2954.         ------------------------------------------------------------------------
  2955.         -- codes a RETURN instruction
  2956.         -- * used in luaY:close_func(), luaY:retstat()
  2957.         ------------------------------------------------------------------------
  2958.         function luaK:ret(fs, first, nret)
  2959.           self:codeABC(fs, "OP_RETURN", first, nret + 1, 0)
  2960.         end
  2961.        
  2962.         ------------------------------------------------------------------------
  2963.         --
  2964.         -- * used in luaK:jumponcond(), luaK:codecomp()
  2965.         ------------------------------------------------------------------------
  2966.         function luaK:condjump(fs, op, A, B, C)
  2967.           self:codeABC(fs, op, A, B, C)
  2968.           return self:jump(fs)
  2969.         end
  2970.        
  2971.         ------------------------------------------------------------------------
  2972.         --
  2973.         -- * used in luaK:patchlistaux(), luaK:concat()
  2974.         ------------------------------------------------------------------------
  2975.         function luaK:fixjump(fs, pc, dest)
  2976.           local jmp = fs.f.code[pc]
  2977.           local offset = dest - (pc + 1)
  2978.           assert(dest ~= self.NO_JUMP)
  2979.           if math.abs(offset) > luaP.MAXARG_sBx then
  2980.             luaX:syntaxerror(fs.ls, "control structure too long")
  2981.           end
  2982.           luaP:SETARG_sBx(jmp, offset)
  2983.         end
  2984.        
  2985.         ------------------------------------------------------------------------
  2986.         -- returns current 'pc' and marks it as a jump target (to avoid wrong
  2987.         -- optimizations with consecutive instructions not in the same basic block).
  2988.         -- * used in multiple locations
  2989.         -- * fs.lasttarget tested only by luaK:_nil() when optimizing OP_LOADNIL
  2990.         ------------------------------------------------------------------------
  2991.         function luaK:getlabel(fs)
  2992.           fs.lasttarget = fs.pc
  2993.           return fs.pc
  2994.         end
  2995.        
  2996.         ------------------------------------------------------------------------
  2997.         --
  2998.         -- * used in luaK:need_value(), luaK:removevalues(), luaK:patchlistaux(),
  2999.         --   luaK:concat()
  3000.         ------------------------------------------------------------------------
  3001.         function luaK:getjump(fs, pc)
  3002.           local offset = luaP:GETARG_sBx(fs.f.code[pc])
  3003.           if offset == self.NO_JUMP then  -- point to itself represents end of list
  3004.             return self.NO_JUMP  -- end of list
  3005.           else
  3006.             return (pc + 1) + offset  -- turn offset into absolute position
  3007.           end
  3008.         end
  3009.        
  3010.         ------------------------------------------------------------------------
  3011.         --
  3012.         -- * used in luaK:need_value(), luaK:patchtestreg(), luaK:invertjump()
  3013.         ------------------------------------------------------------------------
  3014.         function luaK:getjumpcontrol(fs, pc)
  3015.           local pi = fs.f.code[pc]
  3016.           local ppi = fs.f.code[pc - 1]
  3017.           if pc >= 1 and luaP:testTMode(luaP:GET_OPCODE(ppi)) ~= 0 then
  3018.             return ppi
  3019.           else
  3020.             return pi
  3021.           end
  3022.         end
  3023.        
  3024.         ------------------------------------------------------------------------
  3025.         -- check whether list has any jump that do not produce a value
  3026.         -- (or produce an inverted value)
  3027.         -- * return value changed to boolean
  3028.         -- * used only in luaK:exp2reg()
  3029.         ------------------------------------------------------------------------
  3030.         function luaK:need_value(fs, list)
  3031.           while list ~= self.NO_JUMP do
  3032.             local i = self:getjumpcontrol(fs, list)
  3033.             if luaP:GET_OPCODE(i) ~= "OP_TESTSET" then return true end
  3034.             list = self:getjump(fs, list)
  3035.           end
  3036.           return false  -- not found
  3037.         end
  3038.        
  3039.         ------------------------------------------------------------------------
  3040.         --
  3041.         -- * used in luaK:removevalues(), luaK:patchlistaux()
  3042.         ------------------------------------------------------------------------
  3043.         function luaK:patchtestreg(fs, node, reg)
  3044.           local i = self:getjumpcontrol(fs, node)
  3045.           if luaP:GET_OPCODE(i) ~= "OP_TESTSET" then
  3046.             return false  -- cannot patch other instructions
  3047.           end
  3048.           if reg ~= luaP.NO_REG and reg ~= luaP:GETARG_B(i) then
  3049.             luaP:SETARG_A(i, reg)
  3050.           else  -- no register to put value or register already has the value
  3051.             -- due to use of a table as i, i cannot be replaced by another table
  3052.             -- so the following is required; there is no change to ARG_C
  3053.             luaP:SET_OPCODE(i, "OP_TEST")
  3054.             local b = luaP:GETARG_B(i)
  3055.             luaP:SETARG_A(i, b)
  3056.             luaP:SETARG_B(i, 0)
  3057.             -- *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); /* C */
  3058.           end
  3059.           return true
  3060.         end
  3061.        
  3062.         ------------------------------------------------------------------------
  3063.         --
  3064.         -- * used only in luaK:codenot()
  3065.         ------------------------------------------------------------------------
  3066.         function luaK:removevalues(fs, list)
  3067.           while list ~= self.NO_JUMP do
  3068.             self:patchtestreg(fs, list, luaP.NO_REG)
  3069.             list = self:getjump(fs, list)
  3070.           end
  3071.         end
  3072.        
  3073.         ------------------------------------------------------------------------
  3074.         --
  3075.         -- * used in luaK:dischargejpc(), luaK:patchlist(), luaK:exp2reg()
  3076.         ------------------------------------------------------------------------
  3077.         function luaK:patchlistaux(fs, list, vtarget, reg, dtarget)
  3078.           while list ~= self.NO_JUMP do
  3079.             local _next = self:getjump(fs, list)
  3080.             if self:patchtestreg(fs, list, reg) then
  3081.               self:fixjump(fs, list, vtarget)
  3082.             else
  3083.               self:fixjump(fs, list, dtarget)  -- jump to default target
  3084.             end
  3085.             list = _next
  3086.           end
  3087.         end
  3088.        
  3089.         ------------------------------------------------------------------------
  3090.         --
  3091.         -- * used only in luaK:code()
  3092.         ------------------------------------------------------------------------
  3093.         function luaK:dischargejpc(fs)
  3094.           self:patchlistaux(fs, fs.jpc, fs.pc, luaP.NO_REG, fs.pc)
  3095.           fs.jpc = self.NO_JUMP
  3096.         end
  3097.        
  3098.         ------------------------------------------------------------------------
  3099.         --
  3100.         -- * used in (lparser) luaY:whilestat(), luaY:repeatstat(), luaY:forbody()
  3101.         ------------------------------------------------------------------------
  3102.         function luaK:patchlist(fs, list, target)
  3103.           if target == fs.pc then
  3104.             self:patchtohere(fs, list)
  3105.           else
  3106.             assert(target < fs.pc)
  3107.             self:patchlistaux(fs, list, target, luaP.NO_REG, target)
  3108.           end
  3109.         end
  3110.        
  3111.         ------------------------------------------------------------------------
  3112.         --
  3113.         -- * used in multiple locations
  3114.         ------------------------------------------------------------------------
  3115.         function luaK:patchtohere(fs, list)
  3116.           self:getlabel(fs)
  3117.           fs.jpc = self:concat(fs, fs.jpc, list)
  3118.         end
  3119.        
  3120.         ------------------------------------------------------------------------
  3121.         -- * l1 was a pointer, now l1 is returned and callee assigns the value
  3122.         -- * used in multiple locations
  3123.         ------------------------------------------------------------------------
  3124.         function luaK:concat(fs, l1, l2)
  3125.           if l2 == self.NO_JUMP then return l1
  3126.           elseif l1 == self.NO_JUMP then
  3127.             return l2
  3128.           else
  3129.             local list = l1
  3130.             local _next = self:getjump(fs, list)
  3131.             while _next ~= self.NO_JUMP do  -- find last element
  3132.               list = _next
  3133.               _next = self:getjump(fs, list)
  3134.             end
  3135.             self:fixjump(fs, list, l2)
  3136.           end
  3137.           return l1
  3138.         end
  3139.        
  3140.         ------------------------------------------------------------------------
  3141.         --
  3142.         -- * used in luaK:reserveregs(), (lparser) luaY:forlist()
  3143.         ------------------------------------------------------------------------
  3144.         function luaK:checkstack(fs, n)
  3145.           local newstack = fs.freereg + n
  3146.           if newstack > fs.f.maxstacksize then
  3147.             if newstack >= self.MAXSTACK then
  3148.               luaX:syntaxerror(fs.ls, "function or expression too complex")
  3149.             end
  3150.             fs.f.maxstacksize = newstack
  3151.           end
  3152.         end
  3153.        
  3154.         ------------------------------------------------------------------------
  3155.         --
  3156.         -- * used in multiple locations
  3157.         ------------------------------------------------------------------------
  3158.         function luaK:reserveregs(fs, n)
  3159.           self:checkstack(fs, n)
  3160.           fs.freereg = fs.freereg + n
  3161.         end
  3162.        
  3163.         ------------------------------------------------------------------------
  3164.         --
  3165.         -- * used in luaK:freeexp(), luaK:dischargevars()
  3166.         ------------------------------------------------------------------------
  3167.         function luaK:freereg(fs, reg)
  3168.           if not luaP:ISK(reg) and reg >= fs.nactvar then
  3169.             fs.freereg = fs.freereg - 1
  3170.             assert(reg == fs.freereg)
  3171.           end
  3172.         end
  3173.        
  3174.         ------------------------------------------------------------------------
  3175.         --
  3176.         -- * used in multiple locations
  3177.         ------------------------------------------------------------------------
  3178.         function luaK:freeexp(fs, e)
  3179.           if e.k == "VNONRELOC" then
  3180.             self:freereg(fs, e.info)
  3181.           end
  3182.         end
  3183.        
  3184.         ------------------------------------------------------------------------
  3185.         -- * TODO NOTE implementation is not 100% correct, since the assert fails
  3186.         -- * luaH_set, setobj deleted; direct table access used instead
  3187.         -- * used in luaK:stringK(), luaK:numberK(), luaK:boolK(), luaK:nilK()
  3188.         ------------------------------------------------------------------------
  3189.         function luaK:addk(fs, k, v)
  3190.           local L = fs.L
  3191.           local idx = fs.h[k.value]
  3192.           --TValue *idx = luaH_set(L, fs->h, k); /* C */
  3193.           local f = fs.f
  3194.           if self:ttisnumber(idx) then
  3195.             --TODO this assert currently FAILS (last tested for 5.0.2)
  3196.             --assert(fs.f.k[self:nvalue(idx)] == v)
  3197.             --assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v)); /* C */
  3198.             return self:nvalue(idx)
  3199.           else -- constant not found; create a new entry
  3200.             idx = {}
  3201.             self:setnvalue(idx, fs.nk)
  3202.             fs.h[k.value] = idx
  3203.             -- setnvalue(idx, cast_num(fs->nk)); /* C */
  3204.             luaY:growvector(L, f.k, fs.nk, f.sizek, nil,
  3205.                             luaP.MAXARG_Bx, "constant table overflow")
  3206.             -- loop to initialize empty f.k positions not required
  3207.             f.k[fs.nk] = v
  3208.             -- setobj(L, &f->k[fs->nk], v); /* C */
  3209.             -- luaC_barrier(L, f, v); /* GC */
  3210.             local nk = fs.nk
  3211.             fs.nk = fs.nk + 1
  3212.             return nk
  3213.           end
  3214.        
  3215.         end
  3216.        
  3217.         ------------------------------------------------------------------------
  3218.         -- creates and sets a string object
  3219.         -- * used in (lparser) luaY:codestring(), luaY:singlevar()
  3220.         ------------------------------------------------------------------------
  3221.         function luaK:stringK(fs, s)
  3222.           local o = {}  -- TValue
  3223.           self:setsvalue(o, s)
  3224.           return self:addk(fs, o, o)
  3225.         end
  3226.        
  3227.         ------------------------------------------------------------------------
  3228.         -- creates and sets a number object
  3229.         -- * used in luaK:prefix() for negative (or negation of) numbers
  3230.         -- * used in (lparser) luaY:simpleexp(), luaY:fornum()
  3231.         ------------------------------------------------------------------------
  3232.         function luaK:numberK(fs, r)
  3233.           local o = {}  -- TValue
  3234.           self:setnvalue(o, r)
  3235.           return self:addk(fs, o, o)
  3236.         end
  3237.        
  3238.         ------------------------------------------------------------------------
  3239.         -- creates and sets a boolean object
  3240.         -- * used only in luaK:exp2RK()
  3241.         ------------------------------------------------------------------------
  3242.         function luaK:boolK(fs, b)
  3243.           local o = {}  -- TValue
  3244.           self:setbvalue(o, b)
  3245.           return self:addk(fs, o, o)
  3246.         end
  3247.        
  3248.         ------------------------------------------------------------------------
  3249.         -- creates and sets a nil object
  3250.         -- * used only in luaK:exp2RK()
  3251.         ------------------------------------------------------------------------
  3252.         function luaK:nilK(fs)
  3253.           local k, v = {}, {}  -- TValue
  3254.           self:setnilvalue(v)
  3255.           -- cannot use nil as key; instead use table itself to represent nil
  3256.           self:sethvalue(k, fs.h)
  3257.           return self:addk(fs, k, v)
  3258.         end
  3259.        
  3260.         ------------------------------------------------------------------------
  3261.         --
  3262.         -- * used in luaK:setmultret(), (lparser) luaY:adjust_assign()
  3263.         ------------------------------------------------------------------------
  3264.         function luaK:setreturns(fs, e, nresults)
  3265.           if e.k == "VCALL" then  -- expression is an open function call?
  3266.             luaP:SETARG_C(self:getcode(fs, e), nresults + 1)
  3267.           elseif e.k == "VVARARG" then
  3268.             luaP:SETARG_B(self:getcode(fs, e), nresults + 1);
  3269.             luaP:SETARG_A(self:getcode(fs, e), fs.freereg);
  3270.             luaK:reserveregs(fs, 1)
  3271.           end
  3272.         end
  3273.        
  3274.         ------------------------------------------------------------------------
  3275.         --
  3276.         -- * used in luaK:dischargevars(), (lparser) luaY:assignment()
  3277.         ------------------------------------------------------------------------
  3278.         function luaK:setoneret(fs, e)
  3279.           if e.k == "VCALL" then  -- expression is an open function call?
  3280.             e.k = "VNONRELOC"
  3281.             e.info = luaP:GETARG_A(self:getcode(fs, e))
  3282.           elseif e.k == "VVARARG" then
  3283.             luaP:SETARG_B(self:getcode(fs, e), 2)
  3284.             e.k = "VRELOCABLE"  -- can relocate its simple result
  3285.           end
  3286.         end
  3287.        
  3288.         ------------------------------------------------------------------------
  3289.         --
  3290.         -- * used in multiple locations
  3291.         ------------------------------------------------------------------------
  3292.         function luaK:dischargevars(fs, e)
  3293.           local k = e.k
  3294.           if k == "VLOCAL" then
  3295.             e.k = "VNONRELOC"
  3296.           elseif k == "VUPVAL" then
  3297.             e.info = self:codeABC(fs, "OP_GETUPVAL", 0, e.info, 0)
  3298.             e.k = "VRELOCABLE"
  3299.           elseif k == "VGLOBAL" then
  3300.             e.info = self:codeABx(fs, "OP_GETGLOBAL", 0, e.info)
  3301.             e.k = "VRELOCABLE"
  3302.           elseif k == "VINDEXED" then
  3303.             self:freereg(fs, e.aux)
  3304.             self:freereg(fs, e.info)
  3305.             e.info = self:codeABC(fs, "OP_GETTABLE", 0, e.info, e.aux)
  3306.             e.k = "VRELOCABLE"
  3307.           elseif k == "VVARARG" or k == "VCALL" then
  3308.             self:setoneret(fs, e)
  3309.           else
  3310.             -- there is one value available (somewhere)
  3311.           end
  3312.         end
  3313.        
  3314.         ------------------------------------------------------------------------
  3315.         --
  3316.         -- * used only in luaK:exp2reg()
  3317.         ------------------------------------------------------------------------
  3318.         function luaK:code_label(fs, A, b, jump)
  3319.           self:getlabel(fs)  -- those instructions may be jump targets
  3320.           return self:codeABC(fs, "OP_LOADBOOL", A, b, jump)
  3321.         end
  3322.        
  3323.         ------------------------------------------------------------------------
  3324.         --
  3325.         -- * used in luaK:discharge2anyreg(), luaK:exp2reg()
  3326.         ------------------------------------------------------------------------
  3327.         function luaK:discharge2reg(fs, e, reg)
  3328.           self:dischargevars(fs, e)
  3329.           local k = e.k
  3330.           if k == "VNIL" then
  3331.             self:_nil(fs, reg, 1)
  3332.           elseif k == "VFALSE" or k == "VTRUE" then
  3333.             self:codeABC(fs, "OP_LOADBOOL", reg, (e.k == "VTRUE") and 1 or 0, 0)
  3334.           elseif k == "VK" then
  3335.             self:codeABx(fs, "OP_LOADK", reg, e.info)
  3336.           elseif k == "VKNUM" then
  3337.             self:codeABx(fs, "OP_LOADK", reg, self:numberK(fs, e.nval))
  3338.           elseif k == "VRELOCABLE" then
  3339.             local pc = self:getcode(fs, e)
  3340.             luaP:SETARG_A(pc, reg)
  3341.           elseif k == "VNONRELOC" then
  3342.             if reg ~= e.info then
  3343.               self:codeABC(fs, "OP_MOVE", reg, e.info, 0)
  3344.             end
  3345.           else
  3346.             assert(e.k == "VVOID" or e.k == "VJMP")
  3347.             return  -- nothing to do...
  3348.           end
  3349.           e.info = reg
  3350.           e.k = "VNONRELOC"
  3351.         end
  3352.        
  3353.         ------------------------------------------------------------------------
  3354.         --
  3355.         -- * used in luaK:jumponcond(), luaK:codenot()
  3356.         ------------------------------------------------------------------------
  3357.         function luaK:discharge2anyreg(fs, e)
  3358.           if e.k ~= "VNONRELOC" then
  3359.             self:reserveregs(fs, 1)
  3360.             self:discharge2reg(fs, e, fs.freereg - 1)
  3361.           end
  3362.         end
  3363.        
  3364.         ------------------------------------------------------------------------
  3365.         --
  3366.         -- * used in luaK:exp2nextreg(), luaK:exp2anyreg(), luaK:storevar()
  3367.         ------------------------------------------------------------------------
  3368.         function luaK:exp2reg(fs, e, reg)
  3369.           self:discharge2reg(fs, e, reg)
  3370.           if e.k == "VJMP" then
  3371.             e.t = self:concat(fs, e.t, e.info)  -- put this jump in 't' list
  3372.           end
  3373.           if self:hasjumps(e) then
  3374.             local final  -- position after whole expression
  3375.             local p_f = self.NO_JUMP  -- position of an eventual LOAD false
  3376.             local p_t = self.NO_JUMP  -- position of an eventual LOAD true
  3377.             if self:need_value(fs, e.t) or self:need_value(fs, e.f) then
  3378.               local fj = (e.k == "VJMP") and self.NO_JUMP or self:jump(fs)
  3379.               p_f = self:code_label(fs, reg, 0, 1)
  3380.               p_t = self:code_label(fs, reg, 1, 0)
  3381.               self:patchtohere(fs, fj)
  3382.             end
  3383.             final = self:getlabel(fs)
  3384.             self:patchlistaux(fs, e.f, final, reg, p_f)
  3385.             self:patchlistaux(fs, e.t, final, reg, p_t)
  3386.           end
  3387.           e.f, e.t = self.NO_JUMP, self.NO_JUMP
  3388.           e.info = reg
  3389.           e.k = "VNONRELOC"
  3390.         end
  3391.        
  3392.         ------------------------------------------------------------------------
  3393.         --
  3394.         -- * used in multiple locations
  3395.         ------------------------------------------------------------------------
  3396.         function luaK:exp2nextreg(fs, e)
  3397.           self:dischargevars(fs, e)
  3398.           self:freeexp(fs, e)
  3399.           self:reserveregs(fs, 1)
  3400.           self:exp2reg(fs, e, fs.freereg - 1)
  3401.         end
  3402.        
  3403.         ------------------------------------------------------------------------
  3404.         --
  3405.         -- * used in multiple locations
  3406.         ------------------------------------------------------------------------
  3407.         function luaK:exp2anyreg(fs, e)
  3408.           self:dischargevars(fs, e)
  3409.           if e.k == "VNONRELOC" then
  3410.             if not self:hasjumps(e) then  -- exp is already in a register
  3411.               return e.info
  3412.             end
  3413.             if e.info >= fs.nactvar then  -- reg. is not a local?
  3414.               self:exp2reg(fs, e, e.info)  -- put value on it
  3415.               return e.info
  3416.             end
  3417.           end
  3418.           self:exp2nextreg(fs, e)  -- default
  3419.           return e.info
  3420.         end
  3421.        
  3422.         ------------------------------------------------------------------------
  3423.         --
  3424.         -- * used in luaK:exp2RK(), luaK:prefix(), luaK:posfix()
  3425.         -- * used in (lparser) luaY:yindex()
  3426.         ------------------------------------------------------------------------
  3427.         function luaK:exp2val(fs, e)
  3428.           if self:hasjumps(e) then
  3429.             self:exp2anyreg(fs, e)
  3430.           else
  3431.             self:dischargevars(fs, e)
  3432.           end
  3433.         end
  3434.        
  3435.         ------------------------------------------------------------------------
  3436.         --
  3437.         -- * used in multiple locations
  3438.         ------------------------------------------------------------------------
  3439.         function luaK:exp2RK(fs, e)
  3440.           self:exp2val(fs, e)
  3441.           local k = e.k
  3442.           if k == "VKNUM" or k == "VTRUE" or k == "VFALSE" or k == "VNIL" then
  3443.             if fs.nk <= luaP.MAXINDEXRK then  -- constant fit in RK operand?
  3444.               -- converted from a 2-deep ternary operator expression
  3445.               if e.k == "VNIL" then
  3446.                 e.info = self:nilK(fs)
  3447.               else
  3448.                 e.info = (e.k == "VKNUM") and self:numberK(fs, e.nval)
  3449.                                           or self:boolK(fs, e.k == "VTRUE")
  3450.               end
  3451.               e.k = "VK"
  3452.               return luaP:RKASK(e.info)
  3453.             end
  3454.           elseif k == "VK" then
  3455.             if e.info <= luaP.MAXINDEXRK then  -- constant fit in argC?
  3456.               return luaP:RKASK(e.info)
  3457.             end
  3458.           else
  3459.             -- default
  3460.           end
  3461.           -- not a constant in the right range: put it in a register
  3462.           return self:exp2anyreg(fs, e)
  3463.         end
  3464.        
  3465.         ------------------------------------------------------------------------
  3466.         --
  3467.         -- * used in (lparser) luaY:assignment(), luaY:localfunc(), luaY:funcstat()
  3468.         ------------------------------------------------------------------------
  3469.         function luaK:storevar(fs, var, ex)
  3470.           local k = var.k
  3471.           if k == "VLOCAL" then
  3472.             self:freeexp(fs, ex)
  3473.             self:exp2reg(fs, ex, var.info)
  3474.             return
  3475.           elseif k == "VUPVAL" then
  3476.             local e = self:exp2anyreg(fs, ex)
  3477.             self:codeABC(fs, "OP_SETUPVAL", e, var.info, 0)
  3478.           elseif k == "VGLOBAL" then
  3479.             local e = self:exp2anyreg(fs, ex)
  3480.             self:codeABx(fs, "OP_SETGLOBAL", e, var.info)
  3481.           elseif k == "VINDEXED" then
  3482.             local e = self:exp2RK(fs, ex)
  3483.             self:codeABC(fs, "OP_SETTABLE", var.info, var.aux, e)
  3484.           else
  3485.             assert(0)  -- invalid var kind to store
  3486.           end
  3487.           self:freeexp(fs, ex)
  3488.         end
  3489.        
  3490.         ------------------------------------------------------------------------
  3491.         --
  3492.         -- * used only in (lparser) luaY:primaryexp()
  3493.         ------------------------------------------------------------------------
  3494.         function luaK:_self(fs, e, key)
  3495.           self:exp2anyreg(fs, e)
  3496.           self:freeexp(fs, e)
  3497.           local func = fs.freereg
  3498.           self:reserveregs(fs, 2)
  3499.           self:codeABC(fs, "OP_SELF", func, e.info, self:exp2RK(fs, key))
  3500.           self:freeexp(fs, key)
  3501.           e.info = func
  3502.           e.k = "VNONRELOC"
  3503.         end
  3504.        
  3505.         ------------------------------------------------------------------------
  3506.         --
  3507.         -- * used in luaK:goiftrue(), luaK:codenot()
  3508.         ------------------------------------------------------------------------
  3509.         function luaK:invertjump(fs, e)
  3510.           local pc = self:getjumpcontrol(fs, e.info)
  3511.           assert(luaP:testTMode(luaP:GET_OPCODE(pc)) ~= 0 and
  3512.                      luaP:GET_OPCODE(pc) ~= "OP_TESTSET" and
  3513.                      luaP:GET_OPCODE(pc) ~= "OP_TEST")
  3514.           luaP:SETARG_A(pc, (luaP:GETARG_A(pc) == 0) and 1 or 0)
  3515.         end
  3516.        
  3517.         ------------------------------------------------------------------------
  3518.         --
  3519.         -- * used in luaK:goiftrue(), luaK:goiffalse()
  3520.         ------------------------------------------------------------------------
  3521.         function luaK:jumponcond(fs, e, cond)
  3522.           if e.k == "VRELOCABLE" then
  3523.             local ie = self:getcode(fs, e)
  3524.             if luaP:GET_OPCODE(ie) == "OP_NOT" then
  3525.               fs.pc = fs.pc - 1  -- remove previous OP_NOT
  3526.               return self:condjump(fs, "OP_TEST", luaP:GETARG_B(ie), 0, cond and 0 or 1)
  3527.             end
  3528.             -- else go through
  3529.           end
  3530.           self:discharge2anyreg(fs, e)
  3531.           self:freeexp(fs, e)
  3532.           return self:condjump(fs, "OP_TESTSET", luaP.NO_REG, e.info, cond and 1 or 0)
  3533.         end
  3534.        
  3535.         ------------------------------------------------------------------------
  3536.         --
  3537.         -- * used in luaK:infix(), (lparser) luaY:cond()
  3538.         ------------------------------------------------------------------------
  3539.         function luaK:goiftrue(fs, e)
  3540.           local pc  -- pc of last jump
  3541.           self:dischargevars(fs, e)
  3542.           local k = e.k
  3543.           if k == "VK" or k == "VKNUM" or k == "VTRUE" then
  3544.             pc = self.NO_JUMP  -- always true; do nothing
  3545.           elseif k == "VFALSE" then
  3546.             pc = self:jump(fs)  -- always jump
  3547.           elseif k == "VJMP" then
  3548.             self:invertjump(fs, e)
  3549.             pc = e.info
  3550.           else
  3551.             pc = self:jumponcond(fs, e, false)
  3552.           end
  3553.           e.f = self:concat(fs, e.f, pc)  -- insert last jump in `f' list
  3554.           self:patchtohere(fs, e.t)
  3555.           e.t = self.NO_JUMP
  3556.         end
  3557.        
  3558.         ------------------------------------------------------------------------
  3559.         --
  3560.         -- * used in luaK:infix()
  3561.         ------------------------------------------------------------------------
  3562.         function luaK:goiffalse(fs, e)
  3563.           local pc  -- pc of last jump
  3564.           self:dischargevars(fs, e)
  3565.           local k = e.k
  3566.           if k == "VNIL" or k == "VFALSE"then
  3567.             pc = self.NO_JUMP  -- always false; do nothing
  3568.           elseif k == "VTRUE" then
  3569.             pc = self:jump(fs)  -- always jump
  3570.           elseif k == "VJMP" then
  3571.             pc = e.info
  3572.           else
  3573.             pc = self:jumponcond(fs, e, true)
  3574.           end
  3575.           e.t = self:concat(fs, e.t, pc)  -- insert last jump in `t' list
  3576.           self:patchtohere(fs, e.f)
  3577.           e.f = self.NO_JUMP
  3578.         end
  3579.        
  3580.         ------------------------------------------------------------------------
  3581.         --
  3582.         -- * used only in luaK:prefix()
  3583.         ------------------------------------------------------------------------
  3584.         function luaK:codenot(fs, e)
  3585.           self:dischargevars(fs, e)
  3586.           local k = e.k
  3587.           if k == "VNIL" or k == "VFALSE" then
  3588.             e.k = "VTRUE"
  3589.           elseif k == "VK" or k == "VKNUM" or k == "VTRUE" then
  3590.             e.k = "VFALSE"
  3591.           elseif k == "VJMP" then
  3592.             self:invertjump(fs, e)
  3593.           elseif k == "VRELOCABLE" or k == "VNONRELOC" then
  3594.             self:discharge2anyreg(fs, e)
  3595.             self:freeexp(fs, e)
  3596.             e.info = self:codeABC(fs, "OP_NOT", 0, e.info, 0)
  3597.             e.k = "VRELOCABLE"
  3598.           else
  3599.             assert(0)  -- cannot happen
  3600.           end
  3601.           -- interchange true and false lists
  3602.           e.f, e.t = e.t, e.f
  3603.           self:removevalues(fs, e.f)
  3604.           self:removevalues(fs, e.t)
  3605.         end
  3606.        
  3607.         ------------------------------------------------------------------------
  3608.         --
  3609.         -- * used in (lparser) luaY:field(), luaY:primaryexp()
  3610.         ------------------------------------------------------------------------
  3611.         function luaK:indexed(fs, t, k)
  3612.           t.aux = self:exp2RK(fs, k)
  3613.           t.k = "VINDEXED"
  3614.         end
  3615.        
  3616.         ------------------------------------------------------------------------
  3617.         --
  3618.         -- * used only in luaK:codearith()
  3619.         ------------------------------------------------------------------------
  3620.         function luaK:constfolding(op, e1, e2)
  3621.           local r
  3622.           if not self:isnumeral(e1) or not self:isnumeral(e2) then return false end
  3623.           local v1 = e1.nval
  3624.           local v2 = e2.nval
  3625.           if op == "OP_ADD" then
  3626.             r = self:numadd(v1, v2)
  3627.           elseif op == "OP_SUB" then
  3628.             r = self:numsub(v1, v2)
  3629.           elseif op == "OP_MUL" then
  3630.             r = self:nummul(v1, v2)
  3631.           elseif op == "OP_DIV" then
  3632.             if v2 == 0 then return false end  -- do not attempt to divide by 0
  3633.             r = self:numdiv(v1, v2)
  3634.           elseif op == "OP_MOD" then
  3635.             if v2 == 0 then return false end  -- do not attempt to divide by 0
  3636.             r = self:nummod(v1, v2)
  3637.           elseif op == "OP_POW" then
  3638.             r = self:numpow(v1, v2)
  3639.           elseif op == "OP_UNM" then
  3640.             r = self:numunm(v1)
  3641.           elseif op == "OP_LEN" then
  3642.             return false  -- no constant folding for 'len'
  3643.           else
  3644.             assert(0)
  3645.             r = 0
  3646.           end
  3647.           if self:numisnan(r) then return false end  -- do not attempt to produce NaN
  3648.           e1.nval = r
  3649.           return true
  3650.         end
  3651.        
  3652.         ------------------------------------------------------------------------
  3653.         --
  3654.         -- * used in luaK:prefix(), luaK:posfix()
  3655.         ------------------------------------------------------------------------
  3656.         function luaK:codearith(fs, op, e1, e2)
  3657.           if self:constfolding(op, e1, e2) then
  3658.             return
  3659.           else
  3660.             local o2 = (op ~= "OP_UNM" and op ~= "OP_LEN") and self:exp2RK(fs, e2) or 0
  3661.             local o1 = self:exp2RK(fs, e1)
  3662.             if o1 > o2 then
  3663.               self:freeexp(fs, e1)
  3664.               self:freeexp(fs, e2)
  3665.             else
  3666.               self:freeexp(fs, e2)
  3667.               self:freeexp(fs, e1)
  3668.             end
  3669.             e1.info = self:codeABC(fs, op, 0, o1, o2)
  3670.             e1.k = "VRELOCABLE"
  3671.           end
  3672.         end
  3673.        
  3674.         ------------------------------------------------------------------------
  3675.         --
  3676.         -- * used only in luaK:posfix()
  3677.         ------------------------------------------------------------------------
  3678.         function luaK:codecomp(fs, op, cond, e1, e2)
  3679.           local o1 = self:exp2RK(fs, e1)
  3680.           local o2 = self:exp2RK(fs, e2)
  3681.           self:freeexp(fs, e2)
  3682.           self:freeexp(fs, e1)
  3683.           if cond == 0 and op ~= "OP_EQ" then
  3684.             -- exchange args to replace by `<' or `<='
  3685.             o1, o2 = o2, o1  -- o1 <==> o2
  3686.             cond = 1
  3687.           end
  3688.           e1.info = self:condjump(fs, op, cond, o1, o2)
  3689.           e1.k = "VJMP"
  3690.         end
  3691.        
  3692.         ------------------------------------------------------------------------
  3693.         --
  3694.         -- * used only in (lparser) luaY:subexpr()
  3695.         ------------------------------------------------------------------------
  3696.         function luaK:prefix(fs, op, e)
  3697.           local e2 = {}  -- expdesc
  3698.           e2.t, e2.f = self.NO_JUMP, self.NO_JUMP
  3699.           e2.k = "VKNUM"
  3700.           e2.nval = 0
  3701.           if op == "OPR_MINUS" then
  3702.             if not self:isnumeral(e) then
  3703.               self:exp2anyreg(fs, e)  -- cannot operate on non-numeric constants
  3704.             end
  3705.             self:codearith(fs, "OP_UNM", e, e2)
  3706.           elseif op == "OPR_NOT" then
  3707.             self:codenot(fs, e)
  3708.           elseif op == "OPR_LEN" then
  3709.             self:exp2anyreg(fs, e)  -- cannot operate on constants
  3710.             self:codearith(fs, "OP_LEN", e, e2)
  3711.           else
  3712.             assert(0)
  3713.           end
  3714.         end
  3715.        
  3716.         ------------------------------------------------------------------------
  3717.         --
  3718.         -- * used only in (lparser) luaY:subexpr()
  3719.         ------------------------------------------------------------------------
  3720.         function luaK:infix(fs, op, v)
  3721.           if op == "OPR_AND" then
  3722.             self:goiftrue(fs, v)
  3723.           elseif op == "OPR_OR" then
  3724.             self:goiffalse(fs, v)
  3725.           elseif op == "OPR_CONCAT" then
  3726.             self:exp2nextreg(fs, v)  -- operand must be on the 'stack'
  3727.           elseif op == "OPR_ADD" or op == "OPR_SUB" or
  3728.                  op == "OPR_MUL" or op == "OPR_DIV" or
  3729.                  op == "OPR_MOD" or op == "OPR_POW" then
  3730.             if not self:isnumeral(v) then self:exp2RK(fs, v) end
  3731.           else
  3732.             self:exp2RK(fs, v)
  3733.           end
  3734.         end
  3735.        
  3736.         ------------------------------------------------------------------------
  3737.         --
  3738.         -- * used only in (lparser) luaY:subexpr()
  3739.         ------------------------------------------------------------------------
  3740.         -- table lookups to simplify testing
  3741.         luaK.arith_op = {
  3742.           OPR_ADD = "OP_ADD", OPR_SUB = "OP_SUB", OPR_MUL = "OP_MUL",
  3743.           OPR_DIV = "OP_DIV", OPR_MOD = "OP_MOD", OPR_POW = "OP_POW",
  3744.         }
  3745.         luaK.comp_op = {
  3746.           OPR_EQ = "OP_EQ", OPR_NE = "OP_EQ", OPR_LT = "OP_LT",
  3747.           OPR_LE = "OP_LE", OPR_GT = "OP_LT", OPR_GE = "OP_LE",
  3748.         }
  3749.         luaK.comp_cond = {
  3750.           OPR_EQ = 1, OPR_NE = 0, OPR_LT = 1,
  3751.           OPR_LE = 1, OPR_GT = 0, OPR_GE = 0,
  3752.         }
  3753.         function luaK:posfix(fs, op, e1, e2)
  3754.           -- needed because e1 = e2 doesn't copy values...
  3755.           -- * in 5.0.x, only k/info/aux/t/f copied, t for AND, f for OR
  3756.           --   but here, all elements are copied for completeness' sake
  3757.           local function copyexp(e1, e2)
  3758.             e1.k = e2.k
  3759.             e1.info = e2.info; e1.aux = e2.aux
  3760.             e1.nval = e2.nval
  3761.             e1.t = e2.t; e1.f = e2.f
  3762.           end
  3763.           if op == "OPR_AND" then
  3764.             assert(e1.t == self.NO_JUMP)  -- list must be closed
  3765.             self:dischargevars(fs, e2)
  3766.             e2.f = self:concat(fs, e2.f, e1.f)
  3767.             copyexp(e1, e2)
  3768.           elseif op == "OPR_OR" then
  3769.             assert(e1.f == self.NO_JUMP)  -- list must be closed
  3770.             self:dischargevars(fs, e2)
  3771.             e2.t = self:concat(fs, e2.t, e1.t)
  3772.             copyexp(e1, e2)
  3773.           elseif op == "OPR_CONCAT" then
  3774.             self:exp2val(fs, e2)
  3775.             if e2.k == "VRELOCABLE" and luaP:GET_OPCODE(self:getcode(fs, e2)) == "OP_CONCAT" then
  3776.               assert(e1.info == luaP:GETARG_B(self:getcode(fs, e2)) - 1)
  3777.               self:freeexp(fs, e1)
  3778.               luaP:SETARG_B(self:getcode(fs, e2), e1.info)
  3779.               e1.k = "VRELOCABLE"
  3780.               e1.info = e2.info
  3781.             else
  3782.               self:exp2nextreg(fs, e2)  -- operand must be on the 'stack'
  3783.               self:codearith(fs, "OP_CONCAT", e1, e2)
  3784.             end
  3785.           else
  3786.             -- the following uses a table lookup in place of conditionals
  3787.             local arith = self.arith_op[op]
  3788.             if arith then
  3789.               self:codearith(fs, arith, e1, e2)
  3790.             else
  3791.               local comp = self.comp_op[op]
  3792.               if comp then
  3793.                 self:codecomp(fs, comp, self.comp_cond[op], e1, e2)
  3794.               else
  3795.                 assert(0)
  3796.               end
  3797.             end--if arith
  3798.           end--if op
  3799.         end
  3800.        
  3801.         ------------------------------------------------------------------------
  3802.         -- adjusts debug information for last instruction written, in order to
  3803.         -- change the line where item comes into existence
  3804.         -- * used in (lparser) luaY:funcargs(), luaY:forbody(), luaY:funcstat()
  3805.         ------------------------------------------------------------------------
  3806.         function luaK:fixline(fs, line)
  3807.           fs.f.lineinfo[fs.pc - 1] = line
  3808.         end
  3809.        
  3810.         ------------------------------------------------------------------------
  3811.         -- general function to write an instruction into the instruction buffer,
  3812.         -- sets debug information too
  3813.         -- * used in luaK:codeABC(), luaK:codeABx()
  3814.         -- * called directly by (lparser) luaY:whilestat()
  3815.         ------------------------------------------------------------------------
  3816.         function luaK:code(fs, i, line)
  3817.           local f = fs.f
  3818.           self:dischargejpc(fs)  -- 'pc' will change
  3819.           -- put new instruction in code array
  3820.           luaY:growvector(fs.L, f.code, fs.pc, f.sizecode, nil,
  3821.                           luaY.MAX_INT, "code size overflow")
  3822.           f.code[fs.pc] = i
  3823.           -- save corresponding line information
  3824.           luaY:growvector(fs.L, f.lineinfo, fs.pc, f.sizelineinfo, nil,
  3825.                           luaY.MAX_INT, "code size overflow")
  3826.           f.lineinfo[fs.pc] = line
  3827.           local pc = fs.pc
  3828.           fs.pc = fs.pc + 1
  3829.           return pc
  3830.         end
  3831.        
  3832.         ------------------------------------------------------------------------
  3833.         -- writes an instruction of type ABC
  3834.         -- * calls luaK:code()
  3835.         ------------------------------------------------------------------------
  3836.         function luaK:codeABC(fs, o, a, b, c)
  3837.           assert(luaP:getOpMode(o) == luaP.OpMode.iABC)
  3838.           assert(luaP:getBMode(o) ~= luaP.OpArgMask.OpArgN or b == 0)
  3839.           assert(luaP:getCMode(o) ~= luaP.OpArgMask.OpArgN or c == 0)
  3840.           return self:code(fs, luaP:CREATE_ABC(o, a, b, c), fs.ls.lastline)
  3841.         end
  3842.        
  3843.         ------------------------------------------------------------------------
  3844.         -- writes an instruction of type ABx
  3845.         -- * calls luaK:code(), called by luaK:codeAsBx()
  3846.         ------------------------------------------------------------------------
  3847.         function luaK:codeABx(fs, o, a, bc)
  3848.           assert(luaP:getOpMode(o) == luaP.OpMode.iABx or
  3849.                      luaP:getOpMode(o) == luaP.OpMode.iAsBx)
  3850.           assert(luaP:getCMode(o) == luaP.OpArgMask.OpArgN)
  3851.           return self:code(fs, luaP:CREATE_ABx(o, a, bc), fs.ls.lastline)
  3852.         end
  3853.        
  3854.         ------------------------------------------------------------------------
  3855.         --
  3856.         -- * used in (lparser) luaY:closelistfield(), luaY:lastlistfield()
  3857.         ------------------------------------------------------------------------
  3858.         function luaK:setlist(fs, base, nelems, tostore)
  3859.           local c = math.floor((nelems - 1)/luaP.LFIELDS_PER_FLUSH) + 1
  3860.           local b = (tostore == luaY.LUA_MULTRET) and 0 or tostore
  3861.           assert(tostore ~= 0)
  3862.           if c <= luaP.MAXARG_C then
  3863.             self:codeABC(fs, "OP_SETLIST", base, b, c)
  3864.           else
  3865.             self:codeABC(fs, "OP_SETLIST", base, b, 0)
  3866.             self:code(fs, luaP:CREATE_Inst(c), fs.ls.lastline)
  3867.           end
  3868.           fs.freereg = base + 1  -- free registers with list values
  3869.         end
  3870.        
  3871.         return function(a) luaY = a return luaK end
  3872.     end
  3873.     fake_module_scripts[script] = module_script
  3874. end
  3875. do -- nil.LuaU
  3876.     local script = Instance.new('ModuleScript', nil)
  3877.     script.Name = "LuaU"
  3878.     local function module_script()
  3879.         --[[--------------------------------------------------------------------
  3880.        
  3881.           ldump.lua
  3882.           Save precompiled Lua chunks
  3883.           This file is part of Yueliang.
  3884.        
  3885.           Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
  3886.           The COPYRIGHT file describes the conditions
  3887.           under which this software may be distributed.
  3888.        
  3889.           See the ChangeLog for more information.
  3890.        
  3891.         ----------------------------------------------------------------------]]
  3892.        
  3893.         --[[--------------------------------------------------------------------
  3894.         -- Notes:
  3895.         -- * WARNING! byte order (little endian) and data type sizes for header
  3896.         --   signature values hard-coded; see luaU:header
  3897.         -- * chunk writer generators are included, see below
  3898.         -- * one significant difference is that instructions are still in table
  3899.         --   form (with OP/A/B/C/Bx fields) and luaP:Instruction() is needed to
  3900.         --   convert them into 4-char strings
  3901.         --
  3902.         -- Not implemented:
  3903.         -- * DumpVar, DumpMem has been removed
  3904.         -- * DumpVector folded into folded into DumpDebug, DumpCode
  3905.         --
  3906.         -- Added:
  3907.         -- * for convenience, the following two functions have been added:
  3908.         --   luaU:make_setS: create a chunk writer that writes to a string
  3909.         --   luaU:make_setF: create a chunk writer that writes to a file
  3910.         --   (lua.h contains a typedef for lua_Writer/lua_Chunkwriter, and
  3911.         --    a Lua-based implementation exists, writer() in lstrlib.c)
  3912.         -- * luaU:ttype(o) (from lobject.h)
  3913.         -- * for converting number types to its binary equivalent:
  3914.         --   luaU:from_double(x): encode double value for writing
  3915.         --   luaU:from_int(x): encode integer value for writing
  3916.         --     (error checking is limited for these conversion functions)
  3917.         --     (double conversion does not support denormals or NaNs)
  3918.         --
  3919.         -- Changed in 5.1.x:
  3920.         -- * the dumper was mostly rewritten in Lua 5.1.x, so notes on the
  3921.         --   differences between 5.0.x and 5.1.x is limited
  3922.         -- * LUAC_VERSION bumped to 0x51, LUAC_FORMAT added
  3923.         -- * developer is expected to adjust LUAC_FORMAT in order to identify
  3924.         --   non-standard binary chunk formats
  3925.         -- * header signature code is smaller, has been simplified, and is
  3926.         --   tested as a single unit; its logic is shared with the undumper
  3927.         -- * no more endian conversion, invalid endianness mean rejection
  3928.         -- * opcode field sizes are no longer exposed in the header
  3929.         -- * code moved to front of a prototype, followed by constants
  3930.         -- * debug information moved to the end of the binary chunk, and the
  3931.         --   relevant functions folded into a single function
  3932.         -- * luaU:dump returns a writer status code
  3933.         -- * chunk writer now implements status code because dumper uses it
  3934.         -- * luaU:endianness removed
  3935.         ----------------------------------------------------------------------]]
  3936.        
  3937.         --requires luaP
  3938.         local luaU = {}
  3939.         local luaP = require(script.Parent.LuaP)
  3940.        
  3941.         -- mark for precompiled code ('<esc>Lua') (from lua.h)
  3942.         luaU.LUA_SIGNATURE = "\27Lua"
  3943.        
  3944.         -- constants used by dumper (from lua.h)
  3945.         luaU.LUA_TNUMBER  = 3
  3946.         luaU.LUA_TSTRING  = 4
  3947.         luaU.LUA_TNIL     = 0
  3948.         luaU.LUA_TBOOLEAN = 1
  3949.         luaU.LUA_TNONE    = -1
  3950.        
  3951.         -- constants for header of binary files (from lundump.h)
  3952.         luaU.LUAC_VERSION    = 0x51     -- this is Lua 5.1
  3953.         luaU.LUAC_FORMAT     = 0        -- this is the official format
  3954.         luaU.LUAC_HEADERSIZE = 12       -- size of header of binary files
  3955.        
  3956.         --[[--------------------------------------------------------------------
  3957.         -- Additional functions to handle chunk writing
  3958.         -- * to use make_setS and make_setF, see test_ldump.lua elsewhere
  3959.         ----------------------------------------------------------------------]]
  3960.        
  3961.         ------------------------------------------------------------------------
  3962.         -- create a chunk writer that writes to a string
  3963.         -- * returns the writer function and a table containing the string
  3964.         -- * to get the final result, look in buff.data
  3965.         ------------------------------------------------------------------------
  3966.         function luaU:make_setS()
  3967.           local buff = {}
  3968.                 buff.data = ""
  3969.           local writer =
  3970.             function(s, buff)  -- chunk writer
  3971.               if not s then return 0 end
  3972.               buff.data = buff.data..s
  3973.               return 0
  3974.             end
  3975.           return writer, buff
  3976.         end
  3977.        
  3978.         ------------------------------------------------------------------------
  3979.         -- create a chunk writer that writes to a file
  3980.         -- * returns the writer function and a table containing the file handle
  3981.         -- * if a nil is passed, then writer should close the open file
  3982.         ------------------------------------------------------------------------
  3983.        
  3984.         --[[
  3985.         function luaU:make_setF(filename)
  3986.           local buff = {}
  3987.                 buff.h = io.open(filename, "wb")
  3988.           if not buff.h then return nil end
  3989.           local writer =
  3990.             function(s, buff)  -- chunk writer
  3991.               if not buff.h then return 0 end
  3992.               if not s then
  3993.                 if buff.h:close() then return 0 end
  3994.               else
  3995.                 if buff.h:write(s) then return 0 end
  3996.               end
  3997.               return 1
  3998.             end
  3999.           return writer, buff
  4000.         end--]]
  4001.        
  4002.         ------------------------------------------------------------------------
  4003.         -- works like the lobject.h version except that TObject used in these
  4004.         -- scripts only has a 'value' field, no 'tt' field (native types used)
  4005.         ------------------------------------------------------------------------
  4006.         function luaU:ttype(o)
  4007.           local tt = type(o.value)
  4008.           if tt == "number" then return self.LUA_TNUMBER
  4009.           elseif tt == "string" then return self.LUA_TSTRING
  4010.           elseif tt == "nil" then return self.LUA_TNIL
  4011.           elseif tt == "boolean" then return self.LUA_TBOOLEAN
  4012.           else
  4013.             return self.LUA_TNONE  -- the rest should not appear
  4014.           end
  4015.         end
  4016.        
  4017.         -----------------------------------------------------------------------
  4018.         -- converts a IEEE754 double number to an 8-byte little-endian string
  4019.         -- * luaU:from_double() and luaU:from_int() are adapted from ChunkBake
  4020.         -- * supports +/- Infinity, but not denormals or NaNs
  4021.         -----------------------------------------------------------------------
  4022.         function luaU:from_double(x)
  4023.           local function grab_byte(v)
  4024.             local c = v % 256
  4025.             return (v - c) / 256, string.char(c)
  4026.           end
  4027.           local sign = 0
  4028.           if x < 0 then sign = 1; x = -x end
  4029.           local mantissa, exponent = math.frexp(x)
  4030.           if x == 0 then -- zero
  4031.             mantissa, exponent = 0, 0
  4032.           elseif x == 1/0 then
  4033.             mantissa, exponent = 0, 2047
  4034.           else
  4035.             mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, 53)
  4036.             exponent = exponent + 1022
  4037.           end
  4038.           local v, byte = "" -- convert to bytes
  4039.           x = math.floor(mantissa)
  4040.           for i = 1,6 do
  4041.             x, byte = grab_byte(x); v = v..byte -- 47:0
  4042.           end
  4043.           x, byte = grab_byte(exponent * 16 + x); v = v..byte -- 55:48
  4044.           x, byte = grab_byte(sign * 128 + x); v = v..byte -- 63:56
  4045.           return v
  4046.         end
  4047.        
  4048.         -----------------------------------------------------------------------
  4049.         -- converts a number to a little-endian 32-bit integer string
  4050.         -- * input value assumed to not overflow, can be signed/unsigned
  4051.         -----------------------------------------------------------------------
  4052.         function luaU:from_int(x)
  4053.           local v = ""
  4054.           x = math.floor(x)
  4055.           if x < 0 then x = 4294967296 + x end  -- ULONG_MAX+1
  4056.           for i = 1, 4 do
  4057.             local c = x % 256
  4058.             v = v..string.char(c); x = math.floor(x / 256)
  4059.           end
  4060.           return v
  4061.         end
  4062.        
  4063.         --[[--------------------------------------------------------------------
  4064.         -- Functions to make a binary chunk
  4065.         -- * many functions have the size parameter removed, since output is
  4066.         --   in the form of a string and some sizes are implicit or hard-coded
  4067.         ----------------------------------------------------------------------]]
  4068.        
  4069.         --[[--------------------------------------------------------------------
  4070.         -- struct DumpState:
  4071.         --   L  -- lua_State (not used in this script)
  4072.         --   writer  -- lua_Writer (chunk writer function)
  4073.         --   data  -- void* (chunk writer context or data already written)
  4074.         --   strip  -- if true, don't write any debug information
  4075.         --   status  -- if non-zero, an error has occured
  4076.         ----------------------------------------------------------------------]]
  4077.        
  4078.         ------------------------------------------------------------------------
  4079.         -- dumps a block of bytes
  4080.         -- * lua_unlock(D.L), lua_lock(D.L) unused
  4081.         ------------------------------------------------------------------------
  4082.         function luaU:DumpBlock(b, D)
  4083.           if D.status == 0 then
  4084.             -- lua_unlock(D->L);
  4085.             D.status = D.write(b, D.data)
  4086.             -- lua_lock(D->L);
  4087.           end
  4088.         end
  4089.        
  4090.         ------------------------------------------------------------------------
  4091.         -- dumps a char
  4092.         ------------------------------------------------------------------------
  4093.         function luaU:DumpChar(y, D)
  4094.           self:DumpBlock(string.char(y), D)
  4095.         end
  4096.        
  4097.         ------------------------------------------------------------------------
  4098.         -- dumps a 32-bit signed or unsigned integer (for int) (hard-coded)
  4099.         ------------------------------------------------------------------------
  4100.         function luaU:DumpInt(x, D)
  4101.           self:DumpBlock(self:from_int(x), D)
  4102.         end
  4103.        
  4104.         ------------------------------------------------------------------------
  4105.         -- dumps a lua_Number (hard-coded as a double)
  4106.         ------------------------------------------------------------------------
  4107.         function luaU:DumpNumber(x, D)
  4108.           self:DumpBlock(self:from_double(x), D)
  4109.         end
  4110.        
  4111.         ------------------------------------------------------------------------
  4112.         -- dumps a Lua string (size type is hard-coded)
  4113.         ------------------------------------------------------------------------
  4114.         function luaU:DumpString(s, D)
  4115.           if s == nil then
  4116.             self:DumpInt(0, D)
  4117.           else
  4118.             s = s.."\0"  -- include trailing '\0'
  4119.             self:DumpInt(#s, D)
  4120.             self:DumpBlock(s, D)
  4121.           end
  4122.         end
  4123.        
  4124.         ------------------------------------------------------------------------
  4125.         -- dumps instruction block from function prototype
  4126.         ------------------------------------------------------------------------
  4127.         function luaU:DumpCode(f, D)
  4128.           local n = f.sizecode
  4129.           --was DumpVector
  4130.           self:DumpInt(n, D)
  4131.           for i = 0, n - 1 do
  4132.             self:DumpBlock(luaP:Instruction(f.code[i]), D)
  4133.           end
  4134.         end
  4135.        
  4136.         ------------------------------------------------------------------------
  4137.         -- dump constant pool from function prototype
  4138.         -- * bvalue(o), nvalue(o) and rawtsvalue(o) macros removed
  4139.         ------------------------------------------------------------------------
  4140.         function luaU:DumpConstants(f, D)
  4141.           local n = f.sizek
  4142.           self:DumpInt(n, D)
  4143.           for i = 0, n - 1 do
  4144.             local o = f.k[i]  -- TValue
  4145.             local tt = self:ttype(o)
  4146.             self:DumpChar(tt, D)
  4147.             if tt == self.LUA_TNIL then
  4148.             elseif tt == self.LUA_TBOOLEAN then
  4149.               self:DumpChar(o.value and 1 or 0, D)
  4150.             elseif tt == self.LUA_TNUMBER then
  4151.               self:DumpNumber(o.value, D)
  4152.             elseif tt == self.LUA_TSTRING then
  4153.               self:DumpString(o.value, D)
  4154.             else
  4155.               --lua_assert(0)  -- cannot happen
  4156.             end
  4157.           end
  4158.           n = f.sizep
  4159.           self:DumpInt(n, D)
  4160.           for i = 0, n - 1 do
  4161.             self:DumpFunction(f.p[i], f.source, D)
  4162.           end
  4163.         end
  4164.        
  4165.         ------------------------------------------------------------------------
  4166.         -- dump debug information
  4167.         ------------------------------------------------------------------------
  4168.         function luaU:DumpDebug(f, D)
  4169.           local n
  4170.           n = D.strip and 0 or f.sizelineinfo           -- dump line information
  4171.           --was DumpVector
  4172.           self:DumpInt(n, D)
  4173.           for i = 0, n - 1 do
  4174.             self:DumpInt(f.lineinfo[i], D)
  4175.           end
  4176.           n = D.strip and 0 or f.sizelocvars            -- dump local information
  4177.           self:DumpInt(n, D)
  4178.           for i = 0, n - 1 do
  4179.             self:DumpString(f.locvars[i].varname, D)
  4180.             self:DumpInt(f.locvars[i].startpc, D)
  4181.             self:DumpInt(f.locvars[i].endpc, D)
  4182.           end
  4183.           n = D.strip and 0 or f.sizeupvalues           -- dump upvalue information
  4184.           self:DumpInt(n, D)
  4185.           for i = 0, n - 1 do
  4186.             self:DumpString(f.upvalues[i], D)
  4187.           end
  4188.         end
  4189.        
  4190.         ------------------------------------------------------------------------
  4191.         -- dump child function prototypes from function prototype
  4192.         ------------------------------------------------------------------------
  4193.         function luaU:DumpFunction(f, p, D)
  4194.           local source = f.source
  4195.           if source == p or D.strip then source = nil end
  4196.           self:DumpString(source, D)
  4197.           self:DumpInt(f.lineDefined, D)
  4198.           self:DumpInt(f.lastlinedefined, D)
  4199.           self:DumpChar(f.nups, D)
  4200.           self:DumpChar(f.numparams, D)
  4201.           self:DumpChar(f.is_vararg, D)
  4202.           self:DumpChar(f.maxstacksize, D)
  4203.           self:DumpCode(f, D)
  4204.           self:DumpConstants(f, D)
  4205.           self:DumpDebug(f, D)
  4206.         end
  4207.        
  4208.         ------------------------------------------------------------------------
  4209.         -- dump Lua header section (some sizes hard-coded)
  4210.         ------------------------------------------------------------------------
  4211.         function luaU:DumpHeader(D)
  4212.           local h = self:header()
  4213.           assert(#h == self.LUAC_HEADERSIZE) -- fixed buffer now an assert
  4214.           self:DumpBlock(h, D)
  4215.         end
  4216.        
  4217.         ------------------------------------------------------------------------
  4218.         -- make header (from lundump.c)
  4219.         -- returns the header string
  4220.         ------------------------------------------------------------------------
  4221.         function luaU:header()
  4222.          local x = 1
  4223.          return self.LUA_SIGNATURE..
  4224.                 string.char(
  4225.                   self.LUAC_VERSION,
  4226.                   self.LUAC_FORMAT,
  4227.                   x,                    -- endianness (1=little)
  4228.                   4,                    -- sizeof(int)
  4229.                   4,                    -- sizeof(size_t)
  4230.                   4,                    -- sizeof(Instruction)
  4231.                   8,                    -- sizeof(lua_Number)
  4232.                   0)                    -- is lua_Number integral?
  4233.         end
  4234.        
  4235.         ------------------------------------------------------------------------
  4236.         -- dump Lua function as precompiled chunk
  4237.         -- (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
  4238.         -- * w, data are created from make_setS, make_setF
  4239.         ------------------------------------------------------------------------
  4240.         function luaU:dump(L, f, w, data, strip)
  4241.           local D = {}  -- DumpState
  4242.           D.L = L
  4243.           D.write = w
  4244.           D.data = data
  4245.           D.strip = strip
  4246.           D.status = 0
  4247.           self:DumpHeader(D)
  4248.           self:DumpFunction(f, nil, D)
  4249.           -- added: for a chunk writer writing to a file, this final call with
  4250.           -- nil data is to indicate to the writer to close the file
  4251.           D.write(nil, D.data)
  4252.           return D.status
  4253.         end
  4254.        
  4255.         return luaU
  4256.     end
  4257.     fake_module_scripts[script] = module_script
  4258. end
  4259. do -- nil.LuaP
  4260.     local script = Instance.new('ModuleScript', nil)
  4261.     script.Name = "LuaP"
  4262.     local function module_script()
  4263.         --[[--------------------------------------------------------------------
  4264.        
  4265.           lopcodes.lua
  4266.           Lua 5 virtual machine opcodes in Lua
  4267.           This file is part of Yueliang.
  4268.        
  4269.           Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
  4270.           The COPYRIGHT file describes the conditions
  4271.           under which this software may be distributed.
  4272.        
  4273.           See the ChangeLog for more information.
  4274.        
  4275.         ----------------------------------------------------------------------]]
  4276.        
  4277.         --[[--------------------------------------------------------------------
  4278.         -- Notes:
  4279.         -- * an Instruction is a table with OP, A, B, C, Bx elements; this
  4280.         --   makes the code easy to follow and should allow instruction handling
  4281.         --   to work with doubles and ints
  4282.         -- * WARNING luaP:Instruction outputs instructions encoded in little-
  4283.         --   endian form and field size and positions are hard-coded
  4284.         --
  4285.         -- Not implemented:
  4286.         -- *
  4287.         --
  4288.         -- Added:
  4289.         -- * luaP:CREATE_Inst(c): create an inst from a number (for OP_SETLIST)
  4290.         -- * luaP:Instruction(i): convert field elements to a 4-char string
  4291.         -- * luaP:DecodeInst(x): convert 4-char string into field elements
  4292.         --
  4293.         -- Changed in 5.1.x:
  4294.         -- * POS_OP added, instruction field positions changed
  4295.         -- * some symbol names may have changed, e.g. LUAI_BITSINT
  4296.         -- * new operators for RK indices: BITRK, ISK(x), INDEXK(r), RKASK(x)
  4297.         -- * OP_MOD, OP_LEN is new
  4298.         -- * OP_TEST is now OP_TESTSET, OP_TEST is new
  4299.         -- * OP_FORLOOP, OP_TFORLOOP adjusted, OP_FORPREP is new
  4300.         -- * OP_TFORPREP deleted
  4301.         -- * OP_SETLIST and OP_SETLISTO merged and extended
  4302.         -- * OP_VARARG is new
  4303.         -- * many changes to implementation of OpMode data
  4304.         ----------------------------------------------------------------------]]
  4305.        
  4306.         local luaP = {}
  4307.        
  4308.         --[[
  4309.         ===========================================================================
  4310.           We assume that instructions are unsigned numbers.
  4311.           All instructions have an opcode in the first 6 bits.
  4312.           Instructions can have the following fields:
  4313.                 'A' : 8 bits
  4314.                 'B' : 9 bits
  4315.                 'C' : 9 bits
  4316.                 'Bx' : 18 bits ('B' and 'C' together)
  4317.                 'sBx' : signed Bx
  4318.        
  4319.           A signed argument is represented in excess K; that is, the number
  4320.           value is the unsigned value minus K. K is exactly the maximum value
  4321.           for that argument (so that -max is represented by 0, and +max is
  4322.           represented by 2*max), which is half the maximum for the corresponding
  4323.           unsigned argument.
  4324.         ===========================================================================
  4325.         --]]
  4326.        
  4327.         luaP.OpMode = { iABC = 0, iABx = 1, iAsBx = 2 }  -- basic instruction format
  4328.        
  4329.         ------------------------------------------------------------------------
  4330.         -- size and position of opcode arguments.
  4331.         -- * WARNING size and position is hard-coded elsewhere in this script
  4332.         ------------------------------------------------------------------------
  4333.         luaP.SIZE_C  = 9
  4334.         luaP.SIZE_B  = 9
  4335.         luaP.SIZE_Bx = luaP.SIZE_C + luaP.SIZE_B
  4336.         luaP.SIZE_A  = 8
  4337.        
  4338.         luaP.SIZE_OP = 6
  4339.        
  4340.         luaP.POS_OP = 0
  4341.         luaP.POS_A  = luaP.POS_OP + luaP.SIZE_OP
  4342.         luaP.POS_C  = luaP.POS_A + luaP.SIZE_A
  4343.         luaP.POS_B  = luaP.POS_C + luaP.SIZE_C
  4344.         luaP.POS_Bx = luaP.POS_C
  4345.        
  4346.         ------------------------------------------------------------------------
  4347.         -- limits for opcode arguments.
  4348.         -- we use (signed) int to manipulate most arguments,
  4349.         -- so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
  4350.         ------------------------------------------------------------------------
  4351.         -- removed "#if SIZE_Bx < BITS_INT-1" test, assume this script is
  4352.         -- running on a Lua VM with double or int as LUA_NUMBER
  4353.        
  4354.         luaP.MAXARG_Bx  = math.ldexp(1, luaP.SIZE_Bx) - 1
  4355.         luaP.MAXARG_sBx = math.floor(luaP.MAXARG_Bx / 2)  -- 'sBx' is signed
  4356.        
  4357.         luaP.MAXARG_A = math.ldexp(1, luaP.SIZE_A) - 1
  4358.         luaP.MAXARG_B = math.ldexp(1, luaP.SIZE_B) - 1
  4359.         luaP.MAXARG_C = math.ldexp(1, luaP.SIZE_C) - 1
  4360.        
  4361.         -- creates a mask with 'n' 1 bits at position 'p'
  4362.         -- MASK1(n,p) deleted, not required
  4363.         -- creates a mask with 'n' 0 bits at position 'p'
  4364.         -- MASK0(n,p) deleted, not required
  4365.        
  4366.         --[[--------------------------------------------------------------------
  4367.           Visual representation for reference:
  4368.        
  4369.            31    |    |     |            0      bit position
  4370.             +-----+-----+-----+----------+
  4371.             |  B  |  C  |  A  |  Opcode  |      iABC format
  4372.             +-----+-----+-----+----------+
  4373.             -  9  -  9  -  8  -    6     -      field sizes
  4374.             +-----+-----+-----+----------+
  4375.             |   [s]Bx   |  A  |  Opcode  |      iABx | iAsBx format
  4376.             +-----+-----+-----+----------+
  4377.        
  4378.         ----------------------------------------------------------------------]]
  4379.        
  4380.         ------------------------------------------------------------------------
  4381.         -- the following macros help to manipulate instructions
  4382.         -- * changed to a table object representation, very clean compared to
  4383.         --   the [nightmare] alternatives of using a number or a string
  4384.         -- * Bx is a separate element from B and C, since there is never a need
  4385.         --   to split Bx in the parser or code generator
  4386.         ------------------------------------------------------------------------
  4387.        
  4388.         -- these accept or return opcodes in the form of string names
  4389.         function luaP:GET_OPCODE(i) return self.ROpCode[i.OP] end
  4390.         function luaP:SET_OPCODE(i, o) i.OP = self.OpCode[o] end
  4391.        
  4392.         function luaP:GETARG_A(i) return i.A end
  4393.         function luaP:SETARG_A(i, u) i.A = u end
  4394.        
  4395.         function luaP:GETARG_B(i) return i.B end
  4396.         function luaP:SETARG_B(i, b) i.B = b end
  4397.        
  4398.         function luaP:GETARG_C(i) return i.C end
  4399.         function luaP:SETARG_C(i, b) i.C = b end
  4400.        
  4401.         function luaP:GETARG_Bx(i) return i.Bx end
  4402.         function luaP:SETARG_Bx(i, b) i.Bx = b end
  4403.        
  4404.         function luaP:GETARG_sBx(i) return i.Bx - self.MAXARG_sBx end
  4405.         function luaP:SETARG_sBx(i, b) i.Bx = b + self.MAXARG_sBx end
  4406.        
  4407.         function luaP:CREATE_ABC(o,a,b,c)
  4408.           return {OP = self.OpCode[o], A = a, B = b, C = c}
  4409.         end
  4410.        
  4411.         function luaP:CREATE_ABx(o,a,bc)
  4412.           return {OP = self.OpCode[o], A = a, Bx = bc}
  4413.         end
  4414.        
  4415.         ------------------------------------------------------------------------
  4416.         -- create an instruction from a number (for OP_SETLIST)
  4417.         ------------------------------------------------------------------------
  4418.         function luaP:CREATE_Inst(c)
  4419.           local o = c % 64
  4420.           c = (c - o) / 64
  4421.           local a = c % 256
  4422.           c = (c - a) / 256
  4423.           return self:CREATE_ABx(o, a, c)
  4424.         end
  4425.        
  4426.         ------------------------------------------------------------------------
  4427.         -- returns a 4-char string little-endian encoded form of an instruction
  4428.         ------------------------------------------------------------------------
  4429.         function luaP:Instruction(i)
  4430.           if i.Bx then
  4431.             -- change to OP/A/B/C format
  4432.             i.C = i.Bx % 512
  4433.             i.B = (i.Bx - i.C) / 512
  4434.           end
  4435.           local I = i.A * 64 + i.OP
  4436.           local c0 = I % 256
  4437.           I = i.C * 64 + (I - c0) / 256  -- 6 bits of A left
  4438.           local c1 = I % 256
  4439.           I = i.B * 128 + (I - c1) / 256  -- 7 bits of C left
  4440.           local c2 = I % 256
  4441.           local c3 = (I - c2) / 256
  4442.           return string.char(c0, c1, c2, c3)
  4443.         end
  4444.        
  4445.         ------------------------------------------------------------------------
  4446.         -- decodes a 4-char little-endian string into an instruction struct
  4447.         ------------------------------------------------------------------------
  4448.         function luaP:DecodeInst(x)
  4449.           local byte = string.byte
  4450.           local i = {}
  4451.           local I = byte(x, 1)
  4452.           local op = I % 64
  4453.           i.OP = op
  4454.           I = byte(x, 2) * 4 + (I - op) / 64  -- 2 bits of c0 left
  4455.           local a = I % 256
  4456.           i.A = a
  4457.           I = byte(x, 3) * 4 + (I - a) / 256  -- 2 bits of c1 left
  4458.           local c = I % 512
  4459.           i.C = c
  4460.           i.B = byte(x, 4) * 2 + (I - c) / 512 -- 1 bits of c2 left
  4461.           local opmode = self.OpMode[tonumber(string.sub(self.opmodes[op + 1], 7, 7))]
  4462.           if opmode ~= "iABC" then
  4463.             i.Bx = i.B * 512 + i.C
  4464.           end
  4465.           return i
  4466.         end
  4467.        
  4468.         ------------------------------------------------------------------------
  4469.         -- Macros to operate RK indices
  4470.         -- * these use arithmetic instead of bit ops
  4471.         ------------------------------------------------------------------------
  4472.        
  4473.         -- this bit 1 means constant (0 means register)
  4474.         luaP.BITRK = math.ldexp(1, luaP.SIZE_B - 1)
  4475.        
  4476.         -- test whether value is a constant
  4477.         function luaP:ISK(x) return x >= self.BITRK end
  4478.        
  4479.         -- gets the index of the constant
  4480.         function luaP:INDEXK(x) return x - self.BITRK end
  4481.        
  4482.         luaP.MAXINDEXRK = luaP.BITRK - 1
  4483.        
  4484.         -- code a constant index as a RK value
  4485.         function luaP:RKASK(x) return x + self.BITRK end
  4486.        
  4487.         ------------------------------------------------------------------------
  4488.         -- invalid register that fits in 8 bits
  4489.         ------------------------------------------------------------------------
  4490.         luaP.NO_REG = luaP.MAXARG_A
  4491.        
  4492.         ------------------------------------------------------------------------
  4493.         -- R(x) - register
  4494.         -- Kst(x) - constant (in constant table)
  4495.         -- RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
  4496.         ------------------------------------------------------------------------
  4497.        
  4498.         ------------------------------------------------------------------------
  4499.         -- grep "ORDER OP" if you change these enums
  4500.         ------------------------------------------------------------------------
  4501.        
  4502.         --[[--------------------------------------------------------------------
  4503.         Lua virtual machine opcodes (enum OpCode):
  4504.         ------------------------------------------------------------------------
  4505.         name          args    description
  4506.         ------------------------------------------------------------------------
  4507.         OP_MOVE       A B     R(A) := R(B)
  4508.         OP_LOADK      A Bx    R(A) := Kst(Bx)
  4509.         OP_LOADBOOL   A B C   R(A) := (Bool)B; if (C) pc++
  4510.         OP_LOADNIL    A B     R(A) := ... := R(B) := nil
  4511.         OP_GETUPVAL   A B     R(A) := UpValue[B]
  4512.         OP_GETGLOBAL  A Bx    R(A) := Gbl[Kst(Bx)]
  4513.         OP_GETTABLE   A B C   R(A) := R(B)[RK(C)]
  4514.         OP_SETGLOBAL  A Bx    Gbl[Kst(Bx)] := R(A)
  4515.         OP_SETUPVAL   A B     UpValue[B] := R(A)
  4516.         OP_SETTABLE   A B C   R(A)[RK(B)] := RK(C)
  4517.         OP_NEWTABLE   A B C   R(A) := {} (size = B,C)
  4518.         OP_SELF       A B C   R(A+1) := R(B); R(A) := R(B)[RK(C)]
  4519.         OP_ADD        A B C   R(A) := RK(B) + RK(C)
  4520.         OP_SUB        A B C   R(A) := RK(B) - RK(C)
  4521.         OP_MUL        A B C   R(A) := RK(B) * RK(C)
  4522.         OP_DIV        A B C   R(A) := RK(B) / RK(C)
  4523.         OP_MOD        A B C   R(A) := RK(B) % RK(C)
  4524.         OP_POW        A B C   R(A) := RK(B) ^ RK(C)
  4525.         OP_UNM        A B     R(A) := -R(B)
  4526.         OP_NOT        A B     R(A) := not R(B)
  4527.         OP_LEN        A B     R(A) := length of R(B)
  4528.         OP_CONCAT     A B C   R(A) := R(B).. ... ..R(C)
  4529.         OP_JMP        sBx     pc+=sBx
  4530.         OP_EQ         A B C   if ((RK(B) == RK(C)) ~= A) then pc++
  4531.         OP_LT         A B C   if ((RK(B) <  RK(C)) ~= A) then pc++
  4532.         OP_LE         A B C   if ((RK(B) <= RK(C)) ~= A) then pc++
  4533.         OP_TEST       A C     if not (R(A) <=> C) then pc++
  4534.         OP_TESTSET    A B C   if (R(B) <=> C) then R(A) := R(B) else pc++
  4535.         OP_CALL       A B C   R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1))
  4536.         OP_TAILCALL   A B C   return R(A)(R(A+1), ... ,R(A+B-1))
  4537.         OP_RETURN     A B     return R(A), ... ,R(A+B-2)  (see note)
  4538.         OP_FORLOOP    A sBx   R(A)+=R(A+2);
  4539.                               if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }
  4540.         OP_FORPREP    A sBx   R(A)-=R(A+2); pc+=sBx
  4541.         OP_TFORLOOP   A C     R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
  4542.                               if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++
  4543.         OP_SETLIST    A B C   R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B
  4544.         OP_CLOSE      A       close all variables in the stack up to (>=) R(A)
  4545.         OP_CLOSURE    A Bx    R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))
  4546.         OP_VARARG     A B     R(A), R(A+1), ..., R(A+B-1) = vararg
  4547.         ----------------------------------------------------------------------]]
  4548.        
  4549.         luaP.opnames = {}  -- opcode names
  4550.         luaP.OpCode = {}   -- lookup name -> number
  4551.         luaP.ROpCode = {}  -- lookup number -> name
  4552.        
  4553.         ------------------------------------------------------------------------
  4554.         -- ORDER OP
  4555.         ------------------------------------------------------------------------
  4556.         local i = 0
  4557.         for v in string.gmatch([[
  4558.         MOVE LOADK LOADBOOL LOADNIL GETUPVAL
  4559.         GETGLOBAL GETTABLE SETGLOBAL SETUPVAL SETTABLE
  4560.         NEWTABLE SELF ADD SUB MUL
  4561.         DIV MOD POW UNM NOT
  4562.         LEN CONCAT JMP EQ LT
  4563.         LE TEST TESTSET CALL TAILCALL
  4564.         RETURN FORLOOP FORPREP TFORLOOP SETLIST
  4565.         CLOSE CLOSURE VARARG
  4566.         ]], "%S+") do
  4567.           local n = "OP_"..v
  4568.           luaP.opnames[i] = v
  4569.           luaP.OpCode[n] = i
  4570.           luaP.ROpCode[i] = n
  4571.           i = i + 1
  4572.         end
  4573.         luaP.NUM_OPCODES = i
  4574.        
  4575.         --[[
  4576.         ===========================================================================
  4577.           Notes:
  4578.           (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
  4579.               and can be 0: OP_CALL then sets 'top' to last_result+1, so
  4580.               next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use 'top'.
  4581.           (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
  4582.               set top (like in OP_CALL with C == 0).
  4583.           (*) In OP_RETURN, if (B == 0) then return up to 'top'
  4584.           (*) In OP_SETLIST, if (B == 0) then B = 'top';
  4585.               if (C == 0) then next 'instruction' is real C
  4586.           (*) For comparisons, A specifies what condition the test should accept
  4587.               (true or false).
  4588.           (*) All 'skips' (pc++) assume that next instruction is a jump
  4589.         ===========================================================================
  4590.         --]]
  4591.        
  4592.         --[[--------------------------------------------------------------------
  4593.           masks for instruction properties. The format is:
  4594.           bits 0-1: op mode
  4595.           bits 2-3: C arg mode
  4596.           bits 4-5: B arg mode
  4597.           bit 6: instruction set register A
  4598.           bit 7: operator is a test
  4599.        
  4600.           for OpArgMask:
  4601.           OpArgN - argument is not used
  4602.           OpArgU - argument is used
  4603.           OpArgR - argument is a register or a jump offset
  4604.           OpArgK - argument is a constant or register/constant
  4605.         ----------------------------------------------------------------------]]
  4606.        
  4607.         -- was enum OpArgMask
  4608.         luaP.OpArgMask = { OpArgN = 0, OpArgU = 1, OpArgR = 2, OpArgK = 3 }
  4609.        
  4610.         ------------------------------------------------------------------------
  4611.         -- e.g. to compare with symbols, luaP:getOpMode(...) == luaP.OpCode.iABC
  4612.         -- * accepts opcode parameter as strings, e.g. "OP_MOVE"
  4613.         ------------------------------------------------------------------------
  4614.        
  4615.         function luaP:getOpMode(m)
  4616.           return self.opmodes[self.OpCode[m]] % 4
  4617.         end
  4618.        
  4619.         function luaP:getBMode(m)
  4620.           return math.floor(self.opmodes[self.OpCode[m]] / 16) % 4
  4621.         end
  4622.        
  4623.         function luaP:getCMode(m)
  4624.           return math.floor(self.opmodes[self.OpCode[m]] / 4) % 4
  4625.         end
  4626.        
  4627.         function luaP:testAMode(m)
  4628.           return math.floor(self.opmodes[self.OpCode[m]] / 64) % 2
  4629.         end
  4630.        
  4631.         function luaP:testTMode(m)
  4632.           return math.floor(self.opmodes[self.OpCode[m]] / 128)
  4633.         end
  4634.        
  4635.         -- luaP_opnames[] is set above, as the luaP.opnames table
  4636.        
  4637.         -- number of list items to accumulate before a SETLIST instruction
  4638.         luaP.LFIELDS_PER_FLUSH = 50
  4639.        
  4640.         ------------------------------------------------------------------------
  4641.         -- build instruction properties array
  4642.         -- * deliberately coded to look like the C equivalent
  4643.         ------------------------------------------------------------------------
  4644.         local function opmode(t, a, b, c, m)
  4645.           local luaP = luaP
  4646.           return t * 128 + a * 64 +
  4647.                  luaP.OpArgMask[b] * 16 + luaP.OpArgMask[c] * 4 + luaP.OpMode[m]
  4648.         end
  4649.        
  4650.         -- ORDER OP
  4651.         luaP.opmodes = {
  4652.         -- T A B C mode opcode
  4653.           opmode(0, 1, "OpArgK", "OpArgN", "iABx"),     -- OP_LOADK
  4654.           opmode(0, 1, "OpArgU", "OpArgU", "iABC"),     -- OP_LOADBOOL
  4655.           opmode(0, 1, "OpArgR", "OpArgN", "iABC"),     -- OP_LOADNIL
  4656.           opmode(0, 1, "OpArgU", "OpArgN", "iABC"),     -- OP_GETUPVAL
  4657.           opmode(0, 1, "OpArgK", "OpArgN", "iABx"),     -- OP_GETGLOBAL
  4658.           opmode(0, 1, "OpArgR", "OpArgK", "iABC"),     -- OP_GETTABLE
  4659.           opmode(0, 0, "OpArgK", "OpArgN", "iABx"),     -- OP_SETGLOBAL
  4660.           opmode(0, 0, "OpArgU", "OpArgN", "iABC"),     -- OP_SETUPVAL
  4661.           opmode(0, 0, "OpArgK", "OpArgK", "iABC"),     -- OP_SETTABLE
  4662.           opmode(0, 1, "OpArgU", "OpArgU", "iABC"),     -- OP_NEWTABLE
  4663.           opmode(0, 1, "OpArgR", "OpArgK", "iABC"),     -- OP_SELF
  4664.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_ADD
  4665.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_SUB
  4666.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_MUL
  4667.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_DIV
  4668.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_MOD
  4669.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_POW
  4670.           opmode(0, 1, "OpArgR", "OpArgN", "iABC"),     -- OP_UNM
  4671.           opmode(0, 1, "OpArgR", "OpArgN", "iABC"),     -- OP_NOT
  4672.           opmode(0, 1, "OpArgR", "OpArgN", "iABC"),     -- OP_LEN
  4673.           opmode(0, 1, "OpArgR", "OpArgR", "iABC"),     -- OP_CONCAT
  4674.           opmode(0, 0, "OpArgR", "OpArgN", "iAsBx"),    -- OP_JMP
  4675.           opmode(1, 0, "OpArgK", "OpArgK", "iABC"),     -- OP_EQ
  4676.           opmode(1, 0, "OpArgK", "OpArgK", "iABC"),     -- OP_LT
  4677.           opmode(1, 0, "OpArgK", "OpArgK", "iABC"),     -- OP_LE
  4678.           opmode(1, 1, "OpArgR", "OpArgU", "iABC"),     -- OP_TEST
  4679.           opmode(1, 1, "OpArgR", "OpArgU", "iABC"),     -- OP_TESTSET
  4680.           opmode(0, 1, "OpArgU", "OpArgU", "iABC"),     -- OP_CALL
  4681.           opmode(0, 1, "OpArgU", "OpArgU", "iABC"),     -- OP_TAILCALL
  4682.           opmode(0, 0, "OpArgU", "OpArgN", "iABC"),     -- OP_RETURN
  4683.           opmode(0, 1, "OpArgR", "OpArgN", "iAsBx"),    -- OP_FORLOOP
  4684.           opmode(0, 1, "OpArgR", "OpArgN", "iAsBx"),    -- OP_FORPREP
  4685.           opmode(1, 0, "OpArgN", "OpArgU", "iABC"),     -- OP_TFORLOOP
  4686.           opmode(0, 0, "OpArgU", "OpArgU", "iABC"),     -- OP_SETLIST
  4687.           opmode(0, 0, "OpArgN", "OpArgN", "iABC"),     -- OP_CLOSE
  4688.           opmode(0, 1, "OpArgU", "OpArgN", "iABx"),     -- OP_CLOSURE
  4689.           opmode(0, 1, "OpArgU", "OpArgN", "iABC"),     -- OP_VARARG
  4690.         }
  4691.         -- an awkward way to set a zero-indexed table...
  4692.         luaP.opmodes[0] =
  4693.           opmode(0, 1, "OpArgR", "OpArgN", "iABC")      -- OP_MOVE
  4694.        
  4695.         return luaP
  4696.     end
  4697.     fake_module_scripts[script] = module_script
  4698. end
  4699. do -- nil.Rerubi
  4700.     local script = Instance.new('ModuleScript', nil)
  4701.     script.Name = "Rerubi"
  4702.     local function module_script()
  4703.         local Concat    = table.concat;
  4704.         local Select    = select;
  4705.         local _Byte     = string.byte;
  4706.         local Sub       = string.sub;
  4707.         local Opcode    = { -- Opcode types.
  4708.             'ABC',  'ABx',  'ABC',  'ABC';
  4709.             'ABC',  'ABx',  'ABC',  'ABx';
  4710.             'ABC',  'ABC',  'ABC',  'ABC';
  4711.             'ABC',  'ABC',  'ABC',  'ABC';
  4712.             'ABC',  'ABC',  'ABC',  'ABC';
  4713.             'ABC',  'ABC',  'AsBx', 'ABC';
  4714.             'ABC',  'ABC',  'ABC',  'ABC';
  4715.             'ABC',  'ABC',  'ABC',  'AsBx';
  4716.             'AsBx', 'ABC',  'ABC',  'ABC';
  4717.             'ABx',  'ABC';
  4718.         };
  4719.        
  4720.         -- rlbi author -> Rerumu
  4721.        
  4722.         --[[
  4723.             Features;
  4724.                 * Almost complete rework/rewrite
  4725.                 * Fast and performant
  4726.                 * Fixes to upvalues
  4727.                 * C Stack overflow fixes in opcodes
  4728.                 * Fixed missing/broken returns
  4729.                 * Numeric constant 0 is properly handled
  4730.                 * Formatted in a more readable manner
  4731.                 * Tailcalls and stack issues have been fixed
  4732.                 * CLOSE implemented
  4733.                 * SETLIST (extended) implemented
  4734.                 * VARARG fixes
  4735.         --]]
  4736.        
  4737.         local function gBit(Bit, Start, End) -- No tail-calls, yay.
  4738.             if End then -- Thanks to cntkillme for giving input on this shorter, better approach.
  4739.                 local Res   = (Bit / 2 ^ (Start - 1)) % 2 ^ ((End - 1) - (Start - 1) + 1);
  4740.        
  4741.                 return Res - Res % 1;
  4742.             else
  4743.                 local Plc = 2 ^ (Start - 1);
  4744.        
  4745.                 if (Bit % (Plc + Plc) >= Plc) then
  4746.                     return 1;
  4747.                 else
  4748.                     return 0;
  4749.                 end;
  4750.             end;
  4751.         end;
  4752.        
  4753.         local function GetMeaning(ByteString)
  4754.             local Pos   = 1;
  4755.             local gSizet;
  4756.             local gInt;
  4757.        
  4758.             local function gBits8() -- Get the next byte in the stream.
  4759.                 local F = _Byte(ByteString, Pos, Pos);
  4760.        
  4761.                 Pos = Pos + 1;
  4762.        
  4763.                 return F;
  4764.             end;
  4765.        
  4766.             local function gBits32()
  4767.                 local W, X, Y, Z    = _Byte(ByteString, Pos, Pos + 3);
  4768.        
  4769.                 Pos = Pos + 4;
  4770.        
  4771.                 return (Z * 16777216) + (Y * 65536) + (X * 256) + W;
  4772.             end;
  4773.        
  4774.             local function gBits64()
  4775.                 return gBits32() * 4294967296 + gBits32();
  4776.             end;
  4777.        
  4778.             local function gFloat()
  4779.                 local A, B  = gBits32(), gBits32();
  4780.        
  4781.                 if ((A + B) == 0) then
  4782.                     return 0; -- Float 0 tends to be very messy, so this is a temp fix until I figure out what's up.
  4783.                 else
  4784.                     return (-2 * gBit(B, 32) + 1) * (2 ^ (gBit(B, 21, 31) - 1023)) * ((gBit(B, 1, 20) * (2^32) + A) / (2 ^ 52) + 1);
  4785.                 end;
  4786.             end;
  4787.        
  4788.             local function gString(Len)
  4789.                 local Str;
  4790.        
  4791.                 if Len then
  4792.                     Str = Sub(ByteString, Pos, Pos + Len - 1);
  4793.        
  4794.                     Pos = Pos + Len;
  4795.                 else
  4796.                     Len = gSizet();
  4797.        
  4798.                     if (Len == 0) then return; end;
  4799.        
  4800.                     Str = Sub(ByteString, Pos, Pos + Len - 1);
  4801.        
  4802.                     Pos = Pos + Len;
  4803.                 end;
  4804.        
  4805.                 return Str;
  4806.             end;
  4807.        
  4808.             local function ChunkDecode()
  4809.                 local Instr = {};
  4810.                 local Const = {};
  4811.                 local Proto = {};
  4812.                 local Chunk = {
  4813.                     Instr   = Instr; -- Instructions
  4814.                     Const   = Const; -- Constants
  4815.                     Proto   = Proto; -- Prototypes
  4816.                     Lines   = {}; -- Lines
  4817.                     Name    = gString(); -- Grab name string.
  4818.                     FirstL  = gInt(); -- First line.
  4819.                     LastL   = gInt(); -- Last line.
  4820.                     Upvals  = gBits8(); -- Upvalue count.
  4821.                     Args    = gBits8(); -- Arg count.
  4822.                     Vargs   = gBits8(); -- Vararg type.
  4823.                     Stack   = gBits8(); -- Stack.
  4824.                 };
  4825.        
  4826.                 if Chunk.Name then
  4827.                     Chunk.Name  = Sub(Chunk.Name, 1, -2);
  4828.                 end;
  4829.        
  4830.                 for Idx = 1, gInt() do -- Loading instructions to the chunk.
  4831.                     local Data  = gBits32();
  4832.                     local Opco  = gBit(Data, 1, 6);
  4833.                     local Type  = Opcode[Opco + 1];
  4834.                     local Inst;
  4835.        
  4836.                     if Type then
  4837.                         Inst    = {
  4838.                             Enum    = Opco;
  4839.                             gBit(Data, 7, 14); -- Register A.
  4840.                         };
  4841.        
  4842.                         if (Type == 'ABC') then -- Most common, basic instruction type.
  4843.                             Inst[2] = gBit(Data, 24, 32);
  4844.                             Inst[3] = gBit(Data, 15, 23);
  4845.                         elseif (Type == 'ABx') then
  4846.                             Inst[2] = gBit(Data, 15, 32);
  4847.                         elseif (Type == 'AsBx') then
  4848.                             Inst[2] = gBit(Data, 15, 32) - 131071;
  4849.                         end;
  4850.                     else
  4851.                         Inst    = Data; -- Extended SETLIST
  4852.                     end;
  4853.        
  4854.                     Instr[Idx]  = Inst;
  4855.                 end;
  4856.        
  4857.                 for Idx = 1, gInt() do -- Load constants.
  4858.                     local Type  = gBits8();
  4859.                     local Cons;
  4860.        
  4861.                     if (Type == 1) then -- Boolean
  4862.                         Cons    = (gBits8() ~= 0);
  4863.                     elseif (Type == 3) then -- Float/Double
  4864.                         Cons    = gFloat();
  4865.                     elseif (Type == 4) then
  4866.                         Cons    = Sub(gString(), 1, -2);
  4867.                     end;
  4868.        
  4869.                     Const[Idx - 1]  = Cons;
  4870.                 end;
  4871.        
  4872.                 for Idx = 1, gInt() do -- Nested function prototypes.
  4873.                     Proto[Idx - 1]  = ChunkDecode();
  4874.                 end;
  4875.        
  4876.                 do -- Debugging
  4877.                     local Lines = Chunk.Lines;
  4878.        
  4879.                     for Idx = 1, gInt() do
  4880.                         Lines[Idx]  = gBits32();
  4881.                     end;
  4882.        
  4883.                     for Idx = 1, gInt() do -- Locals in stack.
  4884.                         gString(); -- Name of local.
  4885.                         gBits32(); -- Starting point.
  4886.                         gBits32(); -- End point.
  4887.                     end;
  4888.        
  4889.                     for Idx = 1, gInt() do -- Upvalues.
  4890.                         gString(); -- Name of upvalue.
  4891.                     end;
  4892.                 end;
  4893.        
  4894.                 return Chunk; -- Finished chunk.
  4895.             end;
  4896.        
  4897.             do -- Most of this chunk I was too lazy to reformat or change
  4898.                 assert(gString(4) == "\27Lua", "Lua bytecode expected.");
  4899.                 assert(gBits8() == 0x51, "Only Lua 5.1 is supported.");
  4900.        
  4901.                 gBits8(); -- Probably version control.
  4902.                 gBits8(); -- Is small endians.
  4903.        
  4904.                 local IntSize   = gBits8(); -- Int size
  4905.                 local Sizet     = gBits8(); -- size_t
  4906.        
  4907.                 if (IntSize == 4) then
  4908.                     gInt    = gBits32;
  4909.                 elseif (IntSize == 8) then
  4910.                     gInt    = gBits64;
  4911.                 else
  4912.                     error('Integer size not supported', 2);
  4913.                 end;
  4914.        
  4915.                 if (Sizet == 4) then
  4916.                     gSizet  = gBits32;
  4917.                 elseif (Sizet == 8) then
  4918.                     gSizet  = gBits64;
  4919.                 else
  4920.                     error('Sizet size not supported', 2);
  4921.                 end;
  4922.        
  4923.                 assert(gString(3) == "\4\8\0", "Unsupported bytecode target platform");
  4924.             end;
  4925.        
  4926.             return ChunkDecode();
  4927.         end;
  4928.        
  4929.         local function _Returns(...)
  4930.             return Select('#', ...), {...};
  4931.         end;
  4932.        
  4933.         local function Wrap(Chunk, Env, Upvalues)
  4934.             local Instr = Chunk.Instr;
  4935.             local Const = Chunk.Const;
  4936.             local Proto = Chunk.Proto;
  4937.        
  4938.             local function OnError(Err, Position) -- Handle your errors in whatever way.
  4939.                 local Name  = Chunk.Name or 'Code';
  4940.                 local Line  = Chunk.Lines[Position] or '?';
  4941.                 local Err   = Err:match'^.+:%s*(.+)' or Err;
  4942.        
  4943.                 error(string.format('%s (%s): %s', Name, Line, Err), 0);
  4944.             end;
  4945.        
  4946.             return function(...) -- Returned function to run bytecode chunk (Don't be stupid, you can't setfenv this to work your way).
  4947.                 local Upvalues  = Upvalues;
  4948.                 local Instr     = Instr;
  4949.                 local Const     = Const;
  4950.                 local Proto     = Proto;
  4951.        
  4952.                 local InstrPoint, Top   = 1, -1;
  4953.                 local Vararg, Varargsz  = {}, Select('#', ...) - 1;
  4954.        
  4955.                 local GStack    = {};
  4956.                 local Lupvals   = {};
  4957.                 local Stack     = setmetatable({}, {
  4958.                     __index     = GStack;
  4959.                     __newindex  = function(_, Key, Value)
  4960.                         if (Key > Top) and Value then
  4961.                             Top = Key;
  4962.                         end;
  4963.        
  4964.                         GStack[Key] = Value;
  4965.                     end;
  4966.                 });
  4967.        
  4968.                 local function Loop()
  4969.                     local Instr = Instr;
  4970.                     local Inst, Enum, A, B;
  4971.        
  4972.                     while true do
  4973.                         Inst        = Instr[InstrPoint];
  4974.                         Enum        = Inst.Enum;
  4975.                         InstrPoint  = InstrPoint + 1;
  4976.        
  4977.                         if (Enum == 0) then -- MOVE
  4978.                             Stack[Inst[1]]  = Stack[Inst[2]];
  4979.                         elseif (Enum == 1) then -- LOADK
  4980.                             Stack[Inst[1]]  = Const[Inst[2]];
  4981.                         elseif (Enum == 2) then -- LOADBOOL
  4982.                             Stack[Inst[1]]  = (Inst[2] ~= 0);
  4983.        
  4984.                             if (Inst[3] ~= 0) then
  4985.                                 InstrPoint  = InstrPoint + 1;
  4986.                             end;
  4987.                         elseif (Enum == 3) then -- LOADNIL
  4988.                             local Stk   = Stack;
  4989.        
  4990.                             for Idx = Inst[1], Inst[2] do
  4991.                                 Stk[Idx]    = nil;
  4992.                             end;
  4993.                         elseif (Enum == 4) then -- GETUPVAL
  4994.                             Stack[Inst[1]]  = Upvalues[Inst[2]];
  4995.                         elseif (Enum == 5) then -- GETGLOBAL
  4996.                             Stack[Inst[1]]  = Env[Const[Inst[2]]];
  4997.                         elseif (Enum == 6) then -- GETTABLE
  4998.                             local C     = Inst[3];
  4999.                             local Stk   = Stack;
  5000.        
  5001.                             if (C > 255) then
  5002.                                 C   = Const[C - 256];
  5003.                             else
  5004.                                 C   = Stk[C];
  5005.                             end;
  5006.        
  5007.                             Stk[Inst[1]]    = Stk[Inst[2]][C];
  5008.                         elseif (Enum == 7) then -- SETGLOBAL
  5009.                             Env[Const[Inst[2]]] = Stack[Inst[1]];
  5010.                         elseif (Enum == 8) then -- SETUPVAL
  5011.                             Upvalues[Inst[2]]   = Stack[Inst[1]];
  5012.                         elseif (Enum == 9) then -- SETTABLE
  5013.                             local B, C  = Inst[2], Inst[3];
  5014.                             local Stk   = Stack;
  5015.        
  5016.                             if (B > 255) then
  5017.                                 B   = Const[B - 256];
  5018.                             else
  5019.                                 B   = Stk[B];
  5020.                             end;
  5021.        
  5022.                             if (C > 255) then
  5023.                                 C   = Const[C - 256];
  5024.                             else
  5025.                                 C   = Stk[C];
  5026.                             end;
  5027.        
  5028.                             Stk[Inst[1]][B] = C;
  5029.                         elseif (Enum == 10) then -- NEWTABLE
  5030.                             Stack[Inst[1]]  = {};
  5031.                         elseif (Enum == 11) then -- SELF
  5032.                             local A     = Inst[1];
  5033.                             local B     = Inst[2];
  5034.                             local C     = Inst[3];
  5035.                             local Stk   = Stack;
  5036.        
  5037.                             B = Stk[B];
  5038.        
  5039.                             if (C > 255) then
  5040.                                 C   = Const[C - 256];
  5041.                             else
  5042.                                 C   = Stk[C];
  5043.                             end;
  5044.        
  5045.                             Stk[A + 1]  = B;
  5046.                             Stk[A]      = B[C];
  5047.                         elseif (Enum == 12) then -- ADD
  5048.                             local B = Inst[2];
  5049.                             local C = Inst[3];
  5050.                             local Stk, Con  = Stack, Const;
  5051.        
  5052.                             if (B > 255) then
  5053.                                 B   = Const[B - 256];
  5054.                             else
  5055.                                 B   = Stk[B];
  5056.                             end;
  5057.        
  5058.                             if (C > 255) then
  5059.                                 C   = Const[C - 256];
  5060.                             else
  5061.                                 C   = Stk[C];
  5062.                             end;
  5063.        
  5064.                             Stk[Inst[1]]    = B + C;
  5065.                         elseif (Enum == 13) then -- SUB
  5066.                             local B = Inst[2];
  5067.                             local C = Inst[3];
  5068.                             local Stk, Con  = Stack, Const;
  5069.        
  5070.                             if (B > 255) then
  5071.                                 B   = Const[B - 256];
  5072.                             else
  5073.                                 B   = Stk[B];
  5074.                             end;
  5075.        
  5076.                             if (C > 255) then
  5077.                                 C   = Const[C - 256];
  5078.                             else
  5079.                                 C   = Stk[C];
  5080.                             end;
  5081.        
  5082.                             Stk[Inst[1]]    = B - C;
  5083.                         elseif (Enum == 14) then -- MUL
  5084.                             local B = Inst[2];
  5085.                             local C = Inst[3];
  5086.                             local Stk, Con  = Stack, Const;
  5087.        
  5088.                             if (B > 255) then
  5089.                                 B   = Const[B - 256];
  5090.                             else
  5091.                                 B   = Stk[B];
  5092.                             end;
  5093.        
  5094.                             if (C > 255) then
  5095.                                 C   = Const[C - 256];
  5096.                             else
  5097.                                 C   = Stk[C];
  5098.                             end;
  5099.        
  5100.                             Stk[Inst[1]]    = B * C;
  5101.                         elseif (Enum == 15) then -- DIV
  5102.                             local B = Inst[2];
  5103.                             local C = Inst[3];
  5104.                             local Stk, Con  = Stack, Const;
  5105.        
  5106.                             if (B > 255) then
  5107.                                 B   = Const[B - 256];
  5108.                             else
  5109.                                 B   = Stk[B];
  5110.                             end;
  5111.        
  5112.                             if (C > 255) then
  5113.                                 C   = Const[C - 256];
  5114.                             else
  5115.                                 C   = Stk[C];
  5116.                             end;
  5117.        
  5118.                             Stk[Inst[1]]    = B / C;
  5119.                         elseif (Enum == 16) then -- MOD
  5120.                             local B = Inst[2];
  5121.                             local C = Inst[3];
  5122.                             local Stk, Con  = Stack, Const;
  5123.        
  5124.                             if (B > 255) then
  5125.                                 B   = Const[B - 256];
  5126.                             else
  5127.                                 B   = Stk[B];
  5128.                             end;
  5129.        
  5130.                             if (C > 255) then
  5131.                                 C   = Const[C - 256];
  5132.                             else
  5133.                                 C   = Stk[C];
  5134.                             end;
  5135.        
  5136.                             Stk[Inst[1]]    = B % C;
  5137.                         elseif (Enum == 17) then -- POW
  5138.                             local B = Inst[2];
  5139.                             local C = Inst[3];
  5140.                             local Stk, Con  = Stack, Const;
  5141.        
  5142.                             if (B > 255) then
  5143.                                 B   = Const[B - 256];
  5144.                             else
  5145.                                 B   = Stk[B];
  5146.                             end;
  5147.        
  5148.                             if (C > 255) then
  5149.                                 C   = Const[C - 256];
  5150.                             else
  5151.                                 C   = Stk[C];
  5152.                             end;
  5153.        
  5154.                             Stk[Inst[1]]    = B ^ C;
  5155.                         elseif (Enum == 18) then -- UNM
  5156.                             Stack[Inst[1]]  = -Stack[Inst[2]];
  5157.                         elseif (Enum == 19) then -- NOT
  5158.                             Stack[Inst[1]]  = (not Stack[Inst[2]]);
  5159.                         elseif (Enum == 20) then -- LEN
  5160.                             Stack[Inst[1]]  = #Stack[Inst[2]];
  5161.                         elseif (Enum == 21) then -- CONCAT
  5162.                             local Stk   = Stack;
  5163.                             local B     = Inst[2];
  5164.                             local K     = {Stack[B]};
  5165.        
  5166.                             for Idx = B + 1, Inst[3] do
  5167.                                 K[#K + 1]   = Stk[Idx];
  5168.                             end;
  5169.        
  5170.                             Stack[Inst[1]]  = Concat(K);
  5171.                         elseif (Enum == 22) then -- JUMP
  5172.                             InstrPoint  = InstrPoint + Inst[2];
  5173.                         elseif (Enum == 23) then -- EQ
  5174.                             local A = Inst[1] ~= 0;
  5175.                             local B = Inst[2];
  5176.                             local C = Inst[3];
  5177.                             local Stk, Con  = Stack, Const;
  5178.        
  5179.                             if (B > 255) then
  5180.                                 B   = Const[B - 256];
  5181.                             else
  5182.                                 B   = Stk[B];
  5183.                             end;
  5184.        
  5185.                             if (C > 255) then
  5186.                                 C   = Const[C - 256];
  5187.                             else
  5188.                                 C   = Stk[C];
  5189.                             end;
  5190.        
  5191.                             if (B == C) ~= A then
  5192.                                 InstrPoint  = InstrPoint + 1;
  5193.                             end;
  5194.                         elseif (Enum == 24) then -- LT
  5195.                             local A = Inst[1] ~= 0;
  5196.                             local B = Inst[2];
  5197.                             local C = Inst[3];
  5198.                             local Stk, Con  = Stack, Const;
  5199.        
  5200.                             if (B > 255) then
  5201.                                 B   = Const[B - 256];
  5202.                             else
  5203.                                 B   = Stk[B];
  5204.                             end;
  5205.        
  5206.                             if (C > 255) then
  5207.                                 C   = Const[C - 256];
  5208.                             else
  5209.                                 C   = Stk[C];
  5210.                             end;
  5211.        
  5212.                             if (B < C) ~= A then
  5213.                                 InstrPoint  = InstrPoint + 1;
  5214.                             end;
  5215.                         elseif (Enum == 25) then -- LE
  5216.                             local A = Inst[1] ~= 0;
  5217.                             local B = Inst[2];
  5218.                             local C = Inst[3];
  5219.                             local Stk, Con  = Stack, Const;
  5220.        
  5221.                             if (B > 255) then
  5222.                                 B   = Const[B - 256];
  5223.                             else
  5224.                                 B   = Stk[B];
  5225.                             end;
  5226.        
  5227.                             if (C > 255) then
  5228.                                 C   = Const[C - 256];
  5229.                             else
  5230.                                 C   = Stk[C];
  5231.                             end;
  5232.        
  5233.                             if (B <= C) ~= A then
  5234.                                 InstrPoint  = InstrPoint + 1;
  5235.                             end;
  5236.                         elseif (Enum == 26) then -- TEST
  5237.                             if (not not Stack[Inst[1]]) == (Inst[3] == 0) then
  5238.                                 InstrPoint  = InstrPoint + 1;
  5239.                             end;
  5240.                         elseif (Enum == 27) then -- TESTSET
  5241.                             local B = Stack[Inst[2]];
  5242.        
  5243.                             if (not not B) == (Inst[3] == 0) then
  5244.                                 InstrPoint  = InstrPoint + 1;
  5245.                             else
  5246.                                 Stack[Inst[1]] = B;
  5247.                             end;
  5248.                         elseif (Enum == 28) then -- CALL
  5249.                             local A = Inst[1];
  5250.                             local B = Inst[2];
  5251.                             local C = Inst[3];
  5252.                             local Stk   = Stack;
  5253.                             local Args, Results;
  5254.                             local Limit, Loop;
  5255.        
  5256.                             Args    = {};
  5257.        
  5258.                             if (B ~= 1) then
  5259.                                 if (B ~= 0) then
  5260.                                     Limit = A + B - 1;
  5261.                                 else
  5262.                                     Limit = Top;
  5263.                                 end;
  5264.        
  5265.                                 Loop    = 0;
  5266.        
  5267.                                 for Idx = A + 1, Limit do
  5268.                                     Loop = Loop + 1;
  5269.        
  5270.                                     Args[Loop] = Stk[Idx];
  5271.                                 end;
  5272.        
  5273.                                 Limit, Results = _Returns(Stk[A](unpack(Args, 1, Limit - A)));
  5274.                             else
  5275.                                 Limit, Results = _Returns(Stk[A]());
  5276.                             end;
  5277.        
  5278.                             Top = A - 1;
  5279.        
  5280.                             if (C ~= 1) then
  5281.                                 if (C ~= 0) then
  5282.                                     Limit = A + C - 2;
  5283.                                 else
  5284.                                     Limit = Limit + A;
  5285.                                 end;
  5286.        
  5287.                                 Loop    = 0;
  5288.        
  5289.                                 for Idx = A, Limit do
  5290.                                     Loop = Loop + 1;
  5291.        
  5292.                                     Stk[Idx] = Results[Loop];
  5293.                                 end;
  5294.                             end;
  5295.                         elseif (Enum == 29) then -- TAILCALL
  5296.                             local A = Inst[1];
  5297.                             local B = Inst[2];
  5298.                             local C = Inst[3];
  5299.                             local Stk   = Stack;
  5300.                             local Args, Results;
  5301.                             local Limit, Loop;
  5302.        
  5303.                             Args = {};
  5304.        
  5305.                             if (B ~= 1) then
  5306.                                 if (B ~= 0) then
  5307.                                     Limit = A + B - 1;
  5308.                                 else
  5309.                                     Limit = Top;
  5310.                                 end
  5311.        
  5312.                                 Loop = 0;
  5313.        
  5314.                                 for Idx = A + 1, Limit do
  5315.                                     Loop = Loop + 1;
  5316.        
  5317.                                     Args[#Args + 1] = Stk[Idx];
  5318.                                 end
  5319.        
  5320.                                 Results = {Stk[A](unpack(Args, 1, Limit - A))};
  5321.                             else
  5322.                                 Results = {Stk[A]()};
  5323.                             end;
  5324.        
  5325.                             return Results;
  5326.                         elseif (Enum == 30) then -- RETURN
  5327.                             local A = Inst[1];
  5328.                             local B = Inst[2];
  5329.                             local Stk   = Stack;
  5330.                             local Loop, Output;
  5331.                             local Limit;
  5332.        
  5333.                             if (B == 1) then
  5334.                                 return;
  5335.                             elseif (B == 0) then
  5336.                                 Limit   = Top;
  5337.                             else
  5338.                                 Limit   = A + B - 2;
  5339.                             end;
  5340.        
  5341.                             Output = {};
  5342.        
  5343.                             local Loop  = 0;
  5344.        
  5345.                             for Idx = A, Limit do
  5346.                                 Loop    = Loop + 1;
  5347.        
  5348.                                 Output[Loop] = Stk[Idx];
  5349.                             end;
  5350.        
  5351.                             return Output;
  5352.                         elseif (Enum == 31) then -- FORLOOP
  5353.                             local A     = Inst[1];
  5354.                             local Stk   = Stack;
  5355.        
  5356.                             local Step  = Stk[A + 2];
  5357.                             local Index = Stk[A] + Step;
  5358.        
  5359.                             Stk[A]  = Index;
  5360.        
  5361.                             if (Step > 0) then
  5362.                                 if Index <= Stk[A + 1] then
  5363.                                     InstrPoint  = InstrPoint + Inst[2];
  5364.        
  5365.                                     Stk[A + 3] = Index;
  5366.                                 end;
  5367.                             else
  5368.                                 if Index >= Stk[A + 1] then
  5369.                                     InstrPoint  = InstrPoint + Inst[2];
  5370.        
  5371.                                     Stk[A + 3] = Index;
  5372.                                 end
  5373.                             end
  5374.                         elseif (Enum == 32) then -- FORPREP
  5375.                             local A     = Inst[1];
  5376.                             local Stk   = Stack;
  5377.        
  5378.                             Stk[A]  = Stk[A] - Stk[A + 2];
  5379.        
  5380.                             InstrPoint  = InstrPoint + Inst[2];
  5381.                         elseif (Enum == 33) then -- TFORLOOP
  5382.                             local A     = Inst[1];
  5383.                             local B     = Inst[2];
  5384.                             local C     = Inst[3];
  5385.                             local Stk   = Stack;
  5386.        
  5387.                             local Offset    = A + 2;
  5388.                             local Result    = {Stk[A](Stk[A + 1], Stk[A + 2])};
  5389.        
  5390.                             for Idx = 1, C do
  5391.                                 Stack[Offset + Idx] = Result[Idx];
  5392.                             end;
  5393.        
  5394.                             if (Stk[A + 3] ~= nil) then
  5395.                                 Stk[A + 2]  = Stk[A + 3];
  5396.                             else
  5397.                                 InstrPoint  = InstrPoint + 1;
  5398.                             end;
  5399.                         elseif (Enum == 34) then -- SETLIST
  5400.                             local A     = Inst[1];
  5401.                             local B     = Inst[2];
  5402.                             local C     = Inst[3];
  5403.                             local Stk   = Stack;
  5404.        
  5405.                             if (C == 0) then
  5406.                                 InstrPoint  = InstrPoint + 1;
  5407.                                 C           = Instr[InstrPoint]; -- This implementation was ambiguous! Will eventually re-test.
  5408.                             end;
  5409.        
  5410.                             local Offset    = (C - 1) * 50;
  5411.                             local T         = Stk[A]; -- Assuming T is the newly created table.
  5412.        
  5413.                             if (B == 0) then
  5414.                                 B   = Top;
  5415.                             end;
  5416.        
  5417.                             for Idx = 1, B do
  5418.                                 T[Offset + Idx] = Stk[A + Idx];
  5419.                             end;
  5420.                         elseif (Enum == 35) then -- CLOSE
  5421.                             local A     = Inst[1];
  5422.                             local Cls   = {}; -- Slight doubts on any issues this may cause
  5423.        
  5424.                             for Idx = 1, #Lupvals do
  5425.                                 local List = Lupvals[Idx];
  5426.        
  5427.                                 for Idz = 0, #List do
  5428.                                     local Upv   = List[Idz];
  5429.                                     local Stk   = Upv[1];
  5430.                                     local Pos   = Upv[2];
  5431.        
  5432.                                     if (Stk == Stack) and (Pos >= A) then
  5433.                                         Cls[Pos]    = Stk[Pos];
  5434.                                         Upv[1]      = Cls; -- @memcorrupt credit me for the spoonfeed
  5435.                                     end;
  5436.                                 end;
  5437.                             end;
  5438.                         elseif (Enum == 36) then -- CLOSURE
  5439.                             local Proto = Proto[Inst[2]];
  5440.                             local Instr = Instr;
  5441.                             local Stk   = Stack;
  5442.        
  5443.                             local Indexes;
  5444.                             local NewUvals;
  5445.        
  5446.                             if (Proto.Upvals ~= 0) then
  5447.                                 Indexes     = {};
  5448.                                 NewUvals    = setmetatable({}, {
  5449.                                         __index = function(_, Key)
  5450.                                             local Val   = Indexes[Key];
  5451.        
  5452.                                             return Val[1][Val[2]];
  5453.                                         end,
  5454.                                         __newindex = function(_, Key, Value)
  5455.                                             local Val   = Indexes[Key];
  5456.        
  5457.                                             Val[1][Val[2]]  = Value;
  5458.                                         end;
  5459.                                     }
  5460.                                 );
  5461.        
  5462.                                 for Idx = 1, Proto.Upvals do
  5463.                                     local Mvm   = Instr[InstrPoint];
  5464.        
  5465.                                     if (Mvm.Enum == 0) then -- MOVE
  5466.                                         Indexes[Idx - 1] = {Stk, Mvm[2]};
  5467.                                     elseif (Mvm.Enum == 4) then -- GETUPVAL
  5468.                                         Indexes[Idx - 1] = {Upvalues, Mvm[2]};
  5469.                                     end;
  5470.        
  5471.                                     InstrPoint  = InstrPoint + 1;
  5472.                                 end;
  5473.        
  5474.                                 Lupvals[#Lupvals + 1]   = Indexes;
  5475.                             end;
  5476.        
  5477.                             Stk[Inst[1]]            = Wrap(Proto, Env, NewUvals);
  5478.                         elseif (Enum == 37) then -- VARARG
  5479.                             local A = Inst[1];
  5480.                             local B = Inst[2];
  5481.                             local Stk, Vararg   = Stack, Vararg;
  5482.        
  5483.                             for Idx = A, A + (B > 0 and B - 1 or Varargsz) do
  5484.                                 Stk[Idx]    = Vararg[Idx - A];
  5485.                             end;
  5486.                         end;
  5487.                     end;
  5488.                 end;
  5489.        
  5490.                 local Args  = {...};
  5491.        
  5492.                 for Idx = 0, Varargsz do
  5493.                     Stack[Idx] = Args[Idx + 1];
  5494.        
  5495.                     if (Idx >= Chunk.Args) then
  5496.                         Vararg[Idx - Chunk.Args] = Args[Idx + 1];
  5497.                     end;
  5498.                 end;
  5499.        
  5500.                 local A, B      = pcall(Loop); -- Pcalling to allow yielding
  5501.        
  5502.                 if A then -- We're always expecting this to come out true (because errorless code)
  5503.                     if B then -- So I flipped the conditions.
  5504.                         return unpack(B);
  5505.                     end;
  5506.        
  5507.                     return;
  5508.                 else
  5509.                     OnError(B, InstrPoint - 1); -- Didn't get time to test the `-1` honestly, but I assume it works properly
  5510.                 end;
  5511.             end;
  5512.         end;
  5513.        
  5514.         return function(BCode, Env) -- lua_function LoadBytecode (string BCode, table Env)
  5515.             local Buffer    = GetMeaning(BCode);
  5516.        
  5517.             return Wrap(Buffer, Env or getfenv(0)), Buffer;
  5518.         end;
  5519.     end
  5520.     fake_module_scripts[script] = module_script
  5521. end
  5522.  
  5523.  
  5524. -- Scripts:
  5525.  
  5526. local function OLCP_fake_script() -- execute.Script
  5527.     local script = Instance.new('Script', execute)
  5528.     local req = require
  5529.     local require = function(obj)
  5530.         local fake = fake_module_scripts[obj]
  5531.         if fake then
  5532.             return fake()
  5533.         end
  5534.         return req(obj)
  5535.     end
  5536.  
  5537.     local remote = script.Parent.RemoteEvent
  5538.    
  5539.     remote.OnServerEvent:Connect(function(plr,Script)
  5540.         require(script.Loadstring)(Script)()
  5541.     end)
  5542. end
  5543. coroutine.wrap(OLCP_fake_script)()
  5544. local function NSOTFRV_fake_script() -- execute.LocalScript
  5545.     local script = Instance.new('LocalScript', execute)
  5546.     local req = require
  5547.     local require = function(obj)
  5548.         local fake = fake_module_scripts[obj]
  5549.         if fake then
  5550.             return fake()
  5551.         end
  5552.         return req(obj)
  5553.     end
  5554.  
  5555.     local source = script.Parent.Parent.source
  5556.     local execute = script.Parent
  5557.     local remote = script.Parent.RemoteEvent
  5558.    
  5559.     function onClicked()
  5560.         remote:FireServer(source.Text)
  5561.     end
  5562.    
  5563.     script.Parent.MouseButton1Down:Connect(onClicked)
  5564. end
  5565. coroutine.wrap(NSOTFRV_fake_script)()
  5566. local function JAZWKV_fake_script() -- clear.LocalScript
  5567.     local script = Instance.new('LocalScript', clear)
  5568.     local req = require
  5569.     local require = function(obj)
  5570.         local fake = fake_module_scripts[obj]
  5571.         if fake then
  5572.             return fake()
  5573.         end
  5574.         return req(obj)
  5575.     end
  5576.  
  5577.     function onClicked()
  5578.         script.Parent.Parent.source.Text = ""
  5579.     end
  5580.    
  5581.     script.Parent.MouseButton1Down:Connect(onClicked)
  5582. end
  5583. coroutine.wrap(JAZWKV_fake_script)()
  5584.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement