mu/056recipe_header.cc

245 lines
7.4 KiB
C++
Raw Normal View History

//: 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")
2015-10-29 20:01:04 +00:00
bool has_header;
vector<reagent> ingredients;
vector<reagent> products;
2015-10-29 20:01:04 +00:00
:(before "End recipe Constructor")
has_header = false;
2015-10-29 20:01:04 +00:00
:(before "End recipe Refinements")
skip_whitespace(in);
if (in.peek() != '[') {
2015-10-29 22:03:36 +00:00
trace(9999, "parse") << "recipe has a header; parsing" << end();
load_recipe_header(in, result);
}
:(code)
void load_recipe_header(istream& in, recipe& result) {
2015-10-29 20:01:04 +00:00
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(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(in);
}
2015-10-29 20:01:04 +00:00
// End Load Recipe Header(result)
}
2015-11-01 04:56:17 +00:00
//: 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();
}
//: Now 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 = Recipe_ordinal["next-ingredient"];
curr.name = "next-ingredient";
curr.products.push_back(result.ingredients.at(i));
result.steps.push_back(curr);
curr.clear();
}
}
:(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'
:(before "End One-time Setup")
Transform.push_back(check_header_products);
:(code)
void check_header_products(const recipe_ordinal r) {
const recipe& rr = Recipe[r];
if (rr.products.empty()) return;
2015-10-29 19:04:30 +00:00
trace(9991, "transform") << "--- checking reply instructions against header for " << rr.name << end();
for (long long int i = 0; i < SIZE(rr.steps); ++i) {
const instruction& inst = rr.steps.at(i);
if (inst.operation != REPLY) continue;
if (SIZE(rr.products) != SIZE(inst.ingredients)) {
raise_error << maybe(rr.name) << "tried to reply the wrong number of products in '" << inst.to_string() << "'\n" << end();
}
for (long long int i = 0; i < SIZE(rr.products); ++i) {
if (!types_match(rr.products.at(i), inst.ingredients.at(i))) {
raise_error << maybe(rr.name) << "replied with the wrong type at '" << inst.to_string() << "'\n" << end();
}
}
}
}
2015-10-30 03:03:16 +00:00
//: 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(transform_names)")
Transform.push_back(deduce_types_from_header);
:(code)
void deduce_types_from_header(const recipe_ordinal r) {
recipe& rr = Recipe[r];
if (rr.products.empty()) return;
2015-11-01 04:59:15 +00:00
trace(9991, "transform") << "--- deduce types from header for " << rr.name << end();
2015-10-30 03:03:16 +00:00
map<string, const type_tree*> header;
for (long long int i = 0; i < SIZE(rr.ingredients); ++i) {
header[rr.ingredients.at(i).name] = rr.ingredients.at(i).type;
}
for (long long int i = 0; i < SIZE(rr.products); ++i) {
header[rr.products.at(i).name] = rr.products.at(i).type;
}
for (long long int i = 0; i < SIZE(rr.steps); ++i) {
instruction& inst = rr.steps.at(i);
trace(9992, "transform") << inst.to_string() << end();
2015-10-30 03:03:16 +00:00
for (long long int i = 0; i < SIZE(inst.ingredients); ++i) {
if (inst.ingredients.at(i).type) continue;
2015-11-01 00:28:17 +00:00
if (header.find(inst.ingredients.at(i).name) == header.end()) {
raise << maybe(rr.name) << "unknown variable " << inst.ingredients.at(i).name << " in '" << inst.to_string() << "'\n" << end();
continue;
}
2015-10-30 03:03:16 +00:00
inst.ingredients.at(i).type = new type_tree(*header[inst.ingredients.at(i).name]);
trace(9993, "transform") << "type of " << inst.ingredients.at(i).name << " is " << dump_types(inst.ingredients.at(i)) << end();
2015-10-30 03:03:16 +00:00
}
for (long long int i = 0; i < SIZE(inst.products); ++i) {
if (inst.products.at(i).type) continue;
2015-11-01 00:28:17 +00:00
if (header.find(inst.products.at(i).name) == header.end()) {
raise << maybe(rr.name) << "unknown variable " << inst.products.at(i).name << " in '" << inst.to_string() << "'\n" << end();
continue;
}
2015-10-30 03:03:16 +00:00
inst.products.at(i).type = new type_tree(*header[inst.products.at(i).name]);
trace(9993, "transform") << "type of " << inst.products.at(i).name << " is " << dump_types(inst.products.at(i)) << end();
2015-10-30 03:03:16 +00:00
}
}
}
//: 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
2015-10-30 03:03:16 +00:00
z <- add x, y
reply
]
+mem: storing 8 in location 1
:(before "End Rewrite Instruction(curr, recipe result)")
if (curr.name == "reply" && curr.ingredients.empty()) {
for (long long int i = 0; i < SIZE(result.products); ++i) {
curr.ingredients.push_back(result.products.at(i));
}
}
2015-10-29 01:32:21 +00:00
:(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
2015-10-30 03:03:16 +00:00
z <- add x, y
2015-10-29 01:32:21 +00:00
]
+transform: reply z:number
2015-10-29 01:32:21 +00:00
+mem: storing 8 in location 1
:(after "int main")
Transform.push_back(deduce_fallthrough_reply);
:(code)
void deduce_fallthrough_reply(const recipe_ordinal r) {
recipe& rr = Recipe[r];
if (rr.products.empty()) return;
if (rr.steps.empty()) return;
if (rr.steps.at(SIZE(rr.steps)-1).name != "reply") {
2015-10-29 01:32:21 +00:00
instruction inst;
inst.operation = REPLY;
inst.name = "reply";
for (long long int i = 0; i < SIZE(rr.products); ++i) {
inst.ingredients.push_back(rr.products.at(i));
}
rr.steps.push_back(inst);
}
}
:(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: reply z
-transform: reply z:number
+mem: storing 8 in location 1