Advertisement
Kep13

LuaY

Dec 21st, 2020
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 64.76 KB | None | 0 0
  1. --[[--------------------------------------------------------------------
  2.  
  3. lparser.lua
  4. Lua 5 parser in Lua
  5. This file is part of Yueliang.
  6.  
  7. Copyright (c) 2005-2007 Kein-Hong Man <khman@users.sf.net>
  8. The COPYRIGHT file describes the conditions
  9. under which this software may be distributed.
  10.  
  11. See the ChangeLog for more information.
  12.  
  13. ----------------------------------------------------------------------]]
  14.  
  15. --[[--------------------------------------------------------------------
  16. -- Notes:
  17. -- * some unused C code that were not converted are kept as comments
  18. -- * LUA_COMPAT_VARARG option changed into a comment block
  19. -- * for value/size specific code added, look for 'NOTE: '
  20. --
  21. -- Not implemented:
  22. -- * luaX_newstring not needed by this Lua implementation
  23. -- * luaG_checkcode() in assert is not currently implemented
  24. --
  25. -- Added:
  26. -- * some constants added from various header files
  27. -- * luaY.LUA_QS used in error_expected, check_match (from luaconf.h)
  28. -- * luaY:LUA_QL needed for error messages (from luaconf.h)
  29. -- * luaY:growvector (from lmem.h) -- skeleton only, limit checking
  30. -- * luaY.SHRT_MAX (from <limits.h>) for registerlocalvar
  31. -- * luaY:newproto (from lfunc.c)
  32. -- * luaY:int2fb (from lobject.c)
  33. -- * NOTE: HASARG_MASK, for implementing a VARARG_HASARG bit operation
  34. -- * NOTE: value-specific code for VARARG_NEEDSARG to replace a bitop
  35. --
  36. -- Changed in 5.1.x:
  37. -- * various code changes are not detailed...
  38. -- * names of constants may have changed, e.g. added a LUAI_ prefix
  39. -- * struct expkind: added VKNUM, VVARARG; VCALL's info changed?
  40. -- * struct expdesc: added nval
  41. -- * struct FuncState: upvalues data type changed to upvaldesc
  42. -- * macro hasmultret is new
  43. -- * function checklimit moved to parser from lexer
  44. -- * functions anchor_token, errorlimit, checknext are new
  45. -- * checknext is new, equivalent to 5.0.x's check, see check too
  46. -- * luaY:next and luaY:lookahead moved to lexer
  47. -- * break keyword no longer skipped in luaY:breakstat
  48. -- * function new_localvarstr replaced by new_localvarliteral
  49. -- * registerlocalvar limits local variables to SHRT_MAX
  50. -- * create_local deleted, new_localvarliteral used instead
  51. -- * constant LUAI_MAXUPVALUES increased to 60
  52. -- * constants MAXPARAMS, LUA_MAXPARSERLEVEL, MAXSTACK removed
  53. -- * function interface changed: singlevaraux, singlevar
  54. -- * enterlevel and leavelevel uses nCcalls to track call depth
  55. -- * added a name argument to main entry function, luaY:parser
  56. -- * function luaY_index changed to yindex
  57. -- * luaY:int2fb()'s table size encoding format has been changed
  58. -- * luaY:log2() no longer needed for table constructors
  59. -- * function code_params deleted, functionality folded in parlist
  60. -- * vararg flags handling (is_vararg) changes; also see VARARG_*
  61. -- * LUA_COMPATUPSYNTAX section for old-style upvalues removed
  62. -- * repeatstat() calls chunk() instead of block()
  63. -- * function interface changed: cond, test_then_block
  64. -- * while statement implementation considerably simplified; MAXEXPWHILE
  65. -- and EXTRAEXP no longer required, no limits to the complexity of a
  66. -- while condition
  67. -- * repeat, forbody statement implementation has major changes,
  68. -- mostly due to new scoping behaviour of local variables
  69. -- * OPR_MULT renamed to OPR_MUL
  70. ----------------------------------------------------------------------]]
  71.  
  72. --requires luaP, luaX, luaK
  73. local luaY = {}
  74. local luaX = require(script.Parent.LuaX)
  75. local luaK = require(script.Parent.LuaK)(luaY)
  76. local luaP = require(script.Parent.LuaP)
  77.  
  78. --[[--------------------------------------------------------------------
  79. -- Expression descriptor
  80. -- * expkind changed to string constants; luaY:assignment was the only
  81. -- function to use a relational operator with this enumeration
  82. -- VVOID -- no value
  83. -- VNIL -- no value
  84. -- VTRUE -- no value
  85. -- VFALSE -- no value
  86. -- VK -- info = index of constant in 'k'
  87. -- VKNUM -- nval = numerical value
  88. -- VLOCAL -- info = local register
  89. -- VUPVAL, -- info = index of upvalue in 'upvalues'
  90. -- VGLOBAL -- info = index of table; aux = index of global name in 'k'
  91. -- VINDEXED -- info = table register; aux = index register (or 'k')
  92. -- VJMP -- info = instruction pc
  93. -- VRELOCABLE -- info = instruction pc
  94. -- VNONRELOC -- info = result register
  95. -- VCALL -- info = instruction pc
  96. -- VVARARG -- info = instruction pc
  97. } ----------------------------------------------------------------------]]
  98.  
  99. --[[--------------------------------------------------------------------
  100. -- * expdesc in Lua 5.1.x has a union u and another struct s; this Lua
  101. -- implementation ignores all instances of u and s usage
  102. -- struct expdesc:
  103. -- k -- (enum: expkind)
  104. -- info, aux -- (int, int)
  105. -- nval -- (lua_Number)
  106. -- t -- patch list of 'exit when true'
  107. -- f -- patch list of 'exit when false'
  108. ----------------------------------------------------------------------]]
  109.  
  110. --[[--------------------------------------------------------------------
  111. -- struct upvaldesc:
  112. -- k -- (lu_byte)
  113. -- info -- (lu_byte)
  114. ----------------------------------------------------------------------]]
  115.  
  116. --[[--------------------------------------------------------------------
  117. -- state needed to generate code for a given function
  118. -- struct FuncState:
  119. -- f -- current function header (table: Proto)
  120. -- h -- table to find (and reuse) elements in 'k' (table: Table)
  121. -- prev -- enclosing function (table: FuncState)
  122. -- ls -- lexical state (table: LexState)
  123. -- L -- copy of the Lua state (table: lua_State)
  124. -- bl -- chain of current blocks (table: BlockCnt)
  125. -- pc -- next position to code (equivalent to 'ncode')
  126. -- lasttarget -- 'pc' of last 'jump target'
  127. -- jpc -- list of pending jumps to 'pc'
  128. -- freereg -- first free register
  129. -- nk -- number of elements in 'k'
  130. -- np -- number of elements in 'p'
  131. -- nlocvars -- number of elements in 'locvars'
  132. -- nactvar -- number of active local variables
  133. -- upvalues[LUAI_MAXUPVALUES] -- upvalues (table: upvaldesc)
  134. -- actvar[LUAI_MAXVARS] -- declared-variable stack
  135. ----------------------------------------------------------------------]]
  136.  
  137. ------------------------------------------------------------------------
  138. -- constants used by parser
  139. -- * picks up duplicate values from luaX if required
  140. ------------------------------------------------------------------------
  141.  
  142. luaY.LUA_QS = luaX.LUA_QS or "'%s'" -- (from luaconf.h)
  143.  
  144. luaY.SHRT_MAX = 32767 -- (from <limits.h>)
  145. luaY.LUAI_MAXVARS = 200 -- (luaconf.h)
  146. luaY.LUAI_MAXUPVALUES = 60 -- (luaconf.h)
  147. luaY.MAX_INT = luaX.MAX_INT or 2147483645 -- (from llimits.h)
  148. -- * INT_MAX-2 for 32-bit systems
  149. luaY.LUAI_MAXCCALLS = 200 -- (from luaconf.h)
  150.  
  151. luaY.VARARG_HASARG = 1 -- (from lobject.h)
  152. -- NOTE: HASARG_MASK is value-specific
  153. luaY.HASARG_MASK = 2 -- this was added for a bitop in parlist()
  154. luaY.VARARG_ISVARARG = 2
  155. -- NOTE: there is some value-specific code that involves VARARG_NEEDSARG
  156. luaY.VARARG_NEEDSARG = 4
  157.  
  158. luaY.LUA_MULTRET = -1 -- (lua.h)
  159.  
  160. --[[--------------------------------------------------------------------
  161. -- other functions
  162. ----------------------------------------------------------------------]]
  163.  
  164. ------------------------------------------------------------------------
  165. -- LUA_QL describes how error messages quote program elements.
  166. -- CHANGE it if you want a different appearance. (from luaconf.h)
  167. ------------------------------------------------------------------------
  168. function luaY:LUA_QL(x)
  169. return "'"..x.."'"
  170. end
  171.  
  172. ------------------------------------------------------------------------
  173. -- this is a stripped-down luaM_growvector (from lmem.h) which is a
  174. -- macro based on luaM_growaux (in lmem.c); all the following does is
  175. -- reproduce the size limit checking logic of the original function
  176. -- so that error behaviour is identical; all arguments preserved for
  177. -- convenience, even those which are unused
  178. -- * set the t field to nil, since this originally does a sizeof(t)
  179. -- * size (originally a pointer) is never updated, their final values
  180. -- are set by luaY:close_func(), so overall things should still work
  181. ------------------------------------------------------------------------
  182. function luaY:growvector(L, v, nelems, size, t, limit, e)
  183. if nelems >= limit then
  184. error(e) -- was luaG_runerror
  185. end
  186. end
  187.  
  188. ------------------------------------------------------------------------
  189. -- initialize a new function prototype structure (from lfunc.c)
  190. -- * used only in open_func()
  191. ------------------------------------------------------------------------
  192. function luaY:newproto(L)
  193. local f = {} -- Proto
  194. -- luaC_link(L, obj2gco(f), LUA_TPROTO); /* GC */
  195. f.k = {}
  196. f.sizek = 0
  197. f.p = {}
  198. f.sizep = 0
  199. f.code = {}
  200. f.sizecode = 0
  201. f.sizelineinfo = 0
  202. f.sizeupvalues = 0
  203. f.nups = 0
  204. f.upvalues = {}
  205. f.numparams = 0
  206. f.is_vararg = 0
  207. f.maxstacksize = 0
  208. f.lineinfo = {}
  209. f.sizelocvars = 0
  210. f.locvars = {}
  211. f.lineDefined = 0
  212. f.lastlinedefined = 0
  213. f.source = nil
  214. return f
  215. end
  216.  
  217. ------------------------------------------------------------------------
  218. -- converts an integer to a "floating point byte", represented as
  219. -- (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  220. -- eeeee != 0 and (xxx) otherwise.
  221. ------------------------------------------------------------------------
  222. function luaY:int2fb(x)
  223. local e = 0 -- exponent
  224. while x >= 16 do
  225. x = math.floor((x + 1) / 2)
  226. e = e + 1
  227. end
  228. if x < 8 then
  229. return x
  230. else
  231. return ((e + 1) * 8) + (x - 8)
  232. end
  233. end
  234.  
  235. --[[--------------------------------------------------------------------
  236. -- parser functions
  237. ----------------------------------------------------------------------]]
  238.  
  239. ------------------------------------------------------------------------
  240. -- true of the kind of expression produces multiple return values
  241. ------------------------------------------------------------------------
  242. function luaY:hasmultret(k)
  243. return k == "VCALL" or k == "VVARARG"
  244. end
  245.  
  246. ------------------------------------------------------------------------
  247. -- convenience function to access active local i, returns entry
  248. ------------------------------------------------------------------------
  249. function luaY:getlocvar(fs, i)
  250. return fs.f.locvars[ fs.actvar[i] ]
  251. end
  252.  
  253. ------------------------------------------------------------------------
  254. -- check a limit, string m provided as an error message
  255. ------------------------------------------------------------------------
  256. function luaY:checklimit(fs, v, l, m)
  257. if v > l then self:errorlimit(fs, l, m) end
  258. end
  259.  
  260. --[[--------------------------------------------------------------------
  261. -- nodes for block list (list of active blocks)
  262. -- struct BlockCnt:
  263. -- previous -- chain (table: BlockCnt)
  264. -- breaklist -- list of jumps out of this loop
  265. -- nactvar -- # active local variables outside the breakable structure
  266. -- upval -- true if some variable in the block is an upvalue (boolean)
  267. -- isbreakable -- true if 'block' is a loop (boolean)
  268. ----------------------------------------------------------------------]]
  269.  
  270. ------------------------------------------------------------------------
  271. -- prototypes for recursive non-terminal functions
  272. ------------------------------------------------------------------------
  273. -- prototypes deleted; not required in Lua
  274.  
  275. ------------------------------------------------------------------------
  276. -- reanchor if last token is has a constant string, see close_func()
  277. -- * used only in close_func()
  278. ------------------------------------------------------------------------
  279. function luaY:anchor_token(ls)
  280. if ls.t.token == "TK_NAME" or ls.t.token == "TK_STRING" then
  281. -- not relevant to Lua implementation of parser
  282. -- local ts = ls.t.seminfo
  283. -- luaX_newstring(ls, getstr(ts), ts->tsv.len); /* C */
  284. end
  285. end
  286.  
  287. ------------------------------------------------------------------------
  288. -- throws a syntax error if token expected is not there
  289. ------------------------------------------------------------------------
  290. function luaY:error_expected(ls, token)
  291. luaX:syntaxerror(ls,
  292. string.format(self.LUA_QS.." expected", luaX:token2str(ls, token)))
  293. end
  294.  
  295. ------------------------------------------------------------------------
  296. -- prepares error message for display, for limits exceeded
  297. -- * used only in checklimit()
  298. ------------------------------------------------------------------------
  299. function luaY:errorlimit(fs, limit, what)
  300. local msg = (fs.f.linedefined == 0) and
  301. string.format("main function has more than %d %s", limit, what) or
  302. string.format("function at line %d has more than %d %s",
  303. fs.f.linedefined, limit, what)
  304. luaX:lexerror(fs.ls, msg, 0)
  305. end
  306.  
  307. ------------------------------------------------------------------------
  308. -- tests for a token, returns outcome
  309. -- * return value changed to boolean
  310. ------------------------------------------------------------------------
  311. function luaY:testnext(ls, c)
  312. if ls.t.token == c then
  313. luaX:next(ls)
  314. return true
  315. else
  316. return false
  317. end
  318. end
  319.  
  320. ------------------------------------------------------------------------
  321. -- check for existence of a token, throws error if not found
  322. ------------------------------------------------------------------------
  323. function luaY:check(ls, c)
  324. if ls.t.token ~= c then
  325. self:error_expected(ls, c)
  326. end
  327. end
  328.  
  329. ------------------------------------------------------------------------
  330. -- verify existence of a token, then skip it
  331. ------------------------------------------------------------------------
  332. function luaY:checknext(ls, c)
  333. self:check(ls, c)
  334. luaX:next(ls)
  335. end
  336.  
  337. ------------------------------------------------------------------------
  338. -- throws error if condition not matched
  339. ------------------------------------------------------------------------
  340. function luaY:check_condition(ls, c, msg)
  341. if not c then luaX:syntaxerror(ls, msg) end
  342. end
  343.  
  344. ------------------------------------------------------------------------
  345. -- verifies token conditions are met or else throw error
  346. ------------------------------------------------------------------------
  347. function luaY:check_match(ls, what, who, where)
  348. if not self:testnext(ls, what) then
  349. if where == ls.linenumber then
  350. self:error_expected(ls, what)
  351. else
  352. luaX:syntaxerror(ls, string.format(
  353. self.LUA_QS.." expected (to close "..self.LUA_QS.." at line %d)",
  354. luaX:token2str(ls, what), luaX:token2str(ls, who), where))
  355. end
  356. end
  357. end
  358.  
  359. ------------------------------------------------------------------------
  360. -- expect that token is a name, return the name
  361. ------------------------------------------------------------------------
  362. function luaY:str_checkname(ls)
  363. self:check(ls, "TK_NAME")
  364. local ts = ls.t.seminfo
  365. luaX:next(ls)
  366. return ts
  367. end
  368.  
  369. ------------------------------------------------------------------------
  370. -- initialize a struct expdesc, expression description data structure
  371. ------------------------------------------------------------------------
  372. function luaY:init_exp(e, k, i)
  373. e.f, e.t = luaK.NO_JUMP, luaK.NO_JUMP
  374. e.k = k
  375. e.info = i
  376. end
  377.  
  378. ------------------------------------------------------------------------
  379. -- adds given string s in string pool, sets e as VK
  380. ------------------------------------------------------------------------
  381. function luaY:codestring(ls, e, s)
  382. self:init_exp(e, "VK", luaK:stringK(ls.fs, s))
  383. end
  384.  
  385. ------------------------------------------------------------------------
  386. -- consume a name token, adds it to string pool, sets e as VK
  387. ------------------------------------------------------------------------
  388. function luaY:checkname(ls, e)
  389. self:codestring(ls, e, self:str_checkname(ls))
  390. end
  391.  
  392. ------------------------------------------------------------------------
  393. -- creates struct entry for a local variable
  394. -- * used only in new_localvar()
  395. ------------------------------------------------------------------------
  396. function luaY:registerlocalvar(ls, varname)
  397. local fs = ls.fs
  398. local f = fs.f
  399. self:growvector(ls.L, f.locvars, fs.nlocvars, f.sizelocvars,
  400. nil, self.SHRT_MAX, "too many local variables")
  401. -- loop to initialize empty f.locvar positions not required
  402. f.locvars[fs.nlocvars] = {} -- LocVar
  403. f.locvars[fs.nlocvars].varname = varname
  404. -- luaC_objbarrier(ls.L, f, varname) /* GC */
  405. local nlocvars = fs.nlocvars
  406. fs.nlocvars = fs.nlocvars + 1
  407. return nlocvars
  408. end
  409.  
  410. ------------------------------------------------------------------------
  411. -- creates a new local variable given a name and an offset from nactvar
  412. -- * used in fornum(), forlist(), parlist(), body()
  413. ------------------------------------------------------------------------
  414. function luaY:new_localvarliteral(ls, v, n)
  415. self:new_localvar(ls, v, n)
  416. end
  417.  
  418. ------------------------------------------------------------------------
  419. -- register a local variable, set in active variable list
  420. ------------------------------------------------------------------------
  421. function luaY:new_localvar(ls, name, n)
  422. local fs = ls.fs
  423. self:checklimit(fs, fs.nactvar + n + 1, self.LUAI_MAXVARS, "local variables")
  424. fs.actvar[fs.nactvar + n] = self:registerlocalvar(ls, name)
  425. end
  426.  
  427. ------------------------------------------------------------------------
  428. -- adds nvars number of new local variables, set debug information
  429. ------------------------------------------------------------------------
  430. function luaY:adjustlocalvars(ls, nvars)
  431. local fs = ls.fs
  432. fs.nactvar = fs.nactvar + nvars
  433. for i = nvars, 1, -1 do
  434. self:getlocvar(fs, fs.nactvar - i).startpc = fs.pc
  435. end
  436. end
  437.  
  438. ------------------------------------------------------------------------
  439. -- removes a number of locals, set debug information
  440. ------------------------------------------------------------------------
  441. function luaY:removevars(ls, tolevel)
  442. local fs = ls.fs
  443. while fs.nactvar > tolevel do
  444. fs.nactvar = fs.nactvar - 1
  445. self:getlocvar(fs, fs.nactvar).endpc = fs.pc
  446. end
  447. end
  448.  
  449. ------------------------------------------------------------------------
  450. -- returns an existing upvalue index based on the given name, or
  451. -- creates a new upvalue struct entry and returns the new index
  452. -- * used only in singlevaraux()
  453. ------------------------------------------------------------------------
  454. function luaY:indexupvalue(fs, name, v)
  455. local f = fs.f
  456. for i = 0, f.nups - 1 do
  457. if fs.upvalues[i].k == v.k and fs.upvalues[i].info == v.info then
  458. assert(f.upvalues[i] == name)
  459. return i
  460. end
  461. end
  462. -- new one
  463. self:checklimit(fs, f.nups + 1, self.LUAI_MAXUPVALUES, "upvalues")
  464. self:growvector(fs.L, f.upvalues, f.nups, f.sizeupvalues,
  465. nil, self.MAX_INT, "")
  466. -- loop to initialize empty f.upvalues positions not required
  467. f.upvalues[f.nups] = name
  468. -- luaC_objbarrier(fs->L, f, name); /* GC */
  469. assert(v.k == "VLOCAL" or v.k == "VUPVAL")
  470. -- this is a partial copy; only k & info fields used
  471. fs.upvalues[f.nups] = { k = v.k, info = v.info }
  472. local nups = f.nups
  473. f.nups = f.nups + 1
  474. return nups
  475. end
  476.  
  477. ------------------------------------------------------------------------
  478. -- search the local variable namespace of the given fs for a match
  479. -- * used only in singlevaraux()
  480. ------------------------------------------------------------------------
  481. function luaY:searchvar(fs, n)
  482. for i = fs.nactvar - 1, 0, -1 do
  483. if n == self:getlocvar(fs, i).varname then
  484. return i
  485. end
  486. end
  487. return -1 -- not found
  488. end
  489.  
  490. ------------------------------------------------------------------------
  491. -- * mark upvalue flags in function states up to a given level
  492. -- * used only in singlevaraux()
  493. ------------------------------------------------------------------------
  494. function luaY:markupval(fs, level)
  495. local bl = fs.bl
  496. while bl and bl.nactvar > level do bl = bl.previous end
  497. if bl then bl.upval = true end
  498. end
  499.  
  500. ------------------------------------------------------------------------
  501. -- handle locals, globals and upvalues and related processing
  502. -- * search mechanism is recursive, calls itself to search parents
  503. -- * used only in singlevar()
  504. ------------------------------------------------------------------------
  505. function luaY:singlevaraux(fs, n, var, base)
  506. if fs == nil then -- no more levels?
  507. self:init_exp(var, "VGLOBAL", luaP.NO_REG) -- default is global variable
  508. return "VGLOBAL"
  509. else
  510. local v = self:searchvar(fs, n) -- look up at current level
  511. if v >= 0 then
  512. self:init_exp(var, "VLOCAL", v)
  513. if base == 0 then
  514. self:markupval(fs, v) -- local will be used as an upval
  515. end
  516. return "VLOCAL"
  517. else -- not found at current level; try upper one
  518. if self:singlevaraux(fs.prev, n, var, 0) == "VGLOBAL" then
  519. return "VGLOBAL"
  520. end
  521. var.info = self:indexupvalue(fs, n, var) -- else was LOCAL or UPVAL
  522. var.k = "VUPVAL" -- upvalue in this level
  523. return "VUPVAL"
  524. end--if v
  525. end--if fs
  526. end
  527.  
  528. ------------------------------------------------------------------------
  529. -- consume a name token, creates a variable (global|local|upvalue)
  530. -- * used in prefixexp(), funcname()
  531. ------------------------------------------------------------------------
  532. function luaY:singlevar(ls, var)
  533. local varname = self:str_checkname(ls)
  534. local fs = ls.fs
  535. if self:singlevaraux(fs, varname, var, 1) == "VGLOBAL" then
  536. var.info = luaK:stringK(fs, varname) -- info points to global name
  537. end
  538. end
  539.  
  540. ------------------------------------------------------------------------
  541. -- adjust RHS to match LHS in an assignment
  542. -- * used in assignment(), forlist(), localstat()
  543. ------------------------------------------------------------------------
  544. function luaY:adjust_assign(ls, nvars, nexps, e)
  545. local fs = ls.fs
  546. local extra = nvars - nexps
  547. if self:hasmultret(e.k) then
  548. extra = extra + 1 -- includes call itself
  549. if extra <= 0 then extra = 0 end
  550. luaK:setreturns(fs, e, extra) -- last exp. provides the difference
  551. if extra > 1 then luaK:reserveregs(fs, extra - 1) end
  552. else
  553. if e.k ~= "VVOID" then luaK:exp2nextreg(fs, e) end -- close last expression
  554. if extra > 0 then
  555. local reg = fs.freereg
  556. luaK:reserveregs(fs, extra)
  557. luaK:_nil(fs, reg, extra)
  558. end
  559. end
  560. end
  561.  
  562. ------------------------------------------------------------------------
  563. -- tracks and limits parsing depth, assert check at end of parsing
  564. ------------------------------------------------------------------------
  565. function luaY:enterlevel(ls)
  566. ls.L.nCcalls = ls.L.nCcalls + 1
  567. if ls.L.nCcalls > self.LUAI_MAXCCALLS then
  568. luaX:lexerror(ls, "chunk has too many syntax levels", 0)
  569. end
  570. end
  571.  
  572. ------------------------------------------------------------------------
  573. -- tracks parsing depth, a pair with luaY:enterlevel()
  574. ------------------------------------------------------------------------
  575. function luaY:leavelevel(ls)
  576. ls.L.nCcalls = ls.L.nCcalls - 1
  577. end
  578.  
  579. ------------------------------------------------------------------------
  580. -- enters a code unit, initializes elements
  581. ------------------------------------------------------------------------
  582. function luaY:enterblock(fs, bl, isbreakable)
  583. bl.breaklist = luaK.NO_JUMP
  584. bl.isbreakable = isbreakable
  585. bl.nactvar = fs.nactvar
  586. bl.upval = false
  587. bl.previous = fs.bl
  588. fs.bl = bl
  589. assert(fs.freereg == fs.nactvar)
  590. end
  591.  
  592. ------------------------------------------------------------------------
  593. -- leaves a code unit, close any upvalues
  594. ------------------------------------------------------------------------
  595. function luaY:leaveblock(fs)
  596. local bl = fs.bl
  597. fs.bl = bl.previous
  598. self:removevars(fs.ls, bl.nactvar)
  599. if bl.upval then
  600. luaK:codeABC(fs, "OP_CLOSE", bl.nactvar, 0, 0)
  601. end
  602. -- a block either controls scope or breaks (never both)
  603. assert(not bl.isbreakable or not bl.upval)
  604. assert(bl.nactvar == fs.nactvar)
  605. fs.freereg = fs.nactvar -- free registers
  606. luaK:patchtohere(fs, bl.breaklist)
  607. end
  608.  
  609. ------------------------------------------------------------------------
  610. -- implement the instantiation of a function prototype, append list of
  611. -- upvalues after the instantiation instruction
  612. -- * used only in body()
  613. ------------------------------------------------------------------------
  614. function luaY:pushclosure(ls, func, v)
  615. local fs = ls.fs
  616. local f = fs.f
  617. self:growvector(ls.L, f.p, fs.np, f.sizep, nil,
  618. luaP.MAXARG_Bx, "constant table overflow")
  619. -- loop to initialize empty f.p positions not required
  620. f.p[fs.np] = func.f
  621. fs.np = fs.np + 1
  622. -- luaC_objbarrier(ls->L, f, func->f); /* C */
  623. self:init_exp(v, "VRELOCABLE", luaK:codeABx(fs, "OP_CLOSURE", 0, fs.np - 1))
  624. for i = 0, func.f.nups - 1 do
  625. local o = (func.upvalues[i].k == "VLOCAL") and "OP_MOVE" or "OP_GETUPVAL"
  626. luaK:codeABC(fs, o, 0, func.upvalues[i].info, 0)
  627. end
  628. end
  629.  
  630. ------------------------------------------------------------------------
  631. -- opening of a function
  632. ------------------------------------------------------------------------
  633. function luaY:open_func(ls, fs)
  634. local L = ls.L
  635. local f = self:newproto(ls.L)
  636. fs.f = f
  637. fs.prev = ls.fs -- linked list of funcstates
  638. fs.ls = ls
  639. fs.L = L
  640. ls.fs = fs
  641. fs.pc = 0
  642. fs.lasttarget = -1
  643. fs.jpc = luaK.NO_JUMP
  644. fs.freereg = 0
  645. fs.nk = 0
  646. fs.np = 0
  647. fs.nlocvars = 0
  648. fs.nactvar = 0
  649. fs.bl = nil
  650. f.source = ls.source
  651. f.maxstacksize = 2 -- registers 0/1 are always valid
  652. fs.h = {} -- constant table; was luaH_new call
  653. -- anchor table of constants and prototype (to avoid being collected)
  654. -- sethvalue2s(L, L->top, fs->h); incr_top(L); /* C */
  655. -- setptvalue2s(L, L->top, f); incr_top(L);
  656. end
  657.  
  658. ------------------------------------------------------------------------
  659. -- closing of a function
  660. ------------------------------------------------------------------------
  661. function luaY:close_func(ls)
  662. local L = ls.L
  663. local fs = ls.fs
  664. local f = fs.f
  665. self:removevars(ls, 0)
  666. luaK:ret(fs, 0, 0) -- final return
  667. -- luaM_reallocvector deleted for f->code, f->lineinfo, f->k, f->p,
  668. -- f->locvars, f->upvalues; not required for Lua table arrays
  669. f.sizecode = fs.pc
  670. f.sizelineinfo = fs.pc
  671. f.sizek = fs.nk
  672. f.sizep = fs.np
  673. f.sizelocvars = fs.nlocvars
  674. f.sizeupvalues = f.nups
  675. --assert(luaG_checkcode(f)) -- currently not implemented
  676. assert(fs.bl == nil)
  677. ls.fs = fs.prev
  678. -- the following is not required for this implementation; kept here
  679. -- for completeness
  680. -- L->top -= 2; /* remove table and prototype from the stack */
  681. -- last token read was anchored in defunct function; must reanchor it
  682. if fs then self:anchor_token(ls) end
  683. end
  684.  
  685. ------------------------------------------------------------------------
  686. -- parser initialization function
  687. -- * note additional sub-tables needed for LexState, FuncState
  688. ------------------------------------------------------------------------
  689. function luaY:parser(L, z, buff, name)
  690. local lexstate = {} -- LexState
  691. lexstate.t = {}
  692. lexstate.lookahead = {}
  693. local funcstate = {} -- FuncState
  694. funcstate.upvalues = {}
  695. funcstate.actvar = {}
  696. -- the following nCcalls initialization added for convenience
  697. L.nCcalls = 0
  698. lexstate.buff = buff
  699. luaX:setinput(L, lexstate, z, name)
  700. self:open_func(lexstate, funcstate)
  701. funcstate.f.is_vararg = self.VARARG_ISVARARG -- main func. is always vararg
  702. luaX:next(lexstate) -- read first token
  703. self:chunk(lexstate)
  704. self:check(lexstate, "TK_EOS")
  705. self:close_func(lexstate)
  706. assert(funcstate.prev == nil)
  707. assert(funcstate.f.nups == 0)
  708. assert(lexstate.fs == nil)
  709. return funcstate.f
  710. end
  711.  
  712. --[[--------------------------------------------------------------------
  713. -- GRAMMAR RULES
  714. ----------------------------------------------------------------------]]
  715.  
  716. ------------------------------------------------------------------------
  717. -- parse a function name suffix, for function call specifications
  718. -- * used in primaryexp(), funcname()
  719. ------------------------------------------------------------------------
  720. function luaY:field(ls, v)
  721. -- field -> ['.' | ':'] NAME
  722. local fs = ls.fs
  723. local key = {} -- expdesc
  724. luaK:exp2anyreg(fs, v)
  725. luaX:next(ls) -- skip the dot or colon
  726. self:checkname(ls, key)
  727. luaK:indexed(fs, v, key)
  728. end
  729.  
  730. ------------------------------------------------------------------------
  731. -- parse a table indexing suffix, for constructors, expressions
  732. -- * used in recfield(), primaryexp()
  733. ------------------------------------------------------------------------
  734. function luaY:yindex(ls, v)
  735. -- index -> '[' expr ']'
  736. luaX:next(ls) -- skip the '['
  737. self:expr(ls, v)
  738. luaK:exp2val(ls.fs, v)
  739. self:checknext(ls, "]")
  740. end
  741.  
  742. --[[--------------------------------------------------------------------
  743. -- Rules for Constructors
  744. ----------------------------------------------------------------------]]
  745.  
  746. --[[--------------------------------------------------------------------
  747. -- struct ConsControl:
  748. -- v -- last list item read (table: struct expdesc)
  749. -- t -- table descriptor (table: struct expdesc)
  750. -- nh -- total number of 'record' elements
  751. -- na -- total number of array elements
  752. -- tostore -- number of array elements pending to be stored
  753. ----------------------------------------------------------------------]]
  754.  
  755. ------------------------------------------------------------------------
  756. -- parse a table record (hash) field
  757. -- * used in constructor()
  758. ------------------------------------------------------------------------
  759. function luaY:recfield(ls, cc)
  760. -- recfield -> (NAME | '['exp1']') = exp1
  761. local fs = ls.fs
  762. local reg = ls.fs.freereg
  763. local key, val = {}, {} -- expdesc
  764. if ls.t.token == "TK_NAME" then
  765. self:checklimit(fs, cc.nh, self.MAX_INT, "items in a constructor")
  766. self:checkname(ls, key)
  767. else -- ls->t.token == '['
  768. self:yindex(ls, key)
  769. end
  770. cc.nh = cc.nh + 1
  771. self:checknext(ls, "=")
  772. local rkkey = luaK:exp2RK(fs, key)
  773. self:expr(ls, val)
  774. luaK:codeABC(fs, "OP_SETTABLE", cc.t.info, rkkey, luaK:exp2RK(fs, val))
  775. fs.freereg = reg -- free registers
  776. end
  777.  
  778. ------------------------------------------------------------------------
  779. -- emit a set list instruction if enough elements (LFIELDS_PER_FLUSH)
  780. -- * used in constructor()
  781. ------------------------------------------------------------------------
  782. function luaY:closelistfield(fs, cc)
  783. if cc.v.k == "VVOID" then return end -- there is no list item
  784. luaK:exp2nextreg(fs, cc.v)
  785. cc.v.k = "VVOID"
  786. if cc.tostore == luaP.LFIELDS_PER_FLUSH then
  787. luaK:setlist(fs, cc.t.info, cc.na, cc.tostore) -- flush
  788. cc.tostore = 0 -- no more items pending
  789. end
  790. end
  791.  
  792. ------------------------------------------------------------------------
  793. -- emit a set list instruction at the end of parsing list constructor
  794. -- * used in constructor()
  795. ------------------------------------------------------------------------
  796. function luaY:lastlistfield(fs, cc)
  797. if cc.tostore == 0 then return end
  798. if self:hasmultret(cc.v.k) then
  799. luaK:setmultret(fs, cc.v)
  800. luaK:setlist(fs, cc.t.info, cc.na, self.LUA_MULTRET)
  801. cc.na = cc.na - 1 -- do not count last expression (unknown number of elements)
  802. else
  803. if cc.v.k ~= "VVOID" then
  804. luaK:exp2nextreg(fs, cc.v)
  805. end
  806. luaK:setlist(fs, cc.t.info, cc.na, cc.tostore)
  807. end
  808. end
  809.  
  810. ------------------------------------------------------------------------
  811. -- parse a table list (array) field
  812. -- * used in constructor()
  813. ------------------------------------------------------------------------
  814. function luaY:listfield(ls, cc)
  815. self:expr(ls, cc.v)
  816. self:checklimit(ls.fs, cc.na, self.MAX_INT, "items in a constructor")
  817. cc.na = cc.na + 1
  818. cc.tostore = cc.tostore + 1
  819. end
  820.  
  821. ------------------------------------------------------------------------
  822. -- parse a table constructor
  823. -- * used in funcargs(), simpleexp()
  824. ------------------------------------------------------------------------
  825. function luaY:constructor(ls, t)
  826. -- constructor -> '{' [ field { fieldsep field } [ fieldsep ] ] '}'
  827. -- field -> recfield | listfield
  828. -- fieldsep -> ',' | ';'
  829. local fs = ls.fs
  830. local line = ls.linenumber
  831. local pc = luaK:codeABC(fs, "OP_NEWTABLE", 0, 0, 0)
  832. local cc = {} -- ConsControl
  833. cc.v = {}
  834. cc.na, cc.nh, cc.tostore = 0, 0, 0
  835. cc.t = t
  836. self:init_exp(t, "VRELOCABLE", pc)
  837. self:init_exp(cc.v, "VVOID", 0) -- no value (yet)
  838. luaK:exp2nextreg(ls.fs, t) -- fix it at stack top (for gc)
  839. self:checknext(ls, "{")
  840. repeat
  841. assert(cc.v.k == "VVOID" or cc.tostore > 0)
  842. if ls.t.token == "}" then break end
  843. self:closelistfield(fs, cc)
  844. local c = ls.t.token
  845.  
  846. if c == "TK_NAME" then -- may be listfields or recfields
  847. luaX:lookahead(ls)
  848. if ls.lookahead.token ~= "=" then -- expression?
  849. self:listfield(ls, cc)
  850. else
  851. self:recfield(ls, cc)
  852. end
  853. elseif c == "[" then -- constructor_item -> recfield
  854. self:recfield(ls, cc)
  855. else -- constructor_part -> listfield
  856. self:listfield(ls, cc)
  857. end
  858. until not self:testnext(ls, ",") and not self:testnext(ls, ";")
  859. self:check_match(ls, "}", "{", line)
  860. self:lastlistfield(fs, cc)
  861. luaP:SETARG_B(fs.f.code[pc], self:int2fb(cc.na)) -- set initial array size
  862. luaP:SETARG_C(fs.f.code[pc], self:int2fb(cc.nh)) -- set initial table size
  863. end
  864.  
  865. -- }======================================================================
  866.  
  867. ------------------------------------------------------------------------
  868. -- parse the arguments (parameters) of a function declaration
  869. -- * used in body()
  870. ------------------------------------------------------------------------
  871. function luaY:parlist(ls)
  872. -- parlist -> [ param { ',' param } ]
  873. local fs = ls.fs
  874. local f = fs.f
  875. local nparams = 0
  876. f.is_vararg = 0
  877. if ls.t.token ~= ")" then -- is 'parlist' not empty?
  878. repeat
  879. local c = ls.t.token
  880. if c == "TK_NAME" then -- param -> NAME
  881. self:new_localvar(ls, self:str_checkname(ls), nparams)
  882. nparams = nparams + 1
  883. elseif c == "TK_DOTS" then -- param -> `...'
  884. luaX:next(ls)
  885. -- [[
  886. -- #if defined(LUA_COMPAT_VARARG)
  887. -- use `arg' as default name
  888. self:new_localvarliteral(ls, "arg", nparams)
  889. nparams = nparams + 1
  890. f.is_vararg = self.VARARG_HASARG + self.VARARG_NEEDSARG
  891. -- #endif
  892. --]]
  893. f.is_vararg = f.is_vararg + self.VARARG_ISVARARG
  894. else
  895. luaX:syntaxerror(ls, "<name> or "..self:LUA_QL("...").." expected")
  896. end
  897. until f.is_vararg ~= 0 or not self:testnext(ls, ",")
  898. end--if
  899. self:adjustlocalvars(ls, nparams)
  900. -- NOTE: the following works only when HASARG_MASK is 2!
  901. f.numparams = fs.nactvar - (f.is_vararg % self.HASARG_MASK)
  902. luaK:reserveregs(fs, fs.nactvar) -- reserve register for parameters
  903. end
  904.  
  905. ------------------------------------------------------------------------
  906. -- parse function declaration body
  907. -- * used in simpleexp(), localfunc(), funcstat()
  908. ------------------------------------------------------------------------
  909. function luaY:body(ls, e, needself, line)
  910. -- body -> '(' parlist ')' chunk END
  911. local new_fs = {} -- FuncState
  912. new_fs.upvalues = {}
  913. new_fs.actvar = {}
  914. self:open_func(ls, new_fs)
  915. new_fs.f.lineDefined = line
  916. self:checknext(ls, "(")
  917. if needself then
  918. self:new_localvarliteral(ls, "self", 0)
  919. self:adjustlocalvars(ls, 1)
  920. end
  921. self:parlist(ls)
  922. self:checknext(ls, ")")
  923. self:chunk(ls)
  924. new_fs.f.lastlinedefined = ls.linenumber
  925. self:check_match(ls, "TK_END", "TK_FUNCTION", line)
  926. self:close_func(ls)
  927. self:pushclosure(ls, new_fs, e)
  928. end
  929.  
  930. ------------------------------------------------------------------------
  931. -- parse a list of comma-separated expressions
  932. -- * used is multiple locations
  933. ------------------------------------------------------------------------
  934. function luaY:explist1(ls, v)
  935. -- explist1 -> expr { ',' expr }
  936. local n = 1 -- at least one expression
  937. self:expr(ls, v)
  938. while self:testnext(ls, ",") do
  939. luaK:exp2nextreg(ls.fs, v)
  940. self:expr(ls, v)
  941. n = n + 1
  942. end
  943. return n
  944. end
  945.  
  946. ------------------------------------------------------------------------
  947. -- parse the parameters of a function call
  948. -- * contrast with parlist(), used in function declarations
  949. -- * used in primaryexp()
  950. ------------------------------------------------------------------------
  951. function luaY:funcargs(ls, f)
  952. local fs = ls.fs
  953. local args = {} -- expdesc
  954. local nparams
  955. local line = ls.linenumber
  956. local c = ls.t.token
  957. if c == "(" then -- funcargs -> '(' [ explist1 ] ')'
  958. if line ~= ls.lastline then
  959. luaX:syntaxerror(ls, "ambiguous syntax (function call x new statement)")
  960. end
  961. luaX:next(ls)
  962. if ls.t.token == ")" then -- arg list is empty?
  963. args.k = "VVOID"
  964. else
  965. self:explist1(ls, args)
  966. luaK:setmultret(fs, args)
  967. end
  968. self:check_match(ls, ")", "(", line)
  969. elseif c == "{" then -- funcargs -> constructor
  970. self:constructor(ls, args)
  971. elseif c == "TK_STRING" then -- funcargs -> STRING
  972. self:codestring(ls, args, ls.t.seminfo)
  973. luaX:next(ls) -- must use 'seminfo' before 'next'
  974. else
  975. luaX:syntaxerror(ls, "function arguments expected")
  976. return
  977. end
  978. assert(f.k == "VNONRELOC")
  979. local base = f.info -- base register for call
  980. if self:hasmultret(args.k) then
  981. nparams = self.LUA_MULTRET -- open call
  982. else
  983. if args.k ~= "VVOID" then
  984. luaK:exp2nextreg(fs, args) -- close last argument
  985. end
  986. nparams = fs.freereg - (base + 1)
  987. end
  988. self:init_exp(f, "VCALL", luaK:codeABC(fs, "OP_CALL", base, nparams + 1, 2))
  989. luaK:fixline(fs, line)
  990. fs.freereg = base + 1 -- call remove function and arguments and leaves
  991. -- (unless changed) one result
  992. end
  993.  
  994. --[[--------------------------------------------------------------------
  995. -- Expression parsing
  996. ----------------------------------------------------------------------]]
  997.  
  998. ------------------------------------------------------------------------
  999. -- parses an expression in parentheses or a single variable
  1000. -- * used in primaryexp()
  1001. ------------------------------------------------------------------------
  1002. function luaY:prefixexp(ls, v)
  1003. -- prefixexp -> NAME | '(' expr ')'
  1004. local c = ls.t.token
  1005. if c == "(" then
  1006. local line = ls.linenumber
  1007. luaX:next(ls)
  1008. self:expr(ls, v)
  1009. self:check_match(ls, ")", "(", line)
  1010. luaK:dischargevars(ls.fs, v)
  1011. elseif c == "TK_NAME" then
  1012. self:singlevar(ls, v)
  1013. else
  1014. luaX:syntaxerror(ls, "unexpected symbol")
  1015. end--if c
  1016. return
  1017. end
  1018.  
  1019. ------------------------------------------------------------------------
  1020. -- parses a prefixexp (an expression in parentheses or a single variable)
  1021. -- or a function call specification
  1022. -- * used in simpleexp(), assignment(), exprstat()
  1023. ------------------------------------------------------------------------
  1024. function luaY:primaryexp(ls, v)
  1025. -- primaryexp ->
  1026. -- prefixexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs }
  1027. local fs = ls.fs
  1028. self:prefixexp(ls, v)
  1029. while true do
  1030. local c = ls.t.token
  1031. if c == "." then -- field
  1032. self:field(ls, v)
  1033. elseif c == "[" then -- '[' exp1 ']'
  1034. local key = {} -- expdesc
  1035. luaK:exp2anyreg(fs, v)
  1036. self:yindex(ls, key)
  1037. luaK:indexed(fs, v, key)
  1038. elseif c == ":" then -- ':' NAME funcargs
  1039. local key = {} -- expdesc
  1040. luaX:next(ls)
  1041. self:checkname(ls, key)
  1042. luaK:_self(fs, v, key)
  1043. self:funcargs(ls, v)
  1044. elseif c == "(" or c == "TK_STRING" or c == "{" then -- funcargs
  1045. luaK:exp2nextreg(fs, v)
  1046. self:funcargs(ls, v)
  1047. else
  1048. return
  1049. end--if c
  1050. end--while
  1051. end
  1052.  
  1053. ------------------------------------------------------------------------
  1054. -- parses general expression types, constants handled here
  1055. -- * used in subexpr()
  1056. ------------------------------------------------------------------------
  1057. function luaY:simpleexp(ls, v)
  1058. -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... |
  1059. -- constructor | FUNCTION body | primaryexp
  1060. local c = ls.t.token
  1061. if c == "TK_NUMBER" then
  1062. self:init_exp(v, "VKNUM", 0)
  1063. v.nval = ls.t.seminfo
  1064. elseif c == "TK_STRING" then
  1065. self:codestring(ls, v, ls.t.seminfo)
  1066. elseif c == "TK_NIL" then
  1067. self:init_exp(v, "VNIL", 0)
  1068. elseif c == "TK_TRUE" then
  1069. self:init_exp(v, "VTRUE", 0)
  1070. elseif c == "TK_FALSE" then
  1071. self:init_exp(v, "VFALSE", 0)
  1072. elseif c == "TK_DOTS" then -- vararg
  1073. local fs = ls.fs
  1074. self:check_condition(ls, fs.f.is_vararg ~= 0,
  1075. "cannot use "..self:LUA_QL("...").." outside a vararg function");
  1076. -- NOTE: the following substitutes for a bitop, but is value-specific
  1077. local is_vararg = fs.f.is_vararg
  1078. if is_vararg >= self.VARARG_NEEDSARG then
  1079. fs.f.is_vararg = is_vararg - self.VARARG_NEEDSARG -- don't need 'arg'
  1080. end
  1081. self:init_exp(v, "VVARARG", luaK:codeABC(fs, "OP_VARARG", 0, 1, 0))
  1082. elseif c == "{" then -- constructor
  1083. self:constructor(ls, v)
  1084. return
  1085. elseif c == "TK_FUNCTION" then
  1086. luaX:next(ls)
  1087. self:body(ls, v, false, ls.linenumber)
  1088. return
  1089. else
  1090. self:primaryexp(ls, v)
  1091. return
  1092. end--if c
  1093. luaX:next(ls)
  1094. end
  1095.  
  1096. ------------------------------------------------------------------------
  1097. -- Translates unary operators tokens if found, otherwise returns
  1098. -- OPR_NOUNOPR. getunopr() and getbinopr() are used in subexpr().
  1099. -- * used in subexpr()
  1100. ------------------------------------------------------------------------
  1101. function luaY:getunopr(op)
  1102. if op == "TK_NOT" then
  1103. return "OPR_NOT"
  1104. elseif op == "-" then
  1105. return "OPR_MINUS"
  1106. elseif op == "#" then
  1107. return "OPR_LEN"
  1108. else
  1109. return "OPR_NOUNOPR"
  1110. end
  1111. end
  1112.  
  1113. ------------------------------------------------------------------------
  1114. -- Translates binary operator tokens if found, otherwise returns
  1115. -- OPR_NOBINOPR. Code generation uses OPR_* style tokens.
  1116. -- * used in subexpr()
  1117. ------------------------------------------------------------------------
  1118. luaY.getbinopr_table = {
  1119. ["+"] = "OPR_ADD",
  1120. ["-"] = "OPR_SUB",
  1121. ["*"] = "OPR_MUL",
  1122. ["/"] = "OPR_DIV",
  1123. ["%"] = "OPR_MOD",
  1124. ["^"] = "OPR_POW",
  1125. ["TK_CONCAT"] = "OPR_CONCAT",
  1126. ["TK_NE"] = "OPR_NE",
  1127. ["TK_EQ"] = "OPR_EQ",
  1128. ["<"] = "OPR_LT",
  1129. ["TK_LE"] = "OPR_LE",
  1130. [">"] = "OPR_GT",
  1131. ["TK_GE"] = "OPR_GE",
  1132. ["TK_AND"] = "OPR_AND",
  1133. ["TK_OR"] = "OPR_OR",
  1134. }
  1135. function luaY:getbinopr(op)
  1136. local opr = self.getbinopr_table[op]
  1137. if opr then return opr else return "OPR_NOBINOPR" end
  1138. end
  1139.  
  1140. ------------------------------------------------------------------------
  1141. -- the following priority table consists of pairs of left/right values
  1142. -- for binary operators (was a static const struct); grep for ORDER OPR
  1143. -- * the following struct is replaced:
  1144. -- static const struct {
  1145. -- lu_byte left; /* left priority for each binary operator */
  1146. -- lu_byte right; /* right priority */
  1147. -- } priority[] = { /* ORDER OPR */
  1148. ------------------------------------------------------------------------
  1149. luaY.priority = {
  1150. {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, -- `+' `-' `/' `%'
  1151. {10, 9}, {5, 4}, -- power and concat (right associative)
  1152. {3, 3}, {3, 3}, -- equality
  1153. {3, 3}, {3, 3}, {3, 3}, {3, 3}, -- order
  1154. {2, 2}, {1, 1} -- logical (and/or)
  1155. }
  1156.  
  1157. luaY.UNARY_PRIORITY = 8 -- priority for unary operators
  1158.  
  1159. ------------------------------------------------------------------------
  1160. -- Parse subexpressions. Includes handling of unary operators and binary
  1161. -- operators. A subexpr is given the rhs priority level of the operator
  1162. -- immediately left of it, if any (limit is -1 if none,) and if a binop
  1163. -- is found, limit is compared with the lhs priority level of the binop
  1164. -- in order to determine which executes first.
  1165. ------------------------------------------------------------------------
  1166.  
  1167. ------------------------------------------------------------------------
  1168. -- subexpr -> (simpleexp | unop subexpr) { binop subexpr }
  1169. -- where 'binop' is any binary operator with a priority higher than 'limit'
  1170. -- * for priority lookups with self.priority[], 1=left and 2=right
  1171. -- * recursively called
  1172. -- * used in expr()
  1173. ------------------------------------------------------------------------
  1174. function luaY:subexpr(ls, v, limit)
  1175. self:enterlevel(ls)
  1176. local uop = self:getunopr(ls.t.token)
  1177. if uop ~= "OPR_NOUNOPR" then
  1178. luaX:next(ls)
  1179. self:subexpr(ls, v, self.UNARY_PRIORITY)
  1180. luaK:prefix(ls.fs, uop, v)
  1181. else
  1182. self:simpleexp(ls, v)
  1183. end
  1184. -- expand while operators have priorities higher than 'limit'
  1185. local op = self:getbinopr(ls.t.token)
  1186. while op ~= "OPR_NOBINOPR" and self.priority[luaK.BinOpr[op] + 1][1] > limit do
  1187. local v2 = {} -- expdesc
  1188. luaX:next(ls)
  1189. luaK:infix(ls.fs, op, v)
  1190. -- read sub-expression with higher priority
  1191. local nextop = self:subexpr(ls, v2, self.priority[luaK.BinOpr[op] + 1][2])
  1192. luaK:posfix(ls.fs, op, v, v2)
  1193. op = nextop
  1194. end
  1195. self:leavelevel(ls)
  1196. return op -- return first untreated operator
  1197. end
  1198.  
  1199. ------------------------------------------------------------------------
  1200. -- Expression parsing starts here. Function subexpr is entered with the
  1201. -- left operator (which is non-existent) priority of -1, which is lower
  1202. -- than all actual operators. Expr information is returned in parm v.
  1203. -- * used in multiple locations
  1204. ------------------------------------------------------------------------
  1205. function luaY:expr(ls, v)
  1206. self:subexpr(ls, v, 0)
  1207. end
  1208.  
  1209. -- }====================================================================
  1210.  
  1211. --[[--------------------------------------------------------------------
  1212. -- Rules for Statements
  1213. ----------------------------------------------------------------------]]
  1214.  
  1215. ------------------------------------------------------------------------
  1216. -- checks next token, used as a look-ahead
  1217. -- * returns boolean instead of 0|1
  1218. -- * used in retstat(), chunk()
  1219. ------------------------------------------------------------------------
  1220. function luaY:block_follow(token)
  1221. if token == "TK_ELSE" or token == "TK_ELSEIF" or token == "TK_END"
  1222. or token == "TK_UNTIL" or token == "TK_EOS" then
  1223. return true
  1224. else
  1225. return false
  1226. end
  1227. end
  1228.  
  1229. ------------------------------------------------------------------------
  1230. -- parse a code block or unit
  1231. -- * used in multiple functions
  1232. ------------------------------------------------------------------------
  1233. function luaY:block(ls)
  1234. -- block -> chunk
  1235. local fs = ls.fs
  1236. local bl = {} -- BlockCnt
  1237. self:enterblock(fs, bl, false)
  1238. self:chunk(ls)
  1239. assert(bl.breaklist == luaK.NO_JUMP)
  1240. self:leaveblock(fs)
  1241. end
  1242.  
  1243. ------------------------------------------------------------------------
  1244. -- structure to chain all variables in the left-hand side of an
  1245. -- assignment
  1246. -- struct LHS_assign:
  1247. -- prev -- (table: struct LHS_assign)
  1248. -- v -- variable (global, local, upvalue, or indexed) (table: expdesc)
  1249. ------------------------------------------------------------------------
  1250.  
  1251. ------------------------------------------------------------------------
  1252. -- check whether, in an assignment to a local variable, the local variable
  1253. -- is needed in a previous assignment (to a table). If so, save original
  1254. -- local value in a safe place and use this safe copy in the previous
  1255. -- assignment.
  1256. -- * used in assignment()
  1257. ------------------------------------------------------------------------
  1258. function luaY:check_conflict(ls, lh, v)
  1259. local fs = ls.fs
  1260. local extra = fs.freereg -- eventual position to save local variable
  1261. local conflict = false
  1262. while lh do
  1263. if lh.v.k == "VINDEXED" then
  1264. if lh.v.info == v.info then -- conflict?
  1265. conflict = true
  1266. lh.v.info = extra -- previous assignment will use safe copy
  1267. end
  1268. if lh.v.aux == v.info then -- conflict?
  1269. conflict = true
  1270. lh.v.aux = extra -- previous assignment will use safe copy
  1271. end
  1272. end
  1273. lh = lh.prev
  1274. end
  1275. if conflict then
  1276. luaK:codeABC(fs, "OP_MOVE", fs.freereg, v.info, 0) -- make copy
  1277. luaK:reserveregs(fs, 1)
  1278. end
  1279. end
  1280.  
  1281. ------------------------------------------------------------------------
  1282. -- parse a variable assignment sequence
  1283. -- * recursively called
  1284. -- * used in exprstat()
  1285. ------------------------------------------------------------------------
  1286. function luaY:assignment(ls, lh, nvars)
  1287. local e = {} -- expdesc
  1288. -- test was: VLOCAL <= lh->v.k && lh->v.k <= VINDEXED
  1289. local c = lh.v.k
  1290. self:check_condition(ls, c == "VLOCAL" or c == "VUPVAL" or c == "VGLOBAL"
  1291. or c == "VINDEXED", "syntax error")
  1292. if self:testnext(ls, ",") then -- assignment -> ',' primaryexp assignment
  1293. local nv = {} -- LHS_assign
  1294. nv.v = {}
  1295. nv.prev = lh
  1296. self:primaryexp(ls, nv.v)
  1297. if nv.v.k == "VLOCAL" then
  1298. self:check_conflict(ls, lh, nv.v)
  1299. end
  1300. self:checklimit(ls.fs, nvars, self.LUAI_MAXCCALLS - ls.L.nCcalls,
  1301. "variables in assignment")
  1302. self:assignment(ls, nv, nvars + 1)
  1303. else -- assignment -> '=' explist1
  1304. self:checknext(ls, "=")
  1305. local nexps = self:explist1(ls, e)
  1306. if nexps ~= nvars then
  1307. self:adjust_assign(ls, nvars, nexps, e)
  1308. if nexps > nvars then
  1309. ls.fs.freereg = ls.fs.freereg - (nexps - nvars) -- remove extra values
  1310. end
  1311. else
  1312. luaK:setoneret(ls.fs, e) -- close last expression
  1313. luaK:storevar(ls.fs, lh.v, e)
  1314. return -- avoid default
  1315. end
  1316. end
  1317. self:init_exp(e, "VNONRELOC", ls.fs.freereg - 1) -- default assignment
  1318. luaK:storevar(ls.fs, lh.v, e)
  1319. end
  1320.  
  1321. ------------------------------------------------------------------------
  1322. -- parse condition in a repeat statement or an if control structure
  1323. -- * used in repeatstat(), test_then_block()
  1324. ------------------------------------------------------------------------
  1325. function luaY:cond(ls)
  1326. -- cond -> exp
  1327. local v = {} -- expdesc
  1328. self:expr(ls, v) -- read condition
  1329. if v.k == "VNIL" then v.k = "VFALSE" end -- 'falses' are all equal here
  1330. luaK:goiftrue(ls.fs, v)
  1331. return v.f
  1332. end
  1333.  
  1334. ------------------------------------------------------------------------
  1335. -- parse a break statement
  1336. -- * used in statements()
  1337. ------------------------------------------------------------------------
  1338. function luaY:breakstat(ls)
  1339. -- stat -> BREAK
  1340. local fs = ls.fs
  1341. local bl = fs.bl
  1342. local upval = false
  1343. while bl and not bl.isbreakable do
  1344. if bl.upval then upval = true end
  1345. bl = bl.previous
  1346. end
  1347. if not bl then
  1348. luaX:syntaxerror(ls, "no loop to break")
  1349. end
  1350. if upval then
  1351. luaK:codeABC(fs, "OP_CLOSE", bl.nactvar, 0, 0)
  1352. end
  1353. bl.breaklist = luaK:concat(fs, bl.breaklist, luaK:jump(fs))
  1354. end
  1355.  
  1356. ------------------------------------------------------------------------
  1357. -- parse a while-do control structure, body processed by block()
  1358. -- * with dynamic array sizes, MAXEXPWHILE + EXTRAEXP limits imposed by
  1359. -- the function's implementation can be removed
  1360. -- * used in statements()
  1361. ------------------------------------------------------------------------
  1362. function luaY:whilestat(ls, line)
  1363. -- whilestat -> WHILE cond DO block END
  1364. local fs = ls.fs
  1365. local bl = {} -- BlockCnt
  1366. luaX:next(ls) -- skip WHILE
  1367. local whileinit = luaK:getlabel(fs)
  1368. local condexit = self:cond(ls)
  1369. self:enterblock(fs, bl, true)
  1370. self:checknext(ls, "TK_DO")
  1371. self:block(ls)
  1372. luaK:patchlist(fs, luaK:jump(fs), whileinit)
  1373. self:check_match(ls, "TK_END", "TK_WHILE", line)
  1374. self:leaveblock(fs)
  1375. luaK:patchtohere(fs, condexit) -- false conditions finish the loop
  1376. end
  1377.  
  1378. ------------------------------------------------------------------------
  1379. -- parse a repeat-until control structure, body parsed by chunk()
  1380. -- * used in statements()
  1381. ------------------------------------------------------------------------
  1382. function luaY:repeatstat(ls, line)
  1383. -- repeatstat -> REPEAT block UNTIL cond
  1384. local fs = ls.fs
  1385. local repeat_init = luaK:getlabel(fs)
  1386. local bl1, bl2 = {}, {} -- BlockCnt
  1387. self:enterblock(fs, bl1, true) -- loop block
  1388. self:enterblock(fs, bl2, false) -- scope block
  1389. luaX:next(ls) -- skip REPEAT
  1390. self:chunk(ls)
  1391. self:check_match(ls, "TK_UNTIL", "TK_REPEAT", line)
  1392. local condexit = self:cond(ls) -- read condition (inside scope block)
  1393. if not bl2.upval then -- no upvalues?
  1394. self:leaveblock(fs) -- finish scope
  1395. luaK:patchlist(ls.fs, condexit, repeat_init) -- close the loop
  1396. else -- complete semantics when there are upvalues
  1397. self:breakstat(ls) -- if condition then break
  1398. luaK:patchtohere(ls.fs, condexit) -- else...
  1399. self:leaveblock(fs) -- finish scope...
  1400. luaK:patchlist(ls.fs, luaK:jump(fs), repeat_init) -- and repeat
  1401. end
  1402. self:leaveblock(fs) -- finish loop
  1403. end
  1404.  
  1405. ------------------------------------------------------------------------
  1406. -- parse the single expressions needed in numerical for loops
  1407. -- * used in fornum()
  1408. ------------------------------------------------------------------------
  1409. function luaY:exp1(ls)
  1410. local e = {} -- expdesc
  1411. self:expr(ls, e)
  1412. local k = e.k
  1413. luaK:exp2nextreg(ls.fs, e)
  1414. return k
  1415. end
  1416.  
  1417. ------------------------------------------------------------------------
  1418. -- parse a for loop body for both versions of the for loop
  1419. -- * used in fornum(), forlist()
  1420. ------------------------------------------------------------------------
  1421. function luaY:forbody(ls, base, line, nvars, isnum)
  1422. -- forbody -> DO block
  1423. local bl = {} -- BlockCnt
  1424. local fs = ls.fs
  1425. self:adjustlocalvars(ls, 3) -- control variables
  1426. self:checknext(ls, "TK_DO")
  1427. local prep = isnum and luaK:codeAsBx(fs, "OP_FORPREP", base, luaK.NO_JUMP)
  1428. or luaK:jump(fs)
  1429. self:enterblock(fs, bl, false) -- scope for declared variables
  1430. self:adjustlocalvars(ls, nvars)
  1431. luaK:reserveregs(fs, nvars)
  1432. self:block(ls)
  1433. self:leaveblock(fs) -- end of scope for declared variables
  1434. luaK:patchtohere(fs, prep)
  1435. local endfor = isnum and luaK:codeAsBx(fs, "OP_FORLOOP", base, luaK.NO_JUMP)
  1436. or luaK:codeABC(fs, "OP_TFORLOOP", base, 0, nvars)
  1437. luaK:fixline(fs, line) -- pretend that `OP_FOR' starts the loop
  1438. luaK:patchlist(fs, isnum and endfor or luaK:jump(fs), prep + 1)
  1439. end
  1440.  
  1441. ------------------------------------------------------------------------
  1442. -- parse a numerical for loop, calls forbody()
  1443. -- * used in forstat()
  1444. ------------------------------------------------------------------------
  1445. function luaY:fornum(ls, varname, line)
  1446. -- fornum -> NAME = exp1,exp1[,exp1] forbody
  1447. local fs = ls.fs
  1448. local base = fs.freereg
  1449. self:new_localvarliteral(ls, "(for index)", 0)
  1450. self:new_localvarliteral(ls, "(for limit)", 1)
  1451. self:new_localvarliteral(ls, "(for step)", 2)
  1452. self:new_localvar(ls, varname, 3)
  1453. self:checknext(ls, '=')
  1454. self:exp1(ls) -- initial value
  1455. self:checknext(ls, ",")
  1456. self:exp1(ls) -- limit
  1457. if self:testnext(ls, ",") then
  1458. self:exp1(ls) -- optional step
  1459. else -- default step = 1
  1460. luaK:codeABx(fs, "OP_LOADK", fs.freereg, luaK:numberK(fs, 1))
  1461. luaK:reserveregs(fs, 1)
  1462. end
  1463. self:forbody(ls, base, line, 1, true)
  1464. end
  1465.  
  1466. ------------------------------------------------------------------------
  1467. -- parse a generic for loop, calls forbody()
  1468. -- * used in forstat()
  1469. ------------------------------------------------------------------------
  1470. function luaY:forlist(ls, indexname)
  1471. -- forlist -> NAME {,NAME} IN explist1 forbody
  1472. local fs = ls.fs
  1473. local e = {} -- expdesc
  1474. local nvars = 0
  1475. local base = fs.freereg
  1476. -- create control variables
  1477. self:new_localvarliteral(ls, "(for generator)", nvars)
  1478. nvars = nvars + 1
  1479. self:new_localvarliteral(ls, "(for state)", nvars)
  1480. nvars = nvars + 1
  1481. self:new_localvarliteral(ls, "(for control)", nvars)
  1482. nvars = nvars + 1
  1483. -- create declared variables
  1484. self:new_localvar(ls, indexname, nvars)
  1485. nvars = nvars + 1
  1486. while self:testnext(ls, ",") do
  1487. self:new_localvar(ls, self:str_checkname(ls), nvars)
  1488. nvars = nvars + 1
  1489. end
  1490. self:checknext(ls, "TK_IN")
  1491. local line = ls.linenumber
  1492. self:adjust_assign(ls, 3, self:explist1(ls, e), e)
  1493. luaK:checkstack(fs, 3) -- extra space to call generator
  1494. self:forbody(ls, base, line, nvars - 3, false)
  1495. end
  1496.  
  1497. ------------------------------------------------------------------------
  1498. -- initial parsing for a for loop, calls fornum() or forlist()
  1499. -- * used in statements()
  1500. ------------------------------------------------------------------------
  1501. function luaY:forstat(ls, line)
  1502. -- forstat -> FOR (fornum | forlist) END
  1503. local fs = ls.fs
  1504. local bl = {} -- BlockCnt
  1505. self:enterblock(fs, bl, true) -- scope for loop and control variables
  1506. luaX:next(ls) -- skip `for'
  1507. local varname = self:str_checkname(ls) -- first variable name
  1508. local c = ls.t.token
  1509. if c == "=" then
  1510. self:fornum(ls, varname, line)
  1511. elseif c == "," or c == "TK_IN" then
  1512. self:forlist(ls, varname)
  1513. else
  1514. luaX:syntaxerror(ls, self:LUA_QL("=").." or "..self:LUA_QL("in").." expected")
  1515. end
  1516. self:check_match(ls, "TK_END", "TK_FOR", line)
  1517. self:leaveblock(fs) -- loop scope (`break' jumps to this point)
  1518. end
  1519.  
  1520. ------------------------------------------------------------------------
  1521. -- parse part of an if control structure, including the condition
  1522. -- * used in ifstat()
  1523. ------------------------------------------------------------------------
  1524. function luaY:test_then_block(ls)
  1525. -- test_then_block -> [IF | ELSEIF] cond THEN block
  1526. luaX:next(ls) -- skip IF or ELSEIF
  1527. local condexit = self:cond(ls)
  1528. self:checknext(ls, "TK_THEN")
  1529. self:block(ls) -- `then' part
  1530. return condexit
  1531. end
  1532.  
  1533. ------------------------------------------------------------------------
  1534. -- parse an if control structure
  1535. -- * used in statements()
  1536. ------------------------------------------------------------------------
  1537. function luaY:ifstat(ls, line)
  1538. -- ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END
  1539. local fs = ls.fs
  1540. local escapelist = luaK.NO_JUMP
  1541. local flist = self:test_then_block(ls) -- IF cond THEN block
  1542. while ls.t.token == "TK_ELSEIF" do
  1543. escapelist = luaK:concat(fs, escapelist, luaK:jump(fs))
  1544. luaK:patchtohere(fs, flist)
  1545. flist = self:test_then_block(ls) -- ELSEIF cond THEN block
  1546. end
  1547. if ls.t.token == "TK_ELSE" then
  1548. escapelist = luaK:concat(fs, escapelist, luaK:jump(fs))
  1549. luaK:patchtohere(fs, flist)
  1550. luaX:next(ls) -- skip ELSE (after patch, for correct line info)
  1551. self:block(ls) -- 'else' part
  1552. else
  1553. escapelist = luaK:concat(fs, escapelist, flist)
  1554. end
  1555. luaK:patchtohere(fs, escapelist)
  1556. self:check_match(ls, "TK_END", "TK_IF", line)
  1557. end
  1558.  
  1559. ------------------------------------------------------------------------
  1560. -- parse a local function statement
  1561. -- * used in statements()
  1562. ------------------------------------------------------------------------
  1563. function luaY:localfunc(ls)
  1564. local v, b = {}, {} -- expdesc
  1565. local fs = ls.fs
  1566. self:new_localvar(ls, self:str_checkname(ls), 0)
  1567. self:init_exp(v, "VLOCAL", fs.freereg)
  1568. luaK:reserveregs(fs, 1)
  1569. self:adjustlocalvars(ls, 1)
  1570. self:body(ls, b, false, ls.linenumber)
  1571. luaK:storevar(fs, v, b)
  1572. -- debug information will only see the variable after this point!
  1573. self:getlocvar(fs, fs.nactvar - 1).startpc = fs.pc
  1574. end
  1575.  
  1576. ------------------------------------------------------------------------
  1577. -- parse a local variable declaration statement
  1578. -- * used in statements()
  1579. ------------------------------------------------------------------------
  1580. function luaY:localstat(ls)
  1581. -- stat -> LOCAL NAME {',' NAME} ['=' explist1]
  1582. local nvars = 0
  1583. local nexps
  1584. local e = {} -- expdesc
  1585. repeat
  1586. self:new_localvar(ls, self:str_checkname(ls), nvars)
  1587. nvars = nvars + 1
  1588. until not self:testnext(ls, ",")
  1589. if self:testnext(ls, "=") then
  1590. nexps = self:explist1(ls, e)
  1591. else
  1592. e.k = "VVOID"
  1593. nexps = 0
  1594. end
  1595. self:adjust_assign(ls, nvars, nexps, e)
  1596. self:adjustlocalvars(ls, nvars)
  1597. end
  1598.  
  1599. ------------------------------------------------------------------------
  1600. -- parse a function name specification
  1601. -- * used in funcstat()
  1602. ------------------------------------------------------------------------
  1603. function luaY:funcname(ls, v)
  1604. -- funcname -> NAME {field} [':' NAME]
  1605. local needself = false
  1606. self:singlevar(ls, v)
  1607. while ls.t.token == "." do
  1608. self:field(ls, v)
  1609. end
  1610. if ls.t.token == ":" then
  1611. needself = true
  1612. self:field(ls, v)
  1613. end
  1614. return needself
  1615. end
  1616.  
  1617. ------------------------------------------------------------------------
  1618. -- parse a function statement
  1619. -- * used in statements()
  1620. ------------------------------------------------------------------------
  1621. function luaY:funcstat(ls, line)
  1622. -- funcstat -> FUNCTION funcname body
  1623. local v, b = {}, {} -- expdesc
  1624. luaX:next(ls) -- skip FUNCTION
  1625. local needself = self:funcname(ls, v)
  1626. self:body(ls, b, needself, line)
  1627. luaK:storevar(ls.fs, v, b)
  1628. luaK:fixline(ls.fs, line) -- definition 'happens' in the first line
  1629. end
  1630.  
  1631. ------------------------------------------------------------------------
  1632. -- parse a function call with no returns or an assignment statement
  1633. -- * used in statements()
  1634. ------------------------------------------------------------------------
  1635. function luaY:exprstat(ls)
  1636. -- stat -> func | assignment
  1637. local fs = ls.fs
  1638. local v = {} -- LHS_assign
  1639. v.v = {}
  1640. self:primaryexp(ls, v.v)
  1641. if v.v.k == "VCALL" then -- stat -> func
  1642. luaP:SETARG_C(luaK:getcode(fs, v.v), 1) -- call statement uses no results
  1643. else -- stat -> assignment
  1644. v.prev = nil
  1645. self:assignment(ls, v, 1)
  1646. end
  1647. end
  1648.  
  1649. ------------------------------------------------------------------------
  1650. -- parse a return statement
  1651. -- * used in statements()
  1652. ------------------------------------------------------------------------
  1653. function luaY:retstat(ls)
  1654. -- stat -> RETURN explist
  1655. local fs = ls.fs
  1656. local e = {} -- expdesc
  1657. local first, nret -- registers with returned values
  1658. luaX:next(ls) -- skip RETURN
  1659. if self:block_follow(ls.t.token) or ls.t.token == ";" then
  1660. first, nret = 0, 0 -- return no values
  1661. else
  1662. nret = self:explist1(ls, e) -- optional return values
  1663. if self:hasmultret(e.k) then
  1664. luaK:setmultret(fs, e)
  1665. if e.k == "VCALL" and nret == 1 then -- tail call?
  1666. luaP:SET_OPCODE(luaK:getcode(fs, e), "OP_TAILCALL")
  1667. assert(luaP:GETARG_A(luaK:getcode(fs, e)) == fs.nactvar)
  1668. end
  1669. first = fs.nactvar
  1670. nret = self.LUA_MULTRET -- return all values
  1671. else
  1672. if nret == 1 then -- only one single value?
  1673. first = luaK:exp2anyreg(fs, e)
  1674. else
  1675. luaK:exp2nextreg(fs, e) -- values must go to the 'stack'
  1676. first = fs.nactvar -- return all 'active' values
  1677. assert(nret == fs.freereg - first)
  1678. end
  1679. end--if
  1680. end--if
  1681. luaK:ret(fs, first, nret)
  1682. end
  1683.  
  1684. ------------------------------------------------------------------------
  1685. -- initial parsing for statements, calls a lot of functions
  1686. -- * returns boolean instead of 0|1
  1687. -- * used in chunk()
  1688. ------------------------------------------------------------------------
  1689. function luaY:statement(ls)
  1690. local line = ls.linenumber -- may be needed for error messages
  1691. local c = ls.t.token
  1692. if c == "TK_IF" then -- stat -> ifstat
  1693. self:ifstat(ls, line)
  1694. return false
  1695. elseif c == "TK_WHILE" then -- stat -> whilestat
  1696. self:whilestat(ls, line)
  1697. return false
  1698. elseif c == "TK_DO" then -- stat -> DO block END
  1699. luaX:next(ls) -- skip DO
  1700. self:block(ls)
  1701. self:check_match(ls, "TK_END", "TK_DO", line)
  1702. return false
  1703. elseif c == "TK_FOR" then -- stat -> forstat
  1704. self:forstat(ls, line)
  1705. return false
  1706. elseif c == "TK_REPEAT" then -- stat -> repeatstat
  1707. self:repeatstat(ls, line)
  1708. return false
  1709. elseif c == "TK_FUNCTION" then -- stat -> funcstat
  1710. self:funcstat(ls, line)
  1711. return false
  1712. elseif c == "TK_LOCAL" then -- stat -> localstat
  1713. luaX:next(ls) -- skip LOCAL
  1714. if self:testnext(ls, "TK_FUNCTION") then -- local function?
  1715. self:localfunc(ls)
  1716. else
  1717. self:localstat(ls)
  1718. end
  1719. return false
  1720. elseif c == "TK_RETURN" then -- stat -> retstat
  1721. self:retstat(ls)
  1722. return true -- must be last statement
  1723. elseif c == "TK_BREAK" then -- stat -> breakstat
  1724. luaX:next(ls) -- skip BREAK
  1725. self:breakstat(ls)
  1726. return true -- must be last statement
  1727. else
  1728. self:exprstat(ls)
  1729. return false -- to avoid warnings
  1730. end--if c
  1731. end
  1732.  
  1733. ------------------------------------------------------------------------
  1734. -- parse a chunk, which consists of a bunch of statements
  1735. -- * used in parser(), body(), block(), repeatstat()
  1736. ------------------------------------------------------------------------
  1737. function luaY:chunk(ls)
  1738. -- chunk -> { stat [';'] }
  1739. local islast = false
  1740. self:enterlevel(ls)
  1741. while not islast and not self:block_follow(ls.t.token) do
  1742. islast = self:statement(ls)
  1743. self:testnext(ls, ";")
  1744. assert(ls.fs.f.maxstacksize >= ls.fs.freereg and
  1745. ls.fs.freereg >= ls.fs.nactvar)
  1746. ls.fs.freereg = ls.fs.nactvar -- free registers
  1747. end
  1748. self:leavelevel(ls)
  1749. end
  1750.  
  1751. -- }======================================================================
  1752. return luaY
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement