:(before "End Primitive Recipe Declarations")
RUN_INTERACTIVE,
:(before "End Primitive Recipe Numbers")
Recipe_number["run-interactive"] = RUN_INTERACTIVE;
:(before "End Primitive Recipe Implementations")
case RUN_INTERACTIVE: {
assert(scalar(ingredients.at(0)));
run_interactive(ingredients.at(0).at(0));
continue;
}
:(code)
void run_interactive(long long int address) {
long long int size = Memory[address];
if (size == 0) {
++current_step_index();
return;
}
ostringstream tmp;
for (long long int curr = address+1; curr < address+size; ++curr) {
tmp << (char)(int)Memory[curr];
}
assert(Memory[address+size] == 10);
if (Recipe_number.find("interactive") == Recipe_number.end())
Recipe_number["interactive"] = Next_recipe_number++;
string command = trim(strip_comments(tmp.str()));
if (command.empty()) {
++current_step_index();
return;
}
if (is_integer(command)) {
print_value_of_location_as_response(to_integer(command));
++current_step_index();
return;
}
if (Name[Recipe_number["interactive"]].find(command) != Name[Recipe_number["interactive"]].end()) {
print_value_of_location_as_response(Name[Recipe_number["interactive"]][command]);
++current_step_index();
return;
}
Recipe.erase(Recipe_number["interactive"]);
load("recipe interactive [\n"+command+"\n]\n");
transform_all();
Current_routine->calls.push_front(call(Recipe_number["interactive"]));
}
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();
}
void print_value_of_location_as_response(long long int address) {
ostringstream out;
out << "=> " << Memory[address];
string result = out.str();
if (!tb_is_active()) {
cerr << result << '\n';
return;
}
long long int bound = SIZE(result);
if (bound > tb_width()) bound = tb_width();
for (long long int i = 0; i < bound; ++i) {
tb_change_cell(i, Display_row, result.at(i), 245, TB_BLACK);
}
if (Display_row < tb_height()-1)
++Display_row;
Display_column = 0;
tb_set_cursor(Display_column, Display_row);
tb_present();
}
:(before "End Primitive Recipe Declarations")
_RUN_DEPTH,
:(before "End Primitive Recipe Numbers")
Recipe_number["$run-depth"] = _RUN_DEPTH;
:(before "End Primitive Recipe Implementations")
case _RUN_DEPTH: {
cerr << Current_routine->calls.size();
break;
}