public void Simple() { var sourceCode = @" { a = 3; a = 5; b = 3; c = (b + 1) * 2; a = 4; } "; Scanner scanner = new Scanner(); scanner.SetSource(sourceCode, 0); Parser parser = new Parser(scanner); parser.Parse(); var parentFiller = new FillParentsVisitor(); parser.root.Visit(parentFiller); var blocks = AllTacOptimization.Optimize(parser); var actual = blocks.blocks.Select(b => b.ToString().Trim()).ToList(); var expected = new List <string>() { "b = 3\n" + "c = 8\n" + "a = 4" }; Assert.AreEqual(expected, actual); }
public void Complex() { var sourceCode = @" { a = 2; b = 5; c = 3; if a - 1 > b + 2 { i = 1; j = i * i + 0; } else { g = c + 3; } d = c; b = g; } "; Scanner scanner = new Scanner(); scanner.SetSource(sourceCode, 0); Parser parser = new Parser(scanner); parser.Parse(); var parentFiller = new FillParentsVisitor(); parser.root.Visit(parentFiller); var blocks = AllTacOptimization.Optimize(parser); var actual = blocks.blocks.Select(b => b.ToString().Trim()).ToList(); var expected = new List <string>() { "a = 2\n" + "b = 5\n" + "c = 3\n" + "#t2 = False\n" + "if #t2 goto #L0", "g = 6\n" + "goto #L1", "#L0\n" + "i = 1\n" + "j = 1", "#L1\n" + "d = 3\n" + "b = 6" }; Assert.AreEqual(expected, actual); }
public void Simple2() { var sourceCode = @" { a = b + c; d = 3 * 2 + 1; e = (b + c) * d; c = (b + c) * 2; b = 2; goto 1; b = 3; 1: r = b + c; } "; Scanner scanner = new Scanner(); scanner.SetSource(sourceCode, 0); Parser parser = new Parser(scanner); parser.Parse(); var parentFiller = new FillParentsVisitor(); parser.root.Visit(parentFiller); var blocks = AllTacOptimization.Optimize(parser); var actual = blocks.blocks.Select(b => b.ToString().Trim()).ToList(); var expected = new List <string>() { "a = b + c\n" + "d = 7\n" + "#t3 = a * 7\n" + "e = #t3\n" + "#t5 = a * 2\n" + "c = #t5\n" + "b = 2\n" + "goto 1", "b = 3", "1\n" + "r = b + c" }; Assert.AreEqual(expected, actual); }
public void WithConstantPropagationIter() { var sourceCode = @" { b = 3; if a { goto 1; } b = 3; 1: r = b; } "; Scanner scanner = new Scanner(); scanner.SetSource(sourceCode, 0); Parser parser = new Parser(scanner); parser.Parse(); var parentFiller = new FillParentsVisitor(); parser.root.Visit(parentFiller); var blocks = AllTacOptimization.Optimize(parser); var actual = blocks.blocks.Select(b => b.ToString().Trim()).ToList(); var expected = new List <string>() { "b = 3\n" + "#t0 = !a\n" + "#t1 = !#t0\n" + "if #t1 goto 1", "b = 3", "1\n" + "r = 3" }; Assert.AreEqual(expected, actual); }
public void Compile(string path) { try { string Text = File.ReadAllText(path); Scanner scanner = new Scanner(); scanner.SetSource(Text, 0); Parser parser = new Parser(scanner); var parentVisitor = new FillParentsVisitor(); var b = parser.Parse(); if (!b) { Console.WriteLine("Parsing error"); } parser.root.Visit(parentVisitor); AllVisitorsOptimization.Optimization(parser, true); var blocks = AllTacOptimization.Optimize(parser, true); var instructions = blocks.BlockMerging(); Console.WriteLine("=====================Final Result====================="); foreach (var instruction in instructions) { Console.WriteLine(instruction); } } catch (FileNotFoundException) { Console.WriteLine("File {0} doesn't exist", path); } catch (LexException e) { Console.WriteLine("Lex error. " + e.Message); } catch (SyntaxException e) { Console.WriteLine("Syntax error. " + e.Message); } }