Advertisement
tigedraglon1033

Untitled

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