//: Phase 3: Start running a loaded and transformed recipe.
//:
//:   The process of running Mu code:
//:     load -> transform -> run
//:
//: So far we've seen recipes as lists of instructions, and instructions point
//: at other recipes. To kick things off Mu needs to know how to run certain
//: 'primitive' recipes. That will then give the ability to run recipes
//: containing these primitives.
//:
//: This layer defines a skeleton with just two primitive recipes: IDLE which
//: does nothing, and COPY, which can copy numbers from one memory location to
//: another. Later layers will add more primitives.

:(scenario copy_literal)
def main [
  1:num <- copy 23
]
+run: {1: "number"} <- copy {23: "literal"}
+mem: storing 23 in location 1

:(scenario copy)
def main [
  1:num <- copy 23
  2:num <- copy 1:num
]
+run: {2: "number"} <- copy {1: "number"}
+mem: location 1 is 23
+mem: storing 23 in location 2

:(scenario copy_multiple)
def main [
  1:num, 2:num <- copy 23, 24
]
+mem: storing 23 in location 1
+mem: storing 24 in location 2

:(before "End Types")
// Book-keeping while running a recipe.
//: Later layers will replace this to support running multiple routines at once.
struct routine {
  recipe_ordinal running_recipe;
  int running_step_index;
  routine(recipe_ordinal r) :running_recipe(r), running_step_index(0) {}
  bool completed() const;
  const vector<instruction>& steps() const;
};

:(before "End Globals")
routine* Current_routine = NULL;
map<string, int> Instructions_running;
map<string, int> Locations_read;
map<string, int> Locations_read_by_instruction;
:(before "End Setup")
Current_routine = NULL;

:(code)
void run(const recipe_ordinal r) {
  routine rr(r);
  Current_routine = &rr;
  run_current_routine();
  Current_routine = NULL;
}

void run_current_routine() {
  while (should_continue_running(Current_routine)) {  // beware: later layers modify Current_routine here
    // Running One Instruction
    if (current_instruction().is_label) { ++current_step_index();  continue; }
    trace(Initial_callstack_depth + Trace_stream->callstack_depth, "run") << to_string(current_instruction()) << end();
    if (get_or_insert(Memory, 0) != 0) {
      raise << "something wrote to location 0; this should never happen\n" << end();
      put(Memory, 0, 0);
    }
    // read all ingredients from memory, each potentially spanning multiple locations
    vector<vector<double> > ingredients;
    if (should_copy_ingredients()) {
      for (int i = 0;  i < SIZE(current_instruction().ingredients);  ++i)
        ingredients.push_back(read_memory(current_instruction().ingredients.at(i)));
    }
    // instructions below will write to 'products'
    vector<vector<double> > products;
    switch (current_instruction().operation) {
      // Primitive Recipe Implementations
      case COPY: {
        copy(ingredients.begin(), ingredients.end(), inserter(products, products.begin()));
        break;
      }
      // End Primitive Recipe Implementations
      default: {
        cout << "not a primitive op: " << current_instruction().operation << '\n';
      }
    }
    // Write Products of Instruction
    if (SIZE(products) < SIZE(current_instruction().products)) {
      raise << SIZE(products) << " vs " << SIZE(current_instruction().products) << ": failed to write to all products! " << to_original_string(current_instruction()) << '\n' << end();
    }
    else {
      for (int i = 0;  i < SIZE(current_instruction().Builtin.Pseudo */
.highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */
.highlight .vc { color: #336699 } /* Name.Variable.Class */
.highlight .vg { color: #dd7700 } /* Name.Variable.Global */
.highlight .vi { color: #3333bb } /* Name.Variable.Instance */
.highlight .vm { color: #336699 } /* Name.Variable.Magic */
.highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
void test_trace_check_compares() {
  trace("test layer") << "foo" << end();
  CHECK_TRACE_CONTENTS("test layer: foo");
}

void test_trace_check_ignores_other_layers() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 2") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
  CHECK_TRACE_DOESNT_CONTAIN("test layer 2: foo");
}

void test_trace_check_ignores_leading_whitespace() {
  trace("test layer 1") << " foo" << end();
  CHECK(trace_count("test layer 1", /*too little whitespace*/"foo") == 1);
  CHECK(trace_count("test layer 1", /*too much whitespace*/"  foo") == 1);
}

void test_trace_check_ignores_other_lines() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_check_ignores_other_lines2() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_TRACE_CONTENTS("test layer 1: bar");
}

void test_trace_ignores_trailing_whitespace() {
  trace("test layer 1") << "foo\n" << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_ignores_trailing_whitespace2() {
  trace("test layer 1") << "foo " << end();
  CHECK_TRACE_CONTENTS("test layer 1: foo");
}

void test_trace_orders_across_layers() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 2") << "bar" << end();
  trace("test layer 1") << "qux" << end();
  CHECK_TRACE_CONTENTS("test layer 1: footest layer 2: bartest layer 1: qux");
}

void test_trace_supports_count() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "foo" << end();
  CHECK_EQ(trace_count("test layer 1", "foo"), 2);
}

void test_trace_supports_count2() {
  trace("test layer 1") << "foo" << end();
  trace("test layer 1") << "bar" << end();
  CHECK_EQ(trace_count("test layer 1"), 2);
}

void test_trace_count_ignores_trailing_whitespace() {
  trace("test layer 1") << "foo\n" << end();
  CHECK(trace_count("test layer 1", "foo") == 1);
}

// pending: DUMP tests
// pending: readable_contents() adds newline if necessary.
// pending: raise also prints to stderr.
// pending: raise doesn't print to stderr if Hide_errors is set.
// pending: raise doesn't have to be saved if Hide_errors is set, just printed.
// pending: raise prints to stderr if Trace_stream is NULL.
// pending: raise prints to stderr if Trace_stream is NULL even if Hide_errors is set.
// pending: raise << ... die() doesn't die if Hide_errors is set.



// can't check trace because trace methods call 'split'

void test_split_returns_at_least_one_elem() {
  vector<string> result = split("", ",");
  CHECK_EQ(result.size(), 1);
  CHECK_EQ(result.at(0), "");
}

void test_split_returns_entire_input_when_no_delim() {
  vector<string> result = split("abc", ",");
  CHECK_EQ(result.size(), 1);
  CHECK_EQ(result.at(0), "abc");
}

void test_split_works() {
  vector<string> result = split("abc,def", ",");
  CHECK_EQ(result.size(), 2);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
}

void test_split_works2() {
  vector<string> result = split("abc,def,ghi", ",");
  CHECK_EQ(result.size(), 3);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
  CHECK_EQ(result.at(2), "ghi");
}

void test_split_handles_multichar_delim() {
  vector<string> result = split("abc,,def,,ghi", ",,");
  CHECK_EQ(result.size(), 3);
  CHECK_EQ(result.at(0), "abc");
  CHECK_EQ(result.at(1), "def");
  CHECK_EQ(result.at(2), "ghi");
}

void test_trim() {
  CHECK_EQ(trim(""), "");
  CHECK_EQ(trim(" "), "");
  CHECK_EQ(trim("  "), "");
  CHECK_EQ(trim("a"), "a");
  CHECK_EQ(trim(" a"), "a");
  CHECK_EQ(trim("  a"), "a");
  CHECK_EQ(trim("  ab"), "ab");
  CHECK_EQ(trim("a "), "a");
  CHECK_EQ(trim("a  "), "a");
  CHECK_EQ(trim("ab  "), "ab");
  CHECK_EQ(trim(" a "), "a");
  CHECK_EQ(trim("  a  "), "a");
  CHECK_EQ(trim("  ab  "), "ab");
}
="Delimiter">) << "storing " << no_scientific(data.at(offset)) << " in location " << x.value+offset << end(); put(Memory, x.value+offset, data.at(offset)); } } :(code) int size_of(const reagent& r) { if (!r.type) return 0; // End size_of(reagent r) Cases return size_of(r.type); } int size_of(const type_tree* type) { if (!type) return 0; // End size_of(type) Cases return 1; } bool size_mismatch(const reagent& x, const vector<double>& data) { if (!x.type) return true; // End size_mismatch(x) Cases //? if (size_of(x) != SIZE(data)) cerr << size_of(x) << " vs " << SIZE(data) << '\n'; return size_of(x) != SIZE(data); } bool is_literal(const reagent& r) { return is_literal(r.type); } bool is_literal(const type_tree* type) { if (!type) return false; if (!type->atom) return false; return type->value == 0; } bool scalar(const vector<int>& x) { return SIZE(x) == 1; } bool scalar(const vector<double>& x) { return SIZE(x) == 1; } // helper for tests void run(const string& form) { vector<recipe_ordinal> tmp = load(form); transform_all(); if (tmp.empty()) return; if (trace_count("error") > 0) return; // if a test defines main, it probably wants to start there regardless of // definition order if (contains_key(Recipe, get(Recipe_ordinal, "main"))) run(get(Recipe_ordinal, "main")); else run(tmp.front()); } :(scenario run_label) def main [ +foo 1:num <- copy 23 2:num <- copy 1:num ] +run: {1: "number"} <- copy {23: "literal"} +run: {2: "number"} <- copy {1: "number"} -run: +foo :(scenario run_dummy) def main [ _ <- copy 0 ] +run: _ <- copy {0: "literal"} :(scenario write_to_0_disallowed) % Hide_errors = true; def main [ 0:num <- copy 34 ] -mem: storing 34 in location 0 //: Mu is robust to various combinations of commas and spaces. You just have //: to put spaces around the '<-'. :(scenario comma_without_space) def main [ 1:num, 2:num <- copy 2,2 ] +mem: storing 2 in location 1 :(scenario space_without_comma) def main [ 1:num, 2:num <- copy 2 2 ] +mem: storing 2 in location 1 :(scenario comma_before_space) def main [ 1:num, 2:num <- copy 2, 2 ] +mem: storing 2 in location 1 :(scenario comma_after_space) def main [ 1:num, 2:num <- copy 2 ,2 ] +mem: storing 2 in location 1