//: 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();
    
const NimStackTrace = compileOption("stacktrace")
const NimStackTraceMsgs = compileOption("stacktraceMsgs")

template procName*(): string =
  ## returns current C/C++ function name
  when defined(c) or defined(cpp):
    var name {.inject, noinit.}: cstring
    {.emit: "`name` = __func__;".}
    $name

template getPFrame*(): PFrame =
  ## avoids a function call (unlike `getFrame()`)
  block:
    when NimStackTrace:
      var framePtr {.inject, noinit.}: PFrame
      {.emit: "`framePtr` = &FR_;".}
      framePtr

template setFrameMsg*(msg: string, prefix = " ") =
  ## attach a msg to current `PFrame`. This can be called multiple times
  ## in a given PFrame. Noop unless passing --stacktraceMsgs and --stacktrace
  when NimStackTrace and NimStackTraceMsgs:
    block:
      var fr {.inject, noinit.}: PFrame
      {.emit: "`fr` = &FR_;".}
      # consider setting a custom upper limit on size (analog to stack overflow)
      frameMsgBuf.setLen fr.frameMsgLen
      frameMsgBuf.add prefix
      frameMsgBuf.add msg
      fr.frameMsgLen += prefix.len + msg.len
uot;$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))) { raise << maybe(get(Recipe, r).name) << "'assert' requires a literal string for its second ingredient, but got '" << inst.ingredients.at(1).original_string << "'\n" << end(); break; } break; } :(before "End Primitive Recipe Implementations") case ASSERT: { if (!ingredients.at(0).at(0)) { raise << current_instruction().ingredients.at(1).name << '\n' << end(); } break; } //:: 'cheating' by using the host system :(before "End Primitive Recipe Declarations") _PRINT, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$print", _PRINT); :(before "End Primitive Recipe Checks") case _PRINT: { break; } :(before "End Primitive Recipe Implementations") case _PRINT: { for (int i = 0; i < SIZE(ingredients); ++i) { if (is_literal(current_instruction().ingredients.at(i))) { trace(9998, "run") << "$print: " << current_instruction().ingredients.at(i).name << end(); if (!has_property(current_instruction().ingredients.at(i), "newline")) { cout << current_instruction().ingredients.at(i).name; } // hack: '$print 10' prints '10', but '$print 10/newline' prints '\n' // End $print 10/newline Special-cases else { cout << '\n'; } } else { for (int j = 0; j < SIZE(ingredients.at(i)); ++j) { trace(9998, "run") << "$print: " << ingredients.at(i).at(j) << end(); if (j > 0) cout << " "; cout << no_scientific(ingredients.at(i).at(j)); } } } cout.flush(); break; } :(before "End Primitive Recipe Declarations") _EXIT, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$exit", _EXIT); :(before "End Primitive Recipe Checks") case _EXIT: { break; } :(before "End Primitive Recipe Implementations") case _EXIT: { exit(0); break; } :(before "End Primitive Recipe Declarations") _SYSTEM, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$system", _SYSTEM); :(before "End Primitive Recipe Checks") case _SYSTEM: { if (SIZE(inst.ingredients) != 1) { raise << maybe(get(Recipe, r).name) << "'$system' requires exactly one ingredient, but got '" << to_string(inst) << "'\n" << end(); break; } if (!is_literal_text(inst.ingredients.at(0))) { raise << maybe(get(Recipe, r).name) << "ingredient to '$system' must be a literal text, but got '" << to_string(inst) << "'\n" << end(); } break; } :(before "End Primitive Recipe Implementations") case _SYSTEM: { int status = system(current_instruction().ingredients.at(0).name.c_str()); products.resize(1); products.at(0).push_back(status); break; } :(before "End Primitive Recipe Declarations") _DUMP_MEMORY, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$dump-memory", _DUMP_MEMORY); :(before "End Primitive Recipe Checks") case _DUMP_MEMORY: { break; } :(before "End Primitive Recipe Implementations") case _DUMP_MEMORY: { dump_memory(); break; } //: In times of real extremis we need to create a whole new modality for debug //: logs, independent of other changes to the screen or Trace_stream. :(before "End Globals") ofstream LOG; :(before "End One-time Setup") //? LOG.open("log"); :(before "End Primitive Recipe Declarations") _LOG, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$log", _LOG); :(before "End Primitive Recipe Checks") case _LOG: { break; } :(before "End Primitive Recipe Implementations") case _LOG: { ostringstream out; for (int i = 0; i < SIZE(current_instruction().ingredients); ++i) { out << print_mu(current_instruction().ingredients.at(i), ingredients.at(i)); } LOG << out.str() << '\n'; break; } //: set a variable from within mu code //: useful for selectively tracing or printing after some point :(before "End Globals") bool Foo = false; :(before "End Primitive Recipe Declarations") _FOO, :(before "End Primitive Recipe Numbers") put(Recipe_ordinal, "$foo", _FOO); :(before "End Primitive Recipe Checks") case _FOO: { break; } :(before "End Primitive Recipe Implementations") case _FOO: { Foo = true; break; }