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