Example #1
0
        public void Constructor1_Deny_Unrestricted()
        {
            CodeStatementCollection coll = new CodeStatementCollection(array);

            coll.CopyTo(array, 0);
            Assert.AreEqual(1, coll.Add(cs), "Add");
            Assert.AreSame(cs, coll[0], "this[int]");
            coll.AddRange(array);
            coll.AddRange(coll);
            Assert.IsTrue(coll.Contains(cs), "Contains");
            Assert.AreEqual(0, coll.IndexOf(cs), "IndexOf");
            coll.Insert(0, cs);
            coll.Remove(cs);
        }
Example #2
0
        // CodeStatementCollection
        public void CodeStatementCollectionSample()
        {
            //<Snippet1>
            //<Snippet2>
            // Creates an empty CodeStatementCollection.
            CodeStatementCollection collection = new CodeStatementCollection();

            //</Snippet2>

            //<Snippet3>
            // Adds a CodeStatement to the collection.
            collection.Add(new CodeCommentStatement("Test comment statement"));
            //</Snippet3>

            //<Snippet4>
            // Adds an array of CodeStatement objects to the collection.
            CodeStatement[] statements =
            {
                new CodeCommentStatement("Test comment statement"),
                new CodeCommentStatement("Test comment statement")
            };
            collection.AddRange(statements);

            // Adds a collection of CodeStatement objects to the collection.
            CodeStatement           testStatement        = new CodeCommentStatement("Test comment statement");
            CodeStatementCollection statementsCollection = new CodeStatementCollection();

            statementsCollection.Add(new CodeCommentStatement("Test comment statement"));
            statementsCollection.Add(new CodeCommentStatement("Test comment statement"));
            statementsCollection.Add(testStatement);

            collection.AddRange(statementsCollection);
            //</Snippet4>

            //<Snippet5>
            // Tests for the presence of a CodeStatement in the
            // collection, and retrieves its index if it is found.
            int itemIndex = -1;

            if (collection.Contains(testStatement))
            {
                itemIndex = collection.IndexOf(testStatement);
            }

            //</Snippet5>

            //<Snippet6>
            // Copies the contents of the collection beginning at index 0 to the specified CodeStatement array.
            // 'statements' is a CodeStatement array.
            CodeStatement[] statementArray = new CodeStatement[collection.Count];
            collection.CopyTo(statementArray, 0);
            //</Snippet6>

            //<Snippet7>
            // Retrieves the count of the items in the collection.
            int collectionCount = collection.Count;

            //</Snippet7>

            //<Snippet8>
            // Inserts a CodeStatement at index 0 of the collection.
            collection.Insert(0, new CodeCommentStatement("Test comment statement"));
            //</Snippet8>

            //<Snippet9>
            // Removes the specified CodeStatement from the collection.
            collection.Remove(testStatement);
            //</Snippet9>

            //<Snippet10>
            // Removes the CodeStatement at index 0.
            collection.RemoveAt(0);
            //</Snippet10>
            //</Snippet1>
        }
Example #3
0
        public override async System.Threading.Tasks.Task GCode_CodeDom_GenerateCode(CodeTypeDeclaration codeClass, CodeStatementCollection codeStatementCollection, CodeGenerateSystem.Base.LinkPinControl element, CodeGenerateSystem.Base.GenerateCodeContext_Method context)
        {
            if (element == mCtrlArrayElement || element == mCtrlArrayIndex || element == mCtrlDicKey || element == mCtrlDicValue)
            {
                return;
            }

            var param = CSParam as ForeachNodeConstructionParams;

            if (mCtrlArrayIn.HasLink)
            {
                var valuedGUID = EngineNS.Editor.Assist.GetValuedGUIDString(this.Id);
                var array      = "param_" + valuedGUID;
                var current    = "current_" + valuedGUID;

                if (!mCtrlArrayIn.GetLinkedObject(0, true).IsOnlyReturnValue)
                {
                    await mCtrlArrayIn.GetLinkedObject(0, true).GCode_CodeDom_GenerateCode(codeClass, codeStatementCollection, mCtrlArrayIn.GetLinkedPinControl(0, true), context);
                }
                var arrayExpression = mCtrlArrayIn.GetLinkedObject(0, true).GCode_CodeDom_GetValue(mCtrlArrayIn.GetLinkedPinControl(0, true), context);

                codeStatementCollection.Add(new CodeVariableDeclarationStatement("var", array, arrayExpression));

                // 调试的代码
                var debugCodes = CodeDomNode.BreakPoint.BeginMacrossDebugCodeStatments(codeStatementCollection);
                CodeDomNode.BreakPoint.BreakCodeStatement(codeClass, debugCodes, HostNodesContainer.GUID, Id);
                CodeDomNode.BreakPoint.EndMacrossDebugCodeStatements(codeStatementCollection, debugCodes);

                var idxStr = $"index_{valuedGUID}";

                string value = "";
                switch (param.LoopValueType)
                {
                case ForeachNodeConstructionParams.enLoopValueType.Dictionary:
                    value  = "        foreach (var " + current + " in " + array + ")\r\n";
                    value += "        {";
                    codeStatementCollection.Add(new CodeSnippetStatement(value));
                    break;

                case ForeachNodeConstructionParams.enLoopValueType.IntPtr:
                {
                    var countStr = $"count_{ valuedGUID}";
                    if (mCountDeclaration == null || !codeStatementCollection.Contains(mCountDeclaration))
                    {
                        var countType = typeof(Int32);
                        if (mCtrlArrayCount.HasLink)
                        {
                            mCountDeclaration = new CodeVariableDeclarationStatement(countType, countStr);
                            mCountDeclaration.InitExpression = new CodeGenerateSystem.CodeDom.CodeCastExpression(countType, mCtrlArrayCount.GetLinkedObject(0, true).GCode_CodeDom_GetValue(mCtrlArrayCount.GetLinkedPinControl(0, true), context));
                            codeStatementCollection.Add(mCountDeclaration);
                        }
                        else
                        {
                            mCountDeclaration = new CodeVariableDeclarationStatement(countType, countStr);
                            mCountDeclaration.InitExpression = new CodeGenerateSystem.CodeDom.CodeCastExpression(countType, new CodePrimitiveExpression(CountDefaultValue));
                            codeStatementCollection.Add(mCountDeclaration);
                        }
                    }

                    value  = $"        for (int {idxStr} = 0; {idxStr} < {countStr}; {idxStr}++)";
                    value += "        {";
                    codeStatementCollection.Add(new CodeSnippetStatement(value));
                    codeStatementCollection.Add(new CodeVariableDeclarationStatement(ElementType, current, new CodeVariableReferenceExpression($"{array}[{idxStr}]")));
                }
                break;

                case ForeachNodeConstructionParams.enLoopValueType.Array:
                    value  = $"        for (int {idxStr} = 0; {idxStr} < {array}.Length; {idxStr}++)";
                    value += "        {";
                    codeStatementCollection.Add(new CodeSnippetStatement(value));
                    codeStatementCollection.Add(new CodeVariableDeclarationStatement(ElementType, current, new CodeVariableReferenceExpression($"{array}[{idxStr}]")));
                    break;

                case ForeachNodeConstructionParams.enLoopValueType.List:
                    value  = $"        for (int {idxStr} = 0; {idxStr} < {array}.Count; {idxStr}++)";
                    value += "        {";
                    codeStatementCollection.Add(new CodeSnippetStatement(value));
                    codeStatementCollection.Add(new CodeVariableDeclarationStatement(ElementType, current, new CodeVariableReferenceExpression($"{array}[{idxStr}]")));
                    break;
                }


                if (mCtrlMethodLink_LoopBody.HasLink)
                {
                    //if (!mCtrlMethodLink_LoopBody.GetLinkedObject(0, false).IsOnlyReturnValue)
                    await mCtrlMethodLink_LoopBody.GetLinkedObject(0, false).GCode_CodeDom_GenerateCode(codeClass, codeStatementCollection, mCtrlMethodLink_LoopBody.GetLinkedPinControl(0, false), context);
                }

                codeStatementCollection.Add(new CodeSnippetStatement("        }"));

                if (context.GenerateNext)
                {
                    if (mCtrlMethodLink_Completed.HasLink)
                    {
                        //if (!mCtrlMethodLink_Completed.GetLinkedObject(0, false).IsOnlyReturnValue)
                        await mCtrlMethodLink_Completed.GetLinkedObject(0, false).GCode_CodeDom_GenerateCode(codeClass, codeStatementCollection, mCtrlMethodLink_Completed.GetLinkedPinControl(0, false), context);
                    }
                }
            }
        }
Example #4
0
        //System.CodeDom.CodeVariableDeclarationStatement mVecVariableDeclaration = new System.CodeDom.CodeVariableDeclarationStatement();
        public override async System.Threading.Tasks.Task GCode_CodeDom_GenerateCode(CodeTypeDeclaration codeClass, CodeStatementCollection codeStatementCollection, CodeGenerateSystem.Base.LinkPinControl element, CodeGenerateSystem.Base.GenerateCodeContext_Method context)
        {
            var strValueName = GCode_GetValueName(null, context);

            if (!context.Method.Statements.Contains(mVarDec))
            {
                mVarDec = new CodeAssignStatement(new CodeSnippetExpression(mValueType.FullName + " " + strValueName), CodeGenerateSystem.Program.GetDefaultValueExpressionFromType(mValueType));//, paramCodeName, CodeGenerateSystem.Program.GetDefaultValueExpressionFromType(ParamType));
                context.Method.Statements.Insert(0, mVarDec);
            }
            if (mCtrlvalue_VectorIn.HasLink)
            {
                if (!mCtrlvalue_VectorIn.GetLinkedObject(0, true).IsOnlyReturnValue)
                {
                    await mCtrlvalue_VectorIn.GetLinkedObject(0, true).GCode_CodeDom_GenerateCode(codeClass, codeStatementCollection, mCtrlvalue_VectorIn.GetLinkedPinControl(0, true), context);
                }


                if (!codeStatementCollection.Contains(mAssignCode))
                {
                    mAssignCode = new CodeAssignStatement(new System.CodeDom.CodeVariableReferenceExpression(strValueName), mCtrlvalue_VectorIn.GetLinkedObject(0, true).GCode_CodeDom_GetValue(mCtrlvalue_VectorIn.GetLinkedPinControl(0, true), context));
                    codeStatementCollection.Add(mAssignCode);
                }
            }
            else
            {
                if (!codeStatementCollection.Contains(mAssignCode))
                {
                    mAssignCode      = new CodeAssignStatement();
                    mAssignCode.Left = new System.CodeDom.CodeVariableReferenceExpression(strValueName);
                    var paramExp = new System.CodeDom.CodeExpression[mLinkInDic.Count];
                    var param    = CSParam as AixConstructionParams;
                    if (param != null)
                    {
                        for (int i = 0; i < mLinkInDic.Count; i++)
                        {
                            paramExp[i] = new System.CodeDom.CodePrimitiveExpression(param.Value[i]);
                        }
                    }
                    mAssignCode.Right = new CodeObjectCreateExpression(mValueType, paramExp);
                    codeStatementCollection.Add(mAssignCode);
                }
            }

            foreach (var data in mLinkInDic)
            {
                var linkOI = data.Element;
                if (linkOI.HasLink)
                {
                    if (!linkOI.GetLinkedObject(0, true).IsOnlyReturnValue)
                    {
                        await linkOI.GetLinkedObject(0, true).GCode_CodeDom_GenerateCode(codeClass, codeStatementCollection, linkOI.GetLinkedPinControl(0, true), context);
                    }

                    var fieldRef = new System.CodeDom.CodeFieldReferenceExpression();
                    fieldRef.TargetObject = new CodeVariableReferenceExpression(strValueName);
                    fieldRef.FieldName    = data.KeyName;
                    var statValAss = new System.CodeDom.CodeAssignStatement();
                    statValAss.Left  = fieldRef;
                    statValAss.Right = new CodeGenerateSystem.CodeDom.CodeCastExpression(typeof(float), linkOI.GetLinkedObject(0, true).GCode_CodeDom_GetValue(linkOI.GetLinkedPinControl(0, true), context));
                    codeStatementCollection.Add(statValAss);
                }
            }
        }