1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
//: Advanced notation for the common/easy case where a recipe takes some fixed
//: number of ingredients and yields some fixed number of products.
:(scenario recipe_with_header)
recipe main [
1:number/raw <- add2 3, 5
]
recipe add2 x:number, y:number -> z:number [
local-scope
load-ingredients
z:number <- add x, y
reply z
]
+mem: storing 8 in location 1
//: When loading recipes save any header.
:(before "End recipe Fields")
bool has_header;
vector<reagent> ingredients;
vector<reagent> products;
:(before "End recipe Constructor")
has_header = false;
:(before "End recipe Refinements")
skip_whitespace(in);
if (in.peek() != '[') {
trace(9999, "parse") << "recipe has a header; parsing" << end();
load_recipe_header(in, result);
}
:(code)
void load_recipe_header(istream& in, recipe& result) {
result.has_header = true;
while (in.peek() != '[') {
string s = next_word(in);
if (s == "->") break;
result.ingredients.push_back(reagent(s));
trace(9999, "parse") << "header ingredient: " << result.ingredients.back().original_string << end();
skip_whitespace_and_comments(in);
}
while (in.peek() != '[') {
string s = next_word(in);
result.products.push_back(reagent(s));
trace(9999, "parse") << "header product: " << result.products.back().original_string << end();
skip_whitespace_and_comments(in);
}
// End Load Recipe Header(result)
}
:(scenario recipe_handles_stray_comma)
recipe main [
1:number/raw <- add2 3, 5
]
recipe add2 x:number, y:number -> z:number, [
local-scope
load-ingredients
z:number <- add x, y
reply z
]
+mem: storing 8 in location 1
:(scenario recipe_handles_stray_comma_2)
recipe main [
foo
]
recipe foo, [
1:number/raw <- add 2, 2
]
recipe bar [
1:number/raw <- add 2, 3
]
+mem: storing 4 in location 1
:(after "Begin debug_string(recipe x)")
out << "ingredients:\n";
for (long long int i = 0; i < SIZE(x.ingredients); ++i)
out << " " << debug_string(x.ingredients.at(i)) << '\n';
out << "products:\n";
for (long long int i = 0; i < SIZE(x.products); ++i)
out << " " << debug_string(x.products.at(i)) << '\n';
//: If a recipe never mentions any ingredients or products, assume it has a header.
:(scenario recipe_without_ingredients_or_products_has_header)
recipe test [
1:number <- copy 34
]
+parse: recipe test has a header
:(before "End recipe Body(result)")
if (!result.has_header) {
result.has_header = true;
for (long long int i = 0; i < SIZE(result.steps); ++i) {
const instruction& inst = result.steps.at(i);
if ((inst.name == "reply" && !inst.ingredients.empty())
|| inst.name == "next-ingredient"
|| inst.name == "ingredient"
|| inst.name == "rewind-ingredients") {
result.has_header = false;
break;
}
}
}
if (result.has_header) {
trace(9999, "parse") << "recipe " << result.name << " has a header" << end();
}
//: Rewrite 'load-ingredients' to instructions to create all reagents in the header.
:(before "End Rewrite Instruction(curr, recipe result)")
if (curr.name == "load-ingredients") {
curr.clear();
for (long long int i = 0; i < SIZE(result.ingredients); ++i) {
curr.operation = get(Recipe_ordinal, "next-ingredient");
curr.name = "next-ingredient";
curr.products.push_back(result.ingredients.at(i));
result.steps.push_back(curr);
curr.clear();
}
}
//:: Check types going in and out of all recipes with headers.
:(scenarios transform)
:(scenario recipe_headers_are_checked)
% Hide_errors = true;
recipe add2 x:number, y:number -> z:number [
local-scope
load-ingredients
z:address:number <- copy 0/raw
reply z
]
+error: add2: replied with the wrong type at 'reply z'
:(after "Transform.push_back(check_types_by_name)")
Transform.push_back(check_reply_instructions_against_header); // idempotent
:(code)
void check_reply_instructions_against_header(const recipe_ordinal r) {
const recipe& caller_recipe = get(Recipe, r);
if (caller_recipe.products.empty()) return;
trace(9991, "transform") << "--- checking reply instructions against header for " << caller_recipe.name << end();
//? cerr << "--- checking reply instructions against header for " << caller_recipe.name << '\n';
for (long long int i = 0; i < SIZE(caller_recipe.steps); ++i) {
const instruction& inst = caller_recipe.steps.at(i);
if (inst.name != "reply") continue;
if (SIZE(caller_recipe.products) != SIZE(inst.ingredients))
raise_error << maybe(caller_recipe.name) << "tried to reply the wrong number of products in '" << inst.to_string() << "'\n" << end();
for (long long int i = 0; i < SIZE(caller_recipe.products); ++i) {
if (!types_match(caller_recipe.products.at(i), inst.ingredients.at(i)))
raise_error << maybe(caller_recipe.name) << "replied with the wrong type at '" << inst.to_string() << "'\n" << end();
}
}
}
:(scenario recipe_headers_check_for_duplicate_names)
% Hide_errors = true;
recipe add2 x:number, x:number -> z:number [
local-scope
load-ingredients
reply z
]
+error: add2: x can't repeat in the ingredients
:(before "End recipe Fields")
map<string, int> ingredient_index;
:(after "Transform.push_back(insert_fragments)")
Transform.push_back(check_and_update_header_reagents); // idempotent
:(code)
void check_and_update_header_reagents(const recipe_ordinal r) {
recipe& caller_recipe = get(Recipe, r);
if (caller_recipe.products.empty()) return;
trace(9991, "transform") << "--- checking reply instructions against header for " << caller_recipe.name << end();
for (long long int i = 0; i < SIZE(caller_recipe.ingredients); ++i) {
if (contains_key(caller_recipe.ingredient_index, caller_recipe.ingredients.at(i).name))
raise_error << maybe(caller_recipe.name) << caller_recipe.ingredients.at(i).name << " can't repeat in the ingredients\n" << end();
put(caller_recipe.ingredient_index, caller_recipe.ingredients.at(i).name, i);
}
}
//: Deduce types from the header if possible.
:(scenarios run)
:(scenario deduce_instruction_types_from_recipe_header)
recipe main [
1:number/raw <- add2 3, 5
]
recipe add2 x:number, y:number -> z:number [
local-scope
load-ingredients
z <- add x, y # no type for z
reply z
]
+mem: storing 8 in location 1
:(before "Transform.push_back(check_reply_instructions_against_header)")
Transform.push_back(deduce_types_from_header); // idempotent
:(code)
void deduce_types_from_header(const recipe_ordinal r) {
recipe& caller_recipe = get(Recipe, r);
if (caller_recipe.products.empty()) return;
trace(9991, "transform") << "--- deduce types from header for " << caller_recipe.name << end();
//? cerr << "--- deduce types from header for " << caller_recipe.name << '\n';
map<string, const type_tree*> header_type;
map<string, const string_tree*> header_type_name;
for (long long int i = 0; i < SIZE(caller_recipe.ingredients); ++i) {
put(header_type, caller_recipe.ingredients.at(i).name, caller_recipe.ingredients.at(i).type);
put(header_type_name, caller_recipe.ingredients.at(i).name, caller_recipe.ingredients.at(i).properties.at(0).second);
trace(9993, "transform") << "type of " << caller_recipe.ingredients.at(i).name << " is " << debug_string(caller_recipe.ingredients.at(i).type) << end();
}
for (long long int i = 0; i < SIZE(caller_recipe.products); ++i) {
put(header_type, caller_recipe.products.at(i).name, caller_recipe.products.at(i).type);
put(header_type_name, caller_recipe.products.at(i).name, caller_recipe.products.at(i).properties.at(0).second);
trace(9993, "transform") << "type of " << caller_recipe.products.at(i).name << " is " << debug_string(caller_recipe.products.at(i).type) << end();
}
for (long long int i = 0; i < SIZE(caller_recipe.steps); ++i) {
instruction& inst = caller_recipe.steps.at(i);
trace(9992, "transform") << "instruction: " << inst.to_string() << end();
for (long long int i = 0; i < SIZE(inst.ingredients); ++i) {
if (inst.ingredients.at(i).type) continue;
if (header_type.find(inst.ingredients.at(i).name) == header_type.end()) {
raise << maybe(caller_recipe.name) << "unknown variable " << inst.ingredients.at(i).name << " in '" << inst.to_string() << "'\n" << end();
continue;
}
if (!inst.ingredients.at(i).type)
inst.ingredients.at(i).type = new type_tree(*get(header_type, inst.ingredients.at(i).name));
if (!inst.ingredients.at(i).properties.at(0).second)
inst.ingredients.at(i).properties.at(0).second = new string_tree(*get(header_type_name, inst.ingredients.at(i).name));
trace(9993, "transform") << "type of " << inst.ingredients.at(i).name << " is " << debug_string(inst.ingredients.at(i).type) << end();
}
for (long long int i = 0; i < SIZE(inst.products); ++i) {
trace(9993, "transform") << " product: " << debug_string(inst.products.at(i)) << end();
if (inst.products.at(i).type) continue;
if (header_type.find(inst.products.at(i).name) == header_type.end()) {
raise << maybe(caller_recipe.name) << "unknown variable " << inst.products.at(i).name << " in '" << inst.to_string() << "'\n" << end();
continue;
}
if (!inst.products.at(i).type)
inst.products.at(i).type = new type_tree(*get(header_type, inst.products.at(i).name));
if (!inst.products.at(i).properties.at(0).second)
inst.products.at(i).properties.at(0).second = new string_tree(*get(header_type_name, inst.products.at(i).name));
trace(9993, "transform") << "type of " << inst.products.at(i).name << " is " << debug_string(inst.products.at(i).type) << end();
}
}
}
//: One final convenience: no need to say what to return if the information is
//: in the header.
:(scenario reply_based_on_header)
recipe main [
1:number/raw <- add2 3, 5
]
recipe add2 x:number, y:number -> z:number [
local-scope
load-ingredients
z <- add x, y
reply
]
+mem: storing 8 in location 1
:(after "Transform.push_back(check_and_update_header_reagents)")
Transform.push_back(fill_in_reply_ingredients); // idempotent
:(code)
void fill_in_reply_ingredients(recipe_ordinal r) {
recipe& caller_recipe = get(Recipe, r);
if (!caller_recipe.has_header) return;
trace(9991, "transform") << "--- fill in reply ingredients from header for recipe " << caller_recipe.name << end();
for (long long int i = 0; i < SIZE(caller_recipe.steps); ++i) {
instruction& inst = caller_recipe.steps.at(i);
if (inst.name == "reply" && inst.ingredients.empty())
add_header_products(inst, caller_recipe);
}
// fall through reply
if (caller_recipe.steps.at(SIZE(caller_recipe.steps)-1).name != "reply") {
instruction inst;
inst.name = "reply";
add_header_products(inst, caller_recipe);
caller_recipe.steps.push_back(inst);
}
}
void add_header_products(instruction& inst, const recipe& caller_recipe) {
assert(inst.name == "reply");
// collect any products with the same names as ingredients
for (long long int i = 0; i < SIZE(caller_recipe.products); ++i) {
// if the ingredient is missing, add it from the header
if (SIZE(inst.ingredients) == i)
inst.ingredients.push_back(caller_recipe.products.at(i));
// if it's missing /same_as_ingredient, try to fill it in
if (contains_key(caller_recipe.ingredient_index, caller_recipe.products.at(i).name) && !has_property(inst.ingredients.at(i), "same_as_ingredient")) {
ostringstream same_as_ingredient;
same_as_ingredient << get(caller_recipe.ingredient_index, caller_recipe.products.at(i).name);
inst.ingredients.at(i).properties.push_back(pair<string, string_tree*>("same-as-ingredient", new string_tree(same_as_ingredient.str())));
}
}
}
:(scenario explicit_reply_ignores_header)
recipe main [
1:number/raw, 2:number/raw <- add2 3, 5
]
recipe add2 a:number, b:number -> y:number, z:number [
local-scope
load-ingredients
y <- add a, b
z <- subtract a, b
reply a, z
]
+mem: storing 3 in location 1
+mem: storing -2 in location 2
:(scenario reply_on_fallthrough_based_on_header)
recipe main [
1:number/raw <- add2 3, 5
]
recipe add2 x:number, y:number -> z:number [
local-scope
load-ingredients
z <- add x, y
]
+transform: instruction: reply z:number
+mem: storing 8 in location 1
:(scenario reply_on_fallthrough_already_exists)
recipe main [
1:number/raw <- add2 3, 5
]
recipe add2 x:number, y:number -> z:number [
local-scope
load-ingredients
z <- add x, y # no type for z
reply z
]
+transform: instruction: reply z
-transform: instruction: reply z:number
+mem: storing 8 in location 1
:(scenario recipe_headers_perform_same_ingredient_check)
% Hide_errors = true;
recipe main [
1:number <- copy 34
2:number <- copy 34
3:number <- add2 1:number, 2:number
]
recipe add2 x:number, y:number -> x:number [
local-scope
load-ingredients
]
+error: main: '3:number <- add2 1:number, 2:number' should write to 1:number rather than 3:number
|