forked from Matway/mpl-c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
astOptimizers.mpl
executable file
·232 lines (201 loc) · 6.06 KB
/
astOptimizers.mpl
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
"astOptimizers" module
"control" useModule
optimizeLabelsInCurrentNode: [
node:;
addDataToProcess: [
copy isLabel:; recData:;
isLabel @needToSwap.pushBack
IndexArray @processedNodes.pushBack
@recData AsRef @unfinishedNodes.pushBack
0 @unfinishedIndexes.pushBack
];
node.data.getTag AstNodeType.Label = [
AstNodeType.Label @node.@data.get.@children TRUE addDataToProcess
] [
node.data.getTag AstNodeType.Code = [
AstNodeType.Code @node.@data.get FALSE addDataToProcess
] [
node.data.getTag AstNodeType.Object = [
AstNodeType.Object @node.@data.get FALSE addDataToProcess
] [
node.data.getTag AstNodeType.List = [
AstNodeType.List @node.@data.get FALSE addDataToProcess
] [
# do nothing
] if
] if
] if
] if
];
optimizeLabels: [
parserResult:;
unfinishedNodes: IndexArray AsRef Array;
processedNodes: IndexArray Array;
unfinishedIndexes: Int32 Array;
needToSwap: Cond Array;
FALSE @needToSwap.pushBack
IndexArray @processedNodes.pushBack
@parserResult.@nodes AsRef @unfinishedNodes.pushBack
0 dynamic @unfinishedIndexes.pushBack
[
uSize: unfinishedIndexes.dataSize 1 -;
index: uSize unfinishedIndexes.at copy;
nodes: @unfinishedNodes.last.data;
index nodes.dataSize < [
nodeIndex: index @nodes.at copy;
nodeIndex @processedNodes.last.pushBack
nodeIndex @parserResult.@memory.at optimizeLabelsInCurrentNode
indexAfter: uSize @unfinishedIndexes.at;
indexAfter 1 + @indexAfter set
TRUE
] [
needToSwap.last [
sz: processedNodes.dataSize copy;
src: sz 1 - @processedNodes.at;
dst: sz 2 - @processedNodes.at;
ll: dst.last copy;
@dst.popBack
src.dataSize [
i src.at @dst.pushBack
] times
ll @dst.pushBack
@src.clear
] when
lastUnfinished: @unfinishedNodes.last.@data;
processedNodes.last @lastUnfinished set
@needToSwap.popBack
@processedNodes.popBack
@unfinishedIndexes.popBack
@unfinishedNodes.popBack
unfinishedNodes.getSize 0 >
] if
] loop
];
optimizeNamesInCurrentNode: [
node:;
optimizeName: [
nameWithInfo:;
fr: nameWithInfo.name makeStringView ids.find;
fr.success [
fr.value @nameWithInfo.@nameInfo set
] [
ids.dataSize @nameWithInfo.@nameInfo set
nameWithInfo.name ids.dataSize @ids.insert # copy string here
] if
];
addToProcess: [
data:;
@data AsRef @unfinishedNodes.pushBack
0 @unfinishedIndexes.pushBack
];
node.data.getTag AstNodeType.Name < [
node.data.getTag AstNodeType.Label = [
AstNodeType.Label @node.@data.get optimizeName # must not have children, it must be after label optimizing
] [
node.data.getTag AstNodeType.Code = [
AstNodeType.Code @node.@data.get addToProcess # must not have children, it must be after label optimizing
] [
node.data.getTag AstNodeType.Object = [
AstNodeType.Object @node.@data.get addToProcess # must not have children, it must be after label optimizing
] [
node.data.getTag AstNodeType.List = [
AstNodeType.List @node.@data.get addToProcess # must not have children, it must be after label optimizing
] [
# ok
] if
] if
] if
] if
] [
node.data.getTag AstNodeType.Name = [
AstNodeType.Name @node.@data.get optimizeName
] [
node.data.getTag AstNodeType.NameRead = [
AstNodeType.NameRead @node.@data.get optimizeName
] [
node.data.getTag AstNodeType.NameWrite = [
AstNodeType.NameWrite @node.@data.get optimizeName
] [
node.data.getTag AstNodeType.NameMember = [
AstNodeType.NameMember @node.@data.get optimizeName
] [
node.data.getTag AstNodeType.NameReadMember = [
AstNodeType.NameReadMember @node.@data.get optimizeName
] [
node.data.getTag AstNodeType.NameWriteMember = [
AstNodeType.NameWriteMember @node.@data.get optimizeName
] [
# ok
] if
] if
] if
] if
] if
] if
] if
];
optimizeNames: [
multiParserResult:;
ids: @multiParserResult.@names;
i: 0 dynamic;
[
i multiParserResult.nodes.dataSize < [
currentNodesArray: i @multiParserResult.@nodes.at;
unfinishedNodes: IndexArray AsRef Array;
unfinishedIndexes: Int32 Array;
@currentNodesArray AsRef @unfinishedNodes.pushBack
0 dynamic @unfinishedIndexes.pushBack
[
uSize: unfinishedIndexes.dataSize 1 -;
index: uSize unfinishedIndexes.at copy;
nodes: unfinishedNodes.last.data;
index nodes.dataSize < [
index nodes.at @multiParserResult.@memory.at optimizeNamesInCurrentNode # reallocation here
indexAfter: uSize @unfinishedIndexes.at;
indexAfter 1 + @indexAfter set
TRUE
] [
@unfinishedIndexes.popBack
@unfinishedNodes.popBack
unfinishedNodes.getSize 0 >
] if
] loop
i 1 + @i set TRUE
] &&
] loop
];
concatParserResults: [
mresult:;
results:;
p: 0;
shift: 0;
adjustArray: [
indexArray:;
@indexArray [
cur: .@value;
cur shift + @cur set
] each
];
r: @results makeArrayRange;
@results [
current: .@value;
@current.@nodes adjustArray
@current.@memory [
currentNode: .@value;
currentNode.data.getTag AstNodeType.Code = [
AstNodeType.Code @currentNode.@data.get adjustArray
] [
currentNode.data.getTag AstNodeType.List = [
AstNodeType.List @currentNode.@data.get adjustArray
] [
currentNode.data.getTag AstNodeType.Object = [
AstNodeType.Object @currentNode.@data.get adjustArray
] when
] if
] if
@currentNode move @mresult.@memory.pushBack
] each
@current.@nodes move @mresult.@nodes.pushBack
shift current.memory.dataSize + @shift set
] each
];