From 762b099ef6c6ad5b6b61d29e473baa6df8d64ab9 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sat, 18 Jul 2015 15:22:49 -0700 Subject: 1818 --- html/081run_interactive.cc.html | 112 +++++++++++++++++++++++----------------- 1 file changed, 66 insertions(+), 46 deletions(-) (limited to 'html/081run_interactive.cc.html') diff --git a/html/081run_interactive.cc.html b/html/081run_interactive.cc.html index 5183cee6..54b1456c 100644 --- a/html/081run_interactive.cc.html +++ b/html/081run_interactive.cc.html @@ -49,8 +49,10 @@ recipe main [ # result is null +mem: storing 0 in location 1 -//: run code in 'interactive mode', i.e. with warnings off, and recording -//: output in case we want to print it to screen +//: run code in 'interactive mode', i.e. with warnings off and return: +//: stringified output in case we want to print it to screen +//: any warnings encountered +//: simulated screen any prints went to :(before "End Primitive Recipe Declarations") RUN_INTERACTIVE, :(before "End Primitive Recipe Numbers") @@ -59,11 +61,13 @@ Recipe_ordinal["run-interactive"] = RUN_ :(before "End Primitive Recipe Implementations") case RUN_INTERACTIVE: { assert(scalar(ingredients.at(0))); - products.resize(2); + products.resize(3); bool new_code_pushed_to_stack = run_interactive(ingredients.at(0).at(0)); if (!new_code_pushed_to_stack) { products.at(0).push_back(0); products.at(1).push_back(warnings_from_trace()); + products.at(2).push_back(0); + clean_up_interactive(); break; // done with this instruction } else { @@ -73,49 +77,53 @@ case RUN_INTERACTIVE: { :(before "End Globals") bool Running_interactive = false; +long long int Old_screen = 0; // we can support one iteration of screen inside screen :(before "End Setup") Running_interactive = false; +Old_screen = 0; :(code) -// reads a string, tries to call it as code, saving all warnings. +// reads a string, tries to call it as code (treating it as a test), saving +// all warnings. // returns true if successfully called (no errors found during load and transform) bool run_interactive(long long int address) { if (Recipe_ordinal.find("interactive") == Recipe_ordinal.end()) Recipe_ordinal["interactive"] = Next_recipe_ordinal++; + Old_screen = Memory[SCREEN]; +//? cerr << "save screen: " << Old_screen << '\n'; //? 2 + // try to sandbox the run as best you can + // todo: test this + if (!Current_scenario) { + // not already sandboxed + for (long long int i = 1; i < Reserved_for_tests; ++i) + Memory.erase(i); + Name[Recipe_ordinal["interactive"]].clear(); + } +//? cerr << "screen was at " << Name[Recipe_ordinal["interactive"]]["screen"] << '\n'; //? 1 + Name[Recipe_ordinal["interactive"]]["screen"] = SCREEN; +//? cerr << "screen now at " << Name[Recipe_ordinal["interactive"]]["screen"] << '\n'; //? 1 string command = trim(strip_comments(to_string(address))); if (command.empty()) return false; -//? tb_shutdown(); //? 1 -//? cerr << command << '\n'; //? 2 Recipe.erase(Recipe_ordinal["interactive"]); Hide_warnings = true; if (!Trace_stream) { Trace_file = ""; // if there wasn't already a stream we don't want to save it Trace_stream = new trace_stream; + Trace_stream->collect_layer = "warn"; } // call run(string) but without the scheduling - load("recipe interactive [\n"+command+"\n]\n"); + // we won't create a local scope so that we can get to the new screen after + // we return from 'interactive'. + load(string("recipe interactive [\n") + + "screen:address <- new-fake-screen 5, 5\n" + + command + "\n" + + "]\n"); transform_all(); - if (trace_count("warn") > 0) { - Hide_warnings = false; - return false; - } + if (trace_count("warn") > 0) return false; Running_interactive = true; Current_routine->calls.push_front(call(Recipe_ordinal["interactive"])); return true; } -:(after "Starting Reply") -if (current_recipe_name() == "interactive") clean_up_interactive(); -:(after "Falling Through End Of Recipe") -if (current_recipe_name() == "interactive") clean_up_interactive(); -:(code) -void clean_up_interactive() { -//? static int foo = 0; //? 1 - Hide_warnings = false; - Running_interactive = false; -//? ++foo; //? 1 -//? if (foo == 1) tb_init(); //? 1 -} - :(scenario "run_interactive_returns_stringified_result") recipe main [ # try to interactively add 2 and 2 @@ -165,42 +173,65 @@ if (Running_interactive)< :(code) void record_products(const instruction& instruction, const vector<vector<double> >& products) { ostringstream out; -//? cerr << current_instruction().to_string() << '\n'; //? 1 for (long long int i = 0; i < SIZE(products); ++i) { // string if (i < SIZE(instruction.products)) { -//? cerr << "AA\n"; //? 1 -//? cerr << instruction.products.size() << " vs " << i << '\n'; //? 1 if (is_string(instruction.products.at(i))) { -//? cerr << "BB\n"; //? 1 assert(scalar(products.at(i))); out << to_string(products.at(i).at(0)) << '\n'; continue; } // End Record Product Special-cases } - for (long long int j = 0; j < SIZE(products.at(i)); ++j) { -//? cerr << "aa: " << i << ", " << j << ": " << products.at(i).at(j) << '\n'; //? 1 - out << products.at(i).at(j) << ' '; - } + for (long long int j = 0; j < SIZE(products.at(i)); ++j) + out << products.at(i).at(j) << ' '; out << '\n'; } -//? cerr << "aa: {\n" << out.str() << "}\n"; //? 2 Most_recent_results = out.str(); } :(before "Complete Call Fallthrough") if (current_instruction().operation == RUN_INTERACTIVE && !current_instruction().products.empty()) { - assert(SIZE(current_instruction().products) <= 2); + assert(SIZE(current_instruction().products) <= 3); // Send the results of the most recently executed instruction, regardless of // call depth, to be converted to string and potentially printed to string. vector<double> result; result.push_back(new_string(Most_recent_results)); write_memory(current_instruction().products.at(0), result); - if (SIZE(current_instruction().products) == 2) { + if (SIZE(current_instruction().products) >= 2) { vector<double> warnings; warnings.push_back(warnings_from_trace()); write_memory(current_instruction().products.at(1), warnings); } + if (SIZE(current_instruction().products) >= 3) { + vector<double> screen; +//? cerr << "returning screen " << Memory[SCREEN] << " to " << current_instruction().products.at(2).to_string() << " value " << current_instruction().products.at(2).value << '\n'; //? 1 + screen.push_back(Memory[SCREEN]); + write_memory(current_instruction().products.at(2), screen); + } +} + +//: clean up reply after we've popped it off the call-stack +//: however, we need what was on the stack to decide whether to clean up +:(after "Starting Reply") +bool must_clean_up_interactive = (current_recipe_name() == "interactive"); +:(after "Falling Through End Of Recipe") +bool must_clean_up_interactive = (current_recipe_name() == "interactive"); +:(before "End Reply") +if (must_clean_up_interactive) clean_up_interactive(); +:(before "Complete Call Fallthrough") +if (must_clean_up_interactive) clean_up_interactive(); +:(code) +void clean_up_interactive() { + Hide_warnings = false; + Running_interactive = false; + // hack: assume collect_layer isn't set anywhere else + if (Trace_stream->collect_layer == "warn") { + delete Trace_stream; + Trace_stream = NULL; + } +//? cerr << "restore screen: " << Memory[SCREEN] << " to " << Old_screen << '\n'; //? 1 + Memory[SCREEN] = Old_screen; + Old_screen = 0; } :(code) @@ -227,17 +258,13 @@ string to_string(long long int address// todo: unicode tmp << (char)(int)Memory[curr]; } -//? tb_shutdown(); //? 1 -//? cerr << tmp.str() << '\n'; //? 1 return tmp.str(); } long long int stringified_value_of_location(long long int address) { // convert to string ostringstream out; -//? trace(1, "foo") << "a: " << address; //? 1 out << Memory[address]; -//? trace(1, "foo") << "b: " << Memory[address]; //? 1 return new_string(out.str()); } @@ -277,13 +304,6 @@ case RELOAD: { Loading_interactive = true; Hide_warnings = true; load(to_string(ingredients.at(0).at(0))); -//? static int foo = 0; -//? if (++foo == 2) { -//? tb_shutdown(); -//? cerr << Recipe_ordinal["new-add"] << '\n'; -//? cerr << Recipe[Recipe_ordinal["new-add"]].steps[2].to_string() << '\n'; -//? exit(0); -//? } transform_all(); Hide_warnings = false; Loading_interactive = false; -- cgit 1.4.1-2-gfad0