:(scenario calling_recipe)
recipe main [
f
]
recipe f [
3:number <- add 2:literal, 2:literal
]
+mem: storing 4 in location 3
:(scenario return_on_fallthrough)
recipe main [
f
1:number <- copy 0:literal
2:number <- copy 0:literal
3:number <- copy 0:literal
]
recipe f [
4:number <- copy 0:literal
5:number <- copy 0:literal
]
+run: f
+run: 4:number <- copy 0:literal
+run: 5:number <- copy 0:literal
+run: 1:number <- copy 0:literal
+run: 2:number <- copy 0:literal
+run: 3:number <- copy 0:literal
:(before "struct routine {")
struct call {
recipe_number running_recipe;
long long int running_step_index;
call(recipe_number r) {
running_recipe = r;
running_step_index = 0;
}
};
typedef list<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_front(call(r));
}
:(replace{} "inline long long int& current_step_index()")
inline long long int& current_step_index() {
assert(!Current_routine->calls.empty());
return Current_routine->calls.front().running_step_index;
}
:(replace{} "inline const string& current_recipe_name()")
inline const string& current_recipe_name() {
assert(!Current_routine->calls.empty());
return Recipe[Current_routine->calls.front().running_recipe].name;
}
:(replace{} "inline const instruction& current_instruction()")
inline const instruction& current_instruction() {
assert(!Current_routine->calls.empty());
return Recipe[Current_routine->calls.front().running_recipe].steps.at(Current_routine->calls.front().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().to_string() << '\n';
break;
}
Current_routine->calls.push_front(call(current_instruction().operation));
complete_call:
++Callstack_depth;
assert(Callstack_depth < 9000);
continue;
}
:(replace{} "inline bool routine::completed() const")
inline bool routine::completed() const {
return calls.empty();
}
inline const vector<instruction>& routine::steps() const {
assert(!calls.empty());
return Recipe[calls.front().running_recipe].steps;
}
:(before "Running One Instruction")
while (current_step_index() >= SIZE(Current_routine->steps())) {
--Callstack_depth;
Current_routine->calls.pop_front();
if (Current_routine->calls.empty()) return;
++current_step_index();
}
:(before "End Includes")
#include <stack>
using std::stack;