https://github.com/akkartik/mu1/blob/master/028call_return.cc
1
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);
32 put(Recipe_ordinal, "output", RETURN);
33 :(before "End Primitive Recipe Checks")
34 case RETURN: {
35 break;
36 }
37 :(before "End Primitive Recipe Implementations")
38 case RETURN: {
39
40 if (Trace_stream) {
41 trace("trace") << current_instruction().name << ": decrementing callstack depth from " << Trace_stream->callstack_depth << end();
42 --Trace_stream->callstack_depth;
43 if (Trace_stream->callstack_depth < 0) {
44 Current_routine->calls.clear();
45 goto stop_running_current_routine;
46 }
47 }
48 Current_routine->calls.pop_front();
49
50 if (Current_routine->calls.empty()) goto stop_running_current_routine;
51 for (int i = 0; i < SIZE(ingredients); ++i)
52 trace(9998, "run") << "result " << i << " is " << to_string(ingredients.at(i)) << end();
53
54 copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
55
56 break;
57 }
58
59
60
61 :(before "End Checks")
62 Transform.push_back(check_types_of_return_instructions);
63 :(code)
64 void check_types_of_return_instructions(const recipe_ordinal r) {
65 const recipe& caller = get(Recipe, r);
66 trace(9991, "transform") << "--- check types of return instructions in recipe " << caller.name << end();
67 for (int i = 0; i < SIZE(caller.steps); ++i) {
68 const instruction& caller_instruction = caller.steps.at(i);
69 if (caller_instruction.is_label) continue;
70 if (caller_instruction.products.empty()) continue;
71 if (is_primitive(caller_instruction.operation)) continue;
72 const recipe& callee = get(Recipe, caller_instruction.operation);
73 for (int i = 0; i < SIZE(callee.steps); ++i) {
74 const instruction& return_inst = callee.steps.at(i);
75 if (return_inst.operation != RETURN) continue;
76
77 if (SIZE(caller_instruction.products) > SIZE(return_inst.ingredients)) {
78 raise << maybe(caller.name) << "too few values returned from " << callee.name << '\n' << end();
79 break;
80 }
81 for (int i = 0; i < SIZE(caller_instruction.products); ++i) {
82 reagent lhs = return_inst.ingredients.at(i);
83 reagent rhs = caller_instruction.products.at(i);
84
85 if (!types_coercible(rhs, lhs)) {
86 raise << maybe(callee.name) << return_inst.name << " ingredient '" << lhs.original_string << "' can't be saved in '" << rhs.original_string << "'\n" << end();
87 raise << " ['" << to_string(lhs.type) << "' vs '" << to_string(rhs.type) << "']\n" << end();
88 goto finish_return_check;
89 }
90 }
91
92
93 for (int i = 0; i < SIZE(caller_instruction.products); ++i) {
94 if (has_property(return_inst.ingredients.at(i), "same-as-ingredient")) {
95 string_tree* tmp = property(return_inst.ingredients.at(i), "same-as-ingredient");
96 if (!tmp || !tmp->atom) {
97 raise << maybe(caller.name) << "'same-as-ingredient' metadata should take exactly one value in '" << to_original_string(return_inst) << "'\n" << end();
98 goto finish_return_check;
99 }
100 int ingredient_index = to_integer(tmp->value);
101 if (ingredient_index >= SIZE(caller_instruction.ingredients)) {
102 raise << maybe(caller.name) << "too few ingredients in '" << to_original_string(caller_instruction) << "'\n" << end();
103 goto finish_return_check;
104 }
105 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) {
106 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();
107 }
108 }
109 }
110 finish_return_check:;
111 }
112 }
113 }
114
115 bool is_primitive(recipe_ordinal r) {
116 return r < MAX_PRIMITIVE_RECIPES;
117 }
118
119 :(scenario return_type_mismatch)
120 % Hide_errors = true;
121 def main [
122 3:num <- f 2
123 ]
124 def f [
125 12:num <- next-ingredient
126 13:num <- copy 35
127 14:point <- copy 12:point/raw
128 return 14:point
129 ]
130 +error: f: return ingredient '14:point' can't be saved in '3:num'
131
132
133
134
135
136
137
138
139 :(scenario return_same_as_ingredient)
140 % Hide_errors = true;
141 def main [
142 1:num <- copy 0
143 2:num <- test1 1:num
144 ]
145 def test1 [
146 10:num <- next-ingredient
147 return 10:num/same-as-ingredient:0
148 ]
149 +error: main: '2:num <- test1 1:num' should write to '1:num' rather than '2:num'
150
151 :(scenario return_same_as_ingredient_dummy)
152 def main [
153 1:num <- copy 0
154 _ <- test1 1:num
155 ]
156 def test1 [
157 10:num <- next-ingredient
158 return 10:num/same-as-ingredient:0
159 ]
160 $error: 0
161
162 :(code)
163 string to_string(const vector<double>& in) {
164 if (in.empty()) return "[]";
165 ostringstream out;
166 if (SIZE(in) == 1) {
167 out << no_scientific(in.at(0));
168 return out.str();
169 }
170 out << "[";
171 for (int i = 0; i < SIZE(in); ++i) {
172 if (i > 0) out << ", ";
173 out << no_scientific(in.at(i));
174 }
175 out << "]";
176 return out.str();
177 }