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 [
|
2015-05-13 17:03:26 +00:00
|
|
|
3:number <- add 2:literal, 2:literal
|
2015-03-14 00:39:32 +00:00
|
|
|
]
|
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
|
2015-05-21 19:36:59 +00:00
|
|
|
1:number <- copy 0:literal
|
|
|
|
2:number <- copy 0:literal
|
|
|
|
3:number <- copy 0:literal
|
2015-04-23 05:34:34 +00:00
|
|
|
]
|
|
|
|
recipe f [
|
2015-05-21 19:36:59 +00:00
|
|
|
4:number <- copy 0:literal
|
|
|
|
5:number <- copy 0:literal
|
2015-04-23 05:34:34 +00:00
|
|
|
]
|
2015-05-21 19:36:59 +00:00
|
|
|
+run: f
|
|
|
|
# running f
|
|
|
|
+run: 4:number <- copy 0:literal
|
|
|
|
+run: 5:number <- copy 0:literal
|
|
|
|
# back out to main
|
|
|
|
+run: 1:number <- copy 0:literal
|
|
|
|
+run: 2:number <- copy 0:literal
|
|
|
|
+run: 3:number <- copy 0:literal
|
2015-04-23 05:34:34 +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;
|
2015-05-17 09:22:41 +00:00
|
|
|
long long int running_step_index;
|
2015-04-13 03:56:45 +00:00
|
|
|
// End call Fields
|
2015-05-24 00:58:10 +00:00
|
|
|
call(recipe_number r) {
|
|
|
|
running_recipe = r;
|
|
|
|
running_step_index = 0;
|
|
|
|
// End call Constructor
|
|
|
|
}
|
2015-03-15 16:43:05 +00:00
|
|
|
};
|
2015-05-13 23:33:40 +00:00
|
|
|
typedef list<call> call_stack;
|
2015-03-15 16:43:05 +00:00
|
|
|
|
|
|
|
:(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-04-25 03:52:06 +00:00
|
|
|
const vector<instruction>& steps() const;
|
2015-03-27 17:51:27 +00:00
|
|
|
};
|
|
|
|
:(code)
|
2015-04-25 05:24:39 +00:00
|
|
|
routine::routine(recipe_number r) {
|
2015-05-13 23:33:40 +00:00
|
|
|
calls.push_front(call(r));
|
2015-04-27 01:17:39 +00:00
|
|
|
// End routine Constructor
|
2015-04-25 05:24:39 +00:00
|
|
|
}
|
2015-04-18 06:24:52 +00:00
|
|
|
|
|
|
|
//:: now update routine's helpers
|
|
|
|
|
2015-05-17 09:22:41 +00:00
|
|
|
:(replace{} "inline long long int& current_step_index()")
|
|
|
|
inline long long int& current_step_index() {
|
2015-05-13 23:33:40 +00:00
|
|
|
assert(!Current_routine->calls.empty());
|
|
|
|
return Current_routine->calls.front().running_step_index;
|
2015-03-15 16:43:05 +00:00
|
|
|
}
|
2015-04-25 03:25:33 +00:00
|
|
|
:(replace{} "inline const string& current_recipe_name()")
|
|
|
|
inline const string& current_recipe_name() {
|
2015-05-13 23:33:40 +00:00
|
|
|
assert(!Current_routine->calls.empty());
|
|
|
|
return Recipe[Current_routine->calls.front().running_recipe].name;
|
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() {
|
2015-05-13 23:33:40 +00:00
|
|
|
assert(!Current_routine->calls.empty());
|
|
|
|
return Recipe[Current_routine->calls.front().running_recipe].steps.at(Current_routine->calls.front().running_step_index);
|
2015-04-25 03:07:17 +00:00
|
|
|
}
|
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()) {
|
2015-05-06 19:13:04 +00:00
|
|
|
raise << "undefined operation " << current_instruction().operation << ": " << current_instruction().to_string() << '\n';
|
2015-03-15 00:10:33 +00:00
|
|
|
break;
|
|
|
|
}
|
2015-05-24 22:45:09 +00:00
|
|
|
Current_routine->calls.push_front(call(current_instruction().operation));
|
|
|
|
complete_call:
|
2015-05-22 01:57:25 +00:00
|
|
|
++Callstack_depth;
|
|
|
|
assert(Callstack_depth < 9000); // 9998-101 plus cushion
|
2015-04-25 03:37:55 +00:00
|
|
|
continue; // not done with caller; don't increment current_step_index()
|
2015-03-15 00:10:33 +00:00
|
|
|
}
|
|
|
|
|
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-25 03:52:06 +00:00
|
|
|
inline const vector<instruction>& routine::steps() const {
|
2015-05-13 23:33:40 +00:00
|
|
|
assert(!calls.empty());
|
|
|
|
return Recipe[calls.front().running_recipe].steps;
|
2015-04-25 03:52:06 +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-05-17 09:22:41 +00:00
|
|
|
while (current_step_index() >= SIZE(Current_routine->steps())) {
|
2015-05-22 01:57:25 +00:00
|
|
|
--Callstack_depth;
|
2015-06-01 06:44:52 +00:00
|
|
|
//? cerr << "reply " << Current_routine->calls.size() << '\n'; //? 1
|
2015-05-13 23:33:40 +00:00
|
|
|
Current_routine->calls.pop_front();
|
2015-04-25 02:58:38 +00:00
|
|
|
if (Current_routine->calls.empty()) return;
|
2015-03-15 16:43:05 +00:00
|
|
|
// todo: no results returned warning
|
2015-04-25 03:23:34 +00:00
|
|
|
++current_step_index();
|
2015-03-14 08:09:20 +00:00
|
|
|
}
|
2015-04-06 19:21:18 +00:00
|
|
|
|
|
|
|
:(before "End Includes")
|
|
|
|
#include <stack>
|
|
|
|
using std::stack;
|