Пример #1
0
 public frm_Schema(SchemaBlock start)
 {
     InitializeComponent();
     drawer = new SchemaDrawer(start);
     drawer.DrawBlock(canvas);
     drawer.DrawArrows(canvas);
 }
        public void drawSchematic(object sender, EventArgs e)
        {
            SchemaBlock  start = new SchemaBlock("Begin", BlockTypes.Start);
            CodeAnalyzer ca    = new CodeAnalyzer(ref words);
            Dictionary <ExitTypes, List <SchemaBlock> > dic = ca.AnalyzeBlock(0, words.Length - 1, new List <SchemaBlock> {
                start
            });
            SchemaBlock end = new SchemaBlock("End", BlockTypes.Start);

            foreach (var item in dic[ExitTypes.EndofBlock])
            {
                item.links.Add(end);
            }
            foreach (var item in dic[ExitTypes.Return])
            {
                item.links.Add(end);
            }

            frm_Schema form = new frm_Schema(start);

            form.Show();
        }
Пример #3
0
        private void PlaceBlocks()
        {
            int currentX = 0, currentY = 0;
            Stack <SchemaBlock> stack = new Stack <SchemaBlock>();

            stack.Push(_start);
            while (stack.Count != 0)
            {
                SchemaBlock current = stack.Pop();
                if (!current.isPlaced)
                {
                    if (currentX < current.fieldCoord.X)
                    {
                        currentX = current.fieldCoord.X;
                        currentY = current.fieldCoord.Y;
                        foreach (SchemaBlock item in stack)
                        {
                            if (!item.dontMove)
                            {
                                item.fieldCoord += new Size(1, 0);
                            }
                        }
                    }

                    if (current.Type != BlockTypes.Condition)
                    {
                        current.fieldCoord = new Point(currentX, currentY++);
                        Matrix[current.fieldCoord.Y, current.fieldCoord.X] = current;
                        current.isPlaced = true;
                        if (current.links.Count > 0)
                        {
                            if (current.links[0].isPlaced && current.links[0].fieldCoord.Y <= current.fieldCoord.Y && current.links[0].fieldCoord.X != current.fieldCoord.X)
                            {
                                int shift = current.fieldCoord.Y - current.links[0].fieldCoord.Y + 1;
                                for (int i = 0; i < current.fieldCoord.X; i++)
                                {
                                    for (int j = _height - 1; j >= current.fieldCoord.Y + shift; j--)
                                    {
                                        Matrix[j, i] = Matrix[j - shift, i];
                                        if (Matrix[j, i] != null)
                                        {
                                            Matrix[j, i].fieldCoord += new Size(0, shift);
                                        }
                                    }
                                    Matrix[current.links[0].fieldCoord.Y - shift, i] = null;
                                }
                            }
                            if (!stack.Contains(current.links[0]))
                            {
                                stack.Push(current.links[0]);
                            }
                        }
                    }
                    else
                    {
                        current.fieldCoord = new Point(currentX, currentY++);
                        Matrix[current.fieldCoord.Y, current.fieldCoord.X] = current;
                        current.isPlaced = true;
                        for (int i = current.links.Count - 1; i >= 0; i--)
                        {
                            if (!stack.Contains(current.links[i]))
                            {
                                stack.Push(current.links[i]);
                                if (!current.isCycleCondition)
                                {
                                    current.links[i].fieldCoord += new Size(i + currentX, currentY);
                                }
                                else
                                {
                                    current.links[i].dontMove = true;
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #4
0
 public SchemaDrawer(SchemaBlock start)
 {
     _start = start;
     Matrix = new SchemaBlock[_height, _width];
     PlaceBlocks();
 }
Пример #5
0
        public Dictionary <ExitTypes, List <SchemaBlock> > AnalyzeBlock(int start, int end, List <SchemaBlock> lastElement)
        {
            Dictionary <ExitTypes, List <SchemaBlock> > dict = new Dictionary <ExitTypes, List <SchemaBlock> >
            {
                [ExitTypes.EndofBlock] = new List <SchemaBlock>(),
                [ExitTypes.Return]     = new List <SchemaBlock>(),
                [ExitTypes.Continue]   = new List <SchemaBlock>(),
                [ExitTypes.Break]      = new List <SchemaBlock>()
            };
            List <SchemaBlock> prev = new List <SchemaBlock>(lastElement);

            string     currentBlockContent = "";
            BlockTypes currentBlockType    = BlockTypes.Operator;

            for (int i = start; i <= end; i++)
            {
                // Conditions handling
                //
                if (words[i] == "if")
                {
                    #region "if" analyze
                    i++;
                    while (words[++i] != ")")
                    {
                        currentBlockContent += words[i] + " ";
                    }

                    SchemaBlock block = new SchemaBlock(currentBlockContent, BlockTypes.Condition);
                    currentBlockContent = "";
                    foreach (SchemaBlock item in prev)
                    {
                        item.links.Add(block);
                    }
                    prev.Clear();
                    prev.Add(block);

                    if (words[++i] == "{")
                    {
                        dict = DictMix(dict, AnalyzeBlock(i + 1, i = getEndBracket(i) - 1, prev)); i++;
                    }
                    else
                    {
                        dict = DictMix(dict, AnalyzeBlock(i, i = Array.IndexOf(words, ";", i), prev));
                    }
                    #endregion

                    #region "else if" analyze
                    while (words[i + 1] == "else" && words[i + 2] == "if")
                    {
                        i += 3;
                        while (words[++i] != ")")
                        {
                            currentBlockContent += words[i] + " ";
                        }

                        block = new SchemaBlock(currentBlockContent, BlockTypes.Condition);
                        currentBlockContent = "";
                        foreach (SchemaBlock item in prev)
                        {
                            item.links.Add(block);
                        }
                        prev.Clear();
                        prev.Add(block);

                        if (words[++i] == "{")
                        {
                            dict = DictMix(dict, AnalyzeBlock(i + 1, i = getEndBracket(i) - 1, prev)); i++;
                        }
                        else
                        {
                            dict = DictMix(dict, AnalyzeBlock(i, i = Array.IndexOf(words, ";", i), prev));
                        }
                    }
                    #endregion

                    #region "else" analyze
                    if (words[++i] == "else")
                    {
                        if (words[++i] == "{")
                        {
                            dict = DictMix(dict, AnalyzeBlock(i + 1, i = getEndBracket(i) - 1, prev)); i++;
                        }
                        else
                        {
                            dict = DictMix(dict, AnalyzeBlock(i, i = Array.IndexOf(words, ";", i), prev));
                        }
                    }
                    else
                    {
                        dict[ExitTypes.EndofBlock] = prev.Union(dict[ExitTypes.EndofBlock]).ToList();
                        currentBlockContent       += words[i] + " ";
                    }
                    #endregion

                    prev = new List <SchemaBlock>(dict[ExitTypes.EndofBlock]);
                    dict[ExitTypes.EndofBlock].Clear();
                    continue;
                }

                if (words[i] == "while")
                {
                    i++;
                    while (words[++i] != ")")
                    {
                        currentBlockContent += words[i] + " ";
                    }

                    SchemaBlock block = new SchemaBlock(currentBlockContent, BlockTypes.Condition);
                    block.isCycleCondition = true;
                    currentBlockContent    = "";
                    foreach (SchemaBlock item in prev)
                    {
                        item.links.Add(block);
                    }
                    prev.Clear();
                    prev.Add(block);

                    if (words[++i] == "{")
                    {
                        dict = DictMix(dict, AnalyzeBlock(i + 1, i = getEndBracket(i) - 1, prev)); i++;
                    }
                    else
                    {
                        dict = DictMix(dict, AnalyzeBlock(i, i = Array.IndexOf(words, ";", i), prev));
                    }

                    foreach (SchemaBlock item in dict[ExitTypes.EndofBlock])
                    {
                        item.links.Add(block);
                    }
                    dict[ExitTypes.EndofBlock].Clear();

                    foreach (SchemaBlock item in dict[ExitTypes.Continue])
                    {
                        item.links.Add(block);
                    }
                    dict[ExitTypes.Continue].Clear();

                    prev = prev.Union(dict[ExitTypes.Break]).ToList();
                    continue;
                }

                if (words[i] == "for")
                {
                    i++;
                    while (words[++i] != ";")
                    {
                        currentBlockContent += words[i] + " ";
                    }

                    SchemaBlock block = new SchemaBlock(currentBlockContent, BlockTypes.Operator);
                    currentBlockContent = "";
                    foreach (SchemaBlock item in prev)
                    {
                        item.links.Add(block);
                    }
                    prev.Clear();
                    prev.Add(block);

                    while (words[++i] != ";")
                    {
                        currentBlockContent += words[i] + " ";
                    }

                    block = new SchemaBlock(currentBlockContent, BlockTypes.Condition);
                    block.isCycleCondition = true;
                    currentBlockContent    = "";
                    foreach (SchemaBlock item in prev)
                    {
                        item.links.Add(block);
                    }
                    prev.Clear();
                    prev.Add(block);

                    while (words[++i] != ")")
                    {
                        currentBlockContent += words[i] + " ";
                    }

                    SchemaBlock endBlock = new SchemaBlock(currentBlockContent, BlockTypes.Operator);
                    currentBlockContent = "";
                    endBlock.links.Add(block);

                    if (words[++i] == "{")
                    {
                        dict = DictMix(dict, AnalyzeBlock(i + 1, i = getEndBracket(i) - 1, prev)); i++;
                    }
                    else
                    {
                        dict = DictMix(dict, AnalyzeBlock(i, i = Array.IndexOf(words, ";", i), prev));
                    }

                    foreach (SchemaBlock item in dict[ExitTypes.EndofBlock])
                    {
                        item.links.Add(endBlock);
                    }
                    dict[ExitTypes.EndofBlock].Clear();

                    foreach (SchemaBlock item in dict[ExitTypes.Continue])
                    {
                        item.links.Add(endBlock);
                    }
                    dict[ExitTypes.Continue].Clear();

                    prev = prev.Union(dict[ExitTypes.Break]).ToList();
                    continue;
                }

                //Not working correct!!!
                if (words[i] == "do")
                {
                    if (words[++i] == "{")
                    {
                        dict = DictMix(dict, AnalyzeBlock(++i, i = getEndBracket(i - 1) - 1, prev)); i++;
                    }
                    else
                    {
                        dict = DictMix(dict, AnalyzeBlock(++i, i = Array.IndexOf(words, ";", i), prev));
                    }

                    i += 2;
                    while (words[++i] != ")")
                    {
                        currentBlockContent += words[i] + " ";
                    }


                    SchemaBlock block = new SchemaBlock(currentBlockContent, BlockTypes.Condition);
                    currentBlockContent = "";

                    SchemaBlock startBlock = prev[0].links[prev[0].links.Count - 1];
                    block.links.Add(startBlock);

                    foreach (SchemaBlock item in dict[ExitTypes.EndofBlock])
                    {
                        item.links.Add(block);
                    }
                    dict[ExitTypes.EndofBlock].Clear();

                    foreach (SchemaBlock item in dict[ExitTypes.Continue])
                    {
                        item.links.Add(block);
                    }
                    dict[ExitTypes.Continue].Clear();

                    prev.Clear();
                    prev.Add(block);
                    prev = prev.Union(dict[ExitTypes.Break]).ToList();

                    dict[ExitTypes.EndofBlock] = prev;
                    continue;
                }

                if (words[i] == "break")
                {
                    dict[ExitTypes.Break] = dict[ExitTypes.Break].Union(prev).ToList();
                    return(dict);
                }

                if (words[i] == "continue")
                {
                    dict[ExitTypes.Continue] = dict[ExitTypes.Continue].Union(prev).ToList();
                    return(dict);
                }

                if (words[i] == "return")
                {
                    dict[ExitTypes.Return] = dict[ExitTypes.Return].Union(prev).ToList();
                    return(dict);
                }

                if (words[i] != ";")
                {
                    if (Array.IndexOf(IOtags, words[i]) != -1)
                    {
                        currentBlockType = BlockTypes.IO;
                    }
                    else if (Array.IndexOf(ProcedureTags, words[i]) != -1)
                    {
                        currentBlockType = currentBlockType == BlockTypes.Operator ? BlockTypes.Procedure : currentBlockType;
                    }
                    currentBlockContent += words[i] + " ";
                }
                else
                {
                    if (currentBlockContent != "")
                    {
                        SchemaBlock block = new SchemaBlock(currentBlockContent, currentBlockType);
                        currentBlockContent = "";
                        currentBlockType    = BlockTypes.Operator;
                        foreach (SchemaBlock item in prev)
                        {
                            item.links.Add(block);
                        }
                        prev.Clear();
                        prev.Add(block);
                    }
                }
            }
            dict[ExitTypes.EndofBlock] = prev.Union(dict[ExitTypes.EndofBlock]).ToList();
            return(dict);
        }