-
Notifications
You must be signed in to change notification settings - Fork 2
/
Compiler.cs
58 lines (51 loc) · 1.65 KB
/
Compiler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.IO;
using System.Collections.Generic;
using SimpleScanner;
using SimpleParser;
using SimpleLang.Visitors;
using SimpleLang.TAC;
using SimpleLang.Visitors.ChangeVisitors;
using SimpleLang.TACOptimizers;
using SimpleLang.CFG;
namespace SimpleLang
{
public class Compiler
{
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);
}
}
}
}