YT_REALmrsiniac

lus

Jul 31st, 2025
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 34.53 KB | None | 0 0
  1. local raw_tokens = dofile("Tokens.lua") -- output from your tokenizer
  2.  
  3. --functions for env
  4. function table.find(tbl, val)
  5. for i, v in ipairs(tbl) do
  6. if v == val then return i end
  7. end
  8. return nil
  9. end
  10.  
  11. --vars
  12. local BlockFollowKeyword = {'else', 'elseif', 'until', 'end'}
  13.  
  14. local UnopSet = {'-', 'not', '#'}
  15.  
  16. local BinopSet = {'+', '-', '*', '/', '%', '^', '#', '..', '.', ':', '>', '<', '<=', '>=', '~=', '==','+=', '-=', '*=', '/=', '%=', '^=', '..=','and', 'or'}
  17.  
  18. local Compounds = {'+=', '-=', '*=', '/=', '^=', '..=', '%='}
  19.  
  20. local UnaryPriority = 8
  21.  
  22. -- Filter out newline tokens
  23. local tokens = {}
  24. for _, token in ipairs(raw_tokens) do
  25. if token.kind ~= "newline" then
  26. table.insert(tokens, token)
  27. end
  28. end
  29.  
  30. function ParseTokens(text)
  31. local p = 1
  32. local length = #tokens
  33.  
  34. function get()
  35.  
  36. local tok = tokens[p]
  37.  
  38.  
  39. if p < #tokens then
  40. p = p + 1
  41. end
  42. return tok
  43. end
  44.  
  45. function peek(n)
  46. local index = p + (n or 0)
  47.  
  48. return tokens[index] or tokens[#tokens]
  49. end
  50.  
  51. function look(n)
  52. n = n or 0
  53.  
  54. if p <= length then
  55. return string.sub(text, p + n, p + n)
  56. else
  57. return ""
  58. end
  59. end
  60.  
  61. function isBlockFollow()
  62. local token = peek()
  63. return token and token.kind == "keyword" and table.find(BlockFollowKeyword, token.value)
  64. end
  65.  
  66. function isUnop()
  67. local token = peek()
  68. return token and table.find(UnopSet, token.value) or false
  69. end
  70.  
  71. function isBinop()
  72. local token = peek()
  73. return token and table.find(BinopSet, token.value) or false
  74. end
  75.  
  76. function expect(expected_type, expected_source)
  77. local tk = peek()
  78. if tk and tk.Type == expected_type and (expected_source == nil or tk.Source == expected_source) then
  79. return get()
  80. else
  81. for i = -3, 3 do
  82. local lookahead = peek(i)
  83.  
  84. print(string.format("Tokens[%d] = `%s`", i, lookahead and lookahead.Source or "nil"))
  85. end
  86. end
  87. end
  88.  
  89. function MkNode(node)
  90. local getf = node.GetFirstToken
  91. local getl = node.GetLastToken
  92.  
  93. local self = node
  94.  
  95. node.GetFirstToken = function ()
  96. local t = getf(self)
  97. assert(t, "Node has no first token")
  98. return t
  99. end
  100.  
  101. node.GetLastToken = function ()
  102. local t = getl(self)
  103. assert(t, "Node has no last token")
  104. return t
  105. end
  106.  
  107. return node
  108. end
  109.  
  110. local block;
  111. local expr;
  112.  
  113. function varlist(acceptVarg)
  114. local varList = {}
  115. local commaList = {}
  116.  
  117. if peek().kind == "identifier" then
  118. table.insert(varList, get())
  119. elseif peek().val == "..." and acceptVarg then
  120. return {varList, commaList, get()}
  121. end
  122.  
  123. while peek().val == "," do
  124. table.insert(commaList, get())
  125. if peek().val == "..." and acceptVarg then
  126. return {varList, commaList, get()}
  127. else
  128. local id = expect("identifier")
  129. table.insert(varList, id)
  130. end
  131. end
  132. return {varList, commaList}
  133. end
  134.  
  135. function funcdecl(isAnonymous, locals, upvals, loc)
  136. local functionKw = get()
  137.  
  138. local nameChain
  139. local nameChainSeperator
  140.  
  141. if not isAnonymous then
  142. nameChain = {}
  143. nameChainSeperator = {}
  144.  
  145. local token = peek()
  146. table.insert(nameChain, token)
  147.  
  148. while peek().val == "."do
  149. table.insert(nameChainSeperator, get())
  150. table.insert(nameChain, expect("Ident"))
  151. end
  152.  
  153. if peek().val == ":" then
  154. table.insert(nameChainSeperator, get())
  155. table.insert(nameChain, expect("Ident"))
  156. end
  157. end
  158.  
  159. local oparenTk = expect("Symbol", "(")
  160. local argList, argCommaList, vargToken = varlist(true)
  161. local cparenTk = expect("Symbol", ")")
  162. local fbody, enTk = blockbody("end", locals, upvals)
  163.  
  164. node = MkNode({
  165. Type = (isAnonymous == true and "FunctionLiteral" or "FunctionStat"),
  166. NameChain = nameChain,
  167. ArgList = argList,
  168. Body = fbody,
  169. Token_Function = functionKw,
  170. Token_NameChainSeperator = nameChainSeperator,
  171. Token_OpenParen = oparenTk,
  172. Token_Varg = vargToken,
  173. Token_CloseParen = cparenTk,
  174. Token_End = enTk,
  175. GetFirstToken = function() return node.Token_Function end,
  176. GetLastToken = function() return node.Token_End end,
  177. })
  178. return node
  179. end
  180.  
  181. function tableexpr(locals, upvals)
  182. local obrace = expect("Symbol", "{")
  183. local entries = {}
  184. local seperators = {}
  185. local length = 0
  186. local lastIndex
  187. local valLen = 0
  188.  
  189. while peek().val ~= "}" do
  190. local index
  191. local val
  192. if peek().val == "{" then
  193. local obrac = get()
  194. local index = expr(locals, upvals)
  195. local cbrac = expect("Symbol", "]")
  196. local eq = expect("Symbol", "=")
  197. local value = expr(locals, upvals)
  198.  
  199. local indx = index.Token and index.Token.val or false
  200. val = value
  201.  
  202. table.insert(entries, {
  203. EntryType = "Index",
  204. Index = index,
  205. Value = value,
  206. Token_OpenBracket = obrac,
  207. Token_CloseBracket = cbrac,
  208. Token_Equals = eq,
  209. })
  210. elseif peek().kind == "identifier" then
  211. local field = get()
  212. local eq = get()
  213. local value = expr(locals, upvals)
  214.  
  215. inds = field
  216. val = value
  217. table.insert(entries, {
  218. EntryType = "Field",
  219. Field = field,
  220. Value = value,
  221. Token_Equals = eq,
  222. })
  223. else
  224. local value = expr(locals, upvals)
  225.  
  226. table.insert(entries, {
  227. EntryType = "Value",
  228. Value = value,
  229. })
  230. end
  231.  
  232. if peek().val == "," or peek().val == ";" then
  233. table.insert(seperators, get())
  234. else
  235. break
  236. end
  237. end
  238.  
  239. local cbrace = expect("Symbol", "}")
  240. node = MkNode({
  241. Type = "TableLiteral",
  242. EntryList = entries,
  243. Token_SeperatorList = seperators,
  244. Token_OpenBrace = obrace,
  245. Token_CloseBrace = cbrace,
  246. GetFirstToken = function(self)
  247. return self.Token_OpenBrace
  248. end,
  249. GetLastToken = function(self)
  250. return self.Token_CloseBrace
  251. end
  252. })
  253. return node
  254. end
  255.  
  256. function prefixexpr(locals, upvals)
  257. local tk = get()
  258. --print(tk)
  259. if tk.val == "(" then
  260. local oparenTk = get()
  261. local inner = expr(locals, upvals)
  262. local cparenTk = expect('Symbol', ')')
  263.  
  264. node = MkNode({
  265. Type = "ParenExpr",
  266. Expression = inner,
  267. Token_OpenParen = oparenTk,
  268. Token_CloseParen = cparenTk,
  269. GetFirstToken = function(self)
  270. return self.Token_OpenParen
  271. end,
  272. GetLastToken = function(self)
  273. return self.Token_CloseParen
  274. end
  275. })
  276. return node
  277. elseif tk.kind == "identifier" then
  278. node = MkNode({
  279. Type = "VariableExpr",
  280. Token = get(),
  281. GetFirstToken = function(self) return self.Token end,
  282. GetLastToken = function(self) return self.Token end
  283. })
  284.  
  285. if locals[node.Token.Source] ~= nil then
  286. table.insert(locals[node.Token.Source].Tokens, node.Token)
  287. locals[node.Token.Source]:UseCountIncrease()
  288. elseif upvals[node.Token.Source] ~= nil then
  289. table.insert(upvals[node.Token.Source].Tokens, node.Token)
  290. upvals[node.Token.Source]:UseCountIncrease()
  291. end
  292.  
  293. return node
  294. end
  295. end
  296.  
  297. function functionargs(locals, upvals)
  298. local tk = peek()
  299. if tk.val == "(" then
  300. local oparenTk = get()
  301. local argList = {}
  302. local argCommaList = {}
  303.  
  304. while peek().val ~= ")" do
  305. table.insert(argList, expr(locals, upvals))
  306. if peek().val == "," then
  307. table.insert(argCommaList, get())
  308. else
  309. break
  310. end
  311. end
  312.  
  313. local cparenTk = expect("Symbol", ")")
  314. local node = MkNode({
  315. CallType = "ArgCall",
  316. ArgList = argList,
  317. Token_OpenParen = oparenTk,
  318. Token_CloseParen = cparenTk,
  319. GetFirstToken = function(self)
  320. return self.Token_OpenParen
  321. end,
  322. GetLastToken = function(self)
  323. return self.Token_CloseParen
  324. end
  325. })
  326. return node
  327. elseif tk.val == "{" then
  328. node = MkNode({
  329. CallType = "TableCall",
  330. TableExpr = expr(locals, upvals),
  331. GetFirstToken = function(self)
  332. return self.TableExpr:GetFirstToken()
  333. end,
  334. GetLastToken = function(self)
  335. return self.TableExpr:GetLastToken()
  336. end
  337. })
  338. return node
  339. elseif tk.kind == "string" then
  340. node = MkNode({
  341. CallType = "String",
  342. Token = get(),
  343. GetFirstToken = function(self) return self.Token end,
  344. GetLastToken = function(self) return self.Token end
  345. })
  346. return node
  347. else
  348. error("Function arguments expected.")
  349. end
  350. end
  351.  
  352. function primaryexpr(locals, upvals)
  353. local base = prefixexpr(locals, upvals)
  354. assert(base, "Expected a primary expression")
  355.  
  356. while true do
  357. local tk = peek()
  358. if tk.val == "." then
  359. local dotTk = get()
  360. local fieldName = expect("name")
  361. local node = MkNode{
  362. Type = "FieldExpr",
  363. Base = base,
  364. Field = fieldName,
  365. Token_Dot = dotTk,
  366. GetFirstToken = function(self)
  367. return self.Base:GetFirstToken()
  368. end,
  369. GetLastToken = function(self)
  370. return self.Field
  371. end
  372. }
  373. elseif tk.val == ":" then
  374. local colonTk = get()
  375. local methodName = expect("Ident")
  376. local fargs = functionargs(locals, upvals)
  377. local node = MkNode{
  378. Type = "MethodExpr",
  379. Base = base,
  380. Method = methodName,
  381. FunctionArguments = fargs,
  382. Token_Colon = colonTk,
  383. GetFirstToken = function(self)
  384. return self.Base:GetFirstToken()
  385. end,
  386. GetLastToken = function(self)
  387. return self.FunctionArguments:GetLastToken()
  388. end
  389. }
  390. base = node
  391. elseif tk.val == "[" then
  392. local colonTk = get()
  393. local methodName = expect("Ident")
  394.  
  395. local node = MkNode{
  396. Type = "MethodExpr",
  397. Base = base,
  398. Method = methodName,
  399. FunctionArguments = functionargs(locals, upvals),
  400. Token_Colon = colonTk,
  401. GetFirstToken = function(self)
  402. return self.Base:GetFirstToken()
  403. end,
  404. GetLastToken = function(self)
  405. return self.FunctionArguments:GetLastToken()
  406. end
  407. }
  408. base = node
  409. elseif tk.val == "{" then
  410. node = MkNode({
  411. Type = "CallExpr",
  412. Base = base,
  413. FunctionArguments = functionargs(locals, upvals),
  414. GetFirstToken = function(self)
  415. return self.Base:GetFirstToken()
  416. end,
  417. GetLastToken = function(self)
  418. return self.FunctionArguments:GetLastToken()
  419. end
  420. })
  421. base = node
  422. elseif table.find(Compounds, tk.val) then
  423. local compoundTk = get()
  424. local rhs = expr(locals, upvals)
  425. local node = MkNode({
  426. Type = "CompoundStat",
  427. Base = base,
  428. Token_Compound = compoundTk,
  429. Rhs = rhs,
  430. Lhs = base,
  431. GetFirstToken = function(self)
  432. return self.Base:GetFirstToken()
  433. end,
  434. GetLastToken = function(self)
  435. return self.Rhs:GetLastToken()
  436. end
  437. })
  438. base = node
  439. else
  440. return base
  441. end
  442. end
  443. end
  444.  
  445. function simpleexpr(locals, upvals)
  446. local tk = peek()
  447. if tk.kind == "number" then
  448. local node = MkNode({
  449. Type = "NumberExpr",
  450. Token = get(),
  451. GetFirstToken = function(self) return self.Token end,
  452. GetLastToken = function(self) return self.Token end
  453. })
  454. return node
  455. elseif tk.kind == "string" then
  456. node = MkNode({
  457. Type = "StringLiteral",
  458. Token = get(),
  459. GetFirstToken = function(self) return self.Token end,
  460. GetLastToken = function(self) return self.Token end
  461. })
  462. return node
  463. elseif tk.kind == "hash" then
  464. local node = MkNode({
  465. Type = "HashLiteral",
  466. Token = get(),
  467. GetFirstToken = function(self) return self.Token end,
  468. GetLastToken = function(self) return self.Token end
  469. })
  470. return node
  471. elseif tk.val == "nil" then
  472. local node = MkNode({
  473. Type = "NilLiteral",
  474. Token = get(),
  475. GetFirstToken = function(self) return self.Token end,
  476. GetLastToken = function(self) return self.Token end
  477. })
  478. return node
  479. elseif tk.val == "true" or tk.val == "false" then
  480. node = MkNode({
  481. Type = "BooleanLiteral",
  482. Token = get(),
  483. GetFirstToken = function(self) return self.Token end,
  484. GetLastToken = function(self) return self.Token end
  485. })
  486. return node
  487. elseif tk.val == "..." then
  488. local node = MkNode({
  489. Type = "VargLiteral",
  490. Token = get(),
  491. GetFirstToken = function(self) return self.Token end,
  492. GetLastToken = function(self) return self.Token end
  493. })
  494. return node
  495. elseif tk.val == "{" then
  496. return tableexpr(locals, upvals)
  497. elseif tk.val == "function" then
  498. return funcdecl(true, locals, upvals)
  499. else
  500. return primaryexpr(locals, upvals)
  501. end
  502. end
  503.  
  504. function subexpr(limit, locals, upvals)
  505. local curNode;
  506. if isUnop() then
  507. local opTk = get()
  508. local ex = subexpr(UnaryPriority, locals, upvals)
  509. local node = MkNode{
  510. Type = "UnopExpr",
  511. Token_Op = opTk,
  512. Rhs = ex,
  513. GetFirstToken = function(self)
  514. return self.Token_Op
  515. end,
  516. GetLastToken = function(self)
  517. return self.Rhs:GetLastToken()
  518. end
  519. }
  520. curNode = node
  521. else
  522. curNode = simpleexpr(locals, upvals)
  523. assert(curNode, "Expected a simple expression")
  524. end
  525. end
  526.  
  527. function expr(locals, upvals)
  528. return subexpr(0, locals, upvals)
  529. end
  530.  
  531. function exprlist(locals, upvals)
  532. local exprList = { expr(locals, upvals) }
  533. local commaList = {}
  534.  
  535. while peek().Source == "," do
  536. table.insert(commaList, get())
  537. table.insert(exprList, expr(locals, upvals))
  538. end
  539.  
  540. return {exprList, commaList}
  541. end
  542.  
  543. function blockbody(terminator, locals, upvals)
  544. local body = block(locals, upvals)
  545. local after = peek()
  546. if after.kind == "keyword" and after.value == terminator then
  547. return body, get()
  548. end
  549. end
  550.  
  551. function subexpr(limit, locals, upvals)
  552. local curNode
  553.  
  554. if isUnop() then
  555. local opTK = get()
  556. local ex = subexpr(UnaryPriority, locals, upvals)
  557. node = MkNode({
  558. Type = "UnopExpr",
  559. Token_Op = opTk,
  560. Rhs = ex,
  561. GetFirstToken = function() return node.Token_Op end,
  562. GetLastToken = function() return node.Rhs:GetLastToken() end,
  563. })
  564. curNode = node
  565. else
  566. curNode = simpleexpr(locals, upvals)
  567. assert(curNode, "nil simpleexpr")
  568. end
  569. end
  570.  
  571. expr = function(locals, upvals)
  572. return subexpr(0, locals, upvals)
  573. end
  574.  
  575. function exprstat(locals, upvals)
  576. local ex = primaryexpr(locals, upvals)
  577.  
  578. if ex.kind == "MethodExpr" or ex.Type == "CallExpr" then
  579. node = MkNode({
  580. Type = "CallExprStat",
  581. Expression = ex,
  582. GetFirstToken = function(self)
  583. return self.Expression:GetFirstToken()
  584. end,
  585. GetLastToken = function(self)
  586. return self.Expression:GetLastToken()
  587. end
  588. })
  589. return node
  590. elseif ex.kind == "CompoundStat" then
  591. return ex
  592. else
  593. local lhs = {ex}
  594. local lhsSeperator = {}
  595.  
  596. while peek().val == "," do
  597. table.insert(lhsSeperator, get())
  598. local lhsPart = primaryexpr(locals, upvals)
  599. if lhsPart.Type == "MethodExpr" or lhsPart.Type == "CallExpr" then
  600. error("Bad left hand side of assignment")
  601. end
  602. table.insert(lhs, lhsPart)
  603. end
  604.  
  605. local eq = expect("Symbol", "=")
  606. local rhs = { expr(locals, upvals) }
  607. local rhsSeperator = {}
  608.  
  609. while peek().val == "," do
  610. table.insert(rhsSeperator, get())
  611. table.insert(rhs, expr(locals, upvals))
  612. end
  613.  
  614. node = MkNode({
  615. Type = "AssignmentStat",
  616. Rhs = rhs,
  617. Lhs = lhs,
  618. Token_Equals = eq,
  619. Token_LhsSeperatorList = lhsSeperator,
  620. Token_RhsSeperatorList = rhsSeperator,
  621. GetFirstToken = function(self)
  622. return self.Lhs[1]:GetFirstToken()
  623. end,
  624. GetLastToken = function(self)
  625. return self.Rhs[#self.Rhs]:GetLastToken()
  626. end
  627. })
  628. return node
  629. end
  630. end
  631.  
  632. function ifstat(locals, upvals)
  633. local ifKw = get()
  634. local condition = expr(locals, upvals)
  635. local thenKw = expect("Keyword", "then")
  636. local ifBody = block(locals, upvals)
  637. local elseClauses = {}
  638. while peek().val == "elseif" or peek().val == "else" do
  639. local elseifKw = get()
  640. local elseifCondition
  641. local elseifThenKw
  642. if elseifKw.val == "elseif" then
  643. elseifCondition = expr(locals, upvals)
  644. elseifThenKw = expect("Keyword", "then")
  645. end
  646. local elseifBody = block(locals, upvals)
  647. table.insert(elseClauses, {
  648. Condition = elseifCondition,
  649. Body = elseifBody,
  650. ClauseType = elseifKw.val,
  651. Token = elseifKw,
  652. Token_Then = elseifThenKw
  653. })
  654. if elseifKw.val == "else" then
  655. break
  656. end
  657. end
  658.  
  659. local enKw = expect("Keyword", "end")
  660. node = MkNode({
  661. Type = "IfStat",
  662. Condition = condition,
  663. Body = ifBody,
  664. ElseClauseList = elseClauses,
  665. Token_If = ifKw,
  666. Token_Then = thenKw,
  667. Token_End = enKw,
  668. GetFirstToken = function(self)
  669. return self.Token_If
  670. end,
  671. GetLastToken = function(self)
  672. return self.Token_End
  673. end
  674. })
  675. return node
  676. end
  677.  
  678. function dostat(locals, upvals)
  679. local doKw = get()
  680. local body, enKw = blockbody("end", locals, upvals)
  681.  
  682. node = MkNode({
  683. Type = "DoStat",
  684. Body = body,
  685. Token_Do = doKw,
  686. Token_End = enKw,
  687. GetFirstToken = function(self)
  688. return self.Token_Do
  689. end,
  690. GetLastToken = function(self)
  691. return self.Token_End
  692. end
  693. })
  694. return node
  695. end
  696.  
  697. function whilestat(locals, upvals)
  698. local whileKw = get()
  699. local condition = expr(locals, upvals)
  700. local thenKw = expect("Keyword", "do")
  701. local body, enKw = blockbody("end", locals, upvals)
  702. node = MkNode({
  703. Type = "WhileStat",
  704. Condition = condition,
  705. Body = body,
  706. Token_While = whileKw,
  707. Token_Do = thenKw,
  708. Token_End = enKw,
  709. GetFirstToken = function(self)
  710. return self.Token_While
  711. end,
  712. GetLastToken = function(self)
  713. return self.Token_End
  714. end
  715. })
  716. return node
  717. end
  718.  
  719. function forstat(locals, upvals)
  720. local forkw = get()
  721. local loopVars, loopVarCommas = varlist()
  722. local node = {}
  723.  
  724. if peek().val == "=" then
  725. local eqTk = get()
  726. local exprList = exprlist(locals, upvals)
  727. if #exprList < 2 or #exprList > 3 then
  728. error("Expected 2 or 3 expressions for numeric for loop")
  729. end
  730. local doTk = expect("Keyword", "do")
  731. local body, enTk = blockbody("end", locals, upvals)
  732. node = MkNode({
  733. Type = "NumericForStat",
  734. VarList = loopVars,
  735. RangeList = exprList,
  736. Body = body,
  737. Token_For = forkw,
  738. Token_Equals = eqTk,
  739. Token_Do = doTk,
  740. Token_End = enTk,
  741. GetFirstToken = function(self)
  742. return self.Token_For
  743. end,
  744. GetLastToken = function(self)
  745. return self.Token_End
  746. end
  747. })
  748. return node
  749. elseif peek().val == "in" then
  750. local inTk = get()
  751. local exprList = exprlist(locals, upvals)
  752. local doTk = expect("Keyword", "do")
  753. local body, enTk = blockbody("end", locals, upvals)
  754. node = MkNode({
  755. Type = "GenericForStat",
  756. VarList = loopVars,
  757. GeneratorList = exprList,
  758. Body = body,
  759. Token_For = forkw,
  760. Token_In = inTk,
  761. Token_Do = doTk,
  762. Token_End = enTk,
  763. GetFirstToken = function(self)
  764. return self.Token_For
  765. end,
  766. GetLastToken = function(self)
  767. return self.Token_End
  768. end
  769. })
  770. return node
  771. end
  772. end
  773.  
  774. function repeatstat(locals, upvals)
  775. local repeatKw = get()
  776. local body, untilTk = blockbody("until", locals, upvals)
  777. local condition = expr(locals, upvals)
  778. node = MkNode({
  779. Type = "RepeatStat",
  780. Body = body,
  781. Condition = condition,
  782. Token_Repeat = repeatKw,
  783. Token_Until = untilTk,
  784. GetFirstToken = function(self)
  785. return self.Token_Repeat
  786. end,
  787. GetLastToken = function(self)
  788. return self.Condition:GetLastToken()
  789. end
  790. })
  791. return node
  792. end
  793.  
  794. function localdecl(locals, upvals)
  795. local localKw = get()
  796.  
  797. if peek().val == "function" then
  798. local funcStat = funcdecl(false, locals, upvals, true)
  799. node = MkNode({
  800. Type = "LocalFunctionStat",
  801. FunctionStat = funcStat,
  802. Token_Local = localKw,
  803. GetFirstToken = function(self)
  804. return self.Token_Local
  805. end,
  806. GetLastToken = function(self)
  807. return self.FunctionStat:GetLastToken()
  808. end
  809. })
  810. return node
  811. elseif peek().kind == "identifier" then
  812. local varList, varCommaList = varlist(false)
  813. local exprList = {}
  814. local eqToken
  815. if peek().val == "=" then
  816. eqToken = get()
  817. local exprList1 = exprlist(locals, upvals)
  818. exprList = exprlist(locals, upvals)
  819.  
  820. node = MkNode({
  821. Type = "LocalVarStat",
  822. VarList = varList,
  823. ExprList = exprList,
  824. Token_Local = localKw,
  825. Token_Equals = eqToken,
  826. GetFirstToken = function(self)
  827. return self.Token_Local
  828. end,
  829. GetLastToken = function()
  830. if #node.ExprList > 0 then
  831. return node.ExprList[#node.ExprList]:GetLastToken()
  832. else
  833. return node.VarList[#node.VarList]
  834. end
  835. end
  836. })
  837. return node
  838. else
  839. error("`function` or ident expected")
  840. end
  841. end
  842. end
  843.  
  844. function retstat(locals, upvals)
  845. local returnKw = get()
  846. local exprList
  847. local commaList
  848.  
  849. if isBlockFollow() then
  850. exprList = {}
  851. commaList = {}
  852. else
  853. exprList, commaList = exprlist(locals, upvals)
  854. end
  855. local self
  856. self = {
  857. Type = "ReturnStat",
  858. ExprList = exprList,
  859. Token_Return = returnKw,
  860. Token_CommaList = commaList,
  861. GetFirstToken = function(self)
  862. return self.Token_Return
  863. end,
  864. GetLastToken = function(self)
  865. if #self.ExprList > 0 then
  866. return self.ExprList[#self.ExprList]:GetLastToken()
  867. else
  868. return self.Token_Return
  869. end
  870. end
  871. }
  872. return self
  873. end
  874.  
  875. function breakstat(locals, upvals)
  876. local breakKw = get()
  877. local self = {
  878. Type = "BreakStat",
  879. Token_Break = breakKw,
  880. GetFirstToken = function(self)
  881. return self.Token_Break
  882. end,
  883. GetLastToken = function(self)
  884. return self.Token_Break
  885. end
  886. }
  887. return self
  888. end
  889.  
  890. function continuestat(locals, upvals)
  891. local continueKw = get()
  892. local self = {
  893. Type = "ContinueStat",
  894. Token_Continue = continueKw,
  895. GetFirstToken = function(self)
  896. return self.Token_Continue
  897. end,
  898. GetLastToken = function(self)
  899. return self.Token_Continue
  900. end
  901. }
  902. return self
  903. end
  904.  
  905. function statement(locals, upvals)
  906. local tok = peek()
  907. if tok.val == "if" then
  908. return { false, ifstat(locals, upvals) }
  909. elseif tok.val == "while" then
  910. return { false, whilestat(locals, upvals) }
  911. elseif tok.val == "do" then
  912. return { false, dostat(locals, upvals) }
  913. elseif tok.val == "for" then
  914. return { false, forstat(locals, upvals) }
  915. elseif tok.val == "repeat" then
  916. return { false, repeatstat(locals, upvals) }
  917. elseif tok.val == "function" then
  918. return { false, funcdecl(false, locals, upvals) }
  919. elseif tok.val == "local" then
  920. return { false, localdecl(locals, upvals) }
  921. elseif tok.val == "return" then
  922. return { true, retstat(locals, upvals) }
  923. elseif tok.val == "break" then
  924. return { true, breakstat(locals, upvals) }
  925. elseif tok.val == "continue" then
  926. return { true, continuestat(locals, upvals) }
  927. else
  928. return { false, exprstat(locals, upvals) }
  929. end
  930. end
  931.  
  932. local blocks = 1
  933. block = function(a,b)
  934. local myblocknum = blocks + 1
  935. local statements = {}
  936. local semicolons = {}
  937. local isLast = false
  938. local locals = {}
  939. local upvals = {}
  940.  
  941. if b ~= nil then
  942. for i,v in pairs(b) do
  943. upvals[i] = v
  944. end
  945. end
  946. if a ~= nil then
  947. for i,v in pairs(a) do
  948. upvals[i] = v
  949. end
  950. end
  951.  
  952. local thing
  953. local i = 0
  954.  
  955. while not isLast and not isBlockFollow() do
  956. if thing and thing == peek() then
  957. print("INFINITE LOOP POSSIBLE ON STATEMENT " .. thing.val .. " :", thing)
  958. end
  959.  
  960. thing = peek()
  961. isLast, stat = statement(locals, upvals)
  962.  
  963. if stat then
  964. table.insert(statements, statement)
  965.  
  966. if stat.Type == "LocalVarStat" then
  967. for _, token in ipairs(stat.VarList) do
  968. token.UseCount = 0
  969. token.Number = 1
  970. i = i + 1
  971. locals[token.val] = token
  972.  
  973. local tokens = {}
  974. function lol()
  975. token.UseCount = token.UseCount + 1
  976. for _, t in ipairs(tokens) do
  977. t.UseCount = token.UseCount
  978. end
  979. end
  980.  
  981. token.Tokens = {}
  982. function token.Tokens.push(t)
  983. t.UseCountIncrease = lol
  984. t.UseCount = token.UseCount
  985. t.Tokens = token.Tokens
  986. table.insert(tokens, t)
  987. end
  988.  
  989. function token.Tokens.get()
  990. return tokens
  991. end
  992. token.UseCountIncrease = lol
  993. end
  994. break
  995. elseif stat.Type == "LocalFunctionStat" then
  996. local nameChain = stat.FunctionStat.NameChain
  997. if #nameChain == 1 then
  998. local token = nameChain[1]
  999. token.UseCount = 0
  1000. token.Number = 1
  1001. i = i + 1
  1002. locals[token.val] = token
  1003.  
  1004. local tokens = {}
  1005. function lol()
  1006. token.UseCount = token.UseCount + 1
  1007. for _, t in ipairs(tokens) do
  1008. t.UseCount = token.UseCount
  1009. end
  1010. end
  1011.  
  1012. token.Tokens = {}
  1013. function token.Tokens.push(t)
  1014. t.UseCountIncrease = lol
  1015. t.UseCount = token.UseCount
  1016. t.Tokens = token.Tokens
  1017. table.insert(tokens, t)
  1018. end
  1019.  
  1020. function token.Tokens.get()
  1021. return tokens
  1022. end
  1023.  
  1024. token.UseCountIncrease = lol
  1025. end
  1026. break
  1027. else
  1028. break
  1029. end
  1030. end
  1031.  
  1032. local next = peek()
  1033. if next.kind == "symbol" and next.val == ";" then
  1034. table.insert(semicolons, get())
  1035. end
  1036. end
  1037.  
  1038. node = {
  1039. Type = "StatList",
  1040. StatementList = statements,
  1041. GetFirstToken = function(self)
  1042. if #node.StatementList == 0 then
  1043. return
  1044. else
  1045. return node.StatementList[1]:GetFirstToken()
  1046. end
  1047. end,
  1048. GetLastToken = function(self)
  1049. if #node.StatementList == 0 then
  1050. return
  1051. else
  1052. return node.StatementList[#node.StatementList]:GetLastToken()
  1053. end
  1054. end
  1055. }
  1056. return node
  1057. end
  1058.  
  1059. return block({}, {})
  1060. end
  1061.  
  1062.  
  1063. ParseTokens(tokens)
Advertisement
Add Comment
Please, Sign In to add comment