Exemplo n.º 1
0
        private void UpdateTab(Modes m, TextBox txt)
        {
            if (FileName.Length == 0)
            {
                txt.Text = "File is not set";
                return;
            }
            try {
                string tt = System.IO.File.ReadAllText(FileName);
                if (m == Modes.Text)
                {
                    txt.Text = tt;
                    return;
                }
                SimpleScanner.Scanner scan = new SimpleScanner.Scanner();
                scan.SetSource(tt, 0);
                SimpleParser.Parser pars = new SimpleParser.Parser(scan);
                var b = pars.Parse();
                if (!b)
                {
                    txt.Text = "Ошибка парсинга";
                    return;
                }
                var r = pars.root;
                SimpleLang.Visitors.FillParentVisitor parVisitor = new SimpleLang.Visitors.FillParentVisitor();
                r.Visit(parVisitor);

                SimpleLang.Visitors.AutoApplyVisitor optAst = GetASTOptimizer();
                optAst.Apply(r);
                if (m == Modes.ASTOpt)
                {
                    SimpleLang.Visitors.PrettyPrintVisitor vis = new SimpleLang.Visitors.PrettyPrintVisitor();
                    r.Visit(vis);
                    txt.Text = vis.Text;
                    return;
                }

                SimpleLang.Visitors.ThreeAddressCodeVisitor threeCodeVisitor = new SimpleLang.Visitors.ThreeAddressCodeVisitor();
                r.Visit(threeCodeVisitor);
                if (m == Modes.BeforeThreeCode)
                {
                    txt.Text = SimpleLang.Visitors.ThreeAddressCodeVisitor.ToString(threeCodeVisitor.GetCode());
                    Console.WriteLine(txt.Text);
                    return;
                }
                if (m == Modes.BeforeRun)
                {
                    SimpleLang.Compiler.ILCodeGenerator gen = new SimpleLang.Compiler.ILCodeGenerator();
                    gen.Generate(threeCodeVisitor.GetCode());
                    var    timer = System.Diagnostics.Stopwatch.StartNew();
                    string res   = gen.Execute();
                    timer.Stop();
                    res = res + "\n\n\nExecuted: " + timer.ElapsedMilliseconds.ToString() + " ms"
                          + " or " + timer.ElapsedTicks.ToString() + " ticks";
                    txt.Text = res;
                    return;
                }

                if (m == Modes.BeforeBlocks)
                {
                    var blocks = new SimpleLang.Block.Block(threeCodeVisitor).GenerateBlocks();
                    txt.Text = SimpleLang.Visitors.ThreeAddressCodeVisitor.ToString(blocks);
                    return;
                }
                var opt     = GetOptimiser();
                var outcode = opt.Apply(threeCodeVisitor);

                if (m == Modes.AfterBlocks)
                {
                    txt.Text = SimpleLang.Visitors.ThreeAddressCodeVisitor.ToString(outcode);
                    return;
                }
                if (m == Modes.AfterTbreeCode)
                {
                    System.Collections.Generic.LinkedList <SimpleLang.Visitors.ThreeCode> res = new System.Collections.Generic.LinkedList <SimpleLang.Visitors.ThreeCode>();
                    foreach (var block in outcode)
                    {
                        foreach (SimpleLang.Visitors.ThreeCode code in block)
                        {
                            res.AddLast(code);
                        }
                    }
                    txt.Text = SimpleLang.Visitors.ThreeAddressCodeVisitor.ToString(res);
                    Console.WriteLine(txt.Text);
                    return;
                }
                if (m == Modes.AfterRun)
                {
                    System.Collections.Generic.LinkedList <SimpleLang.Visitors.ThreeCode> res = new System.Collections.Generic.LinkedList <SimpleLang.Visitors.ThreeCode>();
                    foreach (var block in outcode)
                    {
                        foreach (SimpleLang.Visitors.ThreeCode code in block)
                        {
                            res.AddLast(code);
                        }
                    }
                    SimpleLang.Compiler.ILCodeGenerator gen = new SimpleLang.Compiler.ILCodeGenerator();
                    gen.Generate(res);
                    var    timer = System.Diagnostics.Stopwatch.StartNew();
                    string ooo   = gen.Execute();
                    timer.Stop();
                    ooo = ooo + "\n\n\nExecuted: " + timer.ElapsedMilliseconds.ToString() + " ms"
                          + " or " + timer.ElapsedTicks.ToString() + " ticks";
                    txt.Text = ooo;
                    return;
                }

                txt.Text = "Is not implemented";
            } catch (Exception e) {
                txt.Text = e.ToString();
            }
        }
Exemplo n.º 2
0
        private void UpdateTab(Modes m, TextBox txt, PictureBox box, Panel panel)
        {
            if (FileName.Length == 0)
            {
                txt.Text = "File is not set";
                return;
            }
            try {
                if (m == Modes.AfterGraph || m == Modes.BeforeGraph)
                {
                    panel.Visible = true;
                    txt.Visible   = false;
                }
                else
                {
                    panel.Visible = false;
                    txt.Visible   = true;
                }
                string tt = System.IO.File.ReadAllText(FileName);
                if (m == Modes.Text)
                {
                    txt.Text = tt;
                    return;
                }
                SimpleScanner.Scanner scan = new SimpleScanner.Scanner();
                scan.SetSource(tt, 0);
                SimpleParser.Parser pars = new SimpleParser.Parser(scan);
                var b = pars.Parse();
                if (!b)
                {
                    txt.Text = "Ошибка парсинга";
                    return;
                }
                var r = pars.root;
                SimpleLang.Visitors.FillParentVisitor parVisitor = new SimpleLang.Visitors.FillParentVisitor();
                r.Visit(parVisitor);

                SimpleLang.Visitors.ThreeAddressCodeVisitor threeCodeVisitor = new SimpleLang.Visitors.ThreeAddressCodeVisitor();
                r.Visit(threeCodeVisitor);
                if (m == Modes.BeforeThreeCode)
                {
                    txt.Text = SimpleLang.Visitors.ThreeAddressCodeVisitor.ToString(threeCodeVisitor.GetCode());
                    Console.WriteLine(txt.Text);
                    return;
                }
                if (m == Modes.BeforeRun)
                {
                    SimpleLang.Compiler.ILCodeGenerator gen = new SimpleLang.Compiler.ILCodeGenerator();
                    gen.Generate(threeCodeVisitor.GetCode());

                    List <long> datas     = new List <long>();
                    List <long> datas2    = new List <long>();
                    int         count_run = Int32.Parse(textBox3.Text);
                    string      res       = "";
                    for (int i = 0; i < count_run; i++)
                    {
                        var timer = System.Diagnostics.Stopwatch.StartNew();
                        res = gen.Execute();
                        timer.Stop();

                        datas.Add(timer.ElapsedMilliseconds);
                        datas2.Add(timer.ElapsedTicks);
                    }

                    res = res + Environment.NewLine + Environment.NewLine + "Executed avg: " + (datas.Min()).ToString() + " ms"
                          + " or " + (datas2.Min()).ToString() + " ticks" + Environment.NewLine;
                    for (int i = 0; i < datas.Count; i++)
                    {
                        res = res + i.ToString() + ": " + datas[i].ToString() + " ms or " + datas2[i].ToString() + " ticks" + Environment.NewLine;
                    }


                    txt.Text = res;
                    return;
                }

                if (m == Modes.BeforeBlocks)
                {
                    var blocks = new SimpleLang.Block.Block(threeCodeVisitor).GenerateBlocks();
                    txt.Text = SimpleLang.Visitors.ThreeAddressCodeVisitor.ToString(blocks);
                    return;
                }
                if (m == Modes.BeforeMass)
                {
                    var blocks = new SimpleLang.Block.Block(threeCodeVisitor).GenerateBlocks();
                    DrawMass(blocks, txt);
                    return;
                }
                if (m == Modes.BeforeGraph)
                {
                    var blocks = new SimpleLang.Block.Block(threeCodeVisitor).GenerateBlocks();
                    SimpleLang.ControlFlowGraph.ControlFlowGraph gra = new SimpleLang.ControlFlowGraph.ControlFlowGraph(blocks);
                    DrawCFG(box, gra.cfg, blocks, panel);
                    return;
                }

                SimpleLang.Visitors.AutoApplyVisitor optAst = GetASTOptimizer();
                optAst.Apply(r);
                if (m == Modes.ASTOpt)
                {
                    SimpleLang.Visitors.PrettyPrintVisitor vis = new SimpleLang.Visitors.PrettyPrintVisitor();
                    r.Visit(vis);
                    txt.Text = vis.Text;
                    return;
                }

                threeCodeVisitor = new SimpleLang.Visitors.ThreeAddressCodeVisitor();
                r.Visit(threeCodeVisitor);
                var opt     = GetOptimiser();
                var outcode = opt.Apply(threeCodeVisitor);

                if (m == Modes.AfterMass)
                {
                    DrawMass(outcode, txt);
                    return;
                }

                if (m == Modes.AfterBlocks)
                {
                    txt.Text = SimpleLang.Visitors.ThreeAddressCodeVisitor.ToString(outcode);
                    return;
                }
                if (m == Modes.AfterGraph)
                {
                    SimpleLang.ControlFlowGraph.ControlFlowGraph gra = new SimpleLang.ControlFlowGraph.ControlFlowGraph(outcode);
                    DrawCFG(box, gra.cfg, outcode, panel);
                    return;
                }
                if (m == Modes.AfterTbreeCode)
                {
                    System.Collections.Generic.LinkedList <SimpleLang.Visitors.ThreeCode> res = new System.Collections.Generic.LinkedList <SimpleLang.Visitors.ThreeCode>();
                    foreach (var block in outcode)
                    {
                        foreach (SimpleLang.Visitors.ThreeCode code in block)
                        {
                            res.AddLast(code);
                        }
                    }
                    txt.Text = SimpleLang.Visitors.ThreeAddressCodeVisitor.ToString(res);
                    Console.WriteLine(txt.Text);
                    return;
                }
                if (m == Modes.AfterRun)
                {
                    System.Collections.Generic.LinkedList <SimpleLang.Visitors.ThreeCode> res = new System.Collections.Generic.LinkedList <SimpleLang.Visitors.ThreeCode>();
                    foreach (var block in outcode)
                    {
                        foreach (SimpleLang.Visitors.ThreeCode code in block)
                        {
                            res.AddLast(code);
                        }
                    }
                    SimpleLang.Compiler.ILCodeGenerator gen = new SimpleLang.Compiler.ILCodeGenerator();
                    gen.Generate(res);

                    List <long> datas     = new List <long>();
                    List <long> datas2    = new List <long>();
                    int         count_run = Int32.Parse(textBox3.Text);
                    string      re2       = "";
                    for (int i = 0; i < count_run; i++)
                    {
                        var timer = System.Diagnostics.Stopwatch.StartNew();
                        re2 = gen.Execute();
                        timer.Stop();

                        datas.Add(timer.ElapsedMilliseconds);
                        datas2.Add(timer.ElapsedTicks);
                    }

                    re2 = re2 + Environment.NewLine + Environment.NewLine + "Executed avg: " + (datas.Min()).ToString() + " ms"
                          + " or " + (datas2.Min()).ToString() + " ticks" + Environment.NewLine;
                    for (int i = 0; i < datas.Count; i++)
                    {
                        re2 = re2 + i.ToString() + ": " + datas[i].ToString() + " ms or " + datas2[i].ToString() + " ticks" + Environment.NewLine;
                    }
                    txt.Text = re2;
                    return;
                }

                txt.Text = "Is not implemented";
            } catch (Exception e) {
                panel.Visible = false;
                txt.Visible   = true;
                txt.Text      = e.ToString();
            }
        }