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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - 030container.cc</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css,pre_wrap,no_foldcolumn,expand_tabs,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #d0d0d0; background-color: #080808; }
body { font-family: monospace; color: #d0d0d0; background-color: #080808; }
* { font-size: 1em; }
.traceContains { color: #008000; }
.cSpecial { color: #008000; }
.Constant { color: #008080; }
.SalientComment { color: #00ffff; }
.traceAbsent { color: #c00000; }
.Comment { color: #8080ff; }
.Delimiter { color: #c000c0; }
.Special { color: #ff6060; }
.Identifier { color: #008080; }
.CommentedCode { color: #6c6c6c; }
-->
</style>
<script type='text/javascript'>
<!--
-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment">//: Containers contain a fixed number of elements of different types.</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
<span class="Comment">//: We'll use this container as a running example, with two number elements.</span>
type_number point = Type_number[<span class="Constant">"point"</span>] = Next_type_number++<span class="Delimiter">;</span>
Type[point]<span class="Delimiter">.</span>size = <span class="Constant">2</span><span class="Delimiter">;</span>
Type[point]<span class="Delimiter">.</span>kind = container<span class="Delimiter">;</span>
Type[point]<span class="Delimiter">.</span>name = <span class="Constant">"point"</span><span class="Delimiter">;</span>
vector<type_number> i<span class="Delimiter">;</span>
i<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>number<span class="Delimiter">);</span>
Type[point]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
Type[point]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>i<span class="Delimiter">);</span>
<span class="Comment">//: Containers can be copied around with a single instruction just like</span>
<span class="Comment">//: numbers, no matter how large they are.</span>
<span class="Comment">//: Tests in this layer often explicitly setup memory before reading it as a</span>
<span class="Comment">//: container. Don't do this in general. I'm tagging exceptions with /raw to</span>
<span class="Comment">//: avoid warnings.</span>
<span class="Delimiter">:(scenario copy_multiple_locations)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">3</span>:point<span class="Special"> <- </span>copy <span class="Constant">1</span>:point/<span class="Special">raw</span> <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 34 in location 3</span>
<span class="traceContains">+mem: storing 35 in location 4</span>
<span class="Delimiter">:(before "End Mu Types Initialization")</span>
<span class="Comment">// A more complex container, containing another container as one of its</span>
<span class="Comment">// elements.</span>
type_number point_number = Type_number[<span class="Constant">"point-number"</span>] = Next_type_number++<span class="Delimiter">;</span>
Type[point_number]<span class="Delimiter">.</span>size = <span class="Constant">2</span><span class="Delimiter">;</span>
Type[point_number]<span class="Delimiter">.</span>kind = container<span class="Delimiter">;</span>
Type[point_number]<span class="Delimiter">.</span>name = <span class="Constant">"point-number"</span><span class="Delimiter">;</span>
vector<type_number> p2<span class="Delimiter">;</span>
p2<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>point<span class="Delimiter">);</span>
Type[point_number]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>p2<span class="Delimiter">);</span>
vector<type_number> i2<span class="Delimiter">;</span>
i2<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>number<span class="Delimiter">);</span>
Type[point_number]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>i2<span class="Delimiter">);</span>
<span class="Delimiter">:(scenario copy_handles_nested_container_elements)</span>
recipe main [
<span class="Constant">12</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal
<span class="Constant">13</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">14</span>:number<span class="Special"> <- </span>copy <span class="Constant">36</span>:literal
<span class="Constant">15</span>:point-number<span class="Special"> <- </span>copy <span class="Constant">12</span>:point-number/<span class="Special">raw</span> <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 36 in location 17</span>
<span class="Comment">//: Containers can be checked for equality with a single instruction just like</span>
<span class="Comment">//: numbers, no matter how large they are.</span>
<span class="Delimiter">:(scenario compare_multiple_locations)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal <span class="Comment"># first</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">36</span>:literal
<span class="Constant">4</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal <span class="Comment"># second</span>
<span class="Constant">5</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">6</span>:number<span class="Special"> <- </span>copy <span class="Constant">36</span>:literal
<span class="Constant">7</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">1</span>:point-number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">4</span>:point-number/<span class="Special">raw</span> <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 1 in location 7</span>
<span class="Delimiter">:(scenario compare_multiple_locations2)</span>
recipe main [
<span class="Constant">1</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal <span class="Comment"># first</span>
<span class="Constant">2</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">3</span>:number<span class="Special"> <- </span>copy <span class="Constant">36</span>:literal
<span class="Constant">4</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal <span class="Comment"># second</span>
<span class="Constant">5</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">6</span>:number<span class="Special"> <- </span>copy <span class="Constant">37</span>:literal <span class="Comment"># different</span>
<span class="Constant">7</span>:boolean<span class="Special"> <- </span>equal <span class="Constant">1</span>:point-number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">4</span>:point-number/<span class="Special">raw</span> <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 0 in location 7</span>
<span class="Delimiter">:(before "End size_of(types) Cases")</span>
type_info t = Type[types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span>]<span class="Delimiter">;</span>
if <span class="Delimiter">(</span>t<span class="Delimiter">.</span>kind == container<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// size of a container is the sum of the sizes of its elements</span>
long long int result = <span class="Constant">0</span><span class="Delimiter">;</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>t<span class="Delimiter">.</span>elements<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="Comment">// todo: strengthen assertion to disallow mutual type recursion</span>
assert<span class="Delimiter">(</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">)</span> != t<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">));</span>
result += size_of<span class="Delimiter">(</span>t<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Identifier">return</span> result<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: To access elements of a container, use 'get'</span>
<span class="Delimiter">:(scenario get)</span>
recipe main [
<span class="Constant">12</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal
<span class="Constant">13</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">15</span>:number<span class="Special"> <- </span>get <span class="Constant">12</span>:point/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:offset <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 35 in location 15</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
GET<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_number[<span class="Constant">"get"</span>] = GET<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case GET: <span class="Delimiter">{</span>
reagent base = current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
long long int base_address = base<span class="Delimiter">.</span>value<span class="Delimiter">;</span>
type_number base_type = base<span class="Delimiter">.</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>Type[base_type]<span class="Delimiter">.</span>kind == container<span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>isa_literal<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)));</span>
assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)));</span>
long long int offset = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
long long int src = base_address<span class="Delimiter">;</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < offset<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
src += size_of<span class="Delimiter">(</span>Type[base_type]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
trace<span class="Delimiter">(</span>Primitive_recipe_depth<span class="Delimiter">,</span> <span class="Constant">"run"</span><span class="Delimiter">)</span> << <span class="Constant">"address to copy is "</span> << src<span class="Delimiter">;</span>
assert<span class="Delimiter">(</span>Type[base_type]<span class="Delimiter">.</span>kind == container<span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>Type[base_type]<span class="Delimiter">.</span>elements<span class="Delimiter">)</span> > offset<span class="Delimiter">);</span>
type_number src_type = Type[base_type]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>offset<span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
trace<span class="Delimiter">(</span>Primitive_recipe_depth<span class="Delimiter">,</span> <span class="Constant">"run"</span><span class="Delimiter">)</span> << <span class="Constant">"its type is "</span> << src_type<span class="Delimiter">;</span>
reagent tmp<span class="Delimiter">;</span>
tmp<span class="Delimiter">.</span>set_value<span class="Delimiter">(</span>src<span class="Delimiter">);</span>
tmp<span class="Delimiter">.</span>types<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>src_type<span class="Delimiter">);</span>
products<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>read_memory<span class="Delimiter">(</span>tmp<span class="Delimiter">));</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario get_handles_nested_container_elements)</span>
recipe main [
<span class="Constant">12</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal
<span class="Constant">13</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">14</span>:number<span class="Special"> <- </span>copy <span class="Constant">36</span>:literal
<span class="Constant">15</span>:number<span class="Special"> <- </span>get <span class="Constant">12</span>:point-number/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:offset <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 36 in location 15</span>
<span class="SalientComment">//:: To write to elements of containers, you need their address.</span>
<span class="Delimiter">:(scenario get_address)</span>
recipe main [
<span class="Constant">12</span>:number<span class="Special"> <- </span>copy <span class="Constant">34</span>:literal
<span class="Constant">13</span>:number<span class="Special"> <- </span>copy <span class="Constant">35</span>:literal
<span class="Constant">15</span>:address:number<span class="Special"> <- </span>get-address <span class="Constant">12</span>:point/<span class="Special">raw</span><span class="Delimiter">,</span> <span class="Constant">1</span>:offset <span class="Comment"># unsafe</span>
]
<span class="traceContains">+mem: storing 13 in location 15</span>
<span class="Delimiter">:(before "End Primitive Recipe Declarations")</span>
GET_ADDRESS<span class="Delimiter">,</span>
<span class="Delimiter">:(before "End Primitive Recipe Numbers")</span>
Recipe_number[<span class="Constant">"get-address"</span>] = GET_ADDRESS<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Primitive Recipe Implementations")</span>
case GET_ADDRESS: <span class="Delimiter">{</span>
reagent base = current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
long long int base_address = base<span class="Delimiter">.</span>value<span class="Delimiter">;</span>
type_number base_type = base<span class="Delimiter">.</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>Type[base_type]<span class="Delimiter">.</span>kind == container<span class="Delimiter">);</span>
assert<span class="Delimiter">(</span>isa_literal<span class="Delimiter">(</span>current_instruction<span class="Delimiter">().</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)));</span>
assert<span class="Delimiter">(</span>scalar<span class="Delimiter">(</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">)));</span>
long long int offset = ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">).</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">);</span>
long long int result = base_address<span class="Delimiter">;</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < offset<span class="Delimiter">;</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
result += size_of<span class="Delimiter">(</span>Type[base_type]<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
trace<span class="Delimiter">(</span>Primitive_recipe_depth<span class="Delimiter">,</span> <span class="Constant">"run"</span><span class="Delimiter">)</span> << <span class="Constant">"address to copy is "</span> << result<span class="Delimiter">;</span>
products<span class="Delimiter">.</span>resize<span class="Delimiter">(</span><span class="Constant">1</span><span class="Delimiter">);</span>
products<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>push_back<span class="Delimiter">(</span>result<span class="Delimiter">);</span>
<span class="Identifier">break</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: Allow containers to be defined in mu code.</span>
<span class="Delimiter">:(scenarios load)</span>
<span class="Delimiter">:(scenario container)</span>
container foo [
x:number
y:number
]
<span class="traceContains">+parse: reading container foo</span>
<span class="traceContains">+parse: element name: x</span>
<span class="traceContains">+parse: type: 1</span>
<span class="traceContains">+parse: element name: y</span>
<span class="traceContains">+parse: type: 1</span>
<span class="Delimiter">:(scenario container_use_before_definition)</span>
container foo [
x:number
y:bar
]
container bar [
x:number
y:number
]
<span class="traceContains">+parse: reading container foo</span>
<span class="traceContains">+parse: type number: 1000</span>
<span class="traceContains">+parse: element name: x</span>
<span class="traceContains">+parse: type: 1</span>
<span class="traceContains">+parse: element name: y</span>
<span class="traceContains">+parse: type: 1001</span>
<span class="traceContains">+parse: reading container bar</span>
<span class="traceContains">+parse: type number: 1001</span>
<span class="Delimiter">:(before "End Command Handlers")</span>
else if <span class="Delimiter">(</span>command == <span class="Constant">"container"</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
insert_container<span class="Delimiter">(</span>command<span class="Delimiter">,</span> container<span class="Delimiter">,</span> in<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(code)</span>
void insert_container<span class="Delimiter">(</span>const string& command<span class="Delimiter">,</span> kind_of_type kind<span class="Delimiter">,</span> istream& in<span class="Delimiter">)</span> <span class="Delimiter">{</span>
skip_whitespace<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
string name = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">"parse"</span><span class="Delimiter">)</span> << <span class="Constant">"reading "</span> << command << <span class="Constant">' '</span> << name<span class="Delimiter">;</span>
<span class="CommentedCode">//? cout << name << '\n'; //? 2</span>
<span class="CommentedCode">//? if (Type_number.find(name) != Type_number.end()) //? 1</span>
<span class="CommentedCode">//? cerr << Type_number[name] << '\n'; //? 1</span>
if <span class="Delimiter">(</span>Type_number<span class="Delimiter">.</span>find<span class="Delimiter">(</span>name<span class="Delimiter">)</span> == Type_number<span class="Delimiter">.</span>end<span class="Delimiter">()</span>
|| Type_number[name] == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
Type_number[name] = Next_type_number++<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
trace<span class="Delimiter">(</span><span class="Constant">"parse"</span><span class="Delimiter">)</span> << <span class="Constant">"type number: "</span> << Type_number[name]<span class="Delimiter">;</span>
skip_bracket<span class="Delimiter">(</span>in<span class="Delimiter">,</span> <span class="Constant">"'container' must begin with '['"</span><span class="Delimiter">);</span>
type_info& t = Type[Type_number[name]]<span class="Delimiter">;</span>
recently_added_types<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>Type_number[name]<span class="Delimiter">);</span>
t<span class="Delimiter">.</span>name = name<span class="Delimiter">;</span>
t<span class="Delimiter">.</span>kind = kind<span class="Delimiter">;</span>
while <span class="Delimiter">(</span>!in<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span>
skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
string element = next_word<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
if <span class="Delimiter">(</span>element == <span class="Constant">"]"</span><span class="Delimiter">)</span> <span class="Identifier">break</span><span class="Delimiter">;</span>
istringstream inner<span class="Delimiter">(</span>element<span class="Delimiter">);</span>
t<span class="Delimiter">.</span>element_names<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>slurp_until<span class="Delimiter">(</span>inner<span class="Delimiter">,</span> <span class="Constant">':'</span><span class="Delimiter">));</span>
trace<span class="Delimiter">(</span><span class="Constant">"parse"</span><span class="Delimiter">)</span> << <span class="Constant">" element name: "</span> << t<span class="Delimiter">.</span>element_names<span class="Delimiter">.</span>back<span class="Delimiter">();</span>
vector<type_number> types<span class="Delimiter">;</span>
while <span class="Delimiter">(</span>!inner<span class="Delimiter">.</span>eof<span class="Delimiter">())</span> <span class="Delimiter">{</span>
string type_name = slurp_until<span class="Delimiter">(</span>inner<span class="Delimiter">,</span> <span class="Constant">':'</span><span class="Delimiter">);</span>
if <span class="Delimiter">(</span>Type_number<span class="Delimiter">.</span>find<span class="Delimiter">(</span>type_name<span class="Delimiter">)</span> == Type_number<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? cerr << type_name << " is " << Next_type_number << '\n'; //? 1</span>
Type_number[type_name] = Next_type_number++<span class="Delimiter">;</span>
<span class="Delimiter">}</span>
types<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>Type_number[type_name]<span class="Delimiter">);</span>
trace<span class="Delimiter">(</span><span class="Constant">"parse"</span><span class="Delimiter">)</span> << <span class="Constant">" type: "</span> << types<span class="Delimiter">.</span>back<span class="Delimiter">();</span>
<span class="Delimiter">}</span>
t<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>types<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
assert<span class="Delimiter">(</span>SIZE<span class="Delimiter">(</span>t<span class="Delimiter">.</span>elements<span class="Delimiter">)</span> == SIZE<span class="Delimiter">(</span>t<span class="Delimiter">.</span>element_names<span class="Delimiter">));</span>
t<span class="Delimiter">.</span>size = SIZE<span class="Delimiter">(</span>t<span class="Delimiter">.</span>elements<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Comment">//: ensure types created in one scenario don't leak outside it.</span>
<span class="Delimiter">:(before "End Globals")</span>
vector<type_number> recently_added_types<span class="Delimiter">;</span>
<span class="Delimiter">:(before "End load_permanently")</span> <span class="Comment">//: for non-tests</span>
recently_added_types<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Delimiter">:(before "End Setup")</span> <span class="Comment">//: for tests</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>recently_added_types<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? cout << "erasing " << Type[recently_added_types.at(i)].name << '\n'; //? 1</span>
Type_number<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>Type[recently_added_types<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span>]<span class="Delimiter">.</span>name<span class="Delimiter">);</span>
Type<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>recently_added_types<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
recently_added_types<span class="Delimiter">.</span>clear<span class="Delimiter">();</span>
<span class="Comment">// delete recent type references</span>
<span class="Comment">// can't rely on recently_added_types to cleanup Type_number, because of deliberately misbehaving tests with references to undefined types</span>
map<string<span class="Delimiter">,</span> type_number>::iterator p = Type_number<span class="Delimiter">.</span>begin<span class="Delimiter">();</span>
while<span class="Delimiter">(</span>p != Type_number<span class="Delimiter">.</span>end<span class="Delimiter">())</span> <span class="Delimiter">{</span>
<span class="Comment">// save current item</span>
string name = p<span class="Delimiter">-></span>first<span class="Delimiter">;</span>
type_number t = p<span class="Delimiter">-></span>second<span class="Delimiter">;</span>
<span class="Comment">// increment iterator</span>
++p<span class="Delimiter">;</span>
<span class="Comment">// now delete current item if necessary</span>
if <span class="Delimiter">(</span>t >= <span class="Constant">1000</span><span class="Delimiter">)</span> <span class="Delimiter">{</span>
<span class="CommentedCode">//? cerr << "AAA " << name << " " << t << '\n'; //? 1</span>
Type_number<span class="Delimiter">.</span>erase<span class="Delimiter">(</span>name<span class="Delimiter">);</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Comment">//: lastly, ensure scenarios are consistent by always starting them at the</span>
<span class="Comment">//: same type number.</span>
Next_type_number = <span class="Constant">1000</span><span class="Delimiter">;</span>
<span class="Delimiter">:(before "End Test Run Initialization")</span>
assert<span class="Delimiter">(</span>Next_type_number < <span class="Constant">1000</span><span class="Delimiter">);</span>
<span class="Delimiter">:(before "End Setup")</span>
Next_type_number = <span class="Constant">1000</span><span class="Delimiter">;</span>
<span class="SalientComment">//:: Allow container definitions anywhere in the codebase, but warn if you</span>
<span class="SalientComment">//:: can't find a definition.</span>
<span class="Delimiter">:(scenarios run)</span>
<span class="Delimiter">:(scenario run_warns_on_unknown_types)</span>
<span class="Special">% Hide_warnings = true;</span>
<span class="CommentedCode">#? % Trace_stream->dump_layer = "run";</span>
recipe main [
<span class="Comment"># integer is not a type</span>
<span class="Constant">1</span>:integer<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
]
<span class="traceContains">+warn: unknown type: integer</span>
<span class="Delimiter">:(scenario run_allows_type_definition_after_use)</span>
<span class="Special">% Hide_warnings = true;</span>
recipe main [
<span class="Constant">1</span>:bar<span class="Special"> <- </span>copy <span class="Constant">0</span>:literal
]
container bar [
x:number
]
<span class="traceAbsent">-warn: unknown type: bar</span>
<span class="Delimiter">:(after "int main")</span>
Transform<span class="Delimiter">.</span>push_back<span class="Delimiter">(</span>check_invalid_types<span class="Delimiter">);</span>
<span class="Delimiter">:(code)</span>
void check_invalid_types<span class="Delimiter">(</span>const recipe_number r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
for <span class="Delimiter">(</span>long long int index = <span class="Constant">0</span><span class="Delimiter">;</span> index < SIZE<span class="Delimiter">(</span>Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">);</span> ++index<span class="Delimiter">)</span> <span class="Delimiter">{</span>
const instruction& inst = Recipe[r]<span class="Delimiter">.</span>steps<span class="Delimiter">.</span>at<span class="Delimiter">(</span>index<span class="Delimiter">);</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
check_invalid_types<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>ingredients<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
check_invalid_types<span class="Delimiter">(</span>inst<span class="Delimiter">.</span>products<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
void check_invalid_types<span class="Delimiter">(</span>const reagent& r<span class="Delimiter">)</span> <span class="Delimiter">{</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>r<span class="Delimiter">.</span>types<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>r<span class="Delimiter">.</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span> == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
if <span class="Delimiter">(</span>Type<span class="Delimiter">.</span>find<span class="Delimiter">(</span>r<span class="Delimiter">.</span>types<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">))</span> == Type<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
raise << <span class="Constant">"unknown type: "</span> << r<span class="Delimiter">.</span>properties<span class="Delimiter">.</span>at<span class="Delimiter">(</span><span class="Constant">0</span><span class="Delimiter">).</span>second<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span> << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">:(scenario container_unknown_field)</span>
<span class="Special">% Hide_warnings = true;</span>
container foo [
x:number
y:bar
]
<span class="traceContains">+warn: unknown type for field y in foo</span>
<span class="Delimiter">:(scenario read_container_with_bracket_in_comment)</span>
container foo [
x:number
<span class="Comment"># ']' in comment</span>
y:number
]
<span class="traceContains">+parse: reading container foo</span>
<span class="traceContains">+parse: element name: x</span>
<span class="traceContains">+parse: type: 1</span>
<span class="traceContains">+parse: element name: y</span>
<span class="traceContains">+parse: type: 1</span>
<span class="Delimiter">:(before "End Load Sanity Checks")</span>
check_container_field_types<span class="Delimiter">();</span>
<span class="Delimiter">:(code)</span>
void check_container_field_types<span class="Delimiter">()</span> <span class="Delimiter">{</span>
for <span class="Delimiter">(</span>map<type_number<span class="Delimiter">,</span> type_info>::iterator p = Type<span class="Delimiter">.</span>begin<span class="Delimiter">();</span> p != Type<span class="Delimiter">.</span>end<span class="Delimiter">();</span> ++p<span class="Delimiter">)</span> <span class="Delimiter">{</span>
const type_info& info = p<span class="Delimiter">-></span>second<span class="Delimiter">;</span>
<span class="CommentedCode">//? cerr << "checking " << p->first << '\n'; //? 1</span>
for <span class="Delimiter">(</span>long long int i = <span class="Constant">0</span><span class="Delimiter">;</span> i < SIZE<span class="Delimiter">(</span>info<span class="Delimiter">.</span>elements<span class="Delimiter">);</span> ++i<span class="Delimiter">)</span> <span class="Delimiter">{</span>
for <span class="Delimiter">(</span>long long int j = <span class="Constant">0</span><span class="Delimiter">;</span> j < SIZE<span class="Delimiter">(</span>info<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">));</span> ++j<span class="Delimiter">)</span> <span class="Delimiter">{</span>
if <span class="Delimiter">(</span>info<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>at<span class="Delimiter">(</span>j<span class="Delimiter">)</span> == <span class="Constant">0</span><span class="Delimiter">)</span> <span class="Identifier">continue</span><span class="Delimiter">;</span>
if <span class="Delimiter">(</span>Type<span class="Delimiter">.</span>find<span class="Delimiter">(</span>info<span class="Delimiter">.</span>elements<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">).</span>at<span class="Delimiter">(</span>j<span class="Delimiter">))</span> == Type<span class="Delimiter">.</span>end<span class="Delimiter">())</span>
raise << <span class="Constant">"unknown type for field "</span> << info<span class="Delimiter">.</span>element_names<span class="Delimiter">.</span>at<span class="Delimiter">(</span>i<span class="Delimiter">)</span> << <span class="Constant">" in "</span> << info<span class="Delimiter">.</span>name << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="Delimiter">}</span>
<span class="SalientComment">//:: helpers</span>
<span class="Delimiter">:(code)</span>
void skip_bracket<span class="Delimiter">(</span>istream& in<span class="Delimiter">,</span> string message<span class="Delimiter">)</span> <span class="Delimiter">{</span>
skip_whitespace_and_comments<span class="Delimiter">(</span>in<span class="Delimiter">);</span>
if <span class="Delimiter">(</span>in<span class="Delimiter">.</span>get<span class="Delimiter">()</span> != <span class="Constant">'['</span><span class="Delimiter">)</span>
raise << message << <span class="cSpecial">'\n'</span><span class="Delimiter">;</span>
<span class="Delimiter">}</span>
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->
|