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));
            }
        }
Beispiel #2
0
 internal void Render(LuaMultipleReturnStatementSyntax node)
 {
     Write(node.ReturnKeyword);
     if (node.Expressions.Count > 0)
     {
         WriteSpace();
         WriteSeparatedSyntaxList(node.Expressions);
     }
     Write(node.SemicolonToken);
     WriteNewLine();
 }
        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);
        }