//: Allow mu programs to log facts just like we've been doing in C++ so far. :(scenario trace) def main [ trace 1, [foo], [this is a trace in mu] ] +foo: this is a trace in mu :(before "End Primitive Recipe Declarations") TRACE, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "trace", TRACE); :(before "End Primitive Recipe Checks") case TRACE: { if (SIZE(inst.ingredients) < 3) { raise << maybe(get(Recipe, r).name) << "'trace' takes three or more ingredients rather than '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_number(inst.ingredients.at(0))) { raise << maybe(get(Recipe, r).name) << "first ingredient of 'trace' should be a number (depth), but got '" << inst.ingredients.at(0).original_string << "'\n" << end(); break; } if (!is_literal_text(inst.ingredients.at(1))) { raise << maybe(get(Recipe, r).name) << "second ingredient of 'trace' should be a literal string (label), but got '" << inst.ingredients.at(1).original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case TRACE: { int depth = ingredients.at(0).at(0); string label = current_instruction().ingredients.at(1).name; ostringstream out; for (int i = 2; i < SIZE(current_instruction().ingredients); ++i) { if (i > 2) out << ' '; out << print_mu(current_instruction().ingredients.at(i), ingredients.at(i)); } trace(depth, label) << out.str() << end(); break; } //: simpler limited version of 'trace' :(before "End Primitive Recipe Declarations") STASH, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "stash", STASH); :(before "End Primitive Recipe Checks") case STASH: { break; } :(before "End Primitive Recipe Implementations") case STASH: { ostringstream out; for (int i = 0; i < SIZE(current_instruction().ingredients); ++i) { if (i) out << ' '; out << print_mu(current_instruction().ingredients.at(i), ingredients.at(i)); } trace(2, "app") << out.str() << end(); break; } :(scenario stash_literal_string) def main [ stash [foo] ] +app: foo :(scenario stash_literal_number) def main [ stash [foo:], 4 ] +app: foo: 4 :(scenario stash_number) def main [ 1:num <- copy 34 stash [foo:], 1:num ] +app: foo: 34 :(code) string print_mu(const reagent& r, const vector& data) { if (is_literal(r)) return r.name; // End print Special-cases(r, data) ostringstream out; for (long long i = 0; i < SIZE(data); ++i) { if (i) out << ' '; out << no_scientific(data.at(i)); } return out.str(); } :(before "End Primitive Recipe Declarations") HIDE_ERRORS, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "hide-errors", HIDE_ERRORS); :(before "End Primitive Recipe Checks") case HIDE_ERRORS: { break; } :(before "End Primitive Recipe Implementations") case HIDE_ERRORS: { Hide_errors = true; break; } :(before "End Primitive Recipe Declarations") SHOW_ERRORS, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "show-errors", SHOW_ERRORS); :(before "End Primitive Recipe Checks") case SHOW_ERRORS: { break; } :(before "End Primitive Recipe Implementations") case SHOW_ERRORS: { Hide_errors = false; break; } :(before "End Primitive Recipe Declarations") TRACE_UNTIL, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "trace-until", TRACE_UNTIL); :(before "End Primitive Recipe Checks") case TRACE_UNTIL: { break; } :(before "End Primitive Recipe Implementations") case TRACE_UNTIL: { if (Trace_stream) { Trace_stream->collect_depth = ingredients.at(0).at(0); } break; } :(before "End Primitive Recipe Declarations") _DUMP_TRACE, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$dump-trace", _DUMP_TRACE); :(before "End Primitive Recipe Checks") case _DUMP_TRACE: { break; } :(before "End Primitive Recipe Implementations") case _DUMP_TRACE: { if (ingredients.empty()) { DUMP(""); } else { DUMP(current_instruction().ingredients.at(0).name); } break; } :(before "End Primitive Recipe Declarations") _CLEAR_TRACE, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$clear-trace", _CLEAR_TRACE); :(before "End Primitive Recipe Checks") case _CLEAR_TRACE: { break; } :(before "End Primitive Recipe Implementations") case _CLEAR_TRACE: { if (Trace_stream) Trace_stream->past_lines.clear(); break; } :(before "End Primitive Recipe Declarations") _SAVE_TRACE, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$save-trace", _SAVE_TRACE); :(before "End Primitive Recipe Checks") case _SAVE_TRACE: { break; } :(before "End Primitive Recipe Implementations") case _SAVE_TRACE: { if (Save_trace) { ofstream fout("last_trace"); fout << Trace_stream->readable_contents(""); fout.close(); } break; } //: assert: perform sanity checks at runtime :(scenario assert) % Hide_errors = true; // '%' lines insert arbitrary C code into tests before calling 'run' with the lines below. Must be immediately after :(scenario) line. def main [ assert 0, [this is an assert in mu] ] +error: this is an assert in mu :(before "End Primitive Recipe Declarations") ASSERT, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "assert", ASSERT); :(before "End Primitive Recipe Checks") case ASSERT: { if (SIZE(inst.ingredients) != 2) { raise << maybe(get(Recipe, r).name) << "'assert' takes exactly two ingredients rather than '" << inst.original_string << "'\n" << end(); break; } if (!is_mu_scalar(inst.ingredients.at(0))) { raise << maybe(get(Recipe, r).name) << "'assert' requires a boolean for its first ingredient, but got '" << inst.ingredients.at(0).original_string << "'\n" << end(); break; } if (!is_literal_text(inst.ingredients.at(1)))