//: Calls can also generate products, using 'reply'.

:(scenario reply)
recipe main [
  1:number, 2:number <- f 34:literal
]
recipe f [
  12:number <- next-ingredient
  13:number <- add 1:literal, 12:number
  reply 12:number, 13:number
]
+mem: storing 34 in location 1
+mem: storing 35 in location 2

:(before "End Primitive Recipe Declarations")
REPLY,
:(before "End Primitive Recipe Numbers")
Recipe_number["reply"] = REPLY;
:(before "End Primitive Recipe Implementations")
case REPLY: {
  const instruction& reply_inst = current_instruction();  // save pointer into recipe before pop
  --Callstack_depth;
//?   if (tb_is_active()) { //? 1
//?     tb_clear(); //? 1
//?     cerr << Recipe[Current_routine->calls.front().running_recipe].name << ' ' << current_step_index() << '\n'; //? 1
//?   } //? 1
  Current_routine->calls.pop_front();
  // just in case 'main' returns a value, drop it for now
  if (Current_routine->calls.empty()) goto stop_running_current_routine;
  const instruction& caller_instruction = current_instruction();
  // make reply results available to caller
  copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
  // check that any reply ingredients with /same-as-ingredient connect up
  // the corresponding ingredient and product in the caller.
  for (long long int i = 0; i < SIZE(caller_instruction.products); ++i) {
//?     cerr << Recipe[Current_routine->calls.front().running_recipe].name << '\n'; //? 1
    trace(Primitive_recipe_depth, "run") << "result " << i << " is " << to_string(ingredients.at(i));
    if (has_property(reply_inst.ingredients.at(i), "same-as-ingredient")) {
      vector<string> tmp = property(reply_inst.ingredients.at(i), "same-as-ingredient");
      assert(SIZE(tmp) == 1);
      long long int ingredient_index = to_integer(tmp.at(0));
      if (caller_instruction.products.at(i).value != caller_instruction.ingredients.at(ingredient_index).value)
        raise << "'same-as-ingredient' result " << caller_instruction.products.at(i).value << " must be location " << caller_instruction.ingredients.at(ingredient_index).value << '\n';
    }
  }
  break;  // continue to process rest of *caller* instruction
}

//: Products can include containers and exclusive containers, addresses and arrays.
:(scenario reply_container)
recipe main [
  3:point <
#
#
#            Nim's Runtime Library
#        (c) Copyright 2015 Andreas Rumpf
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module contains code for reading from `stdin`:idx:. On UNIX the
## linenoise library is wrapped and set up to provide default key bindings
## (e.g. you can navigate with the arrow keys). On Windows ``system.readLine``
## is used. This suffices because Windows' console already provides the
## wanted functionality.

{.deadCodeElim: on.}

when defined(Windows):
  proc readLineFromStdin*(prompt: string): TaintedString {.
                          tags: