private void AddInitFiled(LuaIdentifierNameSyntax name, LuaExpressionSyntax value)
        {
            LuaMemberAccessExpressionSyntax memberAccess = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, name);
            LuaAssignmentExpressionSyntax   assignment   = new LuaAssignmentExpressionSyntax(memberAccess, value);

            initStatements_.Add(new LuaExpressionStatementSyntax(assignment));
        }
        public void AddMethod(LuaIdentifierNameSyntax name, LuaFunctionExpressionSyntax method, bool isPrivate, LuaDocumentStatement document = null, bool isMoreThanLocalVariables = false)
        {
            if (document != null && document.HasIgnoreAttribute)
            {
                return;
            }

            if (isMoreThanLocalVariables)
            {
                CheckTooManyVariables(true);
                if (document != null && !document.IsEmpty)
                {
                    methodList_.Statements.Add(document);
                }
                var left = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.MorenManyLocalVarTempTable, name);
                methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(left, method));
                AddResultTable(name, left);
            }
            else
            {
                local_.Variables.Add(name);
                if (document != null && !document.IsEmpty)
                {
                    methodList_.Statements.Add(document);
                }
                methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(name, method));
                if (!isPrivate)
                {
                    AddResultTable(name);
                }
            }
        }
        private void CheckMetadataMoreThanUpvalues()
        {
            const int kMaxUpvalueCount = kUpvaluesMaxCount - 2;
            const int kNameIndex       = 2;

            if (metaMethods_ != null)
            {
                int upvalueCount = 0;
                foreach (LuaSingleTableItemSyntax item in metaMethods_.Items)
                {
                    var table      = (LuaTableExpression)item.Expression;
                    var expression = (LuaSingleTableItemSyntax)table.Items[kNameIndex];
                    if (expression.Expression is LuaIdentifierNameSyntax name)
                    {
                        if (upvalueCount >= kMaxUpvalueCount)
                        {
                            CheckTooManyVariables(false);
                            var left = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.MorenManyLocalVarTempTable, name);
                            methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(left, name));
                            table.Items[kNameIndex] = new LuaSingleTableItemSyntax(left);
                        }
                        else
                        {
                            ++upvalueCount;
                        }
                    }
                }
            }
        }
        internal void AddBaseTypes(IEnumerable <LuaExpressionSyntax> baseTypes, LuaSpeaicalGenericType genericArgument, List <LuaIdentifierNameSyntax> baseCopyFields)
        {
            bool hasLazyGenericArgument = false;

            if (genericArgument != null)
            {
                if (genericArgument.IsLazy)
                {
                    hasLazyGenericArgument = true;
                }
                else
                {
                    AddResultTable(genericArgument.Name, genericArgument.Value);
                }
            }

            bool hasBaseCopyField = baseCopyFields != null && baseCopyFields.Count > 0;
            LuaFunctionExpressionSyntax functionExpression = new LuaFunctionExpressionSyntax();

            functionExpression.AddParameter(LuaIdentifierNameSyntax.Global);
            if (hasLazyGenericArgument || hasBaseCopyField)
            {
                functionExpression.AddParameter(LuaIdentifierNameSyntax.This);
            }

            if (hasLazyGenericArgument)
            {
                var assignment = new LuaAssignmentExpressionSyntax(new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, genericArgument.Name), genericArgument.Value);
                functionExpression.AddStatement(assignment);
            }

            LuaTableExpression table = new LuaTableExpression();

            if (hasBaseCopyField)
            {
                var baseIdentifier = LuaIdentifierNameSyntax.Base;
                functionExpression.AddStatement(new LuaLocalVariableDeclaratorSyntax(baseIdentifier, baseTypes.First()));
                foreach (var field in baseCopyFields)
                {
                    var left  = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, field);
                    var right = new LuaMemberAccessExpressionSyntax(baseIdentifier, field);
                    functionExpression.AddStatement(new LuaAssignmentExpressionSyntax(left, right));

                    table.Items.Add(new LuaSingleTableItemSyntax(baseIdentifier));
                    table.Items.AddRange(baseTypes.Skip(1).Select(i => new LuaSingleTableItemSyntax(i)));
                }
            }
            else
            {
                table.Items.AddRange(baseTypes.Select(i => new LuaSingleTableItemSyntax(i)));
            }

            functionExpression.AddStatement(new LuaReturnStatementSyntax(table));
            AddResultTable(LuaIdentifierNameSyntax.Inherits, functionExpression);
        }
        private void CheckUsingDeclares()
        {
            var imports = usingDeclares_.Where(i => !i.IsFromCode).ToList();

            if (imports.Count > 0)
            {
                imports.Sort((x, y) => x.Prefix.CompareTo(y.Prefix));
                foreach (var import in imports)
                {
                    AddImport(new LuaIdentifierNameSyntax(import.NewPrefix), new LuaIdentifierNameSyntax(import.Prefix));
                }
            }

            var usingDeclares = usingDeclares_.Where(i => i.IsFromCode).ToList();

            if (usingDeclares.Count > 0)
            {
                usingDeclares.Sort((x, y) => x.Prefix.CompareTo(y.Prefix));
                foreach (var usingDeclare in usingDeclares)
                {
                    AddImport(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), null);
                }

                var global = LuaIdentifierNameSyntax.Global;
                LuaFunctionExpressionSyntax functionExpression = new LuaFunctionExpressionSyntax();
                functionExpression.AddParameter(global);
                foreach (var usingDeclare in usingDeclares)
                {
                    if (usingDeclare.Prefix != usingDeclare.NewPrefix)
                    {
                        LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), new LuaIdentifierNameSyntax(usingDeclare.Prefix));
                        functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                    }
                    else
                    {
                        LuaMemberAccessExpressionSyntax right      = new LuaMemberAccessExpressionSyntax(global, new LuaIdentifierNameSyntax(usingDeclare.Prefix));
                        LuaAssignmentExpressionSyntax   assignment = new LuaAssignmentExpressionSyntax(new LuaIdentifierNameSyntax(usingDeclare.NewPrefix), right);
                        functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                    }
                }

                LuaInvocationExpressionSyntax invocationExpression = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.UsingDeclare, functionExpression);
                importAreaStatements.Statements.Add(new LuaExpressionStatementSyntax(invocationExpression));
            }

            int index = Statements.FindIndex(i => i is LuaNamespaceDeclarationSyntax);

            if (index != -1)
            {
                Statements.Insert(index, importAreaStatements);
            }
        }
Exemplo n.º 6
0
        protected void UpdateIdentifiers(LuaIdentifierNameSyntax name, LuaIdentifierNameSyntax target, LuaIdentifierNameSyntax memberName, LuaIdentifierNameSyntax parameter = null)
        {
            LuaMemberAccessExpressionSyntax memberAccess = new LuaMemberAccessExpressionSyntax(target, memberName);
            LuaInvocationExpressionSyntax   invoke       = new LuaInvocationExpressionSyntax(memberAccess);

            invoke.AddArgument(new LuaStringLiteralExpressionSyntax(name));
            invoke.AddArgument(function_);
            if (parameter != null)
            {
                function_.AddParameter(parameter);
            }
            Statement = new LuaExpressionStatementSyntax(invoke);
        }
 private void AddStaticAssignmentNames(LuaBlockSyntax body)
 {
     if (staticAssignmentNames_.Count > 0)
     {
         LuaMultipleAssignmentExpressionSyntax assignment = new LuaMultipleAssignmentExpressionSyntax();
         foreach (var identifierName in staticAssignmentNames_)
         {
             LuaMemberAccessExpressionSyntax memberAccess = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, identifierName);
             assignment.Lefts.Add(memberAccess);
             assignment.Rights.Add(identifierName);
         }
         body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
     }
 }
Exemplo n.º 8
0
        internal void AddBaseTypes(IEnumerable <LuaExpressionSyntax> baseTypes)
        {
            var global = LuaIdentifierNameSyntax.Global;
            LuaTableInitializerExpression table = new LuaTableInitializerExpression();

            foreach (var baseType in baseTypes)
            {
                LuaMemberAccessExpressionSyntax memberAccess = new LuaMemberAccessExpressionSyntax(global, baseType);
                table.Items.Add(new LuaSingleTableItemSyntax(memberAccess));
            }
            LuaFunctionExpressionSyntax functionExpression = new LuaFunctionExpressionSyntax();

            functionExpression.AddParameter(global);
            functionExpression.AddStatement(new LuaReturnStatementSyntax(table));
            AddResultTable(LuaIdentifierNameSyntax.Inherits, functionExpression);
        }
        private void CheckMetatableMoreThanUpvalues(LuaTableExpression table, int nameIndex, ref int upvalueCount)
        {
            int kMaxUpvalueCount = kUpvaluesMaxCount - 2 - (IsClassUsed ? 1 : 0);
            var expression       = table.GetSingleExpression(nameIndex);

            if (expression is LuaIdentifierNameSyntax name)
            {
                if (upvalueCount >= kMaxUpvalueCount)
                {
                    CheckTooManyVariables(false);
                    var left = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.MorenManyLocalVarTempTable, name);
                    methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(left, name));
                    table.Items[nameIndex] = new LuaSingleTableItemSyntax(left);
                }
                else
                {
                    ++upvalueCount;
                }
            }
        }
        public void AddMethod(LuaIdentifierNameSyntax name, LuaFunctionExpressionSyntax method, bool isPrivate, LuaDocumentStatement document = null, bool isMoreThanLocalVariables = false)
        {
            if (document != null && document.HasIgnoreAttribute)
            {
                return;
            }

            if (isMoreThanLocalVariables)
            {
                if (!hasTooManyLocalVariables_)
                {
                    methodList_.Statements.Add(new LuaShortCommentStatement(" too many local variables (limit is 200)"));
                    methodList_.Statements.Add(new LuaLocalVariableDeclaratorSyntax(LuaIdentifierNameSyntax.MorenManyLocalVarTempTable, LuaTableExpression.Empty));
                    hasTooManyLocalVariables_ = true;
                }
                if (document != null && !document.IsEmpty)
                {
                    methodList_.Statements.Add(document);
                }
                var left = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.MorenManyLocalVarTempTable, name);
                methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(left, method));
                AddResultTable(name, left);
            }
            else
            {
                local_.Variables.Add(name);
                if (document != null && !document.IsEmpty)
                {
                    methodList_.Statements.Add(document);
                }
                methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(name, method));
                if (!isPrivate)
                {
                    AddResultTable(name);
                }
            }
        }
        internal void AddBaseTypes(IEnumerable <LuaExpressionSyntax> baseTypes, LuaSpeaicalGenericType genericArgument = null)
        {
            bool hasLazyGenericArgument = false;

            if (genericArgument != null)
            {
                if (genericArgument.IsLazy)
                {
                    hasLazyGenericArgument = true;
                }
                else
                {
                    AddResultTable(genericArgument.Name, genericArgument.Value);
                }
            }

            var global = LuaIdentifierNameSyntax.Global;
            LuaTableInitializerExpression table = new LuaTableInitializerExpression();

            foreach (var baseType in baseTypes)
            {
                LuaMemberAccessExpressionSyntax memberAccess = new LuaMemberAccessExpressionSyntax(global, baseType);
                table.Items.Add(new LuaSingleTableItemSyntax(memberAccess));
            }
            LuaFunctionExpressionSyntax functionExpression = new LuaFunctionExpressionSyntax();

            functionExpression.AddParameter(global);
            if (hasLazyGenericArgument)
            {
                functionExpression.AddParameter(LuaIdentifierNameSyntax.This);
                var assignment = new LuaAssignmentExpressionSyntax(new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, genericArgument.Name), genericArgument.Value);
                functionExpression.AddStatement(assignment);
            }
            functionExpression.AddStatement(new LuaReturnStatementSyntax(table));
            AddResultTable(LuaIdentifierNameSyntax.Inherits, functionExpression);
        }
Exemplo n.º 12
0
        private (LuaPropertyOrEventIdentifierNameSyntax, LuaPropertyOrEventIdentifierNameSyntax) AddPropertyOrEvent(bool isProperty, LuaIdentifierNameSyntax name, LuaIdentifierNameSyntax innerName, LuaExpressionSyntax value, bool isImmutable, bool isStatic, bool isPrivate, LuaExpressionSyntax typeExpression, List <LuaStatementSyntax> statements)
        {
            LuaPropertyOrEventIdentifierNameSyntax get, set;

            if (isProperty)
            {
                get = new LuaPropertyOrEventIdentifierNameSyntax(true, true, name);
                set = new LuaPropertyOrEventIdentifierNameSyntax(true, false, name);
            }
            else
            {
                get = new LuaPropertyOrEventIdentifierNameSyntax(false, true, name);
                set = new LuaPropertyOrEventIdentifierNameSyntax(false, false, name);
            }

            local_.Variables.Add(get);
            local_.Variables.Add(set);
            if (!isStatic)
            {
                LuaIdentifierNameSyntax initMethodIdentifier     = isProperty ? LuaIdentifierNameSyntax.Property : LuaIdentifierNameSyntax.Event;
                LuaMultipleAssignmentExpressionSyntax assignment = new LuaMultipleAssignmentExpressionSyntax();
                assignment.Lefts.Add(get);
                assignment.Lefts.Add(set);
                LuaInvocationExpressionSyntax invocation = new LuaInvocationExpressionSyntax(initMethodIdentifier);
                invocation.AddArgument(new LuaStringLiteralExpressionSyntax(innerName));
                assignment.Rights.Add(invocation);
                methodList_.Statements.Add(assignment);
            }
            else
            {
                var memberAccess = new LuaMemberAccessExpressionSyntax(typeExpression, innerName);
                var getFunction  = new LuaFunctionExpressionSyntax();
                var setFunction  = new LuaFunctionExpressionSyntax();
                if (isProperty)
                {
                    getFunction.AddStatement(new LuaReturnStatementSyntax(memberAccess));
                    setFunction.AddParameter(LuaIdentifierNameSyntax.Value);
                    var assignment = new LuaAssignmentExpressionSyntax(memberAccess, LuaIdentifierNameSyntax.Value);
                    setFunction.AddStatement(assignment);
                }
                else
                {
                    getFunction.AddParameter(LuaIdentifierNameSyntax.Value);
                    var getAssignment = new LuaAssignmentExpressionSyntax(memberAccess, new LuaBinaryExpressionSyntax(memberAccess, Tokens.Plus, LuaIdentifierNameSyntax.Value));
                    getFunction.AddStatement(getAssignment);
                    setFunction.AddParameter(LuaIdentifierNameSyntax.Value);
                    var setAssignment = new LuaAssignmentExpressionSyntax(memberAccess, new LuaBinaryExpressionSyntax(memberAccess, Tokens.Sub, LuaIdentifierNameSyntax.Value));
                    setFunction.AddStatement(setAssignment);
                }
                methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(get, getFunction));
                methodList_.Statements.Add(new LuaAssignmentExpressionSyntax(set, setFunction));
            }

            if (value != null)
            {
                if (isStatic)
                {
                    if (isImmutable)
                    {
                        AddResultTable(innerName, value);
                    }
                    else
                    {
                        if (statements != null)
                        {
                            staticInitStatements_.AddRange(statements);
                        }
                        LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, innerName), value);
                        staticInitStatements_.Add(new LuaExpressionStatementSyntax(assignment));
                    }
                }
                else
                {
                    if (isImmutable)
                    {
                        AddResultTable(innerName, value);
                    }
                    else
                    {
                        if (statements != null)
                        {
                            initStatements_.AddRange(statements);
                        }
                        AddInitFiled(innerName, value);
                    }
                }
            }

            if (!isPrivate)
            {
                AddResultTable(get);
                AddResultTable(set);
            }

            return(get, set);
        }
Exemplo n.º 13
0
        public void AddField(LuaIdentifierNameSyntax name, LuaExpressionSyntax value, bool isImmutable, bool isStatic, bool isPrivate, bool isReadOnly, List <LuaStatementSyntax> statements)
        {
            if (isStatic)
            {
                if (isPrivate)
                {
                    local_.Variables.Add(name);
                    if (value != null)
                    {
                        LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(name, value);
                        if (isImmutable)
                        {
                            methodList_.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                        }
                        else
                        {
                            if (statements != null)
                            {
                                staticInitStatements_.AddRange(statements);
                            }
                            staticInitStatements_.Add(new LuaExpressionStatementSyntax(assignment));
                        }
                    }
                }
                else
                {
                    if (isReadOnly)
                    {
                        local_.Variables.Add(name);
                        if (value != null)
                        {
                            if (isImmutable)
                            {
                                LuaAssignmentExpressionSyntax assignment = new LuaAssignmentExpressionSyntax(name, value);
                                methodList_.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                                AddResultTable(name);
                            }
                            else
                            {
                                if (statements != null)
                                {
                                    staticInitStatements_.AddRange(statements);
                                }
                                var assignment = new LuaAssignmentExpressionSyntax(name, value);
                                staticInitStatements_.Add(new LuaExpressionStatementSyntax(assignment));

                                var memberAccess = new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, name);
                                staticInitStatements_.Add(new LuaAssignmentExpressionSyntax(memberAccess, name));
                            }
                        }
                    }
                    else
                    {
                        if (value != null)
                        {
                            if (isImmutable)
                            {
                                AddResultTable(name, value);
                            }
                            else
                            {
                                if (statements != null)
                                {
                                    staticInitStatements_.AddRange(statements);
                                }
                                var assignment = new LuaAssignmentExpressionSyntax(new LuaMemberAccessExpressionSyntax(LuaIdentifierNameSyntax.This, name), value);
                                staticInitStatements_.Add(new LuaExpressionStatementSyntax(assignment));
                            }
                        }
                    }
                }
            }
            else
            {
                if (value != null)
                {
                    if (isImmutable)
                    {
                        AddResultTable(name, value);
                    }
                    else
                    {
                        if (statements != null)
                        {
                            initStatements_.AddRange(statements);
                        }
                        AddInitFiled(name, value);
                    }
                }
            }
        }
        private void CheckUsingDeclares()
        {
            var imports = UsingDeclares.Where(i => !i.IsFromCode).ToList();

            if (imports.Count > 0)
            {
                imports.Sort();
                foreach (var import in imports)
                {
                    AddImport(import.NewPrefix, import.Prefix);
                }
            }

            var genericImports = GenericUsingDeclares.Where(i => !i.IsFromCode).ToList();

            if (genericImports.Count > 0)
            {
                genericImports.Sort();
                foreach (var import in genericImports)
                {
                    AddImport(import.NewName, import.InvocationExpression);
                }
            }

            var usingDeclares   = UsingDeclares.Where(i => i.IsFromCode).ToList();
            var genericDeclares = GenericUsingDeclares.Where(i => i.IsFromCode).ToList();

            if (usingDeclares.Count > 0 || genericDeclares.Count > 0 || genericImports.Count > 0)
            {
                usingDeclares.Sort();
                genericDeclares.Sort();
                genericImports.Sort();

                foreach (var usingDeclare in usingDeclares)
                {
                    AddImport(usingDeclare.NewPrefix, null);
                }

                foreach (var usingDeclare in genericDeclares)
                {
                    AddImport(usingDeclare.NewName, null);
                }

                foreach (var import in genericImports)
                {
                    AddImport(import.NewName, null);
                }

                var global             = LuaIdentifierNameSyntax.Global;
                var functionExpression = new LuaFunctionExpressionSyntax();
                functionExpression.AddParameter(global);
                foreach (var usingDeclare in usingDeclares)
                {
                    if (usingDeclare.Prefix != usingDeclare.NewPrefix)
                    {
                        var assignment = new LuaAssignmentExpressionSyntax(usingDeclare.NewPrefix, usingDeclare.Prefix);
                        functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                    }
                    else
                    {
                        var right      = new LuaMemberAccessExpressionSyntax(global, usingDeclare.Prefix);
                        var assignment = new LuaAssignmentExpressionSyntax(usingDeclare.NewPrefix, right);
                        functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                    }
                }

                foreach (var usingDeclare in genericDeclares)
                {
                    var assignment = new LuaAssignmentExpressionSyntax(usingDeclare.NewName, usingDeclare.InvocationExpression);
                    functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                }

                foreach (var genericImport in genericImports)
                {
                    var assignment = new LuaAssignmentExpressionSyntax(genericImport.NewName, genericImport.InvocationExpression);
                    functionExpression.Body.Statements.Add(new LuaExpressionStatementSyntax(assignment));
                }

                var invocationExpression = new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.Import, functionExpression);
                importAreaStatements.Statements.Add(new LuaExpressionStatementSyntax(invocationExpression));
            }

            int index = Statements.FindIndex(i => i is LuaNamespaceDeclarationSyntax);

            if (index != -1)
            {
                Statements.Insert(index, importAreaStatements);
            }
        }