Exemplo n.º 1
0
        public void Fill(LuaExpressionSyntax expression, IEnumerable <LuaStatementSyntax> sections)
        {
            if (expression == null)
            {
                throw new ArgumentNullException(nameof(expression));
            }
            if (sections == null)
            {
                throw new ArgumentNullException(nameof(sections));
            }

            var body = RepeatStatement.Body;

            body.Statements.Add(caseLabelVariables_);
            body.Statements.Add(new LuaLocalVariableDeclaratorSyntax(Temp, expression));

            LuaIfStatementSyntax ifStatement = null;

            foreach (var section in sections)
            {
                LuaIfStatementSyntax statement = section as LuaIfStatementSyntax;
                if (statement != null)
                {
                    if (ifStatement == null)
                    {
                        ifStatement = statement;
                    }
                    else
                    {
                        LuaElseIfStatementSyntax elseIfStatement = new LuaElseIfStatementSyntax(statement.Condition);
                        elseIfStatement.Body.Statements.AddRange(statement.Body.Statements);
                        ifStatement.ElseIfStatements.Add(elseIfStatement);
                    }
                }
                else
                {
                    Contract.Assert(defaultBock_ == null);
                    defaultBock_ = (LuaBlockSyntax)section;
                }
            }

            if (ifStatement != null)
            {
                body.Statements.Add(ifStatement);
                if (defaultBock_ != null)
                {
                    LuaElseClauseSyntax elseClause = new LuaElseClauseSyntax();
                    elseClause.Body.Statements.AddRange(defaultBock_.Statements);
                    ifStatement.Else = elseClause;
                }
                headIfStatement_ = ifStatement;
            }
            else
            {
                if (defaultBock_ != null)
                {
                    body.Statements.AddRange(defaultBock_.Statements);
                }
            }
        }
Exemplo n.º 2
0
        private void CheckCtorsFunction(LuaBlockSyntax body)
        {
            bool hasInit  = initStatements_.Count > 0;
            bool hasCtors = ctors_.Count > 0;

            if (hasCtors)
            {
                if (hasInit)
                {
                    if (ctors_.Count == 1)
                    {
                        ctors_.First().Body.Statements.InsertRange(0, initStatements_);
                    }
                    else
                    {
                        var initIdentifier = LuaIdentifierNameSyntax.Init;
                        AddInitFunction(body, initIdentifier, GetInitFunction(), false);
                        foreach (var ctor in ctors_)
                        {
                            if (!ctor.IsInvokeThisCtor)
                            {
                                LuaInvocationExpressionSyntax invocationInit = new LuaInvocationExpressionSyntax(initIdentifier, LuaIdentifierNameSyntax.This);
                                ctor.Body.Statements.Insert(0, new LuaExpressionStatementSyntax(invocationInit));
                            }
                        }
                    }
                }

                if (ctors_.Count == 1)
                {
                    var ctor = ctors_.First();
                    if (ctor.Body.Statements.Count > 0)
                    {
                        AddInitFunction(body, LuaIdentifierNameSyntax.Ctor, ctor);
                    }
                }
                else
                {
                    LuaTableInitializerExpression ctrosTable = new LuaTableInitializerExpression();
                    int index = 1;
                    foreach (var ctor in ctors_)
                    {
                        string name = SpecailWord(Tokens.Ctor + index);
                        LuaIdentifierNameSyntax nameIdentifier = new LuaIdentifierNameSyntax(name);
                        AddInitFunction(body, nameIdentifier, ctor, false);
                        ctrosTable.Items.Add(new LuaSingleTableItemSyntax(nameIdentifier));
                        ++index;
                    }
                    AddResultTable(LuaIdentifierNameSyntax.Ctor, ctrosTable);
                }
            }
            else
            {
                if (hasInit)
                {
                    AddInitFunction(body, LuaIdentifierNameSyntax.Ctor, GetInitFunction());
                }
            }
        }
Exemplo n.º 3
0
 private void AddInitFunction(LuaBlockSyntax body, LuaIdentifierNameSyntax name, LuaFunctionExpressionSyntax initFunction, bool isAddItem = true)
 {
     local_.Variables.Add(name);
     body.Statements.Add(name.Assignment(initFunction));
     if (isAddItem)
     {
         AddResultTable(name);
     }
 }
Exemplo n.º 4
0
 private void CheckGenericUsingDeclares(LuaBlockSyntax body)
 {
     if (genericUsingDeclares_.Count > 0)
     {
         genericUsingDeclares_.Sort();
         foreach (var import in genericUsingDeclares_)
         {
             body.AddStatement(new LuaLocalVariableDeclaratorSyntax(import.NewName, import.InvocationExpression));
         }
     }
 }
        private void AddAllStatementsTo(LuaBlockSyntax body)
        {
            body.Statements.Add(local_);
            CheckStaticCtorFunction(body);
            CheckCtorsFunction(body);
            body.Statements.Add(methodList_);
            CheckAttributes();
            LuaReturnStatementSyntax returnStatement = new LuaReturnStatementSyntax(resultTable_);

            body.Statements.Add(returnStatement);
        }
Exemplo n.º 6
0
 private void AddStaticAssignmentNames(LuaBlockSyntax body)
 {
     if (staticAssignmentNames_.Count > 0)
     {
         var assignment = new LuaMultipleAssignmentExpressionSyntax();
         foreach (var identifierName in staticAssignmentNames_)
         {
             assignment.Lefts.Add(LuaIdentifierNameSyntax.This.MemberAccess(identifierName));
             assignment.Rights.Add(identifierName);
         }
         body.Statements.Add(assignment);
     }
 }
Exemplo n.º 7
0
        private void CheckStaticCtorFunction(LuaBlockSyntax body)
        {
            List <LuaStatementSyntax> statements = new List <LuaStatementSyntax>();

            statements.AddRange(staticInitStatements_);
            statements.AddRange(staticcCtorStatements_);
            if (statements.Count > 0)
            {
                LuaFunctionExpressionSyntax staticCtor = new LuaFunctionExpressionSyntax();
                staticCtor.AddParameter(LuaIdentifierNameSyntax.This);
                staticCtor.Body.Statements.AddRange(statements);
                AddStaticAssignmentNames(staticCtor.Body);
                AddInitFunction(body, LuaIdentifierNameSyntax.StaticCtor, staticCtor);
            }
        }
Exemplo n.º 8
0
 private void CheckGenericUsingDeclares(LuaBlockSyntax body)
 {
     if (genericUsingDeclares_.Count > 0)
     {
         genericUsingDeclares_.Sort();
         foreach (var import in genericUsingDeclares_)
         {
             LuaExpressionSyntax expression = import.InvocationExpression;
             if (import.IsFromGlobal)
             {
                 expression = LuaIdentifierNameSyntax.Global.MemberAccess(expression);
             }
             body.AddStatement(new LuaLocalVariableDeclaratorSyntax(import.NewName, expression));
         }
     }
 }
Exemplo n.º 9
0
 private void AddAllStatementsTo(LuaBlockSyntax body)
 {
     body.Statements.Add(local_);
     CheckStaticCtorFunction(body);
     CheckCtorsFunction(body);
     body.Statements.Add(methodList_);
     CheckAttributes();
     if (IsClassUsed)
     {
         body.Statements.Add(new LuaAssignmentExpressionSyntax(LuaIdentifierNameSyntax.Class, resultTable_).ToStatement());
         body.Statements.Add(new LuaReturnStatementSyntax(LuaIdentifierNameSyntax.Class));
     }
     else
     {
         body.Statements.Add(new LuaReturnStatementSyntax(resultTable_));
     }
 }
Exemplo n.º 10
0
 private void AddAllStatementsTo(LuaBlockSyntax body, LuaRenderer renderer)
 {
     body.Statements.Add(local_);
     body.Statements.AddRange(nestedTypeDeclarations_);
     CheckGenericUsingDeclares(body);
     CheckStaticCtorFunction(body);
     CheckCtorsFunction(body);
     body.Statements.Add(methodList_);
     CheckMetadata(renderer);
     if (IsClassUsed)
     {
         body.Statements.Add(LuaIdentifierNameSyntax.Class.Assignment(resultTable_));
         body.Statements.Add(new LuaReturnStatementSyntax(LuaIdentifierNameSyntax.Class));
     }
     else
     {
         body.Statements.Add(new LuaReturnStatementSyntax(resultTable_));
     }
 }
Exemplo n.º 11
0
        private void CheckStaticCtorFunction(LuaBlockSyntax body)
        {
            var statements = new List <LuaStatementSyntax>();

            statements.AddRange(staticInitStatements_);
            if (staticCtor_ != null)
            {
                statements.AddRange(staticCtor_.Function.Body.Statements);
            }
            if (statements.Count > 0 || IsForceStaticCtor)
            {
                var staticCtor = new LuaFunctionExpressionSyntax();
                staticCtor.AddParameter(LuaIdentifierNameSyntax.This);
                staticCtor.Body.Statements.AddRange(statements);
                AddStaticAssignmentNames(staticCtor.Body);
                if (staticCtor_ != null && staticCtor_.Document != null)
                {
                    body.AddStatement(staticCtor_.Document);
                }
                AddInitFunction(body, LuaIdentifierNameSyntax.StaticCtor, staticCtor);
            }
        }
Exemplo n.º 12
0
        private void CheckCtorsFunction(LuaBlockSyntax body)
        {
            bool hasInit  = initStatements_.Count > 0;
            bool hasCtors = ctors_.Count > 0;

            if (hasCtors)
            {
                if (hasInit)
                {
                    if (ctors_.Count == 1)
                    {
                        ctors_.First().Function.Body.Statements.InsertRange(0, initStatements_);
                    }
                    else
                    {
                        var init = LuaIdentifierNameSyntax.Init;
                        AddInitFunction(body, init, GetInitFunction(), false);
                        foreach (var ctor in ctors_)
                        {
                            if (!ctor.Function.IsInvokeThisCtor)
                            {
                                var invocationInit = new LuaInvocationExpressionSyntax(init, LuaIdentifierNameSyntax.This);
                                ctor.Function.Body.Statements.Insert(0, invocationInit);
                            }
                        }
                    }
                }

                if (ctors_.Count == 1)
                {
                    var ctor = ctors_.First();
                    if (ctor.Function.Body.Statements.Count > 0)
                    {
                        if (ctor.Document != null)
                        {
                            body.AddStatement(ctor.Document);
                        }
                        AddInitFunction(body, LuaIdentifierNameSyntax.Ctor, ctor.Function);
                    }
                }
                else
                {
                    LuaTableExpression ctrosTable = new LuaTableExpression();
                    int index = 1;
                    foreach (var ctor in ctors_)
                    {
                        if (ctor.Document != null)
                        {
                            body.AddStatement(ctor.Document);
                        }
                        LuaIdentifierNameSyntax nameIdentifier = GetCtorNameString(index);
                        AddInitFunction(body, nameIdentifier, ctor.Function, false);
                        ctrosTable.Add(nameIdentifier);
                        ++index;
                    }
                    AddResultTable(LuaIdentifierNameSyntax.Ctor, ctrosTable);
                }
            }
            else
            {
                if (hasInit)
                {
                    AddInitFunction(body, LuaIdentifierNameSyntax.Ctor, GetInitFunction());
                }
            }
        }
Exemplo n.º 13
0
 public LuaRepeatStatementSyntax(LuaExpressionSyntax condition, LuaBlockSyntax body = null)
 {
     Condition = condition ?? throw new ArgumentNullException(nameof(condition));
     Body      = body ?? new LuaBlockSyntax();
 }