//: Allow Mu programs to log facts just like we've been doing in C++ so far. void test_trace() { run( "def main [\n" " trace 1, [foo], [this is a trace in Mu]\n" "]\n" ); CHECK_TRACE_CONTENTS( "foo: this is a trace in Mu\n" ); } :(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 '" << to_original_string(inst) << "'\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 << inspect(current_instruction().ingredients.at(i), ingredients.at(i)); } trace(depth, label) << out.str() << end(); break; } //: simpler limited version of 'trace' :(before "End Types") //: include in all cleaved compilation units const int App_depth = 1; // where all Mu code will trace to by default :(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 << inspect(current_instruction().ingredients.at(i), ingredients.at(i)); } trace(App_depth, "app") << out.str() << end(); break; } :(code) void test_stash_literal_string() { run( "def main [\n" " stash [foo]\n" "]\n" ); CHECK_TRACE_CONTENTS( "app: foo\n" ); } void test_stash_literal_number() { run( "def main [\n" " stash [foo:], 4\n" "]\n" ); CHECK_TRACE_CONTENTS( "app: foo: 4\n" ); } void test_stash_number() { run( "def main [\n" " 1:num <- copy 34\n" " stash [foo:], 1:num\n" "]\n" ); CHECK_TRACE_CONTENTS( "app: foo: 34\n" ); } :(code) string inspect(const reagent& r, const vector& data) { if (is_literal(r)) return r.name; // End inspect 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 HID