private void BuildOperatorMethodDeclaration(BaseMethodDeclarationSyntax node)
        {
            var symbol = semanticModel_.GetDeclaredSymbol(node);

            methodInfos_.Push(new MethodInfo(symbol));

            bool isStatic  = symbol.IsStatic;
            bool isPrivate = symbol.IsPrivate();

            LuaIdentifierNameSyntax name         = GetMemberName(symbol);
            var parameterList                    = (LuaParameterListSyntax)node.ParameterList.Accept(this);
            LuaFunctionExpressionSyntax function = new LuaFunctionExpressionSyntax();

            function.ParameterList.Parameters.AddRange(parameterList.Parameters);
            PushFunction(function);

            var            comments = BuildDocumentationComment(node);
            LuaBlockSyntax block    = (LuaBlockSyntax)node.Body.Accept(this);

            function.AddStatements(block.Statements);
            CurType.AddMethod(name, function, isPrivate, symbol.IsStaticLazy(), comments);

            PopFunction();
            methodInfos_.Pop();
        }
Example #2
0
 internal void Render(LuaBlockSyntax node)
 {
     Write(node.OpenToken);
     WriteNewLine();
     AddIndent();
     WriteNodes(node.Statements);
     Outdent();
     Write(node.CloseToken);
 }
Example #3
0
 internal void Render(LuaBlockSyntax node)
 {
     Write(node.OpenBraceToken);
     WriteNewLine();
     AddIndent();
     foreach (var statement in node.Statements)
     {
         statement.Render(this);
     }
     Outdent();
     Write(node.CloseBraceToken);
 }
        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);
        }
        public override LuaSyntaxNode VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            IMethodSymbol ctorSymbol = semanticModel_.GetDeclaredSymbol(node);

            methodInfos_.Push(new MethodInfo(ctorSymbol));

            LuaConstructorAdapterExpressionSyntax function = new LuaConstructorAdapterExpressionSyntax();

            PushFunction(function);
            bool isStatic = node.Modifiers.IsStatic();

            function.AddParameter(LuaIdentifierNameSyntax.This);
            var parameterList = (LuaParameterListSyntax)node.ParameterList.Accept(this);

            function.ParameterList.Parameters.AddRange(parameterList.Parameters);

            if (node.Initializer != null)
            {
                var symbol      = (IMethodSymbol)semanticModel_.GetSymbolInfo(node.Initializer).Symbol;
                int ctroCounter = GetConstructorIndex(symbol);
                LuaInvocationExpressionSyntax otherCtorInvoke;
                if (node.Initializer.IsKind(SyntaxKind.ThisConstructorInitializer))
                {
                    Contract.Assert(ctroCounter != 0);
                    LuaIdentifierNameSyntax thisCtor = new LuaIdentifierNameSyntax(LuaSyntaxNode.SpecailWord(LuaSyntaxNode.Tokens.Ctor + ctroCounter));
                    otherCtorInvoke           = new LuaInvocationExpressionSyntax(thisCtor);
                    function.IsInvokeThisCtor = true;
                }
                else
                {
                    otherCtorInvoke = BuildCallBaseConstructor(symbol.ReceiverType, ctroCounter);
                }
                otherCtorInvoke.AddArgument(LuaIdentifierNameSyntax.This);
                var arguments = BuildArgumentList(symbol, symbol.Parameters, node.Initializer.ArgumentList);
                otherCtorInvoke.AddArguments(arguments);
                function.AddStatement(otherCtorInvoke);
            }
            else if (!isStatic)
            {
                var baseCtorInvoke = BuildCallBaseConstructor(ctorSymbol.ContainingType);
                if (baseCtorInvoke != null)
                {
                    function.AddStatement(baseCtorInvoke);
                }
            }

            LuaBlockSyntax block = (LuaBlockSyntax)node.Body.Accept(this);

            function.AddStatements(block.Statements);
            PopFunction();
            if (isStatic)
            {
                CurType.SetStaticCtor(function);
            }
            else
            {
                CurType.AddCtor(function, node.ParameterList.Parameters.Count == 0);
            }

            methodInfos_.Pop();
            return(function);
        }