mu/081run_interactive.cc

278 lines
9.6 KiB
C++
Raw Normal View History

//: Helper for various programming environments: run arbitrary mu code and
//: return some result in string form.
:(scenario run_interactive_code)
recipe main [
2015-08-16 17:45:11 +00:00
2:address:array:character <- new [1:number/raw <- copy 34]
run-interactive 2:address:array:character
]
2015-08-16 17:45:11 +00:00
+mem: storing 34 in location 1
:(scenario run_interactive_empty)
recipe main [
1:address:array:character <- run-interactive 0
]
# result is null
+mem: storing 0 in location 1
//: 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
//: any 'app' layer traces generated
:(before "End Primitive Recipe Declarations")
RUN_INTERACTIVE,
:(before "End Primitive Recipe Numbers")
Recipe_ordinal["run-interactive"] = RUN_INTERACTIVE;
//? cerr << "run-interactive: " << RUN_INTERACTIVE << '\n'; //? 1
:(before "End Primitive Recipe Implementations")
case RUN_INTERACTIVE: {
if (SIZE(ingredients) != 1) {
raise << current_recipe_name() << ": 'run-interactive' requires exactly one ingredient, but got " << current_instruction().to_string() << '\n' << end();
break;
}
if (!scalar(ingredients.at(0))) {
raise << current_recipe_name() << ": first ingredient of 'run-interactive' should be a literal string, but got " << current_instruction().ingredients.at(0).original_string << '\n' << end();
break;
}
bool new_code_pushed_to_stack = run_interactive(ingredients.at(0).at(0));
if (!new_code_pushed_to_stack) {
products.resize(4);
products.at(0).push_back(0);
products.at(1).push_back(trace_contents("warn"));
products.at(2).push_back(0);
products.at(3).push_back(trace_contents("app"));
2015-07-16 01:05:28 +00:00
clean_up_interactive();
break; // done with this instruction
}
else {
continue; // not done with caller; don't increment current_step_index()
}
}
:(before "End Globals")
2015-08-16 05:37:55 +00:00
bool Track_most_recent_products = false;
:(before "End Setup")
2015-08-16 05:37:55 +00:00
Track_most_recent_products = false;
:(code)
// 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++;
// try to sandbox the run as best you can
// todo: test this
if (!Current_scenario) {
for (long long int i = 1; i < Reserved_for_tests; ++i)
Memory.erase(i);
}
2015-07-19 16:57:40 +00:00
string command = trim(strip_comments(read_mu_string(address)));
if (command.empty()) return false;
Recipe.erase(Recipe_ordinal["interactive"]);
Name[Recipe_ordinal["interactive"]].clear();
Hide_warnings = true;
2015-07-11 07:55:01 +00:00
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_layers.insert("warn");
Trace_stream->collect_layers.insert("app");
2015-07-11 07:55:01 +00:00
}
// call run(string) but without the scheduling
load(string("recipe interactive [\n") +
"local-scope\n" +
2015-07-26 05:11:58 +00:00
"screen:address <- new-fake-screen 30, 5\n" +
command + "\n" +
"reply screen\n" +
"]\n");
transform_all();
2015-07-16 01:05:28 +00:00
if (trace_count("warn") > 0) return false;
2015-08-16 05:37:55 +00:00
Track_most_recent_products = true;
Current_routine->calls.push_front(call(Recipe_ordinal["interactive"]));
return true;
}
:(scenario "run_interactive_returns_stringified_result")
recipe 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
2015-07-08 22:25:11 +00:00
:(scenario "run_interactive_returns_string")
recipe main [
# try to interactively add 2 and 2
1:address:array:character <- new [
100:address:array:character <- new [a]
101:address:array:character <- new [b]
102:address:array:character <- string-append 100:address:array:character, 101:address:array:character
]
2:address:array:character <- run-interactive 1:address:array:character
10:array:character <- copy 2:address:array:character/lookup
2015-07-08 22:25:11 +00:00
]
# output contains "ab"
+mem: storing 97 in location 11
+mem: storing 98 in location 12
2015-07-08 23:16:11 +00:00
:(scenario "run_interactive_returns_warnings")
recipe main [
# run a command that generates a warning
1:address:array:character <- new [get 1234: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
2015-07-08 23:16:11 +00:00
]
# warning 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
:(before "End Globals")
2015-08-16 05:37:55 +00:00
string Most_recent_products;
2015-07-08 21:53:37 +00:00
:(before "End Setup")
2015-08-16 05:37:55 +00:00
Most_recent_products = "";
:(before "End of Instruction")
2015-08-16 05:37:55 +00:00
if (Track_most_recent_products) {
track_most_recent_products(current_instruction(), products);
2015-07-08 21:53:37 +00:00
}
:(code)
2015-08-16 05:37:55 +00:00
void track_most_recent_products(const instruction& instruction, const vector<vector<double> >& products) {
ostringstream out;
for (long long int i = 0; i < SIZE(products); ++i) {
2015-07-08 22:25:11 +00:00
// 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_contents("warn")) << '\n';
cerr << SIZE(products.at(i)) << ": ";
for (long long int j = 0; j < SIZE(products.at(i)); ++j)
cerr << products.at(i).at(j) << ' ';
cerr << '\n';
}
2015-07-08 22:25:11 +00:00
assert(scalar(products.at(i)));
2015-07-19 16:57:40 +00:00
out << read_mu_string(products.at(i).at(0)) << '\n';
2015-07-08 22:25:11 +00:00
continue;
}
// End Record Product Special-cases
}
2015-07-16 01:05:28 +00:00
for (long long int j = 0; j < SIZE(products.at(i)); ++j)
out << products.at(i).at(j) << ' ';
out << '\n';
}
2015-08-16 05:37:55 +00:00
Most_recent_products = out.str();
}
//: Recipe 'interactive' doesn't return what 'run-interactive seems to return.
//: Massage results from former to latter.
:(after "Starting Reply")
if (Current_routine->calls.front().running_recipe == Recipe_ordinal["interactive"]) {
products.resize(4);
products.at(0).push_back(new_mu_string(Most_recent_products));
products.at(1).push_back(trace_contents("warn"));
assert(SIZE(ingredients) == 1);
assert(scalar(ingredients.at(0)));
products.at(2).push_back(ingredients.at(0).at(0)); // screen
products.at(3).push_back(trace_contents("app"));
--Callstack_depth;
Current_routine->calls.pop_front();
assert(!Current_routine->calls.empty());
2015-08-16 16:22:22 +00:00
clean_up_interactive();
break;
}
//: clean up reply after we've popped it off the call-stack
:(code)
void clean_up_interactive() {
Hide_warnings = false;
2015-08-16 05:37:55 +00:00
Track_most_recent_products = false;
2015-08-16 16:22:22 +00:00
if (Trace_stream->is_narrowly_collecting("warn")) { // hack
delete Trace_stream;
Trace_stream = NULL;
}
}
:(code)
2015-06-06 18:10:46 +00:00
string strip_comments(string in) {
ostringstream result;
for (long long int i = 0; i < SIZE(in); ++i) {
if (in.at(i) != '#') {
result << in.at(i);
}
else {
while (i < SIZE(in) && in.at(i) != '\n')
++i;
if (i < SIZE(in) && in.at(i) == '\n') ++i;
}
}
return result.str();
}
long long int stringified_value_of_location(long long int address) {
// convert to string
ostringstream out;
out << Memory[address];
2015-07-19 16:57:40 +00:00
return new_mu_string(out.str());
}
long long int trace_contents(const string& layer) {
2015-07-08 23:16:11 +00:00
if (!Trace_stream) return 0;
//? cerr << "trace stream exists\n"; //? 1
if (trace_count(layer) <= 0) return 0;
//? cerr << layer << " has something\n"; //? 1
2015-07-08 23:16:11 +00:00
ostringstream out;
for (vector<trace_line>::iterator p = Trace_stream->past_lines.begin(); p != Trace_stream->past_lines.end(); ++p) {
if (p->label != layer) continue;
2015-07-08 23:16:11 +00:00
out << p->contents;
if (*--p->contents.end() != '\n') out << '\n';
}
assert(!out.str().empty());
//? cerr << layer << ":\n" << out.str() << "\n--\n"; //? 1
2015-07-19 16:57:40 +00:00
return new_mu_string(out.str());
2015-07-08 23:16:11 +00:00
}
//: simpler version of run-interactive: doesn't do any running, just loads
//: recipes and reports warnings.
:(before "End Primitive Recipe Declarations")
RELOAD,
:(before "End Primitive Recipe Numbers")
Recipe_ordinal["reload"] = RELOAD;
:(before "End Primitive Recipe Implementations")
case RELOAD: {
if (SIZE(ingredients) != 1) {
raise << current_recipe_name() << ": 'reload' requires exactly one ingredient, but got " << current_instruction().to_string() << '\n' << end();
break;
}
if (!scalar(ingredients.at(0))) {
raise << current_recipe_name() << ": first ingredient of 'reload' should be a literal string, but got " << current_instruction().ingredients.at(0).original_string << '\n' << end();
break;
}
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_layers.insert("warn");
}
Hide_warnings = true;
2015-07-25 07:24:12 +00:00
Disable_redefine_warnings = true;
2015-08-14 23:46:49 +00:00
vector<recipe_ordinal> recipes_reloaded = load(read_mu_string(ingredients.at(0).at(0)));
for (long long int i = 0; i < SIZE(recipes_reloaded); ++i) {
Name.erase(recipes_reloaded.at(i));
}
transform_all();
Trace_stream->newline(); // flush trace
2015-07-25 07:24:12 +00:00
Disable_redefine_warnings = false;
Hide_warnings = false;
products.resize(1);
products.at(0).push_back(trace_contents("warn"));
// hack: assume collect_layers isn't set anywhere else
if (Trace_stream->is_narrowly_collecting("warn")) {
delete Trace_stream;
Trace_stream = NULL;
}
break;
}