1 //: Calls can also generate products, using 'reply' or 'return'.
  2 
  3 :(scenario return)
  4 def main [
  5   1:num, 2:num <- f 34
  6 ]
  7 def f [
  8   12:num <- next-ingredient
  9   13:num <- add 1, 12:num
 10   return 12:num, 13:num
 11 ]
 12 +mem: storing 34 in location 1
 13 +mem: storing 35 in location 2
 14 
 15 :(scenario reply)
 16 def main [
 17   1:num, 2:num <- f 34
 18 ]
 19 def f [
 20   12:num <- next-ingredient
 21   13:num <- add 1, 12:num
 22   reply 12:num, 13:num
 23 ]
 24 +mem: storing 34 in location 1
 25 +mem: storing 35 in location 2
 26 
 27 :(before "End Primitive Recipe Declarations")
 28 RETURN,
 29 :(before "End Primitive Recipe Numbers")
 30 put(Recipe_ordinal, "return", RETURN);
 31 put(Recipe_ordinal, "reply", RETURN);  // synonym while teaching
 32 :(before "End Primitive Recipe Checks")
 33 case RETURN: {
 34   break;  // checks will be performed by a transform below
 35 }
 36 :(before "End Primitive Recipe Implementations")
 37 case RETURN: {
 38   // Starting Reply
 39   if (Trace_stream) {
 40     trace(9999, "trace") << current_instruction().name << ": decrementing callstack depth from " << Trace_stream->callstack_depth << end();
 41     --Trace_stream->callstack_depth;
 42     if (Trace_stream->callstack_depth < 0) {
 43       Current_routine->calls.clear();
 44       goto stop_running_current_routine;
 45     }
 46   }
 47   Current_routine->calls.pop_front();
 48   // just in case 'main' returns a value, drop it for now
 49   if (Current_routine->calls.empty()) goto stop_running_current_routine;
 50   for (int i = 0;  i < SIZE(ingredients);  ++i)
 51     trace(9998, "run") << "result " << i << " is " << to_string(ingredients.at(i)) << end();
 52   // make return products available to caller
 53   copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
 54   // End Reply
 55   break;  // continue to process rest of *caller* instruction
 56 }
 57 
 58 //: Types in return instructions are checked ahead of time.
 59 
 60 :(before "End Checks")
 61 Transform.push_back(check_types_of_return_instructions);  // idempotent
 62 :(code)
 63 void check_types_of_return_instructions(const recipe_ordinal r) {
 64   const recipe& caller = get(Recipe, r);
 65   trace(9991, "transform") << "--- check types of return instructions in recipe " << caller.name << end();
 66   for (int i = 0;  i < SIZE(caller.steps);  ++i) {
 67     const instruction& caller_instruction = caller.steps.at(i);
 68     if (caller_instruction.is_label) continue;
 69     if (caller_instruction.products.empty()) continue;
 70     if (caller_instruction.operation < MAX_PRIMITIVE_RECIPES) continue;
 71     const recipe& callee = get(Recipe, caller_instruction.operation);
 72     for (int i = 0;  i < SIZE(callee.steps);  ++i) {
 73       const instruction& return_inst = callee.steps.at(i);
 74       if (return_inst.operation != RETURN) continue;
 75       // check types with the caller
 76       if (SIZE(caller_instruction.products) > SIZE(return_inst.ingredients)) {
 77         raise << maybe(caller.name) << "too few values returned from " << callee.name << '\n' << end();
 78         break;
 79       }
 80       for (int i = 0;  i < SIZE(caller_instruction.products);  ++i) {
 81         reagent/*copy*/ lhs = return_inst.ingredients.at(i);
 82         reagent/*copy*/ rhs = caller_instruction.products.at(i);
 83         // End Check RETURN Copy(lhs, rhs)
 84         if (!types_coercible(rhs, lhs)) {
 85           raise << maybe(callee.name) << return_inst.name << " ingredient '" << lhs.original_string << "' can't be saved in '" << rhs.original_string << "'\n" << end();
 86           raise << "  ['" << to_string(lhs.type) << "' vs '" << to_string(rhs.type) << "']\n" << end();
 87           goto finish_return_check;
 88         }
 89       }
 90       // check that any return ingredients with /same-as-ingredient connect up
 91       // the corresponding ingredient and product in the caller.
 92       for (int i = 0;  i < SIZE(caller_instruction.products);  ++i) {
 93         if (has_property(return_inst.ingredients.at(i), "same-as-ingredient")) {
 94           string_tree* tmp = property(return_inst.ingredients.at(i), "same-as-ingredient");
 95           if (!tmp || !tmp->atom) {
 96             raise << maybe(caller.name) << "'same-as-ingredient' metadata should take exactly one value in '" << to_original_string(return_inst) << "'\n" << end();
 97             goto finish_return_check;
 98           }
 99           int ingredient_index = to_integer(tmp->value);
100           if (ingredient_index >= SIZE(caller_instruction.ingredients)) {
101             raise << maybe(caller.name) << "too few ingredients in '" << to_original_string(caller_instruction) << "'\n" << end();
102             goto finish_return_check;
103           }
104           if (!is_dummy(caller_instruction.products.at(i)) && !is_literal(caller_instruction.ingredients.at(ingredient_index)) && caller_instruction.products.at(i).name != caller_instruction.ingredients.at(ingredient_index).name) {
105             raise << maybe(caller.name) << "'" << to_original_string(caller_instruction) << "' should write to '" << caller_instruction.ingredients.at(ingredient_index).original_string << "' rather than '" << caller_instruction.products.at(i).original_string << "'\n" << end();
106           }
107         }
108       }
109       finish_return_check:;
110     }
111   }
112 }
113 
114 :(scenario return_type_mismatch)
115 % Hide_errors = true;
116 def main [
117   3:num <- f 2
118 ]
119 def f [
120   12:num <- next-ingredient
121   13:num <- copy 35
122   14:point <- copy 12:point/raw
123   return 14:point
124 ]
125 +error: f: return ingredient '14:point' can't be saved in '3:num'
126 
127 //: In Mu we'd like to assume that any instruction doesn't modify its
128 //: ingredients unless they're also products. The /same-as-ingredient inside
129 //: the recipe's 'return' indicates that an ingredient is intended to be
130 //: modified in place, and will help catch accidental misuse of such
131 //: 'ingredient-products' (sometimes called in-out parameters in other
132 //: languages).
133 
134 :(scenario return_same_as_ingredient)
135 % Hide_errors = true;
136 def main [
137   1:num <- copy 0
138   2:num <- test1 1:num  # call with different ingredient and product
139 ]
140 def test1 [
141   10:num <- next-ingredient
142   return 10:num/same-as-ingredient:0
143 ]
144 +error: main: '2:num <- test1 1:num' should write to '1:num' rather than '2:num'
145 
146 :(scenario return_same_as_ingredient_dummy)
147 def main [
148   1:num <- copy 0
149   _ <- test1 1:num  # call with different ingredient and product
150 ]
151 def test1 [
152   10:num <- next-ingredient
153   return 10:num/same-as-ingredient:0
154 ]
155 $error: 0
156 
157 :(code)
158 string to_string(const vector<double>& in) {
159   if (in.empty()) return "[]";
160   ostringstream out;
161   if (SIZE(in) == 1) {
162     out << no_scientific(in.at(0));
163     return out.str();
164   }
165   out << "[";
166   for (int i = 0;  i < SIZE(in);  ++i) {
167     if (i > 0) out << ", ";
168     out << no_scientific(in.at(i));
169   }
170   out << "]";
171   return out.str();
172 }