| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287 |
- using Lua.Internal;
- using Lua.CodeAnalysis.Syntax;
- using Lua.CodeAnalysis.Syntax.Nodes;
- using Lua.Runtime;
- namespace Lua.CodeAnalysis.Compilation;
- public sealed class LuaCompiler : ISyntaxNodeVisitor<ScopeCompilationContext, bool>
- {
- public static readonly LuaCompiler Default = new();
- public Chunk Compile(string source, string? chunkName = null)
- {
- return Compile(LuaSyntaxTree.Parse(source, chunkName), chunkName);
- }
- /// <summary>
- /// Returns a compiled chunk of the syntax tree.
- /// </summary>
- public Chunk Compile(LuaSyntaxTree syntaxTree, string? chunkName = null)
- {
- using var context = FunctionCompilationContext.Create(null);
- context.HasVariableArguments = true;
- context.LineDefined = syntaxTree.Position.Line;
- context.LastLineDefined = syntaxTree.Position.Line;
- // set global enviroment upvalue
- context.AddUpValue(new()
- {
- Name = "_ENV".AsMemory(), Id = 0, Index = -1, IsInRegister = false,
- });
- context.ChunkName = chunkName;
- syntaxTree.Accept(this, context.Scope);
- return context.ToChunk();
- }
- // Syntax Tree
- public bool VisitSyntaxTree(LuaSyntaxTree node, ScopeCompilationContext context)
- {
- foreach (var childNode in node.Nodes)
- {
- childNode.Accept(this, context);
- }
- return true;
- }
- // Literals
- public bool VisitNilLiteralNode(NilLiteralNode node, ScopeCompilationContext context)
- {
- context.PushInstruction(Instruction.LoadNil(context.StackPosition, 1), node.Position, true);
- return true;
- }
- public bool VisitBooleanLiteralNode(BooleanLiteralNode node, ScopeCompilationContext context)
- {
- context.PushInstruction(Instruction.LoadBool(context.StackPosition, (ushort)(node.Value ? 1 : 0), 0), node.Position, true);
- return true;
- }
- public bool VisitNumericLiteralNode(NumericLiteralNode node, ScopeCompilationContext context)
- {
- var index = context.Function.GetConstantIndex(node.Value);
- context.PushInstruction(Instruction.LoadK(context.StackPosition, index), node.Position, true);
- return true;
- }
- public bool VisitStringLiteralNode(StringLiteralNode node, ScopeCompilationContext context)
- {
- string? str;
- if (node.IsShortLiteral)
- {
- if (!StringHelper.TryFromStringLiteral(node.Text.Span, out str))
- {
- throw new LuaParseException(context.Function.ChunkName, node.Position, $"invalid escape sequence near '{node.Text}'");
- }
- }
- else
- {
- str = node.Text.ToString();
- }
- var index = context.Function.GetConstantIndex(str);
- context.PushInstruction(Instruction.LoadK(context.StackPosition, index), node.Position, true);
- return true;
- }
- // identifier
- public bool VisitIdentifierNode(IdentifierNode node, ScopeCompilationContext context)
- {
- GetOrLoadIdentifier(node.Name, context, node.Position, false);
- return true;
- }
- // vararg
- public bool VisitVariableArgumentsExpressionNode(VariableArgumentsExpressionNode node, ScopeCompilationContext context)
- {
- CompileVariableArgumentsExpression(node, context, 1);
- return true;
- }
- void CompileVariableArgumentsExpression(VariableArgumentsExpressionNode node, ScopeCompilationContext context, int resultCount)
- {
- context.PushInstruction(Instruction.VarArg(context.StackPosition, (ushort)(resultCount == -1 ? 0 : resultCount + 1)), node.Position, true);
- }
- // Unary/Binary expression
- public bool VisitUnaryExpressionNode(UnaryExpressionNode node, ScopeCompilationContext context)
- {
- var b = context.StackPosition;
- node.Node.Accept(this, context);
- switch (node.Operator)
- {
- case UnaryOperator.Negate:
- context.PushInstruction(Instruction.Unm(b, b), node.Position);
- break;
- case UnaryOperator.Not:
- context.PushInstruction(Instruction.Not(b, b), node.Position);
- break;
- case UnaryOperator.Length:
- context.PushInstruction(Instruction.Len(b, b), node.Position);
- break;
- }
- return true;
- }
- public bool VisitBinaryExpressionNode(BinaryExpressionNode node, ScopeCompilationContext context)
- {
- var r = context.StackPosition;
- if (node.OperatorType is BinaryOperator.And or BinaryOperator.Or)
- {
- byte a;
- if (node.LeftNode is IdentifierNode leftIdentifier)
- {
- a = GetOrLoadIdentifier(leftIdentifier.Name, context, leftIdentifier.Position, true);
- }
- else
- {
- node.LeftNode.Accept(this, context);
- a = context.StackTopPosition;
- }
- context.PushInstruction(Instruction.Test(a, 0), node.Position);
- if (node.OperatorType is BinaryOperator.Or)
- {
- context.PushInstruction(Instruction.Jmp(0, 2), node.Position);
- context.PushInstruction(Instruction.Move(r, a), node.Position);
- }
- var testJmpIndex = context.Function.Instructions.Length;
- context.PushInstruction(Instruction.Jmp(0, 0), node.Position);
- context.StackPosition = r;
- node.RightNode.Accept(this, context);
- context.Function.Instructions[testJmpIndex].SBx = context.Function.Instructions.Length - testJmpIndex - 1;
- }
- else
- {
- var b = (ushort)GetRKIndex(node.LeftNode, context);
- var c = (ushort)GetRKIndex(node.RightNode, context);
- switch (node.OperatorType)
- {
- case BinaryOperator.Addition:
- context.PushInstruction(Instruction.Add(r, b, c), node.Position);
- break;
- case BinaryOperator.Subtraction:
- context.PushInstruction(Instruction.Sub(r, b, c), node.Position);
- break;
- case BinaryOperator.Multiplication:
- context.PushInstruction(Instruction.Mul(r, b, c), node.Position);
- break;
- case BinaryOperator.Division:
- context.PushInstruction(Instruction.Div(r, b, c), node.Position);
- break;
- case BinaryOperator.Modulo:
- context.PushInstruction(Instruction.Mod(r, b, c), node.Position);
- break;
- case BinaryOperator.Exponentiation:
- context.PushInstruction(Instruction.Pow(r, b, c), node.Position);
- break;
- case BinaryOperator.Equality:
- context.PushInstruction(Instruction.Eq(1, b, c), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
- break;
- case BinaryOperator.Inequality:
- context.PushInstruction(Instruction.Eq(0, b, c), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
- break;
- case BinaryOperator.GreaterThan:
- context.PushInstruction(Instruction.Lt(1, c, b), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
- break;
- case BinaryOperator.GreaterThanOrEqual:
- context.PushInstruction(Instruction.Le(1, c, b), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
- break;
- case BinaryOperator.LessThan:
- context.PushInstruction(Instruction.Lt(1, b, c), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
- break;
- case BinaryOperator.LessThanOrEqual:
- context.PushInstruction(Instruction.Le(1, b, c), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 1, 1), node.Position);
- context.PushInstruction(Instruction.LoadBool(r, 0, 0), node.Position);
- break;
- case BinaryOperator.Concat:
- context.PushInstruction(Instruction.Concat(r, b, c), node.Position);
- break;
- }
- context.StackPosition = (byte)(r + 1);
- }
- return true;
- }
- public bool VisitGroupedExpressionNode(GroupedExpressionNode node, ScopeCompilationContext context)
- {
- return node.Expression.Accept(this, context);
- }
- // table
- public bool VisitTableConstructorExpressionNode(TableConstructorExpressionNode node, ScopeCompilationContext context)
- {
- var tableRegisterIndex = context.StackPosition;
- var newTableInstructionIndex = context.Function.Instructions.Length;
- context.PushInstruction(Instruction.NewTable(tableRegisterIndex, 0, 0), node.Position, true);
- var currentArrayChunkSize = 0;
- ushort hashMapSize = 0;
- ushort arrayBlock = 1;
- ListTableConstructorField? lastField = null;
- if (node.Fields.LastOrDefault() is ListTableConstructorField t)
- {
- lastField = t;
- }
- foreach (var group in node.Fields.GroupConsecutiveBy(x => x.GetType()))
- {
- foreach (var field in group)
- {
- var p = context.StackPosition;
- switch (field)
- {
- case ListTableConstructorField listItem:
- context.StackPosition = (byte)(p + currentArrayChunkSize - 50 * (arrayBlock - 1));
- // For the last element, we need to take into account variable arguments and multiple return values.
- if (listItem == lastField)
- {
- bool isFixedItems = true;
- switch (listItem.Expression)
- {
- case CallFunctionExpressionNode call:
- CompileCallFunctionExpression(call, context, false, -1);
- isFixedItems = false;
- break;
- case CallTableMethodExpressionNode method:
- CompileTableMethod(method, context, false, -1);
- break;
- case VariableArgumentsExpressionNode varArg:
- CompileVariableArgumentsExpression(varArg, context, -1);
- isFixedItems = false;
- break;
- default:
- listItem.Expression.Accept(this, context);
- break;
- }
- context.PushInstruction(Instruction.SetList(tableRegisterIndex, (ushort)(isFixedItems ? context.StackTopPosition - tableRegisterIndex : 0), arrayBlock), listItem.Position);
- currentArrayChunkSize = 0;
- }
- else
- {
- listItem.Expression.Accept(this, context);
- currentArrayChunkSize++;
- if (currentArrayChunkSize == 50)
- {
- context.PushInstruction(Instruction.SetList(tableRegisterIndex, 50, arrayBlock), listItem.Position);
- currentArrayChunkSize = 0;
- arrayBlock++;
- }
- }
- break;
- case RecordTableConstructorField recordItem:
- recordItem.ValueExpression.Accept(this, context);
- var keyConstIndex = context.Function.GetConstantIndex(recordItem.Key) + 256;
- context.PushInstruction(Instruction.SetTable(tableRegisterIndex, (ushort)keyConstIndex, p), recordItem.Position);
- hashMapSize++;
- break;
- case GeneralTableConstructorField generalItem:
- var keyIndex = context.StackPosition;
- generalItem.KeyExpression.Accept(this, context);
- var valueIndex = context.StackPosition;
- generalItem.ValueExpression.Accept(this, context);
- context.PushInstruction(Instruction.SetTable(tableRegisterIndex, keyIndex, valueIndex), generalItem.Position);
- hashMapSize++;
- break;
- default:
- throw new NotSupportedException();
- }
- context.StackPosition = p;
- }
- if (currentArrayChunkSize > 0)
- {
- context.PushInstruction(Instruction.SetList(tableRegisterIndex, (ushort)currentArrayChunkSize, arrayBlock), node.Position);
- currentArrayChunkSize = 0;
- arrayBlock = 1;
- }
- }
- context.Function.Instructions[newTableInstructionIndex].B = (ushort)(currentArrayChunkSize + (arrayBlock - 1) * 50);
- context.Function.Instructions[newTableInstructionIndex].C = hashMapSize;
- return true;
- }
- public bool VisitTableIndexerAccessExpressionNode(TableIndexerAccessExpressionNode node, ScopeCompilationContext context)
- {
- // load table
- var tablePosition = context.StackPosition;
- node.TableNode.Accept(this, context);
- // load key
- var keyPosition = (ushort)GetRKIndex(node.KeyNode, context);
- // push interuction
- context.PushInstruction(Instruction.GetTable(tablePosition, tablePosition, keyPosition), node.Position);
- context.StackPosition = (byte)(tablePosition + 1);
- return true;
- }
- public bool VisitTableMemberAccessExpressionNode(TableMemberAccessExpressionNode node, ScopeCompilationContext context)
- {
- // load table
- var tablePosition = context.StackPosition;
- node.TableNode.Accept(this, context);
- // load key
- var keyIndex = context.Function.GetConstantIndex(node.MemberName) + 256;
- // push interuction
- context.PushInstruction(Instruction.GetTable(tablePosition, tablePosition, (ushort)keyIndex), node.Position);
- context.StackPosition = (byte)(tablePosition + 1);
- return true;
- }
- public bool VisitCallTableMethodExpressionNode(CallTableMethodExpressionNode node, ScopeCompilationContext context)
- {
- CompileTableMethod(node, context, false, 1);
- return true;
- }
- public bool VisitCallTableMethodStatementNode(CallTableMethodStatementNode node, ScopeCompilationContext context)
- {
- CompileTableMethod(node.Expression, context, false, 0);
- return true;
- }
- void CompileTableMethod(CallTableMethodExpressionNode node, ScopeCompilationContext context, bool isTailCall, int resultCount)
- {
- // load table
- var tablePosition = context.StackPosition;
- node.TableNode.Accept(this, context);
- // load key
- var keyIndex = context.Function.GetConstantIndex(node.MethodName) + 256;
- // get closure
- context.PushInstruction(Instruction.Self(tablePosition, tablePosition, (ushort)keyIndex), node.Position);
- context.StackPosition = (byte)(tablePosition + 2);
- // load arguments
- var b = node.ArgumentNodes.Length + 2;
- if (node.ArgumentNodes.Length > 0 && !IsFixedNumberOfReturnValues(node.ArgumentNodes[^1]))
- {
- b = 0;
- }
- CompileExpressionList(node, node.ArgumentNodes, b - 2, context);
- // push call interuction
- if (isTailCall)
- {
- context.PushInstruction(Instruction.TailCall(tablePosition, (ushort)b, 0), node.Position);
- context.StackPosition = tablePosition;
- }
- else
- {
- context.PushInstruction(Instruction.Call(tablePosition, (ushort)b, (ushort)(resultCount < 0 ? 0 : resultCount + 1)), node.Position);
- context.StackPosition = (byte)(tablePosition + resultCount);
- }
- }
- // return
- public bool VisitReturnStatementNode(ReturnStatementNode node, ScopeCompilationContext context)
- {
- ushort b;
- // tail call
- if (node.Nodes.Length == 1)
- {
- var lastNode = node.Nodes[^1];
- if (lastNode is CallFunctionExpressionNode call)
- {
- CompileCallFunctionExpression(call, context, true, -1);
- return true;
- }
- else if (lastNode is CallTableMethodExpressionNode callMethod)
- {
- CompileTableMethod(callMethod, context, true, -1);
- return true;
- }
- }
- b = node.Nodes.Length > 0 && !IsFixedNumberOfReturnValues(node.Nodes[^1])
- ? (ushort)0
- : (ushort)(node.Nodes.Length + 1);
- var a = context.StackPosition;
- CompileExpressionList(node, node.Nodes, b - 1, context);
- context.PushInstruction(Instruction.Return(a, b), node.Position);
- return true;
- }
- // assignment
- public bool VisitLocalAssignmentStatementNode(LocalAssignmentStatementNode node, ScopeCompilationContext context)
- {
- var startPosition = context.StackPosition;
- CompileExpressionList(node, node.RightNodes, node.LeftNodes.Length, context);
- for (int i = 0; i < node.Identifiers.Length; i++)
- {
- context.StackPosition = (byte)(startPosition + i + 1);
- var identifier = node.Identifiers[i];
- if (context.TryGetLocalVariableInThisScope(identifier.Name, out var variable))
- {
- // assign local variable
- context.PushInstruction(Instruction.Move(variable.RegisterIndex, (ushort)(context.StackPosition - 1)), node.Position, true);
- }
- else
- {
- // register local variable
- context.AddLocalVariable(identifier.Name, new() { RegisterIndex = (byte)(context.StackPosition - 1), StartPc = context.Function.Instructions.Length, });
- }
- }
- return true;
- }
- public bool VisitAssignmentStatementNode(AssignmentStatementNode node, ScopeCompilationContext context)
- {
- var startPosition = context.StackPosition;
- CompileExpressionList(node, node.RightNodes, node.LeftNodes.Length, context);
- for (int i = 0; i < node.LeftNodes.Length; i++)
- {
- context.StackPosition = (byte)(startPosition + i + 1);
- var leftNode = node.LeftNodes[i];
- switch (leftNode)
- {
- case IdentifierNode identifier:
- {
- if (context.TryGetLocalVariable(identifier.Name, out var variable))
- {
- // assign local variable
- context.PushInstruction(Instruction.Move(variable.RegisterIndex, (ushort)(context.StackPosition - 1)), node.Position, true);
- }
- else if (context.Function.TryGetUpValue(identifier.Name, out var upValue))
- {
- // assign upvalue
- context.PushInstruction(Instruction.SetUpVal((byte)(context.StackPosition - 1), (ushort)upValue.Id), node.Position);
- }
- else if (context.TryGetLocalVariable("_ENV".AsMemory(), out variable))
- {
- // assign env element
- var index = context.Function.GetConstantIndex(identifier.Name.ToString()) + 256;
- context.PushInstruction(Instruction.SetTable(variable.RegisterIndex, (ushort)index, (ushort)(context.StackPosition - 1)), node.Position);
- }
- else
- {
- // assign global variable
- var index = context.Function.GetConstantIndex(identifier.Name.ToString()) + 256;
- context.PushInstruction(Instruction.SetTabUp(0, (ushort)index, (ushort)(context.StackPosition - 1)), node.Position);
- }
- }
- break;
- case TableIndexerAccessExpressionNode tableIndexer:
- {
- var valueIndex = context.StackPosition - 1;
- tableIndexer.TableNode.Accept(this, context);
- var tableIndex = context.StackPosition - 1;
- tableIndexer.KeyNode.Accept(this, context);
- var keyIndex = context.StackPosition - 1;
- context.PushInstruction(Instruction.SetTable((byte)tableIndex, (ushort)keyIndex, (ushort)valueIndex), node.Position);
- }
- break;
- case TableMemberAccessExpressionNode tableMember:
- {
- var valueIndex = context.StackPosition - 1;
- tableMember.TableNode.Accept(this, context);
- var tableIndex = context.StackPosition - 1;
- var keyIndex = context.Function.GetConstantIndex(tableMember.MemberName) + 256;
- context.PushInstruction(Instruction.SetTable((byte)tableIndex, (ushort)keyIndex, (ushort)valueIndex), node.Position);
- }
- break;
- default:
- throw new LuaParseException(default, default, "An error occurred while parsing the code"); // TODO: add message
- }
- }
- context.StackPosition = startPosition;
- return true;
- }
- // function call
- public bool VisitCallFunctionStatementNode(CallFunctionStatementNode node, ScopeCompilationContext context)
- {
- CompileCallFunctionExpression(node.Expression, context, false, 0);
- return true;
- }
- public bool VisitCallFunctionExpressionNode(CallFunctionExpressionNode node, ScopeCompilationContext context)
- {
- CompileCallFunctionExpression(node, context, false, 1);
- return true;
- }
- void CompileCallFunctionExpression(CallFunctionExpressionNode node, ScopeCompilationContext context, bool isTailCall, int resultCount)
- {
- // get closure
- var r = context.StackPosition;
- node.FunctionNode.Accept(this, context);
- // load arguments
- var b = node.ArgumentNodes.Length + 1;
- if (node.ArgumentNodes.Length > 0 && !IsFixedNumberOfReturnValues(node.ArgumentNodes[^1]))
- {
- b = 0;
- }
- CompileExpressionList(node, node.ArgumentNodes, b - 1, context);
- // push call interuction
- if (isTailCall)
- {
- context.PushInstruction(Instruction.TailCall(r, (ushort)b, 0), node.Position);
- context.StackPosition = r;
- }
- else
- {
- context.PushInstruction(Instruction.Call(r, (ushort)b, (ushort)(resultCount == -1 ? 0 : resultCount + 1)), node.Position);
- context.StackPosition = (byte)(r + resultCount);
- }
- }
- // function declaration
- public bool VisitFunctionDeclarationExpressionNode(FunctionDeclarationExpressionNode node, ScopeCompilationContext context)
- {
- var funcIndex = CompileFunctionProto(ReadOnlyMemory<char>.Empty, context, node.ParameterNodes, node.Nodes, node.ParameterNodes.Length, node.HasVariableArguments, false, node.LineDefined, node.EndPosition.Line);
- // push closure instruction
- context.PushInstruction(Instruction.Closure(context.StackPosition, funcIndex), node.EndPosition, true);
- return true;
- }
- public bool VisitLocalFunctionDeclarationStatementNode(LocalFunctionDeclarationStatementNode node, ScopeCompilationContext context)
- {
- // assign local variable
- context.AddLocalVariable(node.Name, new() { RegisterIndex = context.StackPosition, StartPc = context.Function.Instructions.Length, });
- // compile function
- var funcIndex = CompileFunctionProto(node.Name, context, node.ParameterNodes, node.Nodes, node.ParameterNodes.Length, node.HasVariableArguments, false, node.LineDefined, node.EndPosition.Line);
- // push closure instruction
- context.PushInstruction(Instruction.Closure(context.StackPosition, funcIndex), node.EndPosition, true);
- return true;
- }
- public bool VisitFunctionDeclarationStatementNode(FunctionDeclarationStatementNode node, ScopeCompilationContext context)
- {
- var funcIndex = CompileFunctionProto(node.Name, context, node.ParameterNodes, node.Nodes, node.ParameterNodes.Length, node.HasVariableArguments, false, node.LineDefined, node.EndPosition.Line);
- // add closure
- var index = context.Function.GetConstantIndex(node.Name.ToString());
- // push closure instruction
- context.PushInstruction(Instruction.Closure(context.StackPosition, funcIndex), node.EndPosition, true);
- if (context.TryGetLocalVariableInThisScope(node.Name, out var variable))
- {
- // assign local variable
- context.PushInstruction(Instruction.Move(variable.RegisterIndex, (ushort)(context.StackPosition - 1)), node.Position, true);
- }
- else
- {
- // assign global variable
- context.PushInstruction(Instruction.SetTabUp(0, (ushort)(index + 256), (ushort)(context.StackPosition - 1)), node.Position);
- }
- return true;
- }
- public bool VisitTableMethodDeclarationStatementNode(TableMethodDeclarationStatementNode node, ScopeCompilationContext context)
- {
- var funcIdentifier = node.MemberPath[^1];
- var funcIndex = CompileFunctionProto(funcIdentifier.Name, context, node.ParameterNodes, node.Nodes, node.ParameterNodes.Length + 1, node.HasVariableArguments, node.HasSelfParameter, node.LineDefined, node.EndPosition.Line);
- // add closure
- var index = context.Function.GetConstantIndex(funcIdentifier.Name.ToString());
- var r = context.StackPosition;
- // assign global variable
- var first = node.MemberPath[0];
- var tableIndex = GetOrLoadIdentifier(first.Name, context, first.Position, true);
- for (int i = 1; i < node.MemberPath.Length - 1; i++)
- {
- var member = node.MemberPath[i];
- var constant = context.Function.GetConstantIndex(member.Name.ToString());
- context.PushInstruction(Instruction.GetTable(context.StackPosition, tableIndex, (ushort)(constant + 256)), member.Position, true);
- tableIndex = context.StackTopPosition;
- }
- // push closure instruction
- var closureIndex = context.StackPosition;
- context.PushInstruction(Instruction.Closure(closureIndex, funcIndex), node.EndPosition, true);
- // set table
- context.PushInstruction(Instruction.SetTable(tableIndex, (ushort)(index + 256), closureIndex), funcIdentifier.Position);
- context.StackPosition = r;
- return true;
- }
- int CompileFunctionProto(ReadOnlyMemory<char> functionName, ScopeCompilationContext context, IdentifierNode[] parameters, SyntaxNode[] statements, int parameterCount, bool hasVarArg, bool hasSelfParameter, int lineDefined, int lastLineDefined)
- {
- using var funcContext = context.CreateChildFunction();
- funcContext.ChunkName = functionName.ToString();
- funcContext.ParameterCount = parameterCount;
- funcContext.HasVariableArguments = hasVarArg;
- funcContext.LineDefined = lineDefined;
- funcContext.LastLineDefined = lastLineDefined;
- if (hasSelfParameter)
- {
- funcContext.Scope.AddLocalVariable("self".AsMemory(), new() { RegisterIndex = 0, StartPc = 0, });
- funcContext.Scope.StackPosition++;
- }
- // add arguments
- for (int i = 0; i < parameters.Length; i++)
- {
- var parameter = parameters[i];
- funcContext.Scope.AddLocalVariable(parameter.Name, new() { RegisterIndex = (byte)(i + (hasSelfParameter ? 1 : 0)), StartPc = 0, });
- funcContext.Scope.StackPosition++;
- }
- foreach (var statement in statements)
- {
- statement.Accept(this, funcContext.Scope);
- }
- // compile function
- var chunk = funcContext.ToChunk();
- int index;
- if (functionName.Length == 0)
- {
- // anonymous function
- context.Function.AddFunctionProto(chunk, out index);
- }
- else
- {
- context.Function.AddOrSetFunctionProto(functionName, chunk, out index);
- }
- return index;
- }
- // control statements
- public bool VisitDoStatementNode(DoStatementNode node, ScopeCompilationContext context)
- {
- using var scopeContext = context.CreateChildScope();
- foreach (var childNode in node.StatementNodes)
- {
- childNode.Accept(this, scopeContext);
- }
- scopeContext.TryPushCloseUpValue(scopeContext.StackTopPosition, node.Position);
- return true;
- }
- public bool VisitBreakStatementNode(BreakStatementNode node, ScopeCompilationContext context)
- {
- context.Function.AddUnresolvedBreak(new() { Index = context.Function.Instructions.Length }, node.Position);
- context.PushInstruction(Instruction.Jmp(0, 0), node.Position);
- return true;
- }
- public bool VisitIfStatementNode(IfStatementNode node, ScopeCompilationContext context)
- {
- using var endJumpIndexList = new PooledList<int>(8);
- var hasElse = node.ElseNodes.Length > 0;
- var stackPositionToClose = (byte)(context.StackPosition + 1);
- // if
- using (var scopeContext = context.CreateChildScope())
- {
- CompileConditionNode(node.IfNode.ConditionNode, scopeContext, true, node.IfNode.Position);
- var ifPosition = scopeContext.Function.Instructions.Length;
- scopeContext.PushInstruction(Instruction.Jmp(0, 0), node.IfNode.Position);
- foreach (var childNode in node.IfNode.ThenNodes)
- {
- childNode.Accept(this, scopeContext);
- }
- stackPositionToClose = scopeContext.HasCapturedLocalVariables ? stackPositionToClose : (byte)0;
- if (hasElse)
- {
- endJumpIndexList.Add(scopeContext.Function.Instructions.Length);
- scopeContext.PushInstruction(Instruction.Jmp(stackPositionToClose, 0), node.IfNode.ThenNodes[^1].Position, true);
- }
- else
- {
- scopeContext.TryPushCloseUpValue(stackPositionToClose, node.Position);
- }
- scopeContext.Function.Instructions[ifPosition].SBx = scopeContext.Function.Instructions.Length - 1 - ifPosition;
- }
- // elseif
- foreach (var elseIf in node.ElseIfNodes)
- {
- using var scopeContext = context.CreateChildScope();
- CompileConditionNode(elseIf.ConditionNode, scopeContext, true);
- var elseifPosition = scopeContext.Function.Instructions.Length;
- scopeContext.PushInstruction(Instruction.Jmp(0, 0), elseIf.Position);
- foreach (var childNode in elseIf.ThenNodes)
- {
- childNode.Accept(this, scopeContext);
- }
- stackPositionToClose = scopeContext.HasCapturedLocalVariables ? stackPositionToClose : (byte)0;
- // skip if node doesn't have else statements
- if (hasElse)
- {
- endJumpIndexList.Add(scopeContext.Function.Instructions.Length);
- scopeContext.PushInstruction(Instruction.Jmp(stackPositionToClose, 0), elseIf.Position);
- }
- else
- {
- scopeContext.TryPushCloseUpValue(stackPositionToClose, elseIf.Position);
- }
- scopeContext.Function.Instructions[elseifPosition].SBx = scopeContext.Function.Instructions.Length - 1 - elseifPosition;
- }
- // else nodes
- using (var scopeContext = context.CreateChildScope())
- {
- foreach (var childNode in node.ElseNodes)
- {
- childNode.Accept(this, scopeContext);
- }
- scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
- }
- // set JMP sBx
- foreach (var index in endJumpIndexList.AsSpan())
- {
- context.Function.Instructions[index].SBx = context.Function.Instructions.Length - 1 - index;
- }
- return true;
- }
- public bool VisitRepeatStatementNode(RepeatStatementNode node, ScopeCompilationContext context)
- {
- var startIndex = context.Function.Instructions.Length;
- context.Function.LoopLevel++;
- using var scopeContext = context.CreateChildScope();
- var stackPosition = scopeContext.StackPosition;
- foreach (var childNode in node.Nodes)
- {
- childNode.Accept(this, scopeContext);
- }
- CompileConditionNode(node.ConditionNode, scopeContext, true);
- var a = scopeContext.HasCapturedLocalVariables ? (byte)(stackPosition + 1) : (byte)0;
- var untilPosition = node.ConditionNode.Position;
- scopeContext.PushInstruction(Instruction.Jmp(a, startIndex - scopeContext.Function.Instructions.Length - 1), untilPosition);
- scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, untilPosition);
- context.Function.LoopLevel--;
- // resolve break statements inside repeat block
- context.Function.ResolveAllBreaks(a, context.Function.Instructions.Length - 1, scopeContext);
- return true;
- }
- public bool VisitWhileStatementNode(WhileStatementNode node, ScopeCompilationContext context)
- {
- var conditionIndex = context.Function.Instructions.Length;
- context.PushInstruction(Instruction.Jmp(0, 0), node.Position);
- context.Function.LoopLevel++;
- using var scopeContext = context.CreateChildScope();
- var stackPosition = scopeContext.StackPosition;
- foreach (var childNode in node.Nodes)
- {
- childNode.Accept(this, scopeContext);
- }
- context.Function.LoopLevel--;
- // set JMP sBx
- scopeContext.Function.Instructions[conditionIndex].SBx = scopeContext.Function.Instructions.Length - 1 - conditionIndex;
- CompileConditionNode(node.ConditionNode, scopeContext, false);
- var a = scopeContext.HasCapturedLocalVariables ? (byte)(1 + stackPosition) : (byte)0;
- scopeContext.PushInstruction(Instruction.Jmp(a, conditionIndex - context.Function.Instructions.Length), node.Position);
- scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
- // resolve break statements inside while block
- context.Function.ResolveAllBreaks(scopeContext.StackPosition, context.Function.Instructions.Length - 1, scopeContext);
- return true;
- }
- public bool VisitNumericForStatementNode(NumericForStatementNode node, ScopeCompilationContext context)
- {
- var startPosition = context.StackPosition;
- node.InitNode.Accept(this, context);
- node.LimitNode.Accept(this, context);
- if (node.StepNode != null)
- {
- node.StepNode.Accept(this, context);
- }
- else
- {
- var index = context.Function.GetConstantIndex(1);
- context.PushInstruction(Instruction.LoadK(context.StackPosition, index), node.DoPosition, true);
- }
- var prepIndex = context.Function.Instructions.Length;
- context.PushInstruction(Instruction.ForPrep(startPosition, 0), node.DoPosition, true);
- // compile statements
- context.Function.LoopLevel++;
- using var scopeContext = context.CreateChildScope();
- {
- scopeContext.AddLocalVariable("(for index)".AsMemory(), new() { RegisterIndex = startPosition, StartPc = context.Function.Instructions.Length, });
- scopeContext.AddLocalVariable("(for limit)".AsMemory(), new() { RegisterIndex = (byte)(startPosition + 1), StartPc = context.Function.Instructions.Length, });
- scopeContext.AddLocalVariable("(for step)".AsMemory(), new() { RegisterIndex = (byte)(startPosition + 2), StartPc = context.Function.Instructions.Length, });
- // add local variable
- scopeContext.AddLocalVariable(node.VariableName, new() { RegisterIndex = (byte)(startPosition + 3), StartPc = context.Function.Instructions.Length, });
- foreach (var childNode in node.StatementNodes)
- {
- childNode.Accept(this, scopeContext);
- }
- scopeContext.TryPushCloseUpValue((byte)(startPosition + 1), node.Position);
- }
- context.Function.LoopLevel--;
- // set ForPrep
- context.Function.Instructions[prepIndex].SBx = context.Function.Instructions.Length - prepIndex - 1;
- // push ForLoop
- context.PushInstruction(Instruction.ForLoop(startPosition, prepIndex - context.Function.Instructions.Length), node.Position);
- context.Function.ResolveAllBreaks((byte)(startPosition + 1), context.Function.Instructions.Length - 1, scopeContext);
- context.StackPosition = startPosition;
- return true;
- }
- public bool VisitGenericForStatementNode(GenericForStatementNode node, ScopeCompilationContext context)
- {
- // get iterator
- var startPosition = context.StackPosition;
- CompileExpressionList(node, node.ExpressionNodes, 3, context);
- // jump to TFORCALL
- var startJumpIndex = context.Function.Instructions.Length;
- context.PushInstruction(Instruction.Jmp(0, 0), node.DoPosition);
- // compile statements
- context.Function.LoopLevel++;
- using var scopeContext = context.CreateChildScope();
- {
- scopeContext.StackPosition = (byte)(startPosition + 3 + node.Names.Length);
- scopeContext.AddLocalVariable("(for generator)".AsMemory(), new() { RegisterIndex = (byte)(startPosition), StartPc = context.Function.Instructions.Length, });
- scopeContext.AddLocalVariable("(for state)".AsMemory(), new() { RegisterIndex = (byte)(startPosition + 1), StartPc = context.Function.Instructions.Length, });
- scopeContext.AddLocalVariable("(for control)".AsMemory(), new() { RegisterIndex = (byte)(startPosition + 2), StartPc = context.Function.Instructions.Length, });
- // add local variables
- for (int i = 0; i < node.Names.Length; i++)
- {
- var name = node.Names[i];
- scopeContext.AddLocalVariable(name.Name, new() { RegisterIndex = (byte)(startPosition + 3 + i), StartPc = context.Function.Instructions.Length, });
- }
- foreach (var childNode in node.StatementNodes)
- {
- childNode.Accept(this, scopeContext);
- }
- scopeContext.TryPushCloseUpValue(scopeContext.StackPosition, node.Position);
- }
- context.Function.LoopLevel--;
- // set jump
- context.Function.Instructions[startJumpIndex].SBx = context.Function.Instructions.Length - startJumpIndex - 1;
- // push OP_TFORCALL and OP_TFORLOOP
- context.PushInstruction(Instruction.TForCall(startPosition, (ushort)node.Names.Length), node.Position);
- context.PushInstruction(Instruction.TForLoop((byte)(startPosition + 2), startJumpIndex - context.Function.Instructions.Length), node.Position);
- context.Function.ResolveAllBreaks((byte)(startPosition + 1), context.Function.Instructions.Length - 1, scopeContext);
- context.StackPosition = startPosition;
- return true;
- }
- public bool VisitLabelStatementNode(LabelStatementNode node, ScopeCompilationContext context)
- {
- var desc = new LabelDescription() { Name = node.Name, Index = context.Function.Instructions.Length, RegisterIndex = context.StackPosition };
- context.AddLabel(desc);
- context.Function.ResolveGoto(desc);
- return true;
- }
- public bool VisitGotoStatementNode(GotoStatementNode node, ScopeCompilationContext context)
- {
- if (context.TryGetLabel(node.Name, out var description))
- {
- context.PushInstruction(Instruction.Jmp(description.RegisterIndex, description.Index - context.Function.Instructions.Length - 1), node.Position);
- }
- else
- {
- context.Function.AddUnresolvedGoto(new() { Name = node.Name, JumpInstructionIndex = context.Function.Instructions.Length });
- // add uninitialized jmp instruction
- context.PushInstruction(Instruction.Jmp(0, 0), node.Position);
- }
- return true;
- }
- static byte GetOrLoadIdentifier(ReadOnlyMemory<char> name, ScopeCompilationContext context, SourcePosition sourcePosition, bool dontLoadLocalVariable)
- {
- var p = context.StackPosition;
- if (context.TryGetLocalVariable(name, out var variable))
- {
- if (dontLoadLocalVariable)
- {
- return variable.RegisterIndex;
- }
- else if (p == variable.RegisterIndex)
- {
- context.StackPosition++;
- return p;
- }
- else
- {
- context.PushInstruction(Instruction.Move(p, variable.RegisterIndex), sourcePosition, true);
- return p;
- }
- }
- else if (context.Function.TryGetUpValue(name, out var upValue))
- {
- context.PushInstruction(Instruction.GetUpVal(p, (ushort)upValue.Id), sourcePosition, true);
- return p;
- }
- else if (context.TryGetLocalVariable("_ENV".AsMemory(), out variable))
- {
- var keyStringIndex = context.Function.GetConstantIndex(name.ToString()) + 256;
- context.PushInstruction(Instruction.GetTable(p, variable.RegisterIndex, (ushort)keyStringIndex), sourcePosition, true);
- return p;
- }
- else
- {
- context.Function.TryGetUpValue("_ENV".AsMemory(), out upValue);
- var index = context.Function.GetConstantIndex(name.ToString()) + 256;
- context.PushInstruction(Instruction.GetTabUp(p, (ushort)upValue.Id, (ushort)index), sourcePosition, true);
- return p;
- }
- }
- uint GetRKIndex(ExpressionNode node, ScopeCompilationContext context)
- {
- if (node is IdentifierNode identifier)
- {
- return GetOrLoadIdentifier(identifier.Name, context, identifier.Position, true);
- }
- else if (TryGetConstant(node, context, out var constant))
- {
- return context.Function.GetConstantIndex(constant) + 256;
- }
- else
- {
- node.Accept(this, context);
- return context.StackTopPosition;
- }
- }
- static bool TryGetConstant(ExpressionNode node, ScopeCompilationContext context, out LuaValue value)
- {
- switch (node)
- {
- case NilLiteralNode:
- value = LuaValue.Nil;
- return true;
- case BooleanLiteralNode booleanLiteral:
- value = booleanLiteral.Value;
- return true;
- case NumericLiteralNode numericLiteral:
- value = numericLiteral.Value;
- return true;
- case StringLiteralNode stringLiteral:
- if (stringLiteral.IsShortLiteral)
- {
- if (!StringHelper.TryFromStringLiteral(stringLiteral.Text.Span, out var str))
- {
- throw new LuaParseException(context.Function.ChunkName, stringLiteral.Position, $"invalid escape sequence near '{stringLiteral.Text}'");
- }
- value = str;
- }
- else
- {
- value = stringLiteral.Text.ToString();
- }
- return true;
- case UnaryExpressionNode unaryExpression:
- if (TryGetConstant(unaryExpression.Node, context, out var unaryNodeValue))
- {
- switch (unaryExpression.Operator)
- {
- case UnaryOperator.Negate:
- if (unaryNodeValue.TryRead<double>(out var d1))
- {
- value = -d1;
- return true;
- }
- break;
- case UnaryOperator.Not:
- if (unaryNodeValue.TryRead<bool>(out var b))
- {
- value = !b;
- return true;
- }
- break;
- }
- }
- break;
- case BinaryExpressionNode binaryExpression:
- if (TryGetConstant(binaryExpression.LeftNode, context, out var leftValue) &&
- TryGetConstant(binaryExpression.RightNode, context, out var rightValue))
- {
- switch (binaryExpression.OperatorType)
- {
- case BinaryOperator.Addition:
- {
- if (leftValue.TryRead<double>(out var d1) && rightValue.TryRead<double>(out var d2))
- {
- value = d1 + d2;
- return true;
- }
- }
- break;
- case BinaryOperator.Subtraction:
- {
- if (leftValue.TryRead<double>(out var d1) && rightValue.TryRead<double>(out var d2))
- {
- value = d1 - d2;
- return true;
- }
- }
- break;
- case BinaryOperator.Multiplication:
- {
- if (leftValue.TryRead<double>(out var d1) && rightValue.TryRead<double>(out var d2))
- {
- value = d1 * d2;
- return true;
- }
- }
- break;
- case BinaryOperator.Division:
- {
- if (leftValue.TryRead<double>(out var d1) && rightValue.TryRead<double>(out var d2) && d2 != 0)
- {
- value = d1 / d2;
- return true;
- }
- }
- break;
- }
- }
- break;
- }
- value = default;
- return false;
- }
- static bool IsFixedNumberOfReturnValues(ExpressionNode node)
- {
- return node is not (CallFunctionExpressionNode or CallTableMethodExpressionNode or VariableArgumentsExpressionNode);
- }
- /// <summary>
- /// Compiles a conditional boolean branch: if true (or false), the next instruction added is skipped.
- /// </summary>
- /// <param name="node">Condition node</param>
- /// <param name="context">Context</param>
- /// <param name="falseIsSkip">If true, generates an instruction sequence that skips the next instruction if the condition is false.</param>
- /// <param name="testPosition">Position of the test instruction</param>
- void CompileConditionNode(ExpressionNode node, ScopeCompilationContext context, bool falseIsSkip, SourcePosition? testPosition = null)
- {
- if (node is BinaryExpressionNode binaryExpression)
- {
- switch (binaryExpression.OperatorType)
- {
- case BinaryOperator.Equality:
- {
- var b = (ushort)GetRKIndex(binaryExpression.LeftNode, context);
- var c = (ushort)GetRKIndex(binaryExpression.RightNode, context);
- context.PushInstruction(Instruction.Eq(falseIsSkip ? (byte)0 : (byte)1, b, c), node.Position);
- return;
- }
- case BinaryOperator.Inequality:
- {
- var b = (ushort)GetRKIndex(binaryExpression.LeftNode, context);
- var c = (ushort)GetRKIndex(binaryExpression.RightNode, context);
- context.PushInstruction(Instruction.Eq(falseIsSkip ? (byte)1 : (byte)0, b, c), node.Position);
- return;
- }
- case BinaryOperator.LessThan:
- {
- var b = (ushort)GetRKIndex(binaryExpression.LeftNode, context);
- var c = (ushort)GetRKIndex(binaryExpression.RightNode, context);
- context.PushInstruction(Instruction.Lt(falseIsSkip ? (byte)0 : (byte)1, b, c), node.Position);
- return;
- }
- case BinaryOperator.LessThanOrEqual:
- {
- var b = (ushort)GetRKIndex(binaryExpression.LeftNode, context);
- var c = (ushort)GetRKIndex(binaryExpression.RightNode, context);
- context.PushInstruction(Instruction.Le(falseIsSkip ? (byte)0 : (byte)1, b, c), node.Position);
- return;
- }
- case BinaryOperator.GreaterThan:
- {
- var b = (ushort)GetRKIndex(binaryExpression.LeftNode, context);
- var c = (ushort)GetRKIndex(binaryExpression.RightNode, context);
- context.PushInstruction(Instruction.Lt(falseIsSkip ? (byte)0 : (byte)1, c, b), node.Position);
- return;
- }
- case BinaryOperator.GreaterThanOrEqual:
- {
- var b = (ushort)GetRKIndex(binaryExpression.LeftNode, context);
- var c = (ushort)GetRKIndex(binaryExpression.RightNode, context);
- context.PushInstruction(Instruction.Le(falseIsSkip ? (byte)0 : (byte)1, c, b), node.Position);
- return;
- }
- }
- }
- node.Accept(this, context);
- context.PushInstruction(Instruction.Test((byte)(context.StackPosition - 1), falseIsSkip ? (byte)0 : (byte)1), testPosition ?? node.Position);
- }
- void CompileExpressionList(SyntaxNode rootNode, ExpressionNode[] expressions, int minimumCount, ScopeCompilationContext context)
- {
- var isLastFunction = false;
- for (int i = 0; i < expressions.Length; i++)
- {
- var expression = expressions[i];
- var isLast = i == expressions.Length - 1;
- var resultCount = isLast ? (minimumCount == -1 ? -1 : minimumCount - i) : 1;
- if (expression is CallFunctionExpressionNode call)
- {
- CompileCallFunctionExpression(call, context, false, resultCount);
- isLastFunction = isLast;
- }
- else if (expression is CallTableMethodExpressionNode method)
- {
- CompileTableMethod(method, context, false, resultCount);
- isLastFunction = isLast;
- }
- else if (expression is VariableArgumentsExpressionNode varArg)
- {
- CompileVariableArgumentsExpression(varArg, context, resultCount);
- isLastFunction = isLast;
- }
- else if (TryGetConstant(expression, context, out var constant))
- {
- var index = context.Function.GetConstantIndex(constant);
- context.PushInstruction(Instruction.LoadK(context.StackPosition, index), expression.Position, true);
- isLastFunction = false;
- }
- else
- {
- expression.Accept(this, context);
- isLastFunction = false;
- }
- }
- // fill space with nil
- var varCount = minimumCount - expressions.Length;
- if (varCount > 0 && !isLastFunction)
- {
- context.PushInstruction(Instruction.LoadNil(context.StackPosition, (ushort)varCount), rootNode.Position);
- context.StackPosition = (byte)(context.StackPosition + varCount);
- }
- }
- }
|