1
2
3
4
5
6
7 :(scenario call_literal_recipe)
8 def main [
9 1:num <- call f, 34
10 ]
11 def f x:num -> y:num [
12 local-scope
13 load-ingredients
14 y <- copy x
15 ]
16 +mem: storing 34 in location 1
17
18 :(before "End Mu Types Initialization")
19 put(Type_ordinal, "recipe-literal", 0);
20
21 type_ordinal recipe = put(Type_ordinal, "recipe", Next_type_ordinal++);
22 get_or_insert(Type, recipe).name = "recipe";
23
24 :(after "Deduce Missing Type(x, caller)")
25 if (!x.type)
26 try_initialize_recipe_literal(x, caller);
27 :(before "Type Check in Type-ingredient-aware check_or_set_types_by_name")
28 if (!x.type)
29 try_initialize_recipe_literal(x, variant);
30 :(code)
31 void try_initialize_recipe_literal(reagent& x, const recipe& caller) {
32 if (x.type) return;
33 if (!contains_key(Recipe_ordinal, x.name)) return;
34 if (contains_reagent_with_non_recipe_literal_type(caller, x.name)) return;
35 x.type = new type_tree("recipe-literal");
36 x.set_value(get(Recipe_ordinal, x.name));
37 }
38 bool contains_reagent_with_non_recipe_literal_type(const recipe& caller, const string& name) {
39 for (int i = 0; i < SIZE(caller.steps); ++i) {
40 ¦ const instruction& inst = caller.steps.at(i);
41 ¦ for (int i = 0; i < SIZE(inst.ingredients); ++i)
42 ¦ ¦ if (is_matching_non_recipe_literal(inst.ingredients.at(i), name)) return true;
43 ¦ for (int i = 0; i < SIZE(inst.products); ++i)
44 ¦ ¦ if (is_matching_non_recipe_literal(inst.products.at(i), name)) return true;
45 }
46 return false;
47 }
48 bool is_matching_non_recipe_literal(const reagent& x, const string& name) {
49 if (x.name != name) return false;
50 if (!x.type) return false;
51 return !x.type->atom || x.type->name != "recipe-literal";
52 }
53
54
55
56 :(scenario error_on_recipe_literal_used_as_a_variable)
57 % Hide_errors = true;
58 def main [
59 local-scope
60 a:bool <- equal break 0
61 break:bool <- copy 0
62 ]
63 +error: main: missing type for 'break' in 'a:bool <- equal break, 0'
64
65 :(before "End Primitive Recipe Declarations")
66 CALL,
67 :(before "End Primitive Recipe Numbers")
68 put(Recipe_ordinal, "call", CALL);
69 :(before "End Primitive Recipe Checks")
70 case CALL: {
71 if (inst.ingredients.empty()) {
72 ¦ raise << maybe(get(Recipe, r).name) << "'call' requires at least one ingredient (the recipe to call)\n" << end();
73 ¦ break;
74 }
75 if (!is_mu_recipe(inst.ingredients.at(0))) {
76 ¦ raise << maybe(get(Recipe, r).name) << "first ingredient of 'call' should be a recipe, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
77 ¦ break;
78 }
79 break;
80 }
81 :(before "End Primitive Recipe Implementations")
82 case CALL: {
83
84 if (Trace_stream) {
85 ¦ ++Trace_stream->callstack_depth;
86 ¦ trace("trace") << "indirect 'call': incrementing callstack depth to " << Trace_stream->callstack_depth << end();
87 ¦ assert(Trace_stream->callstack_depth < 9000);
88 }
89 if (!ingredients.at(0).at(0)) {
90 ¦ raise << maybe(current_recipe_name()) << "tried to call empty recipe in '" << to_string(current_instruction()) << "'" << end();
91 ¦ break;
92 }
93 const call& caller_frame = current_call();
94 instruction call_instruction = to_instruction(caller_frame);
95 call_instruction.operation = ingredients.at(0).at(0);
96 call_instruction.ingredients.erase(call_instruction.ingredients.begin());
97 Current_routine->calls.push_front(call(ingredients.at(0).at(0)));
98 ingredients.erase(ingredients.begin());
99 finish_call_housekeeping(call_instruction, ingredients);
100 Num_refcount_updates[caller_frame.running_recipe][caller_frame.running_step_index]
101 ¦ ¦ += (Total_refcount_updates - initial_num_refcount_updates);
102 initial_num_refcount_updates = Total_refcount_updates;
103
104 write_products = false;
105 fall_through_to_next_instruction = false;
106 break;
107 }
108
109 :(scenario call_variable)
110 def main [
111 {1: (recipe number -> number)} <- copy f
112 2:num <- call {1: (recipe number -> number)}, 34
113 ]
114 def f x:num -> y:num [
115 local-scope
116 load-ingredients
117 y <- copy x
118 ]
119 +mem: storing 34 in location 2
120
121 :(scenario call_literal_recipe_repeatedly)
122 def main [
123 1:num <- call f, 34
124 1:num <- call f, 35
125 ]
126 def f x:num -> y:num [
127 local-scope
128 load-ingredients
129 y <- copy x
130 ]
131 +mem: storing 34 in location 1
132 +mem: storing 35 in location 1
133
134 :(scenario call_shape_shifting_recipe)
135 def main [
136 1:num <- call f, 34
137 ]
138 def f x:_elem -> y:_elem [
139 local-scope
140 load-ingredients
141 y <- copy x
142 ]
143 +mem: storing 34 in location 1
144
145 :(scenario call_shape_shifting_recipe_inside_shape_shifting_recipe)
146 def main [
147 1:num <- f 34
148 ]
149 def f x:_elem -> y:_elem [
150 local-scope
151 load-ingredients
152 y <- call g x
153 ]
154 def g x:_elem -> y:_elem [
155 local-scope
156 load-ingredients
157 y <- copy x
158 ]
159 +mem: storing 34 in location 1
160
161 :(scenario call_shape_shifting_recipe_repeatedly_inside_shape_shifting_recipe)
162 def main [
163 1:num <- f 34
164 ]
165 def f x:_elem -> y:_elem [
166 local-scope
167 load-ingredients
168 y <- call g x
169 y <- call g x
170 ]
171 def g x:_elem -> y:_elem [
172 local-scope
173 load-ingredients
174 y <- copy x
175 ]
176 +mem: storing 34 in location 1
177
178
179
180 :(scenario call_check_literal_recipe)
181 % Hide_errors = true;
182 def main [
183 1:num <- call f, 34
184 ]
185 def f x:point -> y:point [
186 local-scope
187 load-ingredients
188 y <- copy x
189 ]
190 +error: main: ingredient 0 has the wrong type at '1:num <- call f, 34'
191 +error: main: product 0 has the wrong type at '1:num <- call f, 34'
192
193 :(scenario call_check_variable_recipe)
194 % Hide_errors = true;
195 def main [
196 {1: (recipe point -> point)} <- copy f
197 2:num <- call {1: (recipe point -> point)}, 34
198 ]
199 def f x:point -> y:point [
200 local-scope
201 load-ingredients
202 y <- copy x
203 ]
204 +error: main: ingredient 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34'
205 +error: main: product 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34'
206
207 :(before "End resolve_ambiguous_call(r, index, inst, caller_recipe) Special-cases")
208 if (inst.name == "call" && !inst.ingredients.empty() && is_recipe_literal(inst.ingredients.at(0))) {
209 resolve_indirect_ambiguous_call(r, index, inst, caller_recipe);
210 return;
211 }
212 :(code)
213 bool is_recipe_literal(const reagent& x) {
214 return x.type && x.type->atom && x.type->name == "recipe-literal";
215 }
216 void resolve_indirect_ambiguous_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) {
217 instruction inst2;
218 inst2.name = inst.ingredients.at(0).name;
219 for (int i = 1; i < SIZE(inst.ingredients); ++i)
220 ¦ inst2.ingredients.push_back(inst.ingredients.at(i));
221 for (int i = 0; i < SIZE(inst.products); ++i)
222 ¦ inst2.products.push_back(inst.products.at(i));
223 resolve_ambiguous_call(r, index, inst2, caller_recipe);
224 inst.ingredients.at(0).name = inst2.name;
225 inst.ingredients.at(0).set_value(get(Recipe_ordinal, inst2.name));
226 }
227
228 :(after "Transform.push_back(check_instruction)")
229 Transform.push_back(check_indirect_calls_against_header);
230 :(code)
231 void check_indirect_calls_against_header(const recipe_ordinal r) {
232 trace(9991, "transform") << "--- type-check 'call' instructions inside recipe " << get(Recipe, r).name << end();
233 const recipe& caller = get(Recipe, r);
234 for (int i = 0; i < SIZE(caller.steps); ++i) {
235 ¦ const instruction& inst = caller.steps.at(i);
236 ¦ if (!is_indirect_call(inst.operation)) continue;
237 ¦ if (inst.ingredients.empty()) continue;
238 ¦ const reagent& callee = inst.ingredients.at(0);
239 ¦ if (!is_mu_recipe(callee)) continue;
240 ¦ const recipe callee_header = is_literal(callee) ? get(Recipe, callee.value) : from_reagent(inst.ingredients.at(0));
241 ¦ if (!callee_header.has_header) continue;
242 ¦ if (is_indirect_call_with_ingredients(inst.operation)) {
243 ¦ ¦ for (long int i = 1; i < min(SIZE(inst.ingredients), SIZE(callee_header.ingredients)+1); ++i) {
244 ¦ ¦ ¦ if (!types_coercible(callee_header.ingredients.at(i-1), inst.ingredients.at(i)))
245 ¦ ¦ ¦ ¦ raise << maybe(caller.name) << "ingredient " << i-1 << " has the wrong type at '" << to_original_string(inst) << "'\n" << end();
246 ¦ ¦ }
247 ¦ }
248 ¦ if (is_indirect_call_with_products(inst.operation)) {
249 ¦ ¦ for (long int i = 0; i < min(SIZE(inst.products), SIZE(callee_header.products)); ++i) {
250 ¦ ¦ ¦ if (is_dummy(inst.products.at(i))) continue;
251 ¦ ¦ ¦ if (!types_coercible(callee_header.products.at(i), inst.products.at(i)))
252 ¦ ¦ ¦ ¦ raise << maybe(caller.name) << "product " << i << " has the wrong type at '" << to_original_string(inst) << "'\n" << end();
253 ¦ ¦ }
254 ¦ }
255 }
256 }
257
258 bool is_indirect_call(const recipe_ordinal r) {
259 return is_indirect_call_with_ingredients(r) || is_indirect_call_with_products(r);
260 }
261
262 bool is_indirect_call_with_ingredients(const recipe_ordinal r) {
263 if (r == CALL) return true;
264
265 return false;
266 }
267 bool is_indirect_call_with_products(const recipe_ordinal r) {
268 if (r == CALL) return true;
269
270 return false;
271 }
272
273 recipe from_reagent(const reagent& r) {
274 assert(r.type);
275 recipe result_header;
276 result_header.has_header = true;
277
278 if (r.type->atom) {
279 ¦ assert(r.type->name == "recipe");
280 ¦ return result_header;
281 }
282 const type_tree* root_type = r.type->atom ? r.type : r.type->left;
283 assert(root_type->atom);
284 assert(root_type->name == "recipe");
285 const type_tree* curr = r.type->right;
286 for (; curr && !curr->atom; curr = curr->right) {
287 ¦ if (curr->left->atom && curr->left->name == "->") {
288 ¦ ¦ curr = curr->right;
289 ¦ ¦ goto read_products;
290 ¦ }
291 ¦ result_header.ingredients.push_back(next_recipe_reagent(curr->left));
292 }
293 if (curr) {
294 ¦ assert(curr->atom);
295 ¦ result_header.ingredients.push_back(next_recipe_reagent(curr));
296 ¦ return result_header;
297 }
298 read_products:
299 for (; curr && !curr->atom; curr = curr->right)
300 ¦ result_header.products.push_back(next_recipe_reagent(curr->left));
301 if (curr) {
302 ¦ assert(curr->atom);
303 ¦ result_header.products.push_back(next_recipe_reagent(curr));
304 }
305 return result_header;
306 }
307
308 :(before "End Unit Tests")
309 void test_from_reagent_atomic() {
310 reagent a("{f: recipe}");
311 recipe r_header = from_reagent(a);
312 CHECK(r_header.ingredients.empty());
313 CHECK(r_header.products.empty());
314 }
315 void test_from_reagent_non_atomic() {
316 reagent a("{f: (recipe number -> number)}");
317 recipe r_header = from_reagent(a);
318 CHECK_EQ(SIZE(r_header.ingredients), 1);
319 CHECK_EQ(SIZE(r_header.products), 1);
320 }
321 void test_from_reagent_reads_ingredient_at_end() {
322 reagent a("{f: (recipe number number)}");
323 recipe r_header = from_reagent(a);
324 CHECK_EQ(SIZE(r_header.ingredients), 2);
325 CHECK(r_header.products.empty());
326 }
327 void test_from_reagent_reads_sole_ingredient_at_end() {
328 reagent a("{f: (recipe number)}");
329 recipe r_header = from_reagent(a);
330 CHECK_EQ(SIZE(r_header.ingredients), 1);
331 CHECK(r_header.products.empty());
332 }
333
334 :(code)
335 reagent next_recipe_reagent(const type_tree* curr) {
336 if (!curr->left) return reagent("recipe:"+curr->name);
337 reagent result;
338 result.name = "recipe";
339 result.type = new type_tree(*curr);
340 return result;
341 }
342
343 bool is_mu_recipe(const reagent& r) {
344 if (!r.type) return false;
345 if (r.type->atom) {
346 ¦
347 ¦ return r.type->name == "recipe-literal";
348 }
349 return r.type->left->atom && r.type->left->name == "recipe";
350 }
351
352 :(scenario copy_typecheck_recipe_variable)
353 % Hide_errors = true;
354 def main [
355 3:num <- copy 34
356 {1: (recipe number -> number)} <- copy f
357 {2: (recipe boolean -> boolean)} <- copy {1: (recipe number -> number)}
358 ]
359 def f x:num -> y:num [
360 local-scope
361 load-ingredients
362 y <- copy x
363 ]
364 +error: main: can't copy '{1: (recipe number -> number)}' to '{2: (recipe boolean -> boolean)}'; types don't match
365
366 :(scenario copy_typecheck_recipe_variable_2)
367 % Hide_errors = true;
368 def main [
369 {1: (recipe number -> number)} <- copy f
370 ]
371 def f x:bool -> y:bool [
372 local-scope
373 load-ingredients
374 y <- copy x
375 ]
376 +error: main: can't copy 'f' to '{1: (recipe number -> number)}'; types don't match
377
378 :(before "End Matching Types For Literal(to)")
379 if (is_mu_recipe(to)) {
380 if (!contains_key(Recipe, from.value)) {
381 ¦ raise << "trying to store recipe " << from.name << " into " << to_string(to) << " but there's no such recipe\n" << end();
382 ¦ return false;
383 }
384 const recipe& rrhs = get(Recipe, from.value);
385 const recipe& rlhs = from_reagent(to);
386 for (long int i = 0; i < min(SIZE(rlhs.ingredients), SIZE(rrhs.ingredients)); ++i) {
387 ¦ if (!types_match(rlhs.ingredients.at(i), rrhs.ingredients.at(i)))
388 ¦ ¦ return false;
389 }
390 for (long int i = 0; i < min(SIZE(rlhs.products), SIZE(rrhs.products)); ++i) {
391 ¦ if (!types_match(rlhs.products.at(i), rrhs.products.at(i)))
392 ¦ ¦ return false;
393 }
394 return true;
395 }
396
397 :(scenario call_variable_compound_ingredient)
398 def main [
399 {1: (recipe (address number) -> number)} <- copy f
400 2:&:num <- copy 0
401 3:num <- call {1: (recipe (address number) -> number)}, 2:&:num
402 ]
403 def f x:&:num -> y:num [
404 local-scope
405 load-ingredients
406 y <- copy x
407 ]
408 $error: 0
409
410
411 :(scenario jump_forbidden_on_recipe_literals)
412 % Hide_errors = true;
413 def foo [
414 local-scope
415 ]
416 def main [
417 local-scope
418 {
419 ¦ break-if foo
420 }
421 ]
422
423 +error: main: missing type for 'foo' in 'break-if foo'
424
425 :(before "End JUMP_IF Checks")
426 check_for_recipe_literals(inst, get(Recipe, r));
427 :(before "End JUMP_UNLESS Checks")
428 check_for_recipe_literals(inst, get(Recipe, r));
429 :(code)
430 void check_for_recipe_literals(const instruction& inst, const recipe& caller) {
431 for (int i = 0; i < SIZE(inst.ingredients); ++i) {
432 ¦ if (is_mu_recipe(inst.ingredients.at(i))) {
433 ¦ ¦ raise << maybe(caller.name) << "missing type for '" << inst.ingredients.at(i).original_string << "' in '" << to_original_string(inst) << "'\n" << end();
434 ¦ ¦ if (is_present_in_ingredients(caller, inst.ingredients.at(i).name))
435 ¦ ¦ ¦ raise << " did you forget 'load-ingredients'?\n" << end();
436 ¦ }
437 }
438 }
439
440 :(scenario load_ingredients_missing_error_3)
441 % Hide_errors = true;
442 def foo {f: (recipe num -> num)} [
443 local-scope
444 b:num <- call f, 1
445 ]
446 +error: foo: missing type for 'f' in 'b:num <- call f, 1'
447 +error: did you forget 'load-ingredients'?
448
449 :(before "End Mu Types Initialization")
450 put(Type_abbreviations, "function", new_type_tree("recipe"));
451
452 :(scenario call_function)
453 def main [
454 {1: (function number -> number)} <- copy f
455 2:num <- call {1: (function number -> number)}, 34
456 ]
457 def f x:num -> y:num [
458 local-scope
459 load-ingredients
460 y <- copy x
461 ]
462 +mem: storing 34 in location 2