From e07cbe5edfef74005a56205586202e9a224f0500 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Fri, 12 Aug 2016 18:00:47 -0700 Subject: 3174 --- html/001help.cc.html | 70 +++-- html/002test.cc.html | 2 +- html/011load.cc.html | 8 +- html/020run.cc.html | 13 +- html/029tools.cc.html | 10 +- html/050scenario.cc.html | 50 ++-- html/080display.cc.html | 22 +- html/091run_interactive.cc.html | 544 ----------------------------------- html/091run_sandboxed.cc.html | 556 ++++++++++++++++++++++++++++++++++++ html/edit/005-sandbox.mu.html | 2 +- html/edit/010-sandbox-trace.mu.html | 2 +- html/edit/011-errors.mu.html | 2 +- html/lambda-to-mu.mu.html | 18 ++ 13 files changed, 692 insertions(+), 607 deletions(-) delete mode 100644 html/091run_interactive.cc.html create mode 100644 html/091run_sandboxed.cc.html diff --git a/html/001help.cc.html b/html/001help.cc.html index 09f1e4c9..cf4c82ec 100644 --- a/html/001help.cc.html +++ b/html/001help.cc.html @@ -40,28 +40,52 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color if (argc <= 1 || is_equal(argv[1], "--help")) { // this is the functionality later layers will provide // currently no automated tests for commandline arg parsing - cerr << "To load files and run 'main':\n" - << " mu file1.mu file2.mu ...\n" - << "To run all tests:\n" - << " mu test\n" - << "To load files and then run all tests:\n" - << " mu test file1.mu file2.mu ...\n" - << "To load files and run only the tests in explicitly loaded files (for apps):\n" - << " mu --test-only-app test file1.mu file2.mu ...\n" - << "'--test-only-app' must come before all other args\n" - << "'test' must come before all other args except '--test-only-app'\n" - << "To load all files with a numeric prefix in a directory:\n" - << " mu directory1\n" - << "You can test directories just like files.\n" - << "To pass ingredients to a mu program, provide them after '--':\n" - << " mu file_or_dir1 file_or_dir2 ... -- ingredient1 ingredient2 ...\n" + if (argc <= 1) { + cerr << "Please provide a Mu program to run.\n" + << "\n"; + } + cerr << "Usage:\n" + << " mu [options] [test] [files]\n" + << "or:\n" + << " mu [options] [test] [files] -- [ingredients for function/recipe 'main']\n" + << "Square brackets surround optional arguments.\n" + << "\n" + << "Examples:\n" + << " To load files and run 'main':\n" + << " mu file1.mu file2.mu ...\n" + << " To run all tests:\n" + << " mu test\n" + << " To load files and then run all tests:\n" + << " mu test file1.mu file2.mu ...\n" + << " To load files and run only the tests in explicitly loaded files (for apps):\n" + << " mu --test-only-app test file1.mu file2.mu ...\n" + << " To load all files with a numeric prefix in a directory:\n" + << " mu directory1 directory2 ...\n" + << " You can test directories just like files.\n" + << " mu test directory1 directory2 ...\n" + << " To pass ingredients to a mu program, provide them after '--':\n" + << " mu file_or_dir1 file_or_dir2 ... -- ingredient1 ingredient2 ...\n" << "\n" - << "To browse a trace generated by a previous run:\n" - << " mu browse-trace file\n" + << " To browse a trace generated by a previous run:\n" + << " mu browse-trace file\n" ; return 0; } +//: Support for option parsing. +//: Options always begin with '--' and are always the first arguments. An +//: option will never follow a non-option. +:(before "End Commandline Parsing") +char** arg = &argv[1]; +while (argc > 1 && starts_with(*arg, "--")) { + if (false) + ; // no-op branch just so any further additions can consistently always start with 'else' + // End Commandline Options(*arg) + else + cerr << "skipping unknown option " << *arg << '\n'; + --argc; ++argv; ++arg; +} + //:: Helper function used by the above fragment of code (and later layers too, //:: who knows?). //: The :(code) directive appends function definitions to the end of the @@ -76,6 +100,10 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color return strncmp(s, lit, strlen(lit)) == 0; } +bool starts_with(const string& s, const string& pat) { + return s.substr(0, pat.size()) == pat; +} + //: I'll throw some style conventions here for want of a better place for them. //: As a rule I hate style guides. Do what you want, that's my motto. But since //: we're dealing with C/C++, the one big thing we want to avoid is undefined @@ -233,14 +261,6 @@ feenableexcept(FE_OVERFLOW | FE_UNDERFLOWreturn in && !in.eof(); } -////: A hack to support faster debugging. -:(before "End Globals") -bool Test_only_app = false; -:(before "End Commandline Parsing") -if (argc > 1 && is_equal(argv[1], "--test-only-app")) { - Test_only_app = true; --argc; ++argv; -} - :(before "End Includes") #include <assert.h> diff --git a/html/002test.cc.html b/html/002test.cc.html index c01063f8..84903a24 100644 --- a/html/002test.cc.html +++ b/html/002test.cc.html @@ -34,7 +34,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
 //: A simple test harness. To create new tests define functions starting with
 //: 'test_'. To run all tests so defined, run:
-//:   $ wart test
+//:   $ mu test
 //:
 //: So far it seems tasteful for layers to never ever reach back to modify
 //: previously-defined tests. Every test is a contract once written, and should
diff --git a/html/011load.cc.html b/html/011load.cc.html
index 184151d4..b9194212 100644
--- a/html/011load.cc.html
+++ b/html/011load.cc.html
@@ -60,11 +60,13 @@ vector<recipe_ordinal> load(istream& in
     string command = next_word(in);
     // Command Handlers
     if (command == "recipe" || command == "def") {
-      result.push_back(slurp_recipe(in));
+      recipe_ordinal r = slurp_recipe(in);
+      if (r > 0) result.push_back(r);
     }
     else if (command == "recipe!" || command == "def!") {
       Disable_redefine_checks = true;
-      result.push_back(slurp_recipe(in));
+      recipe_ordinal r = slurp_recipe(in);
+      if (r > 0) result.push_back(r);
       Disable_redefine_checks = false;
     }
     // End Command Handlers
@@ -75,6 +77,8 @@ vector<recipe_ordinal> load(istream& in
   return result;
 }
 
+// return the recipe ordinal slurped, or -1 if it failed
+// (later layers will cause failures)
 int slurp_recipe(istream& in) {
   recipe result;
   result.name = next_word(in);
diff --git a/html/020run.cc.html b/html/020run.cc.html
index 77f00c93..f3a54f18 100644
--- a/html/020run.cc.html
+++ b/html/020run.cc.html
@@ -207,19 +207,28 @@ save_snapshots();
   // Running Main
   setup();
 //?   Save_trace = true;
-//?   START_TRACING_UNTIL_END_OF_SCOPE;
+  if (Trace_main) Trace_stream = new trace_stream;
   trace(9990, "run") << "=== Starting to run" << end();
   assert(Num_calls_to_transform_all == 1);
   run_main(argc, argv);
+  if (Trace_main) delete Trace_stream, Trace_stream = NULL;
   teardown();
 }
-
 :(code)
 void run_main(int argc, char* argv[]) {
   recipe_ordinal r = get(Recipe_ordinal, "main");
   if (r) run(r);
 }
 
+//: By default we don't maintain the trace while running main because its
+//: overheads can grow rapidly. However, it's useful when debugging.
+:(before "End Globals")
+bool Trace_main = false;
+:(before "End Commandline Options(*arg)")
+else if (is_equal(*arg, "--trace")) {
+  Trace_main = true;
+}
+
 :(code)
 void dump_profile() {
   for (map<string, int>::iterator p = Instructions_running.begin(); p != Instructions_running.end(); ++p) {
diff --git a/html/029tools.cc.html b/html/029tools.cc.html
index 1b4ee2f3..0d861715 100644
--- a/html/029tools.cc.html
+++ b/html/029tools.cc.html
@@ -275,10 +275,14 @@ put(Recipe_ordinal,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 << '\n';
-      else
+      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) {
diff --git a/html/050scenario.cc.html b/html/050scenario.cc.html
index d6805cfd..0727ae0e 100644
--- a/html/050scenario.cc.html
+++ b/html/050scenario.cc.html
@@ -99,7 +99,6 @@ scenario foo [
 
 :(before "End Globals")
 vector<scenario> Scenarios;
-set<string> Scenario_names;
 
 //:: Parse the 'scenario' form.
 //: Simply store the text of the scenario.
@@ -113,9 +112,6 @@ set<string> Scenario_names;
 scenario parse_scenario(istream& in) {
   scenario result;
   result.name = next_word(in);
-  if (contains_key(Scenario_names, result.name))
-    raise << "duplicate scenario name: '" << result.name << "'\n" << end();
-  Scenario_names.insert(result.name);
   skip_whitespace_and_comments(in);
   if (in.peek() != '[') {
     raise << "Expected '[' after scenario '" << result.name << "'\n" << end();
@@ -149,30 +145,41 @@ scenario foo [
 //: Treat the text of the scenario as a regular series of instructions.
 
 :(before "End Globals")
-int Num_core_mu_tests = 0;
+int Num_core_mu_scenarios = 0;
 :(after "Check For .mu Files")
-Num_core_mu_tests = SIZE(Scenarios);
+Num_core_mu_scenarios = SIZE(Scenarios);
 :(before "End Tests")
 Hide_missing_default_space_errors = false;
-time_t mu_time; time(&mu_time);
-cerr << "\nMu tests: " << ctime(&mu_time);
-run_mu_scenarios:
-for (int i = 0; i < SIZE(Scenarios); ++i) {
+time(&t);
+cerr << "\nMu tests: " << ctime(&t);
+for (int i = 0; i < Num_core_mu_scenarios; ++i) {
 //?   cerr << i << ": " << Scenarios.at(i).name << '\n';
-  if (i == Num_core_mu_tests) {
-    time(&t);
-    cerr << "\nApp tests: " << ctime(&t);
-  }
   run_mu_scenario(Scenarios.at(i));
   if (Passed) cerr << ".";
 }
+cerr << "\n";
+run_app_scenarios:
+if (Num_core_mu_scenarios != SIZE(Scenarios)) {
+  time(&t);
+  cerr << "App tests: " << ctime(&t);
+  for (int i = Num_core_mu_scenarios; i < SIZE(Scenarios); ++i) {
+//?     cerr << i << ": " << Scenarios.at(i).name << '\n';
+    run_mu_scenario(Scenarios.at(i));
+    if (Passed) cerr << ".";
+  }
+}
 
+//: For faster debugging, support running tests for just the Mu app(s) we are
+//: loading.
+:(before "End Globals")
+bool Test_only_app = false;
+:(before "End Commandline Options(*arg)")
+else if (is_equal(*arg, "--test-only-app")) {
+  Test_only_app = true;
+}
 :(after "End Test Run Initialization")
-if (Test_only_app && Num_core_mu_tests < SIZE(Scenarios)) {
-  // we have app tests; skip core mu tests
-  Scenarios.erase(Scenarios.begin(), Scenarios.begin()+Num_core_mu_tests);
-  // skip C tests
-  goto run_mu_scenarios;
+if (Test_only_app && Num_core_mu_scenarios < SIZE(Scenarios)) {
+  goto run_app_scenarios;
 }
 
 //: Convenience: run a single named scenario.
@@ -789,7 +796,6 @@ is_autogenerated = false;<
 :(code)
 // just for the scenarios running scenarios in C++ layers
 void run_mu_scenario(const string& form) {
-  Scenario_names.clear();
   istringstream in(form);
   in >> std::noskipws;
   skip_whitespace_and_comments(in);
@@ -798,10 +804,6 @@ is_autogenerated = false;<
   scenario s = parse_scenario(in);
   run_mu_scenario(s);
 }
-
-bool starts_with(const string& s, const string& pat) {
-  return s.substr(0, pat.size()) == pat;
-}
 
diff --git a/html/080display.cc.html b/html/080display.cc.html index d85a3f5b..fc7d0e22 100644 --- a/html/080display.cc.html +++ b/html/080display.cc.html @@ -31,7 +31,9 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color
-//: Take charge of the text-mode display and console.
+//: Take raw control of the text-mode display and console, putting it in
+//: 'console' mode rather than the usual automatically-scrolling 'typewriter'
+//: mode.
 
 //:: Display management
 
@@ -55,9 +57,9 @@ put(Recipe_ordinal,int height = tb_height();
   if (width > 222 || height > 222) tb_shutdown();
   if (width > 222)
-    raise << "sorry, mu doesn't support windows wider than 222 characters. Please resize your window.\n" << end();
+    raise << "sorry, mu doesn't support windows wider than 222 characters in console mode. Please resize your window.\n" << end();
   if (height > 222)
-    raise << "sorry, mu doesn't support windows taller than 222 characters. Please resize your window.\n" << end();
+    raise << "sorry, mu doesn't support windows taller than 222 characters in console mode. Please resize your window.\n" << end();
   break;
 }
 
@@ -317,6 +319,20 @@ put(Recipe_ordinal,break;
 }
 
+//: as a convenience, make $print mostly work in console mode
+:(before "End $print 10/newline Special-cases")
+else if (tb_is_active()) {
+  move_cursor_to_start_of_next_line_on_display();
+}
+:(code)
+void move_cursor_to_start_of_next_line_on_display() {
+  if (Display_row < tb_height()-1) Display_row++;
+  else Display_row = 0;
+  Display_column = 0;
+  tb_set_cursor(Display_column, Display_row);
+  if (Autodisplay) tb_present();
+}
+
 :(before "End Primitive Recipe Declarations")
 DISPLAY_WIDTH,
 :(before "End Primitive Recipe Numbers")
diff --git a/html/091run_interactive.cc.html b/html/091run_interactive.cc.html
deleted file mode 100644
index 85cbf97d..00000000
--- a/html/091run_interactive.cc.html
+++ /dev/null
@@ -1,544 +0,0 @@
-
-
-
-
-Mu - 091run_interactive.cc
-
-
-
-
-
-
-
-
-
-
-
-//: Helper for various programming environments: run arbitrary mu code and
-//: return some result in string form.
-
-:(scenario run_interactive_code)
-def main [
-  1:number/raw <- copy 0
-  2:address:array:character <- new [1:number/raw <- copy 34]
-  run-interactive 2:address:array:character
-  3:number/raw <- copy 1:number/raw
-]
-+mem: storing 34 in location 3
-
-:(scenario run_interactive_empty)
-def main [
-  1:address:array:character <- copy 0/unsafe
-  2:address:array:character <- run-interactive 1:address:array:character
-]
-# result is null
-+mem: storing 0 in location 2
-
-//: run code in 'interactive mode', i.e. with errors off and return:
-//:   stringified output in case we want to print it to screen
-//:   any errors encountered
-//:   simulated screen any prints went to
-//:   any 'app' layer traces generated
-:(before "End Primitive Recipe Declarations")
-RUN_INTERACTIVE,
-:(before "End Primitive Recipe Numbers")
-put(Recipe_ordinal, "run-interactive", RUN_INTERACTIVE);
-:(before "End Primitive Recipe Checks")
-case RUN_INTERACTIVE: {
-  if (SIZE(inst.ingredients) != 1) {
-    raise << maybe(get(Recipe, r).name) << "'run-interactive' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end();
-    break;
-  }
-  if (!is_mu_string(inst.ingredients.at(0))) {
-    raise << maybe(get(Recipe, r).name) << "first ingredient of 'run-interactive' should be a string, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end();
-    break;
-  }
-  break;
-}
-:(before "End Primitive Recipe Implementations")
-case RUN_INTERACTIVE: {
-  bool new_code_pushed_to_stack = run_interactive(ingredients.at(0).at(0));
-  if (!new_code_pushed_to_stack) {
-    products.resize(5);
-    products.at(0).push_back(0);
-    products.at(1).push_back(trace_error_contents());
-    products.at(2).push_back(0);
-    products.at(3).push_back(trace_app_contents());
-    products.at(4).push_back(1);  // completed
-    run_code_end();
-    break;  // done with this instruction
-  }
-  else {
-    continue;  // not done with caller; don't increment current_step_index()
-  }
-}
-
-:(before "End Globals")
-bool Track_most_recent_products = false;
-:(before "End Tracing")
-trace_stream* Save_trace_stream = NULL;
-string Save_trace_file;
-:(before "End Setup")
-Track_most_recent_products = false;
-:(code)
-// reads a string, tries to call it as code (treating it as a test), saving
-// all errors.
-// returns true if successfully called (no errors found during load and transform)
-bool run_interactive(int address) {
-  assert(contains_key(Recipe_ordinal, "interactive") && get(Recipe_ordinal, "interactive") != 0);
-  // try to sandbox the run as best you can
-  // todo: test this
-  if (!Current_scenario) {
-    for (int i = 1; i < Reserved_for_tests; ++i)
-      Memory.erase(i);
-  }
-  string command = trim(strip_comments(read_mu_string(address)));
-  Name[get(Recipe_ordinal, "interactive")].clear();
-  run_code_begin(/*should_stash_snapshots*/true);
-  if (command.empty()) return false;
-  // don't kill the current routine on parse errors
-  routine* save_current_routine = Current_routine;
-  Current_routine = NULL;
-  // call run(string) but without the scheduling
-  load(string("recipe! interactive [\n") +
-          "new-default-space\n" +  // disable automatic abandon so tests can see changes
-          "screen:address:screen <- next-ingredient\n" +
-          "$start-tracking-products\n" +
-          command + "\n" +
-          "$stop-tracking-products\n" +
-          "return screen\n" +
-       "]\n");
-  transform_all();
-  Current_routine = save_current_routine;
-  if (trace_count("error") > 0) return false;
-  // now call 'sandbox' which will run 'interactive' in a separate routine,
-  // and wait for it
-  if (Save_trace_stream) {
-    ++Save_trace_stream->callstack_depth;
-    trace(9999, "trace") << "run-interactive: incrementing callstack depth to " << Save_trace_stream->callstack_depth << end();
-    assert(Save_trace_stream->callstack_depth < 9000);  // 9998-101 plus cushion
-  }
-  Current_routine->calls.push_front(call(get(Recipe_ordinal, "sandbox")));
-  return true;
-}
-
-//: Carefully update all state to exactly how it was -- including snapshots.
-
-:(before "End Globals")
-map<string, recipe_ordinal> Recipe_ordinal_snapshot_stash;
-map<recipe_ordinal, recipe> Recipe_snapshot_stash;
-map<string, type_ordinal> Type_ordinal_snapshot_stash;
-map<type_ordinal, type_info> Type_snapshot_stash;
-map<recipe_ordinal, map<string, int> > Name_snapshot_stash;
-map<string, vector<recipe_ordinal> > Recipe_variants_snapshot_stash;
-:(code)
-void run_code_begin(bool should_stash_snapshots) {
-  // stuff to undo later, in run_code_end()
-  Hide_errors = true;
-  Disable_redefine_checks = true;
-  if (should_stash_snapshots)
-    stash_snapshots();
-  Save_trace_stream = Trace_stream;
-  Trace_stream = new trace_stream;
-  Trace_stream->collect_depth = App_depth;
-}
-
-void run_code_end() {
-  Hide_errors = false;
-  Disable_redefine_checks = false;
-  delete Trace_stream;
-  Trace_stream = Save_trace_stream;
-  Save_trace_stream = NULL;
-  Save_trace_file.clear();
-  Recipe.erase(get(Recipe_ordinal, "interactive"));  // keep past sandboxes from inserting errors
-  if (!Recipe_snapshot_stash.empty())
-    unstash_snapshots();
-}
-
-// keep sync'd with save_snapshots and restore_snapshots
-void stash_snapshots() {
-  Recipe_ordinal_snapshot_stash = Recipe_ordinal_snapshot;
-  Recipe_snapshot_stash = Recipe_snapshot;
-  Type_ordinal_snapshot_stash = Type_ordinal_snapshot;
-  Type_snapshot_stash = Type_snapshot;
-  Name_snapshot_stash = Name_snapshot;
-  Recipe_variants_snapshot_stash = Recipe_variants_snapshot;
-  save_snapshots();
-}
-void unstash_snapshots() {
-  restore_snapshots();
-  Recipe_ordinal_snapshot = Recipe_ordinal_snapshot_stash;  Recipe_ordinal_snapshot_stash.clear();
-  Recipe_snapshot = Recipe_snapshot_stash;  Recipe_snapshot_stash.clear();
-  Type_ordinal_snapshot = Type_ordinal_snapshot_stash;  Type_ordinal_snapshot_stash.clear();
-  Type_snapshot = Type_snapshot_stash;  Type_snapshot_stash.clear();
-  Name_snapshot = Name_snapshot_stash;  Name_snapshot_stash.clear();
-  Recipe_variants_snapshot = Recipe_variants_snapshot_stash;  Recipe_variants_snapshot_stash.clear();
-}
-
-:(before "End Load Recipes")
-load(string(
-"recipe interactive [\n") +  // just a dummy version to initialize the Recipe_ordinal and so on
-"]\n" +
-"recipe sandbox [\n" +
-  "local-scope\n" +
-  "screen:address:screen <- new-fake-screen 30, 5\n" +
-  "routine-id:number <- start-running interactive, screen\n" +
-  "limit-time routine-id, 100000/instructions\n" +
-  "wait-for-routine routine-id\n" +
-  "instructions-run:number <- number-of-instructions routine-id\n" +
-  "stash instructions-run [instructions run]\n" +
-  "sandbox-state:number <- routine-state routine-id\n" +
-  "completed?:boolean <- equal sandbox-state, 1/completed\n" +
-  "output:address:array:character <- $most-recent-products\n" +
-  "errors:address:array:character <- save-errors\n" +
-  "stashes:address:array:character <- save-app-trace\n" +
-  "$cleanup-run-interactive\n" +
-  "return output, errors, screen, stashes, completed?\n" +
-"]\n");
-
-//: adjust errors in the sandbox
-:(after "string maybe(string s)")
-  if (s == "interactive") return "";
-
-:(scenario run_interactive_comments)
-def main [
-  1:address:array:character <- new [# ab
-add 2, 2]
-  2:address:array:character <- run-interactive 1:address:array:character
-  3:array:character <- copy *2:address:array:character
-]
-+mem: storing 52 in location 4
-
-:(before "End Primitive Recipe Declarations")
-_START_TRACKING_PRODUCTS,
-:(before "End Primitive Recipe Numbers")
-put(Recipe_ordinal, "$start-tracking-products", _START_TRACKING_PRODUCTS);
-:(before "End Primitive Recipe Checks")
-case _START_TRACKING_PRODUCTS: {
-  break;
-}
-:(before "End Primitive Recipe Implementations")
-case _START_TRACKING_PRODUCTS: {
-  Track_most_recent_products = true;
-  break;
-}
-
-:(before "End Primitive Recipe Declarations")
-_STOP_TRACKING_PRODUCTS,
-:(before "End Primitive Recipe Numbers")
-put(Recipe_ordinal, "$stop-tracking-products", _STOP_TRACKING_PRODUCTS);
-:(before "End Primitive Recipe Checks")
-case _STOP_TRACKING_PRODUCTS: {
-  break;
-}
-:(before "End Primitive Recipe Implementations")
-case _STOP_TRACKING_PRODUCTS: {
-  Track_most_recent_products = false;
-  break;
-}
-
-:(before "End Primitive Recipe Declarations")
-_MOST_RECENT_PRODUCTS,
-:(before "End Primitive Recipe Numbers")
-put(Recipe_ordinal, "$most-recent-products", _MOST_RECENT_PRODUCTS);
-:(before "End Primitive Recipe Checks")
-case _MOST_RECENT_PRODUCTS: {
-  break;
-}
-:(before "End Primitive Recipe Implementations")
-case _MOST_RECENT_PRODUCTS: {
-  products.resize(1);
-  products.at(0).push_back(new_mu_string(Most_recent_products));
-  break;
-}
-
-:(before "End Primitive Recipe Declarations")
-SAVE_ERRORS,
-:(before "End Primitive Recipe Numbers")
-put(Recipe_ordinal, "save-errors", SAVE_ERRORS);
-:(before "End Primitive Recipe Checks")
-case SAVE_ERRORS: {
-  break;
-}
-:(before "End Primitive Recipe Implementations")
-case SAVE_ERRORS: {
-  products.resize(1);
-  products.at(0).push_back(trace_error_contents());
-  break;
-}
-
-:(before "End Primitive Recipe Declarations")
-SAVE_APP_TRACE,
-:(before "End Primitive Recipe Numbers")
-put(Recipe_ordinal, "save-app-trace", SAVE_APP_TRACE);
-:(before "End Primitive Recipe Checks")
-case SAVE_APP_TRACE: {
-  break;
-}
-:(before "End Primitive Recipe Implementations")
-case SAVE_APP_TRACE: {
-  products.resize(1);
-  products.at(0).push_back(trace_app_contents());
-  break;
-}
-
-:(before "End Primitive Recipe Declarations")
-_CLEANUP_RUN_INTERACTIVE,
-:(before "End Primitive Recipe Numbers")
-put(Recipe_ordinal, "$cleanup-run-interactive", _CLEANUP_RUN_INTERACTIVE);
-:(before "End Primitive Recipe Checks")
-case _CLEANUP_RUN_INTERACTIVE: {
-  break;
-}
-:(before "End Primitive Recipe Implementations")
-case _CLEANUP_RUN_INTERACTIVE: {
-  run_code_end();
-  break;
-}
-
-:(scenario "run_interactive_converts_result_to_text")
-def main [
-  # try to interactively add 2 and 2
-  1:address:array:character <- new [add 2, 2]
-  2:address:array:character <- run-interactive 1:address:array:character
-  10:array:character <- copy 2:address:array:character/lookup
-]
-# first letter in the output should be '4' in unicode
-+mem: storing 52 in location 11
-
-:(scenario "run_interactive_returns_text")
-def main [
-  # try to interactively add 2 and 2
-  1:address:array:character <- new [
-    x:address:array:character <- new [a]
-    y:address:array:character <- new [b]
-    z:address:array:character <- append x:address:array:character, y:address:array:character
-  ]
-  2:address:array:character <- run-interactive 1:address:array:character
-  10:array:character <- copy 2:address:array:character/lookup
-]
-# output contains "ab"
-+mem: storing 97 in location 11
-+mem: storing 98 in location 12
-
-:(scenario "run_interactive_returns_errors")
-def main [
-  # run a command that generates an error
-  1:address:array:character <- new [x:number <- copy 34
-get x:number, foo:offset]
-  2:address:array:character, 3:address:array:character <- run-interactive 1:address:array:character
-  10:array:character <- copy 3:address:array:character/lookup
-]
-# error should be "unknown element foo in container number"
-+mem: storing 117 in location 11
-+mem: storing 110 in location 12
-+mem: storing 107 in location 13
-+mem: storing 110 in location 14
-# ...
-
-:(scenario run_interactive_with_comment)
-def main [
-  # 2 instructions, with a comment after the first
-  1:address:array:number <- new [a:number <- copy 0  # abc
-b:number <- copy 0
-]
-  2:address:array:character, 3:address:array:character <- run-interactive 1:address:array:character
-]
-# no errors
-+mem: storing 0 in location 3
-
-:(before "End Globals")
-string Most_recent_products;
-:(before "End Setup")
-Most_recent_products = "";
-:(before "End of Instruction")
-if (Track_most_recent_products) {
-  track_most_recent_products(current_instruction(), products);
-}
-:(code)
-void track_most_recent_products(const instruction& instruction, const vector<vector<double> >& products) {
-  ostringstream out;
-  for (int i = 0; i < SIZE(products); ++i) {
-    // string
-    if (i < SIZE(instruction.products)) {
-      if (is_mu_string(instruction.products.at(i))) {
-        if (!scalar(products.at(i))) {
-          tb_shutdown();
-          cerr << read_mu_string(trace_error_contents()) << '\n';
-          cerr << SIZE(products.at(i)) << ": ";
-          for (int j = 0; j < SIZE(products.at(i)); ++j)
-            cerr << no_scientific(products.at(i).at(j)) << ' ';
-          cerr << '\n';
-        }
-        assert(scalar(products.at(i)));
-        out << read_mu_string(products.at(i).at(0)) << '\n';
-        continue;
-      }
-      // End Record Product Special-cases
-    }
-    for (int j = 0; j < SIZE(products.at(i)); ++j)
-      out << no_scientific(products.at(i).at(j)) << ' ';
-    out << '\n';
-  }
-  Most_recent_products = out.str();
-}
-
-:(code)
-string strip_comments(string in) {
-  ostringstream result;
-  for (int i = 0; i < SIZE(in); ++i) {
-    if (in.at(i) != '#') {
-      result << in.at(i);
-    }
-    else {
-      while (i+1 < SIZE(in) && in.at(i+1) != '\n')
-        ++i;
-    }
-  }
-  return result.str();
-}
-
-int stringified_value_of_location(int address) {
-  // convert to string
-  ostringstream out;
-  out << no_scientific(get_or_insert(Memory, address));
-  return new_mu_string(out.str());
-}
-
-int trace_error_contents() {
-  if (!Trace_stream) return 0;
-  ostringstream out;
-  for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
-    if (p->label != "error") continue;
-    out << p->contents;
-    if (*--p->contents.end() != '\n') out << '\n';
-  }
-  string result = out.str();
-  if (result.empty()) return 0;
-  truncate(result);
-  return new_mu_string(result);
-}
-
-int trace_app_contents() {
-  if (!Trace_stream) return 0;
-  ostringstream out;
-  for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
-    if (p->depth != App_depth) continue;
-    out << p->contents;
-    if (*--p->contents.end() != '\n') out << '\n';
-  }
-  string result = out.str();
-  if (result.empty()) return 0;
-  truncate(result);
-  return new_mu_string(result);
-}
-
-void truncate(string& x) {
-  if (SIZE(x) > 1024) {
-    x.erase(1024);
-    *x.rbegin() = '\n';
-    *++x.rbegin() = '.';
-    *++++x.rbegin() = '.';
-  }
-}
-
-//: simpler version of run-interactive: doesn't do any running, just loads
-//: recipes and reports errors.
-
-:(before "End Primitive Recipe Declarations")
-RELOAD,
-:(before "End Primitive Recipe Numbers")
-put(Recipe_ordinal, "reload", RELOAD);
-:(before "End Primitive Recipe Checks")
-case RELOAD: {
-  if (SIZE(inst.ingredients) != 1) {
-    raise << maybe(get(Recipe, r).name) << "'reload' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end();
-    break;
-  }
-  if (!is_mu_string(inst.ingredients.at(0))) {
-    raise << maybe(get(Recipe, r).name) << "first ingredient of 'reload' should be a string, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
-    break;
-  }
-  break;
-}
-:(before "End Primitive Recipe Implementations")
-case RELOAD: {
-  // conundrum: need to support repeated reloads of the same code, but not
-  // wipe out state for the current test
-  // hacky solution: subset of restore_snapshots() without restoring recipes {
-  // can't yet define containers in a test that runs 'reload'
-  Type_ordinal = Type_ordinal_snapshot;
-  Type = Type_snapshot;
-  // can't yet create new specializations of shape-shifting recipes in a test
-  // that runs 'reload'
-  Recipe_variants = Recipe_variants_snapshot;
-  Name = Name_snapshot;
-  // }
-  string code = read_mu_string(ingredients.at(0).at(0));
-  run_code_begin(/*should_stash_snapshots*/false);
-  routine* save_current_routine = Current_routine;
-  Current_routine = NULL;
-  vector<recipe_ordinal> recipes_reloaded = load(code);
-  transform_all();
-  Trace_stream->newline();  // flush trace
-  Current_routine = save_current_routine;
-  products.resize(1);
-  products.at(0).push_back(trace_error_contents());
-  run_code_end();  // wait until we're done with the trace contents
-  break;
-}
-
-:(scenario reload_continues_past_error)
-def main [
-  local-scope
-  x:address:array:character <- new [recipe foo [
-  get 1234:number, foo:offset
-]]
-  reload x
-  1:number/raw <- copy 34
-]
-+mem: storing 34 in location 1
-
-:(scenario reload_can_repeatedly_load_container_definitions)
-# define a container and try to create it (merge requires knowing container size)
-def main [
-  local-scope
-  x:address:array:character <- new [
-    container foo [
-      x:number
-      y:number
-    ]
-    recipe bar [
-      local-scope
-      x:foo <- merge 34, 35
-    ]
-  ]
-  # save warning addresses in locations of type 'number' to avoid spurious changes to them due to 'abandon'
-  1:number/raw <- reload x
-  2:number/raw <- reload x
-]
-# no errors on either load
-+mem: storing 0 in location 1
-+mem: storing 0 in location 2
-
- - - diff --git a/html/091run_sandboxed.cc.html b/html/091run_sandboxed.cc.html new file mode 100644 index 00000000..375b6beb --- /dev/null +++ b/html/091run_sandboxed.cc.html @@ -0,0 +1,556 @@ + + + + +Mu - 091run_sandboxed.cc + + + + + + + + + + +
+//: Helper for various programming environments: run arbitrary mu code and
+//: return some result in string form.
+
+:(scenario run_interactive_code)
+def main [
+  1:number/raw <- copy 0
+  2:address:array:character <- new [1:number/raw <- copy 34]
+  run-sandboxed 2:address:array:character
+  3:number/raw <- copy 1:number/raw
+]
++mem: storing 34 in location 3
+
+:(scenario run_interactive_empty)
+def main [
+  1:address:array:character <- copy 0/unsafe
+  2:address:array:character <- run-sandboxed 1:address:array:character
+]
+# result is null
++mem: storing 0 in location 2
+
+//: As the name suggests, 'run-sandboxed' will prevent certain operations that
+//: regular Mu code can perform.
+:(before "End Globals")
+bool Sandbox_mode = false;
+//: for starters, users can't override 'main' when the environment is running
+:(before "End Load Recipe Name")
+if (Sandbox_mode && result.name == "main") {
+  slurp_balanced_bracket(in);
+  return -1;
+}
+
+//: run code in 'interactive mode', i.e. with errors off and return:
+//:   stringified output in case we want to print it to screen
+//:   any errors encountered
+//:   simulated screen any prints went to
+//:   any 'app' layer traces generated
+:(before "End Primitive Recipe Declarations")
+RUN_SANDBOXED,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "run-sandboxed", RUN_SANDBOXED);
+:(before "End Primitive Recipe Checks")
+case RUN_SANDBOXED: {
+  if (SIZE(inst.ingredients) != 1) {
+    raise << maybe(get(Recipe, r).name) << "'run-sandboxed' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end();
+    break;
+  }
+  if (!is_mu_string(inst.ingredients.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "first ingredient of 'run-sandboxed' should be a string, but got '" << to_string(inst.ingredients.at(0)) << "'\n" << end();
+    break;
+  }
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case RUN_SANDBOXED: {
+  bool new_code_pushed_to_stack = run_interactive(ingredients.at(0).at(0));
+  if (!new_code_pushed_to_stack) {
+    products.resize(5);
+    products.at(0).push_back(0);
+    products.at(1).push_back(trace_error_contents());
+    products.at(2).push_back(0);
+    products.at(3).push_back(trace_app_contents());
+    products.at(4).push_back(1);  // completed
+    run_code_end();
+    break;  // done with this instruction
+  }
+  else {
+    continue;  // not done with caller; don't increment current_step_index()
+  }
+}
+
+:(before "End Globals")
+bool Track_most_recent_products = false;
+:(before "End Tracing")
+trace_stream* Save_trace_stream = NULL;
+string Save_trace_file;
+:(before "End Setup")
+Track_most_recent_products = false;
+:(code)
+// reads a string, tries to call it as code (treating it as a test), saving
+// all errors.
+// returns true if successfully called (no errors found during load and transform)
+bool run_interactive(int address) {
+  assert(contains_key(Recipe_ordinal, "interactive") && get(Recipe_ordinal, "interactive") != 0);
+  // try to sandbox the run as best you can
+  // todo: test this
+  if (!Current_scenario) {
+    for (int i = 1; i < Reserved_for_tests; ++i)
+      Memory.erase(i);
+  }
+  string command = trim(strip_comments(read_mu_string(address)));
+  Name[get(Recipe_ordinal, "interactive")].clear();
+  run_code_begin(/*should_stash_snapshots*/true);
+  if (command.empty()) return false;
+  // don't kill the current routine on parse errors
+  routine* save_current_routine = Current_routine;
+  Current_routine = NULL;
+  // call run(string) but without the scheduling
+  load(string("recipe! interactive [\n") +
+          "new-default-space\n" +  // disable automatic abandon so tests can see changes
+          "screen:address:screen <- next-ingredient\n" +
+          "$start-tracking-products\n" +
+          command + "\n" +
+          "$stop-tracking-products\n" +
+          "return screen\n" +
+       "]\n");
+  transform_all();
+  Current_routine = save_current_routine;
+  if (trace_count("error") > 0) return false;
+  // now call 'sandbox' which will run 'interactive' in a separate routine,
+  // and wait for it
+  if (Save_trace_stream) {
+    ++Save_trace_stream->callstack_depth;
+    trace(9999, "trace") << "run-sandboxed: incrementing callstack depth to " << Save_trace_stream->callstack_depth << end();
+    assert(Save_trace_stream->callstack_depth < 9000);  // 9998-101 plus cushion
+  }
+  Current_routine->calls.push_front(call(get(Recipe_ordinal, "sandbox")));
+  return true;
+}
+
+//: Carefully update all state to exactly how it was -- including snapshots.
+
+:(before "End Globals")
+map<string, recipe_ordinal> Recipe_ordinal_snapshot_stash;
+map<recipe_ordinal, recipe> Recipe_snapshot_stash;
+map<string, type_ordinal> Type_ordinal_snapshot_stash;
+map<type_ordinal, type_info> Type_snapshot_stash;
+map<recipe_ordinal, map<string, int> > Name_snapshot_stash;
+map<string, vector<recipe_ordinal> > Recipe_variants_snapshot_stash;
+
+:(code)
+void run_code_begin(bool should_stash_snapshots) {
+  // stuff to undo later, in run_code_end()
+  Hide_errors = true;
+  Disable_redefine_checks = true;
+  if (should_stash_snapshots)
+    stash_snapshots();
+  Save_trace_stream = Trace_stream;
+  Trace_stream = new trace_stream;
+  Trace_stream->collect_depth = App_depth;
+}
+
+void run_code_end() {
+  Hide_errors = false;
+  Disable_redefine_checks = false;
+  delete Trace_stream;
+  Trace_stream = Save_trace_stream;
+  Save_trace_stream = NULL;
+  Save_trace_file.clear();
+  Recipe.erase(get(Recipe_ordinal, "interactive"));  // keep past sandboxes from inserting errors
+  if (!Recipe_snapshot_stash.empty())
+    unstash_snapshots();
+}
+
+// keep sync'd with save_snapshots and restore_snapshots
+void stash_snapshots() {
+  assert(Recipe_ordinal_snapshot_stash.empty());
+  Recipe_ordinal_snapshot_stash = Recipe_ordinal_snapshot;
+  assert(Recipe_snapshot_stash.empty());
+  Recipe_snapshot_stash = Recipe_snapshot;
+  assert(Type_ordinal_snapshot_stash.empty());
+  Type_ordinal_snapshot_stash = Type_ordinal_snapshot;
+  assert(Type_snapshot_stash.empty());
+  Type_snapshot_stash = Type_snapshot;
+  assert(Name_snapshot_stash.empty());
+  Name_snapshot_stash = Name_snapshot;
+  assert(Recipe_variants_snapshot_stash.empty());
+  Recipe_variants_snapshot_stash = Recipe_variants_snapshot;
+  save_snapshots();
+}
+void unstash_snapshots() {
+  restore_snapshots();
+  Recipe_ordinal_snapshot = Recipe_ordinal_snapshot_stash;  Recipe_ordinal_snapshot_stash.clear();
+  Recipe_snapshot = Recipe_snapshot_stash;  Recipe_snapshot_stash.clear();
+  Type_ordinal_snapshot = Type_ordinal_snapshot_stash;  Type_ordinal_snapshot_stash.clear();
+  Type_snapshot = Type_snapshot_stash;  Type_snapshot_stash.clear();
+  Name_snapshot = Name_snapshot_stash;  Name_snapshot_stash.clear();
+  Recipe_variants_snapshot = Recipe_variants_snapshot_stash;  Recipe_variants_snapshot_stash.clear();
+}
+
+:(before "End Load Recipes")
+load(string(
+"recipe interactive [\n") +  // just a dummy version to initialize the Recipe_ordinal and so on
+"]\n" +
+"recipe sandbox [\n" +
+  "local-scope\n" +
+  "screen:address:screen <- new-fake-screen 30, 5\n" +
+  "routine-id:number <- start-running interactive, screen\n" +
+  "limit-time routine-id, 100000/instructions\n" +
+  "wait-for-routine routine-id\n" +
+  "instructions-run:number <- number-of-instructions routine-id\n" +
+  "stash instructions-run [instructions run]\n" +
+  "sandbox-state:number <- routine-state routine-id\n" +
+  "completed?:boolean <- equal sandbox-state, 1/completed\n" +
+  "output:address:array:character <- $most-recent-products\n" +
+  "errors:address:array:character <- save-errors\n" +
+  "stashes:address:array:character <- save-app-trace\n" +
+  "$cleanup-run-sandboxed\n" +
+  "return output, errors, screen, stashes, completed?\n" +
+"]\n");
+
+//: adjust errors in the sandbox
+:(after "string maybe(string s)")
+  if (s == "interactive") return "";
+
+:(scenario run_interactive_comments)
+def main [
+  1:address:array:character <- new [# ab
+add 2, 2]
+  2:address:array:character <- run-sandboxed 1:address:array:character
+  3:array:character <- copy *2:address:array:character
+]
++mem: storing 52 in location 4
+
+:(before "End Primitive Recipe Declarations")
+_START_TRACKING_PRODUCTS,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "$start-tracking-products", _START_TRACKING_PRODUCTS);
+:(before "End Primitive Recipe Checks")
+case _START_TRACKING_PRODUCTS: {
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case _START_TRACKING_PRODUCTS: {
+  Track_most_recent_products = true;
+  break;
+}
+
+:(before "End Primitive Recipe Declarations")
+_STOP_TRACKING_PRODUCTS,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "$stop-tracking-products", _STOP_TRACKING_PRODUCTS);
+:(before "End Primitive Recipe Checks")
+case _STOP_TRACKING_PRODUCTS: {
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case _STOP_TRACKING_PRODUCTS: {
+  Track_most_recent_products = false;
+  break;
+}
+
+:(before "End Primitive Recipe Declarations")
+_MOST_RECENT_PRODUCTS,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "$most-recent-products", _MOST_RECENT_PRODUCTS);
+:(before "End Primitive Recipe Checks")
+case _MOST_RECENT_PRODUCTS: {
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case _MOST_RECENT_PRODUCTS: {
+  products.resize(1);
+  products.at(0).push_back(new_mu_string(Most_recent_products));
+  break;
+}
+
+:(before "End Primitive Recipe Declarations")
+SAVE_ERRORS,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "save-errors", SAVE_ERRORS);
+:(before "End Primitive Recipe Checks")
+case SAVE_ERRORS: {
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case SAVE_ERRORS: {
+  products.resize(1);
+  products.at(0).push_back(trace_error_contents());
+  break;
+}
+
+:(before "End Primitive Recipe Declarations")
+SAVE_APP_TRACE,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "save-app-trace", SAVE_APP_TRACE);
+:(before "End Primitive Recipe Checks")
+case SAVE_APP_TRACE: {
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case SAVE_APP_TRACE: {
+  products.resize(1);
+  products.at(0).push_back(trace_app_contents());
+  break;
+}
+
+:(before "End Primitive Recipe Declarations")
+_CLEANUP_RUN_SANDBOXED,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "$cleanup-run-sandboxed", _CLEANUP_RUN_SANDBOXED);
+:(before "End Primitive Recipe Checks")
+case _CLEANUP_RUN_SANDBOXED: {
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case _CLEANUP_RUN_SANDBOXED: {
+  run_code_end();
+  break;
+}
+
+:(scenario "run_interactive_converts_result_to_text")
+def main [
+  # try to interactively add 2 and 2
+  1:address:array:character <- new [add 2, 2]
+  2:address:array:character <- run-sandboxed 1:address:array:character
+  10:array:character <- copy 2:address:array:character/lookup
+]
+# first letter in the output should be '4' in unicode
++mem: storing 52 in location 11
+
+:(scenario "run_interactive_returns_text")
+def main [
+  # try to interactively add 2 and 2
+  1:address:array:character <- new [
+    x:address:array:character <- new [a]
+    y:address:array:character <- new [b]
+    z:address:array:character <- append x:address:array:character, y:address:array:character
+  ]
+  2:address:array:character <- run-sandboxed 1:address:array:character
+  10:array:character <- copy 2:address:array:character/lookup
+]
+# output contains "ab"
++mem: storing 97 in location 11
++mem: storing 98 in location 12
+
+:(scenario "run_interactive_returns_errors")
+def main [
+  # run a command that generates an error
+  1:address:array:character <- new [x:number <- copy 34
+get x:number, foo:offset]
+  2:address:array:character, 3:address:array:character <- run-sandboxed 1:address:array:character
+  10:array:character <- copy 3:address:array:character/lookup
+]
+# error should be "unknown element foo in container number"
++mem: storing 117 in location 11
++mem: storing 110 in location 12
++mem: storing 107 in location 13
++mem: storing 110 in location 14
+# ...
+
+:(scenario run_interactive_with_comment)
+def main [
+  # 2 instructions, with a comment after the first
+  1:address:array:number <- new [a:number <- copy 0  # abc
+b:number <- copy 0
+]
+  2:address:array:character, 3:address:array:character <- run-sandboxed 1:address:array:character
+]
+# no errors
++mem: storing 0 in location 3
+
+:(before "End Globals")
+string Most_recent_products;
+:(before "End Setup")
+Most_recent_products = "";
+:(before "End of Instruction")
+if (Track_most_recent_products) {
+  track_most_recent_products(current_instruction(), products);
+}
+:(code)
+void track_most_recent_products(const instruction& instruction, const vector<vector<double> >& products) {
+  ostringstream out;
+  for (int i = 0; i < SIZE(products); ++i) {
+    // string
+    if (i < SIZE(instruction.products)) {
+      if (is_mu_string(instruction.products.at(i))) {
+        if (!scalar(products.at(i))) continue;  // error handled elsewhere
+        out << read_mu_string(products.at(i).at(0)) << '\n';
+        continue;
+      }
+      // End Record Product Special-cases
+    }
+    for (int j = 0; j < SIZE(products.at(i)); ++j)
+      out << no_scientific(products.at(i).at(j)) << ' ';
+    out << '\n';
+  }
+  Most_recent_products = out.str();
+}
+
+:(code)
+string strip_comments(string in) {
+  ostringstream result;
+  for (int i = 0; i < SIZE(in); ++i) {
+    if (in.at(i) != '#') {
+      result << in.at(i);
+    }
+    else {
+      while (i+1 < SIZE(in) && in.at(i+1) != '\n')
+        ++i;
+    }
+  }
+  return result.str();
+}
+
+int stringified_value_of_location(int address) {
+  // convert to string
+  ostringstream out;
+  out << no_scientific(get_or_insert(Memory, address));
+  return new_mu_string(out.str());
+}
+
+int trace_error_contents() {
+  if (!Trace_stream) return 0;
+  ostringstream out;
+  for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
+    if (p->label != "error") continue;
+    out << p->contents;
+    if (*--p->contents.end() != '\n') out << '\n';
+  }
+  string result = out.str();
+  if (result.empty()) return 0;
+  truncate(result);
+  return new_mu_string(result);
+}
+
+int trace_app_contents() {
+  if (!Trace_stream) return 0;
+  ostringstream out;
+  for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
+    if (p->depth != App_depth) continue;
+    out << p->contents;
+    if (*--p->contents.end() != '\n') out << '\n';
+  }
+  string result = out.str();
+  if (result.empty()) return 0;
+  truncate(result);
+  return new_mu_string(result);
+}
+
+void truncate(string& x) {
+  if (SIZE(x) > 1024) {
+    x.erase(1024);
+    *x.rbegin() = '\n';
+    *++x.rbegin() = '.';
+    *++++x.rbegin() = '.';
+  }
+}
+
+//: simpler version of run-sandboxed: doesn't do any running, just loads
+//: recipes and reports errors.
+
+:(before "End Primitive Recipe Declarations")
+RELOAD,
+:(before "End Primitive Recipe Numbers")
+put(Recipe_ordinal, "reload", RELOAD);
+:(before "End Primitive Recipe Checks")
+case RELOAD: {
+  if (SIZE(inst.ingredients) != 1) {
+    raise << maybe(get(Recipe, r).name) << "'reload' requires exactly one ingredient, but got '" << inst.original_string << "'\n" << end();
+    break;
+  }
+  if (!is_mu_string(inst.ingredients.at(0))) {
+    raise << maybe(get(Recipe, r).name) << "first ingredient of 'reload' should be a string, but got '" << inst.ingredients.at(0).original_string << "'\n" << end();
+    break;
+  }
+  break;
+}
+:(before "End Primitive Recipe Implementations")
+case RELOAD: {
+  // conundrum: need to support repeated reloads of the same code, but not
+  // wipe out state for the current test
+  // hacky solution: subset of restore_snapshots() without restoring recipes {
+  // can't yet define containers in a test that runs 'reload'
+  Type_ordinal = Type_ordinal_snapshot;
+  Type = Type_snapshot;
+  // can't yet create new specializations of shape-shifting recipes in a test
+  // that runs 'reload'
+  Recipe_variants = Recipe_variants_snapshot;
+  Name = Name_snapshot;
+  // }
+  string code = read_mu_string(ingredients.at(0).at(0));
+  run_code_begin(/*should_stash_snapshots*/false);
+  routine* save_current_routine = Current_routine;
+  Current_routine = NULL;
+  Sandbox_mode = true;
+  vector<recipe_ordinal> recipes_reloaded = load(code);
+  transform_all();
+  Trace_stream->newline();  // flush trace
+  Sandbox_mode = false;
+  Current_routine = save_current_routine;
+  products.resize(1);
+  products.at(0).push_back(trace_error_contents());
+  run_code_end();  // wait until we're done with the trace contents
+  break;
+}
+
+:(scenario reload_continues_past_error)
+def main [
+  local-scope
+  x:address:array:character <- new [recipe foo [
+  get 1234:number, foo:offset
+]]
+  reload x
+  1:number/raw <- copy 34
+]
++mem: storing 34 in location 1
+
+:(scenario reload_can_repeatedly_load_container_definitions)
+# define a container and try to create it (merge requires knowing container size)
+def main [
+  local-scope
+  x:address:array:character <- new [
+    container foo [
+      x:number
+      y:number
+    ]
+    recipe bar [
+      local-scope
+      x:foo <- merge 34, 35
+    ]
+  ]
+  # save warning addresses in locations of type 'number' to avoid spurious changes to them due to 'abandon'
+  1:number/raw <- reload x
+  2:number/raw <- reload x
+]
+# no errors on either load
++mem: storing 0 in location 1
++mem: storing 0 in location 2
+
+ + + diff --git a/html/edit/005-sandbox.mu.html b/html/edit/005-sandbox.mu.html index a4aa964c..7c4fe9e8 100644 --- a/html/edit/005-sandbox.mu.html +++ b/html/edit/005-sandbox.mu.html @@ -237,7 +237,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color local-scope load-ingredients data:address:array:character <- get *sandbox, data:offset - response:address:array:character, _, fake-screen:address:screen <- run-interactive data + response:address:array:character, _, fake-screen:address:screen <- run-sandboxed data *sandbox <- put *sandbox, response:offset, response *sandbox <- put *sandbox, screen:offset, fake-screen ] diff --git a/html/edit/010-sandbox-trace.mu.html b/html/edit/010-sandbox-trace.mu.html index 608bd63c..a5c77f15 100644 --- a/html/edit/010-sandbox-trace.mu.html +++ b/html/edit/010-sandbox-trace.mu.html @@ -213,7 +213,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color local-scope load-ingredients data:address:array:character <- get *sandbox, data:offset - response:address:array:character, _, fake-screen:address:screen, trace:address:array:character <- run-interactive data + response:address:array:character, _, fake-screen:address:screen, trace:address:array:character <- run-sandboxed data *sandbox <- put *sandbox, response:offset, response *sandbox <- put *sandbox, screen:offset, fake-screen *sandbox <- put *sandbox, trace:offset, trace diff --git a/html/edit/011-errors.mu.html b/html/edit/011-errors.mu.html index 521006e4..b9db2f50 100644 --- a/html/edit/011-errors.mu.html +++ b/html/edit/011-errors.mu.html @@ -116,7 +116,7 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color local-scope load-ingredients data:address:array:character <- get *sandbox, data:offset - response:address:array:character, errors:address:array:character, fake-screen:address:screen, trace:address:array:character, completed?:boolean <- run-interactive data + response:address:array:character, errors:address:array:character, fake-screen:address:screen, trace:address:array:character, completed?:boolean <- run-sandboxed data *sandbox <- put *sandbox, response:offset, response *sandbox <- put *sandbox, errors:offset, errors *sandbox <- put *sandbox, screen:offset, fake-screen diff --git a/html/lambda-to-mu.mu.html b/html/lambda-to-mu.mu.html index 0478a1b7..d93a8b00 100644 --- a/html/lambda-to-mu.mu.html +++ b/html/lambda-to-mu.mu.html @@ -599,6 +599,24 @@ body { font-size: 12pt; font-family: monospace; color: #eeeeee; background-color 40:array:character <- [ghi] # result.rest.rest ] ] + +## convert tree of cells to mu text + +def to-mu in:address:cell -> out:address:array:character [ + local-scope + load-ingredients + buf:address:buffer <- new-buffer 30 + buf <- to-mu in, buf + out <- buffer-to-array buf +] + +def to-mu in:address:cell, buf:address:buffer -> buf:address:buffer, result-name:address:array:character [ + local-scope + load-ingredients + # null cell? no change. + # pair with all atoms? gensym a new variable + # pair containing other pairs? recurse +]
-- cgit 1.4.1-2-gfad0