LuaCompiler.cs 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172
  1. using Lua.Internal;
  2. using Lua.CodeAnalysis.Syntax;
  3. using Lua.CodeAnalysis.Syntax.Nodes;
  4. using Lua.Runtime;
  5. namespace Lua.CodeAnalysis.Compilation;
  6. public sealed class LuaCompiler : ISyntaxNodeVisitor<ScopeCompilationContext, bool>
  7. {
  8. public static readonly LuaCompiler Default = new();
  9. public Chunk Compile(string source, string? chunkName = null)
  10. {
  11. return Compile(LuaSyntaxTree.Parse(source, chunkName), chunkName);
  12. }
  13. /// <summary>
  14. /// Returns a compiled chunk of the syntax tree.
  15. /// </summary>
  16. public Chunk Compile(LuaSyntaxTree syntaxTree, string? chunkName = null)
  17. {
  18. using var context = FunctionCompilationContext.Create(null);
  19. // set global enviroment upvalue
  20. context.AddUpValue(new()
  21. {
  22. Name = "_ENV".AsMemory(),
  23. Id = 0,
  24. Index = -1,
  25. IsInRegister = false,
  26. });
  27. context.ChunkName = chunkName;
  28. syntaxTree.Accept(this, context.Scope);
  29. return context.ToChunk();
  30. }
  31. // Syntax Tree
  32. public bool VisitSyntaxTree(LuaSyntaxTree node, ScopeCompilationContext context)
  33. {
  34. foreach (var childNode in node.Nodes)
  35. {
  36. childNode.Accept(this, context);
  37. }
  38. return true;
  39. }
  40. // Literals
  41. public bool VisitNilLiteralNode(NilLiteralNode node, ScopeCompilationContext context)
  42. {
  43. context.PushInstruction(Instruction.LoadNil(context.StackPosition, 1), node.Position, true);
  44. return true;
  45. }
  46. public bool VisitBooleanLiteralNode(BooleanLiteralNode node, ScopeCompilationContext context)
  47. {
  48. context.PushInstruction(Instruction.LoadBool(context.StackPosition, (ushort)(node.Value ? 1 : 0), 0), node.Position, true);
  49. return true;
  50. }
  51. public bool VisitNumericLiteralNode(NumericLiteralNode node, ScopeCompilationContext context)
  52. {
  53. var index = context.Function.GetConstantIndex(node.Value);
  54. context.PushInstruction(Instruction.LoadK(context.StackPosition, index), node.Position, true);
  55. return true;
  56. }
  57. public bool VisitStringLiteralNode(StringLiteralNode node, ScopeCompilationContext context)
  58. {
  59. string? str;
  60. if (node.IsShortLiteral)
  61. {
  62. if (!StringHelper.TryFromStringLiteral(node.Text.Span, out str))
  63. {
  64. throw new LuaParseException(context.Function.ChunkName, node.Position, $"invalid escape sequence near '{node.Text}'");
  65. }
  66. }
  67. else
  68. {
  69. str = node.Text.ToString();
  70. }
  71. var index = context.Function.GetConstantIndex(str);
  72. context.PushInstruction(Instruction.LoadK(context.StackPosition, index), node.Position, true);
  73. return true;
  74. }
  75. // identifier
  76. public bool VisitIdentifierNode(IdentifierNode node, ScopeCompilationContext context)
  77. {
  78. LoadIdentifier(node.Name, context, node.Position, false);
  79. return true;
  80. }
  81. // vararg
  82. public bool VisitVariableArgumentsExpressionNode(VariableArgumentsExpressionNode node, ScopeCompilationContext context)
  83. {
  84. CompileVariableArgumentsExpression(node, context, 1);
  85. return true;
  86. }
  87. void CompileVariableArgumentsExpression(VariableArgumentsExpressionNode node, ScopeCompilationContext context, int resultCount)
  88. {
  89. context.PushInstruction(Instruction.VarArg(context.StackPosition, (ushort)(resultCount == -1 ? 0 : resultCount + 1)), node.Position, true);
  90. }
  91. // Unary/Binary expression
  92. public bool VisitUnaryExpressionNode(UnaryExpressionNode node, ScopeCompilationContext context)
  93. {
  94. var b = context.StackPosition;
  95. node.Node.Accept(this, context);
  96. switch (node.Operator)
  97. {
  98. case UnaryOperator.Negate:
  99. context.PushInstruction(Instruction.Unm(b, b), node.Position);
  100. break;
  101. case UnaryOperator.Not:
  102. context.PushInstruction(Instruction.Not(b, b), node.Position);
  103. break;
  104. case UnaryOperator.Length:
  105. context.PushInstruction(Instruction.Len(b, b), node.Position);
  106. break;
  107. }
  108. return true;
  109. }
  110. public bool VisitBinaryExpressionNode(BinaryExpressionNode node, ScopeCompilationContext context)
  111. {
  112. var r = context.StackPosition;
  113. (var b, var c) = GetBAndC(node, context);
  114. switch (node.OperatorType)
  115. {
  116. case BinaryOperator.Addition:
  117. context.PushInstruction(Instruction.Add(r, b, c), node.Position);
  118. break;
  119. case BinaryOperator.Subtraction:
  120. context.PushInstruction(Instruction.Sub(r, b, c), node.Position);
  121. break;
  122. case BinaryOperator.Multiplication:
  123. context.PushInstruction(Instruction.Mul(r, b, c), node.Position);
  124. break;
  125. case BinaryOperator.Division:
  126. context.PushInstruction(Instruction.Div(r, b, c), node.Position);
  127. break;
  128. case BinaryOperator.Modulo:
  129. context.PushInstruction(Instruction.Mod(r, b, c), node.Position);
  130. break;
  131. case BinaryOperator.Exponentiation:
  132. context.PushInstruction(Instruction.Pow(r, b, c), node.Position);
  133. break;
  134. case BinaryOperator.Equality:
  135. context.PushInstruction(Instruction.Eq(1, b, c), node.Position);
  136. context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
  137. context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
  138. break;
  139. case BinaryOperator.Inequality:
  140. context.PushInstruction(Instruction.Eq(0, b, c), node.Position);
  141. context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
  142. context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
  143. break;
  144. case BinaryOperator.GreaterThan:
  145. context.PushInstruction(Instruction.Le(1, c, b), node.Position);
  146. context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
  147. context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
  148. break;
  149. case BinaryOperator.GreaterThanOrEqual:
  150. context.PushInstruction(Instruction.Lt(1, c, b), node.Position);
  151. context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
  152. context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
  153. break;
  154. case BinaryOperator.LessThan:
  155. context.PushInstruction(Instruction.Lt(1, b, c), node.Position);
  156. context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
  157. context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
  158. break;
  159. case BinaryOperator.LessThanOrEqual:
  160. context.PushInstruction(Instruction.Le(1, b, c), node.Position);
  161. context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
  162. context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
  163. break;
  164. case BinaryOperator.Concat:
  165. context.PushInstruction(Instruction.Concat(r, b, c), node.Position);
  166. break;
  167. case BinaryOperator.And:
  168. context.PushInstruction(Instruction.TestSet(r, b, 0), node.Position);
  169. context.PushInstruction(Instruction.Jmp(0, 1), node.Position);
  170. context.PushInstruction(Instruction.Move(r, c), node.Position);
  171. break;
  172. case BinaryOperator.Or:
  173. context.PushInstruction(Instruction.TestSet(r, b, 1), node.Position);
  174. context.PushInstruction(Instruction.Jmp(0, 1), node.Position);
  175. context.PushInstruction(Instruction.Move(r, c), node.Position);
  176. break;
  177. }
  178. context.StackPosition = (byte)(r + 1);
  179. return true;
  180. }
  181. public bool VisitGroupedExpressionNode(GroupedExpressionNode node, ScopeCompilationContext context)
  182. {
  183. return node.Expression.Accept(this, context);
  184. }
  185. // table
  186. public bool VisitTableConstructorExpressionNode(TableConstructorExpressionNode node, ScopeCompilationContext context)
  187. {
  188. var tableRegisterIndex = context.StackPosition;
  189. var newTableInstructionIndex = context.Function.Instructions.Length;
  190. context.PushInstruction(Instruction.NewTable(tableRegisterIndex, 0, 0), node.Position, true);
  191. var currentArrayChunkSize = 0;
  192. ushort hashMapSize = 0;
  193. ushort arrayBlock = 1;
  194. ListTableConstructorField? lastField = null;
  195. if (node.Fields.LastOrDefault() is ListTableConstructorField t)
  196. {
  197. lastField = t;
  198. }
  199. foreach (var group in node.Fields.GroupConsecutiveBy(x => x.GetType()))
  200. {
  201. foreach (var field in group)
  202. {
  203. var p = context.StackPosition;
  204. switch (field)
  205. {
  206. case ListTableConstructorField listItem:
  207. context.StackPosition = (byte)(p + currentArrayChunkSize - 50 * (arrayBlock - 1));
  208. // For the last element, we need to take into account variable arguments and multiple return values.
  209. if (listItem == lastField)
  210. {
  211. switch (listItem.Expression)
  212. {
  213. case CallFunctionExpressionNode call:
  214. CompileCallFunctionExpression(call, context, false, -1);
  215. break;
  216. case CallTableMethodExpressionNode method:
  217. CompileTableMethod(method, context, false, -1);
  218. break;
  219. case VariableArgumentsExpressionNode varArg:
  220. CompileVariableArgumentsExpression(varArg, context, -1);
  221. break;
  222. default:
  223. listItem.Expression.Accept(this, context);
  224. break;
  225. }
  226. context.PushInstruction(Instruction.SetList(tableRegisterIndex, 0, arrayBlock), listItem.Position);
  227. currentArrayChunkSize = 0;
  228. }
  229. else
  230. {
  231. listItem.Expression.Accept(this, context);
  232. currentArrayChunkSize++;
  233. if (currentArrayChunkSize == 50)
  234. {
  235. context.PushInstruction(Instruction.SetList(tableRegisterIndex, 50, arrayBlock), listItem.Position);
  236. currentArrayChunkSize = 0;
  237. arrayBlock++;
  238. }
  239. }
  240. break;
  241. case RecordTableConstructorField recordItem:
  242. recordItem.ValueExpression.Accept(this, context);
  243. var keyConstIndex = context.Function.GetConstantIndex(recordItem.Key) + 256;
  244. context.PushInstruction(Instruction.SetTable(tableRegisterIndex, (ushort)keyConstIndex, p), recordItem.Position);
  245. hashMapSize++;
  246. break;
  247. case GeneralTableConstructorField generalItem:
  248. var keyIndex = context.StackPosition;
  249. generalItem.KeyExpression.Accept(this, context);
  250. var valueIndex = context.StackPosition;
  251. generalItem.ValueExpression.Accept(this, context);
  252. context.PushInstruction(Instruction.SetTable(tableRegisterIndex, keyIndex, valueIndex), generalItem.Position);
  253. hashMapSize++;
  254. break;
  255. default:
  256. throw new NotSupportedException();
  257. }
  258. context.StackPosition = p;
  259. }
  260. if (currentArrayChunkSize > 0)
  261. {
  262. context.PushInstruction(Instruction.SetList(tableRegisterIndex, (ushort)currentArrayChunkSize, arrayBlock), node.Position);
  263. currentArrayChunkSize = 0;
  264. arrayBlock = 1;
  265. }
  266. }
  267. context.Function.Instructions[newTableInstructionIndex].B = (ushort)(currentArrayChunkSize + (arrayBlock - 1) * 50);
  268. context.Function.Instructions[newTableInstructionIndex].C = hashMapSize;
  269. return true;
  270. }
  271. public bool VisitTableIndexerAccessExpressionNode(TableIndexerAccessExpressionNode node, ScopeCompilationContext context)
  272. {
  273. // load table
  274. var tablePosition = context.StackPosition;
  275. node.TableNode.Accept(this, context);
  276. // load key
  277. node.KeyNode.Accept(this, context);
  278. // push interuction
  279. context.PushInstruction(Instruction.GetTable(tablePosition, tablePosition, (ushort)(context.StackPosition - 1)), node.Position);
  280. context.StackPosition = (byte)(tablePosition + 1);
  281. return true;
  282. }
  283. public bool VisitTableMemberAccessExpressionNode(TableMemberAccessExpressionNode node, ScopeCompilationContext context)
  284. {
  285. // load table
  286. var tablePosition = context.StackPosition;
  287. node.TableNode.Accept(this, context);
  288. // load key
  289. var keyIndex = context.Function.GetConstantIndex(node.MemberName) + 256;
  290. // push interuction
  291. context.PushInstruction(Instruction.GetTable(tablePosition, tablePosition, (ushort)keyIndex), node.Position);
  292. context.StackPosition = (byte)(tablePosition + 1);
  293. return true;
  294. }
  295. public bool VisitCallTableMethodExpressionNode(CallTableMethodExpressionNode node, ScopeCompilationContext context)
  296. {
  297. CompileTableMethod(node, context, false, 1);
  298. return true;
  299. }
  300. public bool VisitCallTableMethodStatementNode(CallTableMethodStatementNode node, ScopeCompilationContext context)
  301. {
  302. CompileTableMethod(node.Expression, context, false, 0);
  303. return true;
  304. }
  305. void CompileTableMethod(CallTableMethodExpressionNode node, ScopeCompilationContext context, bool isTailCall, int resultCount)
  306. {
  307. // load table
  308. var tablePosition = context.StackPosition;
  309. node.TableNode.Accept(this, context);
  310. // load key
  311. var keyIndex = context.Function.GetConstantIndex(node.MethodName) + 256;
  312. // get closure
  313. context.PushInstruction(Instruction.Self(tablePosition, tablePosition, (ushort)keyIndex), node.Position);
  314. context.StackPosition = (byte)(tablePosition + 2);
  315. // load arguments
  316. var b = node.ArgumentNodes.Length + 2;
  317. if (node.ArgumentNodes.Length > 0 && !IsFixedNumberOfReturnValues(node.ArgumentNodes[^1]))
  318. {
  319. b = 0;
  320. }
  321. CompileExpressionList(node, node.ArgumentNodes, b - 1, context);
  322. // push call interuction
  323. if (isTailCall)
  324. {
  325. context.PushInstruction(Instruction.TailCall(tablePosition, (ushort)b, 0), node.Position);
  326. context.StackPosition = tablePosition;
  327. }
  328. else
  329. {
  330. context.PushInstruction(Instruction.Call(tablePosition, (ushort)b, (ushort)(resultCount < 0 ? 0 : resultCount + 1)), node.Position);
  331. context.StackPosition = (byte)(tablePosition + resultCount);
  332. }
  333. }
  334. // return
  335. public bool VisitReturnStatementNode(ReturnStatementNode node, ScopeCompilationContext context)
  336. {
  337. ushort b;
  338. // tail call
  339. if (node.Nodes.Length == 1)
  340. {
  341. var lastNode = node.Nodes[^1];
  342. if (lastNode is CallFunctionExpressionNode call)
  343. {
  344. CompileCallFunctionExpression(call, context, true, -1);
  345. return true;
  346. }
  347. else if (lastNode is CallTableMethodExpressionNode callMethod)
  348. {
  349. CompileTableMethod(callMethod, context, true, -1);
  350. return true;
  351. }
  352. }
  353. b = node.Nodes.Length > 0 && !IsFixedNumberOfReturnValues(node.Nodes[^1])
  354. ? (ushort)0
  355. : (ushort)(node.Nodes.Length + 1);
  356. var a = context.StackPosition;
  357. CompileExpressionList(node, node.Nodes, b - 1, context);
  358. context.PushInstruction(Instruction.Return(a, b), node.Position);
  359. return true;
  360. }
  361. // assignment
  362. public bool VisitLocalAssignmentStatementNode(LocalAssignmentStatementNode node, ScopeCompilationContext context)
  363. {
  364. var startPosition = context.StackPosition;
  365. CompileExpressionList(node, node.RightNodes, node.LeftNodes.Length, context);
  366. for (int i = 0; i < node.Identifiers.Length; i++)
  367. {
  368. context.StackPosition = (byte)(startPosition + i + 1);
  369. var identifier = node.Identifiers[i];
  370. if (context.TryGetLocalVariableInThisScope(identifier.Name, out var variable))
  371. {
  372. // assign local variable
  373. context.PushInstruction(Instruction.Move(variable.RegisterIndex, (ushort)(context.StackPosition - 1)), node.Position, true);
  374. }
  375. else
  376. {
  377. // register local variable
  378. context.AddLocalVariable(identifier.Name, new()
  379. {
  380. RegisterIndex = (byte)(context.StackPosition - 1),
  381. });
  382. }
  383. }
  384. return true;
  385. }
  386. public bool VisitAssignmentStatementNode(AssignmentStatementNode node, ScopeCompilationContext context)
  387. {
  388. var startPosition = context.StackPosition;
  389. CompileExpressionList(node, node.RightNodes, node.LeftNodes.Length, context);
  390. for (int i = 0; i < node.LeftNodes.Length; i++)
  391. {
  392. context.StackPosition = (byte)(startPosition + i + 1);
  393. var leftNode = node.LeftNodes[i];
  394. switch (leftNode)
  395. {
  396. case IdentifierNode identifier:
  397. {
  398. if (context.TryGetLocalVariable(identifier.Name, out var variable))
  399. {
  400. // assign local variable
  401. context.PushInstruction(Instruction.Move(variable.RegisterIndex, (ushort)(context.StackPosition - 1)), node.Position, true);
  402. }
  403. else if (context.Function.TryGetUpValue(identifier.Name, out var upValue))
  404. {
  405. // assign upvalue
  406. context.PushInstruction(Instruction.SetUpVal((byte)(context.StackPosition - 1), (ushort)upValue.Id), node.Position);
  407. }
  408. else if (context.TryGetLocalVariable("_ENV".AsMemory(), out variable))
  409. {
  410. // assign env element
  411. var index = context.Function.GetConstantIndex(identifier.Name.ToString()) + 256;
  412. context.PushInstruction(Instruction.SetTable(variable.RegisterIndex, (ushort)index, (ushort)(context.StackPosition - 1)), node.Position);
  413. }
  414. else
  415. {
  416. // assign global variable
  417. var index = context.Function.GetConstantIndex(identifier.Name.ToString()) + 256;
  418. context.PushInstruction(Instruction.SetTabUp(0, (ushort)index, (ushort)(context.StackPosition - 1)), node.Position);
  419. }
  420. }
  421. break;
  422. case TableIndexerAccessExpressionNode tableIndexer:
  423. {
  424. var valueIndex = context.StackPosition - 1;
  425. tableIndexer.TableNode.Accept(this, context);
  426. var tableIndex = context.StackPosition - 1;
  427. tableIndexer.KeyNode.Accept(this, context);
  428. var keyIndex = context.StackPosition - 1;
  429. context.PushInstruction(Instruction.SetTable((byte)tableIndex, (ushort)keyIndex, (ushort)valueIndex), node.Position);
  430. }
  431. break;
  432. case TableMemberAccessExpressionNode tableMember:
  433. {
  434. var valueIndex = context.StackPosition - 1;
  435. tableMember.TableNode.Accept(this, context);
  436. var tableIndex = context.StackPosition - 1;
  437. var keyIndex = context.Function.GetConstantIndex(tableMember.MemberName) + 256;
  438. context.PushInstruction(Instruction.SetTable((byte)tableIndex, (ushort)keyIndex, (ushort)valueIndex), node.Position);
  439. }
  440. break;
  441. default:
  442. throw new LuaParseException(default, default, "An error occurred while parsing the code"); // TODO: add message
  443. }
  444. }
  445. context.StackPosition = startPosition;
  446. return true;
  447. }
  448. // function call
  449. public bool VisitCallFunctionStatementNode(CallFunctionStatementNode node, ScopeCompilationContext context)
  450. {
  451. CompileCallFunctionExpression(node.Expression, context, false, 0);
  452. return true;
  453. }
  454. public bool VisitCallFunctionExpressionNode(CallFunctionExpressionNode node, ScopeCompilationContext context)
  455. {
  456. CompileCallFunctionExpression(node, context, false, 1);
  457. return true;
  458. }
  459. void CompileCallFunctionExpression(CallFunctionExpressionNode node, ScopeCompilationContext context, bool isTailCall, int resultCount)
  460. {
  461. // get closure
  462. var r = context.StackPosition;
  463. node.FunctionNode.Accept(this, context);
  464. // load arguments
  465. var b = node.ArgumentNodes.Length + 1;
  466. if (node.ArgumentNodes.Length > 0 && !IsFixedNumberOfReturnValues(node.ArgumentNodes[^1]))
  467. {
  468. b = 0;
  469. }
  470. CompileExpressionList(node, node.ArgumentNodes, b - 1, context);
  471. // push call interuction
  472. if (isTailCall)
  473. {
  474. context.PushInstruction(Instruction.TailCall(r, (ushort)b, 0), node.Position);
  475. context.StackPosition = r;
  476. }
  477. else
  478. {
  479. context.PushInstruction(Instruction.Call(r, (ushort)b, (ushort)(resultCount == -1 ? 0 : resultCount + 1)), node.Position);
  480. context.StackPosition = (byte)(r + resultCount);
  481. }
  482. }
  483. // function declaration
  484. public bool VisitFunctionDeclarationExpressionNode(FunctionDeclarationExpressionNode node, ScopeCompilationContext context)
  485. {
  486. var funcIndex = CompileFunctionProto(ReadOnlyMemory<char>.Empty, context, node.ParameterNodes, node.Nodes, node.ParameterNodes.Length, node.HasVariableArguments, false);
  487. // push closure instruction
  488. context.PushInstruction(Instruction.Closure(context.StackPosition, funcIndex), node.Position, true);
  489. return true;
  490. }
  491. public bool VisitLocalFunctionDeclarationStatementNode(LocalFunctionDeclarationStatementNode node, ScopeCompilationContext context)
  492. {
  493. // assign local variable
  494. context.AddLocalVariable(node.Name, new()
  495. {
  496. RegisterIndex = context.StackPosition,
  497. });
  498. // compile function
  499. var funcIndex = CompileFunctionProto(node.Name, context, node.ParameterNodes, node.Nodes, node.ParameterNodes.Length, node.HasVariableArguments, false);
  500. // push closure instruction
  501. context.PushInstruction(Instruction.Closure(context.StackPosition, funcIndex), node.Position, true);
  502. return true;
  503. }
  504. public bool VisitFunctionDeclarationStatementNode(FunctionDeclarationStatementNode node, ScopeCompilationContext context)
  505. {
  506. var funcIndex = CompileFunctionProto(node.Name, context, node.ParameterNodes, node.Nodes, node.ParameterNodes.Length, node.HasVariableArguments, false);
  507. // add closure
  508. var index = context.Function.GetConstantIndex(node.Name.ToString());
  509. // push closure instruction
  510. context.PushInstruction(Instruction.Closure(context.StackPosition, funcIndex), node.Position, true);
  511. // assign global variable
  512. context.PushInstruction(Instruction.SetTabUp(0, (ushort)(index + 256), (ushort)(context.StackPosition - 1)), node.Position);
  513. return true;
  514. }
  515. public bool VisitTableMethodDeclarationStatementNode(TableMethodDeclarationStatementNode node, ScopeCompilationContext context)
  516. {
  517. var funcIdentifier = node.MemberPath[^1];
  518. var funcIndex = CompileFunctionProto(funcIdentifier.Name, context, node.ParameterNodes, node.Nodes, node.ParameterNodes.Length + 1, node.HasVariableArguments, node.HasSelfParameter);
  519. // add closure
  520. var index = context.Function.GetConstantIndex(funcIdentifier.Name.ToString());
  521. var r = context.StackPosition;
  522. // assign global variable
  523. var first = node.MemberPath[0];
  524. var tableIndex = LoadIdentifier(first.Name, context, first.Position, true);
  525. for (int i = 1; i < node.MemberPath.Length - 1; i++)
  526. {
  527. var member = node.MemberPath[i];
  528. var constant = context.Function.GetConstantIndex(member.Name.ToString());
  529. context.PushInstruction(Instruction.GetTable(context.StackPosition, tableIndex, (ushort)(constant + 256)), member.Position, true);
  530. tableIndex = context.StackTopPosition;
  531. }
  532. // push closure instruction
  533. var closureIndex = context.StackPosition;
  534. context.PushInstruction(Instruction.Closure(closureIndex, funcIndex), node.Position, true);
  535. // set table
  536. context.PushInstruction(Instruction.SetTable(tableIndex, (ushort)(index + 256), closureIndex), funcIdentifier.Position);
  537. context.StackPosition = r;
  538. return true;
  539. }
  540. int CompileFunctionProto(ReadOnlyMemory<char> functionName, ScopeCompilationContext context, IdentifierNode[] parameters, SyntaxNode[] statements, int parameterCount, bool hasVarArg, bool hasSelfParameter)
  541. {
  542. using var funcContext = context.CreateChildFunction();
  543. funcContext.ChunkName = functionName.ToString();
  544. funcContext.ParameterCount = parameterCount;
  545. funcContext.HasVariableArguments = hasVarArg;
  546. if (hasSelfParameter)
  547. {
  548. funcContext.Scope.AddLocalVariable("self".AsMemory(), new()
  549. {
  550. RegisterIndex = 0,
  551. });
  552. funcContext.Scope.StackPosition++;
  553. }
  554. // add arguments
  555. for (int i = 0; i < parameters.Length; i++)
  556. {
  557. var parameter = parameters[i];
  558. funcContext.Scope.AddLocalVariable(parameter.Name, new()
  559. {
  560. RegisterIndex = (byte)(i + (hasSelfParameter ? 1 : 0)),
  561. });
  562. funcContext.Scope.StackPosition++;
  563. }
  564. foreach (var statement in statements)
  565. {
  566. statement.Accept(this, funcContext.Scope);
  567. }
  568. // compile function
  569. var chunk = funcContext.ToChunk();
  570. int index;
  571. if (functionName.Length == 0)
  572. {
  573. // anonymous function
  574. context.Function.AddFunctionProto(chunk, out index);
  575. }
  576. else
  577. {
  578. context.Function.AddOrSetFunctionProto(functionName, chunk, out index);
  579. }
  580. return index;
  581. }
  582. // control statements
  583. public bool VisitDoStatementNode(DoStatementNode node, ScopeCompilationContext context)
  584. {
  585. using var scopeContext = context.CreateChildScope();
  586. foreach (var childNode in node.StatementNodes)
  587. {
  588. childNode.Accept(this, scopeContext);
  589. }
  590. scopeContext.TryPushCloseUpValue(scopeContext.StackTopPosition, node.Position);
  591. return true;
  592. }
  593. public bool VisitBreakStatementNode(BreakStatementNode node, ScopeCompilationContext context)
  594. {
  595. context.Function.AddUnresolvedBreak(new()
  596. {
  597. Index = context.Function.Instructions.Length
  598. }, node.Position);
  599. context.PushInstruction(Instruction.Jmp(0, 0), node.Position);
  600. return true;
  601. }
  602. public bool VisitIfStatementNode(IfStatementNode node, ScopeCompilationContext context)
  603. {
  604. using var endJumpIndexList = new PooledList<int>(8);
  605. var hasElse = node.ElseNodes.Length > 0;
  606. // if
  607. using (var scopeContext = context.CreateChildScope())
  608. {
  609. CompileConditionNode(node.IfNode.ConditionNode, scopeContext, true);
  610. var ifPosition = scopeContext.Function.Instructions.Length;
  611. scopeContext.PushInstruction(Instruction.Jmp(0, 0), node.Position);
  612. foreach (var childNode in node.IfNode.ThenNodes)
  613. {
  614. childNode.Accept(this, scopeContext);
  615. }
  616. if (hasElse)
  617. {
  618. endJumpIndexList.Add(scopeContext.Function.Instructions.Length);
  619. var a = scopeContext.HasCapturedLocalVariables ? scopeContext.StackPosition : (byte)0;
  620. scopeContext.PushInstruction(Instruction.Jmp(a, 0), node.Position, true);
  621. }
  622. else
  623. {
  624. scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
  625. }
  626. scopeContext.Function.Instructions[ifPosition].SBx = scopeContext.Function.Instructions.Length - 1 - ifPosition;
  627. }
  628. // elseif
  629. foreach (var elseIf in node.ElseIfNodes)
  630. {
  631. using var scopeContext = context.CreateChildScope();
  632. CompileConditionNode(elseIf.ConditionNode, scopeContext, true);
  633. var elseifPosition = scopeContext.Function.Instructions.Length;
  634. scopeContext.PushInstruction(Instruction.Jmp(0, 0), node.Position);
  635. foreach (var childNode in elseIf.ThenNodes)
  636. {
  637. childNode.Accept(this, scopeContext);
  638. }
  639. // skip if node doesn't have else statements
  640. if (hasElse)
  641. {
  642. endJumpIndexList.Add(scopeContext.Function.Instructions.Length);
  643. var a = scopeContext.HasCapturedLocalVariables ? scopeContext.StackPosition : (byte)0;
  644. scopeContext.PushInstruction(Instruction.Jmp(a, 0), node.Position);
  645. }
  646. else
  647. {
  648. scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
  649. }
  650. scopeContext.Function.Instructions[elseifPosition].SBx = scopeContext.Function.Instructions.Length - 1 - elseifPosition;
  651. }
  652. // else nodes
  653. using (var scopeContext = context.CreateChildScope())
  654. {
  655. foreach (var childNode in node.ElseNodes)
  656. {
  657. childNode.Accept(this, scopeContext);
  658. }
  659. scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
  660. }
  661. // set JMP sBx
  662. foreach (var index in endJumpIndexList.AsSpan())
  663. {
  664. context.Function.Instructions[index].SBx = context.Function.Instructions.Length - 1 - index;
  665. }
  666. return true;
  667. }
  668. public bool VisitRepeatStatementNode(RepeatStatementNode node, ScopeCompilationContext context)
  669. {
  670. var startIndex = context.Function.Instructions.Length;
  671. context.Function.LoopLevel++;
  672. using var scopeContext = context.CreateChildScope();
  673. foreach (var childNode in node.Nodes)
  674. {
  675. childNode.Accept(this, scopeContext);
  676. }
  677. CompileConditionNode(node.ConditionNode, scopeContext, true);
  678. var a = scopeContext.HasCapturedLocalVariables ? scopeContext.StackPosition : (byte)0;
  679. scopeContext.PushInstruction(Instruction.Jmp(a, startIndex - scopeContext.Function.Instructions.Length - 1), node.Position);
  680. scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
  681. context.Function.LoopLevel--;
  682. // resolve break statements inside repeat block
  683. context.Function.ResolveAllBreaks(a, context.Function.Instructions.Length - 1, scopeContext);
  684. return true;
  685. }
  686. public bool VisitWhileStatementNode(WhileStatementNode node, ScopeCompilationContext context)
  687. {
  688. var conditionIndex = context.Function.Instructions.Length;
  689. context.PushInstruction(Instruction.Jmp(0, 0), node.Position);
  690. context.Function.LoopLevel++;
  691. using var scopeContext = context.CreateChildScope();
  692. foreach (var childNode in node.Nodes)
  693. {
  694. childNode.Accept(this, scopeContext);
  695. }
  696. context.Function.LoopLevel--;
  697. // set JMP sBx
  698. scopeContext.Function.Instructions[conditionIndex].SBx = scopeContext.Function.Instructions.Length - 1 - conditionIndex;
  699. CompileConditionNode(node.ConditionNode, scopeContext, false);
  700. var a = scopeContext.HasCapturedLocalVariables ? scopeContext.StackPosition : (byte)0;
  701. scopeContext.PushInstruction(Instruction.Jmp(a, conditionIndex - context.Function.Instructions.Length), node.Position);
  702. scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
  703. // resolve break statements inside while block
  704. context.Function.ResolveAllBreaks(scopeContext.StackPosition, context.Function.Instructions.Length - 1, scopeContext);
  705. return true;
  706. }
  707. public bool VisitNumericForStatementNode(NumericForStatementNode node, ScopeCompilationContext context)
  708. {
  709. var startPosition = context.StackPosition;
  710. node.InitNode.Accept(this, context);
  711. node.LimitNode.Accept(this, context);
  712. if (node.StepNode != null)
  713. {
  714. node.StepNode.Accept(this, context);
  715. }
  716. else
  717. {
  718. var index = context.Function.GetConstantIndex(1);
  719. context.PushInstruction(Instruction.LoadK(context.StackPosition, index), node.Position, true);
  720. }
  721. var prepIndex = context.Function.Instructions.Length;
  722. context.PushInstruction(Instruction.ForPrep(startPosition, 0), node.Position, true);
  723. // compile statements
  724. context.Function.LoopLevel++;
  725. using var scopeContext = context.CreateChildScope();
  726. {
  727. // add local variable
  728. scopeContext.AddLocalVariable(node.VariableName, new()
  729. {
  730. RegisterIndex = startPosition
  731. });
  732. foreach (var childNode in node.StatementNodes)
  733. {
  734. childNode.Accept(this, scopeContext);
  735. }
  736. scopeContext.TryPushCloseUpValue((byte)(startPosition + 1), node.Position);
  737. }
  738. context.Function.LoopLevel--;
  739. // set ForPrep
  740. context.Function.Instructions[prepIndex].SBx = context.Function.Instructions.Length - prepIndex - 1;
  741. // push ForLoop
  742. context.PushInstruction(Instruction.ForLoop(startPosition, prepIndex - context.Function.Instructions.Length), node.Position);
  743. context.Function.ResolveAllBreaks((byte)(startPosition + 1), context.Function.Instructions.Length - 1, scopeContext);
  744. context.StackPosition = startPosition;
  745. return true;
  746. }
  747. public bool VisitGenericForStatementNode(GenericForStatementNode node, ScopeCompilationContext context)
  748. {
  749. // get iterator
  750. var startPosition = context.StackPosition;
  751. if (node.ExpressionNode is CallFunctionExpressionNode call)
  752. {
  753. CompileCallFunctionExpression(call, context, false, 3);
  754. }
  755. else if (node.ExpressionNode is CallTableMethodExpressionNode method)
  756. {
  757. CompileTableMethod(method, context, false, 3);
  758. }
  759. else if (node.ExpressionNode is VariableArgumentsExpressionNode varArg)
  760. {
  761. CompileVariableArgumentsExpression(varArg, context, 3);
  762. }
  763. else
  764. {
  765. node.ExpressionNode.Accept(this, context);
  766. }
  767. // jump to TFORCALL
  768. var startJumpIndex = context.Function.Instructions.Length;
  769. context.PushInstruction(Instruction.Jmp(0, 0), node.Position);
  770. // compile statements
  771. context.Function.LoopLevel++;
  772. using var scopeContext = context.CreateChildScope();
  773. {
  774. scopeContext.StackPosition = (byte)(startPosition + 3 + node.Names.Length);
  775. // add local variables
  776. for (int i = 0; i < node.Names.Length; i++)
  777. {
  778. var name = node.Names[i];
  779. scopeContext.AddLocalVariable(name.Name, new()
  780. {
  781. RegisterIndex = (byte)(startPosition + 3 + i)
  782. });
  783. }
  784. foreach (var childNode in node.StatementNodes)
  785. {
  786. childNode.Accept(this, scopeContext);
  787. }
  788. scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
  789. }
  790. context.Function.LoopLevel--;
  791. // set jump
  792. context.Function.Instructions[startJumpIndex].SBx = context.Function.Instructions.Length - startJumpIndex - 1;
  793. // push OP_TFORCALL and OP_TFORLOOP
  794. context.PushInstruction(Instruction.TForCall(startPosition, (ushort)node.Names.Length), node.Position);
  795. context.PushInstruction(Instruction.TForLoop((byte)(startPosition + 2), startJumpIndex - context.Function.Instructions.Length), node.Position);
  796. context.Function.ResolveAllBreaks((byte)(startPosition + 1), context.Function.Instructions.Length - 1, scopeContext);
  797. context.StackPosition = startPosition;
  798. return true;
  799. }
  800. public bool VisitLabelStatementNode(LabelStatementNode node, ScopeCompilationContext context)
  801. {
  802. var desc = new LabelDescription()
  803. {
  804. Name = node.Name,
  805. Index = context.Function.Instructions.Length,
  806. RegisterIndex = context.StackPosition
  807. };
  808. context.AddLabel(desc);
  809. context.Function.ResolveGoto(desc);
  810. return true;
  811. }
  812. public bool VisitGotoStatementNode(GotoStatementNode node, ScopeCompilationContext context)
  813. {
  814. if (context.TryGetLabel(node.Name, out var description))
  815. {
  816. context.PushInstruction(Instruction.Jmp(description.RegisterIndex, description.Index - context.Function.Instructions.Length - 1), node.Position);
  817. }
  818. else
  819. {
  820. context.Function.AddUnresolvedGoto(new()
  821. {
  822. Name = node.Name,
  823. JumpInstructionIndex = context.Function.Instructions.Length
  824. });
  825. // add uninitialized jmp instruction
  826. context.PushInstruction(Instruction.Jmp(0, 0), node.Position);
  827. }
  828. return true;
  829. }
  830. static byte LoadIdentifier(ReadOnlyMemory<char> name, ScopeCompilationContext context, SourcePosition sourcePosition, bool dontLoadLocalVariable)
  831. {
  832. var p = context.StackPosition;
  833. if (context.TryGetLocalVariable(name, out var variable))
  834. {
  835. if (dontLoadLocalVariable)
  836. {
  837. return variable.RegisterIndex;
  838. }
  839. else if (p == variable.RegisterIndex)
  840. {
  841. context.StackPosition++;
  842. return p;
  843. }
  844. else
  845. {
  846. context.PushInstruction(Instruction.Move(p, variable.RegisterIndex), sourcePosition, true);
  847. return p;
  848. }
  849. }
  850. else if (context.Function.TryGetUpValue(name, out var upValue))
  851. {
  852. context.PushInstruction(Instruction.GetUpVal(p, (ushort)upValue.Id), sourcePosition, true);
  853. return p;
  854. }
  855. else if (context.TryGetLocalVariable("_ENV".AsMemory(), out variable))
  856. {
  857. var keyStringIndex = context.Function.GetConstantIndex(name.ToString()) + 256;
  858. context.PushInstruction(Instruction.GetTable(p, variable.RegisterIndex, (ushort)keyStringIndex), sourcePosition, true);
  859. return p;
  860. }
  861. else
  862. {
  863. context.Function.TryGetUpValue("_ENV".AsMemory(), out upValue);
  864. var index = context.Function.GetConstantIndex(name.ToString()) + 256;
  865. context.PushInstruction(Instruction.GetTabUp(p, (ushort)upValue.Id, (ushort)index), sourcePosition, true);
  866. return p;
  867. }
  868. }
  869. static bool IsFixedNumberOfReturnValues(ExpressionNode node)
  870. {
  871. return node is not (CallFunctionExpressionNode or CallTableMethodExpressionNode or VariableArgumentsExpressionNode);
  872. }
  873. /// <summary>
  874. /// Compiles a conditional boolean branch: if true (or false), the next instruction added is skipped.
  875. /// </summary>
  876. /// <param name="node">Condition node</param>
  877. /// <param name="context">Context</param>
  878. /// <param name="falseIsSkip">If true, generates an instruction sequence that skips the next instruction if the condition is false.</param>
  879. void CompileConditionNode(ExpressionNode node, ScopeCompilationContext context, bool falseIsSkip)
  880. {
  881. if (node is BinaryExpressionNode binaryExpression)
  882. {
  883. switch (binaryExpression.OperatorType)
  884. {
  885. case BinaryOperator.Equality:
  886. {
  887. (var b, var c) = GetBAndC(binaryExpression, context);
  888. context.PushInstruction(Instruction.Eq(falseIsSkip ? (byte)0 : (byte)1, b, c), node.Position);
  889. return;
  890. }
  891. case BinaryOperator.Inequality:
  892. {
  893. (var b, var c) = GetBAndC(binaryExpression, context);
  894. context.PushInstruction(Instruction.Eq(falseIsSkip ? (byte)1 : (byte)0, b, c), node.Position);
  895. return;
  896. }
  897. case BinaryOperator.LessThan:
  898. {
  899. (var b, var c) = GetBAndC(binaryExpression, context);
  900. context.PushInstruction(Instruction.Lt(falseIsSkip ? (byte)0 : (byte)1, b, c), node.Position);
  901. return;
  902. }
  903. case BinaryOperator.LessThanOrEqual:
  904. {
  905. (var b, var c) = GetBAndC(binaryExpression, context);
  906. context.PushInstruction(Instruction.Le(falseIsSkip ? (byte)0 : (byte)1, b, c), node.Position);
  907. return;
  908. }
  909. case BinaryOperator.GreaterThan:
  910. {
  911. (var b, var c) = GetBAndC(binaryExpression, context);
  912. context.PushInstruction(Instruction.Le(falseIsSkip ? (byte)0 : (byte)1, c, b), node.Position);
  913. return;
  914. }
  915. case BinaryOperator.GreaterThanOrEqual:
  916. {
  917. (var b, var c) = GetBAndC(binaryExpression, context);
  918. context.PushInstruction(Instruction.Lt(falseIsSkip ? (byte)0 : (byte)1, c, b), node.Position);
  919. return;
  920. }
  921. }
  922. }
  923. node.Accept(this, context);
  924. context.PushInstruction(Instruction.Test((byte)(context.StackPosition - 1), falseIsSkip ? (byte)0 : (byte)1), node.Position);
  925. }
  926. void CompileExpressionList(SyntaxNode rootNode, ExpressionNode[] expressions, int minimumCount, ScopeCompilationContext context)
  927. {
  928. var isLastFunction = false;
  929. for (int i = 0; i < expressions.Length; i++)
  930. {
  931. var expression = expressions[i];
  932. var isLast = i == expressions.Length - 1;
  933. var resultCount = isLast ? (minimumCount == -1 ? -1 : minimumCount - i) : 1;
  934. if (expression is CallFunctionExpressionNode call)
  935. {
  936. CompileCallFunctionExpression(call, context, false, resultCount);
  937. isLastFunction = isLast;
  938. }
  939. else if (expression is CallTableMethodExpressionNode method)
  940. {
  941. CompileTableMethod(method, context, false, resultCount);
  942. isLastFunction = isLast;
  943. }
  944. else if (expression is VariableArgumentsExpressionNode varArg)
  945. {
  946. CompileVariableArgumentsExpression(varArg, context, resultCount);
  947. isLastFunction = isLast;
  948. }
  949. else
  950. {
  951. expression.Accept(this, context);
  952. isLastFunction = false;
  953. }
  954. }
  955. // fill space with nil
  956. var varCount = minimumCount - expressions.Length;
  957. if (varCount > 0 && !isLastFunction)
  958. {
  959. context.PushInstruction(Instruction.LoadNil(context.StackPosition, (ushort)varCount), rootNode.Position);
  960. context.StackPosition = (byte)(context.StackPosition + varCount);
  961. }
  962. }
  963. (byte b, byte c) GetBAndC(BinaryExpressionNode node, ScopeCompilationContext context)
  964. {
  965. byte b, c;
  966. if (node.LeftNode is IdentifierNode leftIdentifier)
  967. {
  968. b = LoadIdentifier(leftIdentifier.Name, context, leftIdentifier.Position, true);
  969. }
  970. else
  971. {
  972. node.LeftNode.Accept(this, context);
  973. b = (byte)(context.StackPosition - 1);
  974. }
  975. if (node.RightNode is IdentifierNode rightIdentifier)
  976. {
  977. c = LoadIdentifier(rightIdentifier.Name, context, rightIdentifier.Position, true);
  978. }
  979. else
  980. {
  981. node.RightNode.Accept(this, context);
  982. c = (byte)(context.StackPosition - 1);
  983. }
  984. return (b, c);
  985. }
  986. }