2015-04-17 18:22:59 +00:00
|
|
|
//: So far the recipes we define can't run each other. Let's fix that.
|
|
|
|
|
2015-04-24 17:19:03 +00:00
|
|
|
:(scenario calling_recipe)
|
2015-03-14 00:39:32 +00:00
|
|
|
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-04-24 17:19:03 +00:00
|
|
|
:(scenario return_on_fallthrough)
|
2015-04-23 05:34:34 +00:00
|
|
|
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
|
|
|
|
|
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);
|
2015-04-25 03:00:56 +00:00
|
|
|
bool completed() const;
|
2015-03-27 17:51:27 +00:00
|
|
|
};
|
|
|
|
:(code)
|
|
|
|
routine::routine(recipe_number r) {
|
2015-03-15 16:43:05 +00:00
|
|
|
calls.push(call(r));
|
|
|
|
}
|
2015-04-18 06:24:52 +00:00
|
|
|
|
|
|
|
//:: now update routine's helpers
|
|
|
|
|
2015-04-25 02:58:38 +00:00
|
|
|
:(replace{} "inline size_t& running_at()")
|
|
|
|
inline size_t& running_at() {
|
|
|
|
return Current_routine->calls.top().pc;
|
2015-03-15 16:43:05 +00:00
|
|
|
}
|
2015-04-25 02:58:38 +00:00
|
|
|
:(replace{} "inline string recipe_name()")
|
|
|
|
inline string recipe_name() {
|
|
|
|
return Recipe[Current_routine->calls.top().running_recipe].name;
|
2015-03-15 16:43:05 +00:00
|
|
|
}
|
2015-04-25 02:58:38 +00:00
|
|
|
:(replace{} "inline vector<instruction>& steps()")
|
|
|
|
inline vector<instruction>& steps() {
|
|
|
|
return Recipe[Current_routine->calls.top().running_recipe].steps;
|
2015-03-15 16:43:05 +00:00
|
|
|
}
|
2015-04-25 03:07:17 +00:00
|
|
|
:(replace{} "inline const instruction& current_instruction()")
|
|
|
|
inline const instruction& current_instruction() {
|
|
|
|
return Recipe[Current_routine->calls.top().running_recipe].steps[Current_routine->calls.top().pc];
|
|
|
|
}
|
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-04-17 18:22:59 +00:00
|
|
|
// not a primitive; try to look up the book of recipes
|
2015-04-25 03:07:17 +00:00
|
|
|
if (Recipe.find(current_instruction().operation) == Recipe.end()) {
|
|
|
|
raise << "undefined operation " << current_instruction().operation << ": " << current_instruction().name << '\n';
|
2015-03-15 00:10:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-04-25 03:07:17 +00:00
|
|
|
Current_routine->calls.push(call(current_instruction().operation));
|
2015-03-15 00:10:33 +00:00
|
|
|
continue; // not done with caller; don't increment pc
|
|
|
|
}
|
|
|
|
|
2015-04-18 06:24:52 +00:00
|
|
|
//:: finally, we need to fix the termination conditions for the run loop
|
2015-03-17 03:41:12 +00:00
|
|
|
|
2015-04-25 03:00:56 +00:00
|
|
|
:(replace{} "inline bool routine::completed() const")
|
|
|
|
inline bool routine::completed() const {
|
|
|
|
return calls.empty();
|
2015-03-14 07:07:44 +00:00
|
|
|
}
|
2015-03-14 08:09:20 +00:00
|
|
|
|
2015-04-23 05:34:34 +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
|
2015-04-23 05:34:34 +00:00
|
|
|
//? trace("foo") << "0: " << pc << " " << &pc; //? 1
|
2015-04-25 02:58:38 +00:00
|
|
|
while (running_at() >= steps().size()) {
|
2015-04-23 05:34:34 +00:00
|
|
|
//? trace("foo") << "pop"; //? 1
|
2015-04-25 02:58:38 +00:00
|
|
|
Current_routine->calls.pop();
|
|
|
|
if (Current_routine->calls.empty()) return;
|
2015-03-15 16:43:05 +00:00
|
|
|
// todo: no results returned warning
|
2015-04-25 02:58:38 +00:00
|
|
|
++running_at();
|
2015-03-14 08:09:20 +00:00
|
|
|
}
|
2015-04-23 05:34:34 +00:00
|
|
|
//? trace("foo") << "1: " << pc << " " << &pc; //? 1
|
2015-04-06 19:21:18 +00:00
|
|
|
|
|
|
|
:(before "End Includes")
|
|
|
|
#include <stack>
|
|
|
|
using std::stack;
|