2015-03-17 03:26:59 +00:00
|
|
|
//: So far the recipes we define can't run each other. Let's change that.
|
2015-03-14 00:39:32 +00:00
|
|
|
:(scenario "calling_recipe")
|
|
|
|
recipe main [
|
|
|
|
f
|
|
|
|
]
|
|
|
|
recipe f [
|
|
|
|
3:integer <- add 2:literal, 2:literal
|
|
|
|
]
|
2015-03-24 06:59:59 +00:00
|
|
|
+mem: storing 4 in location 3
|
2015-03-14 07:07:44 +00:00
|
|
|
|
2015-03-15 16:43:05 +00:00
|
|
|
:(before "struct routine {")
|
|
|
|
// Everytime a recipe runs another, we interrupt it and start running the new
|
|
|
|
// recipe. When that finishes, we continue this one where we left off.
|
|
|
|
// This requires maintaining a 'stack' of interrupted recipes or 'calls'.
|
|
|
|
struct call {
|
|
|
|
recipe_number running_recipe;
|
|
|
|
size_t pc;
|
2015-04-13 03:56:45 +00:00
|
|
|
// End call Fields
|
2015-03-15 16:43:05 +00:00
|
|
|
call(recipe_number r) :running_recipe(r), pc(0) {}
|
|
|
|
};
|
|
|
|
typedef stack<call> call_stack;
|
|
|
|
|
|
|
|
:(replace{} "struct routine")
|
|
|
|
struct routine {
|
|
|
|
call_stack calls;
|
2015-04-13 03:56:45 +00:00
|
|
|
// End routine Fields
|
2015-03-27 17:51:27 +00:00
|
|
|
routine(recipe_number r);
|
|
|
|
};
|
|
|
|
:(code)
|
|
|
|
routine::routine(recipe_number r) {
|
2015-03-15 16:43:05 +00:00
|
|
|
calls.push(call(r));
|
|
|
|
}
|
2015-03-17 03:26:59 +00:00
|
|
|
//: now update routine's helpers
|
2015-03-15 16:43:05 +00:00
|
|
|
:(replace{} "inline size_t& running_at(routine& rr)")
|
|
|
|
inline size_t& running_at(routine& rr) {
|
|
|
|
return rr.calls.top().pc;
|
|
|
|
}
|
|
|
|
:(replace{} "inline string recipe_name(routine& rr)")
|
|
|
|
inline string recipe_name(routine& rr) {
|
|
|
|
return Recipe[rr.calls.top().running_recipe].name;
|
|
|
|
}
|
|
|
|
:(replace{} "inline vector<instruction>& steps(routine& rr)")
|
|
|
|
inline vector<instruction>& steps(routine& rr) {
|
|
|
|
return Recipe[rr.calls.top().running_recipe].steps;
|
|
|
|
}
|
2015-03-15 05:25:06 +00:00
|
|
|
|
2015-04-13 03:47:49 +00:00
|
|
|
:(replace{} "default:" following "End Primitive Recipe Implementations")
|
2015-03-15 00:10:33 +00:00
|
|
|
default: {
|
2015-03-15 05:25:06 +00:00
|
|
|
// not a primitive; try to look for a matching recipe
|
2015-03-15 00:10:33 +00:00
|
|
|
if (Recipe.find(instructions[pc].operation) == Recipe.end()) {
|
2015-03-17 06:41:01 +00:00
|
|
|
raise << "undefined operation " << instructions[pc].operation << ": " << instructions[pc].name << '\n';
|
2015-03-15 00:10:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-03-15 16:43:05 +00:00
|
|
|
rr.calls.push(call(instructions[pc].operation));
|
2015-03-15 00:10:33 +00:00
|
|
|
continue; // not done with caller; don't increment pc
|
|
|
|
}
|
|
|
|
|
2015-03-17 03:41:12 +00:00
|
|
|
//: finally, we need to fix the termination conditions for the run loop
|
|
|
|
|
2015-03-15 16:43:05 +00:00
|
|
|
:(replace{} "inline bool done(routine& rr)")
|
|
|
|
inline bool done(routine& rr) {
|
|
|
|
return rr.calls.empty();
|
2015-03-14 07:07:44 +00:00
|
|
|
}
|
2015-03-14 08:09:20 +00:00
|
|
|
|
2015-04-13 03:47:49 +00:00
|
|
|
:(before "Running one instruction")
|
2015-03-15 16:43:05 +00:00
|
|
|
// when we reach the end of one call, we may reach the end of the one below
|
|
|
|
// it, and the one below that, and so on
|
|
|
|
while (running_at(rr) >= steps(rr).size()) {
|
2015-03-14 08:09:20 +00:00
|
|
|
rr.calls.pop();
|
2015-03-15 16:43:05 +00:00
|
|
|
if (rr.calls.empty()) return;
|
|
|
|
// todo: no results returned warning
|
|
|
|
++running_at(rr);
|
2015-03-14 08:09:20 +00:00
|
|
|
}
|
2015-04-06 19:21:18 +00:00
|
|
|
|
|
|
|
:(before "End Includes")
|
|
|
|
#include <stack>
|
|
|
|
using std::stack;
|