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
101 write_products = false;
102 fall_through_to_next_instruction = false;
103 break;
104 }
105
106 :(scenario call_variable)
107 def main [
108 {1: (recipe number -> number)} <- copy f
109 2:num <- call {1: (recipe number -> number)}, 34
110 ]
111 def f x:num -> y:num [
112 local-scope
113 load-ingredients
114 y <- copy x
115 ]
116 +mem: storing 34 in location 2
117
118 :(scenario call_literal_recipe_repeatedly)
119 def main [
120 1:num <- call f, 34
121 1:num <- call f, 35
122 ]
123 def f x:num -> y:num [
124 local-scope
125 load-ingredients
126 y <- copy x
127 ]
128 +mem: storing 34 in location 1
129 +mem: storing 35 in location 1
130
131 :(scenario call_shape_shifting_recipe)
132 def main [
133 1:num <- call f, 34
134 ]
135 def f x:_elem -> y:_elem [
136 local-scope
137 load-ingredients
138 y <- copy x
139 ]
140 +mem: storing 34 in location 1
141
142 :(scenario call_shape_shifting_recipe_inside_shape_shifting_recipe)
143 def main [
144 1:num <- f 34
145 ]
146 def f x:_elem -> y:_elem [
147 local-scope
148 load-ingredients
149 y <- call g x
150 ]
151 def g x:_elem -> y:_elem [
152 local-scope
153 load-ingredients
154 y <- copy x
155 ]
156 +mem: storing 34 in location 1
157
158 :(scenario call_shape_shifting_recipe_repeatedly_inside_shape_shifting_recipe)
159 def main [
160 1:num <- f 34
161 ]
162 def f x:_elem -> y:_elem [
163 local-scope
164 load-ingredients
165 y <- call g x
166 y <- call g x
167 ]
168 def g x:_elem -> y:_elem [
169 local-scope
170 load-ingredients
171 y <- copy x
172 ]
173 +mem: storing 34 in location 1
174
175
176
177 :(scenario call_check_literal_recipe)
178 % Hide_errors = true;
179 def main [
180 1:num <- call f, 34
181 ]
182 def f x:point -> y:point [
183 local-scope
184 load-ingredients
185 y <- copy x
186 ]
187 +error: main: ingredient 0 has the wrong type at '1:num <- call f, 34'
188 +error: main: product 0 has the wrong type at '1:num <- call f, 34'
189
190 :(scenario call_check_variable_recipe)
191 % Hide_errors = true;
192 def main [
193 {1: (recipe point -> point)} <- copy f
194 2:num <- call {1: (recipe point -> point)}, 34
195 ]
196 def f x:point -> y:point [
197 local-scope
198 load-ingredients
199 y <- copy x
200 ]
201 +error: main: ingredient 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34'
202 +error: main: product 0 has the wrong type at '2:num <- call {1: (recipe point -> point)}, 34'
203
204 :(before "End resolve_ambiguous_call(r, index, inst, caller_recipe) Special-cases")
205 if (inst.name == "call" && !inst.ingredients.empty() && is_recipe_literal(inst.ingredients.at(0))) {
206 resolve_indirect_ambiguous_call(r, index, inst, caller_recipe);
207 return;
208 }
209 :(code)
210 bool is_recipe_literal(const reagent& x) {
211 return x.type && x.type->atom && x.type->name == "recipe-literal";
212 }
213 void resolve_indirect_ambiguous_call(const recipe_ordinal r, int index, instruction& inst, const recipe& caller_recipe) {
214 instruction inst2;
215 inst2.name = inst.ingredients.at(0).name;
216 for (int i = 1; i < SIZE(inst.ingredients); ++i)
217 inst2.ingredients.push_back(inst.ingredients.at(i));
218 for (int i = 0; i < SIZE(inst.products); ++i)
219 inst2.products.push_back(inst.products.at(i));
220 resolve_ambiguous_call(r, index, inst2, caller_recipe);
221 inst.ingredients.at(0).name = inst2.name;
222 inst.ingredients.at(0).set_value(get(Recipe_ordinal, inst2.name));
223 }
224
225 :(after "Transform.push_back(check_instruction)")
226 Transform.push_back(check_indirect_calls_against_header);
227 :(code)
228 void check_indirect_calls_against_header(const recipe_ordinal r) {
229 trace(9991, "transform") << "--- type-check 'call' instructions inside recipe " << get(Recipe, r).name << end();
230 const recipe& caller = get(Recipe, r);
231 for (int i = 0; i < SIZE(caller.steps); ++i) {
232 const instruction& inst = caller.steps.at(i);
233 if (!is_indirect_call(inst.operation)) continue;
234 if (inst.ingredients.empty()) continue;
235 const reagent& callee = inst.ingredients.at(0);
236 if (!is_mu_recipe(callee)) continue;
237 const recipe callee_header = is_literal(callee) ? get(Recipe, callee.value) : from_reagent(inst.ingredients.at(0));
238 if (!callee_header.has_header) continue;
239 if (is_indirect_call_with_ingredients(inst.operation)) {
240 for (long int i = 1; i < min(SIZE(inst.ingredients), SIZE(callee_header.ingredients)+1); ++i) {
241 if (!types_coercible(callee_header.ingredients.at(i-1), inst.ingredients.at(i)))
242 raise << maybe(caller.name) << "ingredient " << i-1 << " has the wrong type at '" << to_original_string(inst) << "'\n" << end();
243 }
244 }
245 if (is_indirect_call_with_products(inst.operation)) {
246 for (long int i = 0; i < min(SIZE(inst.products), SIZE(callee_header.products)); ++i) {
247 if (is_dummy(inst.products.at(i))) continue;
248 if (!types_coercible(callee_header.products.at(i), inst.products.at(i)))
249 raise << maybe(caller.name) << "product " << i << " has the wrong type at '" << to_original_string(inst) << "'\n" << end();
250 }
251 }
252 }
253 }
254
255 bool is_indirect_call(const recipe_ordinal r) {
256 return is_indirect_call_with_ingredients(r) || is_indirect_call_with_products(r);
257 }
258
259 bool is_indirect_call_with_ingredients(const recipe_ordinal r) {
260 if (r == CALL) return true;
261
262 return false;
263 }
264 bool is_indirect_call_with_products(const recipe_ordinal r) {
265 if (r == CALL) return true;
266
267 return false;
268 }
269
270 recipe from_reagent(const reagent& r) {
271 assert(r.type);
272 recipe result_header;
273 result_header.has_header = true;
274
275 if (r.type->atom) {
276 assert(r.type->name == "recipe");
277 return result_header;
278 }
279 const type_tree* root_type = r.type->atom ? r.type : r.type->left;
280 assert(root_type->atom);
281 assert(root_type->name == "recipe");
282 const type_tree* curr = r.type->right;
283 for (; curr && !curr->atom; curr = curr->right) {
284 if (curr->left->atom && curr->left->name == "->") {
285 curr = curr->right;
286 goto read_products;
287 }
288 result_header.ingredients.push_back(next_recipe_reagent(curr->left));
289 }
290 if (curr) {
291 assert(curr->atom);
292 result_header.ingredients.push_back(next_recipe_reagent(curr));
293 return result_header;
294 }
295 read_products:
296 for (; curr && !curr->atom; curr = curr->right)
297 result_header.products.push_back(next_recipe_reagent(curr->left));
298 if (curr) {
299 assert(curr->atom);
300 result_header.products.push_back(next_recipe_reagent(curr));
301 }
302 return result_header;
303 }
304
305 :(before "End Unit Tests")
306 void test_from_reagent_atomic() {
307 reagent a("{f: recipe}");
308 recipe r_header = from_reagent(a);
309 CHECK(r_header.ingredients.empty());
310 CHECK(r_header.products.empty());
311 }
312 void test_from_reagent_non_atomic() {
313 reagent a("{f: (recipe number -> number)}");
314 recipe r_header = from_reagent(a);
315 CHECK_EQ(SIZE(r_header.ingredients), 1);
316 CHECK_EQ(SIZE(r_header.products), 1);
317 }
318 void test_from_reagent_reads_ingredient_at_end() {
319 reagent a("{f: (recipe number number)}");
320 recipe r_header = from_reagent(a);
321 CHECK_EQ(SIZE(r_header.ingredients), 2);
322 CHECK(r_header.products.empty());
323 }
324 void test_from_reagent_reads_sole_ingredient_at_end() {
325 reagent a("{f: (recipe number)}");
326 recipe r_header = from_reagent(a);
327 CHECK_EQ(SIZE(r_header.ingredients), 1);
328 CHECK(r_header.products.empty());
329 }
330
331 :(code)
332 reagent next_recipe_reagent(const type_tree* curr) {
333 if (!curr->left) return reagent("recipe:"+curr->name);
334 reagent result;
335 result.name = "recipe";
336 result.type = new type_tree(*curr);
337 return result;
338 }
339
340 bool is_mu_recipe(const reagent& r) {
341 if (!r.type) return false;
342 if (r.type->atom) {
343
344 return r.type->name == "recipe-literal";
345 }
346 return r.type->left->atom && r.type->left->name == "recipe";
347 }
348
349 :(scenario copy_typecheck_recipe_variable)
350 % Hide_errors = true;
351 def main [
352 3:num <- copy 34
353 {1: (recipe number -> number)} <- copy f
354 {2: (recipe boolean -> boolean)} <- copy {1: (recipe number -> number)}
355 ]
356 def f x:num -> y:num [
357 local-scope
358 load-ingredients
359 y <- copy x
360 ]
361 +error: main: can't copy '{1: (recipe number -> number)}' to '{2: (recipe boolean -> boolean)}'; types don't match
362
363 :(scenario copy_typecheck_recipe_variable_2)
364 % Hide_errors = true;
365 def main [
366 {1: (recipe number -> number)} <- copy f
367 ]
368 def f x:bool -> y:bool [
369 local-scope
370 load-ingredients
371 y <- copy x
372 ]
373 +error: main: can't copy 'f' to '{1: (recipe number -> number)}'; types don't match
374
375 :(before "End Matching Types For Literal(to)")
376 if (is_mu_recipe(to)) {
377 if (!contains_key(Recipe, from.value)) {
378 raise << "trying to store recipe " << from.name << " into " << to_string(to) << " but there's no such recipe\n" << end();
379 return false;
380 }
381 const recipe& rrhs = get(Recipe, from.value);
382 const recipe& rlhs = from_reagent(to);
383 for (long int i = 0; i < min(SIZE(rlhs.ingredients), SIZE(rrhs.ingredients)); ++i) {
384 if (!types_match(rlhs.ingredients.at(i), rrhs.ingredients.at(i)))
385 return false;
386 }
387 for (long int i = 0; i < min(SIZE(rlhs.products), SIZE(rrhs.products)); ++i) {
388 if (!types_match(rlhs.products.at(i), rrhs.products.at(i)))
389 return false;
390 }
391 return true;
392 }
393
394 :(scenario call_variable_compound_ingredient)
395 def main [
396 {1: (recipe (address number) -> number)} <- copy f
397 2:&:num <- copy 0
398 3:num <- call {1: (recipe (address number) -> number)}, 2:&:num
399 ]
400 def f x:&:num -> y:num [
401 local-scope
402 load-ingredients
403 y <- copy x
404 ]
405 $error: 0
406
407
408 :(scenario jump_forbidden_on_recipe_literals)
409 % Hide_errors = true;
410 def foo [
411 local-scope
412 ]
413 def main [
414 local-scope
415 {
416 break-if foo
417 }
418 ]
419
420 +error: main: missing type for 'foo' in 'break-if foo'
421
422 :(before "End JUMP_IF Checks")
423 check_for_recipe_literals(inst, get(Recipe, r));
424 :(before "End JUMP_UNLESS Checks")
425 check_for_recipe_literals(inst, get(Recipe, r));
426 :(code)
427 void check_for_recipe_literals(const instruction& inst, const recipe& caller) {
428 for (int i = 0; i < SIZE(inst.ingredients); ++i) {
429 if (is_mu_recipe(inst.ingredients.at(i))) {
430 raise << maybe(caller.name) << "missing type for '" << inst.ingredients.at(i).original_string << "' in '" << to_original_string(inst) << "'\n" << end();
431 if (is_present_in_ingredients(caller, inst.ingredients.at(i).name))
432 raise << " did you forget 'load-ingredients'?\n" << end();
433 }
434 }
435 }
436
437 :(scenario load_ingredients_missing_error_3)
438 % Hide_errors = true;
439 def foo {f: (recipe num -> num)} [
440 local-scope
441 b:num <- call f, 1
442 ]
443 +error: foo: missing type for 'f' in 'b:num <- call f, 1'
444 +error: did you forget 'load-ingredients'?
445
446 :(before "End Mu Types Initialization")
447 put(Type_abbreviations, "function", new_type_tree("recipe"));
448
449 :(scenario call_function)
450 def main [
451 {1: (function number -> number)} <- copy f
452 2:num <- call {1: (function number -> number)}, 34
453 ]
454 def f x:num -> y:num [
455 local-scope
456 load-ingredients
457 y <- copy x
458 ]
459 +mem: storing 34 in location 2