-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathASTOptimizer.cs
47 lines (45 loc) · 1.43 KB
/
ASTOptimizer.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
using System.Collections.Generic;
using SimpleLang.Visitors;
using SimpleParser;
namespace SimpleLang
{
public static class ASTOptimizer
{
private static IReadOnlyList<ChangeVisitor> ASTOptimizations { get; } = new List<ChangeVisitor>
{
new OptExprVarEqualToItself(),
new OptExprMultDivByOne(),
new OptExprMultZero(),
new OptExprSumZero(),
new OptExprWithOperationsBetweenConsts(),
new OptStatIfTrue(),
new OptStatIfFalse(),
new OptExprEqualBoolNum(),
new OptWhileFalseVisitor(),
new OptExprSimilarNotEqual(),
new OptAssignEquality(),
new IfNullElseNull(),
new OptExprTransformUnaryToValue(),
new OptExprFoldUnary(),
new OptExprAlgebraic(),
new OptExprSubEqualVar()
};
public static void Optimize(Parser parser, IReadOnlyList<ChangeVisitor> Optimizations = null)
{
Optimizations = Optimizations ?? ASTOptimizations;
var optInd = 0;
do
{
parser.root.Visit(Optimizations[optInd]);
if (Optimizations[optInd].Changed)
{
optInd = 0;
}
else
{
++optInd;
}
} while (optInd < Optimizations.Count);
}
}
}