//: So far the recipes we define can't run each other. Let's fix that.

:(scenario calling_recipe)
def main [
  f
]
def f [
  3:num <- add 2, 2
]
+mem: storing 4 in location 3

:(scenario return_on_fallthrough)
def main [
  f
  1:num <- copy 0
  2:num <- copy 0
  3:num <- copy 0
]
def f [
  4:num <- copy 0
  5:num <- copy 0
]
+run: f
# running f
+run: {4: "number"} <- copy {0: "literal"}
+run: {5: "number"} <- copy {0: "literal"}
# back out to main
+run: {1: "number"} <- copy {0: "literal"}
+run: {2: "number"} <- copy {0: "literal"}
+run: {3: "number"} <- copy {0: "literal"}

:(before "struct routine {")
// Everytime a recipe runs another, we interrupt it and start running the new
// recipe. When that finishes, we continue this one where we left off.
// This requires maintaining a 'stack' of interrupted recipes or 'calls'.
struct call {
  recipe_ordinal running_recipe;
  int running_step_index;
  // End call Fields
  call(recipe_ordinal r) {
    running_recipe = r;
    running_step_index = 0;
    // End call Constructor
  }
  ~call() {
    // End call Destructor
  }
};
typedef list<call> call_stack;

:(replace{} "struct routine")
struct routine {
  call_stack calls;
  // End routine Fields
  routine(recipe_ordinal r);
  bool completed() const;
  const vector<instruction>& steps() const;
};
:(code)
routine::routine(recipe_ordinal r) {
  if (Trace_stream) {
    ++Trace_stream->callstack_depth;
    trace(9999, "trace") << "new routine; incrementing callstack depth to " << Trace_stream->callstack_depth << end();
    assert(Trace_stream->callstack_depth < 9000);  // 9998-101 plus cushion
  }
  calls.push_front(call(r));
  // End routine Constructor
}

:(code)
call& current_call() {
  return Current_routine->calls.front();
}

//:: now update routine's helpers

:(replace{} "int& current_step_index()")
int& current_step_index() {
  assert(!Current_routine->calls.empty());
  return current_call().running_step_index;
}
:(replace{} "const string& current_recipe_name()")
const string& current_recipe_name() {
  assert(!Current_routine->calls.empty());
  return get(Recipe, current_call().running_recipe).name;
}
:(replace{} "const recipe& current_recipe()")
const recipe& current_recipe() {
  assert
<!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 - apps/factorial4.subx</title>
<meta name="Generator" content="Vim/8.1">
<meta name="plugin-version" content="vim8.1_v1">
<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-light">
<style type="text/css">
<!--
pre { white-space: pre-wrap; font-family: monospace; color: #000000; background-color: #c6c6c6; }
body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; }
a { color:inherit; }
* { font-size:12<