1
2
3
4
5 :(scenario static_dispatch)
6 def main [
7 7:num/raw <- test 3
8 ]
9 def test a:num -> z:num [
10 z <- copy 1
11 ]
12 def test a:num, b:num -> z:num [
13 z <- copy 2
14 ]
15 +mem: storing 1 in location 7
16
17
18
19
20 :(before "End Globals")
21 map<string, vector<recipe_ordinal> > Recipe_variants;
22 :(before "End One-time Setup")
23 put(Recipe_variants, "main", vector<recipe_ordinal>());
24
25 :(before "End Globals")
26 map<string, vector<recipe_ordinal> > Recipe_variants_snapshot;
27 :(before "End save_snapshots")
28 Recipe_variants_snapshot = Recipe_variants;
29 :(before "End restore_snapshots")
30 Recipe_variants = Recipe_variants_snapshot;
31
32 :(before "End Load Recipe Header(result)")
33
34 if (result.name != "main" && contains_key(Recipe_ordinal, result.name)) {
35 const recipe_ordinal r = get(Recipe_ordinal, result.name);
36 if (!contains_key(Recipe, r) || get(Recipe, r).has_header) {
37 ¦ string new_name = matching_variant_name(result);
38 ¦ if (new_name.empty()) {
39 ¦ ¦
40 ¦ ¦ new_name = next_unused_recipe_name(result.name);
41 ¦ ¦ put(Recipe_ordinal, new_name, Next_recipe_ordinal++);
42 ¦ ¦ get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, new_name));
43 ¦ }
44 ¦ trace(9999, "load") << "switching " << result.name << " to " << new_name << end();
45 ¦ result.name = new_name;
46 ¦ result.is_autogenerated = true;
47 }
48 }
49 else {
50
51 put(Recipe_ordinal, result.name, Next_recipe_ordinal++);
52 get_or_insert(Recipe_variants, result.name).push_back(get(Recipe_ordinal, result.name));
53 }
54
55 :(code)
56 string matching_variant_name(const recipe& rr) {
57 const vector<recipe_ordinal>& variants = get_or_insert(Recipe_variants, rr.name);
58 for (int i = 0; i < SIZE(variants); ++i) {
59 ¦ if (!contains_key(Recipe, variants.at(i))) continue;
60 ¦ const recipe& candidate = get(Recipe, variants.at(i));
61 ¦ if (!all_reagents_match(rr, candidate)) continue;
62 ¦ return candidate.name;
63 }
64 return "";
65 }
66
67 bool all_reagents_match(const recipe& r1, const recipe& r2) {
68 if (SIZE(r1.ingredients) != SIZE(r2.ingredients)) return false;
69 if (SIZE(r1.products) != SIZE(r2.products)) return false;
70 for (int i = 0; i < SIZE(r1.ingredients); ++i) {
71 ¦ expand_type_abbreviations(r1.ingredients.at(i).type);
72 ¦ expand_type_abbreviations(r2.ingredients.at(i).type);
73 ¦ if (!deeply_equal_type_names(r1.ingredients.at(i), r2.ingredients.at(i)))
74 ¦ ¦ return false;
75 }
76 for (int i = 0; i < SIZE(r1.products); ++i) {
77 ¦ expand_type_abbreviations(r1.products.at(i).type);
78 ¦ expand_type_abbreviations(r2.products.at(i).type);
79 ¦ if (!deeply_equal_type_names(r1.products.at(i), r2.products.at(i)))
80 ¦ ¦ return false;
81 }
82 return true;
83 }
84
85 :(before "End Globals")
86 set<string> Literal_type_names;
87 :(before "End One-time Setup")
88 Literal_type_names.insert("number");
89 Literal_type_names.insert("character");
90 :(code)
91 bool deeply_equal_type_names(const reagent& a, const reagent& b) {
92 return deeply_equal_type_names(a.type, b.type);
93 }
94 bool deeply_equal_type_names(const type_tree* a, const type_tree* b) {
95 if (!a) return !b;
96 if (!b) return !a;
97 if (a->atom != b->atom) return false;
98 if (a->atom) {
99 ¦ if (a->name == "literal" && b->name == "literal")
100 ¦ ¦ return true;
101 ¦ if (a->name == "literal")
102 ¦ ¦ return Literal_type_names.find(b->name) != Literal_type_names.end();
103 ¦ if (b->name == "literal")
104 ¦ ¦ return Literal_type_names.find(a->name) != Literal_type_names.end();
105 ¦ return a->name == b->name;
106 }
107 return deeply_equal_type_names(a->left, b->left)
108 ¦ ¦ && deeply_equal_type_names(a->right, b->right);
109 }
110
111 string next_unused_recipe_name(const string& recipe_name) {
112 for (int i = 2; ; ++i) {
113 ¦ ostringstream out;
114 ¦ out << recipe_name << '_' << i;
115 ¦ if (!contains_key(Recipe_ordinal, out.str()))
116 ¦ ¦ return out.str();
117 }
118 }
119
120
121
122
123 :(scenario static_dispatch_picks_most_similar_variant)
124 def main [
125 7:num/raw <- test 3, 4, 5
126 ]
127 def test a:num -> z:num [
128 z <- copy 1
129 ]
130 def test a:num, b:num -> z:num [
131 z <- copy 2
132 ]
133 +mem: storing 2 in location 7
134
135
136 :(before "End check_or_set_invalid_types")
137 for (int i = 0; i < SIZE(caller.ingredients); ++i)
138 check_or_set_invalid_types(caller.ingredients.at(i).type, maybe(caller.name), "recipe header ingredient");
139 for (int i = 0; i < SIZE(caller.products); ++i)
140 check_or_set_invalid_types(caller.products.at(i).type, maybe(caller.name), "recipe header product");
141
142
143 :(after "Transform.push_back(transform_names)")
144 Transform.push_back(resolve_ambiguous_calls);
145
146
147
148
149
150
151
152 :(before "End Globals")
153 list<call> Resolve_stack;
154
155 :(code)
156 void resolve_ambiguous_calls(const recipe_ordinal r) {
157 recipe& caller_recipe = get(Recipe, r);
158 trace(9991, "transform") << "--- resolve ambiguous calls for recipe " << caller_recipe.name << end();
159 for (int index = 0; index < SIZE(caller_recipe.steps); ++index) {
160 ¦ instruction& inst = caller_recipe.steps.at(index);
161 ¦ if (inst.is_label) continue;
162 ¦ if (non_ghost_size(get_or_insert(Recipe_variants, inst.name)) == 0) continue;
163 ¦ trace(9992, "transform") << "instruction " << inst.original_string << end();
164 ¦ Resolve_stack.push_front(call(r));
165 ¦ Resolve_stack.front().running_step_index = index;
166 ¦ string new_name = best_variant(inst, caller_recipe);
167 ¦ if (!new_name.empty())
168 ¦ ¦ inst.name = new_name;
169 ¦ assert(Resolve_stack.front().running_recipe == r);
170 ¦ assert(Resolve_stack.front().running_step_index == index);
171 ¦ Resolve_stack.pop_front();
172 }
173 }
174
175 string best_variant(instruction& inst, const recipe& caller_recipe) {
176 vector<recipe_ordinal>& variants = get(Recipe_variants, inst.name);
177 vector<recipe_ordinal> candidates;
178
179
180 candidates = strictly_matching_variants(inst, variants);
181 if (!candidates.empty()) return best_variant(inst, candidates).name;
182
183
184 candidates = strictly_matching_variants_except_literal_against_address_or_boolean(inst, variants);
185 if (!candidates.empty()) return best_variant(inst, candidates).name;
186
187
188
189
190
191
192 candidates = matching_variants(inst, variants);
193 if (!candidates.empty()) return best_variant(inst, candidates).name;
194
195
196 if (get(Recipe_ordinal, inst.name) >= MAX_PRIMITIVE_RECIPES) {
197 ¦ raise << maybe(caller_recipe.name) << "failed to find a matching call for '" << inst.original_string << "'\n" << end();
198 ¦ for (list<call>::iterator p = ++Resolve_stack.begin(); p != Resolve_stack.end(); ++p) {
199 ¦ ¦ const recipe& specializer_recipe = get(Recipe, p->running_recipe);
200 ¦ ¦ const instruction& specializer_inst = specializer_recipe.steps.at(p->running_step_index);
201 ¦ ¦ if (specializer_recipe.name != "interactive")
202 ¦ ¦ ¦ raise << " (from '" << to_original_string(specializer_inst) << "' in " << specializer_recipe.name << ")\n" << end();
203 ¦ ¦ else
204 ¦ ¦ ¦ raise << " (from '" << to_original_string(specializer_inst) << "')\n" << end();
205 ¦ ¦
206 ¦ ¦ if (specializer_inst.products.at(0).name.find("stash_") == 0) {
207 ¦ ¦ ¦ instruction stash_inst;
208 ¦ ¦ ¦ if (next_stash(*p, &stash_inst)) {
209 ¦ ¦ ¦ ¦ if (specializer_recipe.name != "interactive")
210 ¦ ¦ ¦ ¦ ¦ raise << " (part of '" << stash_inst.original_string << "' in " << specializer_recipe.name << ")\n" << end();
211 ¦ ¦ ¦ ¦ else
212 ¦ ¦ ¦ ¦ ¦ raise << " (part of '" << stash_inst.original_string << "')\n" << end();
213 ¦ ¦ ¦ }
214 ¦ ¦ }
215 ¦ }
216 }
217 return "";
218 }
219
220
221 vector<recipe_ordinal> strictly_matching_variants(const instruction& inst, vector<recipe_ordinal>& variants) {
222 vector<recipe_ordinal> result;
223 for (int i = 0; i < SIZE(variants); ++i) {
224 ¦ if (variants.at(i) == -1) continue;
225 ¦ trace(9992, "transform") << "checking variant (strict) " << i << ": " << header_label(variants.at(i)) << end();
226 ¦ if (all_header_reagents_strictly_match(inst, get(Recipe, variants.at(i))))
227 ¦ ¦ result.push_back(variants.at(i));
228 }
229 return result;
230 }
231
232 bool all_header_reagents_strictly_match(const instruction& inst, const recipe& variant) {
233 for (int i = 0; i < min(SIZE(inst.ingredients), SIZE(variant.ingredients)); ++i) {
234 ¦ if (!types_strictly_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
235 ¦ ¦ trace(9993, "transform") << "strict match failed: ingredient " << i << end();
236 ¦ ¦ return false;
237 ¦ }
238 }
239 for (int i = 0; i < min(SIZE(inst.products), SIZE(variant.products)); ++i) {
240 ¦ if (is_dummy(inst.products.at(i))) continue;
241 ¦ if (!types_strictly_match(variant.products.at(i), inst.products.at(i))) {
242 ¦ ¦ trace(9993, "transform") << "strict match failed: product " << i << end();
243 ¦ ¦ return false;
244 ¦ }
245 }
246 return true;
247 }
248
249
250 vector<recipe_ordinal> strictly_matching_variants_except_literal_against_address_or_boolean(const instruction& inst, vector<recipe_ordinal>& variants) {
251 vector<recipe_ordinal> result;
252 for (int i = 0; i < SIZE(variants); ++i) {
253 ¦ if (variants.at(i) == -1) continue;
254 ¦ trace(9992, "transform") << "checking variant (strict except literal-against-boolean) " << i << ": " << header_label(variants.at(i)) << end();
255 ¦ if (all_header_reagents_strictly_match_except_literal_against_address_or_boolean(inst, get(Recipe, variants.at(i))))
256 ¦ ¦ result.push_back(variants.at(i));
257 }
258 return result;
259 }
260
261 bool all_header_reagents_strictly_match_except_literal_against_address_or_boolean(const instruction& inst, const recipe& variant) {
262 for (int i = 0; i < min(SIZE(inst.ingredients), SIZE(variant.ingredients)); ++i) {
263 ¦ if (!types_strictly_match_except_literal_against_address_or_boolean(variant.ingredients.at(i), inst.ingredients.at(i))) {
264 ¦ ¦ trace(9993, "transform") << "match failed: ingredient " << i << end();
265 ¦ ¦ return false;
266 ¦ }
267 }
268 for (int i = 0; i < min(SIZE(variant.products), SIZE(inst.products)); ++i) {
269 ¦ if (is_dummy(inst.products.at(i))) continue;
270 ¦ if (!types_strictly_match_except_literal_against_address_or_boolean(variant.products.at(i), inst.products.at(i))) {
271 ¦ ¦ trace(9993, "transform") << "match failed: product " << i << end();
272 ¦ ¦ return false;
273 ¦ }
274 }
275 return true;
276 }
277
278 bool types_strictly_match_except_literal_against_address_or_boolean(const reagent& to, const reagent& from) {
279 if (is_literal(from) && is_mu_boolean(to))
280 ¦ return from.name == "0" || from.name == "1";
281
282 if (is_literal(from) && is_mu_address(to))
283 ¦ return from.name == "0";
284
285 return types_strictly_match(to, from);
286 }
287
288
289 vector<recipe_ordinal> matching_variants(const instruction& inst, vector<recipe_ordinal>& variants) {
290 vector<recipe_ordinal> result;
291 for (int i = 0; i < SIZE(variants); ++i) {
292 ¦ if (variants.at(i) == -1) continue;
293 ¦ trace(9992, "transform") << "checking variant " << i << ": " << header_label(variants.at(i)) << end();
294 ¦ if (all_header_reagents_match(inst, get(Recipe, variants.at(i))))
295 ¦ ¦ result.push_back(variants.at(i));
296 }
297 return result;
298 }
299
300 bool all_header_reagents_match(const instruction& inst, const recipe& variant) {
301 for (int i = 0; i < min(SIZE(inst.ingredients), SIZE(variant.ingredients)); ++i) {
302 ¦ if (!types_match(variant.ingredients.at(i), inst.ingredients.at(i))) {
303 ¦ ¦ trace(9993, "transform") << "match failed: ingredient " << i << end();
304 ¦ ¦ return false;
305 ¦ }
306 }
307 for (int i = 0; i < min(SIZE(variant.products), SIZE(inst.products)); ++i) {
308 ¦ if (is_dummy(inst.products.at(i))) continue;
309 ¦ if (!types_match(variant.products.at(i), inst.products.at(i))) {
310 ¦ ¦ trace(9993, "transform") << "match failed: product " << i << end();
311 ¦ ¦ return false;
312 ¦ }
313 }
314 return true;
315 }
316
317
318 const recipe& best_variant(const instruction& inst, vector<recipe_ordinal>& candidates) {
319 assert(!candidates.empty());
320 int min_score = 999;
321 int min_index = 0;
322 for (int i = 0; i < SIZE(candidates); ++i) {
323 ¦ const recipe& candidate = get(Recipe, candidates.at(i));
324 ¦
325 ¦ int score = abs(SIZE(candidate.products)-SIZE(inst.products))
326 ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ ¦ + abs(SIZE(candidate.ingredients)-SIZE(inst.ingredients));
327 ¦
328 ¦ for (int i = 0; i < SIZE(candidate.ingredients); ++i) {
329 ¦ ¦ if (is_mu_address(candidate.ingredients.at(i)))
330 ¦ ¦ ¦ ++score;
331 ¦ }
332 ¦ for (int i = 0; i < SIZE(candidate.products); ++i) {
333 ¦ ¦ if (is_mu_address(candidate.products.at(i)))
334 ¦ ¦ ¦ ++score;
335 ¦ }
336 ¦ assert(score < 999);
337 ¦ if (score < min_score) {
338 ¦ ¦ min_score = score;
339 ¦ ¦ min_index = i;
340 ¦ }
341 }
342 return get(Recipe, candidates.at(min_index));
343 }
344
345 int non_ghost_size(vector<recipe_ordinal>& variants) {
346 int result = 0;
347 for (int i = 0; i < SIZE(variants); ++i)
348 ¦ if (variants.at(i) != -1) ++result;
349 return result;
350 }
351
352 bool next_stash(const call& c, instruction* stash_inst) {
353 const recipe& specializer_recipe = get(Recipe, c.running_recipe);
354 int index = c.running_step_index;
355 for (++index; index < SIZE(specializer_recipe.steps); ++index) {
356 ¦ const instruction& inst = specializer_recipe.steps.at(index);
357 ¦ if (inst.name == "stash") {
358 ¦ ¦ *stash_inst = inst;
359 ¦ ¦ return true;
360 ¦ }
361 }
362 return false;
363 }
364
365 :(scenario static_dispatch_disabled_in_recipe_without_variants)
366 def main [
367 1:num <- test 3
368 ]
369 def test [
370 2:num <- next-ingredient
371 return 34
372 ]
373 +mem: storing 34 in location 1
374
375 :(scenario static_dispatch_disabled_on_headerless_definition)
376 % Hide_errors = true;
377 def test a:num -> z:num [
378 z <- copy 1
379 ]
380 def test [
381 return 34
382 ]
383 +error: redefining recipe test
384
385 :(scenario static_dispatch_disabled_on_headerless_definition_2)
386 % Hide_errors = true;
387 def test [
388 return 34
389 ]
390 def test a:num -> z:num [
391 z <- copy 1
392 ]
393 +error: redefining recipe test
394
395 :(scenario static_dispatch_on_primitive_names)
396 def main [
397 1:num <- copy 34
398 2:num <- copy 34
399 3:bool <- equal 1:num, 2:num
400 4:bool <- copy 0/false
401 5:bool <- copy 0/false
402 6:bool <- equal 4:bool, 5:bool
403 ]
404
405 def equal x:num, y:num -> z:bool [
406 local-scope
407 load-ingredients
408 z <- copy 0/false
409 ]
410
411 +mem: storing 0 in location 3
412
413 +mem: storing 1 in location 6
414
415 :(scenario static_dispatch_works_with_dummy_results_for_containers)
416 def main [
417 _ <- test 3, 4
418 ]
419 def test a:num -> z:point [
420 local-scope
421 load-ingredients
422 z <- merge a, 0
423 ]
424 def test a:num, b:num -> z:point [
425 local-scope
426 load-ingredients
427 z <- merge a, b
428 ]
429 $error: 0
430
431 :(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_first_use)
432 def main [
433 x:&:foo <- new foo:type
434 test x
435 ]
436 container foo [
437 x:num
438 ]
439 def test a:&:foo -> z:num [
440 local-scope
441 load-ingredients
442 z:num <- get *a, x:offset
443 ]
444 $error: 0
445
446 :(scenario static_dispatch_works_with_compound_type_containing_container_defined_after_second_use)
447 def main [
448 x:&:foo <- new foo:type
449 test x
450 ]
451 def test a:&:foo -> z:num [
452 local-scope
453 load-ingredients
454 z:num <- get *a, x:offset
455 ]
456 container foo [
457 x:num
458 ]
459 $error: 0
460
461 :(scenario static_dispatch_prefers_literals_to_be_numbers_rather_than_addresses)
462 def main [
463 1:num <- foo 0
464 ]
465 def foo x:&:num -> y:num [
466 return 34
467 ]
468 def foo x:num -> y:num [
469 return 35
470 ]
471 +mem: storing 35 in location 1
472
473 :(scenario static_dispatch_prefers_literals_to_be_numbers_rather_than_addresses_2)
474 def main [
475 1:num <- foo 0 0
476 ]
477
478
479
480 def foo x:&:num y:&:num -> z:num [
481 return 34
482 ]
483 def foo x:&:num y:num -> z:num [
484 return 35
485 ]
486 +mem: storing 35 in location 1
487
488 :(scenario static_dispatch_on_non_literal_character_ignores_variant_with_numbers)
489 % Hide_errors = true;
490 def main [
491 local-scope
492 x:char <- copy 10/newline
493 1:num/raw <- foo x
494 ]
495 def foo x:num -> y:num [
496 load-ingredients
497 return 34
498 ]
499 +error: main: ingredient 0 has the wrong type at '1:num/raw <- foo x'
500 -mem: storing 34 in location 1
501
502 :(scenario static_dispatch_dispatches_literal_to_boolean_before_character)
503 def main [
504 1:num/raw <- foo 0
505 ]
506 def foo x:char -> y:num [
507 local-scope
508 load-ingredients
509 return 34
510 ]
511 def foo x:bool -> y:num [
512 local-scope
513 load-ingredients
514 return 35
515 ]
516
517 +mem: storing 35 in location 1
518
519 :(scenario static_dispatch_dispatches_literal_to_character_when_out_of_boolean_range)
520 def main [
521 1:num/raw <- foo 97
522 ]
523 def foo x:char -> y:num [
524 local-scope
525 load-ingredients
526 return 34
527 ]
528 def foo x:bool -> y:num [
529 local-scope
530 load-ingredients
531 return 35
532 ]
533
534 +mem: storing 34 in location 1
535
536 :(scenario static_dispatch_dispatches_literal_to_number_if_at_all_possible)
537 def main [
538 1:num/raw <- foo 97
539 ]
540 def foo x:char -> y:num [
541 local-scope
542 load-ingredients
543 return 34
544 ]
545 def foo x:num -> y:num [
546 local-scope
547 load-ingredients
548 return 35
549 ]
550
551 +mem: storing 35 in location 1
552
553 :(code)
554 string header_label(const recipe_ordinal r) {
555 return header_label(get(Recipe, r));
556 }
557 string header_label(const recipe& caller) {
558 ostringstream out;
559 out << "recipe " << caller.name;
560 for (int i = 0; i < SIZE(caller.ingredients); ++i)
561 ¦ out << ' ' << to_string(caller.ingredients.at(i));
562 if (!caller.products.empty()) out << " ->";
563 for (int i = 0; i < SIZE(caller.products); ++i)
564 ¦ out << ' ' << to_string(caller.products.at(i));
565 return out.str();
566 }
567
568 :(scenario reload_variant_retains_other_variants)
569 def main [
570 1:num <- copy 34
571 2:num <- foo 1:num
572 ]
573 def foo x:num -> y:num [
574 local-scope
575 load-ingredients
576 return 34
577 ]
578 def foo x:&:num -> y:num [
579 local-scope
580 load-ingredients
581 return 35
582 ]
583 def! foo x:&:num -> y:num [
584 local-scope
585 load-ingredients
586 return 36
587 ]
588 +mem: storing 34 in location 2
589 $error: 0
590
591 :(scenario dispatch_errors_come_after_unknown_name_errors)
592 % Hide_errors = true;
593 def main [
594 y:num <- foo x
595 ]
596 def foo a:num -> b:num [
597 local-scope
598 load-ingredients
599 return 34
600 ]
601 def foo a:bool -> b:num [
602 local-scope
603 load-ingredients
604 return 35
605 ]
606 +error: main: missing type for 'x' in 'y:num <- foo x'
607 +error: main: failed to find a matching call for 'y:num <- foo x'
608
609 :(scenario override_methods_with_type_abbreviations)
610 def main [
611 local-scope
612 s:text <- new [abc]
613 1:num/raw <- foo s
614 ]
615 def foo a:address:array:character -> result:number [
616 return 34
617 ]
618
619 def! foo a:text -> result:num [
620 return 35
621 ]
622 +mem: storing 35 in location 1
623
624 :(before "End Includes")
625 using std::abs;