Ejemplo n.º 1
0
        public LuaStatementSyntax Transpile(JassExitStatementSyntax exitStatement)
        {
            var ifStatement = new LuaIfStatementSyntax(Transpile(exitStatement.Condition, out _));

            ifStatement.Body.Statements.Add(LuaBreakStatementSyntax.Instance);

            return(ifStatement);
        }
        private LuaStatementSyntax BuildCheckReturnInvocationExpression(LuaInvocationExpressionSyntax invocationExpression, SyntaxNode node)
        {
            if (IsReturnExists(node))
            {
                var  curMethodInfo = CurMethodInfoOrNull;
                bool isReturnVoid  = curMethodInfo != null && curMethodInfo.Symbol.ReturnsVoid;

                var temp1 = GetTempIdentifier(node);
                var temp2 = isReturnVoid ? null : GetTempIdentifier(node);
                LuaLocalVariablesStatementSyntax localVariables = new LuaLocalVariablesStatementSyntax();
                localVariables.Variables.Add(temp1);
                if (temp2 != null)
                {
                    localVariables.Variables.Add(temp2);
                }
                LuaEqualsValueClauseListSyntax initializer = new LuaEqualsValueClauseListSyntax();
                initializer.Values.Add(invocationExpression);
                localVariables.Initializer = initializer;

                LuaIfStatementSyntax ifStatement = new LuaIfStatementSyntax(temp1);
                if (CurFunction is LuaCheckReturnFunctionExpressionSyntax)
                {
                    LuaMultipleReturnStatementSyntax returnStatement = new LuaMultipleReturnStatementSyntax();
                    returnStatement.Expressions.Add(LuaIdentifierNameSyntax.True);
                    if (temp2 != null)
                    {
                        returnStatement.Expressions.Add(temp2);
                    }
                    ifStatement.Body.Statements.Add(returnStatement);
                }
                else
                {
                    if (curMethodInfo != null && curMethodInfo.RefOrOutParameters.Count > 0)
                    {
                        LuaMultipleReturnStatementSyntax returnStatement = new LuaMultipleReturnStatementSyntax();
                        if (temp2 != null)
                        {
                            returnStatement.Expressions.Add(temp2);
                        }
                        returnStatement.Expressions.AddRange(curMethodInfo.RefOrOutParameters);
                        ifStatement.Body.Statements.Add(returnStatement);
                    }
                    else
                    {
                        ifStatement.Body.Statements.Add(new LuaReturnStatementSyntax(temp2));
                    }
                }

                LuaStatementListSyntax statements = new LuaStatementListSyntax();
                statements.Statements.Add(localVariables);
                statements.Statements.Add(ifStatement);
                return(statements);
            }
            else
            {
                return(new LuaExpressionStatementSyntax(invocationExpression));
            }
        }
        public override LuaSyntaxNode VisitConditionalAccessExpression(ConditionalAccessExpressionSyntax node)
        {
            bool isEmpty = functions_.Count == 0;

            if (isEmpty)
            {
                LuaFunctionExpressionSyntax function = new LuaFunctionExpressionSyntax();
                PushFunction(function);
                blocks_.Push(function.Body);
            }

            var temp = GetTempIdentifier(node.Expression);

            conditionalTemps_.Push(temp);

            var expression = (LuaExpressionSyntax)node.Expression.Accept(this);

            CurBlock.Statements.Add(new LuaLocalVariableDeclaratorSyntax(temp, expression));

            LuaBinaryExpressionSyntax condition   = new LuaBinaryExpressionSyntax(temp, LuaSyntaxNode.Tokens.NotEquals, LuaIdentifierNameSyntax.Nil);
            LuaIfStatementSyntax      ifStatement = new LuaIfStatementSyntax(condition);

            CurBlock.Statements.Add(ifStatement);

            blocks_.Push(ifStatement.Body);
            var whenNotNull = (LuaExpressionSyntax)node.WhenNotNull.Accept(this);

            blocks_.Pop();
            conditionalTemps_.Pop();

            if (node.Parent.IsKind(SyntaxKind.ExpressionStatement))
            {
                if (isEmpty)
                {
                    throw new InvalidOperationException();
                }
                ifStatement.Body.Statements.Add(new LuaExpressionStatementSyntax(whenNotNull));
                return(LuaExpressionSyntax.EmptyExpression);
            }
            else
            {
                LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(temp, whenNotNull);
                ifStatement.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                if (isEmpty)
                {
                    var function = CurFunction;
                    function.AddStatement(new LuaReturnStatementSyntax(temp));
                    blocks_.Pop();
                    PopFunction();
                    return(new LuaInvocationExpressionSyntax(new LuaParenthesizedExpressionSyntax(function)));
                }
                else
                {
                    return(temp);
                }
            }
        }
Ejemplo n.º 4
0
        public LuaStatementSyntax Transpile(JassIfStatementSyntax ifStatement)
        {
            var luaIfStatement = new LuaIfStatementSyntax(Transpile(ifStatement.Condition, out _));

            luaIfStatement.Body.Statements.AddRange(Transpile(ifStatement.Body));
            luaIfStatement.ElseIfStatements.AddRange(ifStatement.ElseIfClauses.Select(Transpile));
            luaIfStatement.Else = ifStatement.ElseClause is null ? null : Transpile(ifStatement.ElseClause);

            return(luaIfStatement);
        }
Ejemplo n.º 5
0
 internal void Render(LuaIfStatementSyntax node)
 {
     Write(node.IfKeyword);
     WriteSpace();
     node.Condition.Render(this);
     WriteSpace();
     Write(node.OpenParenToken);
     node.Body.Render(this);
     WriteNodes(node.ElseIfStatements);
     node.Else?.Render(this);
     Write(node.CloseParenToken);
     WriteNewLine();
 }
Ejemplo n.º 6
0
        private void AddStructEqualsObjMethod(INamedTypeSymbol symbol, LuaStructDeclarationSyntax declaration, LuaExpressionSyntax typeName, List <LuaIdentifierNameSyntax> fields)
        {
            var thisIdentifier = LuaIdentifierNameSyntax.This;
            LuaIdentifierNameSyntax     obj = LuaIdentifierNameSyntax.Obj;
            LuaFunctionExpressionSyntax functionExpression = new LuaFunctionExpressionSyntax();

            functionExpression.AddParameter(thisIdentifier);
            functionExpression.AddParameter(obj);

            var left = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.getmetatable, obj);
            LuaIfStatementSyntax ifStatement = new LuaIfStatementSyntax(new LuaBinaryExpressionSyntax(left, LuaSyntaxNode.Tokens.NotEquals, typeName));

            ifStatement.Body.Statements.Add(new LuaReturnStatementSyntax(LuaIdentifierNameSyntax.False));
            functionExpression.AddStatement(ifStatement);

            if (fields.Count > 0)
            {
                var equalsStatic = LuaIdentifierNameSyntax.EqualsStatic;
                LuaLocalVariableDeclaratorSyntax variableDeclarator = new LuaLocalVariableDeclaratorSyntax(equalsStatic, LuaIdentifierNameSyntax.SystemObjectEqualsStatic);
                functionExpression.AddStatement(variableDeclarator);
                LuaExpressionSyntax expression = null;
                foreach (LuaIdentifierNameSyntax field in fields)
                {
                    LuaMemberAccessExpressionSyntax argument1  = new LuaMemberAccessExpressionSyntax(thisIdentifier, field);
                    LuaMemberAccessExpressionSyntax argument2  = new LuaMemberAccessExpressionSyntax(obj, field);
                    LuaInvocationExpressionSyntax   invocation = new LuaInvocationExpressionSyntax(equalsStatic, argument1, argument2);
                    if (expression == null)
                    {
                        expression = invocation;
                    }
                    else
                    {
                        expression = new LuaBinaryExpressionSyntax(expression, LuaSyntaxNode.Tokens.And, invocation);
                    }
                }
                Contract.Assert(expression != null);
                functionExpression.AddStatement(new LuaReturnStatementSyntax(expression));
            }
            declaration.AddMethod(LuaIdentifierNameSyntax.EqualsObj, functionExpression, false);
        }
        private LuaTryAdapterExpressionSyntax VisitTryCatchesExpress(SyntaxList <CatchClauseSyntax> catches)
        {
            LuaTryAdapterExpressionSyntax functionExpress = new LuaTryAdapterExpressionSyntax();

            PushFunction(functionExpress);
            var temp = GetTempIdentifier(catches.First());

            functionExpress.CatchTemp = temp;
            functionExpress.AddParameter(temp);

            LuaIfStatementSyntax ifStatement = null;
            bool hasCatchRoot = false;

            foreach (var catchNode in catches)
            {
                bool isRootExceptionDeclaration = false;
                LuaExpressionSyntax ifCondition = null;
                if (catchNode.Filter != null)
                {
                    ifCondition = (LuaExpressionSyntax)catchNode.Filter.Accept(this);
                }
                if (catchNode.Declaration != null)
                {
                    var typeName = (LuaIdentifierNameSyntax)catchNode.Declaration.Type.Accept(this);
                    if (typeName.ValueText != "System.Exception")
                    {
                        var mathcTypeInvocation = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Is, temp, typeName);
                        if (ifCondition != null)
                        {
                            ifCondition = new LuaBinaryExpressionSyntax(ifCondition, LuaSyntaxNode.Tokens.And, mathcTypeInvocation);
                        }
                        else
                        {
                            ifCondition = mathcTypeInvocation;
                        }
                    }
                    else
                    {
                        if (!catchNode.Declaration.Identifier.IsKind(SyntaxKind.None))
                        {
                            isRootExceptionDeclaration = true;
                        }
                        hasCatchRoot = true;
                    }
                }
                else
                {
                    hasCatchRoot = true;
                }

                var block = (LuaBlockSyntax)catchNode.Block.Accept(this);
                if (ifCondition != null)
                {
                    LuaBlockSyntax body;
                    if (ifStatement == null)
                    {
                        ifStatement = new LuaIfStatementSyntax(ifCondition);
                        body        = ifStatement.Body;
                    }
                    else
                    {
                        LuaElseIfStatementSyntax elseIfStatement = new LuaElseIfStatementSyntax(ifCondition);
                        body = elseIfStatement.Body;
                        ifStatement.ElseIfStatements.Add(elseIfStatement);
                    }
                    if (catchNode.Declaration != null && !catchNode.Declaration.Identifier.IsKind(SyntaxKind.None))
                    {
                        var variableDeclarator = (LuaVariableDeclaratorSyntax)catchNode.Declaration.Accept(this);
                        variableDeclarator.Initializer = new LuaEqualsValueClauseSyntax(temp);
                        body.Statements.Add(new LuaLocalVariableDeclaratorSyntax(variableDeclarator));
                    }
                    body.Statements.AddRange(block.Statements);
                }
                else
                {
                    if (isRootExceptionDeclaration)
                    {
                        var variableDeclarator = (LuaVariableDeclaratorSyntax)catchNode.Declaration.Accept(this);
                        variableDeclarator.Initializer = new LuaEqualsValueClauseSyntax(temp);
                        block.Statements.Insert(0, new LuaLocalVariableDeclaratorSyntax(variableDeclarator));
                    }

                    if (ifStatement != null)
                    {
                        LuaElseClauseSyntax elseClause = new LuaElseClauseSyntax();
                        elseClause.Body.Statements.AddRange(block.Statements);
                        ifStatement.Else = elseClause;
                    }
                    else
                    {
                        functionExpress.AddStatements(block.Statements);
                    }
                    break;
                }
            }

            if (ifStatement != null)
            {
                if (!hasCatchRoot)
                {
                    Contract.Assert(ifStatement.Else == null);
                    LuaMultipleReturnStatementSyntax rethrowStatement = new LuaMultipleReturnStatementSyntax();
                    rethrowStatement.Expressions.Add(LuaIdentifierNameSyntax.One);
                    rethrowStatement.Expressions.Add(temp);
                    LuaBlockSyntax block = new LuaBlockSyntax();
                    block.Statements.Add(rethrowStatement);
                    LuaElseClauseSyntax elseClause = new LuaElseClauseSyntax();
                    elseClause.Body.Statements.AddRange(block.Statements);
                    ifStatement.Else = elseClause;
                }
                functionExpress.AddStatement(ifStatement);
            }

            PopFunction();
            return(functionExpress);
        }