about summary refs log tree commit diff stats
path: root/html/060string.mu.html
blob: 193f146368ce69867434fe069f077826872fbebe (plain) (blame)
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
<!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>~/Desktop/s/mu/060string.mu</title>
<meta name="Generator" content="Vim/7.4">
<meta name="plugin-version" content="vim7.4_v1">
<meta name="syntax" content="none">
<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: #000000; }
body { font-family: monospace; color: #d0d0d0; background-color: #000000; }
* { font-size: 1em; }
.CommentedCode { color: #6c6c6c; }
.Delimiter { color: #c000c0; }
.Comment { color: #8080ff; }
.Constant { color: #008080; }
.Special { color: #ff6060; }
.Identifier { color: #008080; }
-->
</style>

<script type='text/javascript'>
<!--

-->
</script>
</head>
<body>
<pre id='vimCodeElement'>
<span class="Comment"># Some useful helpers for dealing with strings.</span>

recipe string-equal [
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  a:address:array:character<span class="Special"> &lt;- </span>next-ingredient
  a-len:number<span class="Special"> &lt;- </span>length a:address:array:character/deref
  b:address:array:character<span class="Special"> &lt;- </span>next-ingredient
  b-len:number<span class="Special"> &lt;- </span>length b:address:array:character/deref
  <span class="Comment"># compare lengths</span>
  <span class="Delimiter">{</span>
    trace <span class="Constant">[string-equal]</span>, <span class="Constant">[comparing lengths]</span>
    length-equal?:boolean<span class="Special"> &lt;- </span>equal a-len:number, b-len:number
    <span class="Identifier">break-if</span> length-equal?:boolean
    <span class="Identifier">reply</span> <span class="Constant">0:literal</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># compare each corresponding character</span>
  trace <span class="Constant">[string-equal]</span>, <span class="Constant">[comparing characters]</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i:number, a-len:number
    <span class="Identifier">break-if</span> done?:boolean
    a2:character<span class="Special"> &lt;- </span>index a:address:array:character/deref, i:number
    b2:character<span class="Special"> &lt;- </span>index b:address:array:character/deref, i:number
    <span class="Delimiter">{</span>
      chars-match?:boolean<span class="Special"> &lt;- </span>equal a2:character, b2:character
      <span class="Identifier">break-if</span> chars-match?:boolean
      <span class="Identifier">reply</span> <span class="Constant">0:literal</span>
    <span class="Delimiter">}</span>
    i:number<span class="Special"> &lt;- </span>add i:number, <span class="Constant">1:literal</span>
    <span class="Identifier">loop</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">reply</span> <span class="Constant">1:literal</span>
]

scenario string-equal-reflexive [
  run [
    default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
    x:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    3:boolean/<span class="Special">raw &lt;- </span>string-equal x:address:array:character, x:address:array:character
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># x == x for all x</span>
  ]
]

scenario string-equal-identical [
  run [
    default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
    x:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    y:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    3:boolean/<span class="Special">raw &lt;- </span>string-equal x:address:array:character, y:address:array:character
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>1  <span class="Comment"># abc == abc</span>
  ]
]

scenario string-equal-distinct-lengths [
  run [
    default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
    x:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    y:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcd]</span>
    3:boolean/<span class="Special">raw &lt;- </span>string-equal x:address:array:character, y:address:array:character
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># abc != abcd</span>
  ]
  trace-should-contain [
    string-equal: comparing lengths
  ]
  trace-should-not-contain [
    string-equal: comparing characters
  ]
]

scenario string-equal-with-empty [
  run [
    default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
    x:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
    y:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcd]</span>
    3:boolean/<span class="Special">raw &lt;- </span>string-equal x:address:array:character, y:address:array:character
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># &quot;&quot; != abcd</span>
  ]
]

scenario string-equal-common-lengths-but-distinct [
  run [
    default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
    x:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    y:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abd]</span>
    3:boolean/<span class="Special">raw &lt;- </span>string-equal x:address:array:character, y:address:array:character
  ]
  memory-should-contain [
    3<span class="Special"> &lt;- </span>0  <span class="Comment"># abc != abd</span>
  ]
]

<span class="Comment"># A new type to help incrementally construct strings.</span>
container buffer [
  length:number
  data:address:array:character
]

recipe init-buffer [
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
<span class="CommentedCode">#?   $print default-space:address:array:location, [</span>
<span class="CommentedCode">#? ]</span>
  result:address:buffer<span class="Special"> &lt;- </span>new buffer:type
  len:address:number<span class="Special"> &lt;- </span>get-address result:address:buffer/deref, length:offset
  len:address:number/deref<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  s:address:address:array:character<span class="Special"> &lt;- </span>get-address result:address:buffer/deref, data:offset
  capacity:number<span class="Special"> &lt;- </span>next-ingredient
  s:address:address:array:character/deref<span class="Special"> &lt;- </span>new character:type, capacity:number
<span class="CommentedCode">#?   $print s:address:address:array:character/deref, [</span>
<span class="CommentedCode">#? ]</span>
  <span class="Identifier">reply</span> result:address:buffer
]

recipe grow-buffer [
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  in:address:buffer<span class="Special"> &lt;- </span>next-ingredient
  <span class="Comment"># double buffer size</span>
  x:address:address:array:character<span class="Special"> &lt;- </span>get-address in:address:buffer/deref, data:offset
  oldlen:number<span class="Special"> &lt;- </span>length x:address:address:array:character/deref/deref
  newlen:number<span class="Special"> &lt;- </span>multiply oldlen:number, <span class="Constant">2:literal</span>
  olddata:address:array:character<span class="Special"> &lt;- </span>copy x:address:address:array:character/deref
  x:address:address:array:character/deref<span class="Special"> &lt;- </span>new character:type, newlen:number
  <span class="Comment"># copy old contents</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i:number, oldlen:number
    <span class="Identifier">break-if</span> done?:boolean
    src:character<span class="Special"> &lt;- </span>index olddata:address:array:character/deref, i:number
    dest:address:character<span class="Special"> &lt;- </span>index-address x:address:address:array:character/deref/deref, i:number
    dest:address:character/deref<span class="Special"> &lt;- </span>copy src:character
    i:number<span class="Special"> &lt;- </span>add i:number, <span class="Constant">1:literal</span>
    <span class="Identifier">loop</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">reply</span> in:address:buffer
]

recipe buffer-full? [
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  in:address:buffer<span class="Special"> &lt;- </span>next-ingredient
  len:number<span class="Special"> &lt;- </span>get in:address:buffer/deref, length:offset
  s:address:array:character<span class="Special"> &lt;- </span>get in:address:buffer/deref, data:offset
  capacity:number<span class="Special"> &lt;- </span>length s:address:array:character/deref
  result:boolean<span class="Special"> &lt;- </span>greater-or-equal len:number, capacity:number
  <span class="Identifier">reply</span> result:boolean
]

<span class="Comment"># in:address:buffer &lt;- buffer-append in:address:buffer, c:character</span>
recipe buffer-append [
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  in:address:buffer<span class="Special"> &lt;- </span>next-ingredient
  c:character<span class="Special"> &lt;- </span>next-ingredient
  <span class="Delimiter">{</span>
    <span class="Comment"># grow buffer if necessary</span>
    full?:boolean<span class="Special"> &lt;- </span>buffer-full? in:address:buffer
    <span class="Identifier">break-unless</span> full?:boolean
    in:address:buffer<span class="Special"> &lt;- </span>grow-buffer in:address:buffer
  <span class="Delimiter">}</span>
  len:address:number<span class="Special"> &lt;- </span>get-address in:address:buffer/deref, length:offset
  s:address:array:character<span class="Special"> &lt;- </span>get in:address:buffer/deref, data:offset
  dest:address:character<span class="Special"> &lt;- </span>index-address s:address:array:character/deref, len:address:number/deref
  dest:address:character/deref<span class="Special"> &lt;- </span>copy c:character
  len:address:number/deref<span class="Special"> &lt;- </span>add len:address:number/deref, <span class="Constant">1:literal</span>
  <span class="Identifier">reply</span> in:address:buffer/same-as-ingredient:0
]

scenario buffer-append-works [
  run [
    default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
    x:address:buffer<span class="Special"> &lt;- </span>init-buffer <span class="Constant">3:literal</span>
    s1:address:array:character<span class="Special"> &lt;- </span>get x:address:buffer/deref, data:offset
    x:address:buffer<span class="Special"> &lt;- </span>buffer-append x:address:buffer, <span class="Constant">97:literal</span>  <span class="Comment"># 'a'</span>
    x:address:buffer<span class="Special"> &lt;- </span>buffer-append x:address:buffer, <span class="Constant">98:literal</span>  <span class="Comment"># 'b'</span>
    x:address:buffer<span class="Special"> &lt;- </span>buffer-append x:address:buffer, <span class="Constant">99:literal</span>  <span class="Comment"># 'c'</span>
    s2:address:array:character<span class="Special"> &lt;- </span>get x:address:buffer/deref, data:offset
    1:boolean/<span class="Special">raw &lt;- </span>equal s1:address:array:character, s2:address:array:character
<span class="CommentedCode">#?     $print s2:address:array:character, [</span>
<span class="CommentedCode">#? ]</span>
<span class="CommentedCode">#?     $print 1060:number/raw, [</span>
<span class="CommentedCode">#? ]</span>
<span class="CommentedCode">#?     $print 1061:number/raw, [</span>
<span class="CommentedCode">#? ]</span>
<span class="CommentedCode">#?     $print 1062:number/raw, [</span>
<span class="CommentedCode">#? ]</span>
<span class="CommentedCode">#?     $print 1063:number/raw, [</span>
<span class="CommentedCode">#? ]</span>
<span class="CommentedCode">#?     $print 1064:number/raw, [</span>
<span class="CommentedCode">#? ]</span>
<span class="CommentedCode">#?     $print 1065:number/raw, [</span>
<span class="CommentedCode">#? ]</span>
    2:array:character/<span class="Special">raw &lt;- </span>copy s2:address:array:character/deref
   <span class="Identifier"> +buffer-filled</span>
    x:address:buffer<span class="Special"> &lt;- </span>buffer-append x:address:buffer, <span class="Constant">100:literal</span>  <span class="Comment"># 'd'</span>
    s3:address:array:character<span class="Special"> &lt;- </span>get x:address:buffer/deref, data:offset
    10:boolean/<span class="Special">raw &lt;- </span>equal s1:address:array:character, s3:address:array:character
    11:number/<span class="Special">raw &lt;- </span>get x:address:buffer/deref, length:offset
    12:array:character/<span class="Special">raw &lt;- </span>copy s3:address:array:character/deref
  ]
  memory-should-contain [
    <span class="Comment"># before +buffer-filled</span>
    1<span class="Special"> &lt;- </span>1   <span class="Comment"># no change in data pointer</span>
    2<span class="Special"> &lt;- </span>3   <span class="Comment"># size of data</span>
    3<span class="Special"> &lt;- </span>97  <span class="Comment"># data</span>
    4<span class="Special"> &lt;- </span>98
    5<span class="Special"> &lt;- </span>99
    <span class="Comment"># in the end</span>
    10<span class="Special"> &lt;- </span>0   <span class="Comment"># data pointer has grown</span>
    11<span class="Special"> &lt;- </span>4   <span class="Comment"># final length</span>
    12<span class="Special"> &lt;- </span>6   <span class="Comment"># but data's capacity has doubled</span>
    13<span class="Special"> &lt;- </span>97  <span class="Comment"># data</span>
    14<span class="Special"> &lt;- </span>98
    15<span class="Special"> &lt;- </span>99
    16<span class="Special"> &lt;- </span>100
    17<span class="Special"> &lt;- </span>0
    18<span class="Special"> &lt;- </span>0
  ]
]

<span class="Comment"># result:address:array:character &lt;- integer-to-decimal-string n:number</span>
recipe integer-to-decimal-string [
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  n:number<span class="Special"> &lt;- </span>next-ingredient
  <span class="Comment"># is it zero?</span>
  <span class="Delimiter">{</span>
    <span class="Identifier">break-if</span> n:number
    result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[0]</span>
    <span class="Identifier">reply</span> result:address:array:character
  <span class="Delimiter">}</span>
  <span class="Comment"># save sign</span>
  negate-result:boolean<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    negative?:boolean<span class="Special"> &lt;- </span>lesser-than n:number, <span class="Constant">0:literal</span>
    <span class="Identifier">break-unless</span> negative?:boolean
    negate-result:boolean<span class="Special"> &lt;- </span>copy <span class="Constant">1:literal</span>
    n:number<span class="Special"> &lt;- </span>multiply n:number,<span class="Identifier"> -1</span>:literal
  <span class="Delimiter">}</span>
  <span class="Comment"># add digits from right to left into intermediate buffer</span>
  tmp:address:buffer<span class="Special"> &lt;- </span>init-buffer <span class="Constant">30:literal</span>
  digit-base:number<span class="Special"> &lt;- </span>copy <span class="Constant">48:literal</span>  <span class="Comment"># '0'</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>equal n:number, <span class="Constant">0:literal</span>
    <span class="Identifier">break-if</span> done?:boolean
    n:number, digit:number<span class="Special"> &lt;- </span>divide-with-remainder n:number, <span class="Constant">10:literal</span>
    c:character<span class="Special"> &lt;- </span>add digit-base:number, digit:number
    tmp:address:buffer<span class="Special"> &lt;- </span>buffer-append tmp:address:buffer, c:character
    <span class="Identifier">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># add sign</span>
  <span class="Delimiter">{</span>
    <span class="Identifier">break-unless</span> negate-result:boolean
    tmp:address:buffer<span class="Special"> &lt;- </span>buffer-append tmp:address:buffer, <span class="Constant">45:literal</span>  <span class="Comment"># '-'</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># reverse buffer into string result</span>
  len:number<span class="Special"> &lt;- </span>get tmp:address:buffer/deref, length:offset
  buf:address:array:character<span class="Special"> &lt;- </span>get tmp:address:buffer/deref, data:offset
  result:address:array:character<span class="Special"> &lt;- </span>new character:type, len:number
  i:number<span class="Special"> &lt;- </span>subtract len:number, <span class="Constant">1:literal</span>
  j:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while i &gt;= 0</span>
    done?:boolean<span class="Special"> &lt;- </span>lesser-than i:number, <span class="Constant">0:literal</span>
    <span class="Identifier">break-if</span> done?:boolean
    <span class="Comment"># result[j] = tmp[i]</span>
    src:character<span class="Special"> &lt;- </span>index buf:address:array:character/deref, i:number
    dest:address:character<span class="Special"> &lt;- </span>index-address result:address:array:character/deref, j:number
    dest:address:character/deref<span class="Special"> &lt;- </span>copy src:character
    <span class="Comment"># ++i</span>
    i:number<span class="Special"> &lt;- </span>subtract i:number, <span class="Constant">1:literal</span>
    <span class="Comment"># --j</span>
    j:number<span class="Special"> &lt;- </span>add j:number, <span class="Constant">1:literal</span>
    <span class="Identifier">loop</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">reply</span> result:address:array:character
]

scenario integer-to-decimal-digit-zero [
  run [
    1:address:array:character/<span class="Special">raw &lt;- </span>integer-to-decimal-string <span class="Constant">0:literal</span>
    2:array:character/<span class="Special">raw &lt;- </span>copy 1:address:array:character/deref/<span class="Special">raw</span>
  ]
  memory-should-contain [
    2:string<span class="Special"> &lt;- </span><span class="Constant">[0]</span>
  ]
]

scenario integer-to-decimal-digit-positive [
  run [
    1:address:array:character/<span class="Special">raw &lt;- </span>integer-to-decimal-string <span class="Constant">234:literal</span>
    2:array:character/<span class="Special">raw &lt;- </span>copy 1:address:array:character/deref/<span class="Special">raw</span>
  ]
  memory-should-contain [
    2:string<span class="Special"> &lt;- </span><span class="Constant">[234]</span>
  ]
]

scenario integer-to-decimal-digit-negative [
  run [
    1:address:array:character/<span class="Special">raw &lt;- </span>integer-to-decimal-string<span class="Identifier"> -1</span>:literal
    2:array:character/<span class="Special">raw &lt;- </span>copy 1:address:array:character/deref/<span class="Special">raw</span>
  ]
  memory-should-contain [
    2<span class="Special"> &lt;- </span>2
    3<span class="Special"> &lt;- </span>45  <span class="Comment"># '-'</span>
    4<span class="Special"> &lt;- </span>49  <span class="Comment"># '1'</span>
  ]
]

recipe string-append [
  default-space:address:array:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">30:literal</span>
  <span class="Comment"># result = new string[a.length + b.length]</span>
  a:address:array:character<span class="Special"> &lt;- </span>next-ingredient
  a-len:number<span class="Special"> &lt;- </span>length a:address:array:character/deref
  b:address:array:character<span class="Special"> &lt;- </span>next-ingredient
  b-len:number<span class="Special"> &lt;- </span>length b:address:array:character/deref
  result-len:number<span class="Special"> &lt;- </span>add a-len:number, b-len:number
  result:address:array:character<span class="Special"> &lt;- </span>new character:type, result-len:number
  <span class="Comment"># copy a into result</span>
  result-idx:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while i &lt; a.length</span>
    a-done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i:number, a-len:number
    <span class="Identifier">break-if</span> a-done?:boolean
    <span class="Comment"># result[result-idx] = a[i]</span>
    out:address:character<span class="Special"> &lt;- </span>index-address result:address:array:character/deref, result-idx:number
    in:character<span class="Special"> &lt;- </span>index a:address:array:character/deref, i:number
    out:address:character/deref<span class="Special"> &lt;- </span>copy in:character
    <span class="Comment"># ++i</span>
    i:number<span class="Special"> &lt;- </span>add i:number, <span class="Constant">1:literal</span>
    <span class="Comment"># ++result-idx</span>
    result-idx:number<span class="Special"> &lt;- </span>add result-idx:number, <span class="Constant">1:literal</span>
    <span class="Identifier">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># copy b into result</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while i &lt; b.length</span>
    b-done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i:number, b-len:number
    <span class="Identifier">break-if</span> b-done?:boolean
    <span class="Comment"># result[result-idx] = a[i]</span>
    out:address:character<span class="Special"> &lt;- </span>index-address result:address:array:character/deref, result-idx:number
    in:character<span class="Special"> &lt;- </span>index b:address:array:character/deref, i:number
    out:address:character/deref<span class="Special"> &lt;- </span>copy in:character
    <span class="Comment"># ++i</span>
    i:number<span class="Special"> &lt;- </span>add i:number, <span class="Constant">1:literal</span>
    <span class="Comment"># ++result-idx</span>
    result-idx:number<span class="Special"> &lt;- </span>add result-idx:number, <span class="Constant">1:literal</span>
    <span class="Identifier">loop</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">reply</span> result:address:array:character
]

scenario string-append-1 [
  run [
    1:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[hello,]</span>
    2:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[ world!]</span>
    3:address:array:character/<span class="Special">raw &lt;- </span>string-append 1:address:array:character/<span class="Special">raw</span>, 2:address:array:character/<span class="Special">raw</span>
    4:array:character/<span class="Special">raw &lt;- </span>copy 3:address:array:character/<span class="Special">raw</span>/deref
  ]
  memory-should-contain [
    4:string<span class="Special"> &lt;- </span><span class="Constant">[hello, world!]</span>
  ]
]

<span class="Comment"># replace underscores in first with remaining args</span>
<span class="Comment"># result:address:array:character &lt;- interpolate template:address:array:character, ...</span>
recipe interpolate [
  default-space:array:address:location<span class="Special"> &lt;- </span>new location:type, <span class="Constant">60:literal</span>
  template:address:array:character<span class="Special"> &lt;- </span>next-ingredient
  <span class="Comment"># compute result-len, space to allocate for result</span>
  tem-len:number<span class="Special"> &lt;- </span>length template:address:array:character/deref
  result-len:number<span class="Special"> &lt;- </span>copy tem-len:number
  <span class="Delimiter">{</span>
    <span class="Comment"># while arg received</span>
    a:address:array:character, arg-received?:boolean<span class="Special"> &lt;- </span>next-ingredient
    <span class="Identifier">break-unless</span> arg-received?:boolean
    <span class="Comment"># result-len = result-len + arg.length - 1 for the 'underscore' being replaced</span>
    a-len:number<span class="Special"> &lt;- </span>length a:address:array:character/deref
    result-len:number<span class="Special"> &lt;- </span>add result-len:number, a-len:number
    result-len:number<span class="Special"> &lt;- </span>subtract result-len:number, <span class="Constant">1:literal</span>
    <span class="Identifier">loop</span>
  <span class="Delimiter">}</span>
<span class="CommentedCode">#?   $print tem-len:number, [ ], $result-len:number, [ </span>
<span class="CommentedCode">#? ] #? 1</span>
  rewind-ingredients
  _<span class="Special"> &lt;- </span>next-ingredient  <span class="Comment"># skip template</span>
  <span class="Comment"># result = new array:character[result-len]</span>
  result:address:array:character<span class="Special"> &lt;- </span>new character:type, result-len:number
  <span class="Comment"># repeatedly copy sections of template and 'holes' into result</span>
  result-idx:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while arg received</span>
    a:address:array:character, arg-received?:boolean<span class="Special"> &lt;- </span>next-ingredient
    <span class="Identifier">break-unless</span> arg-received?:boolean
    <span class="Comment"># copy template into result until '_'</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># while i &lt; template.length</span>
      tem-done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i:number, tem-len:number
      <span class="Identifier">break-if</span> tem-done?:boolean, 2:blocks
      <span class="Comment"># while template[i] != '_'</span>
      in:character<span class="Special"> &lt;- </span>index template:address:array:character/deref, i:number
      underscore?:boolean<span class="Special"> &lt;- </span>equal in:character, <span class="Constant">95:literal</span>  <span class="Comment"># '_'</span>
      <span class="Identifier">break-if</span> underscore?:boolean
      <span class="Comment"># result[result-idx] = template[i]</span>
      out:address:character<span class="Special"> &lt;- </span>index-address result:address:array:character/deref, result-idx:number
      out:address:character/deref<span class="Special"> &lt;- </span>copy in:character
      <span class="Comment"># ++i</span>
      i:number<span class="Special"> &lt;- </span>add i:number, <span class="Constant">1:literal</span>
      <span class="Comment"># ++result-idx</span>
      result-idx:number<span class="Special"> &lt;- </span>add result-idx:number, <span class="Constant">1:literal</span>
      <span class="Identifier">loop</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># copy 'a' into result</span>
    j:number<span class="Special"> &lt;- </span>copy <span class="Constant">0:literal</span>
    <span class="Delimiter">{</span>
      <span class="Comment"># while j &lt; a.length</span>
      arg-done?:boolean<span class="Special"> &lt;- </span>greater-or-equal j:number, a-len:number
      <span class="Identifier">break-if</span> arg-done?:boolean
      <span class="Comment"># result[result-idx] = a[j]</span>
      in:character<span class="Special"> &lt;- </span>index a:address:array:character/deref, j:number
      out:address:character<span class="Special"> &lt;- </span>index-address result:address:array:character/deref, result-idx:number
      out:address:character/deref<span class="Special"> &lt;- </span>copy in:character
      <span class="Comment"># ++j</span>
      j:number<span class="Special"> &lt;- </span>add j:number, <span class="Constant">1:literal</span>
      <span class="Comment"># ++result-idx</span>
      result-idx:number<span class="Special"> &lt;- </span>add result-idx:number, <span class="Constant">1:literal</span>
      <span class="Identifier">loop</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># skip '_' in template</span>
    i:number<span class="Special"> &lt;- </span>add i:number, <span class="Constant">1:literal</span>
    <span class="Identifier">loop</span>  <span class="Comment"># interpolate next arg</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># done with holes; copy rest of template directly into result</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while i &lt; template.length</span>
    tem-done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i:number, tem-len:number
    <span class="Identifier">break-if</span> tem-done?:boolean
    <span class="Comment"># result[result-idx] = template[i]</span>
    in:character<span class="Special"> &lt;- </span>index template:address:array:character/deref, i:number
    out:address:character<span class="Special"> &lt;- </span>index-address result:address:array:character/deref, result-idx:number
    out:address:character/deref<span class="Special"> &lt;- </span>copy in:character
    <span class="Comment"># ++i</span>
    i:number<span class="Special"> &lt;- </span>add i:number, <span class="Constant">1:literal</span>
    <span class="Comment"># ++result-idx</span>
    result-idx:number<span class="Special"> &lt;- </span>add result-idx:number, <span class="Constant">1:literal</span>
    <span class="Identifier">loop</span>
  <span class="Delimiter">}</span>
  <span class="Identifier">reply</span> result:address:array:character
]

scenario interpolate-works [
<span class="CommentedCode">#?   dump run #? 1</span>
  run [
    1:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[abc _]</span>
    2:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[def]</span>
    3:address:array:character/<span class="Special">raw &lt;- </span>interpolate 1:address:array:character/<span class="Special">raw</span>, 2:address:array:character/<span class="Special">raw</span>
    4:array:character/<span class="Special">raw &lt;- </span>copy 3:address:array:character/<span class="Special">raw</span>/deref
  ]
  memory-should-contain [
    4:string<span class="Special"> &lt;- </span><span class="Constant">[abc def]</span>
  ]
]

scenario interpolate-at-start [
  run [
    1:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[_, hello!]</span>
    2:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[abc]</span>
    3:address:array:character/<span class="Special">raw &lt;- </span>interpolate 1:address:array:character/<span class="Special">raw</span>, 2:address:array:character/<span class="Special">raw</span>
    4:array:character/<span class="Special">raw &lt;- </span>copy 3:address:array:character/<span class="Special">raw</span>/deref
  ]
  memory-should-contain [
    4:string<span class="Special"> &lt;- </span><span class="Constant">[abc, hello!]</span>
    16<span class="Special"> &lt;- </span>0  <span class="Comment"># out of bounds</span>
  ]
]

scenario interpolate-at-end [
  run [
    1:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[hello, _]</span>
    2:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[abc]</span>
    3:address:array:character/<span class="Special">raw &lt;- </span>interpolate 1:address:array:character/<span class="Special">raw</span>, 2:address:array:character/<span class="Special">raw</span>
    4:array:character/<span class="Special">raw &lt;- </span>copy 3:address:array:character/<span class="Special">raw</span>/deref
  ]
  memory-should-contain [
    4:string<span class="Special"> &lt;- </span><span class="Constant">[hello, abc]</span>
  ]
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->