//: 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) def main [ 1:number/raw <- add2 3, 5 ] def add2 x:number, y:number -> z:number [ local-scope load-ingredients z:number <- add x, y return z ] +mem: storing 8 in location 1 //: When loading recipes save any header. :(before "End recipe Fields") bool has_header; vector ingredients; vector products; :(before "End recipe Constructor") has_header = false; :(before "End Recipe Refinements") 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 (has_data(in) && in.peek() != '[' && in.peek() != '\n') { 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_but_not_newline(in); } while (has_data(in) && in.peek() != '[' && in.peek() != '\n') { string s = next_word(in); result.products.push_back(reagent(s)); trace(9999, "parse") << "header product: " << result.products.back().original_string << end(); skip_whitespace_but_not_newline(in); } // End Load Recipe Header(result) } :(scenario recipe_handles_stray_comma) def main [ 1:number/raw <- add2 3, 5 ] def add2 x:number, y:number -> z:number, [ local-scope load-ingredients z:number <- add x, y return z ] +mem: storing 8 in location 1 :(scenario recipe_handles_stray_comma_2) def main [ foo ] def foo, [ 1:number/raw <- add 2, 2 ] def bar [ 1:number/raw <- add 2, 3 ] +mem: storing 4 in location 1 :(scenario recipe_handles_missing_bracket) % Hide_errors = true; def main ] +error: recipe body must begin with '[' :(scenario recipe_handles_missing_bracket_2) % Hide_errors = true; def main local-scope { } ] # doesn't overflow line when reading header -parse: header ingredient: local-scope +error: recipe body must begin with '[' :(scenario recipe_handles_missing_bracket_3) % Hide_errors = true; def main # comment local-scope { } ] # doesn't overflow line when reading header -parse: header ingredient: local-scope +error: recipe body must begin with '[' :(after "Begin debug_string(recipe x)") out << "ingredients:\n"; for (int i = 0; i < SIZE(x.ingredients); ++i) out << " " << debug_string(x.ingredients.at(i)) << '\n'; out << "products:\n"; for (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) def 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 (int i = 0; i < SIZE(result.steps); ++i) { const instruction& inst = result.steps.at(i); if ((inst.name == "reply" && !inst.ingredients.empty()) || (inst.name == "return" && !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(); recipe_ordinal op = get(Recipe_ordinal, "next-ingredient-without-typechecking"); for (int i = 0; i < SIZE(result.ingredients); ++i) { curr.operation = op; curr.name = "next-ingredient-without-typechecking"; curr.products.push_back(result.ingredients.at(i)); result.steps.push_back(curr); curr.clear(); } } //: internal version of next-ingredient; don't call this directly :(before "End Primitive Recipe Declarations") NEXT_INGREDIENT_WITHOUT_TYPECHECKING, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "next-ingredient-without-typechecking", NEXT_INGREDIENT_WITHOUT_TYPECHECKING); :(before "End Primitive Recipe Checks") case NEXT_INGREDIENT_WITHOUT_TYPECHECKING: { break; } :(before "End Primitive Recipe Implementations") case NEXT_INGREDIENT_WITHOUT_TYPECHECKING: { assert(!Current_routine->calls.empty()); if (current_call().next_ingredient_to_process < SIZE(current_call().ingredient_atoms)) { products.push_back( current_call().ingredient_atoms.at(current_call().next_ingredient_to_process)); assert(SIZE(products) == 1); products.resize(2); // push a new vector products.at(1).push_back(1); ++current_call().next_ingredient_to_process; } else { products.resize(2); // pad the first product with sufficient zeros to match its type int size = size_of(current_instruction().products.at(0)); for (int i = 0; i < size; ++i) products.at(0).push_back(0); products.at(1).push_back(0); } break; } //:: Check all calls against headers. :(scenario show_clear_error_on_bad_call) % Hide_errors = true; def main [ 1:number <- foo 34 ] def foo x:point -> y:number [ local-scope load-ingredients return 35 ] +error: main: in
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Mu - mutable.mu</title>
<meta name="Generator" content="Vim/8.0">
<meta name="plugin-version" content="vim7.4_v2">
<meta name="syntax" content="none">
<meta name="settings" content="number_lines,use_css,pre_wrap,no_foldcolumn,expand_tabs,line_ids,prevent_copy=">
<meta name="colorscheme" content="minimal">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #aaaaaa; background-color: #080808; }
body { font-size: 12pt; font-family: monospace; color: #aaaaaa; background-color: #080808; }
a { color:#eeeeee; text-decoration: none; }
a:hover { text-decoration: underline; }
* { font-size: 12pt; font-size: 1em; }