示例#1
0
        public void FuncBlock_Completed_CancelsFunc()
        {
            Test.Async(async() =>
            {
                var tcs   = new TaskCompletionSource();
                var block = new FuncBlock <int>(async send =>
                {
                    try
                    {
                        while (true)
                        {
                            await send(13);
                            await TaskShim.Delay(100);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        tcs.SetResult();
                    }
                });

                var resultTask = GetBlockOutputsAsync(block);
                block.Complete();
                await resultTask;
                await block.Completion;
                await tcs.Task;
            });
        }
示例#2
0
        private void OnBreakpointPause(object sender, BreakpointPauseEventArgs e)
        {
            items.Clear();
            FuncBlock fblock = BreakPointManager.GetFBlock(e.Address);
            void *    rbp    = SimulateDllModel.GetRBP();
            int       laddr  = (int)rbp + 0x4;
            int       paddr  = (int)rbp - 0x8;

            if (fblock != null)
            {
                IList <FuncBlock_Assignment> localvars  = fblock.LocalVars;
                IList <FuncBlock_Assignment> parameters = fblock.Parameters;
                LocalVarElement_ForFuncBlock lvelement  = null;
                foreach (FuncBlock_Assignment localvar in localvars)
                {
                    lvelement = new LocalVarElement_ForFuncBlock(localvar);
                    GetValue(lvelement, localvar, laddr);
                    items.Add(lvelement);
                    laddr -= localvar.Sizeof;
                }
                foreach (FuncBlock_Assignment parameter in parameters)
                {
                    lvelement = new LocalVarElement_ForFuncBlock(parameter);
                    GetValue(lvelement, parameter, paddr);
                    items.Add(lvelement);
                    paddr += parameter.Sizeof;
                }
            }
            PropertyChanged(this, new PropertyChangedEventArgs("Items"));
        }
示例#3
0
        public void FuncBlock_Faulted_CancelsFunc()
        {
            Test.Async(async() =>
            {
                var tcs   = new TaskCompletionSource();
                var block = new FuncBlock <int>(async send =>
                {
                    try
                    {
                        while (true)
                        {
                            await send(13);
                            await TaskShim.Delay(100);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        tcs.SetResult();
                    }
                });

                ((IDataflowBlock)block).Fault(new NotImplementedException());
                await tcs.Task;
            });
        }
示例#4
0
 private void GetBPAddrRange(FuncBlock fblock)
 {
     if (fblock.IsBreakpoint)
     {
         bpaddrmin = Math.Max(bpaddrmin, fblock.BPAddress);
         bpaddrmax = Math.Max(bpaddrmax, fblock.BPAddress);
     }
     if (fblock is FuncBlock_ForHeader)
     {
         FuncBlock_ForHeader fblockfh = (FuncBlock_ForHeader)fblock;
         GetBPAddrRange(fblockfh.Start);
         GetBPAddrRange(fblockfh.Cond);
         GetBPAddrRange(fblockfh.Next);
     }
     if (fblock is FuncBlock_WhileHeader)
     {
         FuncBlock_WhileHeader fblockwh = (FuncBlock_WhileHeader)fblock;
         GetBPAddrRange(fblockwh.Cond);
     }
     if (fblock is FuncBlock_IfHeader)
     {
         FuncBlock_IfHeader fblockih = (FuncBlock_IfHeader)fblock;
         GetBPAddrRange(fblockih.Cond);
     }
     if (fblock is FuncBlock_WhileEnd)
     {
         FuncBlock_WhileEnd fblockwe = (FuncBlock_WhileEnd)fblock;
         GetBPAddrRange(fblockwe.Cond);
     }
     foreach (FuncBlock child in fblock.Childrens)
     {
         GetBPAddrRange(child);
     }
 }
示例#5
0
 private void InitBP(FuncBlock fblock)
 {
     fblock.IsBreakpoint = false;
     if (fblock is FuncBlock_ForHeader)
     {
         FuncBlock_ForHeader fblockfh = (FuncBlock_ForHeader)fblock;
         InitBP(fblockfh.Start);
         InitBP(fblockfh.Cond);
         InitBP(fblockfh.Next);
     }
     if (fblock is FuncBlock_WhileHeader)
     {
         FuncBlock_WhileHeader fblockwh = (FuncBlock_WhileHeader)fblock;
         InitBP(fblockwh.Cond);
     }
     if (fblock is FuncBlock_IfHeader)
     {
         FuncBlock_IfHeader fblockih = (FuncBlock_IfHeader)fblock;
         InitBP(fblockih.Cond);
     }
     if (fblock is FuncBlock_WhileEnd)
     {
         FuncBlock_WhileEnd fblockwe = (FuncBlock_WhileEnd)fblock;
         InitBP(fblockwe.Cond);
     }
     foreach (FuncBlock child in fblock.Childrens)
     {
         InitBP(child);
     }
 }
        private BackTraceElement GetElement(int bpaddr)
        {
            BaseViewModel bvmodel = BreakPointManager.GetBVModel(bpaddr);

            if (bvmodel != null)
            {
                LadderNetworkViewModel lnvmodel = ifacade.ProjectModel.GetNetwork(bvmodel);
                if (lnvmodel == null)
                {
                    return(null);
                }
                LadderDiagramViewModel ldvmodel = lnvmodel.LDVModel;
                SimuBrpoElement        brpo     = new SimuBrpoElement();
                brpo.LDVModel = ldvmodel;
                brpo.LNVModel = lnvmodel;
                brpo.BVModel  = bvmodel;
                return(new BackTraceElement(BackTraceType.Diagram, brpo));
            }
            FuncBlock fblock = BreakPointManager.GetFBlock(bpaddr);

            if (fblock != null)
            {
                FuncBlockViewModel fbvmodel = ifacade.ProjectModel.GetFuncBlock(fblock);
                if (fbvmodel == null)
                {
                    return(null);
                }
                FuncBrpoElement brpo = new FuncBrpoElement();
                brpo.FBVModel = fbvmodel;
                brpo.FBlock   = fblock;
                return(new BackTraceElement(BackTraceType.FuncBlock, brpo));
            }
            return(new BackTraceElement(BackTraceType.External));
        }
示例#7
0
        public void FuncBlock_Canceled_CancelsFunc()
        {
            Test.Async(async() =>
            {
                var tcs   = new TaskCompletionSource();
                var cts   = new CancellationTokenSource();
                var block = new FuncBlock <int>(async send =>
                {
                    try
                    {
                        while (true)
                        {
                            await send(13);
                            await TaskShim.Delay(100);
                        }
                    }
                    catch (OperationCanceledException)
                    {
                        tcs.SetResult();
                    }
                },
                                                new DataflowBlockOptions
                {
                    CancellationToken = cts.Token,
                });

                var resultTask = GetBlockOutputsAsync(block);
                cts.Cancel();
                await resultTask;
                await tcs.Task;
            });
        }
示例#8
0
 /// <summary>
 /// 获得一个函数块程序中所有的注释块
 /// </summary>
 /// <param name="ldvmodel">梯形图程序</param>
 static private void GetComments(FuncBlock fblock, List <FuncBlock_Comment> comments)
 {
     if (fblock is FuncBlock_Comment)
     {
         comments.Add((FuncBlock_Comment)fblock);
     }
     foreach (FuncBlock child in fblock.Childrens)
     {
         GetComments(child, comments);
     }
 }
示例#9
0
        internal static LineCollection ScanLines(string[] lines, out CodeBlock[] userFunctions)
        {
            LineCollection allLines = new LineCollection();
            List <uint>    funLines = new List <uint>();

            for (uint lineNumber = 0; lineNumber < lines.Length; ++lineNumber)
            {
                Line current = new Line(lineNumber + 1, lines[lineNumber]); // Tag all lines with its line number (index + 1)

                if (string.IsNullOrEmpty(current.Text) || current.Text[0] == ';')
                {
                    continue;
                }
                if (current.Name[current.Name.Length - 1] == '$' || current.Name[current.Name.Length - 1] == ']')
                {
                    current.Text = "LET " + current.Text; // add the word LET if it's an equality, but use the original name as visible name
                }
                else if (current.Name.EqualsIgnoreCase("FUNCTION"))
                {
                    funLines.Add(current.LineNumber);
                }
                else
                {
                    current.VisibleName = current.VisibleName.ToUpper();
                }

                while (current.Text[current.Text.Length - 1] == '_')   // line continuation
                {
                    lineNumber++;
                    if (lineNumber >= lines.Length)
                    {
                        throw new EndOfCodeException("line continuation character '_' cannot end script");
                    }
                    current = new Line(current.LineNumber, current.Text.Remove(current.Text.LastIndexOf('_')) + lines[lineNumber].Trim());
                }

                allLines.Add(current);
            }

            List <CodeBlock> userFuncs = new List <CodeBlock>();

            foreach (uint funcLine in funLines)
            {
                FuncBlock func = new FuncBlock(allLines.IndexOf(funcLine), allLines);
                userFuncs.Add(func);
                allLines.Remove(func.Header);
                allLines.Remove(func.Body);
                allLines.Remove(func.Footer);
            }
            userFunctions = userFuncs.ToArray();
            return(allLines);
        }
示例#10
0
        public void FuncBlock_FuncFaults_Faults()
        {
            Test.Async(async() =>
            {
                var block = new FuncBlock <int>(async send =>
                {
                    await send(13);
                    throw new NotImplementedException();
                });

                await GetBlockOutputsAsync(block);
                await AssertEx.ThrowsExceptionAsync <NotImplementedException>(block.Completion, allowDerivedTypes: false);
            });
        }
示例#11
0
 public FuncBlockViewModel GetFuncBlock(FuncBlock fblock)
 {
     if (LibFuncBlock.Model == fblock.Model)
     {
         return(LibFuncBlock);
     }
     foreach (FuncBlockViewModel fbvmodel in FuncBlocks)
     {
         if (fbvmodel.Model == fblock.Model)
         {
             return(fbvmodel);
         }
     }
     return(null);
 }
示例#12
0
        public void FuncBlock_FuncProducesItemsAndCompletes_ProducesItemsAndCompletes()
        {
            Test.Async(async() =>
            {
                var block = new FuncBlock <int>(async send =>
                {
                    await send(13);
                    await send(17);
                    await send(19);
                });

                var results = await GetBlockOutputsAsync(block);
                await block.Completion;
                Assert.IsTrue(results.SequenceEqual(new[] { 13, 17, 19 }));
            });
        }
示例#13
0
        public void FuncBlock_Faulted_Faults()
        {
            Test.Async(async() =>
            {
                var block = new FuncBlock <int>(async send =>
                {
                    while (true)
                    {
                        await send(13);
                        await TaskShim.Delay(100);
                    }
                });

                ((IDataflowBlock)block).Fault(new NotImplementedException());
                await AssertEx.ThrowsExceptionAsync <NotImplementedException>(block.Completion, allowDerivedTypes: false);
            });
        }
示例#14
0
        public void FuncBlock_FuncCancels_Completes()
        {
            Test.Async(async() =>
            {
                var cts = new CancellationTokenSource();
                cts.Cancel();
                var block = new FuncBlock <int>(async send =>
                {
                    await send(13);
                    cts.Token.ThrowIfCancellationRequested();
                });

                var results = await GetBlockOutputsAsync(block);
                await block.Completion;
                Assert.IsTrue(results.SequenceEqual(new[] { 13 }));
            });
        }
示例#15
0
        /// <summary>
        /// Runs a Tbasic script
        /// </summary>
        /// <param name="lines">the lines of the script to process</param>
        public void Execute(string[] lines)
        {
            CodeBlock[]    userFuncs;
            LineCollection code = ScanLines(lines, out userFuncs);

            if (userFuncs != null && userFuncs.Length > 0)
            {
                foreach (CodeBlock cb in userFuncs)
                {
                    FuncBlock fBlock = (FuncBlock)cb;
                    Global.SetFunction(fBlock.Template.Name, fBlock.CreateDelegate());
                }
            }
            Execute(code);

            /*if (ManagedWindows.Count != 0 && !this.ExitRequest) {
             *  System.Windows.Forms.Application.Run(new FormLoader(this));
             * }*/
        }
示例#16
0
        public void FuncBlock_Completed_Completes()
        {
            Test.Async(async() =>
            {
                var block = new FuncBlock <int>(async send =>
                {
                    while (true)
                    {
                        await send(13);
                        await TaskShim.Delay(100);
                    }
                });

                var resultTask = GetBlockOutputsAsync(block);
                block.Complete();
                await resultTask;
                await block.Completion;
            });
        }
示例#17
0
        public List <string> GetCodeCompleteNames(string profix)
        {
            List <string> ret = new List <string>();

            if (Current != null && Current is FuncBlock_Comment)
            {
                return(ret);
            }
            FuncBlock _current = Current;
            string    _profix;

            while (_current != null)
            {
                _profix = String.Format("{0:s}::{1:s}", _current.Namespace, profix);
                if (Assigns.ContainsKey(_profix))
                {
                    SortedList <int, FuncBlock_Assignment> subassigns = Assigns[_profix];
                    foreach (FuncBlock_Assignment fbassign in subassigns.Values)
                    {
                        if (fbassign.IndexEnd < CurrentIndex)
                        {
                            ret.Add(fbassign.Name);
                        }
                    }
                }
                if (_current is FuncBlock_Assignment || _current is FuncBlock_Statement)
                {
                    if (_current.Parent is FuncBlock_Root)
                    {
                        break;
                    }
                    _current = _current.Parent.Parent;
                }
                else
                {
                    _current = _current.Parent;
                }
            }
            ret.Sort();
            return(ret);
        }
示例#18
0
        public void FuncBlock_Canceled_Cancels()
        {
            Test.Async(async() =>
            {
                var cts   = new CancellationTokenSource();
                var block = new FuncBlock <int>(async send =>
                {
                    while (true)
                    {
                        await send(13);
                        await TaskShim.Delay(100);
                    }
                },
                                                new DataflowBlockOptions
                {
                    CancellationToken = cts.Token,
                });

                var resultTask = GetBlockOutputsAsync(block);
                cts.Cancel();
                var results = await resultTask;
                await AssertEx.CompletesCanceledAsync(block.Completion);
            });
        }
示例#19
0
        static private void FuncToCCode(
            StreamWriter sw,
            FuncBlock fblock,
            string code)
        {
            string text = String.Empty;
            string divi = String.Empty;
            int    bp   = 0;
            int    prev = fblock.IndexStart;

            if (fblock is FuncBlock_Root ||
                fblock is FuncBlock_Local)
            {
                if (fblock is FuncBlock_Local)
                {
                    sw.Write("{\n");
                }
                foreach (FuncBlock child in fblock.Childrens)
                {
                    if (prev < child.IndexStart)
                    {
                        sw.Write(code.Substring(prev, child.IndexStart - prev));
                    }
                    if (child is FuncBlock_Local)
                    {
                        FuncToCCode(sw, child, code);
                    }
                }
                BreakPointManager.Register(fblock);
                bp = fblock.BPAddress;
                sw.Write("bpcycle({0});\n", bp);
                if (fblock is FuncBlock_Local)
                {
                    sw.Write("}\n");
                }
            }
            else if (fblock is FuncBlock_ForHeader)
            {
                FuncBlock_ForHeader fblockfh = (FuncBlock_ForHeader)fblock;
                sw.Write("for (");
                if (fblockfh.Start != null)
                {
                    BreakPointManager.Register(fblockfh.Start);
                    bp = fblockfh.Start.BPAddress;
                    sw.Write("bpcycle({0}),{1}",
                             bp, ReplaceType(code, fblockfh.Start.IndexStart, fblockfh.Start.Length));
                }
                //sw.Write(";");
                if (fblockfh.Cond != null)
                {
                    BreakPointManager.Register(fblockfh.Cond);
                    bp = fblockfh.Cond.BPAddress;
                    sw.Write("bpcycle({0}),{1}",
                             bp, ReplaceType(code, fblockfh.Cond.IndexStart, fblockfh.Cond.Length));
                }
                //sw.Write(";");
                if (fblockfh.Next != null)
                {
                    BreakPointManager.Register(fblockfh.Next);
                    bp = fblockfh.Next.BPAddress;
                    sw.Write("bpcycle({0}),{1}",
                             bp, ReplaceType(code, fblockfh.Next.IndexStart, fblockfh.Next.Length));
                }
                sw.Write(")");
            }
            else if (fblock is FuncBlock_WhileHeader)
            {
                FuncBlock_WhileHeader fblockwh = (FuncBlock_WhileHeader)fblock;
                sw.Write("while (");
                if (fblockwh.Cond != null)
                {
                    BreakPointManager.Register(fblockwh.Cond);
                    bp = fblockwh.Cond.BPAddress;
                    sw.Write("bpcycle({0}),{1}",
                             bp, ReplaceType(code, fblockwh.Cond.IndexStart, fblockwh.Cond.Length));
                }
                sw.Write(")");
            }
            else
            {
                if (!(fblock is FuncBlock_Root))
                {
                    BreakPointManager.Register(fblock);
                    bp = fblock.BPAddress;
                    sw.Write("bpcycle({0});\n", bp);
                }
                sw.Write("{0}\n", ReplaceType(code, fblock.IndexStart, fblock.Length));
            }
        }
示例#20
0
 static public void Register(FuncBlock fblock)
 {
     fblock.IsBreakpoint = true;
     fblock.BPAddress    = ++count;
     fblocks.Add(fblock.BPAddress, fblock);
 }
        /// <summary>
        /// 当代码内容改变时发生
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void textEditer_DocumentChanged(object sender, DocumentChangeEventArgs e)
        {
            int offset = e.InsertionLength - e.RemovalLength;

            model.Move(e.Offset);
            Regex localRegex   = new Regex(@"[\{\}]");
            Regex stmtRegex    = new Regex(@";");
            Regex blankRegex   = new Regex(@"^\s*$");
            Match insertMatch1 = localRegex.Match(e.InsertedText);
            Match removeMatch1 = localRegex.Match(e.RemovedText);
            Match insertMatch2 = stmtRegex.Match(e.InsertedText);
            Match removeMatch2 = stmtRegex.Match(e.RemovedText);
            Match insertMatch3 = blankRegex.Match(e.InsertedText);
            Match removeMatch3 = blankRegex.Match(e.RemovedText);
            int   start        = 0;
            int   end          = 0;

            if (model.Current is FuncBlock_Comment)
            {
                FuncBlock _parent = model.Current.Parent;
                start = _parent.IndexStart;
                end   = _parent.IndexEnd - 1;
                while (end < CodeTextBox.Text.Length && CodeTextBox.Text[end] != '\n')
                {
                    end++;
                }
                _parent.Build(CodeTextBox.Text, start, end, offset);
            }
            else if (insertMatch3.Success && removeMatch3.Success)
            {
                model.Current.InnerOffset += offset;
            }
            else if (insertMatch1.Success || removeMatch1.Success)
            {
                model = new FuncBlockModel(CodeTextBox.Text);
                model.Move(e.Offset);
            }
            else
            {
                if (model.Current is FuncBlock_FuncHeader)
                {
                    model.CurrentNode = new LinkedListNode <FuncBlock>(model.Root);
                }
                if (model.Current is FuncBlock_Root)
                {
                    LinkedListNode <FuncBlock> nprev = null;
                    LinkedListNode <FuncBlock> nnext = null;
                    if (model.Current.Current != null)
                    {
                        nprev = model.Current.Current;
                        if (model.Current.Current.Next != null)
                        {
                            nnext = model.Current.Current.Next;
                        }
                    }
                    if (nprev != null &&
                        nprev.Value.IndexStart > e.Offset)
                    {
                        nnext = nprev;
                        nprev = nprev.Previous;
                    }
                    while (nprev != null &&
                           !(nprev.Value is FuncBlock_Local))
                    {
                        nprev = nprev.Previous;
                    }
                    while (nnext != null &&
                           !(nnext.Value is FuncBlock_Local))
                    {
                        nnext = nnext.Next;
                    }
                    start = nprev != null ? (nprev.Value.IndexEnd + 1) : (model.Current.IndexStart);
                    end   = nnext != null ? (nnext.Value.IndexStart - 2) : (model.Current.IndexEnd - 1);
                    model.Root.Build(CodeTextBox.Text, start, end, offset);
                }
                else if (insertMatch2.Success || removeMatch2.Success)
                {
                    start = model.Current.IndexStart;
                    if (model.Current is FuncBlock_Local)
                    {
                        end = model.Current.IndexEnd;
                        model.Current.Build(CodeTextBox.Text, start, end - 1, offset);
                    }
                    else if (model.Current.Parent is FuncBlock_Local)
                    {
                        end = model.Current.Parent.IndexEnd;
                        model.Current.Parent.Build(CodeTextBox.Text, start, end - 1, offset);
                    }
                    else
                    {
                        throw new Exception(String.Format("Code Structure Error : {0:s} in {1:s}",
                                                          model.Current.ToString(), model.Current.Parent.ToString()));
                    }
                }
                else
                {
                    if (model.Current is FuncBlock_Local)
                    {
                        start = model.Current.IndexStart;
                        end   = model.Current.IndexEnd - 1;
                        model.Current.Build(CodeTextBox.Text, start, end, offset);
                    }
                    else
                    {
                        start = model.Current.IndexStart;
                        end   = model.Current.IndexEnd;
                        model.Current.Parent.Build(CodeTextBox.Text, start, end, offset);
                    }
                }
            }
            model.Move(e.Offset);
            if (e.InsertionLength == 1 && e.RemovalLength == 0)
            {
                if (char.IsLetterOrDigit(e.InsertedText[0]) || e.InsertedText[0] == '_')
                {
                    int wordend   = CodeTextBox.CaretOffset - 1;
                    int wordstart = wordend;
                    while (wordstart >= 0 &&
                           (Char.IsLetterOrDigit(CodeTextBox.Text[wordstart]) || CodeTextBox.Text[wordstart] == '_'))
                    {
                        wordstart--;
                    }
                    while (wordend < CodeTextBox.Text.Length &&
                           (Char.IsLetterOrDigit(CodeTextBox.Text[wordend]) || CodeTextBox.Text[wordend] == '_'))
                    {
                        wordend++;
                    }
                    wordstart++;
                    wordend--;
                    CCSOffset       = wordstart;
                    CCSProfix       = CodeTextBox.Text.Substring(wordstart, wordend - wordstart + 1);
                    CCSProfixCursor = CodeTextBox.CaretOffset - wordstart;
                }
                else if (e.InsertedText[0] != '\n')
                {
                    CCSProfix = String.Empty;
                }
            }
            else if (e.InsertionLength == 0 && e.RemovalLength == 1)
            {
                if (CCSProfix.Length > 0 && CCSProfixCursor > 0)
                {
                    CCSProfix = CCSProfix.Remove(CCSProfixCursor - 1, 1);
                    CCSProfixCursor--;
                }
            }
            else
            {
                CCSProfix = String.Empty;
            }
            TextChanged(this, new RoutedEventArgs());
            SetXY(CodeTextBox.TextArea.Caret.Column, CodeTextBox.TextArea.Caret.Line);
            IsModify = true;
            //OutputDebug();
        }