about summary refs log tree commit diff stats
path: root/subx/teensy/test6-global.s
blob: a09eecaee3bd2cd7df244c82f079c26a2d3929e3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
; Example with a data segment.
; nasm -f elf test6-global.s
; gcc -Wall -s test6-global.o -o test6
BITS 32

SECTION .data
foo: dd 42

SECTION .text
GLOBAL main
main:
  mov eax, [foo]
  ret
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 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
<!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 - 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: #eeeeee; background-color: #080808; }
body { font-family: monospace; color: #eeeeee; background-color: #080808; }
* { font-size: 1.05em; }
.muRecipe { color: #ff8700; }
.muData { color: #ffff00; }
.muScenario { color: #00af00; }
.Comment { color: #9090ff; }
.Constant { color: #00a0a0; }
.Special { color: #ff6060; }
.Delimiter { color: #a04060; }
.muControl { color: #c0a020; }
-->
</style>

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

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

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

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

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

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

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

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

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

<span class="muRecipe">recipe</span> new-buffer [
  <span class="Constant">local-scope</span>
  result:address:buffer<span class="Special"> &lt;- </span>new <span class="Constant">buffer:type</span>
  len:address:number<span class="Special"> &lt;- </span>get-address *result, <span class="Constant">length:offset</span>
  *len:address:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  s:address:address:array:character<span class="Special"> &lt;- </span>get-address *result, <span class="Constant">data:offset</span>
  capacity:number, found?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  assert found?, <span class="Constant">[new-buffer must get a capacity argument]</span>
  *s<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, capacity
  <span class="muControl">reply</span> result
]

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

<span class="muRecipe">recipe</span> buffer-full? [
  <span class="Constant">local-scope</span>
  in:address:buffer<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  len:number<span class="Special"> &lt;- </span>get *in, <span class="Constant">length:offset</span>
  s:address:array:character<span class="Special"> &lt;- </span>get *in, <span class="Constant">data:offset</span>
  capacity:number<span class="Special"> &lt;- </span>length *s
  result:boolean<span class="Special"> &lt;- </span>greater-or-equal len, capacity
  <span class="muControl">reply</span> result
]

<span class="Comment"># in &lt;- buffer-append in:address:buffer, c:character</span>
<span class="muRecipe">recipe</span> buffer-append [
  <span class="Constant">local-scope</span>
  in:address:buffer<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  c:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  len:address:number<span class="Special"> &lt;- </span>get-address *in, <span class="Constant">length:offset</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># backspace? just drop last character if it exists and return</span>
    backspace?:boolean<span class="Special"> &lt;- </span>equal c, <span class="Constant">8/backspace</span>
    <span class="muControl">break-unless</span> backspace?
    empty?:boolean<span class="Special"> &lt;- </span>lesser-or-equal *len, <span class="Constant">0</span>
    <span class="muControl">reply-if</span> empty?, in/same-as-ingredient:<span class="Constant">0</span>
    *len<span class="Special"> &lt;- </span>subtract *len, <span class="Constant">1</span>
    <span class="muControl">reply</span> in/same-as-ingredient:<span class="Constant">0</span>
  <span class="Delimiter">}</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># grow buffer if necessary</span>
    full?:boolean<span class="Special"> &lt;- </span>buffer-full? in
    <span class="muControl">break-unless</span> full?
    in<span class="Special"> &lt;- </span>grow-buffer in
  <span class="Delimiter">}</span>
  s:address:array:character<span class="Special"> &lt;- </span>get *in, <span class="Constant">data:offset</span>
  dest:address:character<span class="Special"> &lt;- </span>index-address *s, *len
  *dest<span class="Special"> &lt;- </span>copy c
  *len<span class="Special"> &lt;- </span>add *len, <span class="Constant">1</span>
  <span class="muControl">reply</span> in/same-as-ingredient:<span class="Constant">0</span>
]

<span class="muScenario">scenario</span> buffer-append-works [
  run [
    <span class="Constant">local-scope</span>
    x:address:buffer<span class="Special"> &lt;- </span>new-buffer <span class="Constant">3</span>
    s1:address:array:character<span class="Special"> &lt;- </span>get *x:address:buffer, <span class="Constant">data:offset</span>
    x:address:buffer<span class="Special"> &lt;- </span>buffer-append x:address:buffer, <span class="Constant">97</span>  <span class="Comment"># 'a'</span>
    x:address:buffer<span class="Special"> &lt;- </span>buffer-append x:address:buffer, <span class="Constant">98</span>  <span class="Comment"># 'b'</span>
    x:address:buffer<span class="Special"> &lt;- </span>buffer-append x:address:buffer, <span class="Constant">99</span>  <span class="Comment"># 'c'</span>
    s2:address:array:character<span class="Special"> &lt;- </span>get *x:address:buffer, <span class="Constant">data:offset</span>
    <span class="Constant">1</span>:boolean/<span class="Special">raw &lt;- </span>equal s1:address:array:character, s2:address:array:character
    <span class="Constant">2</span>:array:character/<span class="Special">raw &lt;- </span>copy *s2:address:array:character
<span class="Constant">    +buffer-filled</span>
    x:address:buffer<span class="Special"> &lt;- </span>buffer-append x:address:buffer, <span class="Constant">100</span>  <span class="Comment"># 'd'</span>
    s3:address:array:character<span class="Special"> &lt;- </span>get *x:address:buffer, <span class="Constant">data:offset</span>
    <span class="Constant">10</span>:boolean/<span class="Special">raw &lt;- </span>equal s1:address:array:character, s3:address:array:character
    <span class="Constant">11</span>:number/<span class="Special">raw &lt;- </span>get *x:address:buffer, <span class="Constant">length:offset</span>
    <span class="Constant">12</span>:array:character/<span class="Special">raw &lt;- </span>copy *s3:address:array:character
  ]
  memory-should-contain [
    <span class="Comment"># before +buffer-filled</span>
    <span class="Constant">1</span><span class="Special"> &lt;- </span><span class="Constant">1</span>   <span class="Comment"># no change in data pointer</span>
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">3</span>   <span class="Comment"># size of data</span>
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">97</span>  <span class="Comment"># data</span>
    <span class="Constant">4</span><span class="Special"> &lt;- </span><span class="Constant">98</span>
    <span class="Constant">5</span><span class="Special"> &lt;- </span><span class="Constant">99</span>
    <span class="Comment"># in the end</span>
    <span class="Constant">10</span><span class="Special"> &lt;- </span><span class="Constant">0</span>   <span class="Comment"># data pointer has grown</span>
    <span class="Constant">11</span><span class="Special"> &lt;- </span><span class="Constant">4</span>   <span class="Comment"># final length</span>
    <span class="Constant">12</span><span class="Special"> &lt;- </span><span class="Constant">6</span>   <span class="Comment"># but data's capacity has doubled</span>
    <span class="Constant">13</span><span class="Special"> &lt;- </span><span class="Constant">97</span>  <span class="Comment"># data</span>
    <span class="Constant">14</span><span class="Special"> &lt;- </span><span class="Constant">98</span>
    <span class="Constant">15</span><span class="Special"> &lt;- </span><span class="Constant">99</span>
    <span class="Constant">16</span><span class="Special"> &lt;- </span><span class="Constant">100</span>
    <span class="Constant">17</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
    <span class="Constant">18</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> buffer-append-handles-backspace [
  run [
    <span class="Constant">local-scope</span>
    x:address:buffer<span class="Special"> &lt;- </span>new-buffer <span class="Constant">3</span>
    x<span class="Special"> &lt;- </span>buffer-append x, <span class="Constant">97</span>  <span class="Comment"># 'a'</span>
    x<span class="Special"> &lt;- </span>buffer-append x, <span class="Constant">98</span>  <span class="Comment"># 'b'</span>
    x<span class="Special"> &lt;- </span>buffer-append x, <span class="Constant">8/backspace</span>
    s:address:array:character<span class="Special"> &lt;- </span>buffer-to-array x
    <span class="Constant">1</span>:array:character/<span class="Special">raw &lt;- </span>copy *s
  ]
  memory-should-contain [
    <span class="Constant">1</span><span class="Special"> &lt;- </span><span class="Constant">1</span>   <span class="Comment"># length</span>
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">97</span>  <span class="Comment"># contents</span>
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="Comment"># result:address:array:character &lt;- integer-to-decimal-string n:number</span>
<span class="muRecipe">recipe</span> integer-to-decimal-string [
  <span class="Constant">local-scope</span>
  n:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># is it zero?</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-if</span> n
    result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[0]</span>
    <span class="muControl">reply</span> result
  <span class="Delimiter">}</span>
  <span class="Comment"># save sign</span>
  negate-result:boolean<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    negative?:boolean<span class="Special"> &lt;- </span>lesser-than n, <span class="Constant">0</span>
    <span class="muControl">break-unless</span> negative?
    negate-result<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>
    n<span class="Special"> &lt;- </span>multiply n, <span class="Constant">-1</span>
  <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>new-buffer <span class="Constant">30</span>
  digit-base:number<span class="Special"> &lt;- </span>copy <span class="Constant">48</span>  <span class="Comment"># '0'</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>equal n, <span class="Constant">0</span>
    <span class="muControl">break-if</span> done?
    n, digit:number<span class="Special"> &lt;- </span>divide-with-remainder n, <span class="Constant">10</span>
    c:character<span class="Special"> &lt;- </span>add digit-base, digit
    tmp:address:buffer<span class="Special"> &lt;- </span>buffer-append tmp, c
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># add sign</span>
  <span class="Delimiter">{</span>
    <span class="muControl">break-unless</span> negate-result:boolean
    tmp<span class="Special"> &lt;- </span>buffer-append tmp, <span class="Constant">45</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, <span class="Constant">length:offset</span>
  buf:address:array:character<span class="Special"> &lt;- </span>get *tmp, <span class="Constant">data:offset</span>
  result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, len
  i:number<span class="Special"> &lt;- </span>subtract len, <span class="Constant">1</span>  <span class="Comment"># source index, decreasing</span>
  j:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>  <span class="Comment"># destination index, increasing</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while i &gt;= 0</span>
    done?:boolean<span class="Special"> &lt;- </span>lesser-than i, <span class="Constant">0</span>
    <span class="muControl">break-if</span> done?
    <span class="Comment"># result[j] = tmp[i]</span>
    src:character<span class="Special"> &lt;- </span>index *buf, i
    dest:address:character<span class="Special"> &lt;- </span>index-address *result, j
    *dest<span class="Special"> &lt;- </span>copy src
    i<span class="Special"> &lt;- </span>subtract i, <span class="Constant">1</span>
    j<span class="Special"> &lt;- </span>add j, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result
]

<span class="muRecipe">recipe</span> buffer-to-array [
  <span class="Constant">local-scope</span>
  in:address:buffer<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># propagate null buffer</span>
    <span class="muControl">break-if</span> in
    <span class="muControl">reply</span> <span class="Constant">0</span>
  <span class="Delimiter">}</span>
  len:number<span class="Special"> &lt;- </span>get *in, <span class="Constant">length:offset</span>
  s:address:array:character<span class="Special"> &lt;- </span>get *in, <span class="Constant">data:offset</span>
  <span class="Comment"># we can't just return s because it is usually the wrong length</span>
  result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, len
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i, len
    <span class="muControl">break-if</span> done?
    src:character<span class="Special"> &lt;- </span>index *s, i
    dest:address:character<span class="Special"> &lt;- </span>index-address *result, i
    *dest<span class="Special"> &lt;- </span>copy src
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result
]

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

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

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

<span class="Comment"># result:address:array:character &lt;- string-append a:address:array:character, b:address:array:character</span>
<span class="muRecipe">recipe</span> string-append [
  <span class="Constant">local-scope</span>
  <span class="Comment"># result = new character[a.length + b.length]</span>
  a:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  a-len:number<span class="Special"> &lt;- </span>length *a
  b:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  b-len:number<span class="Special"> &lt;- </span>length *b
  result-len:number<span class="Special"> &lt;- </span>add a-len, b-len
  result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, result-len
  <span class="Comment"># copy a into result</span>
  result-idx:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</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, a-len
    <span class="muControl">break-if</span> a-done?
    <span class="Comment"># result[result-idx] = a[i]</span>
    out:address:character<span class="Special"> &lt;- </span>index-address *result, result-idx
    in:character<span class="Special"> &lt;- </span>index *a, i
    *out<span class="Special"> &lt;- </span>copy in
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    result-idx<span class="Special"> &lt;- </span>add result-idx, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># copy b into result</span>
  i<span class="Special"> &lt;- </span>copy <span class="Constant">0</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, b-len
    <span class="muControl">break-if</span> b-done?
    <span class="Comment"># result[result-idx] = a[i]</span>
    out:address:character<span class="Special"> &lt;- </span>index-address *result, result-idx
    in:character<span class="Special"> &lt;- </span>index *b, i
    *out<span class="Special"> &lt;- </span>copy in
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    result-idx<span class="Special"> &lt;- </span>add result-idx, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result
]

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

<span class="muScenario">scenario</span> replace-character-in-string [
  run [
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>string-replace <span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>, <span class="Constant">98/b</span>, <span class="Constant">122/z</span>
    <span class="Constant">2</span>:array:character/<span class="Special">raw &lt;- </span>copy *<span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span>:string<span class="Special"> &lt;- </span><span class="Constant">[azc]</span>
  ]
]

<span class="muRecipe">recipe</span> string-replace [
  <span class="Constant">local-scope</span>
  s:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  oldc:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  newc:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  from:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  len:number<span class="Special"> &lt;- </span>length *s
  i:number<span class="Special"> &lt;- </span>find-next s, oldc, from
  done?:boolean<span class="Special"> &lt;- </span>greater-or-equal i, len
  <span class="muControl">reply-if</span> done?, s/same-as-ingredient:<span class="Constant">0</span>
  dest:address:character<span class="Special"> &lt;- </span>index-address *s, i
  *dest<span class="Special"> &lt;- </span>copy newc
  i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
  s<span class="Special"> &lt;- </span>string-replace s, oldc, newc, i
  <span class="muControl">reply</span> s/same-as-ingredient:<span class="Constant">0</span>
]

<span class="muScenario">scenario</span> replace-character-at-start [
  run [
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>string-replace <span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>, <span class="Constant">97/a</span>, <span class="Constant">122/z</span>
    <span class="Constant">2</span>:array:character/<span class="Special">raw &lt;- </span>copy *<span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span>:string<span class="Special"> &lt;- </span><span class="Constant">[zbc]</span>
  ]
]

<span class="muScenario">scenario</span> replace-character-at-end [
  run [
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>string-replace <span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>, <span class="Constant">99/c</span>, <span class="Constant">122/z</span>
    <span class="Constant">2</span>:array:character/<span class="Special">raw &lt;- </span>copy *<span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span>:string<span class="Special"> &lt;- </span><span class="Constant">[abz]</span>
  ]
]

<span class="muScenario">scenario</span> replace-character-missing [
  run [
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>string-replace <span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>, <span class="Constant">100/d</span>, <span class="Constant">122/z</span>
    <span class="Constant">2</span>:array:character/<span class="Special">raw &lt;- </span>copy *<span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span>:string<span class="Special"> &lt;- </span><span class="Constant">[abc]</span>
  ]
]

<span class="muScenario">scenario</span> replace-all-characters [
  run [
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[banana]</span>
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>string-replace <span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>, <span class="Constant">97/a</span>, <span class="Constant">122/z</span>
    <span class="Constant">2</span>:array:character/<span class="Special">raw &lt;- </span>copy *<span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span>:string<span class="Special"> &lt;- </span><span class="Constant">[bznznz]</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>
<span class="muRecipe">recipe</span> interpolate [
  <span class="Constant">local-scope</span>
  template:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># compute result-len, space to allocate for result</span>
  tem-len:number<span class="Special"> &lt;- </span>length *template
  result-len:number<span class="Special"> &lt;- </span>copy tem-len
  <span class="Delimiter">{</span>
    <span class="Comment"># while arg received</span>
    a:address:array:character, arg-received?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
    <span class="muControl">break-unless</span> arg-received?
    <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
    result-len<span class="Special"> &lt;- </span>add result-len, a-len
    result-len<span class="Special"> &lt;- </span>subtract result-len, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  rewind-ingredients
  _<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>  <span class="Comment"># skip template</span>
  result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, result-len
  <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</span>
  i:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while arg received</span>
    a:address:array:character, arg-received?:boolean<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
    <span class="muControl">break-unless</span> arg-received?
    <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, tem-len
      <span class="muControl">break-if</span> tem-done?, <span class="Constant">+done:label</span>
      <span class="Comment"># while template[i] != '_'</span>
      in:character<span class="Special"> &lt;- </span>index *template, i
      underscore?:boolean<span class="Special"> &lt;- </span>equal in, <span class="Constant">95/_</span>
      <span class="muControl">break-if</span> underscore?
      <span class="Comment"># result[result-idx] = template[i]</span>
      out:address:character<span class="Special"> &lt;- </span>index-address *result, result-idx
      *out<span class="Special"> &lt;- </span>copy in
      i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
      result-idx<span class="Special"> &lt;- </span>add result-idx, <span class="Constant">1</span>
      <span class="muControl">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</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, a-len
      <span class="muControl">break-if</span> arg-done?
      <span class="Comment"># result[result-idx] = a[j]</span>
      in:character<span class="Special"> &lt;- </span>index *a, j
      out:address:character<span class="Special"> &lt;- </span>index-address *result, result-idx
      *out<span class="Special"> &lt;- </span>copy in
      j<span class="Special"> &lt;- </span>add j, <span class="Constant">1</span>
      result-idx<span class="Special"> &lt;- </span>add result-idx, <span class="Constant">1</span>
      <span class="muControl">loop</span>
    <span class="Delimiter">}</span>
    <span class="Comment"># skip '_' in template</span>
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    <span class="muControl">loop</span>  <span class="Comment"># interpolate next arg</span>
  <span class="Delimiter">}</span>
<span class="Constant">  +done</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, tem-len
    <span class="muControl">break-if</span> tem-done?
    <span class="Comment"># result[result-idx] = template[i]</span>
    in:character<span class="Special"> &lt;- </span>index *template, i
    out:address:character<span class="Special"> &lt;- </span>index-address *result, result-idx:number
    *out<span class="Special"> &lt;- </span>copy in
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    result-idx<span class="Special"> &lt;- </span>add result-idx, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result
]

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

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

<span class="muScenario">scenario</span> interpolate-at-end [
  run [
    <span class="Constant">1</span>:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[hello, _]</span>
    <span class="Constant">2</span>:address:array:character/<span class="Special">raw &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">3</span>:address:array:character/<span class="Special">raw &lt;- </span>interpolate <span class="Constant">1</span>:address:array:character/<span class="Special">raw</span>, <span class="Constant">2</span>:address:array:character/<span class="Special">raw</span>
    <span class="Constant">4</span>:array:character/<span class="Special">raw &lt;- </span>copy *<span class="Constant">3</span>:address:array:character/<span class="Special">raw</span>
  ]
  memory-should-contain [
    <span class="Constant">4</span>:string<span class="Special"> &lt;- </span><span class="Constant">[hello, abc]</span>
  ]
]

<span class="Comment"># result:boolean &lt;- space? c:character</span>
<span class="muRecipe">recipe</span> space? [
  <span class="Constant">local-scope</span>
  c:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># most common case first</span>
  result:boolean<span class="Special"> &lt;- </span>equal c, <span class="Constant">32/space</span>
  <span class="muControl">jump-if</span> result <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">10/newline</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">9/tab</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">13/carriage-return</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  <span class="Comment"># remaining uncommon cases in sorted order</span>
  <span class="Comment"># <a href="http://unicode.org">http://unicode.org</a> code-points in unicode-set Z and Pattern_White_Space</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">11/ctrl-k</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">12/ctrl-l</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">133/ctrl-0085</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">160/no-break-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">5760/ogham-space-mark</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8192/en-quad</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8193/em-quad</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8194/en-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8195/em-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8196/three-per-em-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8197/four-per-em-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8198/six-per-em-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8199/figure-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8200/punctuation-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8201/thin-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8202/hair-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8206/left-to-right</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8207/right-to-left</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8232/line-separator</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8233/paragraph-separator</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8239/narrow-no-break-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">8287/medium-mathematical-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
  result<span class="Special"> &lt;- </span>equal c, <span class="Constant">12288/ideographic-space</span>
  <span class="muControl">jump-if</span> result, <span class="Constant">+reply:label</span>
<span class="Constant">  +reply</span>
  <span class="muControl">reply</span> result
]

<span class="Comment"># result:address:array:character &lt;- trim s:address:array:character</span>
<span class="muRecipe">recipe</span> trim [
  <span class="Constant">local-scope</span>
  s:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  len:number<span class="Special"> &lt;- </span>length *s
  <span class="Comment"># left trim: compute start</span>
  start:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    <span class="Delimiter">{</span>
      at-end?:boolean<span class="Special"> &lt;- </span>greater-or-equal start, len
      <span class="muControl">break-unless</span> at-end?
      result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, <span class="Constant">0</span>
      <span class="muControl">reply</span> result
    <span class="Delimiter">}</span>
    curr:character<span class="Special"> &lt;- </span>index *s, start
    whitespace?:boolean<span class="Special"> &lt;- </span>space? curr
    <span class="muControl">break-unless</span> whitespace?
    start<span class="Special"> &lt;- </span>add start, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># right trim: compute end</span>
  end:number<span class="Special"> &lt;- </span>subtract len, <span class="Constant">1</span>
  <span class="Delimiter">{</span>
    not-at-start?:boolean<span class="Special"> &lt;- </span>greater-than end, start
    assert not-at-start?, <span class="Constant">[end ran up against start]</span>
    curr:character<span class="Special"> &lt;- </span>index *s, end
    whitespace?:boolean<span class="Special"> &lt;- </span>space? curr
    <span class="muControl">break-unless</span> whitespace?
    end<span class="Special"> &lt;- </span>subtract end, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># result = new character[end+1 - start]</span>
  new-len:number<span class="Special"> &lt;- </span>subtract end, start, <span class="Constant">-1</span>
  result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, new-len
  <span class="Comment"># copy the untrimmed parts between start and end</span>
  i:number<span class="Special"> &lt;- </span>copy start
  j:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while i &lt;= end</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-than i, end
    <span class="muControl">break-if</span> done?
    <span class="Comment"># result[j] = s[i]</span>
    src:character<span class="Special"> &lt;- </span>index *s, i
    dest:address:character<span class="Special"> &lt;- </span>index-address *result, j
    *dest<span class="Special"> &lt;- </span>copy src
    i<span class="Special"> &lt;- </span>add i, <span class="Constant">1</span>
    j<span class="Special"> &lt;- </span>add j, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result
]

<span class="muScenario">scenario</span> trim-unmodified [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>trim <span class="Constant">1</span>:address:array:character
    <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span>:string<span class="Special"> &lt;- </span><span class="Constant">[abc]</span>
  ]
]

<span class="muScenario">scenario</span> trim-left [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[  abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>trim <span class="Constant">1</span>:address:array:character
    <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span>:string<span class="Special"> &lt;- </span><span class="Constant">[abc]</span>
  ]
]

<span class="muScenario">scenario</span> trim-right [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc  ]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>trim <span class="Constant">1</span>:address:array:character
    <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span>:string<span class="Special"> &lt;- </span><span class="Constant">[abc]</span>
  ]
]

<span class="muScenario">scenario</span> trim-left-right [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[  abc   ]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>trim <span class="Constant">1</span>:address:array:character
    <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span>:string<span class="Special"> &lt;- </span><span class="Constant">[abc]</span>
  ]
]

<span class="muScenario">scenario</span> trim-newline-tab [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[  abc</span>
<span class="Constant">]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>trim <span class="Constant">1</span>:address:array:character
    <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span>:string<span class="Special"> &lt;- </span><span class="Constant">[abc]</span>
  ]
]

<span class="Comment"># next-index:number &lt;- find-next text:address:array:character, pattern:character, idx:number</span>
<span class="muRecipe">recipe</span> find-next [
  <span class="Constant">local-scope</span>
  text:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  pattern:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  idx:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  len:number<span class="Special"> &lt;- </span>length *text
  <span class="Delimiter">{</span>
    eof?:boolean<span class="Special"> &lt;- </span>greater-or-equal idx, len
    <span class="muControl">break-if</span> eof?
    curr:character<span class="Special"> &lt;- </span>index *text, idx
    found?:boolean<span class="Special"> &lt;- </span>equal curr, pattern
    <span class="muControl">break-if</span> found?
    idx<span class="Special"> &lt;- </span>add idx, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> idx
]

<span class="muScenario">scenario</span> string-find-next [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a/b]</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>find-next <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>, <span class="Constant">0/start-index</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
]

<span class="muScenario">scenario</span> string-find-next-empty [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>find-next <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>, <span class="Constant">0/start-index</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">0</span>
  ]
]

<span class="muScenario">scenario</span> string-find-next-initial [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[/abc]</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>find-next <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>, <span class="Constant">0/start-index</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># prefix match</span>
  ]
]

<span class="muScenario">scenario</span> string-find-next-final [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc/]</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>find-next <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>, <span class="Constant">0/start-index</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">3</span>  <span class="Comment"># suffix match</span>
  ]
]

<span class="muScenario">scenario</span> string-find-next-missing [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>find-next <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>, <span class="Constant">0/start-index</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">3</span>  <span class="Comment"># no match</span>
  ]
]

<span class="muScenario">scenario</span> string-find-next-invalid-index [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>find-next <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>, <span class="Constant">4/start-index</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># no change</span>
  ]
]

<span class="muScenario">scenario</span> string-find-next-first [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[ab/c/]</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>find-next <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>, <span class="Constant">0/start-index</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">2</span>  <span class="Comment"># first '/' of multiple</span>
  ]
]

<span class="muScenario">scenario</span> string-find-next-second [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[ab/c/]</span>
    <span class="Constant">2</span>:number<span class="Special"> &lt;- </span>find-next <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>, <span class="Constant">3/start-index</span>
  ]
  memory-should-contain [
    <span class="Constant">2</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># second '/' of multiple</span>
  ]
]

<span class="Comment"># next-index:number &lt;- find-substring text:address:array:character, pattern:address:array:character, idx:number</span>
<span class="Comment"># like find-next, but searches for multiple characters</span>
<span class="Comment"># fairly dumb algorithm</span>
<span class="muRecipe">recipe</span> find-substring [
  <span class="Constant">local-scope</span>
  text:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  pattern:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  idx:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  first:character<span class="Special"> &lt;- </span>index *pattern, <span class="Constant">0</span>
  <span class="Comment"># repeatedly check for match at current idx</span>
  len:number<span class="Special"> &lt;- </span>length *text
  <span class="Delimiter">{</span>
    <span class="Comment"># does some unnecessary work checking for substrings even when there isn't enough of text left</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal idx, len
    <span class="muControl">break-if</span> done?
    found?:boolean<span class="Special"> &lt;- </span>match-at text, pattern, idx
    <span class="muControl">break-if</span> found?
    idx<span class="Special"> &lt;- </span>add idx, <span class="Constant">1</span>
    <span class="Comment"># optimization: skip past indices that definitely won't match</span>
    idx<span class="Special"> &lt;- </span>find-next text, first, idx
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> idx
]

<span class="muScenario">scenario</span> find-substring-1 [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[bc]</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>find-substring <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
]

<span class="muScenario">scenario</span> find-substring-2 [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcd]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[bc]</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>find-substring <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">1</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>
  ]
]

<span class="muScenario">scenario</span> find-substring-no-match [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[bd]</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>find-substring <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">3</span>  <span class="Comment"># not found</span>
  ]
]

<span class="muScenario">scenario</span> find-substring-suffix-match [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcd]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[cd]</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>find-substring <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>
  ]
]

<span class="muScenario">scenario</span> find-substring-suffix-match-2 [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abcd]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[cde]</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>find-substring <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># not found</span>
  ]
]

<span class="Comment"># result:boolean &lt;- match-at text:address:array:character, pattern:address:array:character, idx:number</span>
<span class="Comment"># checks if substring matches at index 'idx'</span>
<span class="muRecipe">recipe</span> match-at [
  <span class="Constant">local-scope</span>
  text:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  pattern:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  idx:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  pattern-len:number<span class="Special"> &lt;- </span>length *pattern
  <span class="Comment"># check that there's space left for the pattern</span>
  <span class="Delimiter">{</span>
    x:number<span class="Special"> &lt;- </span>length *text
    x<span class="Special"> &lt;- </span>subtract x, pattern-len
    enough-room?:boolean<span class="Special"> &lt;- </span>lesser-or-equal idx, x
    <span class="muControl">break-if</span> enough-room?
    <span class="muControl">reply</span> <span class="Constant">0/not-found</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># check each character of pattern</span>
  pattern-idx:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal pattern-idx, pattern-len
    <span class="muControl">break-if</span> done?
    c:character<span class="Special"> &lt;- </span>index *text, idx
    exp:character<span class="Special"> &lt;- </span>index *pattern, pattern-idx
    <span class="Delimiter">{</span>
      match?:boolean<span class="Special"> &lt;- </span>equal c, exp
      <span class="muControl">break-if</span> match?
      <span class="muControl">reply</span> <span class="Constant">0/not-found</span>
    <span class="Delimiter">}</span>
    idx<span class="Special"> &lt;- </span>add idx, <span class="Constant">1</span>
    pattern-idx<span class="Special"> &lt;- </span>add pattern-idx, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> <span class="Constant">1/found</span>
]

<span class="muScenario">scenario</span> match-at-checks-substring-at-index [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[ab]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># match found</span>
  ]
]

<span class="muScenario">scenario</span> match-at-reflexive [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">1</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># match found</span>
  ]
]

<span class="muScenario">scenario</span> match-at-outside-bounds [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">4</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># never matches</span>
  ]
]

<span class="muScenario">scenario</span> match-at-empty-pattern [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># always matches empty pattern given a valid index</span>
  ]
]

<span class="muScenario">scenario</span> match-at-empty-pattern-outside-bound [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">4</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no match</span>
  ]
]

<span class="muScenario">scenario</span> match-at-empty-text [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no match</span>
  ]
]

<span class="muScenario">scenario</span> match-at-empty-against-empty [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">1</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># matches because pattern is also empty</span>
  ]
]

<span class="muScenario">scenario</span> match-at-inside-bounds [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[bc]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">1</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># matches inner substring</span>
  ]
]

<span class="muScenario">scenario</span> match-at-inside-bounds-2 [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[bc]</span>
    <span class="Constant">3</span>:boolean<span class="Special"> &lt;- </span>match-at <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>:address:array:character, <span class="Constant">0</span>
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># no match</span>
  ]
]

<span class="Comment"># result:address:array:address:array:character &lt;- split s:address:array:character, delim:character</span>
<span class="muRecipe">recipe</span> split [
  <span class="Constant">local-scope</span>
  s:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  delim:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># empty string? return empty array</span>
  len:number<span class="Special"> &lt;- </span>length *s
  <span class="Delimiter">{</span>
    empty?:boolean<span class="Special"> &lt;- </span>equal len, <span class="Constant">0</span>
    <span class="muControl">break-unless</span> empty?
    result:address:array:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">location:type</span>, <span class="Constant">0</span>
    <span class="muControl">reply</span> result
  <span class="Delimiter">}</span>
  <span class="Comment"># count #pieces we need room for</span>
  count:number<span class="Special"> &lt;- </span>copy <span class="Constant">1</span>  <span class="Comment"># n delimiters = n+1 pieces</span>
  idx:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    idx<span class="Special"> &lt;- </span>find-next s, delim, idx
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal idx, len
    <span class="muControl">break-if</span> done?
    idx<span class="Special"> &lt;- </span>add idx, <span class="Constant">1</span>
    count<span class="Special"> &lt;- </span>add count, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="Comment"># allocate space</span>
  result:address:array:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">location:type</span>, count
  <span class="Comment"># repeatedly copy slices start..end until delimiter into result[curr-result]</span>
  curr-result:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  start:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    <span class="Comment"># while next delim exists</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal start, len
    <span class="muControl">break-if</span> done?
    end:number<span class="Special"> &lt;- </span>find-next s, delim, start
    <span class="Comment"># copy start..end into result[curr-result]</span>
    dest:address:address:array:character<span class="Special"> &lt;- </span>index-address *result, curr-result
    *dest<span class="Special"> &lt;- </span>string-copy s, start, end
    <span class="Comment"># slide over to next slice</span>
    start<span class="Special"> &lt;- </span>add end, <span class="Constant">1</span>
    curr-result<span class="Special"> &lt;- </span>add curr-result, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result
]

<span class="muScenario">scenario</span> string-split-1 [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a/b]</span>
    <span class="Constant">2</span>:address:array:address:array:character<span class="Special"> &lt;- </span>split <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>length *<span class="Constant">2</span>:address:array:address:array:character
    <span class="Constant">4</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">0</span>
    <span class="Constant">5</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">1</span>
    <span class="Constant">10</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">4</span>:address:array:character
    <span class="Constant">20</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">5</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">2</span>  <span class="Comment"># length of result</span>
    <span class="Constant">10</span>:string<span class="Special"> &lt;- </span><span class="Constant">[a]</span>
    <span class="Constant">20</span>:string<span class="Special"> &lt;- </span><span class="Constant">[b]</span>
  ]
]

<span class="muScenario">scenario</span> string-split-2 [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a/b/c]</span>
    <span class="Constant">2</span>:address:array:address:array:character<span class="Special"> &lt;- </span>split <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>length *<span class="Constant">2</span>:address:array:address:array:character
    <span class="Constant">4</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">0</span>
    <span class="Constant">5</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">1</span>
    <span class="Constant">6</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">2</span>
    <span class="Constant">10</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">4</span>:address:array:character
    <span class="Constant">20</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">5</span>:address:array:character
    <span class="Constant">30</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">6</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">3</span>  <span class="Comment"># length of result</span>
    <span class="Constant">10</span>:string<span class="Special"> &lt;- </span><span class="Constant">[a]</span>
    <span class="Constant">20</span>:string<span class="Special"> &lt;- </span><span class="Constant">[b]</span>
    <span class="Constant">30</span>:string<span class="Special"> &lt;- </span><span class="Constant">[c]</span>
  ]
]

<span class="muScenario">scenario</span> string-split-missing [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:address:array:character<span class="Special"> &lt;- </span>split <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>length *<span class="Constant">2</span>:address:array:address:array:character
    <span class="Constant">4</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">0</span>
    <span class="Constant">10</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">4</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">1</span>  <span class="Comment"># length of result</span>
    <span class="Constant">10</span>:string<span class="Special"> &lt;- </span><span class="Constant">[abc]</span>
  ]
]

<span class="muScenario">scenario</span> string-split-empty [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[]</span>
    <span class="Constant">2</span>:address:array:address:array:character<span class="Special"> &lt;- </span>split <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>length *<span class="Constant">2</span>:address:array:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">0</span>  <span class="Comment"># empty result</span>
  ]
]

<span class="muScenario">scenario</span> string-split-empty-piece [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a/b//c]</span>
    <span class="Constant">2</span>:address:array:address:array:character<span class="Special"> &lt;- </span>split <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>
    <span class="Constant">3</span>:number<span class="Special"> &lt;- </span>length *<span class="Constant">2</span>:address:array:address:array:character
    <span class="Constant">4</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">0</span>
    <span class="Constant">5</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">1</span>
    <span class="Constant">6</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">2</span>
    <span class="Constant">7</span>:address:array:character<span class="Special"> &lt;- </span>index *<span class="Constant">2</span>:address:array:address:array:character, <span class="Constant">3</span>
    <span class="Constant">10</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">4</span>:address:array:character
    <span class="Constant">20</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">5</span>:address:array:character
    <span class="Constant">30</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">6</span>:address:array:character
    <span class="Constant">40</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">7</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span><span class="Special"> &lt;- </span><span class="Constant">4</span>  <span class="Comment"># length of result</span>
    <span class="Constant">10</span>:string<span class="Special"> &lt;- </span><span class="Constant">[a]</span>
    <span class="Constant">20</span>:string<span class="Special"> &lt;- </span><span class="Constant">[b]</span>
    <span class="Constant">30</span>:string<span class="Special"> &lt;- </span><span class="Constant">[]</span>
    <span class="Constant">40</span>:string<span class="Special"> &lt;- </span><span class="Constant">[c]</span>
  ]
]

<span class="Comment"># x:address:array:character, y:address:array:character &lt;- split-first text:address:array:character, delim:character</span>
<span class="muRecipe">recipe</span> split-first [
  <span class="Constant">local-scope</span>
  text:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  delim:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># empty string? return empty strings</span>
  len:number<span class="Special"> &lt;- </span>length *text
  <span class="Delimiter">{</span>
    empty?:boolean<span class="Special"> &lt;- </span>equal len, <span class="Constant">0</span>
    <span class="muControl">break-unless</span> empty?
    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">[]</span>
    <span class="muControl">reply</span> x, y
  <span class="Delimiter">}</span>
  idx:number<span class="Special"> &lt;- </span>find-next text, delim, <span class="Constant">0</span>
  x:address:array:character<span class="Special"> &lt;- </span>string-copy text, <span class="Constant">0</span>, idx
  idx<span class="Special"> &lt;- </span>add idx, <span class="Constant">1</span>
  y:address:array:character<span class="Special"> &lt;- </span>string-copy text, idx, len
  <span class="muControl">reply</span> x, y
]

<span class="muScenario">scenario</span> string-split-first [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[a/b]</span>
    <span class="Constant">2</span>:address:array:character, <span class="Constant">3</span>:address:array:character<span class="Special"> &lt;- </span>split-first <span class="Constant">1</span>:address:array:character, <span class="Constant">47/slash</span>
    <span class="Constant">10</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
    <span class="Constant">20</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">3</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">10</span>:string<span class="Special"> &lt;- </span><span class="Constant">[a]</span>
    <span class="Constant">20</span>:string<span class="Special"> &lt;- </span><span class="Constant">[b]</span>
  ]
]

<span class="Comment"># result:address:array:character &lt;- string-copy buf:address:array:character, start:number, end:number</span>
<span class="Comment"># todo: make this generic</span>
<span class="muRecipe">recipe</span> string-copy [
  <span class="Constant">local-scope</span>
  buf:address:array:character<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  start:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  end:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Comment"># if end is out of bounds, trim it</span>
  len:number<span class="Special"> &lt;- </span>length *buf
  end:number<span class="Special"> &lt;- </span>min len, end
  <span class="Comment"># allocate space for result</span>
  len<span class="Special"> &lt;- </span>subtract end, start
  result:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">character:type</span>, len
  <span class="Comment"># copy start..end into result[curr-result]</span>
  src-idx:number<span class="Special"> &lt;- </span>copy start
  dest-idx:number<span class="Special"> &lt;- </span>copy <span class="Constant">0</span>
  <span class="Delimiter">{</span>
    done?:boolean<span class="Special"> &lt;- </span>greater-or-equal src-idx, end
    <span class="muControl">break-if</span> done?
    src:character<span class="Special"> &lt;- </span>index *buf, src-idx
    dest:address:character<span class="Special"> &lt;- </span>index-address *result, dest-idx
    *dest<span class="Special"> &lt;- </span>copy src
    src-idx<span class="Special"> &lt;- </span>add src-idx, <span class="Constant">1</span>
    dest-idx<span class="Special"> &lt;- </span>add dest-idx, <span class="Constant">1</span>
    <span class="muControl">loop</span>
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> result
]

<span class="muScenario">scenario</span> string-copy-copies-substring [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>string-copy <span class="Constant">1</span>:address:array:character, <span class="Constant">1</span>, <span class="Constant">3</span>
    <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span>:string<span class="Special"> &lt;- </span><span class="Constant">[bc]</span>
  ]
]

<span class="muScenario">scenario</span> string-copy-out-of-bounds [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>string-copy <span class="Constant">1</span>:address:array:character, <span class="Constant">2</span>, <span class="Constant">4</span>
    <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span>:string<span class="Special"> &lt;- </span><span class="Constant">[c]</span>
  ]
]

<span class="muScenario">scenario</span> string-copy-out-of-bounds-2 [
  run [
    <span class="Constant">1</span>:address:array:character<span class="Special"> &lt;- </span>new <span class="Constant">[abc]</span>
    <span class="Constant">2</span>:address:array:character<span class="Special"> &lt;- </span>string-copy <span class="Constant">1</span>:address:array:character, <span class="Constant">3</span>, <span class="Constant">3</span>
    <span class="Constant">3</span>:array:character<span class="Special"> &lt;- </span>copy *<span class="Constant">2</span>:address:array:character
  ]
  memory-should-contain [
    <span class="Constant">3</span>:string<span class="Special"> &lt;- </span><span class="Constant">[]</span>
  ]
]

<span class="muRecipe">recipe</span> min [
  <span class="Constant">local-scope</span>
  x:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  y:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    return-x?:boolean<span class="Special"> &lt;- </span>lesser-than x, y
    <span class="muControl">break-if</span> return-x?
    <span class="muControl">reply</span> y
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> x
]

<span class="muRecipe">recipe</span> max [
  <span class="Constant">local-scope</span>
  x:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  y:number<span class="Special"> &lt;- </span><span class="Constant">next-ingredient</span>
  <span class="Delimiter">{</span>
    return-x?:boolean<span class="Special"> &lt;- </span>greater-than x, y
    <span class="muControl">break-if</span> return-x?
    <span class="muControl">reply</span> y
  <span class="Delimiter">}</span>
  <span class="muControl">reply</span> x
]
</pre>
</body>
</html>
<!-- vim: set foldmethod=manual : -->