-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoptimizer.go
139 lines (116 loc) · 5.44 KB
/
optimizer.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package gojacego
type optimizer struct {
executor interpreter
}
func (this *optimizer) optimize(op operation, functionRegistry *functionRegistry, constantRegistry *constantRegistry) operation {
return optimize(this.executor, op, functionRegistry, constantRegistry)
}
func optimize(executor interpreter, op operation, functionRegistry *functionRegistry, constantRegistry *constantRegistry) operation {
if _, b := op.(*constantOperation); !op.OperationMetadata().DependsOnVariables && op.OperationMetadata().IsIdempotent && !b {
result, _ := executor.execute(op, nil, functionRegistry, constantRegistry)
return newConstantOperation(floatingPoint, result)
} else {
if cop, ok := op.(*addOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
} else if cop, ok := op.(*subtractionOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
} else if cop, ok := op.(*multiplicationOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop1, ok1 := cop.OperationOne.(*constantOperation)
if ok1 {
if cop1.Metadata.DataType == floatingPoint && cop1.Value == 0.0 {
return newConstantOperation(floatingPoint, 0.0)
} else {
if toFloat64Panic(cop1.Value) == 0.0 {
return newConstantOperation(floatingPoint, 0.0)
}
}
}
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
cop2, ok2 := cop.OperationTwo.(*constantOperation)
if ok2 {
if cop2.Metadata.DataType == floatingPoint && cop2.Value == 0.0 {
return newConstantOperation(floatingPoint, 0.0)
} else {
if toFloat64Panic(cop2.Value) == 0.0 {
return newConstantOperation(floatingPoint, 0.0)
}
}
}
} else if cop, ok := op.(*divisorOperation); ok {
cop.Dividend = optimize(executor, cop.Dividend, functionRegistry, constantRegistry)
cop.Divisor = optimize(executor, cop.Divisor, functionRegistry, constantRegistry)
} else if cop, ok := op.(*exponentiationOperation); ok {
cop.Base = optimize(executor, cop.Base, functionRegistry, constantRegistry)
cop.Exponent = optimize(executor, cop.Exponent, functionRegistry, constantRegistry)
} else if cop, ok := op.(*greaterThanOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
} else if cop, ok := op.(*greaterOrEqualThanOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
} else if cop, ok := op.(*andOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop1, ok1 := cop.OperationOne.(*constantOperation)
if ok1 {
if cop1.Metadata.DataType == floatingPoint && cop1.Value == 0.0 {
return newConstantOperation(floatingPoint, 0.0)
} else {
if toFloat64Panic(cop1.Value) == 0.0 {
return newConstantOperation(floatingPoint, 0.0)
}
}
}
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
cop2, ok2 := cop.OperationTwo.(*constantOperation)
if ok2 {
if cop2.Metadata.DataType == floatingPoint && cop2.Value == 0.0 {
return newConstantOperation(floatingPoint, 0.0)
} else {
if toFloat64Panic(cop2.Value) == 0.0 {
return newConstantOperation(floatingPoint, 0.0)
}
}
}
} else if cop, ok := op.(*orOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop1, ok1 := cop.OperationOne.(*constantOperation)
if ok1 {
if cop1.Metadata.DataType == floatingPoint && cop1.Value == 1.0 {
return newConstantOperation(floatingPoint, 1.0)
} else {
if toFloat64Panic(cop1.Value) == 1.0 {
return newConstantOperation(floatingPoint, 1.0)
}
}
}
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
cop2, ok2 := cop.OperationTwo.(*constantOperation)
if ok2 {
if cop2.Metadata.DataType == floatingPoint && cop2.Value == 1.0 {
return newConstantOperation(floatingPoint, 1.0)
} else {
if toFloat64Panic(cop2.Value) == 1.0 {
return newConstantOperation(floatingPoint, 1.0)
}
}
}
} else if cop, ok := op.(*lessThanOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
} else if cop, ok := op.(*lessOrEqualThanOperation); ok {
cop.OperationOne = optimize(executor, cop.OperationOne, functionRegistry, constantRegistry)
cop.OperationTwo = optimize(executor, cop.OperationTwo, functionRegistry, constantRegistry)
} else if cop, ok := op.(*functionOperation); ok {
optimizedArguments := make([]operation, len(cop.Arguments))
for idx, arg := range cop.Arguments {
ret := optimize(executor, arg, functionRegistry, constantRegistry)
optimizedArguments[idx] = ret
}
cop.Arguments = optimizedArguments
}
return op
}
}