fix other mandelbrot variants
[mu.git] / archive / 1.vm / 053recipe_header.cc
blob73fde5106a435908a93d3165af74bab4ddfbfd36
1 //: Advanced notation for the common/easy case where a recipe takes some fixed
2 //: number of ingredients and yields some fixed number of products.
4 void test_recipe_with_header() {
5 run(
6 "def main [\n"
7 " 1:num/raw <- add2 3, 5\n"
8 "]\n"
9 "def add2 x:num, y:num -> z:num [\n"
10 " local-scope\n"
11 " load-ingredients\n"
12 " z:num <- add x, y\n"
13 " return z\n"
14 "]\n"
16 CHECK_TRACE_CONTENTS(
17 "mem: storing 8 in location 1\n"
21 //: When loading recipes save any header.
23 :(before "End recipe Fields")
24 bool has_header;
25 vector<reagent> ingredients;
26 vector<reagent> products;
27 :(before "End recipe Constructor")
28 has_header = false;
30 :(before "End Recipe Refinements")
31 if (in.peek() != '[') {
32 trace(101, "parse") << "recipe has a header; parsing" << end();
33 load_recipe_header(in, result);
36 :(code)
37 void load_recipe_header(istream& in, recipe& result) {
38 result.has_header = true;
39 while (has_data(in) && in.peek() != '[' && in.peek() != '\n') {
40 string s = next_word(in);
41 if (s.empty()) {
42 assert(!has_data(in));
43 raise << "incomplete recipe header at end of file (0)\n" << end();
44 return;
46 if (s == "<-")
47 raise << "recipe " << result.name << " should say '->' and not '<-'\n" << end();
48 if (s == "->") break;
49 result.ingredients.push_back(reagent(s));
50 trace(101, "parse") << "header ingredient: " << result.ingredients.back().original_string << end();
51 skip_whitespace_but_not_newline(in);
53 while (has_data(in) && in.peek() != '[' && in.peek() != '\n') {
54 string s = next_word(in);
55 if (s.empty()) {
56 assert(!has_data(in));
57 raise << "incomplete recipe header at end of file (1)\n" << end();
58 return;
60 result.products.push_back(reagent(s));
61 trace(101, "parse") << "header product: " << result.products.back().original_string << end();
62 skip_whitespace_but_not_newline(in);
64 // End Load Recipe Header(result)
67 void test_recipe_handles_stray_comma() {
68 run(
69 "def main [\n"
70 " 1:num/raw <- add2 3, 5\n"
71 "]\n"
72 "def add2 x:num, y:num -> z:num, [\n"
73 " local-scope\n"
74 " load-ingredients\n"
75 " z:num <- add x, y\n"
76 " return z\n"
77 "]\n"
79 CHECK_TRACE_CONTENTS(
80 "mem: storing 8 in location 1\n"
84 void test_recipe_handles_stray_comma_2() {
85 run(
86 "def main [\n"
87 " foo\n"
88 "]\n"
89 "def foo, [\n"
90 " 1:num/raw <- add 2, 2\n"
91 "]\n"
92 "def bar [\n"
93 " 1:num/raw <- add 2, 3\n"
94 "]\n"
96 CHECK_TRACE_CONTENTS(
97 "mem: storing 4 in location 1\n"
101 void test_recipe_handles_wrong_arrow() {
102 Hide_errors = true;
103 run(
104 "def foo a:num <- b:num [\n"
105 "]\n"
107 CHECK_TRACE_CONTENTS(
108 "error: recipe foo should say '->' and not '<-'\n"
112 void test_recipe_handles_missing_bracket() {
113 Hide_errors = true;
114 run(
115 "def main\n"
116 "]\n"
118 CHECK_TRACE_CONTENTS(
119 "error: main: recipe body must begin with '['\n"
123 void test_recipe_handles_missing_bracket_2() {
124 Hide_errors = true;
125 run(
126 "def main\n"
127 " local-scope\n"
128 " {\n"
129 " }\n"
130 "]\n"
132 // doesn't overflow line when reading header
133 CHECK_TRACE_DOESNT_CONTAIN("parse: header ingredient: local-scope");
134 CHECK_TRACE_CONTENTS(
135 "error: main: recipe body must begin with '['\n"
139 void test_recipe_handles_missing_bracket_3() {
140 Hide_errors = true;
141 run(
142 "def main # comment\n"
143 " local-scope\n"
144 " {\n"
145 " }\n"
146 "]\n"
148 // doesn't overflow line when reading header
149 CHECK_TRACE_DOESNT_CONTAIN("parse: header ingredient: local-scope");
150 CHECK_TRACE_CONTENTS(
151 "error: main: recipe body must begin with '['\n"
155 :(after "Begin debug_string(recipe x)")
156 out << "ingredients:\n";
157 for (int i = 0; i < SIZE(x.ingredients); ++i)
158 out << " " << debug_string(x.ingredients.at(i)) << '\n';
159 out << "products:\n";
160 for (int i = 0; i < SIZE(x.products); ++i)
161 out << " " << debug_string(x.products.at(i)) << '\n';
163 //: If a recipe never mentions any ingredients or products, assume it has a header.
165 :(code)
166 void test_recipe_without_ingredients_or_products_has_header() {
167 run(
168 "def test [\n"
169 " 1:num <- copy 34\n"
170 "]\n"
172 CHECK_TRACE_CONTENTS(
173 "parse: recipe test has a header\n"
177 :(before "End Recipe Body(result)")
178 if (!result.has_header) {
179 result.has_header = true;
180 for (int i = 0; i < SIZE(result.steps); ++i) {
181 const instruction& inst = result.steps.at(i);
182 if ((inst.name == "reply" && !inst.ingredients.empty())
183 || (inst.name == "return" && !inst.ingredients.empty())
184 || inst.name == "next-ingredient"
185 || inst.name == "ingredient"
186 || inst.name == "rewind-ingredients") {
187 result.has_header = false;
188 break;
192 if (result.has_header) {
193 trace(101, "parse") << "recipe " << result.name << " has a header" << end();
196 //: Support type abbreviations in headers.
198 :(code)
199 void test_type_abbreviations_in_recipe_headers() {
200 run(
201 "def main [\n"
202 " local-scope\n"
203 " a:text <- foo\n"
204 " 1:char/raw <- index *a, 0\n"
205 "]\n"
206 "def foo -> a:text [ # 'text' is an abbreviation\n"
207 " local-scope\n"
208 " load-ingredients\n"
209 " a <- new [abc]\n"
210 "]\n"
212 CHECK_TRACE_CONTENTS(
213 "mem: storing 97 in location 1\n"
217 :(before "End Expand Type Abbreviations(caller)")
218 for (long int i = 0; i < SIZE(caller.ingredients); ++i)
219 expand_type_abbreviations(caller.ingredients.at(i).type);
220 for (long int i = 0; i < SIZE(caller.products); ++i)
221 expand_type_abbreviations(caller.products.at(i).type);
223 //: Rewrite 'load-ingredients' to instructions to create all reagents in the header.
225 :(before "End Rewrite Instruction(curr, recipe result)")
226 if (curr.name == "load-ingredients" || curr.name == "load-inputs") {
227 curr.clear();
228 recipe_ordinal op = get(Recipe_ordinal, "next-ingredient-without-typechecking");
229 for (int i = 0; i < SIZE(result.ingredients); ++i) {
230 curr.operation = op;
231 curr.name = "next-ingredient-without-typechecking";
232 curr.products.push_back(result.ingredients.at(i));
233 result.steps.push_back(curr);
234 curr.clear();
237 if (curr.name == "next-ingredient-without-typechecking") {
238 raise << maybe(result.name) << "never call 'next-ingredient-without-typechecking' directly\n" << end();
239 curr.clear();
242 //: internal version of next-ingredient; don't call this directly
243 :(before "End Primitive Recipe Declarations")
244 NEXT_INGREDIENT_WITHOUT_TYPECHECKING,
245 :(before "End Primitive Recipe Numbers")
246 put(Recipe_ordinal, "next-ingredient-without-typechecking", NEXT_INGREDIENT_WITHOUT_TYPECHECKING);
247 :(before "End Primitive Recipe Checks")
248 case NEXT_INGREDIENT_WITHOUT_TYPECHECKING: {
249 break;
251 :(before "End Primitive Recipe Implementations")
252 case NEXT_INGREDIENT_WITHOUT_TYPECHECKING: {
253 assert(!Current_routine->calls.empty());
254 if (current_call().next_ingredient_to_process < SIZE(current_call().ingredient_atoms)) {
255 products.push_back(
256 current_call().ingredient_atoms.at(current_call().next_ingredient_to_process));
257 assert(SIZE(products) == 1); products.resize(2); // push a new vector
258 products.at(1).push_back(1);
259 ++current_call().next_ingredient_to_process;
261 else {
262 products.resize(2);
263 // pad the first product with sufficient zeros to match its type
264 products.at(0).resize(size_of(current_instruction().products.at(0)));
265 products.at(1).push_back(0);
267 break;
270 //: more useful error messages if someone forgets 'load-ingredients'
271 :(code)
272 void test_load_ingredients_missing_error() {
273 Hide_errors = true;
274 run(
275 "def foo a:num [\n"
276 " local-scope\n"
277 " b:num <- add a:num, 1\n"
278 "]\n"
280 CHECK_TRACE_CONTENTS(
281 "error: foo: tried to read ingredient 'a' in 'b:num <- add a:num, 1' but it hasn't been written to yet\n"
282 "error: did you forget 'load-ingredients'?\n"
286 :(after "use-before-set Error")
287 if (is_present_in_ingredients(caller, ingredient.name))
288 raise << " did you forget 'load-ingredients'?\n" << end();
290 :(code)
291 void test_load_ingredients_missing_error_2() {
292 Hide_errors = true;
293 run(
294 "def foo a:num [\n"
295 " local-scope\n"
296 " b:num <- add a, 1\n"
297 "]\n"
299 CHECK_TRACE_CONTENTS(
300 "error: foo: missing type for 'a' in 'b:num <- add a, 1'\n"
301 "error: did you forget 'load-ingredients'?\n"
305 :(after "missing-type Error 1")
306 if (is_present_in_ingredients(get(Recipe, get(Recipe_ordinal, recipe_name)), x.name))
307 raise << " did you forget 'load-ingredients'?\n" << end();
309 :(code)
310 bool is_present_in_ingredients(const recipe& callee, const string& ingredient_name) {
311 for (int i = 0; i < SIZE(callee.ingredients); ++i) {
312 if (callee.ingredients.at(i).name == ingredient_name)
313 return true;
315 return false;
318 //:: Check all calls against headers.
320 void test_show_clear_error_on_bad_call() {
321 Hide_errors = true;
322 run(
323 "def main [\n"
324 " 1:num <- foo 34\n"
325 "]\n"
326 "def foo x:point -> y:num [\n"
327 " local-scope\n"
328 " load-ingredients\n"
329 " return 35\n"
330 "]\n"
332 CHECK_TRACE_CONTENTS(
333 "error: main: ingredient 0 has the wrong type at '1:num <- foo 34'\n"
337 void test_show_clear_error_on_bad_call_2() {
338 Hide_errors = true;
339 run(
340 "def main [\n"
341 " 1:point <- foo 34\n"
342 "]\n"
343 "def foo x:num -> y:num [\n"
344 " local-scope\n"
345 " load-ingredients\n"
346 " return x\n"
347 "]\n"
349 CHECK_TRACE_CONTENTS(
350 "error: main: product 0 has the wrong type at '1:point <- foo 34'\n"
354 :(after "Transform.push_back(check_instruction)")
355 Transform.push_back(check_calls_against_header); // idempotent
356 :(code)
357 void check_calls_against_header(const recipe_ordinal r) {
358 const recipe& caller = get(Recipe, r);
359 trace(101, "transform") << "--- type-check calls inside recipe " << caller.name << end();
360 for (int i = 0; i < SIZE(caller.steps); ++i) {
361 const instruction& inst = caller.steps.at(i);
362 if (is_primitive(inst.operation)) continue;
363 const recipe& callee = get(Recipe, inst.operation);
364 if (!callee.has_header) continue;
365 for (long int i = 0; i < min(SIZE(inst.ingredients), SIZE(callee.ingredients)); ++i) {
366 // ingredients coerced from call to callee
367 if (!types_coercible(callee.ingredients.at(i), inst.ingredients.at(i))) {
368 raise << maybe(caller.name) << "ingredient " << i << " has the wrong type at '" << to_original_string(inst) << "'\n" << end();
369 raise << " ['" << to_string(callee.ingredients.at(i).type) << "' vs '" << to_string(inst.ingredients.at(i).type) << "']\n" << end();
372 for (long int i = 0; i < min(SIZE(inst.products), SIZE(callee.products)); ++i) {
373 if (is_dummy(inst.products.at(i))) continue;
374 // products coerced from callee to call
375 if (!types_coercible(inst.products.at(i), callee.products.at(i))) {
376 raise << maybe(caller.name) << "product " << i << " has the wrong type at '" << to_original_string(inst) << "'\n" << end();
377 raise << " ['" << to_string(inst.products.at(i).type) << "' vs '" << to_string(callee.products.at(i).type) << "']\n" << end();
383 //:: Check types going in and out of all recipes with headers.
385 void test_recipe_headers_are_checked() {
386 Hide_errors = true;
387 transform(
388 "def add2 x:num, y:num -> z:num [\n"
389 " local-scope\n"
390 " load-ingredients\n"
391 " z:&:num <- copy 0/unsafe\n"
392 " return z\n"
393 "]\n"
395 CHECK_TRACE_CONTENTS(
396 "error: add2: replied with the wrong type at 'return z'\n"
400 :(before "End Checks")
401 Transform.push_back(check_return_instructions_against_header); // idempotent
403 :(code)
404 void check_return_instructions_against_header(const recipe_ordinal r) {
405 const recipe& caller_recipe = get(Recipe, r);
406 if (!caller_recipe.has_header) return;
407 trace(101, "transform") << "--- checking return instructions against header for " << caller_recipe.name << end();
408 for (int i = 0; i < SIZE(caller_recipe.steps); ++i) {
409 const instruction& inst = caller_recipe.steps.at(i);
410 if (inst.name != "reply" && inst.name != "return") continue;
411 if (SIZE(caller_recipe.products) != SIZE(inst.ingredients)) {
412 raise << maybe(caller_recipe.name) << "replied with the wrong number of products at '" << to_original_string(inst) << "'\n" << end();
413 continue;
415 for (int i = 0; i < SIZE(caller_recipe.products); ++i) {
416 if (!types_match(caller_recipe.products.at(i), inst.ingredients.at(i)))
417 raise << maybe(caller_recipe.name) << "replied with the wrong type at '" << to_original_string(inst) << "'\n" << end();
422 void test_recipe_headers_are_checked_2() {
423 Hide_errors = true;
424 transform(
425 "def add2 x:num, y:num [\n"
426 " local-scope\n"
427 " load-ingredients\n"
428 " z:&:num <- copy 0/unsafe\n"
429 " return z\n"
430 "]\n"
432 CHECK_TRACE_CONTENTS(
433 "error: add2: replied with the wrong number of products at 'return z'\n"
437 void test_recipe_headers_are_checked_against_pre_transformed_instructions() {
438 Hide_errors = true;
439 transform(
440 "def foo -> x:num [\n"
441 " local-scope\n"
442 " x:num <- copy 0\n"
443 " z:bool <- copy false\n"
444 " return-if z, z\n"
445 "]\n"
447 CHECK_TRACE_CONTENTS(
448 "error: foo: replied with the wrong type at 'return-if z, z'\n"
452 void test_recipe_headers_check_for_duplicate_names() {
453 Hide_errors = true;
454 transform(
455 "def foo x:num, x:num -> z:num [\n"
456 " local-scope\n"
457 " load-ingredients\n"
458 " return z\n"
459 "]\n"
461 CHECK_TRACE_CONTENTS(
462 "error: foo: 'x' can't repeat in the ingredients\n"
466 void test_recipe_headers_check_for_duplicate_names_2() {
467 Hide_errors = true;
468 transform(
469 "def foo x:num, x:num [ # no result\n"
470 " local-scope\n"
471 " load-ingredients\n"
472 "]\n"
474 CHECK_TRACE_CONTENTS(
475 "error: foo: 'x' can't repeat in the ingredients\n"
479 void test_recipe_headers_check_for_missing_types() {
480 Hide_errors = true;
481 transform(
482 "def main [\n"
483 " foo 0\n"
484 "]\n"
485 "def foo a [\n" // no type for 'a'
486 "]\n"
488 CHECK_TRACE_CONTENTS(
489 "error: foo: ingredient 'a' has no type\n"
493 :(before "End recipe Fields")
494 map<string, int> ingredient_index;
496 :(after "Begin Instruction Modifying Transforms")
497 Transform.push_back(check_header_ingredients); // idempotent
499 :(code)
500 void check_header_ingredients(const recipe_ordinal r) {
501 recipe& caller_recipe = get(Recipe, r);
502 caller_recipe.ingredient_index.clear();
503 trace(101, "transform") << "--- checking return instructions against header for " << caller_recipe.name << end();
504 for (int i = 0; i < SIZE(caller_recipe.ingredients); ++i) {
505 if (caller_recipe.ingredients.at(i).type == NULL)
506 raise << maybe(caller_recipe.name) << "ingredient '" << caller_recipe.ingredients.at(i).name << "' has no type\n" << end();
507 if (contains_key(caller_recipe.ingredient_index, caller_recipe.ingredients.at(i).name))
508 raise << maybe(caller_recipe.name) << "'" << caller_recipe.ingredients.at(i).name << "' can't repeat in the ingredients\n" << end();
509 put(caller_recipe.ingredient_index, caller_recipe.ingredients.at(i).name, i);
513 //: Deduce types from the header if possible.
515 void test_deduce_instruction_types_from_recipe_header() {
516 run(
517 "def main [\n"
518 " 1:num/raw <- add2 3, 5\n"
519 "]\n"
520 "def add2 x:num, y:num -> z:num [\n"
521 " local-scope\n"
522 " load-ingredients\n"
523 " z <- add x, y # no type for z\n"
524 " return z\n"
525 "]\n"
527 CHECK_TRACE_CONTENTS(
528 "mem: storing 8 in location 1\n"
532 :(after "Begin Type Modifying Transforms")
533 Transform.push_back(deduce_types_from_header); // idempotent
535 :(code)
536 void deduce_types_from_header(const recipe_ordinal r) {
537 recipe& caller_recipe = get(Recipe, r);
538 if (caller_recipe.products.empty()) return;
539 trace(101, "transform") << "--- deduce types from header for " << caller_recipe.name << end();
540 map<string, const type_tree*> header_type;
541 for (int i = 0; i < SIZE(caller_recipe.ingredients); ++i) {
542 if (!caller_recipe.ingredients.at(i).type) continue; // error handled elsewhere
543 put(header_type, caller_recipe.ingredients.at(i).name, caller_recipe.ingredients.at(i).type);
544 trace(103, "transform") << "type of " << caller_recipe.ingredients.at(i).name << " is " << names_to_string(caller_recipe.ingredients.at(i).type) << end();
546 for (int i = 0; i < SIZE(caller_recipe.products); ++i) {
547 if (!caller_recipe.products.at(i).type) continue; // error handled elsewhere
548 put(header_type, caller_recipe.products.at(i).name, caller_recipe.products.at(i).type);
549 trace(103, "transform") << "type of " << caller_recipe.products.at(i).name << " is " << names_to_string(caller_recipe.products.at(i).type) << end();
551 for (int i = 0; i < SIZE(caller_recipe.steps); ++i) {
552 instruction& inst = caller_recipe.steps.at(i);
553 trace(102, "transform") << "instruction: " << to_string(inst) << end();
554 for (int i = 0; i < SIZE(inst.ingredients); ++i) {
555 if (inst.ingredients.at(i).type) continue;
556 if (header_type.find(inst.ingredients.at(i).name) == header_type.end())
557 continue;
558 if (!contains_key(header_type, inst.ingredients.at(i).name)) continue; // error handled elsewhere
559 inst.ingredients.at(i).type = new type_tree(*get(header_type, inst.ingredients.at(i).name));
560 trace(103, "transform") << "type of " << inst.ingredients.at(i).name << " is " << names_to_string(inst.ingredients.at(i).type) << end();
562 for (int i = 0; i < SIZE(inst.products); ++i) {
563 trace(103, "transform") << " product: " << to_string(inst.products.at(i)) << end();
564 if (inst.products.at(i).type) continue;
565 if (header_type.find(inst.products.at(i).name) == header_type.end())
566 continue;
567 if (!contains_key(header_type, inst.products.at(i).name)) continue; // error handled elsewhere
568 inst.products.at(i).type = new type_tree(*get(header_type, inst.products.at(i).name));
569 trace(103, "transform") << "type of " << inst.products.at(i).name << " is " << names_to_string(inst.products.at(i).type) << end();
574 //: One final convenience: no need to say what to return if the information is
575 //: in the header.
577 void test_return_based_on_header() {
578 run(
579 "def main [\n"
580 " 1:num/raw <- add2 3, 5\n"
581 "]\n"
582 "def add2 x:num, y:num -> z:num [\n"
583 " local-scope\n"
584 " load-ingredients\n"
585 " z <- add x, y\n"
586 " return\n"
587 "]\n"
589 CHECK_TRACE_CONTENTS(
590 "mem: storing 8 in location 1\n"
594 :(after "Transform.push_back(check_header_ingredients)")
595 Transform.push_back(fill_in_return_ingredients); // idempotent
597 :(code)
598 void fill_in_return_ingredients(const recipe_ordinal r) {
599 recipe& caller_recipe = get(Recipe, r);
600 trace(101, "transform") << "--- fill in return ingredients from header for recipe " << caller_recipe.name << end();
601 if (!caller_recipe.has_header) return;
602 for (int i = 0; i < SIZE(caller_recipe.steps); ++i) {
603 instruction& inst = caller_recipe.steps.at(i);
604 if (inst.name == "reply" || inst.name == "return")
605 add_header_products(inst, caller_recipe);
607 // fall through return
608 if (!caller_recipe.steps.empty()) {
609 const instruction& final_instruction = caller_recipe.steps.at(SIZE(caller_recipe.steps)-1);
610 if (final_instruction.name == "reply" || final_instruction.name == "return")
611 return;
613 instruction inst;
614 inst.name = "return";
615 add_header_products(inst, caller_recipe);
616 caller_recipe.steps.push_back(inst);
619 void add_header_products(instruction& inst, const recipe& caller_recipe) {
620 assert(inst.name == "reply" || inst.name == "return");
621 // collect any products with the same names as ingredients
622 for (int i = 0; i < SIZE(caller_recipe.products); ++i) {
623 // if the ingredient is missing, add it from the header
624 if (SIZE(inst.ingredients) == i)
625 inst.ingredients.push_back(caller_recipe.products.at(i));
626 // if it's missing /same_as_ingredient, try to fill it in
627 if (contains_key(caller_recipe.ingredient_index, caller_recipe.products.at(i).name) && !has_property(inst.ingredients.at(i), "same_as_ingredient")) {
628 ostringstream same_as_ingredient;
629 same_as_ingredient << get(caller_recipe.ingredient_index, caller_recipe.products.at(i).name);
630 inst.ingredients.at(i).properties.push_back(pair<string, string_tree*>("same-as-ingredient", new string_tree(same_as_ingredient.str())));
635 void test_explicit_return_ignores_header() {
636 run(
637 "def main [\n"
638 " 1:num/raw, 2:num/raw <- add2 3, 5\n"
639 "]\n"
640 "def add2 a:num, b:num -> y:num, z:num [\n"
641 " local-scope\n"
642 " load-ingredients\n"
643 " y <- add a, b\n"
644 " z <- subtract a, b\n"
645 " return a, z\n"
646 "]\n"
648 CHECK_TRACE_CONTENTS(
649 "mem: storing 3 in location 1\n"
650 "mem: storing -2 in location 2\n"
654 void test_return_on_fallthrough_based_on_header() {
655 run(
656 "def main [\n"
657 " 1:num/raw <- add2 3, 5\n"
658 "]\n"
659 "def add2 x:num, y:num -> z:num [\n"
660 " local-scope\n"
661 " load-ingredients\n"
662 " z <- add x, y\n"
663 "]\n"
665 CHECK_TRACE_CONTENTS(
666 "transform: instruction: return {z: \"number\"}\n"
667 "mem: storing 8 in location 1\n"
671 void test_return_on_fallthrough_already_exists() {
672 run(
673 "def main [\n"
674 " 1:num/raw <- add2 3, 5\n"
675 "]\n"
676 "def add2 x:num, y:num -> z:num [\n"
677 " local-scope\n"
678 " load-ingredients\n"
679 " z <- add x, y # no type for z\n"
680 " return z\n"
681 "]\n"
683 CHECK_TRACE_CONTENTS(
684 "transform: instruction: return {z: ()}\n"
686 CHECK_TRACE_DOESNT_CONTAIN("transform: instruction: return z:num");
687 CHECK_TRACE_CONTENTS(
688 "mem: storing 8 in location 1\n"
692 void test_return_causes_error_in_empty_recipe() {
693 Hide_errors = true;
694 run(
695 "def foo -> x:num [\n"
696 "]\n"
698 CHECK_TRACE_CONTENTS(
699 "error: foo: tried to read ingredient 'x' in 'return x:num' but it hasn't been written to yet\n"
703 void test_return_after_conditional_return_based_on_header() {
704 run(
705 "def main [\n"
706 " 1:num/raw <- add2 3, 5\n"
707 "]\n"
708 "def add2 x:num, y:num -> z:num [\n"
709 " local-scope\n"
710 " load-ingredients\n"
711 " z <- add x, y\n" // no type for z
712 " return-if false, 34\n"
713 "]\n"
715 CHECK_TRACE_CONTENTS(
716 "mem: storing 8 in location 1\n"
720 void test_recipe_headers_perform_same_ingredient_check() {
721 Hide_errors = true;
722 run(
723 "def main [\n"
724 " 1:num <- copy 34\n"
725 " 2:num <- copy 34\n"
726 " 3:num <- add2 1:num, 2:num\n"
727 "]\n"
728 "def add2 x:num, y:num -> x:num [\n"
729 " local-scope\n"
730 " load-ingredients\n"
731 "]\n"
733 CHECK_TRACE_CONTENTS(
734 "error: main: '3:num <- add2 1:num, 2:num' should write to '1:num' rather than '3:num'\n"
738 //: One special-case is recipe 'main'. Make sure it's only ever taking in text
739 //: ingredients, and returning a single number.
741 void test_recipe_header_ingredients_constrained_for_main() {
742 Hide_errors = true;
743 run(
744 "def main x:num [\n"
745 "]\n"
747 CHECK_TRACE_CONTENTS(
748 "error: ingredients of recipe 'main' must all be text (address:array:character)\n"
751 void test_recipe_header_products_constrained_for_main() {
752 Hide_errors = true;
753 run(
754 "def main -> x:text [\n"
755 "]\n"
757 CHECK_TRACE_CONTENTS(
758 "error: recipe 'main' must return at most a single product, a number\n"
761 void test_recipe_header_products_constrained_for_main_2() {
762 Hide_errors = true;
763 run(
764 "def main -> x:num, y:num [\n"
765 "]\n"
767 CHECK_TRACE_CONTENTS(
768 "error: recipe 'main' must return at most a single product, a number\n"
772 :(after "Transform.push_back(expand_type_abbreviations)")
773 Transform.push_back(check_recipe_header_constraints);
774 :(code)
775 void check_recipe_header_constraints(const recipe_ordinal r) {
776 const recipe& caller = get(Recipe, r);
777 if (caller.name != "main") return;
778 trace(102, "transform") << "check recipe header constraints for recipe " << caller.name << end();
779 if (!caller.has_header) return;
780 reagent/*local*/ expected_ingredient("x:address:array:character");
781 for (int i = 0; i < SIZE(caller.ingredients); ++i) {
782 if (!types_strictly_match(expected_ingredient, caller.ingredients.at(i))) {
783 raise << "ingredients of recipe 'main' must all be text (address:array:character)\n" << end();
784 break;
787 int nprod = SIZE(caller.products);
788 reagent/*local*/ expected_product("x:number");
789 if (nprod > 1
790 || (nprod == 1 && !types_strictly_match(expected_product, caller.products.at(0)))) {
791 raise << "recipe 'main' must return at most a single product, a number\n" << end();