:(scenario calling_recipe)
recipe main [
f
]
recipe f [
3:integer <- add 2:literal, 2:literal
]
+mem: storing 4 in location 3
:(scenario return_on_fallthrough)
recipe main [
f
1:integer <- copy 34:literal
2:integer <- copy 34:literal
3:integer <- copy 34:literal
]
recipe f [
4:integer <- copy 34:literal
5:integer <- copy 34:literal
]
+run: instruction main/0
+run: instruction f/0
+run: instruction f/1
+run: instruction main/1
+run: instruction main/2
+run: instruction main/3
:(before "struct routine {")
struct call {
recipe_number running_recipe;
index_t running_step_index;
call(recipe_number r) :running_recipe(r), running_step_index(0) {}
};
typedef stack<call> call_stack;
:(replace{} "struct routine")
struct routine {
call_stack calls;
routine(recipe_number r);
bool completed() const;
const vector<instruction>& steps() const;
};
:(code)
routine::routine(recipe_number r) {
calls.push(call(r));
}
:(replace{} "inline index_t& current_step_index()")
inline index_t& current_step_index() {
return Current_routine->calls.top().running_step_index;
}
:(replace{} "inline const string& current_recipe_name()")
inline const string& current_recipe_name() {
return Recipe[Current_routine->calls.top().running_recipe].name;
}
:(replace{} "inline const instruction& current_instruction()")
inline const instruction& current_instruction() {
return Recipe[Current_routine->calls.top().running_recipe].steps[Current_routine->calls.top().running_step_index];
}
:(replace{} "default:" following "End Primitive Recipe Implementations")
default: {
if (Recipe.find(current_instruction().operation) == Recipe.end()) {
raise << "undefined operation " << current_instruction().operation << ": " << current_instruction().name << '\n';
break;
}
Current_routine->calls.push(call(current_instruction().operation));
continue;
}
:(replace{} "inline bool routine::completed() const")
inline bool routine::completed() const {
return calls.empty();
}
inline const vector<instruction>& routine::steps() const {
return Recipe[calls.top().running_recipe].steps;
}
:(before "Running One Instruction")
while (current_step_index() >= Current_routine->steps().size()) {
Current_routine->calls.pop();
if (Current_routine->calls.empty()) return;
++current_step_index();
}
:(before "End Includes")
#include <stack>
using std::stack;